Last Updated: 28 May, 2022
In Java or any other programming languages, loops play an important role in executing a set of instructions multiple times based on the given condition. Inside a loop block, the set of instructions is fixed and it executes repeatedly while some condition evaluates to true.
For Example, Suppose We have to print the same message 100 times, then rather than typing the same code 100 times, we can use a loop here.
Java provides three basic types of loops:
In this tutorial we are going to discuss for loop with definition, diagram and example.
Java for loop is used to run a set of instructions for a certain number of times as long as given condition is true. A for Loop is consist of three parts Initial Expression, Test Expression (Boolean condition) and Update Expression.
Java for loop Syntax:
Java for Loop execution flow:
Flowchart of Java for Loop
Example 1 : Java for Loop Program
Output
javabytechie.com
javabytechie.com
javabytechie.com
javabytechie.com
javabytechie.com
Java also allows to write one for loop inside another for loop, it is knowns as Nested for loop or Inner for loop in Java.
Syntax of Nested for loop in Java
Now, let's understand Nested for loop through the given example.
Example 2 : Java Nested for Loop
Output
Outer for loop
Inner for loop
Inner for loop
Inner for loop
Inner for loop
Outer for loop
Inner for loop
Inner for loop
Inner for loop
Inner for loop
Outer for loop
Inner for loop
Inner for loop
Inner for loop
Inner for loop
We can also use more than one variable in the for loop initialization, test expression, and for the step value. In this type of for loop, we can have only one boolean test expression.
Example 3 : Multiple for Loop Variables
Output
Sum of (i + j + k) = 3
Sum of (i + j + k) = 6
Sum of (i + j + k) = 9
Sum of (i + j + k) = 12
Sum of (i + j + k) = 15
Sum of (i + j + k) = 18
A loop that does not terminate that loop is known as an Infinite loop in Java.
Example 4 : Infinite for Loop in Java
Output
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
It's an Infinite Loop
...
...
...
We can use continue and break statements inside for loop according to our requirement.
In Java, the continue statement is mainly used inside the loops such as for loop, while loop, do-while loop, etc. The purpose of the continue statement is to continue the current running flow of the Java program and skip the rest of the code at the specified condition.
A break statement is used to terminate the current processing of the loop, after the termination control returns from the loop immediately to the next statement after the loop.
Let's understand the below Java program where we are iterating a loop from 1 to 30 but printing the even value from 11-20.
Example 5 : Continue and Break Statements inside for Loop
Output
Even Number: 12
Even Number: 14
Even Number: 16
Even Number: 18
Even Number: 20
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com