Page 66 - Code & Click - 8
P. 66

7                           Iterative Statements in Python







                 Pre-Processing
                 Pre-Processing
                       • Iterative Statements                 •  Components of a Loop
                       • For Loop                             •  Use of Range( ) function in For Loop
                       • While Loop                           •  Jump Statements in a Loop in Python

            The statements or instructions in a program or software are executed in a sequential manner. But
            sometimes it is necessary to disrupt the sequential execution of statements and divert the execution
            control to some other part of the program. Statements that are used to control the flow of execution
            of statements in a program are called Control Statements. These are usually of two types:
               •  Selection Statements                        • Iterative Statements
            You have learned about Selection statements, or Decision Control statements, in Python. Let’s now
            learn about Iterative statements in Python.

            ITERATIVE STATEMENTS
            Sometimes,  a  set  of  instructions  needs  to  be  executed  a  number  of  times  based  on  a  specific
            condition. Iterative statements are statements that are executed a specified number of times or
            until a specified condition is True. Iterative statements are also called Loops.

            COMPONENTS OF A LOOP
            The main components of a loop are:
               •     Initialisation of Control Variable: A control variable is usually a numeric variable that counts
                  the number of times the loop is executed. It is also known as a Counter. It is assigned an initial
                  value, which is incremented or decremented after every execution of the loop statements.
               •  Updation  of  Control  Variable:  This  is  the  part  of  the  loop  where  the  counter  variable  is
                  incremented or decremented.

               •  Test  Expression:  This  is  usually  an  expression,  containing  relational  operators,  which  is
                  evaluated to check if the loop is to be executed further or not. The loop execution continues
                  until the value of the test expression is True.

               •  Body of Loop: This is the set of statements that will be executed repeatedly.
            Python provides two basic types of loops:
               •  For loop                             • While loop

            FOR LOOP
            The For loop is used when the number of times the loop is to be executed is known beforehand. That
            is why the For loop is called a Definite loop. The syntax of the For loop is as follows:
                   for <counter variable> in <sequence>:
                          body of for loop
                   other statements


            64
   61   62   63   64   65   66   67   68   69   70   71