Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.

Similar presentations


Presentation on theme: "Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things."— Presentation transcript:

1 Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things

2 Increment and Decrement counter = counter + 1;// Add 1 and reset variable ++counter;// Prefix increment counter++;// Postfix increment counter = counter - 1; // Subtract 1 and reset variable --counter;// Prefix decrement counter--;// Postfix decrement

3 The for Loop for (int counter = 1; counter <= 10; ++counter) for (int counter = 10; counter >= 1; --counter) Count up and do something 10 times Count down and do something 10 times

4 Syntax and Semantics of the for Loop for ( ; ; ) termination statement true false initializer update Loop header Loop body

5 How It works for (int counter = 1; counter <= 10; ++counter) 1. Initialize the loop control variable before the loop runs

6 How It works for (int counter = 1; counter <= 10; ++counter) 2. Test the termination condition before running the loop body (entry control). If true, run the loop body; if false, exit the loop.

7 How It works for (int counter = 1; counter <= 10; ++counter) 3. Run the loop body.

8 How It works for (int counter = 1; counter <= 10; ++counter) 4. Update the loop control variable so it eventually will allow the loop to terminate.

9 The while Loop Write code that computes the sum of the numbers between 1 and 10. int counter = 1; int sum = 0; while (counter <= 10) { sum = sum + counter; counter = counter + 1; }

10 Syntax and Semantics of while Statements while ( ) while ( ) {. } ? statement true false

11 The do…while Loop Write code that inputs numbers (at least one) until -999 is input. int number; do { cout << "Enter a number: "; cin >> number; } while (number != -999); Uses exit control; the body of the loop runs at least once.

12 Syntax and Semantics of do… while Statements do while ( ); Do {. } while ( ); statement false ? true

13 Designing Correct Loops Initialize all variables properly –Plan how many iterations, then set the counter and the limit accordingly Check the logic of the termination condition Update the loop control variable properly

14 Off-by-One Error int counter = 1; while (counter <= 10) { // Executes 10 passes counter++; } int counter = 1; while (counter < 10) { // Executes 9 passes counter++; }

15 Infinite Loop int counter = 1; while (counter <= 10) { // Executes 5 passes counter = counter + 2; } int counter = 1; while (counter != 10) { // Runs forever counter = counter + 2; } In general, avoid using != in loop termination conditions.

16 Testing Loops Can vary the limit or the control variable, or both Use a negative value, zero, and a positive value Display an output trace if things arent working


Download ppt "Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things."

Similar presentations


Ads by Google