What is the usage of C++ operators?
C++ operators are special symbols used for performing various operations such as arithmetic, logical operations, and assignments. Here are some commonly used operators in C++ and their usage:
- Arithmetic operators:
- “+” is the symbol used for addition, it is used to perform the operation of adding two operands.
- “-” is the subtraction operator used to subtract two operands.
- “*” represents multiplication and is used to perform the multiplication of two operands.
- “/” is the division symbol used to divide two operands.
- “%”: Modulus operator, used to calculate the remainder after dividing two operands.
- Relational operators:
- “==” means equal to, it checks if two operands are equal.
- “!=”: means not equal to, it checks if the two operands are not equal.
- “>”: Greater than, check if the left operand is greater than the right operand.
- “<": Less than, checks if the left operand is smaller 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:
- “&&” is a logical AND operator used to check if both conditions are true at the same time.
- “||” is a logical OR operator used to check if at least one of the two conditions is true.
- “!” is a logical NOT operator used to negate a condition.
- Assignment operator:
- “=”: assignment operator used to assign the value of the right operand to the left operand.
- “+=”: add and assign, add the left operand to the right operand and assign the result to the left operand.
- “-=”: subtracts and assigns; subtracts the right operand from the left operand and assigns the result to the left operand.
- “*=” means multiply equals, it multiplies the left operand by the right operand and assigns the result to the left operand.
- “/=”: Divide the left operand by the right operand and assign the result to the left operand.
The above are just a portion of the C++ operators, there are many other operators such as increment, decrement operators, bitwise operators, conditional operators, etc., all with specific uses and functions.