RHS =>  Allan =>  Basic Programming => Exercises => ForWhile Updated:  02/02/2004 23:36
WebMaster: allanhn@rhs.dk

For While

The Mission
Together we are going to do a program, which can perform several elementary loops of the counting type.
All assignments will need two solutions: one based on a for-loop and one based on a while-loop.

The problem
To create a class with methods containing counting-loops: loops until the stop condition becomes true. In the beginning the start and stop conditions will be fixed. Later we extend it to handle a variable start condition, variable stop condition and a variable step !

Question 1
You are to create a program which can print out the 20 numbers in order: 0, 1, 2, 3, ..19.
Type in the Loop model class, which must resemble the following template:

public class Loop {
  //Constructor
  Loop() {}

  public void loopUpF() {
    System.out.println("loopUpF in Loop is running");
    int i;
    for (i = 0; i < 20; i++) {
      System.out.println(i);
    }//for
  }//LoopUpF()
}//Loop

Save and compile.

Question 2
The LoopApp application class must resemble the following template:

public class LoopApp {

  public static void main(String[] args) {

    //Declaration of an instance of the Loop-class, doit yourself
    System.out.println("loopUpF in Loop is called from main in LoopApp");

    //looping is done by calling the object's method loopUpF, doit yourself

  }//main(String[])
}//LoopApp

Save this program in the file LoopApp.java.
Compile and run.

Try to change the counter increment (i.e. i++) so the step is 2 instead of 1.


Question 3
Extend the class, Loop, with a new method loopDownF(), which can print out the 20 numbers in the reverse order: 19, 18, 17 …..2, 1, 0.

Activate this method in LoopApp.

Question 4
Extend the class, Loop, with a new method loopUpW(),which can print out the 20 numbers in the order: 0, 1, 2, ….18, 19. But now the method must use a while-loop; thus the method resembles the following template:

public void loopUpW() {
  int i = 0;
  while (i < 20) {
    System.out.println(i);
    i = i + 1; //increase the counter
  }//while
}//loopUpW

Use also this method in LoopApp.
Try to increase the counter with 2 and see what happens.

Question 5
Extend the class with a new method loopDownW(), which can print out the 20 numbers in the reverse order: 19, 18, 17 …..2, 1, 0. Now the method must use a while-loop.
Use also this method in LoopApp.


Until now we have just made counter-loops stepping with a step value from a given start value until a certain stop value is reached. Now we will see how this can be made more general.

Question 6
Extend the class, Loop, with a new method loopUpGeneral(int start, int stop, int step), which can print out the numbers: in the interval [start, stop] stepping with the number step.

Tip: Copy loopUpF, change the name and the start, stop and step condition.

Use also this method in LoopApp.
Try to run it with different start, stop and step values.
Can you make an infinite loop??