# Expressions and Arithmetic

#### Expressions and Arithmetic

This chapter uses the C++ numeric types introduced in Chapter 3 to build expressions and perform arithmetic. Some other important concepts are covered—user input, source formatting, comments, and dealing with errors.

#### Expressions

A literal value like 34 and a properly declared variable like x are examples of simple expressions. We can use operators to combine values and variables and form more complex expressions.

``````#include <iostream>
int main()
{
int value1, value2, sum;
std::cout << "Please enter two integer values: ";
std::cin >> value1 >> value2;
sum = value1 + value2;
std::cout << value1 << " + " << value2 << " = " << sum << '\n';
}``````
`` int value1, value2, sum; ``
``std::cout << "Please enter two integer values: "; ``

This is because we want the cursor to remain at the end of the printed line so when the user types in values they appear on the same line as the message prompting for the values.

#### The simple C++ arithmetic operators

• – subtraction
• * multiplication
• / division
• % modulus
``````int x = 3;
int y = -4;
int z = 0;
std::cout << -x << " " << -y << " " -z << '\n';
//output -3 4 0``````
``````int x = 2;
int y = 3;
int z = 4;
int k = x * y;
int f = z/x;
std::cout << k << "  " << f;
//output 6  2
``````

#### modulas

Integer division vs. integer modulus. Integer division produces the quotient, and modulus produces the remainder. In this example,25/3 is 8, and25%3 is——–

#### Mixed Type Expressions

Expressions may contain mixed elements; for example, the following program fragment

``````int x = 4;
double y = 10.2,
sum; sum = x + y;``````

As shown in Figure 4.2, the range of ints falls completely within the range of doubles; thus, any int value can represented by a double. The int 4 also can be expressed as the double 4.0. In fact, since the largest int on most systems is 2,147,483,647, the minimum 15 digits of double precision are more than adequate to represent all integers exactly. This means that any int value can be represented by adouble. The converse is nottrue, however. 2,200,000,000 can bere presented by a double butitis too big for the int type. We say that the double type is wider than the int type and that the int type is narrower than the double type.

``````double d1;
int i1 = 500;
1 = i1;
std::cout << "d1 = " << d1 << '\n';
output d1 = 500``````
``````#include <iostream>
int main()
{
double d = 2200000000.0;
int i = d;
std::cout << "d = " << d << ", i = " << i << '\n';
}``````

The printed values of i and d are not even close, nor can they be because it is impossible to represent the value 2,200,000,000 as an int on a system that uses 32-bit integers. When assigning a value of a wider type to a variable of a narrower type, the programmer must assume the responsibility to ensure that the actual value to be narrowed is indeed within the range of the narrower type. The compiler cannot ensure the safety of the assignment.

``````#include <iostream>
int main()
{
enum class Color { Red, Orange, Yellow, Green, Blue, Violet };
std::cout << static_cast<int>(Color::Orange) << " " << static_cast<int>
(Color::Green) << '\n';
}``````
``output 1 3``

#### Operator Precedence and Associativity

Precedence—when an expression contains two different kinds of operators, which should be applied ﬁrst?

Associativity–when an expression contains two operators with the same precedence, which should be applied ﬁrst?

To see how precedence works, consider the expression

``2 + 3 * 4``

Should it be interpreted as

``(2 + 3) * 4``

(that is, 20), or rather is

``````2 + (3 * 4)
``````

``````// Compute the average of the values
``````/* After the computation is completed the result is displayed. */