Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.5- The while Statement The while statement has the following syntax:

Similar presentations


Presentation on theme: "3.5- The while Statement The while statement has the following syntax:"— Presentation transcript:

1 3.5- The while Statement The while statement has the following syntax:
while ( condition ) statement; while is a reserved word 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.

2 3.5- Logic of a while Loop condition evaluated false statement true

3 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

4 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)

5 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

6 3.7 - The for Statement The for statement has the following syntax:
The initialization is executed once before the loop begins The statement is executed until the condition becomes false Reserved word for ( initialization ; condition ; increment ) statement; The increment portion is executed at the end of each iteration The condition-statement-increment cycle is executed repeatedly

7 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

8 3.7- Logic of a for loop initialization condition evaluated false
statement true increment

9 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 Counter2.java (page 161) See Stars.java (page 165) Nested loop

10 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

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


Download ppt "3.5- The while Statement The while statement has the following syntax:"

Similar presentations


Ads by Google