A loop

exercise No. 4

Q:

Execute

int i = 0;
while ( i < 5) {
  System.out.println("loop # " + i);
  i = i + 1;
}

Examine the result and implement the following modifications:

  1. Create five additional lines of output

  2. Enlarge the gap between two adjacent lines of output to a value of 3:

    loop # 0
    loop # 3
    loop # 6
    loop # 9
    loop # 12

A:

  1. The number of output lines is being determined by the loop's termination condition i < 5. Replacing this limit by while ( i < 10) achieves the desired result.

  2. We may adjust two related parameters:

    Adjusting the limit to accommodate larger values.

    Raising the gap to a value of 3.

    while ( i < 15 ) {
       System.out.println("loop # " + i);
       i = i + 3 ;
    }

    Alternatively we may simply modify the print statement:

    System.out.println("loop # " + i * 3);