What is a loop

While loop

A loop is used to repeat a program section several times. The program section, the loop body, is repeated as long as a certain condition, the loop condition, is fulfilled.

The while loop has the general form

while ()
{
      1;
              
      ;
}

Here is the loop condition. It must be an expression that yields a value of type boolean, i.e. true or false. The loop condition is in round brackets after the word while while - as long as).

This is followed by the body of the loop in curly brackets, i.e. the section of the program that is to be repeated, represented here by the instructions 1, ...,. Each statement ends with a semicolon.

When the while loop is executed, it is first checked whether the loop condition is fulfilled, i.e. whether it results in the value true. If this is the case, the instructions of the loop body are carried out. When the loop body is finished, the program jumps back to the beginning of the while loop and checks the loop condition again. If this is still fulfilled, the loop body is executed again and so on - until the loop condition is no longer fulfilled at some point. Then the loop body is no longer executed, but the following instructions after the loop body are continued.

Example: The following While Loop outputs the numbers from 1 to 10 on the screen.

int i = 1; while (i <= 10) {System.out.println (i); i = i + 1; }

Here the loop condition is the expression i <= 10, and the body of the loop consists of the two statements System.out.println (i) and i = i + 1, each terminated by a semicolon.

So that the loop condition can be evaluated, the variable i is assigned a value before the while loop starts.

In this example, the loop condition is initially fulfilled, because i has the value 1 and 1 applies10. With System.out.println (i) the value of i, i.e. the 1, is output on the screen, followed by a line feed. With the instruction i = i + 1, the value of i is then increased by 1, i.e. set to 2.

The loop body is now finished. The program jumps back to the beginning of the while loop and checks the loop condition again. This is still fulfilled because it is 210, so the loop body is executed again, etc. - until i has reached the value 11, then the loop body is no longer executed.

The program sequence of a while loop can in turn be illustrated with the aid of a flow chart. The flow chart for the example program above is shown in Figure 1.

Fig. 1: Flowchart of the sample program (while loop)

For loop

Often times, loops are used to repeat instructions a certain number of times. In the previous example, something is output ten times. As we have seen, this can be done with a while loop. However, a more compact program code results when using the for loop:

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

In the For loop, the initialization of the variables (i = 1), the loop condition (i <= 10), and the action that should take place at the end of the loop body (i = i + 1) are combined.

The for loop has the general form

for (;;)
{
      1;
              
      ;
}

Which initialization is carried out, what the condition looks like, and what kind of action takes place is left to the programmer - all three parts can even be empty. However, it is good programming style to actually only use the for loop as in the example above, namely as a counting loop. In the case of a counting loop, a run variable receives an initial value and then "runs" up to an end value. In the example, the variable i runs from 1 to 10.

The for loop should not be used for loops that are not counting loops.

The for-each loop is also useful for traversing data structures such as arrays.