Basic Operators in C++

C++ comes with several basic operators.1 Below is a table briefly presenting them. The last few rows following an #include are operators (functions) that come from library. They only work if the library is included (e.g., #include <cmath>).

operatordescription
a + baddition
a - bsubtraction
a * bmultiplication
a / bdivision
a mod bmodulus
a++increment by 1
a--decrement by 1
a & &blogical AND
a || blogical OR
!alogical not
a == ba equal to b?
a != ba not equal to b?
a < ba less than b?
a > ba greater than b?
a <= ba greater than or equal to b`?
a >= ba greater than or equal to b`?
a ? b : cIf a, then b, otherwise c

Operators fall into three categories: Unary operators are operators that take just one value as input. For example, the increment operator, ++, increments by 1, and takes just one value as input. Binary operators are operators that take exactly two values as inputs. For example, the division operator \ is a binary operator, since it takes two values. Ternary operators are operators that take exactly three values as inputs. In C++, the ternary operator ?: is an operator takes three expressions as inputs and outputs one expression. For example:

#include <iostream>
	using namespace std;

	int main(int argc, char *argv[]) {
		int x = 1;
		int y = 2;
		int z = 3;
		int w;
		x == 1 ? w = y : w = z;
		cout << "w = " << w << endl;
	}
w = 2

The ternary operator x == 1 ? w = y : w = z; says, if x is equal to 1, then w = y. Otherwise, w = z. Since x == 1 is true, w = y. Thus, w = 2.

Footnotes

  1. An operator is a symbol that takes one or more values as inputs, and outputs another value after performing a particular operation. The values we pass as inputs are called the operands.