Post on 17-Dec-2015
transcript
Loops
For
While
Do While
Loops
Used to repeat something
Loop statement creates and controls the loop
while
Requires a conditional test at the beginning
Runs the conditional test each time the loop runs
If the test is false at the beginning the loop statement will be ignored
Variables are set up before the loop statement
while
Example
while (gameLives > 0) {
//the statements inside the loop go here
Loop continues until gameLives variable is no longer greater than 0
while
Example
int limit = 5;
int count = 1;
while (count < limit) {
System.out.println (“Pork is not a verb”);
count ++;
}
How many times is the line printed?
5
while
int limit = 5
int count = 6
How many times will the same loop continue with these variables?
0
do-while
Similar to the while loop but the conditional test is placed after the statements inside of the loop
The statements between the do and while are handled automatically, the while condition is tested to determine if the loop will run again
The change of condition has to take place in the do and while for the loop to end
do- while
int limit = 5;
int count = 1;
do {
System.out.println (“Pork is not a verb”);
count ++;
} while (count < limit);
do - while
The print line will run 4 times using this example,
If we change the count to an initial value of 6 it will run 1 time (different than the 0 times when just used in the while loop)
while or do-while??
Strategy 1 (do-while) Borrow the car first and tell someone later that
you did
Strategy 2 (while) Ask permission before you borrow the car
for
The most complex of the loop statements
Has 3 values set up between the ( )
Can use a variable to set the number of times that a loop will be repeated
for
Examplefor (int p = 0; p < 500; p ++) {
System.out.println (“Pork is not a verb.”);
}
Brackets are not necessary with a 1 line statement but it makes it easier to understand in comparison to the other loops
for
for (counter, condition, change)
Exiting a loop
W hen condition becomes false (all 3)
To end a loop immediately without the condition becoming false use a break statement (all 3)
To send a loop back to the beginning use continue (all 3)
Using loops together
Example while; for; breakint points = 0;int target = 100;while (target <= 100){
for (int x =0; x < target; x ++) {if (points > 50)break;
points = points + x;}
}
Example explained
break statement causes the for loop to end is the variable is greater than 50, the while loop will continue because the target is never greater than 100
To end the while loop it must be named, Put the name on the line before the beginning
of the loop and follow it with a colon : use the name after break to indicate what break /continue applies to…
Example of while; for; break; nameint points = 0;int target = 100;targetloop:while (target <= 100){
for (int x =0; x < target; x ++) {if (points > 50)break targetloop;
points = points + x;}
}Now both loops will end
Example while; for; continueint points = 0;
int target = 100;
while (target <= 100){
for (int x =0; x < target; x ++) {
if (points == 50)
continue;
points = points + x;
}
}
Questions
What must be used to separate each section of a for statement? A) Commas B) Semicolon C) Colon
Which statement causes a program to go back to the statement that began a loop and then keep going from there? A) continue B) break C) naming
Answer to the word problem
import java.util*/;class cameraint x;long sensor,
camera;while (sensor <=6){
for (int x = 0; x >sensor; x ++) {if (sensor < 6)
System.out.println (“Calling 585-232-0988, you have an intruder”);}
}
Now that you know,
Go back to your class repeat program and exchange the while loop that is used with a for loop (run it, compile & turn in)
Note: using a semicolon with a for loop will not necessarily cause an error but the loop will not run correctly
for (int x = 0; x < 100; x ++); {
Now try this
class Nines{public static void main (String[ ] args){
for (int dex = 1; dex <= 200; dex ++) {
int multiple = 9* dex;
System.out.print (multiple + “ “);
}
}
}