0% found this document useful (0 votes)
29 views20 pages

Conditional Statements

Conditional statements in Python allow for decision making in programs. There are several types of conditional statements: 1) If statements execute code if a condition is True. Else statements execute code if the condition is False. 2) Elif statements (else if) allow checking multiple expressions in chained conditionals. Only one block will execute. 3) Nested conditional statements allow conditionals to be placed within other conditionals for complex logic. Indentation is used to identify nesting levels.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views20 pages

Conditional Statements

Conditional statements in Python allow for decision making in programs. There are several types of conditional statements: 1) If statements execute code if a condition is True. Else statements execute code if the condition is False. 2) Elif statements (else if) allow checking multiple expressions in chained conditionals. Only one block will execute. 3) Nested conditional statements allow conditionals to be placed within other conditionals for complex logic. Indentation is used to identify nesting levels.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CONDITIONAL STATEMENTS

Datec Learning Centers


Introduction
• Decision making is anticipation of conditions occurring while
execution of the program and specifying actions taken according to
the conditions
• Decision structures evaluate multiple expressions which produce
TRUE or FALSE as outcome.
• You need to determine which action to take and which statements to
execute if outcome is TRUE or FALSE otherwise
• Python programming language assumes any non-zero values as
TRUE, and if it is either zero or null, then it is assumed as FALSE
value
Datec Learning Centers
If statement
• The program evaluates the test expression and will execute
statement(s) only if the test expression is True.
• If the test expression is False, the statement(s) is not executed.
• In Python, the body of the if statement is indicated by the indentation.
The body starts with an indentation and the first unindented line
marks the end.

Datec Learning Centers


if statement

Datec Learning Centers


if statement

Syntax:
if(test-expression):
statement-block(s)
statement-x

Datec Learning Centers


Exampleamplerogram
Program 1:

value = float(input("Enter the car kilometeres:"))

if(value > 2500):

print("Need to change the engine oil")

Datec Learning Centers


Example Program
Program 2:

amount = float(input("Enter the amount:"))

if(amount > 40000):

print("Your Daily withdrawl amount exceeds the limit")

Datec Learning Centers


if..else statement

• The if..else statement evaluates test expression and will execute the body
of if only when the test condition is True.
• If the condition is False, the body of else is executed. Indentation is used to
separate the blocks.

Datec Learning Centers


if..else statement

Datec Learning Centers


if..else statement
Syntax:

if(test-expression):

true-block-statement(s)

else:

false-block-statement(s)

statement-x
Datec Learning Centers
Example Program
Program 1:

value = float(input("Enter the Bike Kilometeres:"))

if(value > 2500):

print("Need to change the engine oil")

else:

print("You can ride upto 2500 Kilometers! No need to change the

engine oil now") Datec Learning Centers


Example Program
Program 2:

amount = float(input("Enter the amount:"))

if(amount > 40000):

print("Your Daily withdrawl amount exceeds the limit")

else:

print(”Transaction Successful”)
Datec Learning Centers
elif statement (chained conditional)

• The elif is short for else if. It allows us to check for multiple expressions.
• If the condition for if is False, it checks the condition of the next elif block
and so on.
• If all the conditions are False, the body of else is executed.
• Only one block among the several if...elif...else blocks is executed
according to the condition.
• The if block can have only one else block. But it can have
multiple elif blocks. Datec Learning Centers
elif statement (chained conditional)
Same as else if in C/C++

Datec Learning Centers


elif statement (chained conditional)
Syntax
if(test-condition-1):
statement-1
elif(test-condition-2):
statement-2
...
...
elif(test-condition-n):
statement-n
else:
default-statement
statement-x Datec Learning Centers
Example Program
color = int(input("Enter the color value:")) elif color == 5:
if color == 1: print("Yellow Color")
print("Violet Color") elif color == 6:
elif color == 2:
print("Orange Color")
print("Indigo Color")
elif color == 7:
elif color == 3:
print("Red Color")
print("Blue Color")
else:
elif color == 4:
print("Invalid Color Entry!")
print("Green Color")
Datec Learning Centers
Checking multiple conditions in a statement
Syntax using logical and operator(Both the conditions has to be true):

if(test-condition-1 and test-condition-2):


statement-1
elif(test-condition-3 and test-condition-4 ):
statement-2
else:
default-statement
statement-x

Datec Learning Centers


Nested conditional (nested if Statement)

• We canOne conditional
write can also
an if statement be nested
inside within
another another This is called
if statement.
nesting in computer programming.

• We can also have an if...elif...else statement inside another


if...elif...else statement. Any number of these statements can be nested
inside one another. Indentation is the only way to figure out the level
of nesting.

Datec Learning Centers


Nested conditional (nested if Statement)

Syntax
if(test-condition-1):
if(test-condition-2):
statement-1
else:
statement-2
else:
statement-3
statement-x

Datec Learning Centers


Example Program
num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:"))
num3= int(input("Enter Third Number:"))
if(num1 > num2):
if(num1 > num3):
print("Number 1 is largest")
else:
print("Number 3 is largest")
else:
if(num2 > num3):
print("Number 2 is largest")
else:
print("Number 3 is largest")
Datec Learning Centers

You might also like