Presentation is loading. Please wait.

Presentation is loading. Please wait.

While Loops.

Similar presentations


Presentation on theme: "While Loops."— Presentation transcript:

1 while Loops

2 While Loop Purpose: to repeat a set of 0 or more statements 0 or more times as long as some condition is true.

3 while ( conditionalExpression ) oneStatement
While Loop Syntax: while ( conditionalExpression ) oneStatement where oneStatement is exactly one statement with a semicolon, or a block

4 While Loop Semantics: At the beginning: conditionalExpression is evaluated and - If true: oneStatement is executed, then control returns to the beginning. - If false: oneStatement skipped.

5 While Loop Style #1: one statement while (conditional) Exactly one statement; // indent the 1 statement Style #2: block { // line up with while statements // indent statements } // line up with while

6 While Loop Quick Vocabulary Body (of the loop): the one statement or block Iteration: one execution of the loop (one time through, one “lap”)

7 While Loop Example 1: only syntax counts! int a = 1; while (a <= 3) Prints: a++; 4 cout << a << endl;

8 While Loop Example 2: careful of where begins/ends int a = 1; while (a <= 3) { Prints: a++; 2 cout << a << endl; 3 } 4

9 While Loop Example 3: 0 iterations possible int a = 12; while (a <= 3) { Prints: a++; Done! cout << a << endl; } cout << ”Done!\n”;

10 While Loop Strategies Counter-Controlled Loop int i = start; while (i <= stop) { // do something some number of times i += increment; // i = i + increment; }

11 While Loop Strategies Counter-Controlled Loop Example: start at 1, stop at 20, count by 2's int i = 1; while (i <= 20) { cout << i << ” ”; i += 2; } // prints:

12 While Loop Strategies User Input Validation // answer could be float/string/etc. int answer = wrong_answer; while (answer is wrong) { cout << ”Enter …”; cin >> answer; }

13 While Loop Strategies User Input Validation int answer = 0; while (answer < 1 || answer > 10) { cout << ”Enter integer 1 to 10: ”; cin >> answer; }

14 While Loop Strategies User Input Validation string answer = ””; while (answer !=”left” && answer!=”right”) { cout << ”Enter left or right: ”; cin >> answer; }

15 While Loop Strategies User Input Validation with error message // ask first time before the loop string answer; cout << ”Enter left or right: ”; cin >> answer; while (answer !=”left” && answer!=”right”) { cout << “Error! Type left or right!!\n”; }

16 While Loop Strategies User Input Validation with error message // use if to see if we need an error message string answer = ””; while (answer !=”left” && answer!=”right”) { cout << ”Enter left or right: ”; cin >> answer; if (answer !=”left” && answer!=”right”) cout << ”Invalid answer!\n”; }

17 While Loop Strategies Summation of entered Values int num, sum = 0; while (whatever_control) { // any type of loop cout << ”Enter number: ”; cin >> num; sum += num; // sum = sum + num }

18 While Loop Strategies Counting Strategy int num, count = 0; while (whatever_control) { cout << ”Enter number: ”; cin >> num; count++; // count = count + 1 } cout << count << " numbers entered";

19 While Loop Strategies Average of Inputs: Count + Sum float num, sum = 0, average; int count = 0; while (whatever_control) { cout << ”Enter number: ”; cin >> num; count++; // count = count + 1 sum += num; // sum = sum + num } average = sum / count;

20 While Loop Strategies Mini - find the lowest value int min = ridiculously High number; while (whatever_control) { cout << ”Enter number: ”; cin >> num; if (num < min) min = num; }

21 While Loop Strategies Max - find the highest value int max = ridiculously Low number; while (whatever_control) { cout << ”Enter number: ”; cin >> num; if (num > max) max = num; }

22 Sentinel Controlled Loop
Motivation/Purpose: - programs must often input data serially (one at a time). - the Number of Inputs may not be known: when the program is written when the program is executing and is ready to input the first data value How many data items will there be???

23 Sentinel Controlled Loop
Definition of a Sentinel Value: a value entered as data, but is not processed as data; instead, it signals the end of data. Characteristics: choosing a Sentinel Value - must be the same Data Type as the data - should not "make sense" as data

24 Sentinel Controlled Loop
General Pattern: Input data while (data != sentinel) { process data } Note the check for the sentinel is done immediately after a value is inputted.

25 Sentinel Controlled Loop
Example: Enter an unknown number of test scores and calculate the sum. Use -1 as the Sentinel. Note: the value -1 - is the same type (int) as a test score - "doesn't make sense" as a test score

26 Sentinel Controlled Loop
int score, sum=0; cout << "Enter score, -1 to quit: "; cin >> score; while (score != -1) { sum += score; } cout << "The sum is " << sum << endl;

27 Sentinel Controlled Loop
Origin of the word Sentinel: - a guard on the "lookout" for something (usually the "bad guys" or other mischief) The while() is "on the lookout" for a particular value, and reacts (stops) when it sees the value. The value is not treated as "ordinary".

28 Vocabulary Term Definition Body
the one statement or block of statements repeated by a loop. Iteration one execution of a loop (lap). Sentinel a value entered as data, but is not processed as data; instead, it signals the end of data.


Download ppt "While Loops."

Similar presentations


Ads by Google