Performing and Displaying C++ Calculations
Nesting Parentheses
With complex expressions, you might need to nest parentheses (to put a subexpression inside another subexpression). For example:
4*(3+(6/2))
This complicated expression should be read from the inside out. First, divide 6 by 2. Then add 3 to that result. Then multiply the updated result by 4.
Because C++ doesn't require an expression to be written on a single line, you can make this more understandable by using the parentheses as if they were braces:
4* ( 3+ (6/2) )
This makes it easier to be sure that every opening parenthesis has a closing parenthesis (thus avoiding a common programming mistake).
Expressions in cout
iostream can display the result of a complex expression just as easily as it can display a simple string literal. In the example program, an expression is placed in the cout statement, and the number 6 is written on the screen as simply as "Hi there!" was in Lesson 2.
This works because the compiler causes the expression to be evaluated before providing its result to the cout statement.