Page 49 - Modular v1.1 Pyhton
P. 49

Clickipedia




                   Never use assignment operator in place of equality operator in the test condition as it may
                   cause an infinite loop.


                 Program 10: To add first five natural numbers.



















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



















                    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.
                        A sequence is a succession of values bound together by a single name.
                        The  while  statement  executes  a  set of  statements repeatedly,  until  the  logical  expression
                        evaluates to true.
                        To come out of the infinite loop, we can either close the program window or press Ctrl + C.
                        The else block is executed when the condition given in the while statement becomes false.
                        The break is a keyword in Python which is used for bringing the program control out of the
                        loop.
                        When a continue statement is encountered inside a loop, control of the program jumps to
                        the beginning of the loop for next iteration, skipping the execution of rest of the statements
                        of the loop.



                                                                                  Looping Statements in Python    47
   44   45   46   47   48   49   50   51   52   53   54