to repeat a set of 0 or more statementscs.uky.edu/~kwjoiner/cs215/notes/loops.pdfto repeat a set of...
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/1.jpg)
![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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/12.jpg)
// 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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/15.jpg)
// 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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/16.jpg)
// 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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/22.jpg)
- 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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/27.jpg)
- 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](https://reader035.vdocuments.us/reader035/viewer/2022071403/60f728e45e119f1b774f7dcd/html5/thumbnails/28.jpg)
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.