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 state­ments are placed inside the loop body.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

What is the for-loop in Java?

The for-loop enables a Java ap­pli­ca­tion to perform an action until the point that a pre­vi­ous­ly 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 ad­di­tion­al 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 in­tro­duced by the keyword “for”. This is followed in round brackets by the ini­tial­iza­tion of the run variable, then the ter­mi­na­tion condition and finally the change of the run variable. The three com­po­nents are separated from each other by a semicolon. After the last ex­pres­sion, however, the semicolon should be omitted.

The executing in­struc­tions follow in the con­nec­tion 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 ex­pres­sions:

  1. Ini­tial­iza­tion: The first step of the Java for-loop is ini­tial­iza­tion. This step is executed only once.
  2. Ter­mi­na­tion condition: The ter­mi­na­tion 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 ter­mi­nat­ed and the for-loop is completed by the Java ap­pli­ca­tion.
  3. Run variable: The run or count variable can increase or decrease. The value is modified on each run and checked again for the ter­mi­na­tion 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 rep­e­ti­tion starts again at the ter­mi­na­tion 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 ap­pro­pri­ate 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 ini­tial­iza­tion. To specify that the number shouldn’t go above “5”, the ter­mi­na­tion 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 ap­pro­pri­ate statement so that the program knows how to proceed. This statement is written in the curly brackets and then con­trolled 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 ter­mi­nates the for-loop and continues.

For-each — the de­vel­op­ment of a for-loop

In addition to the normal for-loop in Java, there is the further de­vel­op­ment, 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 cor­re­spond­ing syntax looks like this:

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

Dif­fer­ences 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 cir­cum­stances. 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 ad­di­tion­al­ly checks the defined condition at the end of the statement block.

Go to Main Menu