Page 96 - Digicode_AI_class_7
P. 96

if…elif…else Ladder

            The  if…elif…else ladder  is another  type  of if statement.  It helps us to  test  multiple  conditions
            and follows a top-down approach. In this, as soon as the condition of the if evaluates to be true,
            the  indented block associated  with  that  ‘if’ is executed,  and  the  rest  of  the  ladder  is avoided.
            If none of the conditions evaluates to true, then the final else statement gets executed.


            Syntax:
                                                                              True
            if (Test Expressions_1):                        Test Expression 1        Statement 1

                Indented block 1
                                                                     False
            elif (Test Expression_2):

                Indented block 2                                              True
                                                            Test Expression 2        Statement 2
            elif (Test Expression_3):
                Indented block 3                                     False

            else:                                                             True
                                                            Test Expression 3        Statement 3
                Indented block

                                                                     False
                                                                                     Body of else

                                                                                            Statement just below
                                                                                             if...elif...else ladder






                                  To stop the running of an endless loop press CTRL+C.




            Program: To check whether a given number is two-digit number, three-digit number or four-
            digit number.

























                 94    DigiCode AI-VII
   91   92   93   94   95   96   97   98   99   100   101