Page 96 - ThinkGPT_V2.1_C7_Flipbook
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.


                                                                           Start
                  Syntax:

                  if (condition 1):
                                                                                       True
                      Indented block 1                                 if condition 1           block 1

                  elif (condition 2):
                                                                              False
                      Indented block 2
                                                                                       True
                  elif (condition 3):                                  if condition 2            block 2
                      Indented block 3
                                                                              False
                  else:

                      Indented block                                                   True
                                                                       if condition 3            block 3


                                                                              False
                                                                                                  block

                                                                                                             Stop






                                        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    Premium Edition-VII
   91   92   93   94   95   96   97   98   99   100   101