PreLab 8: More Loops    Name ___________________________________________

  1. Trace the execution of each of the following for loops.

    1. For this one show the value of each variable at each iteration in the table next to the code.
      
            final int LIMIT = 10;                        sum      i
            int sum = 0;                                ------------
      
            for (int i = 1; i < LIMIT; i = i + 2)
            {
              sum = sum + i;
            }
      
      
            
      
    2. For this one, show what is printed (the output).
      
            final int LIMIT = 100;                        Output
                                                          ------
      
            for (int i = LIMIT; i > 0; i = i - 10)
            {
              System.out.println("**: " + i);
            }
      
      
      
      
      
  2. Trace the execution of the following loop by showing the value of each variable at each iteration in the table next to the code.
          final int LIMIT = 12;                num          sum       
          int num = 1;                         ---          ---  
          int sum = 0; 
       
          do
          {
             num = num + 3;
             sum = sum + num;
          }
          while (num < LIMIT);
    
          System.out.println ("Last number is " +  num + " and sum is " + sum);
    
  3. What is printed by the above code?

     

     

  4. Write a for loop equivalent to the do loop above. Your loop should compute exactly the same sum and print exactly the same message at the end. Be sure to set up the three parts of the for so the loop starts and stops correctly.

     

     

     

     

     

     

     

     

  5. Write a for loop that prints the multiples of 5 from 5 to 100.
    
    
    
    
    
    
    
    
    
    
    
    
    

  6. Complete the following code to print the string read in backwards. Use a for loop.
          String str;
          int length;
          System.out.print ("Enter a string of characters: ");
          str = scan.nextLine();
    
          // Find the length of str
    
    
          ________________________________________________________________;
    
          // Write a for loop to print the string backwards
    
    
    
    
    
    
    
    
    
    
    

  7. Convince yourself that the following for loop prints 5 asterisks ("stars") on a row.
          for (int star = 1; star <= 5; star++)
             System.out.print("*");
          System.out.println();
    
    
    The following nested loop prints a triangle of stars (see page 148 of your text - it shows the whole program and the output; page 147 & 148 describe what the program is doing).
          final int MAX_ROWS = 10;
          for (int row = 1; row <= MAX_ROWS; row++)
          {
             // Print a row with the same number of stars as variable "row"
             for (int star = 1; star <= row;  star++)
                System.out.print ("*");
     
             System.out.println();
         }
    

    Trace the following similar nested loop (two loops nested inside of one) and show the pattern that would be printed.

          final int MAX_ROWS = 4;
          final int NUM_DASHES = 4;
    
          for (int row = 1; row <= MAX_ROWS; row++)
          {
             for (int dash = 1; dash <= NUM_DASHES - row; dash++)
                System.out.print ("-");
    
             for (int star = 1; star <= row;  star++)
                System.out.print ("*");
     
             System.out.println();
         }