Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 110 - 2617_JSSPS_C-7
P. 110

    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

                  The for loop in Java helps to repeat a set of statements a definite number of times. It is designed to
                  process the items of any sequence one by one.

                                                               Start

                                                            Initialisation

                                                   False    Conditional
                                                            Expression

                                                                              Increment/Decrement
                                                                   True
                                                          Body of for loop

                                                Stop

                                                            Flowchart of the for loop
                  The syntax of the for loop is as follows:

                  for (initialisation; conditional expression; increment/decrement)
                  {

                  [statements]

                  }
                  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.

                            Premium Edition-VII
                    108

| 105 106 107 108 109 110 111 112 113 114 115 |