ForTest.java Answers

Click here to go back to the questions
// ANSWERS:
//
// 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
// 

for (int i=5; i<=10; i++)
{
   System.out.println(i);
}

// 
// (b) 234 235 236 237 238
// 

for (int i=234; i<=238; i++)
{
   System.out.println(i);
}

// 
// (c) 48 49 50 ... 75 76
// 

for (int i=48; i<=76; i++)
{
   System.out.println(i);
}

// 
// (d) -5 -4 -3 -2 -1 0 1 2 3
// 

for (int i=-5; i<=3; i++)
{
   System.out.println(i);
}

// 
//
// (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
// 

for (int i=20; i<=40; i+=5)
{
   System.out.println(i);
}

// 
// (b) 100 105 110 115 120 125
// 

for (int i=100; i<=125; i+=5)
{
   System.out.println(i);
}

// 
// (c) 2 4 6 8 10 12 14
// 

for (int i=2; i<=14; i+=2)
{
   System.out.println(i);
}

// 
// (d) 10 13 16 19 22 25
// 

for (int i=10; i<=25; i+=3)
{
   System.out.println(i);
}

// 
//
// (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
// 

for (int i=10; i>=4; i--)
{
   System.out.println(i);
}

// 
// (b) 20 19 18 17 16 15 14 13 12
// 

for (int i=20; i>=12; i--)
{
   System.out.println(i);
}

// 
// (c) 66 65 64 ... 47
// 

for (int i=66; i>=47; i--)
{
   System.out.println(i);
}

// 
// (d) 3 2 1 -1 -2 -3 -4 -5 -6 -7
// 

for (int i=3; i>=-7; i--)
{
   System.out.println(i);
}

// 
//
// (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
// 

for (int i=80; i>=20; i-=10)
{
   System.out.println(i);
}

// 
// (b) 500 490 480 470 460
// 

for (int i=500; i>=460; i-=10)
{
   System.out.println(i);
}

// 
// (c) 10 8 6 4 2 0
// 

for (int i=10; i>=0; i-=2)
{
   System.out.println(i);
}

// 
// (d) 33 28 23 18 13 8 3
// 

for (int i=33; i>=3; i-=5)
{
   System.out.println(i);
}

//