Relational and Boolean operators

<< Previous: Arithmetic Next: String handling >>

Relational operators

Relational operators are those operators that can be used to compare two pieces of data.

Relational Operator What it's used for
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
= or == Equal to
!= or <> Not equal to

An important note to remember is that it is always greater or less than and then equal to never equals first.

Logical operators

Logical operators are used to create more complex Boolean conditions by combining multiple Boolean conditions to create Boolean expressions. The three Boolean opeartors you need to know are:
- AND
- OR
- NOT

Logical AND Logical OR Logical NOT
Description Both conditions must be true for the expression to evaluate as true. If either condition is true, then the expression evaluates as true. If the condition is false, the expression is true and vice versa.
Example
Explanation Both enrolled=True and age>=18 must evaluate as True for this expression to be true. If either is False, then the expression evaluates as False. If either distance is greater than 5 or cost is less than 20, then the program outputs Delivery cost will be £5 even if the other condition that is part of the expression is False. The not reverses the condition i.e. The expression will evaluate as True and output Too old for youth club when age<18 evaluates as False.

Using logical operators can be a way to simplify expressions that would otherwise require nested selection as you can see in the example below.



Knowledge check


Questions:
Correct:

Question text


<< Previous: Arithmetic Next: String handling >>

© All materials created by and copyright S.Goff