C++ Operators
Performing operations and calculations in C++
⚡ What are C++ Operators?
C++ operators are symbols that perform operations on variables and values. They include arithmetic, comparison, logical, and assignment operators for calculations and decision-making.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
cout << "Sum: " << a + b << endl;
cout << "Greater: " << (a > b) << endl;
return 0;
}
Output:
Greater: 1
Types of C++ Operators
Arithmetic
Basic math operations
int sum = 5 + 3; // 8
int diff = 10 - 4; // 6
Comparison
Compare values and return true/false
bool result = (5 > 3); // true
bool equal = (4 == 4); // true
Logical
Combine boolean expressions
bool and_op = true && false; // false
bool or_op = true || false; // true
Assignment
Assign and modify values
int x = 10;
x += 5; // x becomes 15
🔹 Arithmetic Operators
Arithmetic operators (+, -, *, /, %) perform basic mathematical computations on numeric data types. They are fundamental to all programming tasks, from simple calculations to complex algorithms. The modulus operator (%) is particularly useful for finding remainders, checking divisibility, or cycling through ranges. Understanding operator precedence and potential issues like integer division truncation or floating-point precision is key to writing accurate numerical code.
#include <iostream>
using namespace std;
int main() {
int a = 20, b = 6;
cout << "Addition: " << a + b << endl; // 26
cout << "Subtraction: " << a - b << endl; // 14
cout << "Multiplication: " << a * b << endl; // 120
cout << "Division: " << a / b << endl; // 3
cout << "Modulus: " << a % b << endl; // 2
return 0;
}
Output:
Subtraction: 14
Multiplication: 120
Division: 3
Modulus: 2
🔹 Comparison Operators
Comparison operators (==, !=, >, <,>=, <=) evaluate relationships between two values, returning a boolean
true or false. They are the backbone of conditional statements and
loops, enabling decision-making in programs. Proper use involves ensuring operands are compatible types and being
wary of floating-point comparisons due to precision issues. These operators control program flow and logic gates in
everything from sorting algorithms to user authentication.
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 5;
cout << "x == y: " << (x == y) << endl; // 0 (false)
cout << "x != y: " << (x != y) << endl; // 1 (true)
cout << "x > y: " << (x > y) << endl; // 1 (true)
cout << "x < y: " << (x < y) << endl; // 0 (false)
cout << "x >= y: " << (x >= y) << endl; // 1 (true)
return 0;
}
Output:
x != y: 1
x > y: 1
x < y: 0
x >= y: 1
🔹 Assignment Operators
Assignment operators (=, +=, -=, *=, /=, %=) store values in variables and can combine arithmetic with
assignment for conciseness. The basic = operator assigns a value, while compound operators (like +=)
modify the variable's current value. For example, x += 5 is shorthand for x = x + 5. Using
compound operators makes code more compact and often more readable, while also providing hints to the compiler for
potential optimization.
#include <iostream>
using namespace std;
int main() {
int num = 10;
cout << "Initial: " << num << endl;
num += 5; // num = num + 5
cout << "After +=5: " << num << endl;
num -= 3; // num = num - 3
cout << "After -=3: " << num << endl;
num *= 2; // num = num * 2
cout << "After *=2: " << num << endl;
return 0;
}
Output:
After +=5: 15
After -=3: 12
After *=2: 24