Back to: Java Tutorials
The for
loop is a control flow statement in Java that allows a program to execute a block of code repeatedly. It is used when you know the number of times you want to execute a block of code. The basic syntax of the for
loop is as follows:
for (initialization; condition; increment/decrement) {
// code to be executed
}
Here’s what each part of the for
loop does:
initialization
: This is where you initialize the loop counter variable. It is usually set to 0 or 1.condition
: This is the condition that is checked at the beginning of each loop iteration. If the condition is true, the loop continues to execute. If it is false, the loop exits.increment/decrement
: This is where you increment or decrement the loop counter variable at the end of each loop iteration.
Here’s an example program that uses a for
loop to print the numbers from 1 to 10:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
}
}
In this program, the for
loop initializes the loop counter variable i
to 1. It checks the condition i <= 10
, which is true since i
is 1. It then executes the block of code within the loop, which prints the value of i
to the console using System.out.print()
. At the end of the first iteration, it increments the value of i
by 1 using i++
. It then checks the condition again, which is true since i
is now 2. It continues to execute the loop until i
is no longer less than or equal to 10.
The output of the program is as follows:
1 2 3 4 5 6 7 8 9 10
You can also use a for
loop to iterate through arrays and collections. Here’s an example program that uses a for
loop to iterate through an array of integers and calculate their sum:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
System.out.println("The sum is: " + sum);
}
}
In this program, the for
loop iterates through the numbers
array using the loop counter variable i
. It uses numbers.length
as the condition to check when to exit the loop. This ensures that the loop only iterates through the number of elements in the array. It adds each element in the array to the sum
variable using the +=
operator. At the end of the loop, it prints the value of the sum
variable to the console.
The output of the program is as follows:
The sum is: 15
As you can see, the for
loop can be a very powerful tool for controlling the flow of execution in a Java program.
🗒️Note: Increment or decrement in Java for loop
The increment or decrement in a for
loop can be positive or negative. This determines the direction in which the loop counter variable is changed with each iteration.
A positive increment or decrement increases the value of the loop counter variable with each iteration, while a negative increment or decrement decreases the value of the loop counter variable with each iteration. Here are examples of each:
Positive increment:
for (int i = 0; i < 10; i++) {
// code to be executed
}
In this for
loop, the loop counter variable i
is initialized to 0, and then incremented by 1 at the end of each iteration using the i++
operator. This means that i
will have the values of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 in successive iterations.
Negative increment:
for (int i = 10; i > 0; i--) {
// code to be executed
}
In this for
loop, the loop counter variable i
is initialized to 10, and then decremented by 1 at the end of each iteration using the i--
operator. This means that i
will have the values of 10, 9, 8, 7, 6, 5, 4, 3, 2, and 1 in successive iterations.
You can use either positive or negative increments or decrements depending on the requirements of your program. For example, if you need to iterate through an array or collection in reverse order, you would use a negative increment.
Here’s an example of a for
loop that uses a negative increment to print the elements of an array in reverse order:
int[] array = {1, 2, 3, 4, 5};
for (int i = array.length - 1; i >= 0; i--) {
System.out.print(array[i] + " ");
}
In this example, the loop counter variable i
is initialized to array.length - 1
, which is the index of the last element in the array. It is then decremented by 1 at the end of each iteration using the i--
operator. This means that i
will have the values of 4, 3, 2, 1, and 0 in successive iterations, which are the indices of the elements of the array in reverse order. The loop then prints the elements of the array in reverse order using the array[i]
expression.
The output of the program is as follows:
5 4 3 2 1
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author