Loops in Java

In this article, you will be learning about loops in Java with its use case and examples.

Loops in Java

Loops are used when a set or a block of code needs to be executed several number of times. In any programming language, in a set of codes, the code is executed in a sequence i.e. first line of statement is executed first and followed by each line of statement. But, when we need to execute a line or a block of code for several times until a certain condition is satisfied, that is where looping takes its role.

When and where is a loop used?

An operation is done, such as getting an item of data and changing it, and then some condition is checked such as whether the condition has reached the prescribed number. Condition not reached: If the counter has not reached the desired number, the next instruction in the sequence returns to the first instruction in the sequence and repeat it. Condition reached: If the condition has been reached, the next instruction “falls through” to the next sequential instruction or branches outside the loop. Depending upon the requirement Java programming supports the following loops:

  • for loop
  • while loop
  • do…while loop

For Loop

The Java for loop is a control flow statement that iterates a part of the programs or a block of code for the required number of times.

Syntax
for(declaration : expression) {
   // Statements
}
Sample program using for loop in Java
Input
public class Main {
  public static void main(String[] args) {
String[] Trucks = {"Scania", "Volvo", "Mazda", "Eicher"};
for (String i : cars) {
  System.out.println(i);
        }   
  }
}

Output

Scania
Volvo
Mazda
Eicher

While loop

Repeats a statement or group of statements until a given condition is met/true. It tests the condition before executing the loop body

Syntax
while (test Expression) {
    // body of loop
}
Sample problem using while loop in Java
Input
public class Main {
  public static void main(String[] args) {
    int i = 111, n = 115;
    while(i <= n) {
      System.out.println(i);
      i++;
    }
  }
}

Output

111
112
113
114
115

Do while loops

The do…while of Java loop is similar to the while loop. But with a condition that the body of do…while loop is executed atleast once before the test expression is checked.

Syntax
do {
    // body of loop
} while(textExpression) 
Sample program using do..while loops in Java
Input
class Main {
  public static void main(String[] args) {
    int i = 111, n = 115;
    do {
      System.out.println(i);
      i++;
    } while(i <= n);
  }
}

Output

111
112
113
114
115

Loop control statements

While working with loops, it is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. That’s where loop control statements finds its place. Generally, a loop control statement transfer the control of the loop to the remaining program statements

There are two types of loop control statements. They are

  • Break statement
  • Continue statement

break statement

Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

Syntax
Loop or switch statements 
break;
Sample program using break in Java
Input
class Test {
    public static void main(String[] args) {
      
        for (int i = 111; i <= 115; ++i) {
            if (i == 115) {
                break;
            }      
            System.out.println(i);
        }   
    }
}

Output

111
112
113
114

Continue statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Syntax

Loop or switch statements 
continue;
Sample program for continue statement in Java
Input
class Main {
  public static void main(String[] args) {
    for (int i = 111; i <= 120; ++i) {
           if (i > 114 && i < 119) {
        continue;
      }
      System.out.println(i);
    }
  }
}

Output

111
112
113
114
119
120

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024