Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures RepetitionorIterationorLooping Part I.

Similar presentations


Presentation on theme: "Control Structures RepetitionorIterationorLooping Part I."— Presentation transcript:

1

2 Control Structures RepetitionorIterationorLooping Part I

3 Sequence Print out a sales report Open the salesperson file Print heading on form Skip 3 lines Read the first record Print salesperson’s name

4 Decision (selection, branching) sales > quota yes bonus=sales*.01 no bonus = 0

5 Switch (Multiple Decision) Option=1 Option=2 Option=3 Option=4

6 Repetition (iteration, looping) More? Comm = Sales*.02 Calculate Pay Print Check No Yes

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

8 The for Statement ã used as a counting loop ã semicolons separate the items ã items 1 and 2 are statements (end with ;) ã your may declare variable and initialize it ex. for (int cnt = 1; cnt < 7; cnt++) ã performs the same functions as the while

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

10 for Statement Examples for Statement Examples for (k = 1; k <= n; k = k + 1) k = k * k; * for (j = 2; k % j == 0; j = j + 1) { cout << j << “ is a divisor of “ << k << ‘\n’; sum = sum + j; }

11 Not Valid: for (j = 0, j < n, j = j + 3) // semicolons needed for (j = 0; j < n) // three parts needed for Statement Examples for Statement Examples

12 for -- Null Expressions for -- Null Expressions Example 1:j = 1; sum = 0; for ( ; j <= 10; j = j + 1) sum = sum + j; Example 3:j = 1; sum = 0; for ( ; ; ) {sum = sum + j; j++; cout << "\n" << sum; } Example 2:j = 1; sum = 0; for ( ; j <= 10; ) sum = sum + j; * * *

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

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

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

16 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 *

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

18 The while Statement Syntax while (expression) statement Example: count = 1; while (count <= 10) { cout << “Yankees are #1\n”; count = count + 1; } next statement *

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

20 The while Statement Syntax while (expression) statement ã a loop control variable is evaluated in the expression ã the loop statements contain the lines executed each time the loop repeats

21 The while Statement loop Exit the while 0 or False Test the expression statements to execute 1 or True

22 Something to Note Note Note... count = 1; while (count <= 10) { cout << “Yankees are #1\n”; cout << “and will win the series\n”; } next statement *

23 The while Statement ã loop control variable is initialized before while statement ã evaluation or test is performed within the expression ã the body may contain any number of statements, including branches and other loops ã the control variable is changed during loop execution in order to exit loop ã the statement immediately after the while is executed upon exiting * * * * *

24 int num; cout << "NUMBER SQUARE CUBE\n" << "------ ------ ----\n"; A Simple Example num = 1; while (num < 11) { cout << setw(3) << num << " " << setw(3) << num * num << " " << setw(4) << num * num * num <<‘\n’; num++; // increment num } * * *

25 double celsius, fahren; cout << "CELSIUS FAHRENHEIT\n" << "------- ----------\n"; Another Example celsius = 5; // starting Celsius value while (celsius <= 50) { fahren = (9.0/5.0) * celsius + 32.0; cout << setw(4) << celsius << setiosflags(ios::showpoint) << setw(13) << setprecision(2) << fahren << '\n'; celsius = celsius + 5; } * * *

26 year = 1; deprec = 4000; endyr = 28000; accum_depreci = 0; while (year <= 7) { endyr = endyr - deprec; accum_depreci = accum_depreci + deprec; cout << year << deprec << endyr << accum_depreci << "\n"; year = year + 1; } year = year + 1; } Still Another Example * * *

27 Problem Solving: Finding the Largest Value â The program asks for the number of items in the list. â Checks to see if that number is positive. â Gets user input. â Assigns the largest to variable max. * *

28 int count = 0, n = 0; double max = 0, x = 0; int count = 0, n = 0; double max = 0, x = 0; cout << "The maximum value will be computed.\n"; cout > n; while (n > n; } cout > x; max = x;// first value to max * *

29 while (count++ > x; if (max > x; if (max < x) max = x; } cout << “Maximum value: “ << max << “\n”; } Output The maximum value will be computed. How many numbers do you wish to enter? 4 Enter a real number: 1.01 Enter a real number: -3 Enter a real number: 2.2 Enter a real number: 7.07000 Maximum value: 7.07 * * *

30 Closer look at a counter: cout > x; max = x; cout > x; max = x;// count = 0 while (count++ < (n-1)) { 1 while (count++ < (n-1)) 2 { cout << “LOOP number: “ 3 cout << “LOOP number: “ cin >> x; if (max > x; 5 if (max < x) 6 max = x; 7 } loop countn executed 15 1 2 2 3 3 4 4 5 *

31 Running Totals while (count > num; total = total + num; cout > num; total = total + num; cout << “The total is “ << total; count++; } // count =1 *

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

33 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

34 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 || *

35 The End - part I The End


Download ppt "Control Structures RepetitionorIterationorLooping Part I."

Similar presentations


Ads by Google