Operators Explained
Operators in C++ are symbols that perform operations on variables and values. Understanding different types of operators is crucial for effective programming. This webpage will delve into the key concepts related to operators in C++.
Key Concepts
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. These include addition, subtraction, multiplication, division, and modulus.
Example:
#include <iostream> using namespace std; int main() { int a = 10, b = 3; cout << "Addition: " << a + b << endl; // Output: 13 cout << "Subtraction: " << a - b << endl; // Output: 7 cout << "Multiplication: " << a * b << endl; // Output: 30 cout << "Division: " << a / b << endl; // Output: 3 cout << "Modulus: " << a % b << endl; // Output: 1 return 0; }
2. Relational Operators
Relational operators are used to compare two values. They return a boolean result (true or false). These include equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to.
Example:
#include <iostream> using namespace std; int main() { int x = 5, y = 8; cout << "Equal to: " << (x == y) << endl; // Output: 0 (false) cout << "Not equal to: " << (x != y) << endl; // Output: 1 (true) cout << "Greater than: " << (x > y) << endl; // Output: 0 (false) cout << "Less than: " << (x < y) << endl; // Output: 1 (true) cout << "Greater than or equal to: " << (x >= y) << endl; // Output: 0 (false) cout << "Less than or equal to: " << (x <= y) << endl; // Output: 1 (true) return 0; }
3. Logical Operators
Logical operators are used to combine multiple conditions. These include logical AND, logical OR, and logical NOT.
Example:
#include <iostream> using namespace std; int main() { bool p = true, q = false; cout << "Logical AND: " << (p && q) << endl; // Output: 0 (false) cout << "Logical OR: " << (p || q) << endl; // Output: 1 (true) cout << "Logical NOT: " << (!p) << endl; // Output: 0 (false) return 0; }
4. Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operator is =, and there are also compound assignment operators like +=, -=, *=, /=, and %=.
Example:
#include <iostream> using namespace std; int main() { int a = 10; a += 5; // Equivalent to a = a + 5 cout << "After +=: " << a << endl; // Output: 15 a -= 3; // Equivalent to a = a - 3 cout << "After -=: " << a << endl; // Output: 12 a *= 2; // Equivalent to a = a * 2 cout << "After *=: " << a << endl; // Output: 24 a /= 4; // Equivalent to a = a / 4 cout << "After /=: " << a << endl; // Output: 6 a %= 5; // Equivalent to a = a % 5 cout << "After %=: " << a << endl; // Output: 1 return 0; }
5. Increment and Decrement Operators
Increment and decrement operators are used to increase or decrease the value of a variable by 1. The increment operator is ++ and the decrement operator is --. These can be used in both prefix and postfix forms.
Example:
#include <iostream> using namespace std; int main() { int x = 5; cout << "Prefix increment: " << ++x << endl; // Output: 6 cout << "Postfix increment: " << x++ << endl; // Output: 6 (x is now 7) cout << "Prefix decrement: " << --x << endl; // Output: 6 cout << "Postfix decrement: " << x-- << endl; // Output: 6 (x is now 5) return 0; }