1 of 13

COL100 �Introduction to Computer Science

Lecture 8 – Conditionals

January 2025

Semester II

2024-2025

Vireshwar Kumar, Aaditeshwar Seth

CSE@IITD

2 of 13

Agenda

  • Conditional Statement

​

  • Nested Conditional Statement

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.

3 of 13

Conditional for Jumping/Branching

  • A conditional statement is used for jumping/branching to an instruction which is not the next one, based on a condition evaluating to true or false.

​

  • It allows different sets of instructions to be executed depending on truth or falsity of a logical condition.

​

  • The conditions usually include logical expressions but can be any expression. In case of arithmetic expression, the value of the expression can be used.
    • non-zero value means condition is true
    • value 0 means condition is false

​

​

​

​

​

3

4 of 13

if Statement

  • The condition to be tested is any expression enclosed in parentheses.

​

  • The statement after if is executed only if the condition is true/non-zero.

​

  • Syntax:
  • if (expression) :
  • block of statements

​

​

marks = 50

if (marks >= 40):

print("Passed, Good!")

4

5 of 13

Conditional Expression

  • Any expression that produces a value can be used in control structures.

​

  • Nonzero values are true, zero values are false.

marks = 50

if (marks - 39):

print("Passed, Good!")

5

6 of 13

if-else Statement

  • The block of statements after if is executed, if the condition is true/non-zero.

​

  • The statement after else is executed, if the condition is false/zero.

​

  • if (expression):
  • Block of statements
  • else:
  • Block of statements

​

marks = 50

if (marks >= 40):

print("Passed, Good!")

else:

print("Work harder!")

6

7 of 13

if-elif-else Statement

  • if (expression):
  • Block of statements
  • elif (expression):
  • Block of statements
  • else:
  • Block of statements

​

marks = 50

if (marks >= 40):

print("Passed, Good!")

elif (marks >= 30):

print(“Borderline Pass!")

else:

print("Work harder!")

7

8 of 13

Nested if-else

  • Indentation using spaces and tabs is critical for correlating a set of if statements with their else counterparts.

​

  • All if statements may not have the else counterpart.

​

​

8

if (exp1):

if (exp2):

stmta

else:

stmtb

​

if (exp1):

if (exp2):

stmta

else:

stmtb

9 of 13

Find the Largest Number

  • First compare two numbers to find the larger between them.

​

  • Then compare the larger of the first two numbers with the third number.

x = 4

y = 5

z = 6

​

if (x > y):

max = x

else:

max = y

​

if (max > z):

print(max)

else:

print(z)

9

10 of 13

Another Version: Find the Largest Number

  • Check if the first number is the largest

​

  • Else check if the second number is the largest

​

  • If none of the above conditions are true, the third number must be the largest.

​

if (x > y and x > z):

max = x

elif (y > x and y > z):

max = y

else:

max = z

​

print(max)

10

11 of 13

Yet Another Version

  • First compare two numbers to find the larger between them.

​

  • Then compare the larger of the first two numbers with the third number.

​

  • Identical to version 1?

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

12 of 13

Reading Material

  • Reference Book
    • Chapter 2.3

​

12

13 of 13

Additional example

  • Rounding up
  • Income tax slabs

​

COL100: Introduction to Computer Science (Semester I, 2024-2025)

13