0% found this document useful (0 votes)
5 views

Discussion Assignment unit 3

Uploaded by

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

Discussion Assignment unit 3

Uploaded by

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

Chained vs.

Nested Conditionals

Both chained conditionals and nested conditionals are ways to handle multiple conditions in a

Python program. However, they differ in structure and usage:

Chained Conditional:

A chained conditional uses multiple if, elif, and else statements to evaluate different conditions

in sequence. It is linear, and is easy to read. The first condition that evaluates to True then the

remaining conditions are skipped.

Example of Chained Conditional:

age = 25

if age < 18:

print("You are a minor.")

elif 18 <= age < 65:

print("You are an adult.")

else:

print("You are a senior.")

Output Explanation:

If age < 18 is true, then it prints “You are a minor.” skips the rest. If the first condition is False

then it will check the second condition “Otherwise, it executes the else block and prints “You are

a senior."

Nested Conditional:

A nested conditional places one conditional statement inside another, forming a hierarchy of

decisions. Nested conditionals can become difficult to follow as the depth increases.

Example of Nested Conditional:


# Check if a person qualifies for a scholarship

age = 18

grade = 85

if age >= 18:

if grade >= 80:

print("Eligible for scholarship")

else:

print("Grade too low")

else:

print("Age too low")

Output Explanation:

The outer if checks the age condition. If true, the inner if checks the grade. Depending on both

conditions, the appropriate message is printed.

Strategy to Avoid Nested Conditionals

To avoid deep nesting, you can:

1. Use logical operators (and, or) to combine conditions into a single conditional.

2. Return early in functions or use break to exit loops if a condition is met.

3. Refactor code into separate functions for clarity.

Example of Nested Conditional to Single Conditional:

Nested Conditional:

age = 18

grade = 85

if age >= 18:


if grade >= 80:

print("Eligible for scholarship")

else:

print("Grade too low")

else:

print("Age too low")

Single Conditional Equivalent:

age = 18

grade = 85

if age >= 18 and grade >= 80:

print("Eligible for scholarship")

elif age < 18:

print("Age too low")

else:

print("Grade too low")

Explanation:

The nested structure is flattened by combining conditions with and. Additional conditions are

handled separately with elif, improving readability.

Discussion Question:

When would you prefer a nested conditional over a chained conditional, even though the latter is

easier to read? What factors influence your decision?


References

 Downey, A. B. (2015). Think Python: How to think like a computer scientist (2nd ed.).

O'Reilly Media.

 Matthes, E. (2019). Python Crash Course: A Hands-On, Project-Based Introduction to

Programming (2nd ed.)

You might also like