to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/loops.pdfto repeat a set of...

28

Upload: others

Post on 24-Feb-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression
Page 2: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

to repeat a set of 0 or more statements

0 or more times

as long as some condition is true.

Page 3: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

conditionalExpression

oneStatement

where oneStatement is exactly one statement

with a semicolon, or a block

Page 4: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

At the beginning: conditionalExpression is

evaluated and

- If true: oneStatement is executed, then control

returns to the beginning.

- If false: oneStatement skipped.

Page 5: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

while (conditional)

Exactly one statement; // indent the 1 statement

while (conditional)

{ // line up with while

statements // indent statements

} // line up with while

Page 6: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

the one statement or block

one execution of the loop

(one time through, one “lap”)

Page 7: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int a = 1;

while (a <= 3) Prints:

a++; 4

cout << a << endl;

Page 8: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int a = 1;

while (a <= 3) Prints:

a++; 2

cout << a << endl; 3

4

Page 9: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int a = 12;

while (a <= 3) { Prints:

a++; Done!

cout << a << endl;

}

cout << ”Done!\n”;

Page 10: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int i = ;

while (i <= ) {

i += ;

}

Page 11: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

Example: start at 1, stop at 20, count by 2's

int i = ;

while (i <= ) {

cout << i << ” ”;

i += ;

}

Page 12: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

// answer could be float/string/etc.

int answer = wrong_answer;

while (answer is wrong) {

cout << ”Enter …”;

cin >> answer;

}

Page 13: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int answer = 0;

while (answer < 1 || answer > 10) {

cout << ”Enter integer 1 to 10: ”;

cin >> answer;

}

Page 14: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

string answer = ””;

while (answer !=”left” && answer!=”right”) {

cout << ”Enter left or right: ”;

cin >> answer;

}

Page 15: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

// ask first time before the loop

string answer;

while (answer !=”left” && answer!=”right”) {

cout << “Error! Type left or right!!\n”;

cout << ”Enter left or right: ”;

cin >> answer;

}

Page 16: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

// use to see if we need an error message

string answer = ””;

while (answer !=”left” && answer!=”right”) {

cout << ”Enter left or right: ”;

cin >> answer;

}

Page 17: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int num,

while (whatever_control) { // any type of loop

cout << ”Enter number: ”;

cin >> num;

// sum = sum + num

}

Page 18: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int num,

while (whatever_control) {

cout << ”Enter number: ”;

cin >> num;

// count = count + 1

}

cout << count << " numbers entered";

Page 19: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

float num, , average;

int ;

while (whatever_control) {

cout << ”Enter number: ”;

cin >> num;

// count = count + 1

// sum = sum + num

}

average = sum / count;

Page 20: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int

while (whatever_control) {

cout << ”Enter number: ”;

cin >> num;

}

Page 21: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int Low

while (whatever_control) {

cout << ”Enter number: ”;

cin >> num;

}

Page 22: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

- programs must often input data

(one at a time).

- the may not be known:

when the program is written

when the program is executing and is

ready to input the first data value

Page 23: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

a value entered as data,

but is not processed as data;

instead, it signals the end of data.

choosing a Sentinel Value

- must be the same Data Type as the data

- should not "make sense" as data

Page 24: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

Input data

while (data != sentinel) {

process data

Input data

}

Note the check for the sentinel is done immediately after a value is inputted.

Page 25: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

Enter an unknown number of test scores and

calculate the sum. Use -1 as the Sentinel.

Note: the value

- is the same type (int) as a test score

- "doesn't make sense" as a test score

Page 26: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

int score, sum=0;

while (score != -1) {

sum += score;

}

cout << "The sum is " << sum << endl;

Page 27: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

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

Page 28: to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/Loops.pdfto repeat a set of 0 or more statements 0 or more times as long as some condition is true. conditionalExpression

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.