0% found this document useful (0 votes)
41 views2 pages

Java Infinitive Do-While Loop

The Java do-while loop iterates a part of the program multiple times if the number of iterations is unknown or must occur at least once. It checks the loop condition at the end of each iteration, so the body is executed at least once. An infinite do-while loop can be created by passing true as the conditional.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Java Infinitive Do-While Loop

The Java do-while loop iterates a part of the program multiple times if the number of iterations is unknown or must occur at least once. It checks the loop condition at the end of each iteration, so the body is executed at least once. An infinite do-while loop can be created by passing true as the conditional.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java do-while Loop

The Java do-while loop is used to iterate a part of the program several times. If the number
of iteration is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is checked after loop
body.

Syntax:

1. do{  
2. //code to be executed  
3. }while(condition);  

Example:

1. public class DoWhileExample {  
2. public static void main(String[] args) {  
3.     int i=1;  
4.     do{  
5.         System.out.println(i);  
6.     i++;  
7.     }while(i<=10);  
8. }  
9. }  
Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java Infinitive do-while Loop


If you pass true in the do-while loop, it will be infinitive do-while loop.

Syntax:

1. do{  
2. //code to be executed  
3. }while(true);  

Example:

1. public class DoWhileExample2 {  
2. public static void main(String[] args) {  
3.     do{  
4.         System.out.println("infinitive do while loop");  
5.     }while(true);  
6. }  
7. }  

Output:

infinitive do while loop


infinitive do while loop
infinitive do while loop
ctrl+c

Now, you need to press ctrl+c to exit from the program.

You might also like