Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 For Loops l From Chapter 9 l A shorthand way of coding count loops.

Similar presentations


Presentation on theme: "1 For Loops l From Chapter 9 l A shorthand way of coding count loops."— Presentation transcript:

1 1 For Loops l From Chapter 9 l A shorthand way of coding count loops

2 2 A For Loop is a Count-Controlled Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

3 3 The for loop contains an initialization of a count variable an expression to test the count variable for continuing an update to the count variable after each iteration of the body

4 4 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { cout << num << “Potato” << endl; }

5 5 Example of Repetition num int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; OUTPUT ?

6 6 Example of Repetition num OUTPUT 1 int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl;

7 7 Example of Repetition num OUTPUT 1 int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; true

8 8 Example of Repetition num int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; OUTPUT 1 1Potato

9 9 Example of Repetition num OUTPUT 2 int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; 1Potato

10 10 Example of Repetition num OUTPUT 2 true 1Potato int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl;

11 11 Example of Repetition num int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; OUTPUT 2 1Potato 2Potato

12 12 Example of Repetition num OUTPUT 3 int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; 1Potato 2Potato

13 13 Example of Repetition num OUTPUT 3 true 1Potato 2Potato int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl;

14 14 Example of Repetition num int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; OUTPUT 3 1Potato 2Potato 3Potato

15 15 Example of Repetition num OUTPUT 4 int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; 1Potato 2Potato 3Potato

16 16 Example of Repetition num OUTPUT 4 false 1Potato 2Potato 3Potato int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl;

17 17 Example of Repetition num When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement. 4 false int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl;

18 18 The output was: 1Potato 2Potato 3Potato

19 19 A Matching While Loop int num; num = 1; // Initialization of count variable while ( num <= 3 ) // Test of count variable { cout << num << “Potato” << endl; num++; // Update of count variable }

20 20 While vs. For Loops l A for loop is a shorthand way of coding a count controlled loop l You still use while loops for event controlled loops

21 21 int count ; for ( count = 4 ; count > 0 ; count -- ) { cout << count << endl; } cout << “Done” << endl; Count-controlled Loop OUTPUT: 4 3 2 1 Done

22 22 What is output? int count; for ( count = 0 ; count < 10 ; count++ ) { cout << “  ”  ; }

23 23 OUTPUT ********** NOTE: the 10 asterisks are all on one line. Why?

24 24 What output from this loop? int count; for (count = 0; count < 10; count++) ; { cout << “  ”  ; }

25 25 l no output from the for loop! Why? l the ; right after the ( ) means that the body statement is a null statement l in general, the Body of the for loop is whatever statement immediately follows the ( ) l that statement can be a single statement, a block, or a null statement actually, the code outputs one * after the loop completes its counting to 10 OUTPUT

26 26 Several Statements in Body Block const int MONTHS = 12 ; int count ; float bill ; float sum = 0.0 ; for (count = 1; count <= MONTHS; count++ ) { cout << “Enter bill: “ ; cin >> bill ; sum = sum + bill ; } cout << “Your total bill is : “ << sum << endl ; 26


Download ppt "1 For Loops l From Chapter 9 l A shorthand way of coding count loops."

Similar presentations


Ads by Google