Page 85 - Trackpad_ipro 4.1_Class8
P. 85

The for Loop

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

                 The  syntax  of  the  'for'  loop  is  as
                 follows:                                         False   Conditional

                 for (initialisation;                                      Expression
                 conditional expression;
                                                                                             Increment/Decrement
                 increment/decrement)                                            True
                 {                                                      Body of for loop

                 [statements]
                                                              Stop
                 }
                 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("Trackpad");
                               }
                     }
                 }

                 The preceding program prints the word ‘Trackpad’ ten times.


                     JUMP STATEMENTS

                 Sometimes,  it is  necessary to transfer  control out of the loop body before all iterations are
                 completed. For this purpose, jump statements are used in Java. Java offers two jump statements—
                 break and continue—which are used within the loops.





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