Page 86 - Digicode_AI_class_8
P. 86

Sometimes, you need to repeat a task multiple time or you may need to repeat the task either a
            certain number of times or until a condition is satisfied. In Python, the statements that are used to
            repeat a set of instructions are called iterative or looping statements. Looping statements are very
            useful and necessary for developing applications.

            Python provides two types of looping statements—for and while. The for statement is used to repeat
            an instruction or a set of instructions a fixed number of times. Whereas, the while statement is used
            to repeat a set of instructions until a condition evaluates to true. Let’s discuss these constructs in
            brief.

            The for Statement                                                                      for each
                                                                                                   item in
            The  for  statement executes  a  simple or  compound  statement for
                                                                                                   sequence
            a fixed number of times. The syntax of the for statement is given
                                                                                             Last item    True
            below:
                                                                                             reached?
            for <variable> in <iterator>:

                   Statements                                                                     False

                                                                                            Body of for
            Program: To print all the days of a week.
                                                                                                    Exit loop












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



















            Program: To print a pattern using for loop.













                 84    DigiCode AI-VIII
   81   82   83   84   85   86   87   88   89   90   91