Page 200 - AI Ver 1.0 Class 10
P. 200

Conditional Statements

              Conditional statements are used for selecting the block of statements to be executed based on the condition. We
              specify the condition in the program which evaluates to either True or False. If condition is True then the block
              of statements written for True will be executed and in case the condition is False then the block of statements
              for False will be executed. It is also called branching as a program decides which statement to execute based on
              the result of the evaluated condition. In Python:
                 • A block is identified by using an indentation (minimum 1 space). Ensure that all statements in one block are
                 indented at the same level.

                 • We use if to do conditional programming.
                 • 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 with the help of a flowchart.





                                              if           False
                                           Condition



                                                  True          elif        False
                                                              Condition
                                           Body of if
                                                                    True


                                                            Body of elif       Body of else



                                              Exit


               if (a condition evaluates to True):                   Num=int(input("Enter the number: "))
                        statements to be executed                    if Num>9 and Num<100:

                        always indented                              print("Two Digit Number")

               if (a condition evaluates to True):                   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")







                        198   Touchpad Artificial Intelligence-X
   195   196   197   198   199   200   201   202   203   204   205