You are on page 1of 17

Chapter 5

Repetition

Repetition

Statement1

Condition
True

False

Set 2 of Statements

Set 1 of Statements

statement1 if (condition) statement2 statement3

// execute statement2 if // condition is true

statement1 while (condition) statement2 statement3

// repeat statement2 as long // as condition is true

statement2 could be a compound statement

statement1 while (condition) statement2 statement3

// repeat statement2 as long // as condition is true

statement2 is called the body of the while loop the variables appearing in the condition are called the loop control variables (lcv) The lvcs control the number of iterations of the loop in order for the loop to terminate, the lcvs should be updated inside the loop body

while loop design process


Identify what needs to be repeated Identify control mechanism and variables Initialize lcvs before the loop Write condition using lcvs Update lcvs in the body of the loop

// lcvs are initialized before the loop while (condition) statement2 statement3 // lcvs are used in the condition // lcvs are updated in the body

char ch; cin.get(ch); if (ch >= a && ch <= z) ch = ch 32; cout << ch << endl;
I want to repeat it 10 times repeat what?

I want to repeat it 10 times char ch; int count; count = 0;

while (count < 10) { cin.get(ch); if (ch >= a && ch <= z) ch = ch 32; cout << ch << endl;
count = count + 1; // usually the last statement }

Keep initializecontrolnumberlcv iterarations a test conditionvariable? Loop control mechanism? update of the in the body count the lcv using of Loop lcv before the loop

while loop design strategies


Counter-controlled while loops Sentinel-controlled while loops Flag-controlled while loops EOF-controlled while loops

Counter-controlled
The number of iterations are given
char ch; int count; count = 0; while (count < 10) { cin.get(ch); if (ch >= a && ch <= z) ch = ch 32; cout << ch << endl; count = count + 1; }

Counter-controlled
The number of iterations are given
char ch; int count; int n; // n is the number of times the body is to be repeated cin >> n; count = 0; while (count < n) { cin.get(ch); if (ch >= a && ch <= z) ch = ch 32; cout << ch << endl; count = count + 1; }

Beware of Infinite Loops


cin >> x; y = 0; while (x != 12) {
y = y + x; x = x + 2;

If x is odd or greater cin >> x; than 12, this loop will y = 0; never terminate! while (x <= 12) {
y = y + x; x = x + 2;

cout << x << y;

cout << x << y;

Sentinel-Controlled Loops
The number of iterations are not known up front. Check for a sentinel (a special entry)
cin >> lcv; // initlialize the lcv while (lcv != someSentinel) { cin >> lcv; }

calculate average of a set of numbers cardinality of the set is not known


int total, count; float average; char toContinue; total = 0; count = 0; cout << enter n/N to stop, any other key to continue: ; cin >> toContinue; while (toContinue != n || toContinue != N) Checking the value of { lcv against a sentinel cin << nextNumber; value. total = total + nextNumber; count = count + 1; cout << enter n/N to stop, any other key to continue: ; cin >> toContinue; } Update the value of the average = total / count;

lcv

calculate average of a set of numbers cardinality of the set is not known


int total, count; float average; char toContinue; total = 0; count = 0; cout << enter n/N to stop, any other key to continue: ; cin >> toContinue; while (toContinue != n || toContinue != N) { cin << nextNumber; total = total + nextNumber; count = count + 1; cout << enter n/N to stop, any other key to continue: ; cin >> toContinue; }

Error: This is integer division


average = total / count;

int total, count; float average; char toContinue; total = 0; count = 0;

cout << enter n/N to stop, any other key to continue: ; cin >> toContinue;
while (toContinue != n || toContinue != N) { cin << nextNumber; total = total + nextNumber; count = count + 1; cout << enter n/N to stop, any other key to continue: ; cin >> toContinue; }

average = float(total) / count;

Error: what happens if count is zero?

int total, count; float average; char toContinue; total = 0; count = 0;

cout << enter n/N to stop, any other key to continue: ; cin >> toContinue;
while (toContinue != n || toContinue != N) { cin << nextNumber; total = total + nextNumber; count = count + 1; cout << enter n/N to stop, any other key to continue: ; cin >> toContinue; }

if (count > 0) average = float (total) / count; else average = 0.0;

Flag-controlled loops
secretNumber = // generate a random number between 1 and 1000 found = false; tries = 0;

while (!found) {
tries = tries + 1; cin >> myGuess;

The lcv (flag) is updated here

if (myGuess == secretNumber) found = true; else if (myGuess > secretNumber) cout << The secret number is lower than your guess try again: ; else cout << The secret number is higher than your guess try again: ; } cout << You found the number in << tries << tries. << endl;

How to generate random numbers? See html documents in the shared folder.

You might also like