CONDITIONAL STATEMENT IN PYTHON
CONDITIONAL STATEMENT
- Decision Making Statement In Programming Language Decides The Direction Of Flow Of Program Execution.
- In Conditional Statement We Use If , If Else , If Elif Else And Nested If.
- IF — — TRUE BLOCK
- ELSE — — FALSE BLOCK
- THE MOST IMPORTANT THING IN THE CONDITIONAL STATEMENT IS INDENTATION.
If Condition :- “if statement” is Written By Using “if” Keyword.
- If the condition is TRUE automatically the statement under the TRUE BLOCK will be executed and ends the program.
- If the condition is FALSE the program will end suddenly.
- SYNTEX:- if (condition):
- statement 1
- statement 2
- .
- .
- statement n
- : ← — — Represent INDENTATION.
- Indentation means alignment.
- After the condition we apply Colon.
- Every condition should end with Colon and its automatically in the next line the FOUIR spaces will be ignored and from the fifth space the statement will be return.
- After Indentation the cursor always start with fifth space.
Example :-
n = int(input(“Enter the value of n : “))
if (n==0):
print(“Zero”)
If-Else-Condition :- “if-else statement” is Written By Using “if- else” Keyword.
- If the condition is TURE automatically all the statements under the true block will be executed and ELSE part will be ignored.
- If the condition is FALSE than all the statements under the true block will be ignored and ELSE part will be executed.
SYNTEX:- if (condition):
statement 1
statement 2
.
.
else:
statement 1
statement 2
Example :-
n = int(input(“Enter Your Age : “))
if (n>=18):
print(“You are Eligible For Vote”)
else:
print(“You Are Under 18 Come Back When You Are 18”)
If-Elif-Else Condition :-”if-elif-else statement” is Written By Using “if-elif-else” Keyword.
- The if-elif-else statement are like a ladder of if and else statements.Their can be multiple elif statement.
- The elif statement also has its condition so, if the elif condition is True it will execute the elif statement.
- Here the else statement will only execute if both of the if and elif statements are False.
SYNTEX:- if (condition):
Statement 1
Statement 2
.
.
elif (condition):
Statement 1
Statement 2
.
.
else:
Statement 1
Statement 2
Example:-
a = int(input(“Enter the no of a” : ))
b =int(input(“Enter the no of b” : ))
if (a==b):
print(“a and b are equal”)
elif (a>b):
print(“a is greater than b”)
else:
print(“a is less than b”)
NESTED-IF:-
- Nested if means (if condition inside the if condition) so if the condition is TRUE again inside the if we will write another condition.
SYNTEX:- if (condition):
if (condition):
Statement 1
Statement 2
.
.
else:
Statement 1
Statement 2
else:
Statement 1
Statement 2
Example:-
a = int(input(“Enter the value of a”))
b = int(input(“Enter the value of b”))
c = int(input(“Enter the value of c”))
if (a>b):
if (a>c):
print(“a is large”)
else:
print(“c is large”)
elif (b>c):
print(“b is large”)
else:
print(“c is large”)