print
Advertisment
Advertisment

For Loop in java

For loop is most commonly used. for loop in Java is used when we know the exact value of how many times we have to run the loop. There are three formats in this loop: initialization, condition & increment-decrement. If we didn't give any condition in the for loop So he gets stuck in infinite conditions.

Initialization: It is the initial condition Here we declare the value of the variable. And this is where the loop starts running. It is an optional condition.

Condition: It is the second condition that is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return a boolean value either true or false. It is an optional condition.

Increment-Decrement: Increment and Decrement work to increase or decrease the value of the variable. It is an optional condition.

Syntax

for(initialization;condition;increment-decrement){
  statemetn 1
  statement n
}

Example

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

Output :


   for loop
   for loop
   for loop
   for loop
   for loop
Advertisment

Nested For loop in java

A Nested for loop is a loop in which the inner loop is defined inside the outer loop. The inner or outer loop can be of any type: while, while, or for, is called a nested loop. When a loop is nested inside another loop, the inner loop runs multiple times inside the outer loop. In each iteration of the outer loop, the inner loop will be iterated over. The inner loop must finish all its iterations before the outer loop can continue with its next iteration.

When the test expression is true, the flow of control enters the inner loop, and codes inside the body of the inner loop are executed and updating statements are updated. This process repeats until the test expression is false(inner while loop) Then when the test expression is false, the loop exit from the inner loop and the flow of control comes to the outer loop. Again, the flow of control evaluates the test condition. If the condition is true then it runs the inner loop but if it is false then it ends the inner loop and goes to the outer loop.

Main Points of for loop in java

  • The outer loop works for the row.
  • The inner loop works for the column.

Syntax

for( initialization; condition; increment ){
   for( initialization; condition; increment ){
      // statement of inside loop
   }
   // statement of outer loop
}

Example

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

Output :


    1
    22
    333
    4444
    55555
Advertisment

If we didn't give any condition in the for loop So it gets stuck in infinite conditions.

Example

class Forexample{
  public static void main(String[] args) {
    for (int i=1; ; ) {
      System.out.println("infinte loop");
    }
  }
}

Output :

 
  
    infinte loop
    infinte loop
    infinte loop
    infinte loop
    infinte loop
    infinte loop ...n
  
Advertisment
Advertisment
arrow_upward