While Loop

Presented By Steffi Dias
© 2004 Pearson Addison-Wesley. All rights reserved 5-1

Repetition Statements
‡ Repetition statements allow us to execute a statement multiple times ‡ Often they are referred to as loops ‡ Like conditional statements, they are controlled by boolean expressions ‡ Java has three kinds of repetition statements:
± the while loop2004 Pearson Addison-Wesley. All © rights reserved ± the do loop
5-2

Outline The while Statement ‡ Syntax: while ( condition ) { statement. } © 2004 Pearson Addison-Wesley. All rights reserved 5-3 .

The while Statement ‡ If the condition is true. the statement is executed again ‡ The statement is executed repeatedly until the condition becomes false ‡ If the condition of a while loop is false initially. the statement is executed ‡ Then the condition is evaluated again. the body of a while loop will execute zero or more times © 2004 Pearson Addison-Wesley. All 5-4 rights reserved . the statement is never executed ‡ Therefore. and if it is still true.

All rights reserved 5-5 .Logic of a while Loop condition evaluated false true statement © 2004 Pearson Addison-Wesley.

Write "Go to school.<br>" ‡ If TheDay = "Saturday" Or TheDay = "Sunday" Then ‡ SchoolHolidays = True ‡ End If ‡ Wend ‡ %> © 2004 Pearson Addison-Wesley.AN EXAMPLE ‡ <% ‡ While Not SchoolHolidays ‡ Response. All rights reserved SchoolHolidays is what's known as a 5-6 .

Outline The do while Statement ‡ A do-while statement (also called a do loop) has the following syntax: do{ statement. }while ( condition ) © 2004 Pearson Addison-Wesley. All rights reserved 5-7 .

All rights reserved 5-8 .The do-while Statement ‡ The statement is executed once initially. and then the condition is evaluated ‡ The statement is executed repeatedly until the condition becomes false ‡ The body of a do loop executes at least once © 2004 Pearson Addison-Wesley.

Logic of a do-while Loop statement true condition evaluated false © 2004 Pearson Addison-Wesley. All rights reserved 5-9 .

All ‡ %> rights reserved 5-10 .‡ <% ‡ Do While Not SchoolHolidays ‡ Response.Write "Go to school.<br>" ‡ If TheDay = "Saturday" Or TheDay = "Sunday" Then ‡ SchoolHolidays = True ‡ End If ‡ Loop © 2004 Pearson Addison-Wesley.

All rights reserved 5-11 .Comparing while and do The while Loop The do Loop statement condition evaluated false true condition evaluated false true statement © 2004 Pearson Addison-Wesley.

Outline The for while Statement ‡ A for statement has the following syntax: The initialization is executed once before the loop begins The statement is executed until the condition becomes false for ( initialization . All rights reserved 5-12 . } The increment portion is executed at the end of each iteration © 2004 Pearson Addison-Wesley. condition . increment ){ statement.

the body of a for loop will execute zero 2004 Pearson Addison-Wesley.The for Statement ‡ The initialization section can be used to declare a variable ‡ Like a while loop. All © or more times rights reserved 5-13 . the condition of a for loop is tested prior to executing the loop body ‡ Therefore.

All rights reserved 5-14 .Logic of a for loop initialization condition evaluated true statement increment false © 2004 Pearson Addison-Wesley.

All rights reserved 5-15 .The for Statement ‡ A for loop is functionally equivalent to the following while loop structure: initialization. } © 2004 Pearson Addison-Wesley. while ( condition ){ statement. increment.

we generally go to school. All School'sreserved out! rights 5-16 ..Write "Go to school. from age 5 to 16. <% We could write this using a For loop as For follows: i = 5 To 16 Response. You are 15. Go to school. You are 16. © 2004 Pearson Addison-Wesley. Go to school. Go to school.<br>" Next Response.Write "School's out!" %> This would produce output like this: You are 5. ." ‡ Response. Go to school.‡ To use another real-world example.. You are 6.Write "You are " & i & ".

println (num). } ‡ A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance © 2004 Pearson Addison-Wesley. num > 0. num -= 5){ System. All rights reserved 5-17 .The for Statement ‡ The increment section can perform any for (int calculationnum=100.out.

All rights reserved 5-18 . it is always considered to be true.The for Statement ‡ Each expression in the header of a for loop is optional ‡ If the initialization is left out. and therefore creates an infinite loop ± We usually call this a ³forever loop´ © 2004 Pearson Addison-Wesley. no initialization is performed ‡ If the condition is left out.

Infinite Loops ‡ The body of a while loop eventually must make the condition false ‡ If not. which will execute until the user interrupts the program ‡ This is a common logical (semantic) error ‡ You should always double check the logic 5-19 of a program © 2004ensure that your loops will to Pearson Addison-Wesley. it is called an infinite loop. All rights reserved terminate normally .

while (count <= 25){ System.println (count). All rights reserved 5-20 . count = count .out. } ‡ This loop will continue executing until interrupted (Control-C) or until an underflow error occurs © 2004 Pearson Addison-Wesley.1.Infinite Loops ‡ An example of an infinite loop: int count = 1.

All rights reserved 5-21 .break revisited ‡ Remember the break keyword that we used to stop a switch statement from executing more than one statement? ‡ break can also be used to exit an infinite loop ‡ But it is almost always best to use a wellwritten while loop © 2004 Pearson Addison-Wesley.

the inner loop iterates completely ‡ Your second course project involves a © 2004 Pearson inside All while loop nested Addison-Wesley. the body of a loop can contain another loop ‡ For each iteration of the outer loop. loops can be nested as well ‡ That is.Nested Loops ‡ Similar to nested if statements.of a for loop rights reserved 5-22 .

Nested Loops ‡ How many times will the string "Here" be printed? count1 = 1. } count1++. All rights reserved 5-23 . while (count2 <= 20) { System. count2++.println ("Here"). while (count1 <= 10){ count2 = 1. } 10 * 20 = 200 © 2004 Pearson Addison-Wesley.out.

Sign up to vote on this title
UsefulNot useful