Page 131 - Computer Genius Class 08
P. 131

l    If the condition for ‘if’ is False, it checks the condition of the next ‘elif’ block and so on.

                 l    If all the conditions are False, body of ‘else’ is executed.
                 Syntax:

                   if (Condition1):
                     Statement1
                     Statement2
                     . . . . . . . . . .
                   elif(Condition2):
                     Statement3
                     Statement4
                     . . . . . . . . . .
                   elif(Condition3):
                     Statement5
                     Statement6
                     . . . . . . . . . .
                   else :
                     Statement7
                     Statement8
                   . . . . . . . . . .




                    l     Only one block among the several ‘if-elif-else’ blocks is executed according to the condition.
                    l   The ‘if’ block can have only one ‘else’ block. But it can have multiple ‘elif’ blocks.


                 Example 5: if-elif-else Statement
                   num = 3.4
                   if num > 0:
                     print("Positive number.")

                   elif num ==0:
                     print("Z ero")
                   else :
                     print("N egative number")
                 Output:

                   Positive number.
                 Description:

                 In the above program, 2 conditions are being tested using if and elif statement, if both the conditions
                 are False then else statement get’s executed.

                 Iterative Statements

                 Iterative statements or looping statements allow us to execute a set of code repeatedly as long
                 as a specified condition is true.  hese are mainly used when we need to run the same code again
                 and again.



                                                                                    Control Flow Statements   129
   126   127   128   129   130   131   132   133   134   135   136