Conditional Statement |
In
real life, there are times when we must make decisions and then decide
what to do next base on those decisions. Similar circumstances exist in
programming, where we must make decisions and then execute the following block
of code based on those decisions. In programming languages, decision-making
statements determine the course of programme execution.
There are different types of Decision making Statement.
- if
- if-else
- if-elif
- Nested if
If
Statement
The, if condition is the most
basic decision-making condition. It is used to determine if a statement or a
block of statements will be executed or not, i.e., if a condition is true, a
block of statements will be executed; otherwise, it will not.
Syntax:
if condition: # Statements to
execute if #
condition is true |
After examination, the
condition will be either true or false. If the value is true, the if statement
will execute the block of statements below it; otherwise, it will not. We can
also use a condition using the bracket '(' ')'.
Python uses indentation to
distinguish blocks, as we all know. As seen in the example below, the block
under an if statement will be identified:
if
condition: statement1 statement2 |
# Here if
the condition is true, if block # will
consider only statement1 to be inside # Its block. |
Flowchart
of Python if statement
Example:
#Write a Python Program to illustrate If statement i = 10 if (i > 15):
print("10 is less than 15") print("If Statement Fails")
|
Output If Statement Fails |
Because the if statement's
condition is false. As a result, the block below the if statement is skipped.
If-else
Statement
If a condition is true, the if
statement will execute a block of statements; if the condition is false, the if
statement will not. But what if the condition is false and we want to do
something else? This is when the otherwise statement comes in. When the condition
is false, we can combine the else statement with the if statement to execute a
block of code.
Syntax:
if (condition): # Executes this block
if #
condition is true else: # Executes this block
if #
condition is false |
Flowchart
of Python if statement
Example:
# Write a Python Program to illustrate If else statement i = 22 if (i < 15): print("i
is smaller than 15") print("Else
Statement Fails") else: print("i
is greater than 15") print("If
Statement Fails") |
Output: If Statement Fails |
After calling the statement
that is not in block, the block of code following the else statement is
performed because the condition in the if statement is false (without spaces).
Nested
If-else
An if statement that is the
target of another if statement is known as a nested if. An if statement inside
another if statement is referred to as nested if statements. Yes, we may stack
if statements within if statements in Python. We can put an if statement inside
another if statement, for example.
Syntax:
if (condition1): #
Executes when condition1 is true if (condition2):
# Executes when
condition2 is true # if
Block is end here # if Block is end here |
Flow Chart in Nested if-else
Example:
# Write a Python Program to illustrate nested If
statement i = 10 if (i == 10): # First
if statement if (i < 15): print("i
is smaller than 15") # Nested
- if statement # Will
only be executed if statement above # it is
true if (i < 12): print("i
is smaller than 12 too") else: print("i
is greater than 15") else:
print(“i is less than 10”) |
Output i is smaller than 15 i is smaller than 12 too |
if-elif-else
ladder
A user can choose from a
variety of alternatives here. From the top down, the if statements are
performed. The statement linked with that if is performed as soon as one of the
criteria controlling the if is true, and the rest of the ladder is bypassed. The
last else line will be performed if none of the criteria are true.
Syntax:
if (condition): statement elif (condition): statement . . else: statement |
Flow
Chart of If-elif-else Ladder Statement
Example:
# Write a Python Program to illustrate if-elif-else
ladder i = 35 if (i == 10): print("i
is 10") elif (i == 25): print("i
is 25") elif (i == 35): print("i
is 35") else: print("i
is not present") |
Output: i is 35 |