Do-While Loop

In java do-while loop is used to repeat the part of the program many times. If the number of repetition is not fixed and need to execute loop at least once.

In while loop, condition is evaluated before execution of loop’s body but in do-while loop condition evaluated after the execution of loop’s body.

Syntax

do{

statements(s);

} while(condition);

Flowchart
Do-While Loop
Example

public class DoWhileExample {

public static void main(String[] args) {

int i=1;

do{

System.out.println(i);

i++;

}while(i<=15);

}

}

Output

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Infinitive do-While Loop

In do-while loop if we pass true it will be infinitive do-while loop.

Syntax

do{

statement(s);

}while(true);

Example

public class DoWhileExample2 {

public static void main(String[] args) {

do{

System.out.println(“infinitive do while loop”);

}while(true);

}

}

Output

infinitive do while loop

infinitive do while loop

infinitive do while loop

ctrl+c

Ctrl+c is use to exit the program

Copyright 1999- Ducat Creative, All rights reserved.