In this tutorial, we will be learning what are Loops in Java. It is one of the important fundamental concepts of programming. After this tutorial, you will have a clear understanding of the concept of loops and also know how to implement them in a Java program.
What are Loops in Java?
A Loop in Java is a repeated execution of one or more statements. By the nature of a loop, we can understand that a loop is something that repeats again and again. In the same way, there are a few ways of executing statements again and again in Java.
Why do we need to loop in Java?
Consider a scenario in which you want a statement to execute 10 times. One of the ways to accomplish this is to type that statement 10 times and then run the program. This would seem easy but it will get difficult if there is a need to execute it 100000 times. Isn’t it?
To tackle such scenarios, loops are used in Java. Basically, there are 3 ways to execute loops in a Java program. They are:
- For Loop
- While Loop
- Do-while Loop
For Loop in Java
The For Loop is one of the control flow statements in Java that allows execution a code repeatedly. The syntax of a For Loop has 4 parts. Each part has its own meaning and place.
Initialization
This part of the For Loop is generally used for initializing a variable. It is meant for the starting point of a For Loop.
Condition
This is an important part of a loop. It decides whether the looping must carry on or terminate. A wrong condition may sometimes put the program into an infinite loop.
Increment/Decrement
This part of the For Loop is generally used to increment or decrement certain variable that keeps the count of how many time the loop has executed.
Body of the Loop
This part actually contains the code which gets executed repeatedly in a For Loop.
Syntax
for (Initialization; Condition; Increment/Decrement) { //Body of the For Loop }
Example
class Program { public static void main(String agmts[]) { for (int i=1; i<=5; i++) { System.out.println(i); } // Other Statements } } /* Output 1 2 3 4 5 */
Working of a For Loop using the above example
- The For Loop gets Initialized by initializing a local variable ‘i’ and assigning it with the value 1.
- The For Loop then checks for the condition if it is true or not. If the condition is true, then the body of the For Loop gets executed, if not then the For Loop terminates and other statements start getting executed.
- By looking at the condition part of the example, we can say that it is true for the first time i.e. 1<=5 (1 is less than or equal to 5). So, the body of the loop gets executed and prints 1 on the console.
- After executing the body, the Increment/Decrement part of the For Loop gets executed. According to the example, the value of ‘i’ gets incremented by 1 (i++). This means after every loop the value of ‘i’ will be 1 plus the previous value of ‘i’.
- The loop gets repeatedly executed and also prints 2, 3, 4, and 5.
- When the value of the variable ‘i’ is 6, the condition fails i.e. (6<=5) which is false. At this point, the For Loop terminates and the control goes to the other statement that is after the For Loop.
While Loop in Java
The While Loop is similar to a For Loop, but with a few differences.
A while loop has no Initialization or Increment/Decrement part (which was there in a For Loop). It has basically two parts, they are:
Condition
This part decides whether to continue to loop or terminate the loop.
Body of the Loop
This is the part that gets executed again and again when the Condition part of the While loop is true.
Syntax
while(Condition) { // Body of the While Loop }
Example
class prog { public static void main(String agmts[]) { int i=1; while(i<=5) { System.out.println(i); i++; } // Other Statements } } /* Output 1 2 3 4 5 */
Working of a While Loop using the above example
- As there is no initialization part in a While Loop, we will have to declare any variables before the While Loop (if needed).
- The Condition is the first thing that is evaluated in a While Loop. In the above case, the condition (i<=5) evaluates to true.
- After the condition is true, the body of the While Loop gets executed.
- This process keeps on repeating and stops only when the condition of the While Loop is false.
- There is a statement in the body of the While Loop that increments the value of ‘i’ by 1. This is being used so that at some point the value of the variable ‘i’ will be equal to 6 and then the loop will terminate.
Do-while Loop in Java
In a Do-while Loop, the body of the Do-while Loop is executed first and then the condition of the Do-while Loop is evaluated. This guarantees that the body of the Do-while Loop will get executed at least once during its execution.
Do-while Loop can be seen as a horizontal flip side of a While Loop. In a While Loop, the Condition is evaluated first and then the body gets executed. But in the case of the Do-while Loop, the body gets executed first and then the condition is evaluated.
Note: A Do-while Loop statement ends with a semi-colon.
Syntax
do { //statements; } while(Condition);
Example
class prog { public static void main(String agmts[]) { int i=1; do { System.out.println(i); i++; } while (i<=5); // Other Statements } } /* Output 1 2 3 4 5 */
Working of a Do-while Loop using the above example
- As there is no initialization part in a While Loop, we will have to declare any variables before the While Loop (if needed).
- The first thing that happens in a Do-while Loop is the execution of the statements in its body.
- After the execution of statements in the body, the condition of the Do-while Loop is evaluated.
- If the condition is evaluated to be true, then the loop continues and the body of the Do-while Loop gets executed again. This process keeps on repeating and stops when the condition of the Do-while Loop is false.