Page 195 - CA_Blue( J )_Class9
P. 195

09                     ITERATIVE CONSTRUCTS




                                                                                   IN JAVA














                           Learning Objectives


                     9.1 Loop                                9.4 Categories of Loop
                     9.3 Different Forms of Loops






                 Many a time, we need to make the computer work in a specific way to complete a task. For example, if we need to
                 check whether a number is a prime number or not, we need to repeat some steps again and again.
                 For example, if we want to check whether 7 is a prime number or not. We have to find how many times 7 is
                 divisible by numbers from 2 to 7. If it is divided only 2 times, i.e., 1 and 7 only, then we can say it is a prime
                 number. So, we need  a statement that will be repeated to check divisibility. This is done by using loops.


                     9.1 LOOP
                 A loop is a programming construct that repeats a set of statements until a certain condition is met. A loop
                 can be repeated any number of times, it depends on the programmed code. The process of execution of the
                 loop is known as iteration.

                 9.1.1 Different Parts of a Loop
                 Let's learn about various parts of a loop.


                                      Parts of a Loop                                    Definition
                                                                            Loop  is a programming construct  which
                                                                            runs repeatedly based on a condition. The
                                 Condition     Increment      Body of       statements are executed until the condition
                                                  or
                  Initialisation  for Testing  Decrement     the Loop       is false.



                 Initialisation
                 In this part, a variable is initialised that helps the loop to execute. In this example, the variable will be initialised
                 as int i = 1;





                                                                                       Iterative Constructs in Java  193
   190   191   192   193   194   195   196   197   198   199   200