(Latest Revision: 04/02/2000)
Week 09 Notes for CS 1500 -- Spring 2000
(this is draft material)
* Take roll
* Take a look at the schedule for the week.
* We have a very simple Solo #3 program due on Friday, April 14.
* We have to do Lab #6 on Friday, April 14. The lab directions
are ready -- have a look and rehearse.
* Quiz #2 is scheduled for Wednesday, April 19. The quiz will
cover Shiflet chapters 4-5; Andersen 10-12, 17-21. There are
some review hints in the "studyGuide" document in the web
space. (Have a look.)
* We have a very simple Solo #4 program due on Friday, April 21.
=========================
MONDAY
=========================
* I corrected lab #5 work and posted the grades in the web
space. Have a look. If you did not get 100 I sent e-mail
indicating what you did wrong. You can talk to me outside of
class if you have questions about it.
* 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.
=========================
WEDNESDAY
=========================
=========================
FRIDAY
=========================
Topics covered in readings this week:
+ Updating assignment operators
(x = 31; x += 4 ; x == ?; )
(x = 41; x %= 3 ; x == ?; )
+ Increment and decrement operators (c++) (get the joke?)
+ 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 stip 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.
+ 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 ;
+ For more examples of loops like the one needed in Solo #4,
look at the later examples in the assignment directory for
Lab #6.
+ Looping and interactive programs
- file EX0608.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 EX0610.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.