Page 84 - TP_iPlus_V2.1_Class8
P. 84

{
                          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               Body of do...while loop
                  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.                     True      Conditional
                                                                                                    Expression
                  The syntax of the while loop is as follows:

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

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

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

                          {



                    82
                         iPlus (Ver. 2.1)-VIII
   79   80   81   82   83   84   85   86   87   88   89