Operators

Operator let us perform calculations or modify variables. Here’s a list of some basic operators in C++.

Assignment: =

The assignment operator allows us to set a specific value to a variable.

int x;
x = 5; // here we set the value of the variable 'x' to 5

In the previous example, 5 (the right operand) was assigned to x (the left operand). The order of evaluation is from right to left (more on this in a later example)

Arithmetic Operators: *, /, %, +, -

Operator Meaning
* Multiplication
/ Division
% Modulo
+ Addition
- Subtraction

All these operators are binary operators, meaning they require two operands. For example:

5 * 3
1 + 1
6 / 2

The modulo operator (%) gives the remainder of diving the left operand by the right operand. The remaining operations are applied in the same way as regular mathematics.

Assigning Expressions

The value that we assign to a variable does not have to be a simple constant or other variable. We can actually assign full expressions which use arithmetic operators.

int x, y;
x = 5;
y = 3 + 2 * x;
// since '*' comes before '+' in operator precedence, y will be 13 not 25

Compound Assignments: *=, /=, %=, +=, -=

Suppose we had a variable x and we wanted to add some value to it, multiply it by some value or apply some operation and assign its result to x. Normally we would use x = x + y or x = x * y. We can instead do x += y or x *= y and it would do the same operation.

Expression Using Compound
x = x + y x += y
x = x - 5 x -= 5
x = x / y x /= y
price = price * (units + 1) price *= units + 1

Increment and Decrement: ++, --

If we wanted to add 1 to some variable x we saw that we can shorten it to x += 1, but when we are adding or subtracting 1, we have an even shorter way to write it using increment and decrement operators.


int x;
x = 0;
x++; // after this line is executed x is 1
x--; // after this line is executed x is 0

A subtle difference between ++ and += 1 is that ++ and -- can be written before (prefix) or after (suffix) a variable and each has its own meaning. We will explore the difference with an example:

int x, y;
x = 1;
y = ++x;

In this example y will be 2 because when ++ is before the variable we first apply the increment then use its value.

int x, y;
x = 1;
y = x++;

Here, y will be 1 because when ++ is after the variable we first use the value of the variable then apply the increment.

Relational and Comparison Operators: ==, !=, >, <, >=, <=

Similar to arithmetic operators, these are binary operators meaning they take two operands, but here we are comparing the operands and what we get in return is a boolean.

Reminder: Booleans are data types which store a value that is either true or false

Operator Description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
int x = 1, y = 2; 
bool same = (x==y); // since x and y are not equal (x==y) will be 'false'
bool x_less_than_y = (x < y); // since x < y the expression will be 'true'

Comparision is not only limited to variables, the left and right operands may be full expressions, for example:

int x = 5;
bool even = (x % 2 == 0);
// a number is even if it can be divided by 2 with no remainder
// in this case 'false' will be assigned to the variable 'even'

Logical Operators: !, &&, ||

The ! operator (NOT) is a unary operator meaning it has only one operand which is a boolean. If the operand’s value is true then this operation produces false otherwise it produces true.

bool a = (5 > 7);  // false
bool b = !a;       // true
bool c = !(7 > 5); // false

The && and || operators are operators which receive two boolean values as operands. The && operator (AND) produces true if both operands’ values are true and produces false otherwise.

bool a = (5 % 2 == 0); // false, 5 is odd
bool b = (3 > 2); // true
bool c = a && b; // false because a is false
bool d = (!c) && b; // true because (!c) is true and b is true

The || operator (OR) produces true if at least one of its operands’ values are true and produces false otherwise.

bool a = (5 < 2); // false
bool b = (4 % 2 == 0); // true, 4 is even
bool c = a && b; // false, because a is false
bool d = b || c; //  true, because b is true

A special and important property of the AND and OR operators is the short-circuit evaluation. Suppose we have two boolean expressions A and B, if we want to evaluate A && B and we know that A is false, then we dont need to evaluate B, because we know that A && B is false.

This is important in cases where evaluating the right operand modifies values, for example:

int x = 0;
bool b = (5 % 2 == 0) && (x++ == 0); 

Since 5 % 2 == 0 is false, the execution does not evaluate the expression (x++ == 0) which means that x never gets incremented. Similarly for the || operator if the first operand is true the evaluation of the second operand is skipped.

Conditional Ternary Operator: ?

This operator is a ternary operator meaning it accepts three operands: condition, expression_t and expression_f. The syntax is as follows:

condition ? expression_t : expression_f

If the condition provided evaluates to true then the statement produces expression_t, otherwise it produces expression_f. 

int x = 5;
int y = (x % 2 == 0 ? x : x + 1); // if x is even then y = x, otherwise y = x + 1
                                  // since 5 is odd then y = x + 1 = 6

If we had two integers a and b and we wanted the larger one, we can do the following:

int max = (a > b ? a : b);

Precendence of Operators

When an expression has multiple operators, C++ has a defined order in which it performs these operations. For example 5 + 7 % 2 evaluates to 6 because % happens before +. If instead we had (5 + 7) % 2 then the result would be 0.

From greatest to smallest priority, C++ operators are evaluated in the following order:

Level Precedence group Operator Description Grouping
1 Postfix (unary) ++ –
postfix increment / decrement Left-to-right
2 Prefix (unary) ++ –
!
prefix increment / decrement
logical NOT
Right-to-left
3 Arithmetic: scaling * / % multiply, divide, modulo Left-to-right
4 Arithmetic: addition + - addition, subtraction Left-to-right
5 Relational < > <= >= comparison operators Left-to-right
6 Equality ==
!=
equality / inequality Left-to-right
7 Conjunction && logical AND Left-to-right
8 Disjunction || logical OR Left-to-right
9 Assignment-level expressions = *= /= %= += -=
?:
assignment / compound assignment
conditional operator
Right-to-left