Java Loops Explained: while, do-while, for, for-each
1. Java Loop Types: while and for
Loops are a repeating part of a program that executes multiple times.
In Java, there are two main types of loops: "while-type" and "n-times-type".
The "while-type" is used when an action should repeat *while* a certain condition is true. Example: increase a number by 5 until it becomes three digits. This includes the while loop and do-while loop loops:
1. while (boolexpr) { /* statements */ }
2. do { /* statements */ }
while (boolexp);
The "n-times-type" is used when the number of iterations is known in advance. Example: multiply a number by itself 4 times. This includes for loop and for-each loop loops.
3. for(initialization; condition; iteration) { /* statements */ }
4. for(type variable : array) { /* statements */ }
The loop’s exit condition must be clear to avoid creating an infinite loop.
2. while Loop
The while loop is a "while-type" loop in Java.
It is used when a block of code should execute repeatedly *while* a specific condition is true.
Let's look at the syntax with an example.
After the keyword while
, in parentheses, you specify a condition that must return a boolean
value — this is the loop condition. Then, inside curly braces, you write the loop body — the code that will repeat as long as the condition evaluates to true
. In the example below, the condition is n > 0
, so the loop prints System.out.println
while n
is positive. The variable n
changes inside the loop body, eventually breaking the loop.
public class WhileExample1 {
public static void main(String[] args) {
int n = 10;
while (n > 0) {
System.out.println("Tick " + n--);
}
}
}
The condition of the while loop is checked before the loop body is executed. This is the main difference between while
and do-while loops.
The next example shows a while loop without a body. Given two numbers, 100 and 200, the goal is to find the midpoint. The value of i
increases and j
decreases until they become equal. The changes happen inside the condition itself, so no loop body is needed — a semicolon is used instead.
public class NoBodyExample {
public static void main(String[] args) {
int i = 100;
int j = 200; // find midpoint between i and j
while (++i < --j) ; // loop without a body
System.out.println("Midpoint: " + i);
}
}
while(true)
:public class EndlessLoopExample {
public static void main(String[] args) {
int i = 0;
while (true) {
System.out.println(i++);
}
}
}
3. do-while Loop
The do-while loop is similar to the while loop loop — it is also a "while-type" loop with a body and a condition. The key difference is that in a do-while loop, the condition is checked after the body is executed. This means the do-while body will execute at least once, even if the condition evaluates to false
.
Example of a do-while loop:
public class DoWhileExample {
public static void main(String[] args) {
int n = 10;
do {
System.out.println("Tick " + n--);
} while (n > 0);
}
}
4. for Loop
The for loop in Java is used when you need to execute a block of code multiple times and the number of repetitions is known in advance.
General syntax of the for loop:
for(initialization; condition; iteration) { /* statements */ }
Example of a for loop:
public class ForTickExample {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Tick " + i);
}
}
}
The first parameter usually declares a variable that serves as a loop counter. This variable is often named i
and initialized with a starting value. It is not recommended to modify the loop index inside the body of a Java for
loop.
The second parameter sets the loop's condition — it defines how long the loop should run based on the counter's value.
The third parameter contains an expression that updates the counter after each loop iteration. This is typically an increment or decrement but can be any valid update expression.
Before each iteration (except the first one, where only the initialization runs), the condition is checked. If it's true, the loop body executes again. If it's false on the first check, the loop body won't run at all.
In the initialization section, you can declare and initialize multiple variables using a comma, as shown in the following example:
public class ForExample {
public static void main(String[] args) {
for (int i = 1, j = 4; i < j; i++, j--) {
System.out.println("i = " + i);
System.out.println("j = " + j);
}
}
}
5. for-each Loop
The for loop in Java is used when you need to execute a block of code multiple times and the number of repetitions is known in advance.
General syntax of the for loop:
for(initialization; condition; iteration) { /* statements */ }
Example of a for loop:
public class ForTickExample {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Tick " + i);
}
}
}
The first parameter usually declares a variable that serves as a loop counter. This variable is often named i
and initialized with a starting value. It is not recommended to modify the loop index inside the body of a Java for
loop.
The second parameter sets the loop's condition — it defines how long the loop should run based on the counter's value.
The third parameter contains an expression that updates the counter after each loop iteration. This is typically an increment or decrement but can be any valid update expression.
Before each iteration (except the first one, where only the initialization runs), the condition is checked. If it's true, the loop body executes again. If it's false on the first check, the loop body won't run at all.
In the initialization section, you can declare and initialize multiple variables using a comma, as shown in the following example:
public class ForExample {
public static void main(String[] args) {
for (int i = 1, j = 4; i < j; i++, j--) {
System.out.println("i = " + i);
System.out.println("j = " + j);
}
}
}

Please log in or register to have a possibility to add comment.