C++ Operators Explained | Arithmetic, Relational, Logical, Bitwise and Ternary Operator


This complete tutorial on C++ Operators explains how operators work in C++ programs. It covers arithmetic, relational, logical, bitwise, assignment, and conditional (ternary) operators with clear examples and best practices. This tutorial helps beginners understand expressions, conditions, and calculations, which are essential for writing efficient and readable C++ code.

Operators – Complete Tutorial

1. What are Operators?

Operators are symbols that perform operations on variables and values. They are used to manipulate data, compare values, and control program flow.

Example:


int sum = a + b;

2. Arithmetic Operators

Arithmetic operators are used for mathematical calculations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b

Example:


int a = 10, b = 3;
cout << a + b;
cout << a % b;

Best Practices:

  1. Avoid division by zero
  2. Use % only with integers

3. Relational Operators

Relational operators compare two values and return a boolean result (true or false).

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example:


int x = 10, y = 20;
cout << (x < y);

4. Logical Operators

Logical operators are used to combine multiple conditions.

OperatorDescription
&&Logical AND
`
!Logical NOT

Example:


if (age > 18 && age < 60) {
cout << "Eligible";
}

Best Practices:

  1. Use parentheses for clarity
  2. Avoid complex logical expressions

5. Bitwise Operators

Bitwise operators work on individual bits of integers.

OperatorDescription
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

Example:


int a = 5, b = 3;
cout << (a & b);

Use Cases:

  1. Low-level programming
  2. Performance optimization
  3. Embedded systems

6. Assignment Operators

Assignment operators assign values to variables.

OperatorExampleEquivalent
=a = ba = b
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b

Example:


int x = 10;
x += 5;

7. Conditional (Ternary) Operator

The conditional operator is a shorthand for if-else.

Syntax:


condition ? expression1 : expression2;

Example:


int max = (a > b) ? a : b;

Best Practices:

  1. Use for simple conditions only
  2. Avoid nested ternary operators

Operator Precedence (Basic Idea)

Operator precedence determines the order of evaluation.

Example:


int result = 10 + 5 * 2; // Output: 20

Best Practice:

  1. Use parentheses to improve readability

Common Mistakes to Avoid

  1. Confusing = with ==
  2. Using bitwise operators instead of logical operators
  3. Ignoring operator precedence
  4. Overusing ternary operator

Summary

In this chapter, you learned about C++ operators, including arithmetic, relational, logical, bitwise, assignment, and conditional operators. Operators are fundamental to writing expressions, conditions, and calculations in C++ programs.