Java Loop Statements
Java Looping statements repeat a set of actions based on a condition. Its similar to taking steps until you reach your friends home or a shop nearby. Here the condition is to reach your desired destination, and that may not get completed in a single step. So, what you have to do is repeat taking the steps.
There are three types of Java Loop Statements in Java. They are while, do-while and for loops.
In this tutorial, we will go through all these looping statements in Java and learn their execution flow and usage with examples.
Java While Loop Statement
The following flow chart depicts the execution flow in while loop.

How does While Loop execute in Java?
- Evaluate the condition.
- If the condition evaluates to true, execute all the statements in the while loop body.
- If the condition evaluates is false, get out of the loop and proceed with statements after while loop (if any).
Example.java
public class Example { public static void main(String[] args) { int i = 0; while(i < 3){ System.out.println("Printing in while loop"); i = i + 1; // increment i, } } }
Output
Printing in while loop Printing in while loop Printing in while loop
Java do-while Loop Statement
The following flow chart depicts the execution flow in do-while loop.

There is only a single distinction between while and do-while loop. Which is, do-while loop executes the statements in the loop for the first time, without checking the condition. From the second time, before executing the statements in the loop, the condition is checked. Its like do execute the statements in the loop, and keep repeating them until the condition becomes false.
Example.java
public class Example { public static void main(String[] args) { int i = 0; do{ System.out.println("Printing in do-while loop"); i++; }while(i < 3); } }
Output
Printing in do-while loop Printing in do-while loop Printing in do-while loop
Java For Loop Statement
The following flow chart depicts the execution flow in for loop.

How does For Loop execute in Java?
- Initialize variables.
- Evaluate the condition.
- If the condition evaluates to true, execute all the statements in the while loop body. Execute “update variables” section.
- If the condition evaluates is false, get out of the loop and proceed with statements after while loop (if any).
In the following program, we will use for loop and print a string three times.
Example.java
public class Example { public static void main(String[] args) { for(int i = 0; (i < 3); i++){ System.out.println("Printing in for loop"); } } }
Output
Printing in for loop Printing in for loop Printing in for loop
Conclusion
In this Java Tutorial, we have learnt Java While, Java Do-while and Java For loop statements. In our next tutorial, we shall learn Arrays in Java.