Sometimes it is necessary in the program to execute the statement several times, and Java loops execute a block of commands a specified number of times, until a condition is met. In this chapter you will learn about all the looping statements of Java along with their use.

What is Loop?

A computer is the most suitable machine to perform repetitive tasks and can tirelessly do a task tens of thousands of times. Every programming languagehas the feature to instruct to do such repetitive tasks with the help of certain form of statements. The process of repeatedly executing a collection of statement is called looping. The statements gets executed many number of times based on the condition. But if the condition is given in such a logic that the repetition continues any number of times with no fixed condition to stop looping those statements, then this type of looping is called infinite looping.
Java supports many looping features which enable programmers to develop concise Java programs with repetitive processes.
  • for loops
  • do while loops
  • while loops
  • for loops in java

    The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
    There are three types of for loop in java.
  • Simple For Loop
  • For-each or Enhanced For Loop
  • Labeled For Loop
  • Simple For Loop

    The simple for loop is same as C/C++. We can initialize variable, check condition and increment/decrement value.

    syntax:

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

    Example

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

    Output

    2 3 4 5 6 7 8 9 10 11 12

    Java For-each Loop

    The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
    It works on elements basis not index. It returns element one by one in the defined variable.

    syntax:

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

    example

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

    Output

    10 15 20 30 50

    Java Labeled For Loop

    We can have name of each for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.
    Normally, break and continue keywords breaks/continues the inner most for loop only.

    syntax:

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

    example

    public class LabeledForExample {  
    public static void main(String[] args) {  
    bb:  
    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

    11 12 13 21