Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Friday, 29 July 2011

While statement


This is a simple looping statement. The general form is

while(test condition)
{
        Body of the loop;
}
next statement;


                When this statement is executed, the computer first evaluates the test condition. If the value is false, the control is transferred to next statement. if the value is true, then the body of the loop is executed repeatedly until the test condition becomes false. When the test condition becomes false the control is transferred to next statement.

Rules
1. The test condition should be any relational or logical expression enclosed within brackets.
2. If the body of the loop contains more than one statement, the {} brackets are must.

Flow Diagram

 

Example
Class Test
{
                Public static void main(String args[])
                {
                                Int i;
                                i=1;
                                while(i<5)
                                {
                                                System.out.println(“JAVA PROGRAM”);
                                                i++;
                                }
                }
}

                This program statements are used to display the message JAVA PROGRAM, 4 times.

                Initially the condition i<5 is tested. If it is false the program ends without executing the body of the loop. If it is true the body of the loop will be executed repeatedly (4 times).

No comments:

Post a Comment

Followers