Page 242 - Ai_V1.0_Class9
P. 242

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 mandatory 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.
              The syntax of the if is as follows:

              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 2:  To input the percentage of a student and award "Certificate of Excellence" if the student gets more
                than 80%.

              Algorithm                                                     Flowchart
               Step 1   Start                                                                 START
               Step 2   Input Percentage
                                                                                          Input Percentage
               Step 3   if Percentage is more than 80 then
                            Display "Congratulations"
                            Display "You are awarded - 'Certificate of Excellence'"             Is            No
               Step 4   Stop                                                              Percentage > 80?

              Source Code:
                                                                                                  Yes
              percent=int(input("Enter your percentage:"))
                                                                                      Display "Congratulations"
              if percent>80:                                                          Display "You are awarded"
                  print("Congratulations")

                  print("You are awarded– 'Certificate of Excellence'")
                                                                                               STOP
              Output:

              Enter your percentage:89
              Congratulations
              You are awarded– 'Certificate of Excellence'

              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
                    240     Artificial Intelligence Play (Ver 1.0)-IX
   237   238   239   240   241   242   243   244   245   246   247