Page 81 - iPlus_Ver_2.0_class_8
P. 81

{
                         int i = 1;
                         while (i <= 10)

                         {
                             System.out.println("The value of i is: " + i);
                             i++;
                         }
                     }
                 }
                 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 the variable i
                 will be incremented by 1 (i++). The loop checks the condition
                 again and again until the value of the 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
                                                                                                    Start
                 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            do...while Loop Body
                 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
                                                                                       True      Conditional
                 and again until the given condition becomes false.
                                                                                                 Expression
                 The syntax of the while loop is as follows:

                 do
                                                                                                        False
                 {
                                                                                                    Stop
                        [statements]
                                                                                           Flowchart of do-while loop
                        [increment/decrement expression]
                 }

                 while(< conditional expression >);
                 For example,
                 public class DoWhileLoop

                 {
                     public static void main(String args[])
                     {
                         int i = 1;
                           do

                         {



                                                                                                                  79
                                                              Conditional, Looping and Jumping Statements in Java
   76   77   78   79   80   81   82   83   84   85   86