CALCULATING IN C++

This is where you actually start doing something with the data that you enter. You know how to carry out simple input and output; now, you are beginning the bit in the middle, the “processing” part of a C++ program. Almost all of the computational aspects of C++ are fairly intuitive, so you should slice through this like a hot knife through butter.

The Assignment Statement

You have already seen examples of the assignment statement. A typical assignment statement looks like this:

whole = part1 + part2 + part3;

The assignment statement enables you to calculate the value of an expression that appears on the right-hand side of the equals sign, in this case, the sum of part1, part2, and part3, and store the result in the variable specified on the left-hand side, in this case, the variable with the name whole. In this statement, the whole is exactly the sum of its parts, and no more.

NOTE Note how the statement, as always, ends with a semicolon.

You can also write repeated assignments, such as:

a = b = 2;

This is equivalent to assigning the value 2 to b and then assigning the value of b to a, so both variables will end up storing the value 2.

Arithmetic Operations

The basic arithmetic operators are addition, subtraction, multiplication, and division, represented by the symbols +, -, *, and /, respectively. Generally, these operate as you would expect, with the exception of division, which has a slight aberration when working with integer variables or constants, ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.