CS1101 Discussion Assignment Unit 3 - The difference between a chained conditional and a nested conditional

Questions

Describe the difference between a chained conditional and a nested conditional. Give your own example of each. Do not copy examples from the textbook.

Deeply nested conditionals can become difficult to read. Describe a strategy for avoiding nested conditionals. Give your own example of a nested conditional that can be modified to become a single conditional, and show the equivalent single conditional. Do not copy the example from the textbook.

1. Difference between a chained conditional and a nested conditional

A chained conditional is “a conditional statement with a series of alternative branches” ( Downey, 2015, p.47);

A nested conditional is “a conditional statement that appears in one of the branches of another conditional statement” (Downey, 2015, p.47);

The difference is that a chained conditional only has one conditional statement. In comparison, a nested conditional have more than one conditional statement, and one of these conditional statements would appear in one of the branches of another conditional statement.

2. An example of chained conditional

1
2
3
4
5
6
7
8
if grades >= 98:
print("Perfect! the letter grade is an A+.")
elif grades >= 93:
print("Excellent, the letter grade is an A.")
elif grades >= 90:
print("Good, the letter grade is an A-.")
else:
print("Holy smokes, the A went to outer space.")

3. An example of nested conditional

1
2
3
4
5
6
7
8
9
10
if grades >= 98:
print("Perfect! the letter grade is an A+.")
else:
if grades >= 93:
print("Excellent, the letter grade is an A.")
else:
if grades >= 90:
print("Good, the letter grade is an A-")
else:
print("Holy smokes, the A went to outer space.")

4. A strategy for avoiding nested conditionals

We can use a combination of boolean expressions and logical operators to avoid nested conditionals. A boolean expression is “an expression whose value is either True or False” (Downey, 2015, p.47); A logical operator is “one of the operators that combines boolean expressions: and, or, and not” (Downey, 2015, p.47). We can connect boolean expressions with logical operators, thereby simplifying nested conditions into a single condition.

5. An example of simplifying nested conditions into a single condition

As we know, if our score is greater than or equal to 98 and less than or equal to 100, our letter grade will be an A+ (UoPeople, 2021).

We can use the code with a nested conditional to express this information. The code with a nested conditional is as follows:

1
2
3
4
5
if grades <= 100:
if grades >= 98:
print("Perfect! the letter grade is an A+.")
else:
print("Holy smokes, the A+ went to outer space.")

Now, we can use a logical operator (and) to combine the boolean expression grades <= 100 with grades >= 98, thereby simplifying nested conditions into a single condition: grades <= 100 and grades >= 98. The code with the equivalent single conditional is as follows:

1
2
3
4
if grades <= 100 and grades >= 98:
print("Perfect! the letter grade is an A+.")
else:
print("Holy smokes, the A+ went to outer space.")

Furthermore, we can simplify the above single conditional into a more concise one: 98 <= grades <= 100. The code with the equivalent single conditional is as follows:

1
2
3
4
if 98 <= grades <= 100:
print("Perfect! the letter grade is an A+")
else:
print("Holy smokes, the A+ went to outer space.")

Word count: 535

References

1.Downey, A. (2015). Think Python. Green Tea Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf

2.University of the People. (2021). Grading Scale.
https://my.uopeople.edu/mod/page/view.php?id=259138

​


This is the ending, thanks for reading.