Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 137 - 2612_Digimind
P. 137

    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.

                               CONTROL STRUCTURE

                     A control structure is a programming construct that affects the flow of the execution of a program.
                     Various types of control structures provided by Python are shown below:

                                                              Control Structure

                        Sequential Statements                Selection Statements                 Iterative Statements

                               SEQUENTIAL STATEMENTS

                     The statements that are executed in sequential order, i.e., one after the other without any jumps,
                     are called sequential statements. A sequential structure is also known as a straight line path.

                                                                 Statement 1

                                                                 Statement 2

                                                                 Statement 3

                               SELECTION STATEMENTS

                     Some problems cannot be solved by performing a set of ordered steps as seen in sequential
                     execution. When programmers are required to execute a particular set of statements depending
                     on a particular  test  condition,  a selection  or decision  making  statement  is  required.  Python
                     provides the following selection statements:

                     (i)  if statement                    (ii)  if-else statement   (iii)  if-elif-else statement

                     THE if STATEMENT

                     The if statement is a decision-making statement that is used to control the flow of execution of
                     statements.

                                                                                            Control Structures in Python  135

| 132 133 134 135 136 137 138 139 140 141 142 |