1 / 20

Control Structures

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)

sukey
Download Presentation

Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures • Looping Part 2 • Part I

  2. 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

  3. 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 *

  4. 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

  5. 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";} *

  6. 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;} *

  7. 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

  8. 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

  9. 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 * * *

  10. 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. * *

  11. 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"; *

  12. 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. ** *

  13. 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 } *

  14. 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

  15. 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 } * *

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

  17. 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.

  18. 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

  19. 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 || *

  20. 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

More Related