Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 113 - 2611_SmartGPT Pro V(5.0) C-7
P. 113

    Syntax of the if … else … statement:                           Start

                     if (condition):

                       Statement block 1                                          Input Age
                     else:

                         Statement block 2                                                           False
                     Note  that  both  statement  blocks  1 and  2  are           Is Age>18?
                     indented. Block 1 is under if, so it will be executed
                                                                                        True
                     if the condition mentioned is True. Block 2 is under                              Print ‘Not Eligible’
                     else, so it will be executed if the condition is False.     Print ‘Eligible’

                     Here, if the age of the user is more than 18, ‘Eligible’
                     will  be printed,  otherwise  ‘Not  Eligible’  will  be         Stop
                     printed.

                     Program 3. To show a message if the user is ‘Eligible’ or ‘Not Eligible’ to cast a vote.
                         Program3.py

                      File  Edit  Format   Run    Options   Window    Help
                      age=int(input("Enter Age: "))
                      if ( age>=18):
                          print("You are eligible to cast a vote.")
                      else:
                          print("You are not eligible to cast a vote.")

                         Output

                      Enter Age: 25
                      You are eligible to cast a vote.

                      Enter Age: 15
                      You are not eligible to cast a vote.

                     Program 4. To check whether a number is odd or even.

                         Program4.py
                      File  Edit  Format   Run    Options   Window    Help

                      Num=int(input("Enter a Number: "))
                      if Num%2==0:
                        print("Number entered is an even number.")
                      else:
                        print("Number entered is an odd number.")

                                                                                               Flow of Control in Python  111

| 108 109 110 111 112 113 114 115 116 117 118 |