Page 156 - CA_Blue( J )_Class10
P. 156

08                    ITERATIVE CONSTRUCTS IN



                                                                                    JAVA















                        Learning Objectives



                   8.1 Different Parts of a Loop       8.2 Categories of Loops          8.3 The for Loop
                   8.4 The while Loop                  8.5 The do-while Loop            8.6 Different Forms of Loops
                   8.7 Interconversion Between Different Types of Loops                 8.8 Jump Statements






              Sometimes, we may require to execute a set of programming statements repeatedly until a specified condition evaluates
              to true. The statements that are used to execute a a set of statements repeatedly are called looping statements or loops.
              One pass of a loop is known as an iteration. Hence, the looping statements are also known as iterative statements.


                   8.1 DIFFERENT PARTS OF A LOOP
              A loop consists of the following parts:
              •  Initialization: In this part, a variable that helps to execute the loop is initialized. This variable is known as a loop
                 control variable. This part is executed only once. For example, i = 1.
              •  Condition for Testing: In this part, a conditional expression is given that evaluates at the start of every iteration of
                 the loop. The loop will stop executing when the condition becomes false. For example, i <= 5.
              •  Increment or Decrement: Each and every iteration will increase or decrease the value of the loop control variable.
                 For example, i++ or i--.
              •  Job Performed by the Body of the Loop: Every loop has specific statements inside its body that are required to
                 perform the specified task or job. For example,
                {
                    System.out.println("The value of loop control variable is: " + i);
                }
                The loop body is repeatedly executed until the specified test condition will evaluate to true.


                   8.2 CATEGORIES OF LOOPS
              Loops can be divided into two categories which help to control the execution of the loops at the entry level or at the
              exit level. The two types are as follows:
              •  Entry Controlled Loop: If the testing condition is checked before executing the body of the loop, then it is called
                 entry controlled loop. Examples of entry controlled loops are for and while.



                154154  Touchpad Computer Applications-X
   151   152   153   154   155   156   157   158   159   160   161