if statement in Python

For controlling the flow of the program by making conditions on that basis specific block of code will execute. If the statement is being used for making condition or decision withing program in Python.

if statement in Python


Contents

  1. if statement
  2. if else statement
  3. elif statement
  4. Nesting

 


1. if statement

In if statement, it first checks the condition if the condition is true then execute the block of code otherwise skip it and execute next statement.

Syntax –

if condition :
   # true statement

# next statement

Example –

num = 4
if num%2 == 0 :
   print("Even number")

print("num : ",num)

When we execute the above program, it will produce the following output –

Even number
num : 4

2. if else statement

If statement only executes when condition return true if the condition is false for handling that we use else statement with if. It executes when the condition is false.

Syntax –

if condition :
   # true statement
else:
   # false statement

# next statement

Example –

num = 5
if num%2 == 0 :
   print("Even number")
else:
   print("Odd number")
print("num : ",num)

When we execute the above program, it will produce the following output –

Odd number
num : 5

3. elif statement

elif statement allows us to define multiple if statment. If the condition is not true then we again check with other condition using elif. We can define multiple elif statements but only one else statement.

Syntax –

if condition:
   # true statement1
elif condition:
   # true statement2
elif condition:
   # true statement3
.
.
.
else:
   # false statement

# next statement

Example –

num = 30
if num == 10:
   print("If statement1")
elif num == 20:
   print("If statement2")
elif num == 30:
   print("if statement3")
else:
   print("Else statement")

print("num : ",num)

When we execute the above program, it will produce the following output –

if statement3
num : 30

In above program, we defined multiple if statements using elif statement. Only one if statement will executes when we run above program. It checks to if statement until true condition not found. If found valid condition then execute the block of code and then go to next block of code.


4. Nesting

In a program in some case, we require checking another condition if one is true. For this purpose, we generally use nested if statement using this we define if statement within another if statement. Same we can do with else.

Syntax –

if condition:
   # true statement
   if condition:
      # true statement
   else:
      # false statement
else:
   if condition:
      # true statement
   else:
      # false statement

# next statement

Example –

num = 30
if num == 10:
   print("If statement1")
elif num == 20:
   print("If statement2")
elif num == 30:
   print("if statement3")
else:
   print("Else statement")

print("num : ",num)

When we execute the above program, it will produce the following output –

if statement3
num : 30

Related Post

Spread the love

Be First to Comment

Leave a Reply

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