In C++, operators are special symbols or characters that perform specific operations on one or more values or variables. C++ supports a wide range of operators, including arithmetic, assignment, comparison, logical, bitwise, and ternary operators.
Arithmetic Operators: Arithmetic operators are used to perform mathematical operations on numerical values. C++ supports the following arithmetic operators: +
for addition, -
for subtraction, *
for multiplication, /
for division, and %
for modulus.
Assignment Operators: Assignment operators are used to assign values to variables. C++ supports the following assignment operators: =
for simple assignment, +=
for addition assignment, -=
for subtraction assignment, *=
for multiplication assignment, /=
for division assignment, and %=
for modulus assignment.
Comparison Operators: Comparison operators are used to compare two values or variables. C++ supports the following comparison operators: ==
for equal to, !=
for not equal to, >
for greater than, <
for less than, >=
for greater than or equal to, and <=
for less than or equal to.
Logical Operators: Logical operators are used to combine multiple conditions or expressions. C++ supports the following logical operators: &&
for logical and, ||
for logical or, and !
for logical not.
Bitwise Operators: Bitwise operators are used to perform bitwise operations on binary numbers. C++ supports the following bitwise operators: &
for bitwise and, |
for bitwise or, ^
for bitwise xor, ~
for bitwise not, <<
for bitwise left shift, and >>
for bitwise right shift.
Ternary Operator: The ternary operator in C++ is a shorthand way to write an if-else statement in a single line. It has the syntax condition ? value_if_true : value_if_false
, and is often used to assign a value to a variable based on a condition.
In summary, C++ provides a wide range of operators that can be used to perform various operations on values and variables. Understanding the different types of operators and their usage is important for developing complex programs and solving real-world problems.