Parentheses and Precedence


If the precedence and associativity rules do not correspond with your calculations, you can apply the parentheses operator ( <Numerical_expression> ) to control the order in which each operator is applied in an arithmetic expression.

Syntax Box 7.3 The Parentheses Operator

 Parenthesized_expression ::=                  ( <Numerical_expression> ) 

Parentheses in C# are used in the same way as in algebra and arithmetic to accomplish this task. Because of their higher precedence level (as shown in Table 7.1), they have first priority over any precedence rules set for most other operators. The next example illustrates.

We want to construct a statement that finds the average time two people spend exercising every day. Our first attempt,


does not achieve our goal because exercisePerson2 is first divided by 2, and then exercisePerson1 is added. Fortunately, we can change the order by which the two operators are applied by using the parentheses operator


which assigns the correct value to averageExerciseTime.

You might need to form more complex expressions involving nested parentheses. It is often possible to enhance the readability of these expressions by introducing a temporary variable. Let's look at an example.

A sports drink company, Unlimited Energy Limited, is considering putting a new sports drink on the market in San Francisco and New York. They want to make an estimate of the total amount of time the people of San Francisco and New York combined spend exercising. The only currently available data are in the form of exercisePerson1 and exercisePerson2, as shown earlier. The junior marketing consultant has figured out that by finding the average exercise time and multiplying this amount by the total number of people in San Francisco and New York, it is possible to get an (albeit very inaccurate) estimate of the total exercise time of all people in the two cities combined. The result of this calculation will be called totalExerciseTime. A first attempt to calculate this size is as follows:


However, the expression on the right of the assignment operator = is somewhat complicated; it must be broken down from inside out to be understood. First, the average exercise time of the two persons is calculated in ((exercisePerson1 + exercisePerson2) / 2) and then (numPeopleSanFrancisco + numPeopleNewYork) is calculated. Finally, these two results are multiplied together. So the statement is correct and fulfills our goal but has one problem; it is hard to understand for a programmer reading through the code, and so it is difficult to maintain. Let's try to improve the readability while getting the same result:


By separating the complex statement into three statements and adding the temporary variable totalPeople in the second line, the calculation has become easier to understand without altering the final value assigned to totalExerciseTime.



To increase the clarity and the self-documenting ability of source code, avoid deeply nested expressions. Instead, use temporary variables and more statements if necessary.

Temporary variables do take up a bit of computer memory and can slow down the code slightly. In most cases, though, the slowdown is unnoticeable and insignificant. Furthermore, computer memory and speed is increasing daily, whereas computer programmers and their time have become extremely sought after and highly valued as scarce resources. In fact, too scarce to spend time looking at unclear code.



Clarify your code by using parentheses, even if they are not needed. The following statement might do exactly what you want but when a programmer reads through your code, will he or she remember all the precedence and associativity rules?


By applying a few parentheses, there is no doubt how the expression is calculated:



C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: