Difficulty: Beginner
Programming Language: C++

Article Index

Value Operators

While logical operators are able to return a boolean value to indicate the true/false nature of the operation it's performing, value operators are able to return any type of value, depending on the types used for the operands and the operation being performed. Typically value operators are associated with simple mathematical functions such as addition and subtraction.

A note with C++ operators:

Some types may be automatically converted to other compatible types in order for the operation to be carried out. This allows some mixing of the operand types so for example we may add an integer and a double together without having to inform the compiler of an explicit conversion ourselves. It's very important to remember this as it can lead to bugs programs if an operation has performed automatic type conversion and then the result may is not as expected.


Assignment =

The assignment operator is one of a few special kind of operators in the way that it behaves. Unlike the operators you've seen so far and the value operators further down in this list, the assignment operator does not simply take the result of an operation performed on the required operands. The assignment operator accepts two operands, the left hand operand must be a physical memory location such as a programming variable. The right hand operator may be a variable, a literal value, or a temporary value such as the result calling of other operators.

The assignment operator takes the value of the right side operand and copies it into the physical memory location represented by the left side operator.

// Create 2 int types variables.
int var1;
int var2;

var1 = 8; // Use the assignment operator to assign the literal 8 to var1.
var2 = var1; // Use the assignment operator to assign the value of var1 to var2.
var1 = 3 + 4; // Use the assignment operator to assign the result of an addition to var1.

Addition + 
expression1 + expression2;

 Returns the result of an addition of the two provided operators.

std::cout << ( 5.0 + 2.34 ) << "\n";    // Prints  "7.34" to the terminal
std::cout << ( 3.2 + 3.2  ) << "\n";    // Prints  "6.4"  to the terminal
std::cout << ( 2.9 + 8.35 ) << "\n";    // Prints "11.25" to the terminal​

Subtraction -
expression1 - expression2;

 Returns the result of the right side operator subtracted from the left side operator.

std::cout << ( 5.0 - 2.34 ) << "\n";    // Prints  "2.66" to the terminal
std::cout << ( 3.2 - 3.2  ) << "\n";    // Prints  "0.0"  to the terminal
std::cout << ( 2.9 - 8.35 ) << "\n";    // Prints "-5.45" to the terminal​

Multiplication *
expression1 * expression2;

 Returns the result of a multiplication of the two provided operands.

std::cout << ( 5.0 * 2.34 ) << "\n";    // Prints "11.7"   to the terminal
std::cout << ( 3.2 * 3.2  ) << "\n";    // Prints "15.21"  to the terminal
std::cout << ( 2.9 * 8.35 ) << "\n";    // Prints "24.215" to the terminal​

Division /
expression1 / expression2;

Returns the results of the operand on the left side of the operator, divided by the operand on the right.

The type of data used with a division is important, as floating point values will work as expected but integer values will return only whole number results. For example 10 / 3 is 3 with a remainder of 1, so the result when using integers is 3. When using floats or doubles 10.0 / 3.0 will result in 3.3333...

std::cout << (  8   / 2   ) << "\n";    // Prints "4" to the terminal
std::cout << ( 10   / 3   ) << "\n";    // Prints "3" to the terminal (integer division)
std::cout << ( 10.0 / 3.0 ) << "\n";    // Prints "3.333333" to the terminal (floating point division)

Modulus %
expression1 % expression2;

Performs an integer division and returns the remainder of the division.

In C++ the modulus operator can only be used with integer type operands.

std::cout << (  9 % 3 ) << "\n";    // Prints "0" to the terminal
std::cout << ( 10 % 3 ) << "\n";    // Prints "1" to the terminal
std::cout << ( 11 % 3 ) << "\n";    // Prints "2" to the terminal​
std::cout << ( 12 % 3 ) << "\n";    // Prints "0" to the terminal