Page 109 - Code_GPT_Class_8
P. 109

On executing the above program, you will get the following output:

                     Output

                  the sum of the first five natural numbers is 15







                                   Imagine you possess the power of the jump statement, allowing you to leap to any part of
                                   the universe. Where would you jump to, and what quests or missions would you undertake
                                   in each dimension?






                            Quick Quiz

                  1.  In programming, what is an "infinite loop"?
                             a. A loop that repeats a fixed number of times


                             b. A loop that repeats endlessly without a clear exit condition

                  2.  Which statement is used to prematurely exit a loop in programming?
                             a. Infinite Loop


                             b. Jump Statement

                  3.  The FOR statement is best suited for situations where:

                             a. You want to create an endless loop

                             b. You know the number of times you want to repeat a block of code

                  4.  What is the primary difference between the FOR and WHILE loops in programming?
                             a. FOR is used for conditional loops, WHILE is used for counting loops


                             b.  FOR is used when the number of iterations is known in advance, WHILE is used when the exit
                              condition is known beforehand



                              Recap




                               The  statements that  are used  to  repeat a set  of  instructions  are called  iterative or looping
                            statements.
                               Python provides two types of looping statements—for and while.

                               The for statement executes a simple or compound statement for a fixed number of times.

                              The range( ) function is an in-built function of Python.





                                                                                                 Loops in Python   107
   104   105   106   107   108   109   110   111   112   113   114