In do while loop, at the first time body of loop
is executed before the condition test is performed.
General Form of Java do while Loop
do{
//Body of Loop
}while(condition);
On reaching do statement, body of loop executes
first and after executing body of loop, test condition will evaluate which
appears at the end of do statement. Body of loop executed
repeatedly until the test condition is true.
Test condition always appears at the bottom of loop,
therefore body of loop always executes at least once.
Flow
Diagram of Java do while Loop
Java do while Loop |
Example of Java do while Loop
class JavaDoWhileLoop
{
public static void main(String[] args) {
char c = 'A';
do
{
System.out.println(c);
c++;
}
while(c<='E');
}
}
Output:
A
B
C
D
E
0 comments:
Post a Comment