2.1.4 - Logical decisions and control flow
An algorithm's route depends on the conditions it tests. You will locate decisions, translate requirements into precise Boolean conditions, and follow how true and false outcomes change the program's state and output.
Find where the next action depends on data
A decision point chooses a route according to a condition that is true or false. It appears in selection and in loop control, not only in a displayed if statement.
A ticket service must decide whether a requested quantity is valid, whether enough seats remain, and whether the customer qualifies for a discount. These are different decisions: each has its own condition and consequence. Sequence alone, such as adding an already determined fee to a total, does not choose between routes.
For a permitted positive quantity, this static pseudocode selects either a successful booking or an unavailable message:
if quantity <= seats then
seats = seats - quantity
print("Booked")
else
print("Unavailable")
endif
print(str(seats))
The test chooses exactly one branch. With quantity 3 and seats 5, it subtracts 3, prints Booked, then prints 2. With quantity 6 and seats 5, the subtraction is skipped, it prints Unavailable, then prints 5. The statement after endif is reached from either branch. A skipped assignment does not reset its variable.
A loop also tests whether another repetition should occur. A condition-controlled while checks before its body, so an initially false condition skips the body entirely. Identify the decision, the data it uses, and each possible next action before tracing values.
Translate the whole rule into a logical condition
A logical condition must express the complete rule, including limits and overriding restrictions. Define each proposition before combining it.
A secure workshop allows entry when a person has a valid pass or staff approval, provided the workshop is not closed. Let P mean valid pass, A mean staff approval, and C mean closed. The condition is (P OR A) AND NOT C. Inclusive OR permits either or both authorisations. The restriction NOT C applies to their whole combined demand, so closure prevents both routes.
| P | A | C | P OR A | Entry allowed? |
|---|---|---|---|---|
| False | False | False | False | False |
| False | True | False | True | True |
| True | False | False | True | True |
| True | True | False | True | True |
| True | False | True | True | False |
This is a deliberately selected set of cases, not a complete three-input truth table. It demonstrates no authorisation, each authorisation alone, both, and an overriding closure. Full equivalence testing would examine all eight combinations.
Numeric boundaries need equally precise conditions. An accepted integer age from 16 to 21 inclusive needs (age >= 16) AND (age <= 21). Using OR accepts values outside the range: age 10 satisfies age <= 21. For this interval, the rejection condition is (age < 16) OR (age > 21). State whether endpoints are accepted before choosing < versus <=.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account