When designing programs, there are often points where a condition needs to be tested in order to make a decision. Conditions are formed using Boolean logic.

Part of

A Boolean expression is one that has a Boolean value. The Boolean value is either True or False.

Boolean expressions are represented using algebra.

Consider these statements:

- 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 variables. 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.

In Boolean logic, 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. |

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. |

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