Lect8.ppt

advertisement
More General TaskControlled Loops
Chapter 8
CSIS 10A
1
Agenda
Examples and Guidelines 
Trapping Input Errors
Multiple Reasons for Loop Exit
Nested Loops
2
Task Controlled Loop
When a loop exits on a condition determined by a
variable used in the loop
You don’t know when it will stop
(not step controlled)
The user input does not necessarily determine
when it will stop either
(not user-controlled)
3
Finding the first square to put sum over 100
// over1000.cpp
int n, sum;
n=0;
sum=0;
while (sum <= 1000)
{
n++;
sum = sum + n*n;
}
cout << "Sum first goes over 1000 when "
<< n << " squared is added.\n";
cout << "Sum is " << sum << ".\n";
return 0;
}
See
over1000.cpp
A slight mod
will solve
problem 16
(also Lab8-2)
4
Guidelines in developing
Understand the problem
Develop the loop algorithm
What variables? What exit test?
What needs to be done Before/After loop?
How to generate next value
Pseudocode
Code and Test
5
Some Problem Statements
If $500 is invested at 4.5% compounded
annually, after how many years will the balance
first exceed $1000 ?
Find what value of n makes the series
sum = 1 + 1/2 + 1/3 + … 1/n
exceed the value 2…and what is the sum?
(let’s develop some solutions!)
6
Agenda
Examples and Guidelines
Trapping Input Errors 
Multiple Reasons for Loop Exit
Nested Loops
7
How can this program go wrong?
// letter1.cpp
char ch;
cout << "Enter capital letter: ";
cin >> ch;
if (ch >= 'A' && ch <= 'M')
cout << ch << " in first half of alphabet.\n";
else
cout << ch << " in second half of alphabet.\n";
return 0;
}
8
Many times we have to protect
our code from bad data
cout<<“Enter value between 0 and 100”<<endl;
cin>>num;
if (num<0 || num > 100)
cout<<“Bad data”;
else
It would be nice to not go on until
{
Good data is entered!
//process data
}
9
Soln1: Use a trap using while
cout<<“Enter value between 0 and 100”<<endl;
cin>>num;
while (num<0 || num > 100)
{
cout<<“Bad data, try again”;
cin>>num;
We can’t go on until
}
Good data is entered!
//process data
10
Soln2: Use a trap using do-while
do
{
cout<<“Enter value between 0 and 100”<<endl;
cin>>num;
}
while (num<0 || num > 100);
//process data
11
Letter1.cpp Fixed in letter2.cpp
char ch;
do {
cout << "Enter capital letter: ";
cin >> ch;
}
while (ch<'A' || ch>'Z');
if (ch >= 'A' && ch <= 'M')
cout << ch << " in first half of alphabet.\n";
else
cout << ch << " in second half of alphabet.\n";
12
Agenda
Examples and Guidelines
Trapping Input Errors
Multiple Reasons for Loop Exit 
Nested Loops
13
Can use complex booleans in loops
int dots, // on top face of die
rolls; // rolls so far
See rolls.cpp
srand(time(0)); // init r.n.g.
rolls = 0;
do {
dots = rand()%6 + 1;
// random # between 1 and 6
rolls++;
cout << dots << ' ';
}
while (dots!=1 && dots != 6);
if (dots == 1)
cout << "You lose $";
else // dots == 6
cout << "You win $";
cout << rolls << endl;
14
Agenda
Examples and Guidelines
Trapping Input Errors
Multiple Reasons for Loop Exit
Nested Loops 
15
Nesting loops
A loop inside another becomes a nested loop
void main()
{
for(int x = 1;
{
cout <<“Now
for(int y =
cout
}//end of for
}
x <= 10; x=x+1)
x is ” << x << endl;
1; y <= 5; y=y+1)
<< y << endl; //end of for
Trace the output of this program
16
male_sum = 0; fem_sum = 0;
do {
cout << "Enter number of points: ";
cin >> points;
do {
cout << "Enter sex (m or f): ";
cin >> sex;
What is output when
sex = toupper(sex);
the following is input:
}
while (sex!='M' && sex!='F');
fyfymyfymyfymn
if (sex=='F')
fem_sum += points;
else
male_sum += points;
cout << "Continue? (y or n): ";
cin >> ans;
} while (toupper(ans) != 'N');
cout << "Female sum = " << fem_sum << endl;
cout << "Male sum = " << male_sum << endl;
Other loops can be
nested (points.cpp)
17
You should be able to trace a nested
loop (see asterisk.cpp)
for (int k=1; k<=4; k++)
{
for (m=k; m>0; m--)
cout<<“*”;
cout<<endl;
}
k
m (k<=4) (m>0)
Output
18
Download