Loops In Java



What is loop in programming language?
Loop in programming languages is a control flow statement, which we use to execute a set of instructions repeatedly while some condition evaluate to true.

There are three steps on which every loop works :
1. Initialization Condition : In this step we initialize a variable with a value from which we want to start our loop, we will continuously change the value of this variable in Step 2 till our terminating condition is reached.
2. Increment/Decrement of initialized variable to move the loop one step further.
3. Termination Condition : In this step we define condition, which when reached our loop will end. 
4. Body of Loop : This defines set of instructions that needs to be executed repeatedly.

There are 3 types of loop that Java provide us, they all provide similar basic functionality but they differ in syntax and the time at which terminating condition is evaluated. Let us discuss them in detail :

1. for Loop
Below is the syntax for For loop in Java :

    for(Initialization Condition;Terminating Condition;Increment/Decrement) {
     
/**
       * Body of loop
       */
   
}


Below is the sequence of execution in for loop :
1. First a variable is declared with its initial value which act as the starting condition of our loop.
2. In next step we define our terminating condition, if it is true then 3rd step is executed otherwise loop will terminate.
3. In this step body of loop will get executed which means, the statements that you want to execute repeatedly will be executed.
4. In this step value of the variable declared in first step will either increases or decreases as per the code we have written, then control will pass to 2nd step.

Let's take an example of printing 2's table  :

public TestLoop{
  public static void main(String[] args){
    for(int i=1;i<=10;i++){
      System.out.println("2 x "+i+" = "+(2*i));
    } 
  }
}

When we run above program, it will print the following output on console :


2. while Loop
Below is the general syntax for while loop in Java :

    Initialization Condition;
    while(Terminating Condition) {
      
/**
       * Body of loop
       */

       Increment/Decrement
    
}


Below is the sequence of execution in while loop :
1. First a variable is declared with its initial value which act as the starting condition for our loop.
2. In next step we define our terminating condition, if it is true then 3rd step is executed otherwise loop will terminate.
3. In this step body of loop will get executed which means, the statements that you want to execute repeatedly will be executed.
4. In this step value of the variable declared in first step will either increases or decreases as per the code we have written, then control will pass to 2nd step.

Let's take same example of printing 2's table  :

public TestLoop{
  public static void main(String[] args){
    int i=1;
    while(i<=10){
      System.out.println("2 x "+i+" = "+(2*i));
      i++;
    } 
  }
}

Output of the above program will be same as for loop.

3. do-while Loop
Below is the general syntax for while loop in Java :

    Initialization Condition;
    do {
      
/**
       * Body of loop
       */

       Increment/Decrement
    
}
while(Terminating Condition);

Below is the sequence of execution in while loop :
1. First a variable is declared with its initial value which act as the starting condition for our loop.
2. In this step body of loop will get executed which means, the statements that you want to execute repeatedly will be executed.
3. In this step value of the variable declared in first step will either increases or decreases as per the code we have written.
4. In next step we define our terminating condition, if it is true then 2nd step is executed otherwise loop will terminate.

Let's take same example of printing 2's table  :

public TestLoop{
  public static void main(String[] args){
    int i=1;
    do{
      System.out.println("2 x "+i+" = "+(2*i));
      i++;
    }while(i<=10); 
  }
}

Output of the above program will be same as for loop.

So the big question, when to use which loop? There is a principle which I personally follows while making a decision on loops :
1. If you know how many times a loop will execute then go for "for loop" otherwise for "while loop" and "do..while loop".
2. If you have to execute body of loop at least once then go for "do..while loop" otherwise go for "while loop".

There are so many questions that interviewers may ask on this topic so be well prepared for this. Also if you have any doubts you can ask in comments section.







Share:

Related Posts:

No comments:

Post a Comment

Popular Posts

Recent Posts

Followers

Total Pageviews

5,030