Page 493 - ComputerScience_Class_11
P. 493

Syntax:
                                                                                         Start
                 if condition :

                       block of code if true
                 Working of if Statement                                             if(condition)?        true
                 The working of if statement is as follows:
                                                                                                              Statement
                 1.  When the program reaches  the if statement,  it evaluates the   false
                   condition.

                 2.  If the condition is True, the indented code block following the if
                   statement is executed.                                                Stop
                 3.  If the condition is False, the indented block is skipped and the
                   program continues with the next part of the code.

                 Program 2: Write a program to check if a number entered by the user is greater than 10 and also check if a number
                 is even.

                     Program 2.py

                  File  Edit  Format   Run   Options   Window     Help

                  x = int(input("Enter a number: "))
                  if x > 10:
                      print("x is greater than 10")
                  if x % 2 == 0:
                      print("x is even")




                     Output

                  Enter a number: 12
                  x is greater than 10
                  x is even




                 The if-else Statement
                 The if-else statement is one of the most commonly used control structures in programming. It allows the program to
                 make decisions based on a condition. The program checks if a condition is True or False and executes one of two blocks
                 of code accordingly.

                 This type of control flow is essential for enabling decision-making within a program.

                 Syntax:
                 if condition :
                       block of code if true
                 else:
                       block of code if false




                                                                                                    Flow of Control  491
   488   489   490   491   492   493   494   495   496   497   498