Java do while Loop

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

The do block is executed at least once, even if the condition is false.

Syntax:

do{    
//code to be executed   

//Iteration 
}while (condition);  

 

Example :

import java.util.*;

public class Main {
public static void main(String[] args) {
int p=0;
do{
System.out.println("The number is : "+p);
p++;
}while(p<7);
}
}

Output :

The number is : 0
The number is : 1
The number is : 2
The number is : 3
The number is : 4
The number is : 5
The number is : 6

Comments

Leave a Reply

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

52574