In this lesson of our Java course, we will learn about Java for Loop. In Java, loops are used to execute a block of code repeatedly until a certain condition is met. One of the loop types available in Java is the for loop.
A Java for loop is a control flow statement that allows you to iterate over a sequence, such as an array or a list, and perform a set of instructions for each element in the sequence. This can be useful for tasks, such as processing data or performing calculations. In Java, the for loop is a popular choice for iterating over arrays and lists, as well as for performing repetitive tasks.
1. Java for Loop Syntax
Let’s look at the syntax of a for loop in Java:
for (initialization; condition; increment/decrement) {
// code to be executed
}
- The initialization section is executed before the loop starts and is used to initialize variables or perform other setup tasks.
- The condition section is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop will be executed. If the condition is false, the loop will exit.
- The increment/decrement section is executed after each iteration of the loop and is used to update the variables used in the condition section.
2. How Java for Loop Works?
Let’s look at the following flowchart to understand how java for loop works.Here’s an example of a for loop in Java that iterates over an array of integers and prints each element:
2.1. for Loop Example
int[] numbers = {
1,
2,
3,
4,
5
};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
In this example, the loop starts by initializing the variable i
to 0
. The condition i < numbers.length
is evaluated before each iteration of the loop, and as long as i
is less than the length of the numbers array, the code inside the loop will be executed. The code inside the loop prints the element at the current index of the numbers array.
After each iteration, i
is incremented by 1 using i++
. Once i is no longer less than the length of the numbers array, the loop exits. The output of this example will be:
1
2
3
4
5
Here is another example of our Java for loop to provide you with more understanding.
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i + 1;
}
System.out.println("The sum of numbers from 1 to 10 is: " + sum);
In this example, the loop starts by initializing the variable i
to 0
. The condition i <= 10
is evaluated before each iteration of the loop, and as long as i is less than or equal to 10
, the code inside the loop will be executed. The code inside the loop adds the value of i to the variable sum on each iteration. After each iteration, i is incremented by 1 using i++
. Once i is greater than 10
, the loop exits. After the loop exit, it will print “The sum of numbers from 1
to 10
is: 55
“
Summary
In conclusion, the Java for loop is a powerful control flow statement that allows you to iterate over sequences and perform repetitive tasks in Java. The syntax, flowchart, and example provided above should give you a good understanding of how to use the for loop in your Java programs. The for loop is a powerful tool that can be used in many situations and it’s a good idea to be familiar with the syntax and the flowchart of the for loop. As Always the source code for this Java course is available on our GitHub repository.