Download Java Loops Often, a program will need to repeat the same set of

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Electric charge wikipedia , lookup

Electrostatics wikipedia , lookup

Transcript
Java Loops
Often, a program will need to repeat the same set of instructions over and over until a
certain condition is met. This kind of structure is called a loop.
One type of loop is called a while loop. The structure is
while( condition ) {
instructions go here
}
This repeats the instructions until the condition is met. See the following example:
import java.io.*;
// need to get Java input/output comands
public class Count {
public static void main(String[] args) throws IOException {
// throws IOException is needed because user can cause errors with bad input.
// Java requires us to declare what the program will do if that happens.
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
// creates stdin so we can read in keyboard input
System.out.println("I’ll count from minimum
to maximum by step.");
System.out.print("Minimum = ");
double min = Double.parseDouble(stdin.readLine());
System.out.print("Maximum = ");
double max = Double.parseDouble(stdin.readLine());
System.out.print("Skip = ");
double step = Double.parseDouble(stdin.readLine());
double n = min;
System.out.println(n);
while(n <= max){
n = n + step;
System.out.println(n);
}
}
}
Another type of loop is called a do while loop. The structure is
do {
instructions go here
} while( condition );
This type of loop checks at the end of the loop, after the instructions are executed.
Therefore, the instructions are always executed at least once.
Another example:
import java.io.*;
public class Total {
public static void main(String[] args) throws IOException {
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("I’ll keep adding numbers until you
tell me to stop, by entering 0.");
double sum = 0;
double n;
do {
System.out.print("Enter a number: ");
n = Double.parseDouble(stdin.readLine());
sum = sum + n;
} while(n != 0); // repeats if user didn’t enter 0
// “!=” is “not equal to” ; “==” is “equal to”
System.out.print("The sum is ");
System.out.println(sum);
}
}
1. Write a program that prints out the Fibonacci series to the number of terms the user
specifies.
a. The series starts as with 1 and 1 as the first two numbers.
b. The next number in the series is the sum of the previous two numbers.
c. Example output:
I’ll compute the Fibonacci series.
How many terms would you like? 8
1
1
2
3
5
8
13
21
Goodbye!
2. Write a program that computes the electric field from a point charge at several points
along a path.
a. First write this in 1D with the point charge at the origin (x=0). Example:
Charge = 10
Start x = 2
End x = 5
Number of intermediate points = 5
x
E
2.0
2.25e10
2.6
1.33136095e10
3.2
8.7890625e9
3.8
6.23268698e9
4.4
4.64876033e9
5.0
3.6e9
b. Then write this in 2D, where you specify the beginning and ending x and y
coordinates, and now the electric field has two coordinates as well.
c. Finally, if there is time, modify the program so that the point charge can be
specified at any position, not just at the origin.
d. Next time, we’ll program particles to move because of their mutual forces!