Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures Looping Part 2 Part I The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute.

Similar presentations


Presentation on theme: "Control Structures Looping Part 2 Part I The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute."— Presentation transcript:

1

2 Control Structures Looping Part 2 Part I

3 The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute (compound) statement b)Evaluate all altering expressions(s) c)Repeat until expression is false Syntax for (initialize; expression; altering list) statement

4 The for Statement Syntax for (initialize; expression; alter) statement cnt=1 Example: for (cnt=1; cnt < 7; cnt++) { cout << “Yankees are #1\n”; cout << “They’ll win the Series!\n”; } next statement *

5 The for Statement ã used as a counting loop (up or down) ã semicolons separate the items ã items 1 and 2 are statements (end with ;) ã you may declare variable and initialize it ex. for (int cnt = 1; cnt < 7; cnt++) (not recommended by instructor) ã performs the same functions as the while

6 int num; cout << "NUMBER SQUARE CUBE\n" << "------ ------ ----\n"; for (num = 1; num < 11; num++) { cout << setw(3) << num << " " << setw(3) << num * num << " " << setw(4) << num * num * num << "\n"; } for (num = 1; num < 11; num++) { cout << setw(3) << num << " " << setw(3) << num * num << " " << setw(4) << num * num * num << "\n"; } A Simple Example *

7 for Statement Examples for Statement Examples for (k = 1; k <= n; k = k + 1) x = k * k; * k = 12; for (j = 2; k % j == 0; j = j + 1) { cout << j << “ is a divisor of “ << k << ‘\n’; sum = sum + j; } n = 2; // perhaps as input by user

8 Not Valid: for (j = 0, j < n, j = j + 3) // semicolons needed for (j = 0; j < n ) // three parts needed //or at least a semicolon for Statement Examples for Statement Examples

9 What goes in the for statement? Initialization: a comma separated list of assignments to execute before even testing for entry Expression: any expression that has a true/false (numeric) result Update: any expression to be evaluated at the end of the loop (after executing the statements inside the loop) before testing for loop exit. “any” may be null or a comma separated list

10 for -- Null Expressions for -- Null Expressions Example 1:j = 1; sum = 0; for ( ; j <= 10; j = j + 1) sum = sum + j; Example 2:j = 1; sum = 0; for ( ; j <= 10; ) sum = sum + j; * * * Do not leave out the expression or you may have an infinite loop

11 Nested Loops When a decision or control structure is contained within another decision or control sturcture, the inner one is said to be nested. * while... if... while... You may have repetition within decision and vice versa.

12 Example int y,z; for (y = 5; y > 0; y--) {cout << "\nAli R."; cout << “**”; } for (z = 1; z < 3; z++) cout <<"\tVic R.\t"; Nested Loops - Ex.1 *

13 Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Nested Loops - Ex.1 Output Ali R. Vic R.Vic R.** *

14 1 int y,z; 2 for (y = 5; y > 0; y--) 3 {cout << "\nAli R."; 4 for (z = 1; z < 3; z++) 5 cout <<"\tVic R.\t"; 6 cout << “**“; 7 } Nested Loops - Ex.1 Execution 1 2 3 4 5 4 5 4 6 2 3 4 5 4 5 4 6 2 3 4 5 4 5 4 6 2 3 4 5 4 5 4 6 2 3 4 5 4 5 4 6 2 7 *

15 Example 2 Write a program that determines the average score for each of four experiments Each experiment includes six trials Read a score for each trial and print the average for each experiment for each experiment for each trial read score accumulate scores calculate and print averages Even a simple program deserves an algorithm

16 4 for (trial = 1; trial > score; 7 total = total + score; 8 } 9 avg = total/(trial-1); 10 cout << "Average for experiment "<< exper 11 << " is “<< setprecision(4)<< avg<< "\n\n"; 12 } Nested Loops - Ex. 2 * 1 for (exper =1; exper<=4; exper=exper +1) 2 { cout << "\tScores for experiment "<< exper <<":\n"; 3 total = 0.0;

17 for vs. while cnt = 1cnt++ for (cnt = 1; cnt < 7; cnt++) { cout << … } cnt = 1; while (cnt < 7) { cout << … cnt++; cnt++;} *

18 Bowling Team Example A bowling team consists of five players each of whom bowls three games. Write a program that allows the user to enter each players scores and displays the players average. The team average is displayed at the end. First write a plan.

19 Bowling Team Plan display instructions for - players for - games I/O scores sum scores average scores display scores sum player averages team average display team average

20 Common Errors Improper braces in nested structures Using = in place of == != versus == This changes the logic, be especially careful when used with && or || infinite loops: != versus && versus || *

21 The End - part 2 The End


Download ppt "Control Structures Looping Part 2 Part I The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute."

Similar presentations


Ads by Google