Exercise_ Advanced if statements [Slides][Re-brand]
Exercise_ Advanced if statements [Slides][Re-brand]
Exercise context
Let’s sayNations
United we want to create a program to monitor We want to create a program to monitor and improve
and improve access
Sustainable to clean
Development water
Goal 6 in a rural access to clean water in rural communities.
community.
Clean water and sanitation The program needs to consider the presence and
Ensure availability and sustainable management concentration of certain microbial and chemical
of 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.
2
Programmatic thinking
Exercise overview
Create the flowchart and pseudocode that Create the flowchart and pseudocode that
represent the following general standards on represent 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 (Risk-Of-Contamination) score to determine the
action is required to ensure that the water source is level of risk posed by the combination of the scores,
safe for human consumption. and thus, the resulting priority of the action
required.
Instructions | Flowchart solution | Pseudocode solution Instructions | Pseudocode solution | Flowchart solution
3
Programmatic thinking
We consider two microbial and two chemical We can rewrite the contents of the table:
contaminants to determine whether action is
If coliforms or E. coli are present or the pH is lower
required to ensure that the water source is safe for
than 6.5 or higher than 8.5 or ammonia is greater
human consumption. The acceptable
than 0.5 mg/l, then action is required.
concentration limits are listed below.
Breaking it down further:
Contaminant Unit Concentration limit*
None Absent
Action is required when:
Coliforms
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
“Action
required”
Flowchart solution #1
5
Programmatic thinking
False
“Action
required”
Flowchart solution #1
6
Programmatic thinking
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 represent 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 The output of the flowchart and pseudocode is one
prioritize the required action based on the of four priorities:
concentration limit or classification of a specific
contaminant. ● _No action_
● _Low action priority_
We consider an E. coli classification and a sanitary ● _Higher action priority_
inspection score called the ROC ● _Urgent action_
(Risk-Of-Contamination) score to determine the
level 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.
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)
_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)
_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)
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)
Presence of E. coli
Write down all the conditions for each of the four No action
outputs. (Class == A AND ROC == 0)
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.
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”.
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
Else if Class == C AND ROC < 7 then conditions 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
If Class == A AND ROC == 0 then for both the class and ROC conditions using the
- Output = “No action” AND boolean 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 (Class
- Output = “Urgent action” == A OR Class == B). Are any of the conditions
End if 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
● Only when ROC < 4 and the class is either A or B, output to the same problem using pseudocode:
is the output “Low action priority”.
● When the class is A, B, or C, ROC >= 7 indicates ● Grouping by ROC for the if-else-if ladder in
an output of “Urgent action”. However, when the either ascending or descending order.
class is D or E, regardless of the ROC, the output ● Mixing ROC and class conditions in the if-else-if
is also “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 Class == A AND ROC == 0 then
if-else-if 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
Else if Class == D OR Class == E OR ROC >= 7 then to 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 _Low action priority_
that includes only one instance of each output. (Class == A AND ROC > 0 AND ROC < 4)
OR (Class == B AND ROC < 4)
Presence of E. coli
False False False
Start Class == A Class == B Class == C
True True
“Urgent action”
True
True True
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
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