Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.

Similar presentations


Presentation on theme: "Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop."— Presentation transcript:

1 Chapter 7 LOOPING OPERATIONS: ITERATION

2 Chapter 7 The Flow of the while Loop

3 Chapter 7 while Loop Syntax while (test expression) { statement 1; statement 2; statement n; } //END WHILE

4 Chapter 7 DebuggingTip Remember, an infinite loop is the result of a logical error in your program. The compiler will not detect an infinite loop condition. For this reason, you should always desk- check your loop structures very closely prior to coding and execution. A little time desk-checking will save you a lot of time at the keyboard.

5 Chapter 7 The Flow of the do/while Loop

6 Chapter 7 do/while Loop Syntax do { statement 1; statement n; } //END DO/WHILE while (test expression);

7 Chapter 7 The Flow of the for Loop

8 Chapter 7 for Loop Syntax for (int counter = initial value; counter test; increment or decrement counter) { statement 1; statement 2; statement n; } //END FOR

9 Chapter 7 Tips –Never change the for loop counter within the body of the loop. –Do not place a semicolon at the end of the first line of the for statement unless you purposely want to create a time delay.

10 Chapter 7 The continue Statement When a continue statement is executed within a loop, only the current iteration is terminated and loop execution “continues” to the next iteration.

11 Chapter 7 The break Statement When a break statement is executed within a loop, the loop “breaks” and all loop iterations are terminated. Execution then passes to the next sequential statement after the loop.

12 Chapter 7  Use while whenever there is a possibility that the loop statements will not need to be executed.  Use do/while when the loop statements must be executed at least once. Use for when it can be predetermined exactly how many times the loop statements must be executed.


Download ppt "Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop."

Similar presentations


Ads by Google