How do loops work in Java

Java: loops

A loop is a block of instructions whose instructions are repeated several times. The beginning and end are marked. How often the loop is repeated is specified in the loop condition, which can be at the beginning or at the end of the loop. A loop has the following structure:

Beginning of the loop (loop head)
Statement / statement block in the loop (loop body);
End of loop (loop foot);

for loop

The loop is suitable if it is already clear before the start of the loop how often the loop should be run through. Example: Our robot is supposed to pack 3 cars into a box at a time. The instructions for doing this look like this:

for (AutosGenommen = 0; AutosGenommen <3; AutosGenommen ++)
Take car;

In general terms:

for (int name = initial value; logical expression; value change)

All parameters that the loop needs are specified in the line: Initial value: At the beginning, the robot has not yet taken a car. Logical expression: It should repeat the instructions in the loop as long as the number is less than 3. Change in value: after each loop pass it is increased by one.

Java processes the loop as follows: The first run is 0, i.e. less than 3. The procedure is executed. Then "Java looks again in the loop head". is increased by 1, so it is now 1. This is still less than 3, so the loop is run through again and the procedure is executed again. And so on. After the 3rd time, it is increased to 3, so it is no longer less than 3. This means that the loop condition is no longer fulfilled. Java jumps behind the loop and continues there.
In our example, the loop body contains only one statement. Therefore, the curly brackets () can be omitted. If you have multiple statements, the loop looks like this:

for (...)

Statement after the loop;

This applies to all types of loops as well as to branches.

for-in loop

It is a special form of the loop. It goes through all the properties of an object. What an object is will be explained later.

for (variable in object)

while loop

The loop is suitable if it is not yet known how often the loop should be run when entering the loop.
A common application is reading tables. Java reads one line at a time. When the last line has been read (Java notices that it cannot find another line) the loop is exited. It would be unreasonable to set the number of loop iterations beforehand, because the table would have to be read to the end beforehand in order to determine the number of rows before the actual read loop begins.
In our example, the loop is suitable for fetching the cars:

EsSindAutosDa = true;
while (EsAutosDa)
EsSindAutosDa = Check ifAutosDaSind ();

do-while loop

This loop is required if you have an exit condition instead of an entry condition, which means that the condition is checked at the end of the loop run. In any case, the instruction is executed once here.

EsSindAutosDa = Check ifAutosDaSind ();
while (EsAutosDa)

That works if it is clear at the beginning that there are cars. The loop is run through at least once without checking the loop condition. Only then is the condition even noticed and a check is made as to whether the loop should be run through again.

do statement while (logical expression);

do {instructions;} while (logical expression);


int n = In.readInt ();
Out.print (n% 10);
n = n / 10;

while (n> 0);

Lables, break, continue

Sometimes it is necessary to break off the loop "in the middle", to leave it or to continue with the next iteration at the beginning of the loop. There are instructions and. They may only be in loops and differ in their effect: leaves the loop by continuing behind the loop foot, continues at the loop head and continues through the loop (unless a condition in the loop head prevents this.
Lables are jump labels. If I know that the program should jump to a very specific point, I can designate this point with a label and call it up. Such jumps are frowned upon among programmers because they make it difficult to understand. In most cases you can do without them.

Read more: ⯈ Variables and operators

about us