For Loop In Java

In this page we will learn about What is for loop in Java?, What are the types of for loop in java?, What is After we fell & after we collided?, Syntax of simple for loop in java, Example of simple for loop in java, What is Java Nested For Loop?, Syntax of nasted for loop, Example of nasted for loop, What is Java for-each Loop?, Syntax of for-each loop, Example of for-each loop, What is Java labeled For Loop?, Syntax of labeled For Loop, Example of labeled For Loop, What is Java Infinitive For Loop?, Syntax of Java Infinitive For Loop, Example of Java Infinitive For Loop.


What is for loop in Java?

The Java for loop is employed to repeat a region of the program many times. If the quantity of iteration is known, then it's suggested to use for loop.

For Loop In Java

What are the types of for loop in java?

There are 3 types of for loop in java.

  • Simple For Loop
  • For-each or Enhanced For Loop
  • Labeled For Loop

What is After we fell & after we collided

A simple for loop is that the same as C/C++. we will initialize the variable, check condition and increment/decrement price. It consists of 4 parts:

  • Initialization: It's the initial condition that is dead once once the loop starts. Here, we will initialize the variable, or we will use associate already initialized variable. it's associate optional condition.
  • Condition: It's the second condition that is dead every time to check the condition of the loop. It continues execution till the condition is fake. It should come back Boolean price either true or false. it's associate optional condition.
  • Statement: The statement of the loop is dead every time till the second condition is fake.
  • Increment/Decrement: It increments or decrements the variable price. it's associate optional condition.

Syntax of simple for loop in java:


  for( initialization; condition; incr/decr) {  
    //statement or code to be executed  
    }      

Example of simple for loop in java:


  public class SimpleLoop {  
    public static void main(String[] args) {  
     //Code of Java for loop 
     for(int i=1; i<=5; i++){  
        System.out.println(i);  
      }  
    }  
  }  

Output:

  1
  2
  3
  4
  5

What is Java Nested For Loop?

If we've got a for loop within the another loop, it's referred to as nested for loop. The inner loop executes whenever outer loop executes.

Syntax of nasted for loop:


  for( initialization; condition; incr/decr) {  
      //inner for loop
    for( initialization; condition; incr/decr) {  
      //statement or code to be executed  
    }     
  }      

Example of nasted for loop:

  
    public class exampleLoop {  
      public static void main(String[] args) {  
       //Code of Java for loop 
       for(int i=1; i<=2; i++){  
          for(int j=1; j<=3; j++){  
            System.out.println(i+" "+j);  
          }
        }  
      }  
    }  
  

Output:

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

What is Java for-each Loop?

The for-each loop is employed to traverse array or assortment in java. it's easier to use than easy for loop as a result of we do not got to increment worth and use subscript notation.

It works on parts basis not index. It returns part one by one within the outlined variable.

Syntax of for-each loop:


  for(Type var:array){  
    //code to be executed  
  }        

Example of for-each loop:


  public class ForEachLoopExample {  
    public static void main(String[] args) {  
        //Array declaration
        int num[]={ 10, 20, 30, 40, 50};  
        //Printing array using for-each loop  
        for(int i:num){  
            System.out.print(i+" ");  
        }  
    }  
  }       

Output:

  
    10 20 30 40 50
  

What is Java labeled For Loop?

We can have a reputation of every Java for loop. To do so, we tend to use label before the for loop. it's helpful if we've got nested for loop so we will break/continue specific for loop. Usually, break and continue keywords breaks/continues the innermost for loop solely.

Syntax of labeled For Loop:


  labelname:  
  for( initialization; condition; incr/decr){  
    //code to be executed  
  }       

Example of labeled For Loop:


  public class LabeledForLoopExample {  
    public static void main(String[] args) {  
      //Using Label for outer and for loop  
      aa:  
        for(int i = 1; i <= 3; i++){  
          bb:  
            for(int j = 1; j <= 3; j++){  
              if(i==2 && j==2){  
                break aa;  
              }  
              System.out.println(i+" "+j);  
            }  
        }  
    }  
  }            

Output:

  
    1 1
    1 2
    1 3
    2 1
  

If you use break bb;, it will break inner loop only which is the default behavior of any loop.

Second example of labeled For Loop:


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

Output:

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

What is Java Infinitive For Loop?

If you employ 2 semicolons ;; within the for loop, it'll be infinitive for loop.

Syntax of Java Infinitive For Loop:


  for( ; ;){  
    //code to be executed  
  }           

Example of Java Infinitive For Loop:


  public class InfinitiveForExample {  
    public static void main(String[] args) {   
      for( ; ; ){  
        System.out.println("infinitive loop");  
      }  
    }  
  }                  

Output:

  
    infinitive loop
    infinitive loop
    infinitive loop
    infinitive loop
    infinitive loop
    ctrl+c //to stop the loop