Loop Control Statements
Java Loops are used to execute the same block of code a specified number of times or while a specified condition is true.
Very often when we write code, we want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a code we can use loops to perform a repetitive task like this.
In Java there are mainly three different kinds of loops:
- while loop – loops through a block of code while a specified condition is true.
- do-while loop – a variant of the while loop which will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested.
- for loop – loops through a block of code a specified number of times
Besides this, there are two other variations of for loop — labeled for loop and enhanced for loop.
1) The while loop
The while loop is used when we want the loop to execute and continue executing while the specified condition is true. As the condition is checked at the beginning of the loop, it is also called entry controlled loop.
Syntax
while (condition) {
//code to be executed
}
Example 1.
See the program below.
LoopTest01.java
public class LoopTest01 {
public static void main(String[] args) {
int i = 1;
while (i <= 5) { //while loop
System.out.println("The number is " + i);
i = i + 1;
}
}
}
Output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
2) The do..while Loop
The do…while loop is a variant of the while loop. This loop will always execute a block of code at least once, and then it will repeat the loop as long as the specified condition is true. This loop will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested. As the condition is checked at the end of the loop, it is also called exit controlled loop.
Syntax
do {
//code to be executed
} while (condition);
Example 2.
See the program below.
LoopTest02.java
public class LoopTest02 {
public static void main(String[] args) {
int i = 1;
do { //do-while loop
System.out.println("The number is " + i);
i = i + 1;
} while (i <= 5);
}
}
Output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
3) The for Loop
The for loop is used when we know in advance how many times the code should run.
Syntax
for (initialization part; condition part; re-initialization part)
{
//code to be executed
}
Example 3.
The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. The value of i will increase by 1 each time the loop runs.
LoopTest03.java
public class LoopTest03 {
public static void main(String[] args) {
//for loop
for (int i = 1; i <= 5; i++) {
System.out.println("The number is " + i);
}
}
}
Output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
♦ Java break and continue statements
There are two special statements that can be used inside loops: break and continue. See examples below.
◊ Using “break” statement:
The break statement will break the loop and continue executing the code that follows after the loop (if any).
Example 4.
See the following example.
LoopTest04.java
public class LoopTest04 {
public static void main(String[] args) {
//for loop
for (int i = 1; i <= 5; i++) {
if(i==3)
break;
System.out.println("The number is " + i);
}
}
}
Output
The number is 1
The number is 2
◊ Using “continue” statement :
The continue statement will break the current loop and continue with the next value.
Example 5.
See the example below.
LoopTest05.java
public class LoopTest05 {
public static void main(String[] args) {
//for loop
for (int i = 1; i <= 5; i++) {
if(i==3)
continue;
System.out.println("The number is " + i);
}
}
}
Output
The number is 1
The number is 2
The number is 4
The number is 5
♦ Labeled for loop
We can use labeled loops in our Java program. In the following example, we have used nested loops. Note that each of these loops (outer or inner) is associated with a label. Using “break” statement with label, we can exit from the outer for loop. See the code below.
LabeledLoop.java
public class LabeledLoop {
public static void main(String[] args) {
//labeled loop
level1:
for (int i = 1; i <= 5; i++) { //outer loop
level2:
for (int j = 1; j <= 5; j++) { //inner loop
if (j == 4) {
break level1;
}
System.out.println("i = " + i + "; j = " + j);
}
}
}
}
Output
i = 1; j = 1
i = 1; j = 2
i = 1; j = 3
♦ Enhanced for loop
The following example demonstrates the enhanced for loop. This type of for loop does not include “initialization” or “re-initialization” parts like the normal for loop. It can be used to traverse the elements of an array or a collection from the beginning to the end.
EnhancedForLoop.java
public class EnhancedForLoop {
public static void main(String[] args) {
int array[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int sum = 0;
//Enhanced for loop
for (int arr : array) {
sum += arr; //computing sum
}
System.out.println("SUM = " + sum);
int len = array.length; //array length
int avg = sum / len; //computing average
System.out.println("AVG = " + avg);
}
}
Output
SUM = 450
AVG = 50