DEV Community

Cover image for The 3 Types of Loops in Java
Waithaka
Waithaka

Posted on • Updated on

The 3 Types of Loops in Java

Meaning of Looping and Its Importance

Looping is the process of executing a block of code multiple times without frequently writing it.

Looping is important because of the following reasons:

  1. It saves a developer a lot of time.
  2. Fast execution of a Java Program.
  3. Tasks are accomplished in a more accurate manner.

Types of Loops

For Loop:
(i) Definition - This is a repetition control structure that enables developers the ability to write a loop in an efficient manner to be executed a specific number of times.

(ii) Java Syntax -
Step 1(Initialization), in this stage a variable is declared then a value is assigned to that variable.

Step 2(Condition), this part checks to see if a program is true or false.

Step 3(Increment), this is the increasing of a value in a for loop.

Step 4(Statement), its job is to perform the task we give it in the for loop.

Syntax: 
for(initialization;condition;increment){
    //statements
}

 Program Example:
 // A program to display numbers between
 public class ForLoop{
   //for Loop
    for (int i = 40; i<50; i++) {
        //statements
   System.out.println("value of i:" +i);
    }
}
Result:
value of i: 41
value of i: 42
value of i: 43
value of i: 44
value of i: 45
value of i: 46
value of i: 47
value of i: 48
value of i: 49
Enter fullscreen mode Exit fullscreen mode

While Loop
(i) Definition - This loop iterates a block of code an unknown number of times until the condition is met.

(ii) Java Syntax -
Step 1(Initialize the Expression), in this first step a variable will be declared then assigned a value.

Step 2(Testing of the Expression), this step will verify the expression. If true we will execute the body and go to the update expression, if false we will exit the loop.

Step 3(Statements) performs the task we give it in the while loop.

Step 4(Update the Expression), this step increments or decrements the loop variable by some value.

Syntax: 
// initialize expression
 while (test_expression)
{
  //statements
  update_expression;
}

Program Example:
//A program displaying numbers starting from 30 that are less than 40
 public class Test{
   public static void main(String args[]){
    //initialization of expression
    int n = 30;
    while(n < 40){
    //statement
    System.out.println("The following numbers are: " +n);
    //update expression
    n++;
    }
  }
}

Result:
The following numbers are: 30
The following numbers are: 31
The following numbers are: 32
The following numbers are: 33
The following numbers are: 33
The following numbers are: 34
The following numbers are: 35
The following numbers are: 36
The following numbers are: 37
The following numbers are: 38
The following numbers are: 39
Enter fullscreen mode Exit fullscreen mode

Do-While Loop:
(i) Definition - This type of loop executes a block of code at least once. It either does or does not repeat itself depending on a Boolean condition at the end of the block.

(ii) Java Syntax -
Step 1(Initialize the Expression), in this first step a variable will be declared then assigned a value.

Step 2(Update Expression), in this step a statement will be printed, and the expression will either increment or decrement the loop variable by some value. Both operations will be performed in the do loop.

Step 3(Test Expression), this step will verify the expression. If true, we will execute the body and go to the update expression, if false we will exit the loop. This operation will take place in the while loop.

Syntax: 
//initialize expression
 do {
    //statement
   //update expression
}
//condition check
while (test expression);

Program Example: 
//A program to display numbers from 60 to 70
 public class DoWhileExample{
 public static void main (String args[]) {
 //initialization of variable
 int n = 60;
 do {
    //statements
    System.out.println("value of n" + n);
    n++;
    } while (n <= 70);
    }
    }

Result:
 value of n: 60
 value of n: 61
 value of n: 62
 value of n: 63
 value of n: 64
 value of n: 65
 value of n: 66
 value of n: 67
 value of n: 68
 value of n: 69
 value of n: 70
Enter fullscreen mode Exit fullscreen mode

Closing Remarks

If you have any questions or comments regarding Loops in Java feel free to reach out to me on LinkedIn at John Ndungu Waithaka , @ThakkaJay on Twitter or in the comments section below. Thank you for taking the time to read this article, see you in the next one.

Top comments (0)