Do-While Loop example in Java

The do-while loop in java is similar to the while loop except that the do-while loop is guaranteed to execute at least once . The do while loop evaluates its condition at the bottom of the loop.

The syntax of the do-while loop in java can be written as

do
{
statement(s)
}while(expression)

Do-While Loop example in Java

Below is a sample code snippet demonstrating the usage of the do-while loop in java.

package com.abundantcode;
import java.util.Arrays;
import java.util.Comparator;

public class Main {

    public static void main(String[] args)
    {
        int input=0;
        do
        {
            System.out.println(input);
            input=input+1;
        }  while(input < 5);
    }
}
image
%d bloggers like this: