Page 397 - Ai_417_V3.0_C9_Flipbook
P. 397

The if…else Statement

                 When the condition is 'True' then the statements indented just below the if statement will be executed and if
                 the condition is 'False' then the statements indented just below the else statement will be executed. The else

                 statement is optional and there can be only one else statement in one if block. The syntax to use the if…
                 else statement is:

                 if (condition):

                      Statements for True
                 else:
                      Statements for False
                 Where,
                    • if and else are keywords.
                    • condition can be any relation or logical expression.
                    • statements are always indented and can be single or a block.

                  Program 5:  To input two numbers and display the bigger of the two numbers.
                 Algorithm                                      Flowchart

                 Step 1   Start                                                 START
                 Step 2   Input N1, N2
                 Step 3   if N1 is greater than N2 then
                              Display "N1 is bigger"                         Input N1, N2

                 Step 4   else
                              Display "N2 is bigger"
                 Step 5   Stop
                                                                                  Is            No
                                                                               N1>N2?
                 Source Code:

                 N1=int(input("Enter First Number:"))                                          Display "N2 is bigger"
                                                                                    Yes
                 N2=int(input("Enter Second Number:"))
                 if N1>N2:
                     print(N1," is bigger ")                             Display "N1 is bigger"
                 else:
                     print(N2,"is bigger ")
                 Output:
                                                                                 STOP
                 Enter First Number:34
                 Enter Second Number:45

                 45 is bigger
                  Program 6:  To input a number and display whether it is even or odd.

                 Algorithm
                 Step 1   Start
                 Step 2   Input Num



                                                                                        Introduction to Python  395
   392   393   394   395   396   397   398   399   400   401   402