The for loop is one of the most commonly used control flow structures in Java. It provides a compact way to iterate over a sequence of values, perform repetitive tasks, and execute code a specific number of times. In this guide, we will explore how the for
loop works in Java, its syntax, and various examples to help you understand its usage.
The for loop is a control flow statement that is used to repeatedly execute a block of code a fixed number of times. It is commonly used when the number of iterations is known beforehand. The loop iterates over a range of values, typically incrementing or decrementing a counter variable until a specified condition is met.
The syntax of the for
loop consists of three parts:
Here is the general syntax of the for
loop in Java:
for (initialization; condition; update) {
// block of code to be executed
}
int i = 0
).i < 5
). The loop continues as long as the condition is true.i++
to increment).The traditional for
loop is used when you know the exact number of iterations.
for (int i = 0; i < 5; i++) {
// code to be executed
}
The enhanced for loop (also called the for-each loop) simplifies iteration over collections, arrays, or other iterable data structures. It eliminates the need for an index counter and makes the code more readable.
for (type element : array_or_collection) {
// code to be executed for each element
}
The enhanced for
loop is especially useful when you need to iterate through arrays or collections without modifying the index.
Let’s take a look at some examples to understand how the for
loop works in Java.
public class ForLoopExample {
public static void main(String[] args) {
// Simple for loop to print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}
Explanation:
i = 1
.i <= 5
ensures the loop runs until i
reaches 5.i
is incremented (i++
).
1
2
3
4
5
public class ForLoopSumExample {
public static void main(String[] args) {
int sum = 0;
// Using the for loop to calculate the sum of numbers from 1 to 10
for (int i = 1; i <= 10; i++) {
sum += i; // Add the value of i to sum
}
System.out.println("Sum of numbers from 1 to 10: " + sum);
}
}
Explanation:
for
loop.sum
accumulates the total sum.
Sum of numbers from 1 to 10: 55
public class ForLoopArrayExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
// Enhanced for loop to iterate through the array and print each number
for (int num : numbers) {
System.out.println(num);
}
}
}
Explanation:
for
loop is used to iterate through the array numbers
.
10
20
30
40
50
A nested for loop is when you use one for
loop inside another. This is useful for iterating over multi-dimensional arrays or when you need to perform more complex operations with multiple loops.
public class NestedForLoopExample {
public static void main(String[] args) {
// Print multiplication table of 1 to 5
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(i * j + "\t"); // Print the product of i and j
}
System.out.println(); // Move to the next line after each row
}
}
}
Explanation:
i
and inner loop variable j
.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
i
, j
, count
).for
loop is ideal for iterating through collections and arrays where you don't need to manipulate the index.