Operators are symbols that perform operations on variables and values. In C++, operators are categorized based on the type of operation they perform. Let’s explore the different types of operators available in C++.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder of division)
Example
#include <iostream>
int main() {
int a = 10, b = 3;
std::cout << "a + b = " << (a + b) << std::endl;
std::cout << "a - b = " << (a - b) << std::endl;
std::cout << "a * b = " << (a * b) << std::endl;
std::cout << "a / b = " << (a / b) << std::endl;
std::cout << "a % b = " << (a % b) << std::endl;
return 0;
}
2. Relational Operators
Relational operators are used to compare two values. They 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
Example
#include <iostream>
int main() {
int a = 10, b = 20;
std::cout << "a == b: " << (a == b) << std::endl;
std::cout << "a != b: " << (a != b) << std::endl;
std::cout << "a > b: " << (a > b) << std::endl;
std::cout << "a < b: " << (a < b) << std::endl;
std::cout << "a >= b: " << (a >= b) << std::endl;
std::cout << "a <= b: " << (a <= b) << std::endl;
return 0;
}
3. Logical Operators
Logical operators are used to combine multiple boolean expressions.
&&
: Logical AND||
: Logical OR!
: Logical NOT
Example
#include <iostream>
int main() {
bool x = true, y = false;
std::cout << "x && y: " << (x && y) << std::endl;
std::cout << "x || y: " << (x || y) << std::endl;
std::cout << "!x: " << (!x) << std::endl;
return 0;
}
4. Assignment Operators
Assignment operators are used to assign values to variables.
=
: Simple assignment+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign%=
: Modulus and assign
Example
#include <iostream>
int main() {
int a = 10;
a += 5; // Equivalent to a = a + 5
std::cout << "a += 5: " << a << std::endl;
a -= 3; // Equivalent to a = a - 3
std::cout << "a -= 3: " << a << std::endl;
a *= 2; // Equivalent to a = a * 2
std::cout << "a *= 2: " << a << std::endl;
a /= 4; // Equivalent to a = a / 4
std::cout << "a /= 4: " << a << std::endl;
a %= 3; // Equivalent to a = a % 3
std::cout << "a %= 3: " << a << std::endl;
return 0;
}
5. Bitwise Operators
Bitwise operators are used to perform bit-level operations.
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR~
: Bitwise NOT<<
: Left shift>>
: Right shift
Example
#include <iostream>
int main() {
int a = 5, b = 3; // In binary: a = 0101, b = 0011
std::cout << "a & b: " << (a & b) << std::endl; // Bitwise AND
std::cout << "a | b: " << (a | b) << std::endl; // Bitwise OR
std::cout << "a ^ b: " << (a ^ b) << std::endl; // Bitwise XOR
std::cout << "~a: " << (~a) << std::endl; // Bitwise NOT
std::cout << "a << 1: " << (a << 1) << std::endl; // Left shift
std::cout << "a >> 1: " << (a >> 1) << std::endl; // Right shift
return 0;
}
6. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by one.
++
: Increment--
: Decrement
Example
#include <iostream>
int main() {
int a = 5;
a++; // Increment
std::cout << "a++: " << a << std::endl;
a--; // Decrement
std::cout << "a--: " << a << std::endl;
return 0;
}
7. Conditional (Ternary) Operator
The conditional operator is used to return one of two values depending on a condition.
?:
: Conditional (ternary) operator
Example
#include <iostream>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b; // If a > b, max = a; otherwise, max = b
std::cout << "The maximum value is: " << max << std::endl;
return 0;
}
Conclusion
Operators are essential tools in C++ that allow you to perform a wide range of operations on variables and values. By understanding and using these operators effectively, you can write more powerful and efficient code. Practice using these operators to become more comfortable with their functionality and applications. Happy coding!