Title: Arithmetic Operators
Course: CPS 120: Introduction to Computer Science ~ Master (Aug/2015) OLD > Section: Learning Units

Visual C++ Arithmetic Operators:

- Have to follow the order of operations.
- Perform arithmetic on one or two numbers.
- Numbers may be integers or real numbers.

Operation | Operator
Addition | +
Subtraction | -
Multiplication | *
Division | /
Remainder after division | %
Exponentiation | pow(<base>, <exponent>)
Increment By 1 | ++
Decrement By 1 | --

Visual C++ Division Operator (/)

- An operand is the expression on either side of an operator.
- For the division operator, an operand may be an integer or real number expression.
- The division operator gives a real number result if either operand is real.  Otherwise, it gives an integer result (truncated).

Division Operator Result Table
Left-side operand type | Right-side operand type | Result type
integer | integer | truncated integer
integer | real | real
real | integer | real
real | real | real

Division Operator Examples

Code:

// Division operator in cout statements
cout << "Dividing with integers and reals "
<< "in cout statements" << endl;
cout << "1  / 2  is " << 1  / 2  << endl;
cout << "1  / 2. is " << 1  / 2. << endl;
cout << "1. / 2  is " << 1. / 2  << endl;
cout << "1. / 2. is " << 1. / 2. << endl;
cout << "1  / (double) 2  is "
<< 1 / (double) 2 << endl;

Output:

Dividing with integers and reals in cout statements
1  / 2  is 0
1  / 2. is 0.5
1. / 2  is 0.5
1. / 2. is 0.5
1  / (double) 2  is 0.5

Activity:

- Watch the Khan Academy Introduction to Order of Operations video (9:40 minutes).