Introduction to For Loop In Java
In Java, a for loop is a control flow statement that allows you to repeat a block of code a specific number of times. It is one of the most widely used loops in programming because it is compact, readable, and easy to manage.
The for loop is especially useful when you know in advance how many times a task should be executed, such as printing numbers from 1 to 10 or iterating through elements of an array.
With a for loop, you can perform repetitive tasks without writing the same code multiple times, which makes your programs clean, organized, and maintainable.
Syntax of For Loop In Java
for (initialization; condition; update) {
// Code to execute in each iteration
}
- Initialization: This part runs only once at the start of the loop. It is typically used to create and set the starting value of the loop control variable. Example: int i = 0 initializes a counter i to 0.
- Condition: The condition is checked before every iteration. If it evaluates to true, the loop executes its body, and if it evaluates to false, the loop stops immediately.
- Update: This part is executed after each iteration of the loop body. It usually changes the loop control variable to eventually make the condition false and stop the loop.
Examples of For Loop
Example 1: Print Numbers from 1 to 5
public class ForLoopSyntaxExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Example 2: Sum of First 10 Natural Numbers
This program calculates the sum of the first 10 natural numbers (1 to 10) using a simple for loop.
Code example:
public class SumOfNumbers {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 10; i++) {
total = total + i; // adding each number to total
}
System.out.println("The sum of first 10 natural numbers is: " + total);
}
}
Output:
The sum of first 10 natural numbers is: 55
- The loop starts with i = 1 and runs till i <= 10.
- In each iteration, the value of i is added to the total.
- When the loop finishes, the program prints the final sum (which is 55).
Example 3: Printing an Array Using For Loop
This program shows how to display all elements of an array using a for loop. Arrays store multiple values, and using a loop, you can easily access each element one by one.
Code example:
public class DisplayArray {
public static void main(String[] args) {
int[] marks = {85, 90, 78, 92, 88};
for (int index = 0; index < marks.length; index++) {
System.out.println("Mark at position " + index + " = " + marks[index]);
}
}
}
Output:
Mark at position 0 = 85
Mark at position 1 = 90
Mark at position 2 = 78
Mark at position 3 = 92
Mark at position 4 = 88
- The marks array stores five integers.
- The loop starts from index 0 and continues until it reaches the last index (marks.length – 1).
- In each iteration, the program prints the value stored at the current index.
Example 4: Nested For Loop (Loop Inside Another Loop)
A nested loop means a loop inside another loop. It is useful when you want to repeat actions in multiple dimensions, for example, printing a table or pattern.
Code example:
public class NestedLoopDemo {
public static void main(String[] args) {
for (int outer = 1; outer <= 3; outer++) { // outer loop
for (int inner = 1; inner <= 2; inner++) { // inner loop
System.out.println("Outer: " + outer + ", Inner: " + inner);
}
}
}
}
Output:
Outer: 1, Inner: 1
Outer: 1, Inner: 2
Outer: 2, Inner: 1
Outer: 2, Inner: 2
Outer: 3, Inner: 1
Outer: 3, Inner: 2
Understand this code logic:
- The outer loop runs 3 times.
- For every single run of the outer loop, the inner loop runs 2 times.
- This means the total output lines = 3 × 2 = 6.
Enhanced For Loop (For-Each Loop)
The enhanced for loop, also called the for-each loop, is a simplified version of the traditional for loop.
It is mainly used to traverse arrays and collections like ArrayList, without needing to deal with indexes manually.
It automatically picks each element from the collection one by one, making your code cleaner, shorter, and easier to read.
Syntax of For-Each Loop
for (type variable : array) {
// Code to execute for each element
}
Explanation of this syntax:
- type is the data type of the elements in the array or collection.
- variable is a temporary variable that holds the current element.
- arrayOrCollection is the array or list you want to loop through.
Example: Displaying Marks of Students Using For-Each Loop
public class StudentMarks {
public static void main(String[] args) {
int[] marks = {88, 92, 76, 81, 95};
System.out.println("Displaying student marks:");
for (int mark : marks) {
System.out.println("Mark: " + mark);
}
}
}
Output:
Displaying student marks:
Mark: 88
Mark: 92
Mark: 76
Mark: 81
Mark: 95
In this code:
- The array marks stores the scores of five students.
- The for-each loop automatically picks one mark at a time from the array.
Advantages of For Loop
- Compact and Readable:
- All the important loop components like initialization, condition, and update are written in a single line. This makes the loop short, clear, and easy to understand at a glance.
- Control Over Iterations:
- The for loop gives you complete control over how many times your code runs. You can start and end the loop wherever you want, skip values, or even change the increment logic.
- Flexibility:
- The for loop isn’t limited to simple counting. You can use it for mathematical calculations, iterating through arrays, nested loops for patterns or multi-dimensional arrays, and conditional checks inside complex logic
Also Learn Important Topics of Java
- What are the operators in Java Programming?
- What are Data Types in Java?
- What are the Variables in Java?
- Learn Java syntax.
- What is Java if else statements?

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.