loops/iteration
DESCRIPTION
Loops/Iteration. Used to repeat an action Must have a STOP condition Three flavors - for, while , do/while. 01/05/100. 1. stmt before loop. test ?. loop body. stmt following loop. Anatomy of a while loop. 1 check the test 2 if the test is true exec the body - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/1.jpg)
01/05/100 1
Loops/Iteration
• Used to repeat an action • Must have a STOP condition• Three flavors - for, while, do/while
![Page 2: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/2.jpg)
01/05/100 2
• 1 check the test • 2 if the test is true
– exec the body– when the body has finished
• go to step 1
• if the test is false– exit the loop
int n = ?; // try n as 6while (n >= 0) {
n -= 2;cout << n << endl;
}cout << “final n is “ << n << endl;
Anatomy of a while loop
test ?
loop body
stmt following loop
stmt before loop
The test is always a “keep going” condition. To determine the termination condition, negate the test.I.e. the loop will keep going as long as n >= 0 the loop will terminate when n becomes negative (n < 0)
![Page 3: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/3.jpg)
01/05/100 3
while Loops
• The test is checked at the very beginning and then again each time after the after the entire loop body has been executed
• The test is NOT checked in the middle of the loop body• This is true for all the loops (for, while, and do/while), not just
the while loop
x = ?; // try x as 45
while (x < 50)
{
x++;
cout << x << endl;
x++;
cout << x << endl;
}
![Page 4: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/4.jpg)
01/05/100 4
Practice what’s the output?
int a = 20, b = 50;while (a < b) { if (a % 5 == 0)
{ cout << “s”; }
else if (a % 3 == 0) {
cout << “e”; }
else {
cout << “w”; } a += 4;}cout << “\n”;cout << “a’s final value is “ << a << endl;
![Page 5: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/5.jpg)
01/05/100 5
PracticeWhat’s the output?
int d = 90;
while (d < 80) {
d ++;
}
cout << “d is “ << d << endl;
int x = 90;
while (x < 100)
{
x -= 5;
}
cout << “final value for x is “ << x << endl;
![Page 6: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/6.jpg)
01/05/100 6
Summing (even) numbers with a while loop
Example of an indeterminate loop - the user’s input will determine how many times the loop executes
int sum = 0, evensum = 0, number;
cout << “ First number please “;
cin >> number;
while (number > 0)
{
sum += number;
if (number % 2 == 0)
{
evensum += number;
}
cout << "number please ";
cin >> number;
}
cout << "sum is " << sum << endl;
cout << “sum of even #’s is “ << evensum << endl;
![Page 7: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/7.jpg)
01/05/100 7
Error checking with a while loopif the number is out of range, enter the while loop
otherwise, skip the loopreenter loop when the user’s number continues to be out of range
const int cMax = 10, cMin = 5;
cout << "Pls enter a # between "
<< cMax << " and "
<< cMin << " please " << endl;
cin >> number;
while (number < cMin || number > cMax)
{
cout << "error in input \n";
cout << "Pls enter a # between "
<< cMax << " and "
<< cMin << " please " << endl;
cin >> number;
}
cout << “The user entered “
<< number << endl;
test ?
loop body
stmt following loop
stmt before loop
![Page 8: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/8.jpg)
Block/Scope
• { } define a scope
void main()
{
int n = 9; // this n belongs to main
{
int n = 56; // this n belongs to this block nested in main
cout << n << endl; // use the inner most n
}
cout << n << endl; // don’t know about inner n, use n from main
}
this is not a good idea, having nested blocks that reuse the same variable name
![Page 9: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/9.jpg)
Scope/Blocks
identifiers declared in a nested block cannot be referred to outside the block
but identifiers in an outer block are accessible in a nested block
void main() {
int a = 5;
{ int x = 9;
cout << “inside inner block\n”; cout << x << “ “ << a << endl; } // ILLEGAL, x is not visible in this block cout << x << “ “ << a << endl;}}
![Page 10: Loops/Iteration](https://reader035.vdocuments.site/reader035/viewer/2022072013/56812bc7550346895d9013e8/html5/thumbnails/10.jpg)
Where you really use blocks
• You won’t use free-standing nested or sequential blocks demo’ed in the last slides, but you do use blocks in constructs that have a pair of { }
void main() {
int sum = 0;while (sum < 80) {
int response;cout << “Please enter a number\n”;cin >> response;sum += response;
}cout << response << endl; // ILLEGAL , response is declared inside of the
while loop block and cannot be used outside of the while loop
cout << sum << endl; // sum can be used both in and out of the while loop
}