While statement

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

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...