The general form is
do { Body of the loop; } while(test condition); next statement; |
When this statement is executed the body of the loop is executed first. Then the test condition is evaluated. If the value is false, the control is transferred to the next statement. If the value is true the body of the loop is executed repeated ly until the test condition becomes false. When the test condition becomes false the control is transferred to the next statement.
Rules
1. The test condition should be any relational or logical expression enclosed within parentheses.
2. Brackets are must if the body of the loop contains more than one statement.
3. The while statement should be immediately after the body of the loop.
Flow Diagram
Example
Class Test
{
public static void main(String args[])
{
int I;
i=1;
do
{
System.out.println(“JAVA PROGRAM”);
}
while(i<5);
}
}
This program statements are used to display the message JAVA PROGRAM, 4 times.
Initially the body of the loop is executed once and the condition i<5 is tested. If it is false the program ends. If it is true the body of the loop is executed repeatedly (3 times).
No comments:
Post a Comment