Do while loop in Java

Do while loop in Java

Loops are used to repeat some steps until the desired condition is satisfied. Loops basically execute certain piece of code which programmer does not want to write again and again. For e.g. if I want print numbers from 1 to 100, I have to declare 100 variables assign values to them and print them. It will definitely make my program large and will consume a lot of memory. Thus to avoid this problems looping structure provides easiest way by re-executing everything written inside it. do – while loop in Java is a post-tested loop. Which means irrespective of the condition the loop will execute at least once. Whereas while loop is pre-tested loop. Syntax-


Q. Write a program to print 0-9 numbers using do while loop in java.


class numbers
  public static void main(String[] args) {
    int i=0;


do while java output


First we declare the variable ‘i’ with value 0. Then the code inside is executed as it is a do while loop. Inside while loop ‘i’ is printed and then incremented. Then the while condition is checked. If value of ‘i’ is less than 10 then the code inside the loop is executed. This continues till the value of ‘i’ equals 10. When it become 10 the condition becomes false and the control of program gets outside the do while loop.

Program parts-

  1. class– Used to declare a class in java.
  2. public– Access modifier which represents it is visible to all.
  3. static– The main method is declared as static, so it doesn’t require to create an object to invoke the main method.
  4. void– Return type of the method.
  5. main– Represents the starting point of the program.
  6. String[] args– Used for command line argument.
  7. System.out.println()– System is a class, out is the object of PrintStream class, println() is the method of PrintStream class. System.out.println is used to print a statement.


So this is the concept of do while loop in Java. We hope it was clear to you. If you have any doubts tell us in the comment section. For daily updates and more such content do follow us on Instagram.

Keep programming!


Leave a Reply

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