Exercise - Advanced If Statements (Slides)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Programmatic thinking

Exercise: Advanced if statements


Please do not copy without permission. © ExploreAI 2023.
Programmatic thinking

Exercise context
Let’s sayNations
United we want to create a program to monitor and We want to create a program to monitor and improve
improve access
Sustainable to clean water
Development Goalin6a rural community. access to clean water in rural communities.

Clean water and sanitation The program needs to consider the presence and
Ensure availability and sustainable management of concentration of certain microbial and chemical
water and sanitation for all. contaminants in water sources to determine whether
action is required to ensure that it is a safe drinking
water source.
Contamination of drinking water is a significant cause
of various diseases which greatly impact human
health.

The majority of these drinking water-related health


problems are the result of microbial contamination.
However, chemical contamination may also cause
serious health concerns.

2
Programmatic thinking

Exercise overview

General safety standards Presence of E. coli

Create the flowchart and pseudocode that Create the flowchart and pseudocode that
represents the following general standards on represents the assessment of the priority of the
drinking water quality. required action based on the presence of E. coli.

We consider the presence of two microbial We consider an E. coli classification and a sanitary
contaminants and the concentration limits of two inspection score called the ROC
chemical contaminants to determine whether action is (Risk-Of-Contamination) score to determine the level
required to ensure that the water source is safe for of risk posed by the combination of the scores, and
human consumption. thus, the resulting priority of the action required.

Instructions | Flowchart solution | Pseudocode solution Instructions | Pseudocode solution | Flowchart solution

3
Programmatic thinking

General safety standards


Create the flowchart and pseudocode that represents the following general standards on drinking
water quality.

We consider two microbial and two chemical We can rewrite the contents of the table:
contaminants to determine whether action is required
If coliforms or E. coli are present or the pH is lower
to ensure that the water source is safe for human
than 6.5 or higher than 8.5 or ammonia is greater than
consumption. The acceptable concentration limits are
0.5 mg/l, then action is required.
listed below.
Breaking it down further:
Contaminant Unit Concentration limit*
Action is required when:
Coliforms None Absent
1. Coliforms == True OR
E. coli None Absent 2. E. coli == True OR
3. pH < 6.5 OR
pH None 6.5 to 8.5
4. pH > 8.5 OR
Ammonia mg/l 0.5 5. Ammonia > 0.5
*Based on Kenya’s Water Service Regulatory Board (WASREB) guidelines on drinking water quality. We assume the concentration limits are inclusive. 4
Programmatic thinking

General safety standards


If any one of the contaminants is present or above the recommended concentration limits, the output will be
“Action required” – this represents the OR logic in our flow of control.

Do you recognise the conditional statement


Start represented in this flowchart?

False False False False


Coliforms E. coli pH < 6.5 pH > 8.5 Ammonia > 0.5

True True True True True

“Action
required”

Flowchart solution #1
5
Programmatic thinking

General safety standards


Since the statement didn’t specify that we have to output anything when none of the conditions is met, we don’t
have to include the False output on the last condition.

However, we could infer that the opposite will be “No action


Start the output, namely “No action required”. required”

False

False False False False


Coliforms E. coli pH < 6.5 pH > 8.5 Ammonia > 0.5

True True True True True

“Action
required”

Flowchart solution #1
6
Programmatic thinking

General safety standards


Pseudocode solution #1 Since the if statement already assumes we’re checking for a
True result, it’s more concise to write the condition without the
Start boolean expression.
If Coliforms then
- Output = “Action required”
Else if E. coli then We can add the alternative output, “No action required” by
- Output = “Action required” adding the following before we end the if statement:
Else if pH < 6.5 then
- Output = “Action required” Else
Else if pH > 8.5 then - Output = “No action required”
- Output = “Action required” End if
Else if Ammonia > 0.5 then
- Output = “Action required”
End if
End Since the outcome of any one of the conditions is the same, we can also combine all the
conditions using the OR boolean operator:

If Coliforms OR E. Coli OR pH < 6.5 OR pH > 8.5 OR Ammonia > 0.5 then
- Output = “Action required”
End if

7
Programmatic thinking

Presence of E. coli
Create the flowchart and pseudocode that represents the assessment of the priority of the
required action based on the presence of E. coli.

In most cases, we would like to know how to prioritise The output of the flowchart and pseudocode is one of
the required action based on the concentration limit four priorities:
or classification of a specific contaminant.
● _No action_
We consider an E. coli classification and a sanitary ● _Low action priority_
inspection score called the ROC ● _Higher action priority_
(Risk-Of-Contamination) score to determine the level ● _Urgent action_
of risk posed by the combination of the scores, and
thus, the resulting priority of the action required.

8
Programmatic thinking

Presence of E. coli
To set the appropriate conditions, we will need to group the outcomes based on the E. coli classification (A to E)
and the ROC score (0 to 9) based on the table* below.

For example, we can already see that when the classification is D or E, the output is very high risk: urgent action,
regardless of the ROC score.

Risk-Of-Contamination score (ROC)

0 1 2 3 4 5 6 7 8 9
E. coli classification (Class)

No
Low action priority Higher action priority Urgent action
action

*Lloyd, B.J. and Bartram, J.K., 1991. Surveillance solutions to microbiological problems in water quality control in developing countries. Water Science and Technology, 24(2), pp.61-75. 9
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four _No action_
outputs. (Class == A AND ROC == 0)

_Low action priority_


Note how we use AND boolean operators to consider (Class == A AND ROC > 0 AND ROC < 4)
the combined requirements of the class and ROC. We OR (Class == B AND ROC >= 0 AND ROC < 4)
use the OR boolean operators to account for the
different combinations based on each class. _Higher action priority_
(Class == A AND ROC > 3 AND ROC < 7)
OR (Class == B AND ROC > 3 AND ROC < 7)
OR (Class == C AND ROC >= 0 AND ROC < 7)

_Urgent action_
(Class == A AND ROC > 6 AND ROC <= 9)
OR (Class == B AND ROC > 6 AND ROC <= 9)
OR (Class == C AND ROC > 6 AND ROC <= 9)
OR (Class == D AND ROC >= 0 AND ROC <= 9)
OR (Class == E AND ROC >= 0 AND ROC <= 9)
10
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four _No action_
outputs. (Class == A AND ROC == 0)

_Low action priority_


Since we know that the ROC ranges from 0 to 9 (Class == A AND ROC > 0 AND ROC < 4)
(inclusive) we don’t have to explicitly consider these OR (Class == B AND ROC >= 0 AND ROC < 4)
bounds.
_Higher action priority_
For example, if we say all ROCs smaller than 4 then we (Class == A AND ROC > 3 AND ROC < 7)
assume that it includes all ROCs that are greater than OR (Class == B AND ROC > 3 AND ROC < 7)
or equal to zero. OR (Class == C AND ROC >= 0 AND ROC < 7)

_Urgent action_
(Class == A AND ROC > 6 AND ROC <= 9)
OR (Class == B AND ROC > 6 AND ROC <= 9)
OR (Class == C AND ROC > 6 AND ROC <= 9)
OR (Class == D AND ROC >= 0 AND ROC <= 9)
OR (Class == E AND ROC >= 0 AND ROC <= 9)
11
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four No action
outputs. (Class == A AND ROC == 0)

Low action priority


We also see that specific ROC scores indicate a divide (Class == A AND ROC > 0 AND ROC < 4)
between the priorities. OR (Class == B AND ROC < 4)

0 1 2 3 4 5 6 7 8 9 Higher action priority


(Class == A AND ROC > 3 AND ROC < 7)
OR (Class == B AND ROC > 3 AND ROC < 7)
OR (Class == C AND ROC < 7)

Urgent action
(Class == A AND ROC > 6)
OR (Class == B AND ROC > 6)
OR (Class == C AND ROC > 6)
OR (Class == D)
OR (Class == E)
12
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four No action
outputs. (Class == A AND ROC == 0)

Low action priority


We also see that specific ROC scores indicate a divide (Class == A AND ROC > 0 AND ROC < 4)
between the priorities. OR (Class == B AND ROC < 4)
ROC >= 4
0 1 2 3 4 5 6 7 8 9 Higher action priority
(Class == A AND ROC > 3 AND ROC < 7)
However, we can also represent > 3 as >= 4, and OR (Class == B AND ROC > 3 AND ROC < 7)
similarly > 6 as >= 7. We can use three ROC scores OR (Class == C AND ROC < 7)
as decisions, rather than five.
Urgent action
ROC >= 7
0 1 2 3 4 5 6 7 8 9 (Class == A AND ROC > 6)
OR (Class == B AND ROC > 6)
OR (Class == C AND ROC > 6)
OR (Class == D)
OR (Class == E)
13
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four No action
outputs. (Class == A AND ROC == 0)

Low action priority


We can write down an if-else-if ladder where the
(Class == A AND ROC > 0 AND ROC < 4)
conditions are only related to the classification (Class).
OR (Class == B AND ROC < 4)

Higher action priority


If Class == A then (Class == A AND ROC >= 4 AND ROC < 7)
- what needs to happen if the condition is true OR (Class == B AND ROC >= 4 AND ROC < 7)
Else if Class == B then OR (Class == C AND ROC < 7)
- what needs to happen if the condition is true
Else if Class == C then Urgent action
- what needs to happen if the condition is true (Class == A AND ROC >= 7)
Else OR (Class == B AND ROC >= 7)
- what needs to happen if none of the conditions is true OR (Class == C AND ROC >= 7)
(i.e. Class == D OR Class == E) OR (Class == D)
End if OR (Class == E)
14
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four Let’s only consider the ROC conditions and related
outputs. output when Class == A.

If Class == A then No action


- If ROC == 0 then (Class == A AND ROC == 0)
- - Output = “No action”
- Else if ROC < 4 then Low action priority
- - Output = “Low action priority” (Class == A AND ROC > 0 AND ROC < 4)
- Else if ROC < 7 then Higher action priority
- - Output = “Higher action priority” (Class == A AND ROC >= 4 AND ROC < 7)
- Else
- - Output = “Urgent action” Urgent action
- End if (Class == A AND ROC >= 7)
Else if Class == B then
Note, we don’t need to test for ROC > 0 because when ROC != 0
- ...
in the first condition then ROC can only be greater than zero
when the program gets to ROC < 4.

15
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four Let’s only consider the ROC conditions and related
outputs. output when Class == B.

- ... No action
Else if Class == B then
- If ROC < 4 then Low action priority
- - Output = “Low action priority” (Class == B AND ROC < 4)
- Else if ROC < 7 then
- - Output = “Higher action priority” Higher action priority
- Else (Class == B AND ROC >= 4 AND ROC < 7)
- - Output = “Urgent action” Urgent action
- End if (Class == B AND ROC >= 7)
Else if Class == C then
- ...

Note, we also don’t need to test for ROC >= 4 because when ROC is not smaller than 4 in the first condition then ROC can only be
greater than four when the program gets to ROC < 7.

16
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four Let’s only consider the ROC conditions and related
outputs. output when Class == C.

- ... No action
Else if Class == C then
- If ROC < 7 then Low action priority
- - Output = “Higher action priority”
- Else Higher action priority
- - Output = “Urgent action” (Class == C AND ROC < 7)
- End if Urgent action
Else (Class == C AND ROC >= 7)
- ...

Note, we also don’t have to specifically state the last condition (ROC >= 7) because when ROC is not smaller than 7 it can only be
greater than or equal to 7. We simply use an else statement.

17
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four Let’s only consider the ROC conditions and related
outputs. output when Class == D or Class == E.

- ... No action
Else
- Output = “Urgent action” Low action priority
End if
Higher action priority

Urgent action
(Class == D)
(Class == E)

When the class is neither A, B, nor C, then the flow of control reverts to the else of the if-else-if ladder. Since the output is “Urgent
action” when Class == D or Class == E for any ROC, we do not require a nested if statement.

18
Programmatic thinking

Presence of E. coli
Write down all the conditions for each of the four We see that when the class is NOT A nor B, the output
outputs. can only be “Higher action priority” or “Urgent action”.

Furthermore, when the class is NOT C AND ROC is


Consider the below pseudocode for the end of our
greater than or equal to 7 (NOT smaller than 7) the
flow of control:
output can only be “Urgent action”.

- ... In other words, we can combine the two conditions


Else if Class == C then with the AND boolean operator to test both conditions
- If ROC < 7 then at the same time.
- - Output = _Higher action priority_
- Else - ...
- - Output = _Urgent action_ Else if Class == C AND ROC < 7 then
- End if - Output = _Higher action priority_
Else Else
- Output = _Urgent action_ - Output = _Urgent action_
End if End if

19
Programmatic thinking

Presence of E. coli
If Class == A then
Combining the various parts we’ve constructed per class, we notice
Initial pseudocode solution #0

- If ROC == 0 then
- - Output = “No action” that, except for when ROC == 0, Class A and B are the same.
- Else if ROC < 4 then
- - Output = “Low action priority” ...
- Else if ROC < 7 then
Else if Class == A OR Class == B then
- - Output = “Higher action priority”
- If ROC < 4 then
- Else
- - Output = “Low action priority”
- - Output = “Urgent action”
- End if - Else if ROC < 7 then
Else if Class == B then - - Output = “Higher action priority”
- If ROC < 4 then - Else
- - Output = “Low action priority” - - Output = “Urgent action”
- Else if ROC < 7 then - End if
- - Output = “Higher action priority” ...
- Else
- - Output = “Urgent action”
- End if We use the OR boolean operator to combine the same conditions
Else if Class == C AND ROC < 7 then together.
- Output = “Higher action priority”
Else
- Output = “Urgent action”
However, we need to account for the exception, i.e. when ROC ==
End if 0, to set the output to “No action”.
20
Programmatic thinking

Presence of E. coli
Alternative pseudocode solution #1 We account for the exception by adding a preceding
condition that simultaneously accounts for both the
If Class == A AND ROC == 0 then class and ROC conditions using the AND boolean
- Output = “No action” operator.
Else if Class == A OR Class == B then
- If ROC < 4 then
- - Output = “Low action priority” Note, the (Class == A AND ROC == 0) condition
- Else if ROC < 7 then must precede the (Class == A OR Class == B)
- - Output = “Higher action priority” condition.
- Else
- - Output = “Urgent action”
- End if
Else if Class == C AND ROC < 7 then Consider the following scenario:
- Output = “Higher action priority” Input Class as A and ROC as 0 and compare the
Else outputs of (Class == A AND ROC == 0) and
- Output = “Urgent action” (Class == A OR Class == B). Are any of the
End if conditions true for this input?
21
Programmatic thinking

Presence of E. coli
We can identify other similarities and exceptions: There are many different ways to find the same output
● Only when ROC < 4 and the class is either A or B, to the same problem using pseudocode:
is the output “Low action priority”.
● When the class is A, B, or C, ROC >= 7 indicates an ● Grouping by ROC for the if-else-if ladder in either
output of “Urgent action”. However, when the class ascending or descending order.
is D or E, regardless of the ROC, the output is also ● Mixing ROC and class conditions in the if-else-if
“Urgent action”. ladder.
● Setting conditions for classes in the if-else-if
Alternative pseudocode solution #2 ladder from E to A rather than A to E.
● Using nested if statements for a shorted if-else-if
If Class == A AND ROC == 0 then
ladder.
- Output = “No action”
Else if (Class == A OR Class == B) AND ROC < 4 then
- Output = “Low action priority” Shorter and more efficient pseudocode often leads to
Else if Class == D OR Class == E OR ROC >= 7 then better and more efficient implementations of the
- Output = “Urgent action” solution when coding in a specific programming
Else
language.
- Output = “Higher action priority”
End if

22
Programmatic thinking

Presence of E. coli
_No action_
We’ve already mapped out the logic of control
(Class == A AND ROC == 0)
required while writing the pseudocode. See if you
can use these groupings to create a flowchart that _Low action priority_
includes only one instance of each output. (Class == A AND ROC > 0 AND ROC < 4)
OR (Class == B AND ROC < 4)

Class vs ROC _Higher action priority_


(Class == A AND ROC >= 4 AND ROC < 7)
0 1 2 3 4 5 6 7 8 9
OR (Class == B AND ROC >= 4 AND ROC < 7)
E OR (Class == C AND ROC < 7)
D
_Urgent action_
C
(Class == A AND ROC >= 7)
B OR (Class == B AND ROC >= 7)
A OR (Class == C AND ROC >= 7)
OR (Class == D)
Low action priority Higher action priority Urgent action
OR (Class == E)
23
Programmatic thinking

Presence of E. coli
False False False
Start Class == A Class == B Class == C

True True
“Urgent action”
True

True False False False


“No
ROC == 0 ROC < 4 ROC < 7
action”

True True

“Low action “Higher action


priority” priority”
Flowchart solution #1

Write down the pseudocode of this flowchart using both if-else-if ladders and nested if statements.
Does the pseudocode seem more or less efficient than our last pseudocode iteration?
24
Programmatic thinking

Presence of E. coli
Flowchart to the alternative pseudocode solution #2

Start

False (Class == A OR False Class == D OR False


Class == A
AND ROC == 0 Class == B) AND Class == E OR
ROC < 4 ROC >= 7

True True True

“No “Low action “Higher action


“Urgent action”
action” priority” priority”

Although our second pseudocode solution was the most efficient, the flowchart of this pseudocode is unintuitive.
This shows that the most efficient pseudocode doesn’t necessarily translate into the most intuitive flowchart, and
vice versa.

25

You might also like