Page 85 - iprime_V2.2_class8
P. 85

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

                 for (initialisation;                                         Start
                 conditional expression;
                 increment/decrement)                                     Initialisation

                 {

                 [statements]
                                                                  False   Conditional
                 }
                                                                           Expression
                                                                                             Increment/Decrement
                                                                                 True
                                                                        Body of for loop



                                                              Stop

                                                                          Flowchart of the 'for' Loop

                 In a 'for' loop, 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
                 terminates the initialisation section. Next, the conditional expression section is 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 is  executed.  This controls the
                 iterations of the 'for' loop.
                 For example:
                 public class ForLoop

                 {
                     public static void main(String args[])
                     {
                           int i;
                               for(i=1; i<=10; i++)
                               {
                                      System.out.println("Touchpad");

                               }
                     }
                 }                                                                                   Output
                 The preceding program prints the word ‘Touchpad’ ten times.







                                                                  Conditional, Loop and Jump Statements in Java    83
   80   81   82   83   84   85   86   87   88   89   90