Ci S 260 App Dev I The while

  • Slides: 12
Download presentation
Ci. S 260: App Dev I

Ci. S 260: App Dev I

The while Loop (Repetition) n n n You often need to repeat the same

The while Loop (Repetition) n n n 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 ; n The expression, called a loop _____, is the decision-maker and is either true or false. n The statement is called the ______ of the loop. The statement executes continuously while the expression is ______. 2 Watch out for _____ loops. n n

Example T statement i = 0 F while( i <= 20 ) { System.

Example T statement i = 0 F while( i <= 20 ) { System. out. print( i + “ ” ); i = i + 5; } expression n The output is ______ The variable i is called the loop ______ variable. When i reaches the value of ____, flow leaves the loop. 3

Counter-Controlled while Loops n If you know in advance how many times a loop

Counter-Controlled while Loops n 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++; } n The output is ______________ 4

Sentinel-Controlled while Loops n If you don’t know in advance how many times a

Sentinel-Controlled while Loops n 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. parse. Int(keyboard. read. Line()); System. out. println( number + “ ” ); } n The output is the series of numbers typed by the user until the user types _______. 5

Other while Loops n Flag-controlled while loops Ø A _____ variable controls the loop

Other while Loops n 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; } n EOF-Controlled while loops Ø If reading data from a file, a loop can test each line of data read. 6 Ø When the line doesn’t exist (EOF), the loop stops.

The for Loop (Repetition) n n n The while loop is used for any

The for Loop (Repetition) n n n The while loop is used for any kind of repetition. The for loop is equivalent to a countercontrolled _____ loop. The syntax is for ( initial statement; loop condition; increment statement ) statement ; n n n 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. 7 Then the previous step is repeated.

Examples of for Loops for( i = 1; i <= 5; i++ ) {

Examples of for Loops for( i = 1; i <= 5; i++ ) { System. out. print( “Hello” ); System. out. println( “ everyone” ); } n n 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 ); n The output of the previous statements is _____. 8

The do…while Loop (Repetition) n The syntax of the do…while statement is do statement

The do…while Loop (Repetition) n The syntax of the do…while statement is do statement while ( expression ) ; n n n 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. 9

Example of do…while i = 0; do { System. out. print(i + “ ”);

Example of do…while i = 0; do { System. out. print(i + “ ”); i = i + 5; } while( i <= 20); n The output from the above is _______. 10

break and continue n break is used to Ø Skip the remainder of the

break and continue n break is used to Ø Skip the remainder of the switch structure Ø Exit early from a loop n continue causes the remaining statements in the loop to be skipped and continues with the next iteration. 11

Nested Control Structures n Nesting is placing control structures (such as if…else) within control

Nested Control Structures n Nesting is placing control structures (such as if…else) within control structures (such as while). n One author states, “…nesting of control structures provides new power, subtlety, and complexity. ” 12