鶹Լ

Boolean expressions

Boolean are represented using algebra.

Consider these :

  • 5 < 10
  • x < 10
  • x < y

Each of these statements is a Boolean expression in the form of algebra. The only difference between them is that the first expression uses numbers and the second and third use . If we give x the value 5 and y the value 10, then each statement is identical.

Each statement is also a comparison. The statements compare the first value with the second. In this case we are saying that 5 is less than 10.

Boolean values

In , each statement is a comparison, and each comparison gives a Boolean value – True or False.

When x = 5 and y = 10 then:

StatementExpressionBoolean value
y > xy is greater than xTrue. When x is 5 and y is 10, then y is greater than x.
x < yx is less than yTrue. When x is 5 and y is 10, then x is less than y.
x = yx equals yFalse. When x is 5 and y is 10, then x does not equal y.
x<>yx does not equal yTrue. When x is 5 and y is 10, then x does not equal y.
Statementy > x
Expressiony is greater than x
Boolean valueTrue. When x is 5 and y is 10, then y is greater than x.
Statementx < y
Expressionx is less than y
Boolean valueTrue. When x is 5 and y is 10, then x is less than y.
Statementx = y
Expressionx equals y
Boolean valueFalse. When x is 5 and y is 10, then x does not equal y.
Statementx<>y
Expressionx does not equal y
Boolean valueTrue. When x is 5 and y is 10, then x does not equal y.

When x = 5 and y = 5, we get a different set of Boolean values:

StatementExpressionBoolean value
y > xy is greater than xFalse. When x is 5 and y is 5, then y is not greater than x.
x < yx is less than yFalse. When x is 5 and y is 5, then x is not less than y.
x = yx equals yTrue. When x is 5 and y is 5, then x is equal to y.
x<>yx does not equal yFalse. When x is 5 and y is 5, then x is equal to y.
Statementy > x
Expressiony is greater than x
Boolean valueFalse. When x is 5 and y is 5, then y is not greater than x.
Statementx < y
Expressionx is less than y
Boolean valueFalse. When x is 5 and y is 5, then x is not less than y.
Statementx = y
Expressionx equals y
Boolean valueTrue. When x is 5 and y is 5, then x is equal to y.
Statementx<>y
Expressionx does not equal y
Boolean valueFalse. When x is 5 and y is 5, then x is equal to y.

Each Boolean expression gives a result that we can use in selection and iteration.