Java Do While Loop

Do-while loop:

  • Do- while loop is similar to a while loop except for the fact that it is guaranteed to execute at least once.
  • Use a do-while loop when the exact number of iterations is unknown, but you need to execute a code block at least once.
  • After executing a part of a program for once, the rest of the code gets executed on the basis of a given boolean condition.Syntax :/* do { //code } while (condition); //Note this semicolon */CopyExample : int i=1; do{ System.out.println(i); i++; }while(i<=10); Copy

Difference Between while loop and do-while loop :

  • while – checks the condition & executes the code.
  • do-while – executes the code at least once and then checks the condition. Because of this reason, the code in the do-while loop executes at least once, even if the condition fails.

Flow control of do-while loop : 


Exercise: Write a program to print first n natural numbers using a do-while loop.



public class cwh_22_ch4_do_while {
    public static void main(String[] args) {
//        int a = 0;
//        while(a<5){
//            System.out.println(a);
//            a++;
//        }
        int b = 10;
        do {

        int c = 1;

Was this article helpful?

Leave a Comment

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