COL100 �Introduction to Computer Science
Lecture 8 – Conditionals
January 2025
Semester II
2024-2025
Vireshwar Kumar, Aaditeshwar Seth
CSE@IITD
Agenda
2
Disclaimer: The contents of these slides are taken from various publicly available resources such as books, talks and lectures. They are being used for the purpose of classroom teaching and academic dissemination only. The sources are acknowledged wherever possible.
Conditional for Jumping/Branching
3
if Statement
marks = 50
if (marks >= 40):
print("Passed, Good!")
4
Conditional Expression
marks = 50
if (marks - 39):
print("Passed, Good!")
5
if-else Statement
marks = 50
if (marks >= 40):
print("Passed, Good!")
else:
print("Work harder!")
6
if-elif-else Statement
marks = 50
if (marks >= 40):
print("Passed, Good!")
elif (marks >= 30):
print(“Borderline Pass!")
else:
print("Work harder!")
7
Nested if-else
8
if (exp1):
if (exp2):
stmta
else:
stmtb
if (exp1):
if (exp2):
stmta
else:
stmtb
Find the Largest Number
x = 4
y = 5
z = 6
if (x > y):
max = x
else:
max = y
if (max > z):
print(max)
else:
print(z)
9
Another Version: Find the Largest Number
if (x > y and x > z):
max = x
elif (y > x and y > z):
max = y
else:
max = z
print(max)
10
Yet Another Version
x = 4
y = 5
z = 6
if (x > y):
if (x > z):
max = x
else:
max = z
else:
if (z > z):
max = y
else:
max = z
print(max)
11
Reading Material
12
Additional example
COL100: Introduction to Computer Science (Semester I, 2024-2025)
13