Page 83 - iprime_V2.2_class8
P. 83

The while Loop

                 The while loop is a flow control statement in Java. It is used to execute a block of statements
                 repeatedly until a given condition becomes false. In a 'while' loop, the condition is evaluated first
                 and if it returns true, then the statements inside the loop are executed.
                 The syntax of 'while' loop is as follows:

                 while(<conditional expression>)
                 {                                                                               Start
                        [statements]
                        increment/decrement expression
                 }
                                                                                                 Test         False
                 For example:
                                                                                              Expression
                 public class WhileLoop
                 {
                     public static void main(String args[])                                         True
                     {
                                                                                         Body of while loop
                         int i = 1;

                         while (i <= 10)
                         {
                              System.out.println("The  value  of
                                  i is: " + i);                                                  End
                             i++;                                                        Flowchart of 'while' Loop
                         }

                     }

                 }
                 In  the  preceding  code,  the  'while'  loop  checks  the
                 condition (i <= 10) that evaluates to true. The statements
                 written inside the body of the 'while' loop get executed.
                 The value of variable 'i' will be incremented by 1 (i++).
                 The loop checks the condition again and again until
                 the  value  of  variable  'i'  becomes  more  than  10.  This
                 variable is also called a control variable. The control

                 variable defines the duration until which the loop will
                 execute.

                 The do-while Loop
                                                                                                  Output
                 The  do-while  loop  is  similar  to  the  'while'  loop.  The  only
                 difference  is  that  the  'do-while'  loop  will  execute  the  statements  written  inside  its  body  at
                 least once, whether the given condition returns true or false. The 'do-while' loop executes the
                 statements written inside its body and checks the condition. If the condition evaluates to true,
                 the loop will execute again and again until the given condition becomes false.


                                                                  Conditional, Loop and Jump Statements in Java    81
   78   79   80   81   82   83   84   85   86   87   88