X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

do...while loop :

The do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute atleast one time.

Syntax: do { //Statements } while(boolean_expression);

Notice that the boolean expression appears at the end of the loop, so the statements in the loop execute once before the boolean expression is tested. If the boolean expression is true, the flow of control jumps back up and the statements in the loop executes again. This process repeats until the boolean expression is false.

Curly braces are optional and without curly braces we can take only one statement which should not be declarative statement.

do
System.out.println("Algo");
while(true); // valid
do; 
while(true); // valid
do
int a = 10;
while(true); // invalid
do
{
 int a = 10;
}
while(true); // valid
do 
while(true); // invalid
do 
while(true)
System.out.println("Algo");
while(false); // valid
do
{
 System.out.println("Algo");
}
while(true);
System.out.println("Valley"); // Compile time error : unreachable statement
do
{
 System.out.println("Algo");
}
while(false);
System.out.println("Valley"); // valid
int a = 10 , b = 20;
do
{
 System.out.println("Algo");
}
while(a < b)
System.out.println("Valley"); // valid
int a = 10 , b = 20;
do
{
 System.out.println("Algo");
}
while(a > b)
System.out.println("Valley"); // valid
final int a = 10 , b = 20;
do
{
 System.out.println("Algo");
}
while(a < b)
System.out.println("Valley"); // Compile time error : unreachable statement
final int a = 10 , b = 20;
do
{
 System.out.println("Algo");
}
while(a > b)
System.out.println("Valley"); // valid

Example:

class Example { public static void main(String [ ] args) { int a = 1; do { System.out.println("USA"); a++; } while(a <= 5); } }

Output:

Here, the loop will be carried on until the condition becomes false. Therefore, output will be - USA USA USA USA USA


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.