ForTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called ForTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

Program listing

// FOR LOOPS TUTORIAL
// Copyright (C) 1998-2016 Davin Pearson
// Website: http://davin.50webs.com/java-tutorials

class ForTest
{
   /*
    *
    * The main method is the point of entry into the program...
    *
    */
   public static void main(String[] args)
   {
      // first for loop...
      // prints: 2 3 4 5 6 7 8 9 10
      for (int i=2; i<=10; i++)
      {
         System.out.println(i);
      }

      System.out.println(); // prints a blank line

      // second for loop...
      // prints: 10 15 20 25 30 35 40
      for (int i=10; i<=40; i+=5)
      {
         System.out.println(i);
      }

      System.out.println(); // prints a blank line

      // third for loop...
      // prints: 10 9 8 7 6 5 4 3 2 1
      for (int i=10; i>=1; i--)
      {
         System.out.println(i);
      }

      System.out.println(); // prints a blank line

      // fourth for loop...
      // prints: 100 90 80 70 60 50 40 30 20
      for (int i=100; i>=20; i-=10)
      {
         System.out.println(i);
      }

      System.out.println(); // prints a blank line
   }
}

// QUESTIONS:
//
// Compile and run this file and verify that the "for" loops
// in the main method do what their comments claim they do.
// The questions of this tutorial will ask you to write some
// new "for" loops that are based on the ones that are
// already present in the main method.
//
// (1) The first "for" loop prints out an increasing series
// of numbers starting at 2 and finishing at 10.  Copy the
// pattern of this loop to achieve the following sequences
// of numbers.  Don't forget to compile and run the program
// to make sure that your answers generate the correct
// output.
//
// (a) 5 6 7 8 9 10
// 
// (b) 234 235 236 237 238
// 
// (c) 48 49 50 ... 75 76
// 
// (d) -5 -4 -3 -2 -1 0 1 2 3
// 
//
// (2) The second "for" loop prints out an increasing series
// of numbers starting at 10 and finishing at 40.  Unlike
// the first "for" loop, the second "for" loop increments
// the numbers in steps of 5 instead of 1.
//
// Copy the pattern of this loop to achieve the following
// sequences of numbers.  Don't forget to compile and run
// the program to make sure that your answers generate the
// correct output.
//
// (a) 20 25 30 35 40
// 
// (b) 100 105 110 115 120 125
// 
// (c) 2 4 6 8 10 12 14
// 
// (d) 10 13 16 19 22 25
// 
//
// (3) The third "for" loop prints out a decreasing series
// of numbers starting at 10 and finishing at 1.  Copy the
// pattern of this loop to achieve the following sequences
// of numbers.  Don't forget to compile and run the program
// to make sure that your answers generate the correct
// output.
//
// (a) 10 9 8 7 6 5 4
// 
// (b) 20 19 18 17 16 15 14 13 12
// 
// (c) 66 65 64 ... 47
// 
// (d) 3 2 1 -1 -2 -3 -4 -5 -6 -7
// 
//
// (4) The fourth "for" loop prints out a decreasing series
// of numbers starting at 100 and finishing at 20.  Unlike
// the third "for" loop, the fourth "for" loop decrements
// the numbers in steps of 10 instead of 1.
//
// Copy the pattern of this loop to achieve the following
// sequences of numbers.  Don't forget to compile and run
// the program to make sure that your answers generate the
// correct output.
//
// (a) 80 70 60 50 40 30 20
// 
// (b) 500 490 480 470 460
// 
// (c) 10 8 6 4 2 0
// 
// (d) 33 28 23 18 13 8 3
//