200 likes | 295 Views
Control Structures. Looping Part 2 Part I. The C++ for Statement. Syntax for ( initialize ; expression ; altering list) statement. Meaning: Evaluate all initialization expressions Evaluate expression , if true: Execute (compound) statement Evaluate all altering expressions(s)
E N D
Control Structures • Looping Part 2 • Part I
The C++ for Statement Syntax for (initialize; expression; altering list)statement • Meaning: • Evaluate all initialization expressions • Evaluate expression, if true: • Execute (compound) statement • Evaluate all altering expressions(s) • Repeat until expression is false
The for Statement • Syntax for (initialize; expression; alter)statement Example: for (cnt=1; cnt < 7; cnt++) {cout << “Yankees are #1\n”; cout << “They’ll win the Series!\n”; }next statement *
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
A Simple Example 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 Statement Examples n = 2; // perhaps as input by user • 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;} *
for Statement Examples • 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
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
Example 2: j = 1; sum = 0; for ( ; j <= 10; ) sum = sum + j; for -- Null Expressions • Example 1: j = 1; sum = 0; for ( ; j <= 10; j = j + 1) sum = sum + j; Do not leave out the expression or you may have an infinite loop * * *
if ... while... while ... while ... Nested Loops When a decision or control structure is contained within another decision or control sturcture, the inner one is said to be nested. You may have repetition within decision and vice versa. * *
Nested Loops - Ex.1 • 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 Output 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 Execution1 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 • 1 int y,z; • 2for (y = 5; y > 0; y--) • 3 { cout << "\nAli R."; • 4for (z = 1; z < 3; z++)5 cout <<"\tVic R.\t"; • 6 cout << “**“;7 } *
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
Nested Loops - Ex. 2 1 for (exper =1; exper<=4; exper=exper +1)2{ cout << "\tScores for experiment "<< exper <<":\n";3 total = 0.0; • 4 for (trial = 1; trial <=6; trial = trial +1)5 { cout << "Enter result of trial " << trial <<" : ";6 cin >> score;7 total = total + score;8 } • 9 avg = total/(trial-1);10 cout << "Average for experiment "<< exper11 << " is “<< setprecision(4)<< avg<< "\n\n";12 } * *
for vs. while • cnt = 1; • while (cnt < 7) • { cout << … • cnt++; • } for (cnt = 1; cnt < 7; cnt++) {cout << … } *
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.
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
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 || *
The End - part 2 • The End The End The End The End The End The End The End The End The End The End The End The End