In a java programming language, loops are the function to execute statements on the condition. Loops in java iterate several times till the condition are satisfied. In java programming language, there are four types of loops.
Types of Loops in Java
- While loop
- Do-while loop
- For loop
- For each loop
While loop in java
In java, while-loop will first check the condition, If the condition is true means conditions are satisfied then the statements block will execute.
After executing the statement block, then the loop will check the condition again, if the condition is true or satisfied then the statement block will execute again. This process will repeat until the condition is false or not satisfied.
The following is the syntax of while loop
Syntax of the while-loop
while(condition) {
// statement block
}
Diagram of while loop
Java program for the while loop
public class WhileLoop
{
public static void main(String[] args)
{
int number = 1;
while (number <= 5)
{
// statement block
System.out.println("number = " + number);
number++;
}
}
}
In the above java program example, we have created a class in java with the name WhileLoop. Here we have declared one java variable i.e number and initialize to 1.
To execute while loop in java, we have created while loop as shown in the above code and condition (number <= 5 ) we are Iterating or looping until the number is less than or equal to 5.
Comprehensive knowledge of the while loop program
In the above program, we have one variable such as number, which is initialized to 1.
Now in a while loop, we have one condition if number <= 5. So, here while loop will check the first condition, if number <=5 and in our program, we initially assign a number to 1.
While loop will check condition as 1 <=5. At this time condition is true, now while loop will execute statement block and will print number = 1, and then we have an incremental number variable which means the number value will increment by 1.
At the second iteration ofthe while loop number value will be 2. when the while loop will check the condition, then the condition will look like this 2 <=5. this process will repeat until number = 5.
Once a number is incremented to 6, then the while loop condition will be false, as 6 is neither equal to 5 nor less than 5 (6 <= 5). Now while loop will not execute the statement block.
Once you, execute this java program in your machine you will get the following output
Output of the above program
number = 1 number = 2 number = 3 number = 4 number = 5(code-box)
Do while loop in java
In a java programming language, The do-while loop is similar to while loop but there are one simple changes done in a do-while loop as compare to a while loop
As the name do-while, the do-while loop will first execute the statement block and then it will check the condition
Once a condition is false, the do-while loop will not execute a statement block anymore.
Syntax of the do-while loop
do
{
//statement block;
}
while (condition);
Diagram of the do-while loop
Java program for do-while loop
public class DoWhileLoop
{
public static void main(String[] args)
{
int number = 1;
do
{
//statement block
System.out.println("number = " + number);
number++;
}
while(number <=5);
}
}
In the above Java programs, we took the same example of while loop
Here in the do-while loop, it will first execute the statement block and then it will check the condition
Comprehensive knowledge of the do-while loop program
In the above java program, we have one variable i.e number. This program will first execute the statement block, which is under the do. After executing the statement block program will check while condition.
Once while the condition is true or satisfied then,The statement block will execute again
The statement block will execute until the while condition is false, just like the while loop
If you run the above program in your machine then, you will get the following output
Output of the above java program
number = 1
number = 2
number = 3
number = 4
number = 5
For loop in java
For loop in java is the simple loop, which has initialization, condition, statement, and increment/decrement
Syntax of for loop in java
for(initialization, condition, increment/decrement) { // statement block }(code-box)
Initialization
: It is the initialization of the variable. Here we can initialize variable or if we have already variable in local or global then we can use that also.
Condition
: Here we declare condition for the loop. This condition will execute each time to verify the condition is true or false. If the condition is true then the statement block will execute else it will end the loop.
Statement
: This is the statement block, here you can write java code.
Increment/Decrement
: It's increment or decrement variable.
Diagram of for loop
Java program of for loop
public class ForLoop
{
public static void main(String[] args)
{
for (int number = 1; number <=5; number++)
{
// statement block
System.out.println("number = " + number);
}
}
}
Comprehensive knowledge of the for loop in java
In the above program, under the for loop, we are initializing number to 1, providing condition number <-=5 and the number is incrementing
At first, for loop will initialize the number, then check the condition, if the number is less then or equal to 5 then, for loop will execute the statement block. Here at this point number is 1, the condition will check 1<=5, thus the condition is true now statement block will execute and will print number = 1
Once statement block execution finished, then the number will get increment by 1, Now number value will be 2
A second iteration, the condition will check as 2<=5 and the condition is true, statement block will execute and will print number = 2 and also number value will get increment by 1. Thus, the number value will be 3
This process will repeat until the condition doesn't satisfied. Once the condition will give false value then, for loop will stop executing statement block
Conclusion
In this article, we learn what is loops in java and what are the different type of loops. We learn how to create a loop in java program. Please don't stop here, explore more about the loop in java and take a hand on experience in creating loops. Try to create some start pattern using a loop.
Thanks for reading and keep learning
(getButton) #text=(Next: For-each loop in java) #icon=(link) #color=(#2339bd)