Introduction:
Operators in C++ are symbols or keywords that perform various operations on operands. An operand can be a variable, constant, or an expression. C++ provides a wide range of operators that allow you to perform arithmetic, logical, relational, bitwise, and other types of operations.
Arithmetic Operators:
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.
- + (Addition): Adds two operands.
- - (Subtraction): Subtracts the right operand from the left operand.
- * (Multiplication): Multiplies two operands.
- / (Division): Divides the left operand by the right operand.
- % (Modulus): Returns the remainder after division.
Relational Operators:
Relational operators are used to compare the values of two operands and return a Boolean result (true or false).
- == (Equal to): Checks if two operands are equal.
- != (Not equal to): Checks if two operands are not equal.
- > (Greater than): Checks if the left operand is greater than the right operand.
- < (Less than): Checks if the left operand is less than the right operand.
- >= (Greater than or equal to): Checks if the left operand is greater than or equal to the right operand.
- <= (Less than or equal to): Checks if the left operand is less than or equal to the right operand.
Logical Operators:
Logical operators are used to perform logical operations on Boolean expressions.
- && (Logical AND): Returns true if both operands are true.
- || (Logical OR): Returns true if at least one of the operands is true.
- ! (Logical NOT): Returns the opposite of the operand's value.
Bitwise Operators:
Bitwise operators perform operations on individual bits of operands.
- & (Bitwise AND): Performs bitwise AND operation.
- | (Bitwise OR): Performs bitwise OR operation.
- ^ (Bitwise XOR): Performs bitwise XOR (exclusive OR) operation.
- ~ (Bitwise NOT): Performs bitwise NOT operation.
- << (Left Shift): Shifts bits to the left.
- >> (Right Shift): Shifts bits to the right.
Assignment Operators:
Assignment operators are used to assign values to variables.
- = (Assignment): Assigns the value on the right to the variable on the left.
- += (Add and assign): Adds the right operand to the left operand and assigns the result to the left operand.
- -= (Subtract and assign): Subtracts the right operand from the left operand and assigns the result to the left operand.
- *= (Multiply and assign): Multiplies the left operand by the right operand and assigns the result to the left operand.
- /= (Divide and assign): Divides the left operand by the right operand and assigns the result to the left operand.
- %= (Modulus and assign): Calculates the modulus of the left operand with the right operand and assigns the result to the left operand.
Miscellaneous Operators:
There are a few other operators in C++.
- ?: (Conditional Operator/Ternary Operator): A shorthand for the if-else statement.
- sizeof (): Returns the size of a variable or data type.
- , (Comma Operator): Evaluates multiple expressions and returns the result of the last expression.
Conclusion:
Understanding and using C++ operators is essential for performing various computations and making decisions in your programs. Make sure to use them appropriately and be aware of operator precedence to avoid unexpected results.
0 Comments