230 likes | 338 Views
Control Structures. Repetition or Iteration or Looping Part I. Open the salesperson file. Print heading on form. Skip 3 lines. Read the first record. Print salesperson’s name. Sequence Print out a sales report. Also called “Straight-Line” Programs. yes. no. sales > quota.
E N D
Control Structures • Repetition • or • Iteration • or • Looping • Part I
Open the salesperson file Print heading on form Skip 3 lines Read the first record Print salesperson’s name SequencePrint out a sales report Also called “Straight-Line” Programs
yes no sales > quota bonus = 0 bonus=sales*.01 Decision (selection, branching) Sequential prior to if { sequential inside an if } block true false Sequential after an if
Option=1 Option=2 Option=3 Option=4 Switch (Multiple Decision) Can always be written as nested if Assumes no multiple cases, break for each, no default
No More? Yes Comm = Sales*.02 Calculate Pay Print Check Repetition (iteration, looping) false Sequential inside of the loop true After the loop, continue sequentially with next statement
The while Statement • Syntax while (expression) statement Example: count = 1; while (count <= 10) { cout << “Yankees are #1\n”; count = count + 1; } next statement *
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
0 or False Exit the while Test the expression 1 or True loop statements to execute The while Statement
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? *
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 * * * * *
A Simple Example • int num;cout << "NUMBER SQUARE CUBE\n" << "------ ------ ----\n"; num = 1; while (num < 11){ cout << setw(3) << num << " " << setw(3) << num * num << " " << setw(4) << num * num * num <<‘\n’; num++; // increment num} * * *
Simple Example Output • NUMBER SQUARE CUBE • ----------------------------------------------------------- • 1 1 1 • 2 4 8 • 3 9 27 • … • 10 100 1000
Another Example • double celsius, fahren;cout << "CELSIUS FAHRENHEIT\n" << "------- ----------\n"; 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 * * *
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. * * * *
int count = 0, n = 0; double max = 0, x = 0; • cout << "The maximum value will be computed.\n"; • cout << "How many numbers do you wish to enter? "; cin >> n; • while (n <= 0) { cout << "\nERROR: Positive integer required.\n\n”; cout << "How many numbers to enter? "; cin >> n; } cout << “Enter a real number: “;cin >> x;max = x; // first value to max * * * *
while (count++ < (n-1)) // first n accepted above{ cout << “Enter a real number: “; cin >> x; if (max < x) max = x;} • cout << “Maximum value: “ << max << “\n”;} • OutputThe 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 * * *
n = 5; • count = 0; • cout << “Enter “ << n << … cin >> x;max = x;// count = 0 • while (count++ < (n-1)) • { • 3cout << “LOOP number: “ • cin >> x; • if (max < x) • max = x; • } • cout <<“Max is “<<max; Counter++ loopcount n executed 1 5 1 2 2 3 3 4 4 5 *
Common Use: Running Totals(Accumulator) count =1; // variables must be initialized total = 0; // before loop • while (count <=4){ cout << “Enter a number: “; cin >> num; total = total + num; cout << “The total is “ << total; count++;} *
Common Use: Average count =1; total = 0; float average; • While (count <=4) • { • cout <<“Enter a number ”; • cin >> number; • total += number; • count++; • } • average = total / (count-1); • cout << “The average is “ << average << endl; Why count – 1?
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
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;
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 || *