🟢 Foundation  ·  Lesson 09

Operators in C++

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.

Operators क्या हैं?

Operators वे प्रतीक हैं जो C++ को values के साथ कुछ करने को कहते हैं — जोड़ना, तुलना, conditions जोड़ना, इत्यादि। मुख्य समूह सीखना रोज़मर्रा के code के लिए लगभग सब कुछ कवर कर देता है।

Arithmetic operators

Operatorअर्थउदाहरण (a=7, b=2)
+जोड़a + b → 9
-घटावa - b → 5
*गुणाa * b → 14
/भागa / b → 3
%शेषफलa % b → 1
💡 Integer division

7 / 2 3 देता है, 3.5 नहीं, क्योंकि दोनों integers हैं। दशमलव रखने को एक को double बनाएँ — 7.0 / 2

Relational operators

ये दो values की तुलना करके एक bool (true या false) देते हैं: == बराबर, != बराबर नहीं, <, >, <=, >=

C++
cout << (5 > 3) << " " << (5 == 3);   // 1 0 print karta hai

Logical operators

ये conditions जोड़ते हैं: && (AND, दोनों true), || (OR, कम-से-कम एक true), ! (NOT, इसे पलटता है)।

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

Assignment और compound

= assign करता है; compound रूप एक संक्रिया को assignment से जोड़ते हैं: +=, -=, *=, /=, %=

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

Increment और decrement

++ एक जोड़ता है, -- एक घटाता है। किसी बड़े expression में इस्तेमाल होने पर स्थान मायने रखता है:

C++
int i = 5;
cout << i++ << "\n";   // 5 print, phir i 6 ho jata hai
cout << ++i << "\n";   // i 7 ho jata hai, phir 7 print
Output:
5
7

Ternary operator

एक-line का if-else जो value देता है:

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

Operator precedence

गणित की तरह, कुछ operators दूसरों से ज़्यादा कसकर बँधते हैं: + और - से पहले * और /, && तथा || से पहले तुलनाएँ। संदेह हो तो कोष्ठक जोड़ें:

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

आम गलतियाँ

  • if में == (तुलना) की जगह = (assign) इस्तेमाल करना।
  • दो ints पर / से दशमलव रखने की उम्मीद करना — यह नहीं रखता।
  • बड़े expression के अंदर i++ और ++i को भ्रमित करना।
  • Precedence भूलकर चौंकाने वाला परिणाम पाना — कोष्ठक इस्तेमाल करें।
🏋️ अभ्यास

दो integers पढ़ें और उनका योग, अंतर, गुणनफल, integer भागफल और शेषफल print करें। फिर ternary operator से print करें कि कौन-सा बड़ा है।

सारांश

  • Arithmetic: + - * / %; integer division का ध्यान रखें।
  • Relational और logical operators true/false देते हैं।
  • Compound assignment (+= आदि) आम updates छोटा करता है।
  • ++/-- pre बनाम post expressions के अंदर मायने रखता है।
  • Ternary ?: संक्षिप्त if-else है; कोष्ठक precedence नियंत्रित करते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में operators क्या हैं?
Operators वे प्रतीक हैं जो values और variables पर संक्रियाएँ करते हैं, जैसे जोड़ के लिए + या तुलना के लिए ==। C++ इन्हें arithmetic, relational, logical, assignment, bitwise और increment तथा ternary जैसे कुछ विशेष operators में बाँटता है।
C++ में = और == में क्या अंतर है?
एक = assignment operator है, जो variable में value रखता है। दोहरा == equality operator है, जो दो values की तुलना करके true या false देता है। इन्हें मिलाना, खासकर if conditions के अंदर, बहुत आम bug है।
C++ में ++i और i++ में क्या अंतर है?
दोनों i को एक बढ़ाते हैं, पर वे इसमें भिन्न हैं कि किस value में evaluate होते हैं। Pre-increment ++i पहले बढ़ाता है फिर नई value देता है, जबकि post-increment i++ पहले पुरानी value देता है फिर बढ़ाता है। यह अंतर तभी मायने रखता है जब परिणाम किसी बड़े expression में इस्तेमाल हो।
C++ में ternary operator क्या है?
Ternary operator ?: एक संक्षिप्त if-else है जो value लौटाता है। condition ? a : b में, अगर condition true है तो परिणाम a है, वरना b। यह max = (x > y) ? x : y; जैसे छोटे चुनावों के लिए काम आता है।
C++ में operator precedence क्या है?
Operator precedence तय करता है कि expression में operators किस क्रम में evaluate हों, गणित की तरह जहाँ गुणा जोड़ से पहले होता है। जब क्रम अस्पष्ट हो, आप कोष्ठक इस्तेमाल करके अपना चाहा grouping लागू करते और expression पढ़ने योग्य बनाते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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