4.2 Simple Expressions

I l @ ve RuBoard

Computers can do more than just print strings. They can also perform calculations. Expressions are used to specify simple computations . C++ has the five simple operators listed in Table 4-1.

Table 4-1. Simple operators

Operator

Meaning

*

Multiply

/

Divide

+

Add

-

Subtract

%

Modulus (remainder after division)

Multiply (*), divide (/), and modulus (%) have precedence over addition (+) and subtraction (-). Parentheses may be used to group terms. Thus, the following expression yields 12:

 (1 + 2) * 4 

The next expression yields 9:

 1 + 2 * 4 

The program in Example 4-1 computes the value of the expression (1 + 2) * 4 .

Example 4-1. Simple expression
 int main(  ) {     (1 + 2) * 4;     return(0); } 

Although we calculate the answer, we don't do anything with it. (This program will generate a " null effect" warning when it's compiled, which indicates that there is a correctly written, but useless, statement in the program.)

If we were constructing a building, think about how confused a worker would be if we said, "Take your wheelbarrow and go back and forth between the truck and the building site."

"Do you want me to carry bricks in the wheelbarrow?"

"No. Just go back and forth."

You need to output the results of your calculations.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net