Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 130 - 2609_Logiccode
P. 130

    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
                                                                            if condition 1           block 1
                          Indented block 1
                      elif (condition 2):                                         False

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

                          Indented block 3
                                                                                  False
                      else:
                                                                                            True
                                                                            if condition 3           block 3
                          Indented block

                                                                                  False               block
                                  To stop the running of
                                  an endless loop press:
                              SHORT KEY  CTRL +  C                                                               Stop

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

                      128   Premium Edition-VII

| 125 126 127 128 129 130 131 132 133 134 135 |