Page 84 - Trackpad_ipro 4.1_Class8
P. 84

The do-while Loop

                  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.
                  The syntax of the 'do-while' loop is as follows:
                                                                                                      Start
                  do

                  {
                                                                                             Body of do...while loop
                  [statements]

                  increment/decrement expression
                  }                                                                      True      conditional

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

                      public static void main(String args[])

                      {
                          int i = 1;

                            do

                          {
                               System.out.println("The  value
                                   of i is: " + i);

                              i++;

                          }

                          while (i <= 10);
                      }

                  }
                  If we change the condition used in the preceding
                  program from (i <= 10) to (i >= 10), the 'do-while'
                  loop executes the statements only once and stops
                  executing. However, the condition is false. The output
                  will be displayed as shown.








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