Page 243 - Ai_V1.0_Class9
P. 243

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 3:  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
                 Source Code:                                                  N1>N2?           No

                 N1=int(input("Enter First Number:"))

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


                 Enter First Number:34
                                                                                 STOP
                 Enter Second Number:45
                 45 is bigger

                 The if…elif…else Statement
                 When there are multiple conditions in a program then it is handled by using elif statement. It means if the
                 previous condition is not 'True' then try this condition written next to elif. If one condition is 'True' then the
                 statements below it will be executed and the rest of the conditions given using elif will be ignored. In case all
                 the conditions written with elif are not 'True', then the statements written just below the else statement will
                 be executed. The syntax of the if…elif…else statement is as follows:

                 if (condition1):
                   Statements 1
                 elif (condition2):
                   Statements 2
                 elif (condition3):

                   Statements 3
                 ...
                 else:
                   Statements executed when all above conditions are False
                                                                                        Introduction to Python  241
   238   239   240   241   242   243   244   245   246   247   248