Outline Altering flow of control Boolean expressions Conditional
Outline Altering flow of control Boolean expressions Conditional Statements The while Statement Other Repetition Statements
Repetition statements l l A repetition statement l allows us to execute a statement multiple times l Often is referred to as loop Like conditional statements l l They are controlled by Boolean expressions Java has three types of repetition statements l The while, do, and for loops
The while statement l A while statement has the following syntax while ( condition ) statement; l If the condition is true, the statement is executed l Then the condition is evaluated again l l And if it is still true, the statement is executed again The statement is executed repeatedly l Until the condition becomes false
Logic of a while Loop condition evaluated true statement false
The while statement: example l An example of a while statement int count = 1; while (count <= 5) { System. out. println (count); count++; } l If the condition of a while loop is false initially l l the statement is never executed Therefore, the body of a while loop will execute l Zero or more times
Use of a while loop l Sample program l l l Compute the average of integer values Algorithm l Read a series of integer values from the user l Sum them up l Compute their average See (Average. java)
Infinite Loops l The body of a while loop l l eventually must make the condition false If not, it is called an infinite loop, l which will execute until the user interrupts the program l This is a common logical error l You should always l double check the logic of a program l to ensure that your loops will terminate normally 7
Infinite Loops l An example of an infinite loop: int count = 1; while (count <= 25) { System. out. println (count); count = count - 1; } l This loop will continue executing l until interrupted (Control-C) or l until an underflow error occurs
Nested Loops l Similar to nested if statements, l loops can be nested as well l That is, the body of a loop can contain another loop l For each iteration of the outer loop, the inner loop iterates completely
Nested Loops l How many times will the string "Here" be printed? count 1 = 1; while (count 1 <= 10) { count 2 = 1; while (count 2 <= 20) { System. out. println ("Here"); count 2++; } 10 * 20 = 200 count 1++; }
break, and continue keywords l break placed in the body of any loop l The execution of the loop is stopped, and l the statement following the loop is executed l However => not a good practice l int count = 0; while (count < 5) { System. out. println(count); count++; break; }
break, and continue keywords l continue placed in the body of any loop l The loop condition is evaluated again, and l the loop body is executed again if it is till true l int count = 0; while (count <=5) { if (count == 4) { count++; continue; } System. out. println(count); count++; }
Outline Altering flow of control Boolean expressions Conditional Statements The while Statement Other Repetition Statements
The do Statement l A do statement has the following syntax: do { statement; } while ( condition ) l The statement l l is executed once initially, then the condition is evaluated The statement is executed repeatedly l until the condition becomes false
Logic of a do loop statement true condition evaluated false
The do statement: example l An example of a do loop int count = 0; do { count++; System. out. println (count); } while (count < 5); l The body of a do loop executes at least once l See Reverse. Number. java
Comparing while and do The while Loop The do Loop statement condition evaluated true statement true false condition evaluated false
Outline Altering flow of control Boolean expressions Conditional Statements The while Statement Other Repetition Statements
The for Statement l An example of a for loop: for (int count=1; count <= 5; count++) System. out. println (count); l The initialization section l l Like a while loop, the condition of a for loop l l can be used to declare a variable is tested prior to executing the loop body Therefore, the body of a for loop l will execute zero or more times
The for Statement l 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 ; condition ; increment ) statement; The increment portion is executed at the end of each iteration
Logic of a for loop initialization condition evaluated true statement increment false
The for Statement l A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }
The for Statement l The increment section can perform any calculation for (int num=100; num > 0; num -= 5) System. out. println (num); l A for loop is well suited l for executing statements a specific number of times l that can be calculated or determined in advance l See Multiples. java l See Stars. java
The for Statement l Each expression in the header l l If the initialization is left out, l l no initialization is performed If the condition is left out, it is always considered to l l of a for loop is optional be true, and therefore creates an infinite loop If the increment is left out, l no increment operation is performed
Comparing loops l The 3 loops are equivalent l l l i. e. , any loop written using one type can be written using the other two loop types Usage l Use for when the number of iterations is fixed l To execute the loop body at least once, Use do l Use while for loops executed 0 or more times
- Slides: 25