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 | | Both sides of the AND operator must be true for the IF statement to succeed |
| OR | | Either side of the OR operator must be true for the IF statement to succeed |
| NOT | | 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 |
|
| Both sides of the AND operator must be true for the IF statement to succeed |
| OR |
|
| Either side of the OR operator must be true for the IF statement to succeed |
| NOT |
|
| 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 THENSEND “You are a teenager” TO DISPLAYEND IFHere 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” THENSET found TO TRUEEND IFHere 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 THENSEND “Sorry, you did not win” TO DISPLAYEND IF