Boolean expressions
Boolean expressionA combination of symbols representing a value. Expressions are used in programming languages and applications. are represented using algebra.
Consider these statementThe smallest element of a programming language which expresses an action to be carried out.:
- 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 variableA memory location within a computer program where values are stored.. 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 Boolean logicA form of logical algebra which works only with two values, true or false., each statement is a comparison, and each comparison gives a Boolean value 鈥 True or False.
When x = 5 and y = 10 then:
Statement | Expression | Boolean value |
y > x | y is greater than x | True. When x is 5 and y is 10, then y is greater than x. |
x < y | x is less than y | True. When x is 5 and y is 10, then x is less than y. |
x = y | x equals y | False. When x is 5 and y is 10, then x does not equal y. |
x<>y | x does not equal y | True. When x is 5 and y is 10, then x does not equal y. |
Statement | y > x |
---|---|
Expression | y is greater than x |
Boolean value | True. When x is 5 and y is 10, then y is greater than x. |
Statement | x < y |
---|---|
Expression | x is less than y |
Boolean value | True. When x is 5 and y is 10, then x is less than y. |
Statement | x = y |
---|---|
Expression | x equals y |
Boolean value | False. When x is 5 and y is 10, then x does not equal y. |
Statement | x<>y |
---|---|
Expression | x does not equal y |
Boolean value | True. 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:
Statement | Expression | Boolean value |
y > x | y is greater than x | False. When x is 5 and y is 5, then y is not greater than x. |
x < y | x is less than y | False. When x is 5 and y is 5, then x is not less than y. |
x = y | x equals y | True. When x is 5 and y is 5, then x is equal to y. |
x<>y | x does not equal y | False. When x is 5 and y is 5, then x is equal to y. |
Statement | y > x |
---|---|
Expression | y is greater than x |
Boolean value | False. When x is 5 and y is 5, then y is not greater than x. |
Statement | x < y |
---|---|
Expression | x is less than y |
Boolean value | False. When x is 5 and y is 5, then x is not less than y. |
Statement | x = y |
---|---|
Expression | x equals y |
Boolean value | True. When x is 5 and y is 5, then x is equal to y. |
Statement | x<>y |
---|---|
Expression | x does not equal y |
Boolean value | False. 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.