CiS 260: App Dev I Chapter 4: Control Structures II
The while Loop (Repetition) You often need to repeat the same statements in a program many times. Repetition or __________ structures allow this. The Java while statement has this syntax while( expression ) statement ; The expression, called a loop _________, is the decision-maker and is either true or false. The statement is called the ______ of the loop. The statement executes continuously while the expression is ______. Watch out for _________ loops.
Example The output is ____________ F expression statement i = 0 while( i <= 20 ) { System.out.print( i + “ ” ); i = i + 5; } The output is ____________ The variable i is called the loop ___________ variable. When i reaches the value of ____, flow leaves the loop.
Counter-Controlled while Loops If you know in advance how many times a loop is to execute, you can use a ________ variable. final int N = 4; int counter = 0; while( counter < N ) { System.out.print( “Hello ” ); counter++; } The output is ___________________________
Sentinel-Controlled while Loops If you don’t know in advance how many times a loop is to execute, you can use a ________. final int SENTINEL = -999; int number; while( number != SENTINEL ) { number = Integer.parseInt(keyboard.readLine()); System.out.println( number + “ ” ); } The output is the series of numbers typed by the user until the user types _______.
Other while Loops Flag-controlled while loops A _________ variable controls the loop boolean found = false; while( ! Found ) { System.out.println( “Did you find it?” ); answer = System.in.read(); if( answer == ‘y’ || answer == ‘Y’ ) found = true; } EOF-Controlled while loops If reading data from a file, a loop can test each line of data read. When the line doesn’t exist (EOF), the loop stops.
The for Loop (Repetition) The while loop is used for any kind of repetition. The for loop is equivalent to a counter-controlled __________ loop. The syntax is for ( initial statement; loop condition; increment statement ) statement ; The initial statement is executed only once. The loop condition is then evaluated. If _____, the loop statement (block) is executed and the increment statement is executed. Then the previous step is repeated.
Examples of for Loops The output “Hello everyone” appears __ times. for( i = 1; i <= 5; i++ ) { System.out.print( “Hello” ); System.out.println( “ everyone” ); } The output “Hello everyone” appears __ times. Without the braces, “ everyone” appears _____. int N = 5, sum = 0; for( counter = 1; counter <= N; counter++ ) sum = sum + counter; System.out.println( sum ); The output of the previous statements is _____.
The do…while Loop (Repetition) The syntax of the do…while statement is do statement while ( expression ) ; The expression is called the _____ condition. The statement is always executed at least once. Then the expression is evaluated. If true, the statement is executed again, and so on.
Example of do…while The output from the above is ______________. { System.out.print(i + “ ”); i = i + 5; } while( i <= 20); The output from the above is ______________.
break and continue break is used to Skip the remainder of the switch structure Exit early from a loop continue causes the remaining statements in the loop to be skipped and continues with the next iteration.
Nested Control Structures Nesting is placing control structures (such as if…else) within control structures (such as while). One author states, “…nesting of control structures provides new power, subtlety, and complexity.”