Module 7: Conditionals | CMSC 105 Elementary Programming - Fall 2024

Module 7: Conditionals

Note: Create a text file called module7.txt where you will store you answers to exercise questions. The questions that are not related to changing code. You will submit this file on Blackboard along with your code.

Objectives

By the end of this module you will be able to:

A simple example

Consider this program:

x = 5
y = 4

if x > y:
    print('Hey, x is bigger')

print("OK, we're done")

Exercise: 1 In my_ifexample.py, type up the above and examine the output. Then, just below the print with 'Hey' (and indented 4 spaces) add another print to print anything. Then, change the value of y to 6 and report the output in module7.txt. Submit the program with these modifications.


Let’s explain:

If the condition is True then the code that’s indented below the if-statement executes.


Exercise: 2 Consider this program:

s = 7

s = s + 9

if s < 15:
    print('Less than 15')

print('Done')

Try to mentally execute (trace its execution in your mind) and predict the output before typing it up in my_ifexample2.py to confirm.


if-else

Think of else as if’s occasional partner.

Consider this example:

x = 5
y = 4

if x > y:
    print('Hey, x is bigger')
else:
    print('Who said x is bigger?')
    print('In fact, y is bigger')

print("OK, we're done")

Exercise 3: Type up the above in my_ifexample3.py and examine the output. Then, change the value of y to 6. What is the output? Change y to 5. What is the output? Report these in your module7.txt file.


Let’s point out:

if-elif-else

Consider this variation:

x = 5
y = 5

if x > y:
    print('Hey, x is bigger')
elif y > x:
    print('Who said x is bigger?')
    print('In fact, y is bigger')
else:
    print('Actually, they are equal')

print("OK, we're done")


Exercise 4: Type up the above in my_ifexample4.py and examine the output. Then, try y = 6 and y = 4. Report results in your module7.txt.


Let’s explain:

One can have as many elif sections as one would like, for example:

x = 3

if x == 1:
    print('one')
elif x == 2:
    print('two')
elif x == 3:
    print('three')
elif x == 4:
    print('four')
else:
    print('big')

Think of the whole thing as a giant if-statement:

x = 3

# This if statement has an if, three elif's, and one else
# Execution of the if begins with the if condition.
if x == 1:
    print('one')
elif x == 2:
    print('two')
elif x == 3:
    print('three')
elif x == 4:
    print('four')
else:
    print('big')


# After navigating a path through the various parts of the giant
# if-statement, execution continues here    

In the above case, when x is 3, the execution path through the giant if-statement is:

x = 3

if x == 1:
    print('one')
elif x == 2:
    print('two')
elif x == 3: # This second elif succeeds
    print('three')
elif x == 4:
    print('four')
else:
    print('big')


# After the 2nd elif block, execution continues after the else    

Exercise 5: Type up the above in my_ifexample5.py. Then, try each of x = 1, x = 2, x = 4, x = 5. Explain the execution pathways (similar to the examples above) for each case in your module7.txt.


Consider this program:

x = 5
y = 4
z = 3

if x > y:
    print('Hey, x is bigger')

if x > z:
    print('x is bigger than z')
    print('So, x must be the largest')

Exercise 6: Type up the above in my_ifexample6.py. Try y = 6. Explain why it does not work. Then try to alter the program without changing the print-statements so that it works in all cases for possible values of x, y, and z. That is, whichever of the above print-statements gets printed correctly reflects the values of x, y and z.


Nested conditionals

Consider this program:

a = 3
b = 4
c = 5

if a < b:
    if a < c:
        print('a is the smallest')
    else:
        print('a is not the smallest')

print('Done')

This is an example of a nested conditional (nested if):

Consider this variation:

a = 3
b = 4
c = 5

if a < b:
    if a < c:
        print('a is the smallest')
    else:
        print('a is not the smallest')
    print('We know a is less than b')
else:
    print('We know a is not less than b')

print('Done')

Exercise 7: Type up the above in my_nestedif.py. Trace the flow of execution for the following three cases: (1) when a = 3, b = 4, c = 5; (2) when a = 3, b = 4, c = 2; (3) when a = 6, b = 4, c = 5.


Exercise 8: In my_smallest_of_three.py, modify the above program so that it prints out, appropriately, one of “a is the smallest”, “b is the smallest” or “c is the smallest”, depending on the actual values of a, b, and c. Try different values of these variables to make sure your program is working correctly.


Note:

Combining conditions

Consider this program:

x = 5
y = 5
z = 5

if x == y and y == z:
    print('All three are equal')

Note:

Let’s go back to finding the smallest of three numbers using conditionals:

a = 3
b = 4
c = 5

# Fill in code here ... 

Exercise 9: In my_smallest_of_three2.py, fill in code to identify which of the three variables has the smallest value, depending on the actual values of a, b, and c. Use if-statements with multiple clauses. Try different values of these variables to make sure your program is working correctly.


As the counterpart to the and operator, there is the or operator:

a = -2.718

if (a <= 0) or (a >= 1):
    print('a is not between 0 and 1')


Exercise 10: Type up the above in my_boolean2.py. Then try a = 0.5.


Note:

In this case, both sub-conditions are satisfied, and so the whole if-condition is satisfied, which means the print will execute.

In this case, the second comparison fails, and the print won’t occur.

Next, let’s look at the NOT operator (written with !):

x = 5
y = 6
z = 7

if (x != y) and (x != z):
    print('x is different from y and from z')

Here, read != as “not equals”.


Exercise 11: Type up the above in my_boolean3.py, then change z to be 6 (same as y). What do you observe?


One can combine any number of and’s, for example:

x = 5
y = 6
z = 7

if (x != y) and (x != z) and (y != z):
    print('x, y, z are all different')

The difference between != and not:

The not operator


Exercise 12: Suppose integer variables a, b, c, d, e have values a = 1, b = 1, c = 3, d = 4, e = 5. Consider the following three expressions:

( (a <= b) and (c+d > e) and (d > 1) )
( (a > c) or ( (c+1 < e) and (c-b > a) ) )
not ( (b == d-c) and (a > b) or (c < d) )

Try to evaluate each expression by hand. Then, in my_boolean4.py, write up each of these in an if-statement to see if the result matches with your hand-evaluation.