Conditionals Statement
Conditionals Statement
Conditional if
Alternative if… else
Chained if…elif…else
Nested if….else
Conditional (if):
conditional (if) is used to test a condition, if the condition is true the
statements inside if will be executed.
syntax:
if(condition 1):
Statement 1
Flowchart:
Example:
1. Program to provide flat rs 500, if the purchase amount is greater than
2000.
2. Program to provide bonus mark if the category is sports.
Program to provide flat rs 500, if the purchase amount is greater than
2000.
purchase=eval(input(“enter your purchase amount”))
if(purchase>=2000):
purchase=purchase-500
print(“amount to pay”,purchase)
output
enter your purchase
amount
2500
amount to pay
2000
output
enter ur mark out of 100
85
enter ur categery G/S
S
mark is 90
alternative (if-else)
In the alternative the condition must be true or false. In
this else statement can be combined with if statement. The else statement
contains the block of code that executes when the condition is false. If the
condition is true statements inside the if get executed otherwise else part
gets executed. The alternatives are called branches, because they are
branches in the flow of execution.
syntax:
Flowchart:
Examples:
1. odd or even number
2. positive or negative number
3. leap year or not
4. greatest of two numbers
5. eligibility for voting
Odd or even number
n=eval(input("enter a number"))
if(n%2==0):
print("even number")
else:
print("odd number")
Output
enter a number4
even number
Output
enter a number8
positive number
Output
enter a yaer2000
leap year
greatest of two numbers
a=eval(input("enter a value:"))
b=eval(input("enter b value:"))
if(a>b):
print("greatest:",a)
else:
print("greatest:",b)
Output
enter a value:4
enter b value:7
greatest: 7
eligibility for voting
age=eval(input("enter ur age:"))
if(age>=18):
print("you are eligible for vote")
else:
print("you are eligible for vote")
Output
enter ur age:78
you are eligible for vote
Chained conditionals(if-elif-else)
The elif is short for else if.
This is used to check more than one condition.
If the condition1 is False, it checks the condition2 of the elif block. If
all the conditions are False, then the else part is executed.
Among the several if...elif...else part, only one part is executed
according to the condition.
• The if block can have only one else block. But it can have multiple elif
blocks.
• The way to express a computation like that is a chained conditional.
syntax:
Flowchart:
Example:
1. student mark system
2. traffic light system
3. compare two numbers
4. roots of quadratic equation
Output
enter ur mark:78
grade:B
Output
enter x value:5
enter y value:7
x is less than y
Nested conditionals
One conditional can also be nested within another. Any number of
condition can be nested inside one another. In this, if the condition is true it
checks another if condition1. If both the conditions are true statement1 get
executed otherwise statement2 get execute. if the condition is false
statement3 gets executed
Syntax:
Flowchart:
Example:
1. greatest of three numbers
2. positive negative or zero
greatest of three numbers
a=eval(input(“enter the value of a”))
b=eval(input(“enter the value of b”))
c=eval(input(“enter the value of c”))
if(a>b):
if(a>c):
print(“the greatest no is”,a)
else:
print(“the greatest no is”,c)
else:
if(b>c):
print(“the greatest no is”,b)
else:
print(“the greatest no is”,c)
output
enter the value of a 9
enter the value of a 1
enter the value of a 8
the greatest no is 9
positive negative or zero
n=eval(input("enter the value of n:"))
if(n==0):
print("the number is zero")
else:
if(n>0):
print("the number is positive")
else:
print("the number is negative")
output
enter the value of n:-9
the number is negative