C++ includes a variety of operators that can be used to perform operations on variables and values. Some of the most commonly used operators in C++ include:
Arithmetic operators: These operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Comparison operators: These operators are used to compare values and determine whether they are equal (==), not equal (!=), greater than (>), less than (<), greater than or equal to (>=), or less than or equal to (<=).
Logical operators: These operators are used to evaluate the logical relationship between two expressions and determine whether they are true (&&) or false (||).
Assignment operators: These operators are used to assign a value to a variable (=) or to perform an operation on a variable and then assign the result to the variable (+=, -=, *=, /=, %=).
Increment and Decrement operators: These operators are used to increase or decrease a variable by 1 (++, --).
Bitwise operators: These operators are used to perform bit-level operations on variables, such as bitwise AND (&), OR (|), XOR (^), left shift (<<), and right shift (>>).
Conditional operator: This operator is used to assign a value to a variable based on a condition (? :).
Here's an example of how some of these operators can be used:
int x = 5;
int y = 2;
int sum = x + y; // 7
int difference = x - y; // 3
int product = x * y; // 10
int quotient = x / y; // 2
int remainder = x % y; // 1
bool isEqual = (x == y); // false
bool isGreater = (x > y); // true
bool isLess = (x < y); // false
x += 2; // x is now 7
x++; // x is now 8
x--; // x is now 7
Note that the bitwise operators work on the binary representation of the number, and the conditional operator is also known as ternary operator, it's a shorthand for an if-else statement, it takes three operands, the first one is a condition and the second and third one are the value that will be returned if the condition is true or false.
Comments
Post a Comment