Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 81 - 2612_Digimind
P. 81

    As you know, a computer program is a set of instructions given to the computer to perform
                     a specific task. These individual instructions are known as statements. All the statements are
                     executed  sequentially,  one  after  the  other,  as  they  appear  in  a  program.  However, in  some
                     situations,  we have to change the order of execution of the program based on certain conditions.
                     In such situations, we use control statements to control or change the flow of execution.

                               ITERATIVE STATEMENTS

                     Iterative statements refer to the statements that are used to repeat a task based on a given
                     condition. These statements are also known as looping statements. An iteration means one pass
                     of a loop.

                     Python provides the following iterative statements:

                     (i)  For loop                   (ii)  While loop

                     THE FOR LOOP

                     The for loop is a type of iterative statement that allows the user to repeat a set of statements
                     multiple times until a given condition is falsified. The for loop is commonly used when the number
                     of iterations is known in advance or when iterating over a sequence, such as a list, string, tuple or
                     a range.
                     The syntax of for loop is as follows:

                     for <counter variable> in range(start, stop, step_size):
                         statements

                     The  range() function  is  an in-built  function  in Python.  This function  generates  a  sequence  of
                     numbers. A sequence is a succession of values bound together by a single name. The range()
                     function is generally used in the for loop to iterate over the sequence of numbers.

                     The general syntax for using the range() function is given below:
                         range(start, stop, step_size): The start specifies the starting point for generating numbers.

                         By default, it starts from 0. The stop specifies the position up to which the numbers are to be
                         generated (last number is not included). The step_size specifies the increment or decrement.
                         By default, the value of the step_size = 1 and numbers are generated with an increment of
                         1. However, we can generate numbers by specifying the value of step_size according to our
                         requirement.

                         range(start, stop): It generates a set of whole numbers starting from ‘start’ to ‘stop–1’.
                         Example: range(3, 9) is equivalent to [3, 4, 5, 6, 7, 8].

                         range(stop): Generates a set of whole numbers starting from 0 to (stop–1).
                         Example: range(6) is equivalent to [0, 1, 2, 3, 4, 5].

                     The start and step_size values are optional in the application of range() function and they can
                     be omitted.

                                                                                                    Iteration in Python  79

| 76 77 78 79 80 81 82 83 84 85 86 |