Page 312 - Artificial Intellegence_v2.0_Class_9
P. 312

Selection Statements


        In Python,
           • Selection flow of control is achieved through conditional statements.
           • In  the  conditional/selection  statements,  flow  of  control  is  changed  based  on  a  condition.  We  specify  the
           condition in the program which evaluates to either True or False.
           • If condition is True then the block of statement written for True will be executed and in case the condition is
           False then the block of statements for False will be executed.
           • Conditional statements  are also called as branching  statements  as a program decides which statement to
           execute based on the result of the evaluated condition.
           • A block is identified by using an indentation (minimum 1 space). Ensure that all statements in one block are
           indented at the same level.
           • Python mainly provides three types of conditional statements—if, if…else and if…elif…else statements.
           Let us learn about them in detail.

        The if Statement

        We use the if statement to do conditional programming. Condition with the if statement can also be written
        within the parenthesis ( ). However, it is optional to use parenthesis. 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 statement
        can be of any size. A statement or block of statement will be executed only when the condition is True. If the
        condition is False then nothing happens.

        Syntax of the if statement:
        if (condition):

                 statements
        Where,
           • if is a keyword.
           • condition can be any relational or logical expression.

           • statements are always indented can be single or a block.
          Program 1:  To input the percentage of a student and award "Certificate of Excellence" if the student gets more
          than 80%.

        Algorithm                                                    Flowchart
                                                                                        START
        Start
        Input Percent                                                                Input Percent

        if Percent is more than 80 then
            Display "Congratulations"
                                                                                          Is            No
            Display "Your are awarded Certificate of Excellence"                     Percent > 80?
        Stop
        Source Code:                                                                        Yes
                                                                                Display "Congratulations"
        percent=int(input("Enter your percentage :"))
                                                                                Display "Yes are awarded..."
        if percent>80:
            print("CONGRATULATIONS")
                                                                                         STOP
            print("You are awarded– 'Certificate of Excellence'")
              310     Touchpad Artificial Intelligence (Ver. 2.0)-IX
   307   308   309   310   311   312   313   314   315   316   317