Loops in Java
Loops in Java
1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the
variable, or we can use an already initialized variable. It is an optional condition.
2. Condition: It is the second condition which is executed each time to test the condition of the loop. It continues
execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
3. Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
4. Statement: The statement of the loop is executed each time until the second condition is false.
Syntax:
Syntax:
ForEachExample.java
Note: The break and continue keywords breaks or continues the innermost for loop respectively.
Syntax:
labelname:
for(initialization; condition; increment/decrement){
//code to be executed
}
LabeledForExample.java
LabeledForExample2.java
//A Java program to demonstrate the use of labeled for loop
1. public class LabeledForExample2 {
1. public class LabeledForExample {
2. public static void main(String[] args) {
2. public static void main(String[] args) {
3. aa:
3. //Using Label for outer and for loop
4. for(int i=1;i<=3;i++){
4. aa:
5. bb:
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
6. bb:
7. if(i==2&&j==2){
7. for(int j=1;j<=3;j++){
8. break bb;
8. if(i==2&&j==2){
9. }
9. break aa;
10. System.out.println(i+" "+j);
10. }
11. }
11. System.out.println(i+" "+j);
12. }
12. }
13. }
13. }
14. }
14. }
15. }
Output:-
1 1
1 2
Output: 1 3
11 2 1
12 3 1
13 3 2
21 3 3
If you use break bb;, it will break inner loop only which is the default behaviour of any loop.
Java While Loop
The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As
soon as the Boolean condition becomes false, the loop automatically stops.
The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to
use the while loop.
Syntax:
while (condition){
//code to be executed
I ncrement / decrement statement
}
1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to
update expression. When the condition becomes false, we exit the while loop.
Example:
i <=100
2. Update expression: Every time the loop body is executed, this expression increments or decrements loop variable.
Example:
i++;
In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and increment
the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.
WhileExample.java
Output:
1
2
3
4
5
6
7
8
9
10
Java do-while Loop
The Java do-while loop is used to iterate a part of the program repeatedly, until the specified condition is true. If the number of
iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop.
Java do-while loop is called an exit control loop. Therefore, unlike while loop and for loop, the do-while check the condition at
the end of loop body. The Java do-while loop is executed at least once because condition is checked after loop body.
Syntax:
do{
//code to be executed / loop body
//update statement
}while (condition);
1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to update
expression. As soon as the condition becomes false, loop breaks automatically.
Example:
i <=100
2. Update expression: Every time the loop body is executed, the this expression increments or decrements loop variable.
Example:
i++;
Note: The do block is executed at least once, even if the condition is false.
Example:
In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and
increment the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.
DoWhileExample.java
Output:
1
2
3
4
5
6
7
8
9
10