Page 42 - Modular v1.1 Pyhton
P. 42

04             LOOPING STATEMENTS



                                                                                                IN PYTHON















                         Your Aim

                         to learn about:

                                   The for Statement                       The while Statement
                                    The jump Statements                       Some More Programs




                  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>:
                                                                                                         False
                         Statements
                                                                                                   Body of for

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















                   40     Touchpad MODULAR (Version 1.0)
   37   38   39   40   41   42   43   44   45   46   47