成人论坛

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 of Computing 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=鈥漜ola鈥 OR drink=鈥漧emonade鈥
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=鈥漜ola鈥 OR drink=鈥漧emonade鈥
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 鈥淵ou 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 = 鈥淵鈥 OR answer = 鈥測鈥 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 鈥淪orry, you did not win鈥 TO DISPLAY
END IF