Page 82 - iPlus_Ver_2.0_class_8
P. 82

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:

                  The for Loop                                                     Start

                  The for loop in Java helps to repeat a set
                                                                               Initialisation
                  of statements a definite number of times.
                  It is designed to process the items of any
                  sequence one by one.
                                                                       False
                  The syntax of the for loop is as follows:                    Conditional
                                                                                expression
                  for                 (initialisation;
                  conditional                expression;                                            Update Counter
                                                                                      True
                  increment/decrement)
                                                                             Body of for loop
                  {

                  [statements]
                                                                   Stop
                  }
                                                                                Flowchart of the for loop
                  Where, the initialisation section initialises a variable when the loop
                  starts its execution. This section executes only once and controls
                  the execution of the for loop. The semicolon symbol will terminate
                  the initialisation section. Next, the conditional expression section
                  will be evaluated. This section contains a test condition. The body
                  of the loop will be executed until the test condition becomes false.
                  At the end, the increment/decrement section will be executed. It
                  controls the iterations of the for loop.

                  For example,
                  public class ForLoop
                  {
                      public static void main(String args[])
                      {



                    80
                         iPlus (Ver. 2.0)-VIII
   77   78   79   80   81   82   83   84   85   86   87