Home Segments Index Top Previous Next

64: Mainline

Of course, you can always deploy parentheses around subexpressions whenever the C++ compiler's interpretation of the entire expression is not the interpretation you want:

6 + 3 * 2       // Value is 12, rather than 18 
(6 + 3) * 2     // Value is 18, rather than 12 

You can also use parentheses to make your intentions clearer. In the following, for example, the parentheses are not required, but many programmers insert them anyway, just to make the meaning of the expression absolutely clear:

6 + 3 * 2       // Value is clearly 12 
6 + (3 * 2)     // Value is even more clearly 12 

Inserting such parentheses is a good idea, especially when you are working with less familiar operators, such as the output operator, or when you are working with large expressions.