Page 74 - CloudGPT_C8_Flipbook
P. 74

If you do this with the same method, discussed above, it will become difficult to do so. In
                  such a scenario, you will use a loop to repeat the task as many times as you need.

                  INCREMENTING A LOOP

                  Loops execute a block of code repetitively till the specified condition remains true. Each
                  time the loop runs, the condition is checked with the help of the loop's control variable.
                  Control variable is used to prevent the loop from running forever as that can cause an
                  infinite loop. As soon as the condition results in false, the execution of the loop stops.


                  Whenever the condition is true, the block of code executes. Moving to the next iteration, you
                  need to increase the value of the loop control variable. This is called incrementing a loop.





                    Example 1: To print numbers from 1 to 10.                                          Subject Enrichment


                  Flowchart:

                                                                  Start




                                                                num = 1






                                                                                     No
                                                              If num <= 10




                                                                         Yes


                                                               Print num





                                                             num = num + 1



                                                                  End


                  Most loops have a variable called a counter variable, which keeps track of how many times
                  the loop is executed.






                   72     Premium Edition-VIII
   69   70   71   72   73   74   75   76   77   78   79