Java loops are also known as control flow statement and used for iteration (repetition). Loops are used to repeat statements multiple times until the condition is satisfied. In this tutorial, we will learn about the syntax of a loop and how to use a loop in Java program with the example.
Types of Loops
In Java, there are different types of loop, here we will discuss each loop in detail with an example.
- While
- Do-While
- For
- Foreach (Enhanced For Loop)
While Loop in Java
The while loop repeats the codes based on a specific condition. It stops repeating statements until the condition becomes false.
Syntax:
while(condition){
//statement
//statement
}
Example: How to use while loop in Java?
class Example
{
public static void main(String arg[])
{
int n=1;
//begin the loop
while(n<=10){
System.out.println(n);
n++;
}
//end of the loop block
}
}
How While Loop Works
The while loop tests the condition inside the round bracket (), if the condition is true then code of the loop block will execute otherwise it will not repeat the statements. Every time once the code is executed, again it tests the condition and repeats the code until the condition becomes false.
Do-While Loops
In Java, the do-while loop executes at least once and the rest depends on the given condition that is at the end of the loop block.
Syntax:
do {
//statements
//statements
}while(condition);
Example: Do-while loop in Java.
class Example
{
public static void main(String arg[])
{
int n=1;
//loop block start here
do {
System.out.println(n);
n++;
}while(n<=10);
//condtion at the end in do-while
}
}
How Do-While Loop Works
Unlike while, do-while loop first executes the statement once then test the condition. After the first execution, if the condition is true, then repeat this cycle until the condition becomes false.
For Loops in Java
The for loops in Java also know as a counter-controlled loop. The for loop is useful when we know the number of repetition. It has three arguments initialisation, condition and increment/decrement.
Syntax:
for(initialisation; condition; increment/decrement)
{
//statements
//statements
}
Example: For loop example in Java.
class Example
{
public static void main(String arg[])
{
int n;
//for loop start here
for(n=1; n<=10; n++)
{
System.out.println(n);
}
}
}
How For Loop Works
As we know for loop has three arguments and it executes the code in four steps. The first step is initialisation then as the second step it tests the condition, rest depends on the condition. If the condition is true, as the third step it executes the statement after that as the last step increment or decrement occurs. The for loop repeat the code until the condition becomes false.
In the above examples all the loops will print 1 to 10 as a output.
Loops can be nested, it means we can use a loop as a statement inside the curly brackets { } of the another loop.
Break Keyword in Java
The break keyword in Java is used with the loop or switch case. Whenever a break keyword is found in code, it immediate exits from that block.
Example: Java program to use a break keyword.
class Example
{
public static void main(String arg[])
{
int n=0;
while(n<=10)
{
n++;
//loop will stop, when n will be 3
if(n==3)
break;
System.out.println(n);
}
}
}
//It will print 1 and 2.
Continue in Java
The continue statement is used with the loop in Java. It moves the control to the beginning of the loop and starts the next iteration.
Example: How to use continue in Java.
class Example
{
public static void main(String arg[])
{
int n=0;
while(n<=5)
{
n++;
//loop will move to beginning when n will be 3
if(n==3)
continue;
System.out.println(n);
}
}
}
//output: 1 2 4 5 6
//It will skip the print statement when n will be 3.
For-Each loop – foreach or enhanced for loop used with array and collections. We will discuss it in next chapter.