-
Hajipur, Bihar, 844101
In Java, a for loop is one of the most frequently used looping structures. It allows you to execute a block of code repeatedly for a specific number of times. Unlike the while loop, where the loop variable is initialized and updated separately, the for loop manages initialization, condition checking, and updating all in one line. This makes it compact and easier to read, especially when the number of iterations is known in advance.
The general syntax of the for loop in Java is:
for (initialization; condition; update) {
// code to be executed
}
Here’s what each part means:
Initialization: Used to set the starting value of the loop control variable.
Condition: The loop continues to execute as long as this condition is true.
Update: Changes the loop control variable after every iteration.
A simple example of the for loop is printing numbers from 1 to 5:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Output:
1
2
3
4
5
Here, i is initialized to 1, and as long as i <= 5, the loop executes. After each iteration, i increases by 1 due to the i++ statement.
Let’s break down how the for loop works internally:
The initialization runs once before the loop starts.
The condition is checked; if it’s true, the loop body executes.
After execution, the update statement runs.
The condition is checked again, and the cycle repeats.
When the condition becomes false, the loop stops.
This sequence ensures that the for loop executes in a controlled and predictable manner.
You can easily print even numbers using a for loop:
for (int num = 2; num <= 10; num += 2) {
System.out.println(num);
}
Output:
2
4
6
8
10
Here, the loop starts from 2 and increases by 2 each time until it reaches 10.
Here’s a simple example of how to calculate the sum of numbers using a for loop:
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
System.out.println("Sum = " + sum);
Output:
Sum = 15
The program adds each number from 1 to 5 and prints the total sum.
The for loop doesn’t always have to count upwards. You can make it count down by using the decrement operator.
Example:
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
Output:
5
4
3
2
1
In this case, i starts from 5 and decreases by 1 in each iteration until it becomes less than 1.
A nested for loop means placing one for loop inside another. This is useful for working with tables, grids, or patterns.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print("(" + i + "," + j + ") ");
}
System.out.println();
}
Output:
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
The outer loop controls the number of rows, and the inner loop controls the number of columns.
For loops are often used to iterate through arrays.
Example:
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Output:
10
20
30
40
50
The loop runs until the index reaches the array’s length, printing each element.
Java also provides an enhanced for loop, also known as the for-each loop, which is a simpler way to iterate over arrays or collections.
Example:
int[] marks = {85, 90, 78, 92};
for (int mark : marks) {
System.out.println(mark);
}
Output:
85
90
78
92
This loop automatically handles the iteration and works well when you don’t need to track the index.
A for loop can also be written to run forever by skipping all expressions.
Example:
for (;;) {
System.out.println("Infinite Loop");
}
Since there is no condition to stop the loop, it keeps running until the program is manually terminated.
Java allows you to use multiple variables inside a for loop.
Example:
for (int i = 1, j = 5; i <= j; i++, j--) {
System.out.println(i + " " + j);
}
Output:
1 5
2 4
3 3
Here, both variables i and j are updated simultaneously after each iteration.
Using the wrong condition can lead to loops that run too long or never execute.
Forgetting to increment or decrement the loop variable can cause an infinite loop.
Modifying the loop variable inside the loop body can make the code unpredictable.
The for loop in Java is ideal when the number of iterations is known. It combines initialization, condition checking, and updating in a single statement, making the code concise and efficient. With variants like the enhanced for loop, Java provides flexibility to handle both simple and complex repetitive tasks efficiently.
Write a Java program using a for loop to print numbers from 1 to 50. Make sure each number appears on a new line.
Create a Java program using a for loop to print all even numbers between 1 and 100.
Write a program that prints the sum of all odd numbers between 1 and 50 using a for loop.
Develop a Java program that prints the multiplication table of a given number using a for loop. For example, if the number is 7, display the table from 7 × 1 to 7 × 10.
Create a program using a for loop that calculates the factorial of a given number entered by the user. For example, if the number is 5, print “Factorial = 120.”
Write a program that takes an integer input and prints whether it is a prime number or not using a for loop.
Develop a Java program that reverses a given number using a for loop. For example, if the input is 6789, the output should be 9876.
Write a program using a nested for loop to print a simple square pattern of stars (*) with 5 rows and 5 columns.
Create a program that finds the largest element in an integer array using a for loop.
Write a program using a for loop that generates the Fibonacci series up to 10 terms and prints the sequence on the screen.