(Latest Revision: 10/28/2001)

Week 09 Notes for CS 1500 -- Fall 2001


  • Take roll.

  • Announcements

  • Check what's new in schedule

  • 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
  • What does 6.01LineNums.cpp write?
  • What does 6.06CountDown.cpp write?
  • Examine 6.08SumDigits.cpp -- How could we turn this program into a program that reads digits one at a time and constructs the number that the sequence of digits represents? Why would we want to do that? What is the relation to error control?