How to use for-loop in Java

Using a for-loop, a statement can be executed several times over in Java. It is mainly used when the total number of passes is known before execution. The required statements are placed inside the loop body.

Web hosting with a personal consultant

Fast and scalable, including a free domain for the first year and email address, trust web hosting from IONOS!

Domain
Wildcard SSL
24/7 support

What is the for-loop in Java?

The for-loop enables a Java application to perform an action until the point that a previously defined condition is met. To do this, a code is repeated inside the familiar curly brackets until the specified target value is reached.

Unlike other loops, such as the Java while-loop or the do-while loop, the for-loop in Java is mainly used when the number of required passes is known in advance. The starting point and the step size can also be specified with the for-loop, since it has an additional run variable in contrast to the while-loop.

Structure of for-loops in Java

The syntax of a for-loop in Java is always the same. The loop is introduced by the keyword “for”. This is followed in round brackets by the initialization of the run variable, then the termination condition and finally the change of the run variable. The three components are separated from each other by a semicolon. After the last expression, however, the semicolon should be omitted.

The executing instructions follow in the connection within the loop body, which is separated again by curly brackets. The basic structure is as follows:

for (initialization of the run variables; termination condition; change of the run variables) {
Statements
}

How does the for-loop work in Java?

The operation of for-loops in Java can be divided into four basic steps based on the stored expressions:

  1. Initialization: The first step of the Java for-loop is initialization. This step is executed only once.
  2. Termination condition: The termination condition is defined in advance and then checked on each pass. As long as the condition is true, the statement or an action is repeated. The moment the condition is false, the operation is terminated and the for-loop is completed by the Java application.
  3. Run variable: The run or count variable can increase or decrease. The value is modified on each run and checked again for the termination condition. If it proves false, the for-loop is exited and the program continues normally. However, as long as the check is true, the procedure is performed again.
  4. Repeat: This repeat is the fourth step. Each repetition starts again at the termination condition and subjects it to a new check.

Example of a for-loop

The easiest way to explain how a for-loop works in Java is to use the appropriate source code. In our example, the program is to count until we reach “5”. After that the for-loop should be stopped. This looks like this:

for (int i = 1; i <= 5; i++) {
Statement
}

“int i = 1” is the initialization. To specify that the number shouldn’t go above “5”, the termination condition for the number is specified by the Java operator with “less than or equal to 5” (

Note

The name “i” for the variable is the common name. However, you can give your variable a different name.

Add statement

Now add the appropriate statement so that the program knows how to proceed. This statement is written in the curly brackets and then controlled by the Java command “System.out.println”:

for (int i = 1; i <= 5; i++) {
System.out.println ("The value of the number is: " + i);
}

How the loop works

Now, if this for-loop is used in Java, the following happens: In the first pass, the value of the number is “0” and therefore less than “5”. The condition is fulfilled (true) and the process is repeated. The value is increased by “1” and is therefore “1”. The condition that the value must be less than “5” is still true. This continues until the value of “i” is increased to “6”. Now the condition that the value must be less than or equal to “5” is no longer true (false). Java then properly terminates the for-loop and continues.

For-each — the development of a for-loop

In addition to the normal for-loop in Java, there is the further development, for-each. For this loop, an array, i.e. a container with several objects of one data type, is required. The for-each loop can replace the for-loop in Java in some cases. The corresponding syntax looks like this:

for ( type variable : collection ) {
Code in which the variable is used
}

Differences between while- and for-loops in Java

A while-loop is also used to execute code until a certain condition is met. However, it only gets one condition inside the round brackets, so it can be a bit more confusing than the for-loop in some circumstances. Here is the structure of a while-loop:

int i = 0;
while ( i 
System.out.println ("The value of the number is: " + i);
}
Note

In Java, there are a total of four types of loops. In addition to the for-loop, the while- and for-each loops also listed here (also known as the extended for-loop), there is also the do-while loop. This is similar to the while-loop, but additionally checks the defined condition at the end of the statement block.