<<Previous
Next >>
do while statement
do while is a conditional loop statement. do while loop is similar to while loop except that do while executes the block and then only checks the condition specified. Thus, do while will execute the block atleast once, even if the condition evaluates to false the first time itself.do while - Syntax
do
{
statement;
}while(condition)
do while - Example
public class DoWhile {
public static void main(String a[]){
int i=0;
System.out.println(
"\nbefore first while loop");
do{
System.out.print("" + i + ",");
i=i+1;
}while(i<10);
i=20;
System.out.println(
"\n\nbefore second while loop");
do{
System.out.print("" + i + ",");
i=i+1;
}while(i<10);
System.out.println(
"\nafter second while loop");
}
}
The above do while loop prints:
before first while loop
0,1,2,3,4,5,6,7,8,9,
before second while loop
20,
after second while loop
<<Previous
Next >>