Operators
- Description
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment and Decrement Operators
- Ternary Operator
Description
Operators are used to perform operations on variables and values. Most operators are binary, meaning they take two operands. For most applications, operaters are used on variables with integral numeric types, however some exceptions exist when a custom type overloads the use of an operator.
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and values. The following table lists the arithmetic operators:
Remember that the result of an operation depends on the types of the operands. For example, the result of 5 / 2
is 2
, but the result of 5.0 / 2
is 2.5
. In the first example, both operands are integers, so the result is an integer. In the second example, one operand is a floating-point number, so the result is a floating-point number.
+
- Addition-
- Subtraction*
- Multiplication/
- Division%
- Modulus (remainder)- This operator is only used with integers. It returns the remainder of the division of the two operands.
- For use with floating-point numbers, use the
std::fmod(lhs, rhs)
function.
Relational Operators
Relational operators are used to compare two operands. The following table lists the relational operators:
==
- Equal to!=
- Not equal to>
- Greater than<
- Less than>=
- Greater than or equal to<=
- Less than or equal to
Logical Operators
Logical operators are used to combine two or more conditions. The following table lists the logical operators:
&&
- Logical AND||
- Logical OR!
- Logical NOT
Bitwise Operators
Bitwise operators are used to perform bitwise operations on variables and values. The following table lists the bitwise operators:
&
- Bitwise AND|
- Bitwise OR^
- Bitwise XOR~
- Bitwise NOT<<
- Bitwise left shift>>
- Bitwise right shift
Assignment Operators
Assignment operators are used to assign values to variables. The following table lists the assignment operators:
=
- Assigns a value to a variable+=
- Adds a value to a variable-=
- Subtracts a value from a variable*=
- Multiplies a variable by a value/=
- Divides a variable by a value%=
- Divides a variable by a value and assigns the remainder to the variable&=
- Performs a bitwise AND operation on a variable and a value and assigns the result to the variable|=
- Performs a bitwise OR operation on a variable and a value and assigns the result to the variable^=
- Performs a bitwise XOR operation on a variable and a value and assigns the result to the variable<<=
- Performs a bitwise left shift operation on a variable and a value and assigns the result to the variable>>=
- Performs a bitwise right shift operation on a variable and a value and assigns the result to the variable
Increment and Decrement Operators
Increment and decrement operators are used to increment or decrement a variable by 1. The following table lists the increment and decrement operators:
++
- Increments a variable by 1--
- Decrements a variable by 1
The increment and decrement operators can be placed before or after a variable. If they are placed before a variable, the variable is incremented or decremented before the value is used. If they are placed after a variable, the variable is incremented or decremented after the value is used.
Ternary Operator
The ternary operator is used to choose a value based on a condition.