Operators
Operators are a basic feature of the C++ language, which, similar to operators in mathematics, allow the production of a result of computation from one, or a combination of two variables. There are roughly 60 operators in C++; fortunately, you only need to know a few of them to get started writing programs.
Contents |
Assignment operator
The assignment operator (=
) assigns a value to a variable. For example:
b = 14;
This statement assigns the integer value 14 to the variable b
. The assignment operator always works from right to left. For example:
c = b;
Here the variable c
is assigned the value that is held in b
. The value stored in b
is left unmodified, whereas the previous value of c
is lost.
Below is an example that shows how to use assignment operator to swap two values:
Output:
x: 10; y: 20 x: 20; y: 10
Arithmetic operators
The arithmetic operators compute a new result from two given values. The following arithmetic operators are available in C++:
- addition. Example: a + b. Here the sum of
a
andb
is calculated.
- subtraction. Example: a - b. Here
b
is subtracted froma
.
- multiplication. Example: a * b. Here the multiplication of
a
andb
is performed.
- division. Example: a / b. Here
a
is divided byb
. For integer types, non-integer results are rounded towards zero (truncated).
- modulo. Example: a % b. Here the remainder of the division of
a
byb
is calculated.
The below example demonstrates use of the arithmetic operators:
#include <iostream> int main() { int a = 14; int b = 5; int c = 12; std::cout << "a: " << a << "; b: " << b << "; c: " << c << "\n"; std::cout << "a+b: " << (a + b) << "; b+c: " << (b + c) << "; a+c: " << (a + c) << "\n"; std::cout << "a-b: " << (a - b) << "; b-c: " << (b - c) << "; a-c: " << (a - c) << "\n"; std::cout << "a*b: " << (a * b) << "; b*c: " << (b * c) << "; a*c: " << (a * c) << "\n"; std::cout << "a/b: " << (a / b) << "; b/c: " << (b / c) << "; a/c: " << (a / c) << "\n"; std::cout << "a%b: " << (a % b) << "; b%c: " << (b % c) << "; a%c: " << (a % c) << "\n"; return 0; }
Output:
a: 14; b: 5; c: 12 a+b: 19; b+c: 17; a+c: 26 a-b: 9; b-c: -7; a-c: 2 a*b: 70; b*c: 60; a*c: 168 a/b: 2; b/c: 0; a/c: 1 a%b: 4; b%c: 5; a%c: 2
This section is incomplete Reason: Operator precedence |
Bitwise logical operators
This section is incomplete |
Bitwise shift operators
This section is incomplete |
Compound assignment operators
This section is incomplete |
Increment and decrement operators
This section is incomplete |
Logical operators
This section is incomplete |
Comparison operators
Comparison operators allow to determine the relation of two different values. The following operators are available in C++:
- less-than. Example: a < b: Yields true if the value on the left (
a
) side is less (smaller) than the value on the right side (b
).
- less-or-equal. Example: a <= b: Yields true if the value of
a
is less than or equal to the value ofb
.
- equality. Example: a == b: Yields true if the value of
a
is equal to the value ofb
.
- greater-or-equal. Example: a >= b: Yields true if the value of
a
is greater than or equal to the value ofb
.
- greater-than. Example: a > b: Yields true if the value of
a
side is greater than the value ofb
.
- non-equality. Example: a != b: Yields true if the value of
a
is not equal to the value ofb
.
The following example demonstrates use of the comparison operators:
Output:
14 is greater than 5
Be aware that comparison of floating point values may sometimes yield unexpected results due to rounding effects. Therefore, it is recommended to always use <= or >= comparison with floating point values, instead of checking for equality with ==.
Other operators
There are several other operators that we will learn about later.
This section is incomplete |