CPP Tutorial



C++ OPERATORS


Operators in C++

Operators are special symbols or keywords in C++ that perform operations on variables and values. They form the basis of all expressions and calculations in C++.


1. Arithmetic Operators

Used to perform basic mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

2. Relational Operators

Used to compare two values or variables. Result is either true (1) or false (0).

Operator Description Example
== Equal to a == b
!= Not equal to a != b
< Less than a < b
> Greater than a > b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b

3. Logical Operators

Used to combine multiple conditions and return boolean results.

Operator Description Example
&& Logical AND (true if both conditions true) (a > 5) && (b < 10)
|| Logical OR (true if at least one condition true) (a == 5) || (b == 10)
! Logical NOT (inverts the condition) !(a == b)

4. Assignment Operators

Used to assign values to variables.

Operator Description Example
= Assign a = 5
+= Add and assign a += 3 (same as a = a + 3)
-= Subtract and assign a -= 2 (same as a = a - 2)
*= Multiply and assign a *= 4 (same as a = a * 4)
/= Divide and assign a /= 5 (same as a = a / 5)
%= Modulus and assign a %= 3 (same as a = a % 3)

5. Increment and Decrement Operators

Used to increase or decrease a variable’s value by 1.

Operator Description Example
++ Increment by 1 ++a or a++
-- Decrement by 1 --a or a--

6. Other Operators

  • Conditional (Ternary) Operator: ?: Used as a shortcut for if-else conditions.
    condition ? expression1 : expression2;
  • Bitwise Operators: Perform operations on bits (e.g., &, |, ^, ~, <<, >>).
  • sizeof Operator: Returns the size of a variable or data type in bytes.
    Example: sizeof(int)
  • Comma Operator (,): Allows multiple expressions in places where one expression is expected.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review