Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 09

Operators in C++

In this lesson: Arithmetic, relational, logical, bitwise, assignment and ternary operators in C++ — precedence and output. In Hindi & English.

What are operators?

Operators are symbols that tell C++ to do something with values — add them, compare them, combine conditions, and so on. Learning the main groups covers almost everything you need for day-to-day code.

Arithmetic operators

OperatorMeaningExample (a=7, b=2)
+Adda + b → 9
-Subtracta - b → 5
*Multiplya * b → 14
/Dividea / b → 3
%Remaindera % b → 1
💡 Integer division

7 / 2 gives 3, not 3.5, because both are integers. Make one a double — 7.0 / 2 — to keep the decimals.

Relational operators

These compare two values and give a bool (true or false): == equal, != not equal, <, >, <=, >=.

C++
cout << (5 > 3) << " " << (5 == 3);   // prints 1 0

Logical operators

These combine conditions: && (AND, both true), || (OR, at least one true), ! (NOT, flips it).

C++
int age = 20;
bool ok = (age >= 18) && (age < 60);   // true

Assignment & compound

= assigns; compound forms combine an operation with assignment: +=, -=, *=, /=, %=.

C++
int total = 10;
total += 5;    // same as total = total + 5;  → 15
total *= 2;    // → 30

Increment and decrement

++ adds one, -- subtracts one. Placement matters when used inside a bigger expression:

C++
int i = 5;
cout << i++ << "\n";   // prints 5, then i becomes 6
cout << ++i << "\n";   // i becomes 7, then prints 7
Output:
5
7

The ternary operator

A one-line if-else that produces a value:

C++
int x = 8, y = 12;
int max = (x > y) ? x : y;   // 12
cout << "Larger: " << max;
Output:
Larger: 12

Operator precedence

Like maths, some operators bind tighter than others: * and / before + and -, comparisons before && and ||. When in doubt, add parentheses:

C++
int r = 2 + 3 * 4;         // 14, not 20
int r2 = (2 + 3) * 4;      // 20

Common mistakes

  • Using = (assign) where you meant == (compare) in an if.
  • Expecting / on two ints to keep decimals — it does not.
  • Confusing i++ and ++i inside a larger expression.
  • Forgetting precedence and getting a surprising result — use parentheses.
🏋️ Practice

Read two integers and print their sum, difference, product, integer quotient and remainder. Then use the ternary operator to print which one is larger.

Summary

  • Arithmetic: + - * / %; watch integer division.
  • Relational and logical operators give true/false.
  • Compound assignment (+= etc.) shortens common updates.
  • ++/-- pre vs post matters inside expressions.
  • The ternary ?: is a compact if-else; parentheses control precedence.

Frequently Asked Questions

What are operators in C++?
Operators are symbols that perform operations on values and variables, such as + for addition or == for comparison. C++ groups them into arithmetic, relational, logical, assignment, bitwise and a few special operators like increment and the ternary.
What is the difference between = and == in C++?
A single = is the assignment operator, which stores a value in a variable. A double == is the equality operator, which compares two values and gives true or false. Mixing them up, especially inside if conditions, is a very common bug.
What is the difference between ++i and i++ in C++?
Both increase i by one, but they differ in what they evaluate to. Pre-increment ++i increases first and then gives the new value, while post-increment i++ gives the old value first and then increases. The difference only matters when the result is used in a larger expression.
What is the ternary operator in C++?
The ternary operator ?: is a compact if-else that returns a value. In condition ? a : b, if the condition is true the result is a, otherwise it is b. It is handy for short choices like max = (x > y) ? x : y;.
What is operator precedence in C++?
Operator precedence decides the order in which operators are evaluated in an expression, much like maths where multiplication happens before addition. When the order is unclear, you use parentheses to force the grouping you want and make the expression readable.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicType Conversion and Type Casting Your progress: 9 / 81 (11%) · Lesson 10 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free