Example HTML page

Conditional Statements

Introduction to Conditional statements

The conditional statements are also known as “Decision Making statements”. Python language provides the following kinds of conditional statements.

Statement Description
if An if statement has Boolean expression which results either TRUE or FALSE.
if else If statement is followed by an else statement which is optional. When an if  is evaluated to false then the else statement is executed.
Nested statements We can implement if statement and or if-else statement inside another if or if – else statement. Here more than one if conditions are applied & there can be more than one if within elif.

Let us have a look on the each of the above statements.

if statement

When an if condition is true then the statements in if section are executed. Otherwise the statements which are outside of an if section will be executed.

The syntax of if statement is as follows:

if(condition):
     statements    

Ex : A sample program on if statement

>>> x=21
>>> if x>15:
	print("x is greater")

	
x is greater

Another example of if statement is

x=21
if x<15:
    print("x is greater")
print("x is not greater")

Output

x is not greater

if else statement

When an if condition is true then the statements in if section are executed. When the condition is evaluated to false the statements in the else section will be executed

The syntax of if else statement is as follows

if(condition):
     statements  
else:
     statements  

Ex  A sample program on if else statement

>>> p=10
>>> q=20
>>> if p>q:
	print("p is greater")
else:
	print("q is greater")

	
q is greater

Nested if statement

A nested if is an if statement that has another if statement inside it. When if condition is satisfied then the immediate if section is executed; when the condition is evaluated to false it directly goes to the else section and the statements in it are executed

The syntax of nested if statement is as follows

if(condition):
     statements 
          if(condition):
              statements
          elif (expression)
       statements
else:
      statements

Ex A simple example of nested if statement is shown in below

num=float(input("enter a number:"))
if num>=0:
    if num==0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Output

enter a number:7

Positive number

The elif statements/Nested if-else statements

elif is a keyword in python. It is used as replacement of else if . Whenever you want to add another condition in the same program this elif is used. It is otherwise called as Nested if-else.

The syntax of elif statement is as follows

if (condition): 
      statements 
elif (condition): 
      statements 
else: 
      statements 

Ex A sample program of elif statement.

>>> a=267
>>> b=276
>>> if a>b:
	print("a is greater")
elif a==b:
	print("Both are equal")
else:
	print("b is greater")

	
b is greater

The if-elif-else statement

When an if condition is true then the statements in if section are executed. When the condition is evaluated to false the statements in the else section will be executed. In the meanwhile it also evaluates the elif sections. If it results to true then it moves to next immediate elif section otherwise the execution flow transfers to the else section.

The syntax of  if-elif-else statements is shown in below

if boolean-expression:
   statements
elif boolean-expression:
   statements
elif boolean-expression:
   statements
elif boolean-expression:
   statements
else:
   statements

Ex A sample program of if-elif-else statement

x=int(input("Enter any number"))
if x<=5:
    print("True")
elif x==6:
    print("False")
elif x==8:
    print("Equal")
else:
    print("Bye")

Output

Enter any number 3

True

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest