Loops in Java: For, While, Do-While, For-Each Explained
for
, while
, do-while
, and for-each
—each serving different purposes depending on the situation. This tutorial provides clear examples and explanations of how each loop works and when to use them in your Java programs.
For Loop
- For loop is used to execute set of statements for specific number of times
- In the example below the execution starts when the value of i = 0 and continues till i <= 10 and value of i increments by 1 with each iteration.
for (int i = 0; i <= 10; i = i + 1) {
System.out.println(i);
}
For Each Loop
- It is used to loop through elements of an Array and Collections.
- It is also known as enhanced for loop.
- In the example below there are four elements in the numbers array. So the loop executes four times printing each number.
// create an array
int[] numbers = {3, 9, 5, -5};
// for each loop
for (int number: numbers) {
System.out.println(number);
}
While Loop
- It loops through the statements till the specified condition is true
- In the example below the execution will start with the value of i as 1 and repeat till i <= n. With each iteration the value of i is being incremented by 1. So the loop will execute 100 times.
int i = 1, n = 100;
// while loop from 1 to 100
while(i <= n) {
System.out.println(i);
i++;
}
Do While Loop
- It is similar to while loop however the body is executed first before the condition is checked.
- In the example below the execution starts without checking any condition and condition i <= n is checked only after the iteration. Value of i increments by 1 with each iteration so this loop will execute 5 times.
int i = 1, n = 5;
// do...while loop from 1 to 5
do {
System.out.println(i);
i++;
} while(i <= n);
When to Use Each Loop
for | Known number of iterations |
for-each | Traversing arrays/collections |
while | Unknown number of iterations |
do-while | At least one execution guaranteed |
for
, while
, do-while
, and for-each
loops helps you write cleaner, more efficient code. Try practicing each loop type with your own examples to strengthen your understanding. Explore more tutorials on itcodescanner.com to master other core Java concepts.FAQ Section
Q1. What's the difference between for and while loop in Java?
A for
loop is used when you know the number of iterations, while a while
loop is used when the condition depends on runtime evaluation.
Q2. Can a do-while loop run zero times?
No. A do-while
loop always runs at least once because the condition is checked after executing the loop body.
Q3. When should I use a for-each loop in Java?
Use for-each
when you need to iterate through elements of arrays or collections without modifying the index.
Q4. Can I break out of a loop early?
Yes. You can use the break
statement to exit any loop prematurely, or continue
to skip the current iteration.
Q5. Are loops only used for numbers?
No. Loops can iterate over arrays, strings, collections, or any condition that evaluates to true
or false
.
Leave a Comment