So far you’ve seen how you can substitute any Boolean value with a Boolean expression, for example replacing 'true' with '!true', which evaluates to false, or replacing '!true' with '!!true' which evaluates back to true again. Similarly, any numerical value can be replaced with a numerical expression. Practically all programming languages have the ability to express basic arithmetic, and more complex arithmetic can be achieved if necessary by building on the basic arithmetic. An arithmetical expression looks like:
firstNumber operator secondNumber
Where firstNumber and secondNumber can be any numerical value we like, and operator could be:
| firstNumber | operator | secondNumber | Result | Description |
|---|---|---|---|---|
| 2 | + | 2 | 4 | Adds the numbers |
| 6 | - | 2 | 4 | Subtracts the numbers |
So basically we just use the standard + and – symbols on the keyboard between any two numbers and the compiler works out the result for us.
The – symbol also serves an extra purpose, it can negate a number. When we place it immediately before a number, we’re telling the compiler that we want the negative version of that number, for example:
| 6 | means positive six |
| -6 | means negative six |
Compile and run the given program and note the result of the arithmetical expression
Change the expression to 10 – 6 and note the result
Change the expression to -6 + 4 and note the result
Try adding or subtracting any numbers you like