Home > Articles > Programming > C/C++

Performing and Displaying C++ Calculations

  • Print
  • + Share This
Mark Cashman and Jesse Liberty demonstrate how to use literals and how to declare and define variables and constants; how to assign values to these variables and constants, and what values and names are valid; and how to get input from the user.
This chapter is from the book

Nesting Parentheses

With complex expressions, you might need to nest parentheses (to put a subexpression inside another subexpression). For example:


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:


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.

  • + Share This
  • 🔖 Save To Your Account