Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the...

22
Loops For While Do While

Transcript of Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the...

Page 1: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

Loops

For

While

Do While

Page 2: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

Loops

Used to repeat something

Loop statement creates and controls the loop

Page 3: 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

Page 4: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

while

Example

while (gameLives > 0) {

//the statements inside the loop go here

Loop continues until gameLives variable is no longer greater than 0

Page 5: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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

Page 6: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

while

int limit = 5

int count = 6

How many times will the same loop continue with these variables?

0

Page 7: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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

Page 8: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

do- while

int limit = 5;

int count = 1;

do {

System.out.println (“Pork is not a verb”);

count ++;

} while (count < limit);

Page 9: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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)

Page 10: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the 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

Page 11: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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

Page 12: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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

Page 13: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

for

for (counter, condition, change)

Page 14: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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)

Page 15: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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;}

}

Page 16: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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…

Page 17: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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

Page 18: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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;

}

}

Page 19: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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

Page 20: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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”);}

}

Page 21: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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 ++); {

Page 22: Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.

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 + “ “);

}

}

}