3 5 The while Statement The while statement

  • Slides: 11
Download presentation
3. 5 - The while Statement • The while statement has the following syntax:

3. 5 - The while Statement • The while statement has the following syntax: while is a reserved word while ( condition ) statement; If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repeatedly until the condition becomes false. 1

3. 5 - Logic of a while Loop condition evaluated true statement 2 false

3. 5 - Logic of a while Loop condition evaluated true statement 2 false

3. 5 - The while Statement • Note that if the condition of a

3. 5 - The while Statement • Note that if the condition of a while statement is false initially, the statement is never executed • Therefore, the body of a while loop will execute zero or more times • See Counter. java (page 147) • See Average. java (page 148) • A sentinel value indicates the end of the input (value != 0) • The variable sum maintains a running sum 3

3. 5 - Infinite Loops • The body of a while loop eventually must

3. 5 - Infinite Loops • The body of a while loop eventually must make the condition false • If not, it is an infinite loop, which will execute until the user interrupts the program • This is a common logical error • You should always double check to ensure that your loops will terminate normally to avoid JCreator crashing • See Forever. java (page 152) 4

3. 5 - Nested Loops • Similar to nested if statements, loops can be

3. 5 - Nested Loops • Similar to nested if statements, loops can be nested as well • That is, the body of a loop can contain another loop • Each time through the outer loop, the inner loop goes through its full set of iterations 5

3. 7 - The for Statement • The for statement has the following syntax:

3. 7 - The for Statement • The for statement has the following syntax: Reserved word 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 The condition-statement-increment cycle is executed repeatedly 6

3. 7 - The for Statement • Each expression in the header of a

3. 7 - The for Statement • Each expression in the header of a for loop is optional • If the initialization is left out, no initialization is performed • If the condition is left out, it is always considered to be true, and therefore creates an infinite loop • If the increment is left out, no increment operation is performed • Two semi-colons are always required in the for loop header 7

3. 7 - Logic of a for loop initialization condition evaluated true statement increment

3. 7 - Logic of a for loop initialization condition evaluated true statement increment 8 false

3. 7 - The for Statement • Like a while loop, the condition of

3. 7 - The for Statement • Like a while loop, the condition of a for statement is tested prior to executing the loop body • Therefore, the body of a for loop will execute zero or more times • It is well suited for executing a loop a specific number of times that can be determined in advance • See Counter 2. java (page 161) • See Stars. java (page 165) 9 Nested loop

3. 7 - Choosing a Loop Structure • When you can’t determine how many

3. 7 - Choosing a Loop Structure • When you can’t determine how many times you want to execute the loop body, use a while statement • If you can determine how many times you want to execute the loop body, use a for statement 10

ASSIGNMENT 2 Textbook Problems MC: 4 -8 TF: 7 SA: 7 -15 11

ASSIGNMENT 2 Textbook Problems MC: 4 -8 TF: 7 SA: 7 -15 11