What are Operators in C++?
Operators in C++ are special symbols used to perform operations on
variables and values.
For example, + is used for addition and == is
used to compare two values.
C++ provides different types of operators for tasks like mathematical calculations, comparisons, logical decisions, and even bit-level operations.
Operands and Operators
An expression contains two main parts:
- Operand: The value on which the operation is performed.
- Operator: The symbol that performs the action.
Example:
int x = 20 + 5;
Explanation:
- Operands: 20 and 5
- Operator: + (Addition)
Types of Operators in C++:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Increment & Decrement Operators
- Conditional (Ternary) Operator
- Miscellaneous Operators
- Operator Precedence & Associativity
Why Do We Need Operators?
- To perform mathematical calculations.
- To compare two values.
- To make decisions inside programs.
- To assign values to variables.
- To increase or decrease values quickly.
1. Arithmetic Operators
These operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.
Click the next page to learn the types of arithmetic operators.
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 3;
cout << "Sum = " << a + b << endl;
cout << "Product = " << a * b << endl;
cout << "Modulus = " << a % b << endl;
return 0;
}
Output:
Sum = 13
Product = 30
Modulus = 1
Explanation:
- a + b: Adds two numbers.
- a * b: Multiplies the values.
- a % b: Gives the remainder.
2. Assignment Operators
These operators assign values to variables. Some of them also perform calculations while assigning.
Click the next page for full details.
= Simple assignment
+= a += b → a = a + b
-= a -= b → a = a - b
*= a *= b → a = a * b
/= a /= b → a = a / b
%= a %= b → a = a % b
3. Relational (Comparison) Operators
These operators are used to compare two values. They return true or false.
Click the next page for detailed explanation.
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
4. Logical Operators
Logical operators are used to combine two or more conditions. They return true or false. They are very useful in decision-making.
Click the next page to learn more.
&& Logical AND
|| Logical OR
! Logical NOT
5. Increment & Decrement Operators
Used to increase or decrease the value of a variable by 1.
Click the next page to learn prefix & postfix.
++a Pre-increment
a++ Post-increment
--a Pre-decrement
a-- Post-decrement
6. Bitwise Operators
These operators work on binary bits and perform bit-level operations.
Click the next page to learn more about each operator.
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift
7. Conditional (Ternary) Operator
This operator works like a short form of if-else.
Click the next page for more.
condition ? value1 : value2
Summary:
- Operators are symbols that perform operations on operands.
- They help in calculations, comparisons, assignments, and logical decisions.
- C++ supports many operator categories like arithmetic, logical, relational, bitwise, etc.
- Understanding operators is important to work with expressions and conditions in C++.