- Check schedule
- Class starts at 11:15. I take roll at 11:25
- Announcement(s)
- I sent out lab #4 results by e-mail to individual class members.
- Quiz #2 is almost all graded.
- I graded solo program #3
- Maybe we need to re-match a large number of partners?
- Check on partner list members -- if someone did lab #4, but
got a "0" on it -- let me know.
- Check posted grades and grades received in e-mail.
Let me know of any discrepancies you may find.
-
-
- Some programs need to contain loops. They are a practical way to make
the program repeat some instruction a large number of times.
The number of times that the instruction has to be repeated could be
known at the time the program is written. Example:
/* Write exactly 1000 things. */
for (int i=1; i<=1000; i++) cout << i << "." << endl ;
In other cases, the number of times an instruction has to be repeated
could be unknown at the time the program is written. Example:
/* Read words until there are no more. */
#include <string>
string theString ;
while (cin >> theString) cout << theString.length() << endl;
- Loops are a great convenience, but it is a challenge for the programmer
to "rig" the loop to execute correctly and to start and stop correctly.
- Updating assignment operators
(x = 31; x += 4 ; x == ?; )
(x = 41; x %= 3 ; x == ?; )
- Increment and decrement operators (c++) (get the pun?)
- The while-loop:
while (expression)
statement
Example:
/* What does this loop do? What is the relation
between the loop test and the loop body? When does
the loop stop, and why? What happens if the
initial value of count is 2? 1? 0? -1? */
int count = 100 ;
while (count >= 0)
{
cout << count << " bottles of beer on the wall" << endl ;
count-- ;
}
cout << "That's all." << endl ;
- The "Infinite Loop"
/* This code is an infinite loop. Nothing inside the
loop body will change the loop-control variable
count. Therefore the loop condition can never
become false. The loop, once entered will not halt
on it's own. The operator can stop it by typing a
ctrl-c. */
int count = 100 ;
while (count >= 0)
{
cout << count << " bottles of beer on the wall" << endl ;
}
/* This is an infinite loop too -- although it may
appear to be alright. The decrement of count is
not being done in the loop body -- braces missing. */
int count = 100 ;
while (count >= 0)
cout << count << " bottles of beer on the wall" << endl ;
count-- ;
- In the while-loop, the loop test is done (only) before beginning to
execute the first statement of the loop body. Therefore if the loop
condition becomes false somewhere in the middle of the execution of the
loop body, the loop body will nevertheless continue executing to the
end.
EXAMPLES:
int count = 100 ;
while (count >= 0)
{
count = -1 ;
cout << count << " bottles of beer on the wall" << endl ;
}
int count = 100 ;
while (count >= 0)
{
count = count - 150;
cout << count << " bottles of beer on the wall" << endl ;
count = count + 150 ;
}
- The do-while loop:
do
statment
while (expression) ;
Discuss the difference between a while-loop and a do-while loop. What
is the difference in the output of these loops? What's the difference
if count is initialized to 9?
count = 10 ;
while (count <10)
{
cout << count << " " ;
count++ ;
}
cout << endl ;
count = 10 ;
do
{
cout << count << " " ;
count++ ;
}
while (count <10) ;
cout << endl ;
- Looping and interactive programs
- file 6.08SumDigits.cpp in the directory with these notes
illustrates how the programmer can use a loop to continue
prompting for a value until the user enters one that is of the
correct form.
- file 6.10MakeSquares.cpp in the directory with these notes
illustrates how the programmer can use a "main-loop" to do the
main task of the program over and over until the user no longer
wants any more work done. This is a convenience for the user. The
user does not have to re-start the program each time that s/he
wants the task performed.
-
-
-