2.3) Operators in C++
Operators in C++ are symbols that represent operations to be performed on operands (values or variables). They allow you to manipulate and work with data in various ways. Let’s explore different categories of operators in C++ with code examples and explanations.
Table of Contents
1. Arithmetic Operators
Arithmetic operators perform basic arithmetic operations.
- 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 %: Computes the remainder of the division of the left operand by the right operand.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 4;
int sum = a + b; // Addition
int diff = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus (Remainder)
cout << "Sum: " << sum << endl;
cout << "Difference: " << diff << endl;
cout << "Product: " << product << endl;
cout << "Quotient: " << quotient << endl;
cout << "Remainder: " << remainder << endl;
return 0;
}
Output:
Sum: 14
Difference: 6
Product: 40
Quotient: 2
Remainder: 2
2. Comparison Operators
Comparison operators compare two values and return a boolean result (true
or false
).
- Equal to ==
- Not equal to !=
- Greater than
>
- Less than <
- Greater than or equal to >=
- Less than or equal to <=
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
bool isEqual = (x == y); // Equal to
bool isNotEqual = (x != y); // Not equal to
bool isGreater = (x > y); // Greater than
bool isLess = (x < y); // Less than
bool isGreaterOrEqual = (x >= y); // Greater than or equal to
bool isLessOrEqual = (x <= y); // Less than or equal to
cout << "Is equal: " << isEqual << endl;
cout << "Is not equal: " << isNotEqual << endl;
cout << "Is greater: " << isGreater << endl;
cout << "Is less: " << isLess << endl;
cout << "Is greater or equal: " << isGreaterOrEqual << endl;
cout << "Is less or equal: " << isLessOrEqual << endl;
return 0;
}
Output:
Is equal: 0
Is not equal: 1
Is greater: 0
Is less: 1
Is greater or equal: 0
Is less or equal: 1
3. Logical Operators
Logical operators perform logical operations on boolean values.
- Logical AND &&: Returns true if both operands are true.
- Logical OR ||: Returns true if at least one operand is true.
- Logical NOT !: Returns the opposite of the operand’s value.
#include <iostream>
using namespace std;
int main() {
bool a = true, b = false;
bool logicalAnd = a && b; // Logical AND
bool logicalOr = a || b; // Logical OR
bool logicalNotA = !a; // Logical NOT for 'a'
bool logicalNotB = !b; // Logical NOT for 'b'
cout << "Logical AND: " << logicalAnd << endl;
cout << "Logical OR: " << logicalOr << endl;
cout << "Logical NOT for 'a': " << logicalNotA << endl;
cout << "Logical NOT for 'b': " << logicalNotB << endl;
return 0;
}
Output:
Logical AND: 0
Logical OR: 1
Logical NOT for 'a': 0
Logical NOT for 'b': 1
4. Assignment Operators
Assignment operators assign values to variables.
- Assignment =: Assigns the value on the right to the variable on the left.
- Addition assignment
+
=: Adds the right operand to the left operand and assigns the result to the left. - Subtraction assignment -=: Subtracts the right operand from the left operand and assigns the result to the left.
- Multiplication assignment *=: Multiplies the left operand by the right operand and assigns the result to the left.
- Division assignment /=: Divides the left operand by the right operand and assigns the result to the left.
- Modulus assignment %=: Computes the remainder of the division of the left operand by the right operand and assigns the result to the left.
#include <iostream>
using namespace std;
int main() {
int x = 5;
x += 3; // x = x + 3
cout << "x after +=: " << x << endl;
x -= 2; // x = x - 2
cout << "x after -=: " << x << endl;
x *= 4; // x = x * 4
cout << "x after *=: " << x << endl;
x /= 2; // x = x / 2
cout << "x after /=: " << x << endl;
x %= 3; // x = x % 3
cout << "x after %=: " << x << endl;
return 0;
}
Output:
x after +=: 8
x after -=: 6
x after *=: 24
x after /=: 12
x after %=: 0
5. Increment and Decrement Operators
Increment (++) and decrement (–) operators change the value of a variable by one.
#include <iostream>
using namespace std;
int main() {
int count = 5;
count++; // Increment
cout << "After increment: " << count << endl;
count--; // Decrement
cout << "After decrement: " << count << endl;
return 0;
}
Output:
After increment: 6
After decrement: 5
6. Bitwise Operators
Bitwise operators perform operations on individual bits of integers.
- Bitwise AND &: Performs a bitwise AND operation.
- Bitwise OR |: Performs a bitwise OR operation.
- Bitwise XOR ^: Performs a bitwise XOR (exclusive OR) operation.
- Bitwise NOT ~: Flips the bits of its operand.
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
int bitwiseAnd = a & b; // Bitwise AND
int bitwiseOr = a | b; // Bitwise OR
int bitwiseXor = a ^ b; // Bitwise XOR
int bitwiseNotA = ~a; // Bitwise NOT for 'a'
cout << "Bitwise AND: " << bitwiseAnd << endl;
cout << "Bitwise OR: " << bitwiseOr << endl;
cout << "Bitwise XOR: " << bitwiseXor << endl;
cout << "Bitwise NOT for 'a': " << bitwiseNotA << endl;
return 0;
}
Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT for 'a': -6
Understanding and utilizing these operators allows you to perform various operations on your data in C++ programs.
Operator Precedence in C++
Operator precedence in C++ determines the order in which operators are evaluated when an expression contains multiple operators. Operators with higher precedence are evaluated before operators with lower precedence.
Here’s a table showing the operator precedence in C++ from highest to lowest:
Precedence | Operators | Description |
---|---|---|
Highest | () , [] , -> , . | Parentheses, member access |
++ , -- , typeid , sizeof , new | Increment, decrement, type queries | |
! , ~ , + , - , * , & | Unary operators | |
* , / , % | Multiplication, division, modulus | |
+ , - | Addition, subtraction | |
<< , >> | Bitwise shift | |
< , <= , > , >= | Relational operators | |
== , != | Equality operators | |
& | Bitwise AND | |
^ | Bitwise XOR | |
| | Bitwise OR | |
&& | Logical AND | |
|| | Logical OR | |
?: | Ternary conditional | |
= | Assignment | |
Lowest | , | Comma operator |
Keep in mind that this table represents the general operator precedence in C++, but it’s always a good practice to use parentheses to explicitly indicate the desired order of operations when there’s ambiguity.