A for loop in Java is used to execute a block of code repeatedly. It is especially useful when we know how many times we want to repeat a task.
Suppose you want to print "Hello Java" five times. Instead of writing five print statements, you can use a loop.
for (int i = 1; i <= 5; i++) {
System.out.println("Hello Java");
}
Output:
Hello Java
Hello Java
Hello Java
Hello Java
Hello Java
for (initialization; condition; update) {
// Code to execute repeatedly
}
Example:
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
| Part | Meaning |
|---|---|
| int i = 1 | Starts the counter at 1. |
| i <= 5 | Continues while i is less than or equal to 5. |
| i++ | Increases i by 1 after each iteration. |
| System.out.println(i) | Prints the current value of i. |
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10
You can use a for loop to print numbers from 10 to 1.
for (int i = 10; i >= 1; i--) {
System.out.println(i);
}
Output:
10
9
8
7
6
5
4
3
2
1
i-- decreases the value of i by 1.
We can print even numbers from 2 to 20 by increasing the counter by 2.
for (int i = 2; i <= 20; i += 2) {
System.out.println(i);
}
Output:
2
4
6
8
10
12
14
16
18
20
i += 2 adds 2 to the current value of i.
This program calculates the sum of numbers from 1 to 10.
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum = sum + i;
}
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 55
The variable sum stores the running total.
Print the multiplication table of 5.
public class Main {
public static void main(String[] args) {
int number = 5;
for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i
+ " = " + (number * i));
}
}
}
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
A for loop can access array elements using their indexes.
public class Main {
public static void main(String[] args) {
String[] names = {"Amit", "Rahul", "Priya", "Neha"};
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
}
Output:
Amit
Rahul
Priya
Neha
names.length gives the number of elements.
You can initialize and update two variables in one for loop.
for (int i = 1, j = 10; i <= 5; i++, j--) {
System.out.println("i = " + i + ", j = " + j);
}
Output:
i = 1, j = 10
i = 2, j = 9
i = 3, j = 8
i = 4, j = 7
i = 5, j = 6
An infinite loop continues indefinitely because its condition never becomes false.
for (;;) {
System.out.println("Hello Java");
}
This loop has no initialization, condition, or update expression.
| Mistake | Correction |
|---|---|
Using i < 5 when you need 1 to 5 |
Use i <= 5 when starting at 1. |
| Forgetting to update the counter | Include an update such as i++. |
Using i <= array.length |
Use i < array.length. |
| Adding an unnecessary semicolon after the loop header | Write the opening brace directly after the header. |
Incorrect example:
for (int i = 1; i <= 5; i++); {
System.out.println(i);
}
The semicolon ends the loop body. Also, in this example,
i is not in scope inside the following block.
Remove the semicolon and keep the print statement inside the loop.
Write Java programs to solve these beginner-level assignments.
1. Which keyword is used to create a for loop in Java?
2. What does i++ do?
3. How many times does this loop run?
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}