Loops in Java3 min read

What are loops in Java?

Loops in Java are a feature that allows a collection of instructions/functions to be executed again until some condition evaluates to true. With the help of different examples, we will learn how to use the loops in Java in this tutorial.

The loops can be executed in three different ways in Java

while loop

Syntax:

while (boolean condition){
  loop statements...
}
  • The condition is checked first in the while loop. The loop body statements are executed if it evaluates to true; otherwise, the first statement following the loop is executed. As a result, it’s also known as the Entry Control Loop.
  • The statements in the loop body are performed after the condition is evaluated to be true. The update value for the variable being processed for the following iteration is usually included in the statements.
  • The loop ends when the condition turns false, marking the end of its life cycle.

Example:

// Java program to illustrate while loop
class WhileLoopDemo{
  public static void main(String[] args){
    int x = 1;
    // Exit when x becomes greater than 4
    while (x <= 4){
      System.out.println("Value of x:" + x);
      
      // Increment the value of x for next iteration
      x++;
    }
  }
}

Output:

Value of x:1
Value of x:2
Value of x:3
Value of x:4

for loop

The for loop structure is written in a concise manner. Unlike a while loop, a for statement consumes the setup, condition, and increment/decrement on a single line, resulting in a looping structure that is shorter and easier to debug.

Syntax:

for (initialization; testing condition; increment/decrement){
  statement(s);
}
  • Initialization: This is where we set the value of the variable we’re working with. It is the beginning of for loop. A variable that has already been declared or a variable that is exclusively local to the loop can be utilized.
  • Testing condition: The statements in the loop body are performed after the condition is evaluated to be true.
  • Increment/Decrement: It is used to update the variable for the next iteration.
  • When the condition becomes false, the loop is terminated.

Example:

// Java program to illustrate for loop.
class ForLoopDemo{
  public static void main(String args[]){
    // for loop begins when x=2
    // and runs till x <=4
    for (int x = 2; x <= 4; x++)
      System.out.println("Value of x:" + x);
  }
}

Output:

Value of x:2
Value of x:3
Value of x:4

do…while loop

Do While loops are identical to while loops, except that after executing the statements, it checks for conditions, making it an Exit Control Loop.

Syntax:

do{
  statements..
  }while (condition);
  • The execution of the statement initiates the do-while loop. There is no first-time inspection of any condition.
  • The condition is verified for true or false value after the statements are executed and the variable value is updated.
  • The next iteration of the loop begins if it is evaluated to be true.
  • The loop ends when the condition turns false, marking the end of its life cycle.
  • It’s worth noting that the do-while loop will execute all of its statements at least once before any condition is verified, making it an example of an exit control loop.
// Java program to illustrate do-while loop
class DoWhileloopDemo{
  public static void main(String args[]){
    int x = 21;
    do{
      // The line will be printed even
      // if the condition is false
      System.out.println("Value of x:" + x);
      x++;
    }while (x < 20);
  }
}

Output:

Value of x: 21

Infinite loop

One of the most typical blunders when developing any type of looping is that it may never end, i.e. the loop will run indefinitely. When the condition fails for some reason, this occurs.

Example:

// Infinite for Loop
class Infinite {
  public static void main(String[] args) {
    int sum = 0;
    for (int i = 1; i <= 10; --i) {
      System.out.println("Hello");
    }
  }
}

Here, the test expression, i <= 10, is never false and Hello is printed repeatedly until the memory runs out.

Reading recommendation

Branching statements in Java


Share:

Leave a Reply