Implementation: Computational constructsConditional statements and logical operators

Programs use computational constructs such as loops and predefined functions to break programs up into sections. This makes programs more compact, easier to write and easier to maintain.

Part ofComputing ScienceSoftware design and development

Complex conditional statements and logical operators

Selection statements can be simple, where there is only one condition, eg: age > 18

More complex selection statements are where there is more than one condition, eg: age > 18 AND age < 25.

There are three logical operators that can be used as part of selection statements.

AND
IF age >18 AND age <25
Both sides of the AND operator must be true for the IF statement to succeed
OR
IF drink=”cola” OR drink=”lemonade”
Either side of the OR operator must be true for the IF statement to succeed
NOT
IF NOT (score = 0)
If the score is equal to 0, the section in brackets will be true. NOT will invert (reverse) this – the result will be false, so the IF statement will not succeed.
AND
IF age >18 AND age <25
Both sides of the AND operator must be true for the IF statement to succeed
OR
IF drink=”cola” OR drink=”lemonade”
Either side of the OR operator must be true for the IF statement to succeed
NOT
IF NOT (score = 0)
If the score is equal to 0, the section in brackets will be true. NOT will invert (reverse) this – the result will be false, so the IF statement will not succeed.

Here is an example of AND being used as part of a complex condition. When AND is used both conditions must be met.

IF age ≥13 AND age ≤19 THEN
SEND “You are a teenager” TO DISPLAY
END IF

Here is an example of OR being used as part of a complex condition. When OR is used only one of the conditions must be met.

If answer = “Y” OR answer = “y” THEN
SET found TO TRUE
END IF

Here is an example of NOT being used as part of simple condition. NOT is used to invert the result of a condition. For example, if the condition was 'If found = TRUE' the use of NOT would mean that the selection statement was only going to proceed if found was not true.

IF NOT found = TRUE THEN
SEND “Sorry, you did not win” TO DISPLAY
END IF