Page 55 - Modular_V1.1_Flipbook
P. 55

06                                               LOOPS


















                                 Your Aim


                                to learn about:
                                        The for Loop                    The while Loop
                                         The do-while Loop                Nested Loop
                                         Infinite Loop
                                          Difference Between while and do-while Loops
                                           Jump Statements                       More Solved Programs



                 Sometimes, you may need to repeat a task several times or until a condition is satisfied. In C++, the
                 statements that are used to repeat a set of instructions are called iterative or looping statements.
                 It saves the trouble of writing the same statement again and again. Looping statements are very
                 useful and necessary for developing applications.
                 Loops allow you to repeat a section of your program for a certain number of times. The repetition
                 continues until a condition is true. When the condition becomes false, the loop ends and control
                 passes to the statements following the loop. C++ provides three kinds of loops which are for
                 loop, while loop and do-while loop.

                     THE FOR LOOP

                 The for loop executes a simple or compound statements for a fixed number of times or until a
                 condition becomes false. The syntax of the for statement is given below:

                 for(initialization; condition; counter)
                 {
                 statements;

                 }
                 where,
                      initialization is a method to assign value to the variable(s) starting the loop (called loop
                      variables). Once the initialization process is completed, it’s never evaluated again.

                      condition is a conditional expression to decide whether to enter the loop or not.
                      counter is a variable to change the value(s) of loop variable(s) after each iteration. Iteration
                      is the process of running a loop one time.



                                                                                                         Loops    53
   50   51   52   53   54   55   56   57   58   59   60