Page 94 - Touchcode_C8_Flipbook
P. 94

The 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.

                  The syntax of if...elif...else ladder is shown below:
                  Syntax:

                  if (Test Expressions_1):                              Test        True
                                                                    Expression 1           Statement 1
                      Indented block 1
                  elif (Test Expression_2):                                False

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


                                                                           False           Body of else




                                                                                                      Statement just
                                                                                                    below if...elif...else
                                                                                                         ladder
                  Sometimes, you need to repeat a task multiple time or you may need to repeat the task

                  either a certain number of times or until a condition is satisfied. In Python, the statements
                  that are used  to repeat a set of instructions  are called iterative or looping  statements.
                  Looping statements are very useful and necessary for developing applications.

                                                                                                            for each
                  Python provides two types of looping  statements—for and                                   item in
                  while. The  for statement is used  to repeat an instruction  or a                         sequence
                  set of instructions a fixed number of times. Whereas, the while                     Last item    True

                  statement is used to repeat a set of instructions until a condition                 reached?
                  evaluates to true. Let’s discuss these constructs in brief.
                                                                                                           False
                  THE FOR STATEMENT
                                                                                                     Body of for
                  The for statement executes a simple or compound statement for
                  a fixed number of times.
                                                                                                             Exit loop


                   92     Touchcode-VIII
   89   90   91   92   93   94   95   96   97   98