Loop Statements
Java provides a set of looping statements that executes a block of code repeatedly while some condition evaluates to true. Looping control statements in Java are used to traverse a collection of elements, like arrays.
1.While Loop-
It is used to iterate over a single statement or a block of statements until the specified boolean condition is false.
The while loop statement is also called the entry-control looping statement because the condition is checked prior to the execution of the statement and as soon as the boolean condition becomes false, the loop automatically stops.
You can use a while loop statement if the number of iterations is not fixed. Normally the while loop statement contains an update section where the variables, which are involved in while loop condition, are updated.
Syntax:
while (condition)
{
// code block to be executed
}
Example-
public class WhileLoopConcept {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}
Output:
1
2
3
4
5
2. Do-while Loop
The Java do-while loop statement works the same as the while loop statement with the only difference being that its boolean condition is evaluated post first execution of the body of the loop. Thus it is also called exit controlled looping statement. You can use a do-while loop if the number of iterations is not fixed and the body of the loop has to be executed at least once.
Syntax:
do
{
// code block to be executed
} while (condition);
Example-
public class WhileLoopConcept {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}
Output:
1
2
3
4
5
3.For Loop-
for loop statement consists of the initialization of a variable, a condition, and an increment/decrement value, all in one line. It executes the body of the loop until the condition is false.
The for loop statement is shorter and provides an easy way to debug structure in Java. You can use the for loop statement if the number of iterations is known.
In a for loop statement, execution begins with the initialization of the looping variable, then it executes the condition, and then it increments or decrements the looping variable. If the condition results in true then the loop body is executed otherwise the for loop statement is terminated.
Syntax:
for (initialization; condition; increment/decrement)
{
// code block to be executed if condition is true
}
Example-
public class ForLoopConcept {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
4.For-each Loop
The for-each loop statement provides an approach to traverse through elements of an array or a collection in Java. It executes the body of the loop for each element of the given array or collection. It is also known as the Enhanced for loop statement because it is easier to use than the for loop statement as you don’t have to handle the increment operation. The major difference between the for and for-each loop is that for loop is a general-purpose loop that we can use for any use case, the for-each loop can only be used with collections or arrays.
In for-each loop statement, you cannot skip any element of the given array or collection. Also, you cannot traverse the elements in reverse order using the for-each loop control statement in Java.
Example:
public class ForEachConcept {
public static void main(String[] args) {
int arr[] = { 5, 8, 1, 9};
for (int i : arr) {
System.out.println(i);
}
}
}
Output-
5
8
1
9
2 . Jump or Branch Statements
Jump/Branching control statements in Java transfer the control of the program to other blocks or parts of the program and hence are known as the jump statements.
1. Break Statement
The break statement as we can deduce from the name is used to break the current flow of the program.
The break statement is commonly used in the following three situations:
Terminate a case block in a switch statement as we saw in the example of the switch statement in the above section.
To exit the loop explicitly, even if the loop condition is true.
Use as the alternative for the goto statement along with java labels, since java doesn’t have goto statements.
The break statement cannot be used as a standalone statement in Java. It must be either inside a switch or a loop.
If we try to use it outside a loop or a switch, JVM will give an error.
Example:
public class BreakStatementConcept {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
}
Output-
1
2
3
4
2. Continue Statement
Sometimes there are situations where we just want to ignore the rest of the code in the loop body and continue from the next iteration. The continue statement in Java allows us to do just that. This is similar to the break statement in the sense that it bypasses every line in the loop body after itself, but instead of exiting the loop, it goes to the next iteration.
Example:
public class ContinueStatementConcept {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
}
}
Output-
1
2
3
4
6
7
8
9
10