In C++, an operator is a symbol used to perform logical and mathematical operations in a C++ program. An expression ia a statement containing operators and variables. C++ operators connects constants and variables to form expressions.
For Example:3.141 x radius x radius;
- x is operator
- 3.141 is a constant
- radius is a variables
- "3.141 x radius x radius;" is an expression, performing one logical task.
C++ Assignment Operators
Assignment Operator in C++ is used to assign a value to a variable. "=" is called simple assignment operator in C++, it assigns values from right side operands(R value) to left side operand (L value).
syntax of assignment operatorage = 25; count = 500;
C++ also supports compound assignment operators. These operators are combination of arithmetic and assignment operators. They first perform arithmetic operation between left and right operand and then assign the result to left operand.
- var+=10; is equivalent to var = var+10;
It first add 10 to var, and then assign result to var.
Operator | Example | Equivalent Expression | Description |
---|---|---|---|
+= | A += B; | A = A+B; | It Adds A and B, then assign result to A |
-= | A -= B; | A = A-B; | It subtract B from A, then assign result to A |
/= | A /= B; | A = A/B; | It divides A by B, then assign result to A |
*= | A *= B; | A = A*B; | It multiply A and B, then assign result to A |
%= | A %= B; | A = A%B; | It takes modulus of A by B, then assign result A |
&= | A &= B; | A = A&B; | It does bitwise AND between A and B, then assign result to A |
|= | A |= B; | A = A|B; | It does bitwise OR between A and B, then assign result to A |
^= | A ^= B; | A = A^B; | It does bitwise XOR between A and B, then assign result to A |
>>= | A >>= B; | A = A>>B; | It does bitwise right shift, then assign result to A |
<<= | A <<= B; | A = A< | It does bitwise left shift, then assign result to A |
C++ Arithmetic Operators
Arithmetic operators in C++ are used to perform mathematical operations. There are five fundamental arithmetic operators supported by C++ language, which are addition(+), subtraction(-), division(/), multiplication(-), and modulus(%) of two numbers.
Operator | Description | Syntax | Example |
---|---|---|---|
+ | Adds two numbers | a + b | 15 + 5 = 20 |
- | Subtracts two numbers | a - b | 15 - 5 = 10 |
* | Multiplies two numbers | a * b | 15 * 5 = 75 |
/ | Divides numerator by denominator | a / b | 15 / 5 = 3 |
% | Returns remainder after an integer division | a % b | 15 % 5 = 0 |
C++ Relational Operators
Relational Operators in C++ are used to compare two values. It specifies the relation between two values like equal, greater than, less than etc. Relational operators always returns boolean value (zero for false and non-zero value for true).
For Example :
(X < Y) : It checks whether X is less than Y or not. It will return none-zero(true) if X is less than Y otherwise zero(false).
Relational Operator | Example | Description |
---|---|---|
> | A > B | Checks if A is greater than B |
< | A < B | Checks if A is less than B |
>= | A >= B | Checks if A is greater than or equal to B |
<= | A <= B | Checks if A is less than or equal to B |
== | A == B | Checks if A is equal to B |
!= | A != B | Checks if A is not equal to B |
C++ Logical Operators
Logical Operators in C++ combines multiple boolean expressions. If we want to combine multiple boolean statements, then we need logical Operators. Logical Operators in C++ always produces boolean results, either TRUE(non-zero value) or FALSE(zero value). There are 3 logical operators in C++ language AND(&&), OR(||) and NOT(!).
- AND : Returns true, If both operands are true otherwise false.
- OR : Returns false If both operands are false, otherwise true.
- NOT : Returns true if operand is false and Returns false if operand is true.
A | B | A && B | A || B | !A |
---|---|---|---|---|
TRUE | FALSE | FALSE | TRUE | FALSE |
TRUE | TRUE | TRUE | TRUE | FALSE |
FALSE | TRUE | FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE | FALSE | TRUE |
C++ Bitwise operators
C++ inherits the mid-level language features from C. It support many operations which can be performed in assembly language like operations on bits. Bitwise operators performs bit-by-bit operations on operands. There are six bitwise operators supported by C++.
Bitwise Operator | Syntax | Description |
---|---|---|
| | A | B | Bitwise OR Operator |
& | A & B | Bitwise AND Operator |
~ | ~A | NOT Operator(One's Complement) |
^ | A ^ B | Bitwise Exclusive OR Operator |
>> | A >> B | Right Shift Operator |
<< | A << B | Left Shift Operator |
C++ Conditional Operator
C++ Conditional operators are also called as Ternary Operator. Conditional Operator is a powerful Operator which can be used to implement if-then-else type of logic.
Syntax of Ternary OperatorTernary Operator will execute Expression_One if Conditional_Expression is true, otherwise it execute Expression_Two.
For Example int X = 25;int Y = (X > 20 ? 1 : 2);
As X > 20, So after above statement Y's value becomes 1.
Operator Precedence
Operator precedence defines the order in which operators are evaluated within an expression. It ensures that expressions are unambiguous and that the correct operations are performed in the correct sequence. Like a set of rules guiding the dance of operators, understanding precedence is key to crafting expressions that behave as expected.
C++ has a well-defined hierarchy of operator precedence. Operators with higher precedence are evaluated before those with lower precedence. Here's a breakdown of the common operators and their relative precedence:
Parentheses () Unary operators +, -, ++, --, ! Multiplication, division, and modulus *, /, % Addition and subtraction +, - Bitwise shift <<, >> Relational operators <, >, <=, >= Equality operators ==, != Bitwise AND & Bitwise XOR ^ Bitwise OR | Logical AND && Logical OR || Conditional operator ? : Assignment operators =, +=, -=, *=, /=, %= Comma ,Understanding this hierarchy is crucial for interpreting and predicting how expressions will be evaluated.
Forcing Priority with Parentheses
Parentheses have the highest precedence, and they can be used to force the evaluation order within an expression. Any expression enclosed in parentheses is evaluated first.
int result = (10 + 5) * 3; // result is 45In this example, the addition within the parentheses is performed before the multiplication, ensuring that the result is 45.
Conclusion
You've navigated the vast sea of operators in C++, from the shores of arithmetic and assignment to the deep waters of bitwise and logical operations. Armed with this knowledge, you can now craft intricate expressions, make informed decisions, and wield the power of operators to shape the logic of your programs.
As you continue your coding adventure, remember that operators are your allies, allowing you to express complex computations and create dynamic, responsive software. Whether you're manipulating bits, making decisions, or iterating through loops, operators are the tools that transform your code into a masterpiece.