Parentheses ( ( and ) )
What are they?
Parentheses, otherwise known as round brackets, are a special type of operator not specifically used for arithmetic, but nonetheless very useful for arithmetic calculation and all other types of expression.
Parentheses can be used to encapsulate smaller sub-statements that are evaluated before any operations outside the parentheses are applied to them. Similarly, parentheses can be used to enclose function arguments.
What is the Syntax?
Within an expression, each time an opening parenthesis – ( – is written, everything that follows will be considered as its contents until an equivalent closing parenthesis – ) – is also included. For example:
AverageWeight := ( Weight1 + Weight2 + Weight3 ) / 3;
Here, the contents of the parentheses are Weight1 + Weight2 + Weight3, which indicates that these three numbers will be added together before their sum is divided by 3 to find the average. The pairing between the opening and closing brackets is clear.
What if we needed to include more brackets? Sticking with the example above, we discover that Weight1 and Weight2 were measured in pounds, while Weight3 was measured in kilograms. We want to convert Weight3 to pounds as well, so we modify our expression as follows:
AverageWeight := ( Weight1 + Weight2 + ( Weight3 * 2.2 ) ) / 3;
This time, the expression is calculated in this order:
- Weight3 is multiplied by 2.2 to find its equivalent value in pounds.
- Weight1 is added to Weight2, and the total is added to the calculated value of Weight3 in pounds.
- The combined total of the three weights, in pounds, is divided by 3 to give the AverageWeight.
The second open parenthesis occurs before the first one was closed, so although the second set of brackets is self-contained, their entire contents are also contained as part of the first set of brackets. Having one set of brackets inside another is known as Nesting.
Why might I want to use this?
The most common use for parentheses in expressions is to group together related information so that it is evaluated in the order you want, rather than the order dictated by precedence (see Arithmetic Operator Precedence for more on operator precedence).
Besides overriding operator precedence, grouping the constituent parts of your expression together with parentheses creates easily distinguishable segments to the expression, which makes your code easier to read and modify later.
For example, consider the following expression:
4 + 3 * 9 – 6 / 12 ^ 2 * 6 % 4
The expression engine knows how to evaluate this expression, and the order in which the operator pairings should be evaluated due to their operator precedence. To a typical person reading the expression, however, it just looks like a line of numbers joined together with arithmetic symbols. Whether you want to override the operator precedence or simply tidy your code up, applying parentheses to the previous expression can produce far more readable alternatives, like the one shown below:
( ( (4 + 3) * 9 ) – ( 6 / 12 ) ^ 2 ) * ( 6 % 4 )
Parentheses are capable of completely changing the meaning of your code. All it would take is a single modification to change the following:
( 6 / 12 ) ^ 2
Into a different calculation with a different value:
6 / ( 12 ^ 2 );
The value of the first code snippet is the fraction 1/4, while the second is 1/24. This will absolutely make all the difference in your expression results, so choose your parentheses placement carefully!
<< Previous: Exponent ( ^ ) | Next: Arithmetic Operator Precedence >>