0% found this document useful (0 votes)
35 views25 pages

Exercise_ Advanced if statements [Slides][Re-brand]

The document outlines an exercise on programmatic thinking focused on monitoring and improving access to clean water in rural communities. It details the creation of flowcharts and pseudocode to assess drinking water quality based on microbial and chemical contaminants, specifically addressing E. coli presence and its associated risk levels. The exercise emphasizes the importance of determining necessary actions based on contamination levels to ensure safe drinking water.

Uploaded by

Wakanda Citizen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views25 pages

Exercise_ Advanced if statements [Slides][Re-brand]

The document outlines an exercise on programmatic thinking focused on monitoring and improving access to clean water in rural communities. It details the creation of flowcharts and pseudocode to assess drinking water quality based on microbial and chemical contaminants, specifically addressing E. coli presence and its associated risk levels. The exercise emphasizes the importance of determining necessary actions based on contamination levels to ensure safe drinking water.

Uploaded by

Wakanda Citizen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Programmatic thinking

Exercise: Advanced if statements


Please do not copy without permission. © ALX 2024.
Programmatic thinking

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.

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
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

General safety standards


Create the flowchart and pseudocode that represent 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
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

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


Start statement 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 “No action


Start be 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
Start without the 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 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.

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 (Class == A AND ROC > 0 AND ROC < 4)
consider the combined requirements of the class OR (Class == B AND ROC >= 0 AND ROC < 4)
and ROC. We use the OR boolean operators to
account for the different combinations based on _Higher action priority_
each class. (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 OR (Class == B AND ROC >= 0 AND ROC < 4)
these bounds.
_Higher action priority_
For example, if we say all ROCs smaller than 4 then (Class == A AND ROC > 3 AND ROC < 7)
we assume that it includes all ROCs that are OR (Class == B AND ROC > 3 AND ROC < 7)
greater than 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 (Class == A AND ROC > 0 AND ROC < 4)
divide 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 (Class == A AND ROC > 0 AND ROC < 4)
divide 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
OR (Class == B AND ROC < 4)
(Class).
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 greater


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

In other words, we can combine the two conditions with


- ...
the AND boolean operator to test both conditions at the
Else if Class == C then same time.
- If ROC < 7 then
- - 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
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)

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)
No
OR (Class == D)
Low action priority Higher action priority Urgent action
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