to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/loops.pdfto repeat a set of...
TRANSCRIPT
to repeat a set of 0 or more statements
0 or more times
as long as some condition is true.
conditionalExpression
oneStatement
where oneStatement is exactly one statement
with a semicolon, or a block
At the beginning: conditionalExpression is
evaluated and
- If true: oneStatement is executed, then control
returns to the beginning.
- If false: oneStatement skipped.
while (conditional)
Exactly one statement; // indent the 1 statement
while (conditional)
{ // line up with while
statements // indent statements
} // line up with while
the one statement or block
one execution of the loop
(one time through, one “lap”)
int a = 1;
while (a <= 3) Prints:
a++; 4
cout << a << endl;
int a = 1;
while (a <= 3) Prints:
a++; 2
cout << a << endl; 3
4
int a = 12;
while (a <= 3) { Prints:
a++; Done!
cout << a << endl;
}
cout << ”Done!\n”;
int i = ;
while (i <= ) {
i += ;
}
Example: start at 1, stop at 20, count by 2's
int i = ;
while (i <= ) {
cout << i << ” ”;
i += ;
}
// answer could be float/string/etc.
int answer = wrong_answer;
while (answer is wrong) {
cout << ”Enter …”;
cin >> answer;
}
int answer = 0;
while (answer < 1 || answer > 10) {
cout << ”Enter integer 1 to 10: ”;
cin >> answer;
}
string answer = ””;
while (answer !=”left” && answer!=”right”) {
cout << ”Enter left or right: ”;
cin >> answer;
}
// 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;
}
// use to see if we need an error message
string answer = ””;
while (answer !=”left” && answer!=”right”) {
cout << ”Enter left or right: ”;
cin >> answer;
}
int num,
while (whatever_control) { // any type of loop
cout << ”Enter number: ”;
cin >> num;
// sum = sum + num
}
int num,
while (whatever_control) {
cout << ”Enter number: ”;
cin >> num;
// count = count + 1
}
cout << count << " numbers entered";
float num, , average;
int ;
while (whatever_control) {
cout << ”Enter number: ”;
cin >> num;
// count = count + 1
// sum = sum + num
}
average = sum / count;
int
while (whatever_control) {
cout << ”Enter number: ”;
cin >> num;
}
int Low
while (whatever_control) {
cout << ”Enter number: ”;
cin >> num;
}
- 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
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
Input data
while (data != sentinel) {
process data
Input data
}
Note the check for the sentinel is done immediately after a value is inputted.
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
int score, sum=0;
while (score != -1) {
sum += score;
}
cout << "The sum is " << sum << endl;
- 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".
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.