Page 440 - AI Ver 3.0 class 10_Flipbook
P. 440

• Conditions with the if statements can also be written within (). It is totally optional.
                 • else statement is optional and if required can be only one else statement in one if block.

                 • It is must to specify colon (:) after writing a condition with the if statement.
                 • The number of statements (also called as a block) written within if can be of any size.
              Let us understand the flow of conditional statements:

               if (Condition):
                                                                     Num=int(input("Enter the number: "))
                         statements to be executed if a condition evaluates
                                                                     if Num>9 and Num<100:
                    to True
                                                                            print("Two Digit Number")
                        always indented
               if (Condition):                                       Num=int(input("Enter the number: "))
                       Statements for True will be executed          if Num>0:

               else:                                                        print("Positive Number")
                    Statements for False will be executed            else:
                                                                            print("Negative Number")
               if (Condition):                                       Num=int(input("Enter the number: "))
                      Statements for condition1 will be executed     if Num%2==0:
               elif (Condition):                                            print("Even Number")
                      Statements for condition2 will be executed     elif Num%3==0:
               elif (Condition):
                                                                            print("Multiple of 3")
                      Statements for condition2 will be executed
                                                                     elif Num%5==0:
                     .
                                                                            print("Multiple of 5")
                     .
                                                                     else:
                     .
                                                                            print("Invalid Number")
               else:
                       Statements executed when all above conditions are
                   False


              Few examples of conditional statements:

               [1]:   Age=int(input("Enter the age: "))
                      if Age>18:
                          print("Eligible for Voting")
                      else:
                          print("Not Eligible for Voting")
                      Enter the age:  25
                      Eligible for Voting


               [2]:   Year=int(input("Enter year: "))
                      if Year%4==0 or Year%100==0 and Year%400==0:
                          print("Leap Year")
                      else:
                          print("Not a Leap Year")

                      Enter year:  2025
                      Not a Leap Year



                    438     Touchpad Artificial Intelligence (Ver. 3.0)-X
   435   436   437   438   439   440   441   442   443   444   445