Page 495 - ComputerScience_Class_11
P. 495

The elif statement allows you to check additional conditions       Start
                 when the first if condition is not satisfied. You can use multiple
                 elif statements, which means that the program can test several
                 different conditions before reaching the final else (if any).
                                                                                if condition 1?     true
                 Syntax:

                 if condition1 :
                                                                                false                  Statement 1
                       first block
                 elif condition2:                                                                   true
                                                                               elif Condition 2?
                       second block
                                                                              false                    Statement 2
                 ………………
                 else:                                                           Statement 3

                       final block
                                                                                    Stop

                 Program 4: Write a program to check if a number is positive, negative or zero.

                     Program 4.py
                  File  Edit  Format   Run   Options   Window     Help

                  a=int(input('Enter any number: '))

                  if a>0:
                      print(a, 'is positive')
                  elif a==0:
                      print(a, 'is zero')
                  else:
                      print(a, 'is negative')




                     Output

                  Enter any number: 10
                  10 is positive




                 The Nested if Statement
                 The nested if statement in Python is a way of placing one if statement inside another. This allows you to check multiple
                 conditions in a hierarchical manner. It is useful when you need to make decisions based on the outcome of previous
                 conditions.

                 A nested if statement is used when you want to check a condition inside another condition. This makes your code
                 more flexible for complex decision-making scenarios. The basic structure of a nested if statement involves having an if
                 statement inside another if, elif or else block.





                                                                                                    Flow of Control  493
   490   491   492   493   494   495   496   497   498   499   500