Page 252 - AI Computer 10
P. 252

The basic syntax of if statement is as follows:

                 if<conditional expression>:
                      Body of if(a set of statements)
            Example:













              Always remember that If statement will do nothing if the test expression evaluates to False.

            If-Else Statement

            The if else control structure also tests a conditional expression. If the condition evaluates to True, the statements
            inside the if part will be executed, and if the condition evaluates to False, then statements inside the else part
            will be executed.
            The basic syntax of if else statement is as follows:
                                                                                True                       False
                                                                                          <IF Conditi on>

                 if<conditional expression>:
                      body of if(Set of Statements)
                                                                           Body of IF                        Body of ELSE
                 else:
                      body of else( Set of Statements)
            Example:









            If-Elif-Else Statement

            Sometimes, just a single test condition is not sufficient to take a decision but, it may be several other conditions
            which need to be tested in a cascading order. In such situations, if…elif…else statement is used.
                                1
                 if<condition >:
                      Set of Statements
                                  2
                 elif<condition >:
                      Set of statements
                                  3
                 elif<condition >:
                      Set of statements
                 else:
                      Set of statements
            Let us make a program using if….elif….else conditional statement in which user enters a choice.






                118
                118
   247   248   249   250   251   252   253   254   255   256   257