5. Selection

Selection is one of the three basic control structures.
Selection provides alternatives or branching using ifelifelse.
Selection tests condition that evaluate to True or False.
The logical operators, and, or, and not increase the options for testing conditions.
Python - Pseudocode equivalents

Python

Pseudocode

=

if

IF ….THEN

elif

ELSEIF ….THEN

else

ELSE

“end of if”

ENDIF

or

OR

and

AND

not

NOT

True

TRUE

False

FALSE


5.1. If, else

if is used with a condition that results in True or False.
When the condition is True, the code indented below the if statement is executed.
If all conditions are False, else: can be used to execute other code.
score = 65
cut_off_score = 60
if score >= cut_off_score:
   print("Suitable standard.")
else:
   print("Do a retest.")
Pseudocode. The equivalent pseudocode is:
BEGIN
   score ← 65
   cut_off_score ← 60
   IF score >= cut_off_score THEN
      OUTPUT "Suitable standard."
   ELSE
      OUTPUT "Do a retest."
   ENDIF
END

Tasks

  1. In the code above, is the condition in the if statement True or False?

  2. Give a value for score in the code above such that the output is "Do a retest.".

  3. Assuming scores can only be integers from and including 0 to 100, how many different scores would result in the output of "Do a retest."?

  4. Assuming scores can only be integers from and including 0 to 100, how many different scores would result in the output of "Suitable standard."?

In the code above, is the condition in the if statement True or False?
if score >= cut_off_score becomes if 65 >= 60:
This evaluates to True
Give a value for score in the code above such that the output is "Do a retest.".
“Do a retest.” is in the False branch.
So the score is below 60. e.g. 59
Assuming scores can only be integers from and including 0 to 100, how many different scores would result in the output of "Do a retest."?
Required scores are from 0 to 59.
There are 60 scores.
Assuming scores can only be integers from and including 0 to 100, how many different scores would result in the output of "Suitable standard."?
Required scores are from 60 to 100.
There are 41 scores.

5.2. If, elif, else

Alternatives can be provided using elif with a condition.
Note that there must be a colon, :, at the end of each if, elif and else statement.
scoreA = 88
scoreB = 85
if scoreA > scoreB:
   print("A won.")
elif scoreB > scoreA:
   print("B won.")
else:
   print("A drew with B.")
Pseudocode. The equivalent pseudocode is:
BEGIN
   scoreA ← 88
   scoreB ← 85
   IF scoreA > scoreB THEN
      OUTPUT "A won."
   ELSEIF scoreB > scoreA THEN
      OUTPUT "B won."
   ELSE
      OUTPUT "A drew with B."
   ENDIF
END

Tasks

  1. In the code above, is the condition in the if statement True or False?

  2. Give a value for scoreB in the code above such that the output is "B won.".

  3. Give a value for scoreB in the code above such that the output is "A drew with B.".

In the code above, is the condition in the if statement True or False?
scoreA > scoreB is True since 88 > 85.
Give a value for scoreB in the code above such that the output is "B won.".
scoreB > scoreA so scoreB > 88. e.g. scoreB = 89
Give a value for scoreB in the code above such that the output is "A drew with B.".
scoreB == scoreA so scoreB = 88. e.g. scoreB = 88

Tasks

  1. Write python code for the following pseudocode for travelling to school.

    BEGIN
       is_raining ← TRUE
       IF is_raining THEN
          OUTPUT "Catch the bus."
       ELSE
          OUTPUT "Ride the bike."
       ENDIF
    END
    
is_raining = True
if is_raining:
   print("Catch the bus.")
else:
   print("Ride the bike.")

5.3. And, or, not

The and keyword is a logical operator used to combine conditional statements.
The return value will be True if all of the statements are True.
if is_raining and is_cold returns True if both are True.
If will return False if any of the statements are False.
if is_raining and is_cold returns False if either are False.
is_raining = True
is_cold = True
print("Good Morning.")
if is_raining and is_cold:
   print("Bring Umbrella and jacket.")
else:
   print("Umbrella and jacket are optional.")
Pseudocode. The equivalent pseudocode is:
BEGIN
   is_raining ← TRUE
   is_cold ← TRUE
   OUTPUT "Good Morning."
   IF is_raining AND is_cold THEN
      OUTPUT "Bring Umbrella and jacket."
   ELSE
      OUTPUT "Umbrella and jacket are optional."
   ENDIF
END

Tasks

  1. In the code above, is the condition in the if statement True or False?

  2. What is the expected output from the code above?

  3. Would changing is_raining to False result in a change in the output?

  4. Would changing is_raining to False and is_cold to False result in a change from the original output?

In the code above, is the condition in the if statement True or False?
is_raining AND is_cold is True since both is_raining``and ``is_cold are True.
What is the expected output from the code above?
“Bring Umbrella and jacket.”
Would changing is_raining to False result in a change in the output?
Yes, since the condition would evaluate to False instead of True.
Would changing is_raining to False and is_cold to False result in a change from the original output?
Yes, the out put would be: “Umbrella and jacket are optional.”

The or keyword is a logical operator used to combine conditional statements.
The return value will be True if one is True.
if is_raining or is_cold returns True if either is True.
If will return False if all of the statements are False.
if is_raining or is_cold returns False if both are False.
is_raining = False
is_cold = False
print("Good Morning.")
if is_raining or is_cold:
   print("Bring Umbrella or jacket or both.")
else:
   print("Wear a sun hat.")
Pseudocode. The equivalent pseudocode is:
BEGIN
   is_raining ← FALSE
   is_cold ← FALSE
   OUTPUT "Good Morning."
   IF is_raining OR is_cold THEN
      OUTPUT "Bring Umbrella or jacket or both."
   ELSE
      OUTPUT "Wear a sun hat."
   ENDIF
END

Tasks

  1. In the code above, is the condition in the if statement True or False?

  2. What is the expected output from the code above?

  3. Would changing is_raining to True result in a change in the output?

  4. Would changing is_raining to True and is_cold to True result in a change from the original output?

In the code above, is the condition in the if statement True or False?
is_raining OR is_cold is False since both is_raining``and ``is_cold are False.
What is the expected output from the code above?
“Wear a sun hat.”
Would changing is_raining to True result in a change in the output?
Yes, since the condition would evaluate to True instead of False.
Would changing is_raining to True and is_cold to True result in a change from the original output?
Yes, the output would be: “Bring Umbrella or jacket or both.”

The not keyword is a logical operator.
It changes True to False, and False to True.
is_raining = True
is_cold = False
print("Good Morning.")
if is_raining and is_cold:
   print("Bring Umbrella and jacket.")
elif is_raining and not(is_cold):
   print("Bring Umbrella.")
Pseudocode. The equivalent pseudocode is:
BEGIN
   is_raining ← TRUE
   is_cold ← FALSE
   OUTPUT "Good Morning."
   IF is_raining AND is_cold THEN
      OUTPUT "Bring Umbrella AND jacket."
   ELSEIF is_raining AND NOT (is_cold) THEN
      OUTPUT "Bring Umbrella."
   ENDIF
END

Tasks

  1. In the code above, is the condition in the ELSEIF statement True or False?

  2. What is the expected output from the code above?

  3. What change would be needed in the variable assignments to result in the output being "Bring Umbrella AND jacket."?

In the code above, is the condition in the ELSEIF statement True or False?
is_raining AND NOT (is_cold) is TRUE since both is_raining``and ``NOT (is_cold) are True.
What is the expected output from the code above?
“Bring Umbrella.”
What change would be needed in the variable assignments to result in the output being "Bring Umbrella AND jacket."?
Change is_cold FALSE to is_cold TRUE.

Tasks

  1. Write python code for the following pseudocode on preparing for the weather.

    BEGIN
       is_raining ← False
       is_cold ← False
       OUTPUT "Good Morning."
       IF is_raining AND is_cold THEN
          OUTPUT "Bring Umbrella and jacket."
       ELSEIF is_raining AND NOT(is_cold) THEN
          OUTPUT "Bring Umbrella."
       ELSEIF NOT(is_raining) AND is_cold THEN
          OUTPUT "Bring Jacket."
       ELSE
          OUTPUT "Wear a sun hat."
       ENDIF
    END
    
is_raining = False
is_cold = False
print("Good Morning.")
if is_raining and is_cold:
   print("Bring Umbrella and jacket.")
elif is_raining and not(is_cold):
   print("Bring Umbrella.")
elif not(is_raining) and is_cold:
   print("Bring Jacket.")
else:
   print("Wear a sun hat.")

5.4. Nested if

Nesting is the inclusion of other if statements within if statements.
Both the if and the elif below have a nested if and else that are used when their condition is true.
scoreA = 38
scoreB = 35
if scoreA > scoreB:
   if scoreA - scoreB > 14:
      print("A won easily.")
   else:
      print("A won.")
elif scoreB > scoreA:
   if scoreB - scoreA > 14:
      print("B won easily.")
   else:
      print("B won.")
else:
   print("A drew with B.")
Pseudocode. The equivalent pseudocode is:
BEGIN
   scoreA ←38
   scoreB ← 35
   IF scoreA > scoreB THEN
      IF scoreA - scoreB > 14 THEN
            OUTPUT "A won easily."
      ELSE
            OUTPUT "A won."
      ENDIF
   ELSEIF scoreB > scoreA THEN
      IF scoreB - scoreA > 14 THEN
            OUTPUT "B won easily."
      ELSE
            OUTPUT "B won."
      ENDIF
   ELSE
      OUTPUT "A drew with B."
   ENDIF
END

Tasks

  1. Using python code, add the variables teamA and teamB and make up team names for them. Modify the code to print the team name instead of ‘A’ or ‘B’. Hint: To join text use a plus symbol. e.g (myteam + “ my text”)

  2. Using pseudocode, modify the code to include the changes in Q1, and to print the winning margins. Use str(number) to convert numbers to text for joining with other text. e.g “The Chiefs won by 3.”

teamA = "Chiefs"
teamB = "Eagles"
scoreA = 38
scoreB = 35

if scoreA > scoreB:
   if scoreA - scoreB > 14:
      print("The " + teamA + " won easily.")
   else:
      print("The " + teamA + " won.")
elif scoreB > scoreA:
   if scoreB - scoreA > 14:
      print("The " + teamB + " won easily.")
   else:
      print("The " + teamB + " won.")
else:
   print("The " + teamA + " drew with the " + teamB + ".")
BEGIN
   teamA ← "Chiefs"
   teamB ← "Eagles"
   scoreA ←38
   scoreB ← 35
   IF scoreA > scoreB THEN
      margin = str(scoreA - scoreB)
      IF scoreA - scoreB > 14 THEN
         OUTPUT ("The " + teamA + " won easily by " + margin + ".")
      ELSE
         OUTPUT ("The " + teamA + " won by " + margin + ".")
      ENDIF
   ELSEIF scoreB > scoreA THEN
      margin = str(scoreB - scoreA)
      IF scoreB - scoreA > 14 THEN
         OUTPUT ("The " + teamB + " won easily by " + margin + ".")
      ELSE
         OUTPUT ("The " + teamB + " won by " + margin + ".")
      ENDIF
   ELSE
      OUTPUT ("The " + teamA + " drew with the " + teamB + ".")
   ENDIF
END