Operators and Expressions in C++
Programming mein operators aur expressions kaafi important role play karte hain. Inki help se hum calculations, comparisons, aur logic build karte hain. Aaj hum C++ ke most important operators ko samjhenge.
Operators kya hote hain?
Operators aise symbols hote hain jo variables ya values par operations perform karte hain. Jaise +
, -
, ==
, &&
, etc.
Ek expression woh hota hai jisme values aur operators milke kuch result produce karte hain.
1. Arithmetic Operators
Yeh basic mathematical operations ke liye use hote hain:
Operator Meaning Example (a = 10, b = 13)
+
Addition a + b
→ 13
-
Subtraction a - b
→ 7
*
Multiplication a * b
→ 30
/
Division a / b
→ 3
%
Modulus (remainder) a % b
→ 1
2. Relational Operators
Ye comparison ke liye use hote hain. Output hota hai true (1)
ya false (0)
.
Operator Meaning Example
==
Equal to a == b
!=
Not equal to a != b
>
Greater than a > b
<
Less than a < b
>=
Greater than or equal a >= b
<=
Less than or equal a <= b
3. Logical Operators
Operator Name Use Case
&&
Logical AND Dono condition true ho toh true.
||
Logical OR Dono mein se koi bhi ek true ho toh.
!
NOT True ko false aur false ko true banata hai.
if (age > 18 && isStudent) {
cout << "Eligible for student pass!";
}
4. Operator Precedence & Associativity
Jab multiple operators ek saath use hote hain, toh precedence decide karta hai kaunsa operator pehle chalega. Associativity batata hai kis direction mein operation hoga – left to right ya right to left.
Example:
int result = 10 + 5 * 2; // Output: 20
Yahan *
ki precedence +
se zyada hai, toh pehle 5*2
hoga → 10, phir 10+10
= 20
Precedence Order (High to Low):
()
– Brackets* / %
+ -
- Relational
> < >= <=
- Equality
== !=
- Logical
&& ||
5. Type Casting
Kabhi-kabhi hume ek data type ko doosre mein convert karna padta hai – ise Type Casting kehte hain.
Implicit Casting (Automatically):
int a = 5;
float b = a; // int → float automatically
Explicit Casting (Manually):
float a = 5.6;
int b = (int)a; // Now b = 5
Isse value ka type toh change hota hai, lekin kuch data (jaise decimal) lost ho sakta hai.
Conclusion
So guys in this blog we've learned:
- Arithmetic, Relational, Logical operators
- Precedence and associativity ka matlab
- Type casting kaise aur kyu use hoti hai
Comments