Java do-while loop

 Java do-while loop

The do-while loop checks the condition at the end of the loop after executing the loop statements. When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop.

It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop is given below.

  1. do     
  2. {    
  3. //statements    
  4. while (condition);    

The flow chart of the do-while loop is given in the following image.

Control Flow in Java

Consider the following example to understand the functioning of the do-while loop in Java.

  1. //print "I am Indian" 10 times.

  1. public class DoWhile1 {
  2. public static void main(String []args){
  3. int i=1;
  4. do{
  5. System.out.println("I am Indian");
  6. i++;
  7. }while (i<=10);
  8. }
  9. }

Output:

I am Indian
I am Indian
I am Indian
I am Indian
I am Indian
I am Indian
I am Indian



  1. //print 1 to 100.
  1. public class DoWhile2 {
    public static void main(String[] args) {
    int i=1;
    do{
    System.out.println(i);
    i++;
    }while(i<=100);
    }
    }

Output:

1
2
3
4
5
6
7
8
9
10





Comments

Popular Topics

What is JSP. How to create one JSP application in Eclipse

How to develop a RESTful webservice in java ?

What is JDBC ?

Data Types in PL/SQL

What is Hibernate in java ?

If you have any query or suggestions, Please feel free to write us

Name

Email *

Message *