Page 199 - ComputerScience_Class_11
P. 199

Each looping structure contains four parts:
                 •  Starting value/initialisation
                 •  Test condition
                 •  Increment or decrement (also known as step value)
                 •  Body of the loop
                 Let us take an example:

                    int i;
                    for(i=1; i<=10; i=i+2)

                    {
                        System.out.println(i);
                    }
                 Here,

                 •  Starting value → i=1;
                 •  Test condition → i<=10;
                 •  Increment → i=i+2
                 •  Body of the loop → System.out.println(i);
                 There are two types of loops based on the nature of repetition which are as follows:
                 1.  Fixed  Iteration  Loop:  Fixed  type  of  iterative  loop  repeats  the  process  for  a  defined  number  of  times.
                   For example, for loop
                 2.  Unfixed  Iteration  Loop:  Unfixed  type  of  iterative  loop  repeats  the  process  till  a  given  condition  is  true.
                   For example,
                   • while loop
                   • do-while loop

                 There are two types of loops based on the condition which are as follows:
                 1.  Entry Controlled Loop: If the test condition is checked before executing the body of the loop, then it is called an
                   entry controlled loop. For example,
                    • for loop
                    • while loop
                 2.  Exit Controlled Loop: If the test condition is checked after executing the body of the loop, then it is called an exit
                   controlled loop.
                   For example,
                   • do-while loop

                 The for Loop
                 The Java for loop executes a set of statements repeatedly for a fixed number of times.
                 As soon as the control statement does not match the condition, the loop terminates.
                 The syntax of for loop is:

                    for (initialisation; test condition; increment or decrement)
                    {
                        // body of the loop
                    }





                                                                                              Statements and Scope  197
   194   195   196   197   198   199   200   201   202   203   204