Page 270 - Computer Science V2.0 Class 11
P. 270

False
                                                         Test
                                                       Condition




                                                            True

                                                   Sequence of Statements
                                                       (if block)



                                                 Fig 10.3: Flow diagram of an if statement

              Syntax:
              if <conditional expression>:                              ……… header

                           <sequence of statements>                     ………. suite
              In the above description of the syntax for the if-statement, the first line is the header, and the rest of the statements
              form the suite (sometimes called the body of the if statement or if block). In the above description of the header,
              if is a keyword, and <conditional expression> is a boolean expression which will yield either True or False when
              the code is executed. Since the if clause is the header clause, it ends with a colon(:). The following statement(s) in
              its suite will be executed if the conditional expression yields True. These statements are indented at a level next to
              the header. The suite following the if clause may comprise a single statement, or a sequence of statements (possibly
              including compound statement(s)), or just a pass statement. If the condition is False, the suite is simply ignored.
              Consider the following example:
              num = int(input("Enter a number : "))
              if num%2 == 0:

                  print("Number is EVEN")
              print("Divisibility Check Done")
              In the above code,
              •  The conditional expression is num%2 == 0.
              •  If the conditional expression num%2 == 0 yields True (when the user enters an even number), the following
                 statement in the if block will be executed:
                                         print("Number is EVEN").

                 On completion of the if block, the next statement in the sequence will be executed. Thus, the statement
                                         print("Divisibility Check Done")
                 will get executed.
              •  If the conditional expression yields False, the statement in the if block will be ignored. The control moves to
                 the next statement immediately after the if statement. Hence, the statement:
                                          print("Divisibility Check Done")
                 will be executed.

              •  A variable such as num whose value controls the flow of execution of the program is called the control variable.
                 Note that the statement
                     print("Divisibility Check Done")



               256   Touchpad Computer Science (Ver. 2.0)-XI
   265   266   267   268   269   270   271   272   273   274   275