PreLab 9: While Loops        Name__________________________________

  1. The following is a basic loop that prints the numbers from 1 to LIMIT and finds their sum:

       final int LIMIT = 100;         // setup
       int count = 1;                 // - initialize the loop control variable 
       int sum = 0;                   // - and the summing variable
       while (count <= LIMIT)         // loop control condition
       {                              // body
         System.out.println(count);   //   -- perform task
         sum = sum + count;           //      (print and sum)
         count = count + 1;           //   -- update condition
       System.out.println ("The sum of the integers from 1 to " +
            LIMIT + " is " + sum);

    This loop is an example of a count-controlled loop, that is, a loop that contains a counter (a variable that increases or decreases by a fixed value -- usually 1 -- each time through the loop) and that stops when the counter reaches a certain value.

    1. In this loop the println statement and the statement to update the sum comes before the value of count is incremented. What would happen if you reversed the order of these statements so that count was incremented before the other two statements? Would the loop still print the same values? Would the sum be the same? Exactly what numbers would be printed and summed?




    2. Assuming the order of statements is changed as in part (a) (with the count incremented first), you can make one small change in the loop setup to make the loop behave exactly as it did originially. What is that change?

  2. Not all loops with counters are count-controlled; consider the example below, which determines how many even numbers must be added together, starting at 2, to reach or exceed a given limit.

       final int LIMIT = 16;                            TRACE
       int count = 1;                        sum        nextVal     count
       int sum = 0;                          ---        -------     -----
       int nextVal = 2;
       while (sum < LIMIT)
         sum = sum + nextVal;     
         nextVal = nextVal + 2;
         count = count + 1;
       System.out.println("Had to add together " + (count-1) + " even numbers " +
                          "to reach value " + LIMIT + ".  Sum is " + sum);
    Note that although this loop counts how many times the body is executed, the condition does not depend on the value of count.

    1. Trace this loop, that is, in the table next to the code show values for variables nextVal, sum and count at each iteration.

    2. Show what the code prints.

    3. Note that when the loop terminates, the number of even numbers added together before reaching the limit is count-1, not count. How could you modify the code so that when the loop terminates, the number of things added together is simply count?

  3. Not all loops have counters. For example, if the task in the loop above were simply to add together even numbers until the sum reached a certain limit and then print the sum (as opposed to printing the number of things added together), there would be no need for the counter. Similarly, the loop below determines whether or not integers entered by the user are divisible by 37; it contains no counter. The loop is controlled by the user's answer to a "keep going?" question.

       String keepGoing = "Y";
       int nextVal;
       while (keepGoing.equals("y") || keepGoing.equals("Y"))
           System.out.print("Enter the next integer: ");       //do work
           nextVal = scan.nextInt();
           if (nextVal % 37 == 0)
               System.out.println (nextVal + " is divisible by 37.");
             System.out.println (nextVal + " is NOT divisible by 37.");
           System.out.print("Type y or Y to keep going: ");    //update condition
           keepGoing =;
       System.out.println("Bye, hope this program was helpful!");
  4. Not all counters are counting the number of times a loop execute. Modify this loop so that it counts the integers that are divisible by 37 and those that are not. Write your changes on the code above as follows:
    1. Declare two counting variables - one named count37 that will count the number of integers divisible by 37 and the other named not37 that will count the number not divisible by 37.
    2. Update each variable in the appropriate place in the code.
    3. After the loop, print the number of integers entered that are divisible by 37 and the number that weren't. example code.

  5. This last loop continues as long as the user types in a Y or y in response to the keep going question. It stops on any other input. Write a loop control condition that would cause the loop to STOP when N or n is entered but continue on any other input.



  6. Write a while loop that will print "I love computer science!!" 100 times.

  7. Is the loop you just wrote count-controlled? _____________

  8. The code below is supposed to print the integers from 10 to 1 backwards. What is wrong with it? (Hint: there are two problems!) Correct the code so it does the right thing.

       count = 10;
       while (count >= 0)
        count = count + 1;