ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING
3.2. Conditional (if), alternative (if-else), chained conditional (if-elif-else)
CONTROL STRUCTURES
if statement while loop break statement
if-else statement for loop pass statement
if-elif-else statement nested loop continue statement
Nested Conditional
Decision Making (or) Conditionals (or) Branching
The execution of the program depends upon the condition. The sequence of the control
flow differs from the normal program. The decision making statements evaluate the conditions
and produce True or False as outcome.
Types of conditional Statement
1. if statement
2. if-else statement
3. if-elif-else statement
4. Nested Conditional
Unconditional Statement
Control structures
Decision Making
Iteration
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING
1.if statement(conditional)
If statement contains a logical expression using which data is compared and a decision is
made based on the result of comparison.
Syntax
Flow Chart
False
True
Example:
a=10
if a==10:
print(“a is equal to 10”)
Result:
a is equal to 10
Test
Expres
sion
Body of if stmt
if <expression>:
true statements
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING
2. Alternative execution (if… else)
The second form of if statement is “alternative execution” in which there are two possibilities
and the condition determines which block of statement executes.
Syntax
If the testexpression evaluates to true then <body_1>statements are executed else
<body_2> statements are executed.
Flow Chart
True False
Example:
a=10
if a==10:
print(“a is equal to 10”)
else:
print(“a is not equal to 10”)
Result
a is equal to 10
Test
Expres
sion
Body of else
Body of if
End of Code
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING
3.elif else Statement(chained conditionals)
The elif statement or chained conditional allows you to check multiple expressions for
true and execute a block of code as soon as one of the conditions evaluates to true. The elif
statement has more than one statements and there is only one if condition.
Syntax
Flow Chart
False
True False
True
if <test_expression_1>:
<body1>
elif <test_expression_2>:
<body2>
elif <test_expression_3>:
<body3>
….
…..
else:
<bodyN>
Test
Expressi
on
of if
Test
Expressi
on
of elif
Body of if
Body of else
Body of elif
End of Code
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING
Example:
a=9
if a==10:
print(“a is equal to 10”)
elif a<10:
print(“a is lesser than 10”)
elif a>10:
print(“a is greater than 10”)
else
print(“a is not equal to 10”)
Result:
a is lesser than 10
4. Nested Conditionals (or) Nested if-else
One conditional can also be nested with another condition.(ie) we can have if…elif….else
statement inside another if …elif…else statements.
Syntax
if expression 1:
true statements
else:
if expression 2:
true statements
else:
false statement
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
GE3151-PROBLEM SOLVING AND PYTHON PROGRAMMING
Flow Chart
True False
True False
Example:
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Result:
Enter a number:50
Positive number
Test
Conditio
n 1
Test
Conditio
n 2
Body of if
Body of if
Body of else
End of Code