For loop in Java

For loop in Java

for loop is used to iterate the block of code several times in java. For loop is recommended if the number of iterations is fixed.

for (initialization, condition, increment/decrement) {
      // code block to be executed inside for loop
}

There are Four types of for loop
1). Simple for Loop
2). Nested For Loop
3). For each or Enhanced for Loop
4). Labeled for Loop

1). Simple for Loop

Simple for loop is a basic loop structure where we initialize the variable, then
perform the check condition and then perform increment/decrement value

Syntax of simple for loop

for (initialization, condition, increment/decrement) {
      // code block to be executed inside for loop
}

Initialization is the first condition that is executed once the beginning of the loop
starts. Here variable initialization did or we use an already initialized variable.

Condition or we can say condition check is the second statement of the for a loop. This
condition check is done every time before executing the code block inside the for loop.

When the condition failed then it will return false and the code block inside the for loop will not execute. Every time it will return either a boolean true or false.

Increment/Decrement will call every time to increase and decrease the variable value.

Code to demonstrate for loop in java

public class Main {
  public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
      System.out.println(i);
    }
  }
}

Output

1
2
3
4
5

2). Nested For Loop

Nested for loop means for loop inside the for loop. In the nested for loop, for each condition of the outer for loop, the inner loop will iterate itself till the condition is satisfied.

Syntax of nested for loop

for (initialization, condition, increment/decrement) {
      // code block to be executed inside for loop
     for (initialization, condition, increment/decrement) {
          // code block to be executed inside for loop
     }
}

Code to demonstrate for loop in java

public class Main {
  public static void main(String[] args) {
    for (int i = 1; i <= 2; i++) {
      for (int j = 1; j <= 5; j++) {
        System.out.println(i + " and " + j);
      } 
    } 
  }
}

Output

1 and 1
1 and 2
1 and 3
1 and 4
1 and 5
2 and 1
2 and 2
2 and 3
2 and 4
2 and 5

3). Java for-each Loop

for-each loop is very similar to simple for-loop operation-wise. But syntax and its implementation are different. for-each loop is used to traverse an array or collection in Java. Here we don’t need to perform any condition check and increment value etc.

It does not work on an index, it works on the basis of elements and It returns it one by one.

Syntax of for-each loop

for(datatype variable : arrayname){    
      // code block to be executed inside for loop   
}  

Java for-each loop example

public class Main {
  public static void main(String[] args) {
    int arr[] = {10,20,30,40,50};
    for (int element : arr) {
      System.out.println(element);
    }
  }
}

Output

10
20
30
40
50

4). Labeled for Loop

In Labeled for Loop in Java, each for loop has a label. It is used in a nested loop to break or continue the specific for loop.

Syntax of Labeled for loop

label:    
for(initialization; condition; increment/decrement){    
      // code block to be executed inside for loop   
}    

Java Program to demonstrate Labeled for loop

public class Main {
  public static void main(String[] args) {
    label1: for (int i = 1; i <= 3; i++) {
      label2: for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
          break label2;
        }
        System.out.println(i + " " + j);
      }
    }
  }
}

Output

1 1
1 2
1 3
2 1
3 1
3 2
3 3