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 Also called “Straight-Line” Programs

4 Decision (selection, branching) sales > quota yes bonus=sales*.01 no bonus = 0 Sequential prior to if { sequential inside an if } true Sequential after an if false blockblock

5 Switch (Multiple Decision) Option=1 Option=2 Option=3 Option=4 Can always be written as nested if Assumes no multiple cases, break for each, no default

6 Repetition (iteration, looping) More? Comm = Sales*.02 Calculate Pay Print Check No Yes Sequential inside of the loop true false After the loop, continue sequentially with next statement

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

8 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

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

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

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

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

13 Simple Example Output NUMBER SQUARE CUBE ----------------------------------------------------------- 11 1 24 8 39 27 … 10 100 1000 10 100 1000

14 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; } Note: The text shows CONSTANTS in this program on page 179 * * *

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

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

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

18 Counter++ n = 5; count = 0; cout > x; max = x; cout > x; max = x;// count = 0 1while (count++ < (n-1)) { 2 { cout << “LOOP number: “ 3 cout << “LOOP number: “ cin >> x; if (max < x) 4 if (max < x) max = x; 5 max = x; } 6 } cout <<“Max is “<<max; loop countn executed 15 1 2 2 3 3 4 4 5 *

19

20 Common Use: Running Totals (Accumulator) while (count > num; total = total + num; cout > num; total = total + num; cout << “The total is “ << total; count++; } count =1; // variables must be initialized total = 0; // before loop *

21 Common Use: Average While (count <=4) { cout <<“Enter a number ”; cout <<“Enter a number ”; cin >> number; cin >> number; total += number; total += number; count++; count++;} average = total / (count-1); cout << “The average is “ << average << endl; count =1; total = 0; float average; Why count – 1?

22 Sentinel Loops – exit controlled by user Different from fixed-count loops Actually “variable-condition” loops User can be asked: “Do you want to continue?” Or, entry of unusual data value will end loop Book example: Program 5-8 on page 189 A grades program, where entry of a grade higher than 100 will exit the loop

23 Sentinel example grade = 0; total = 0; cout << “To stop, type number higher than 100”; while (grade < 100) {total = total + grade; cout << “Enter a grade: “; cin >> grade; } cout << “\nThe total of the grades is “ << total << endl;

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


Download ppt "Control Structures RepetitionorIterationorLooping Part I."

Similar presentations


Ads by Google