Difference between if-else and ?: Operator in Hindi
/ BCA / Programming with C and CPP
Difference between if-else and Ternary ?: Operator in Hindi
Difference between if-else and ?: Operator in Hindi
Introduction of Conditional Statements
किसी भी प्रोग्रामिंग भाषा में "Conditional Statement" का उपयोग निर्णय लेने के लिए किया जाता है। जब हमें किसी condition के आधार पर अलग-अलग कार्य करने होते हैं, तब हम if-else या ternary operator का उपयोग करते हैं। C और C++ जैसी भाषाओं में decision-making के लिए यह दोनों विकल्प उपलब्ध होते हैं।
What is if-else Statement?
if-else एक traditional decision-making statement है जिसमें पहले if condition को चेक किया जाता है और यदि वह true होती है तो एक block execute होता है, अन्यथा else block execute होता है।
Syntax of if-else Statement
if(condition) {
// अगर condition सही है तो यह block चलेगा
} else {
// अगर condition गलत है तो यह block चलेगा
}
Example of if-else
int number = 10;
if(number > 0) {
printf("Positive number");
} else {
printf("Negative number or Zero");
}
What is ?: (Ternary) Operator?
Ternary operator एक shorthand तरीका है decision लेने का। यह छोटा और compact syntax provide करता है। इसे conditional operator भी कहा जाता है क्योंकि यह तीन operands के साथ काम करता है।
Syntax of Ternary Operator
condition ? expression1 : expression2;
यदि condition सही होती है तो expression1 execute होता है, अन्यथा expression2।
Example of Ternary Operator
int number = 10;
printf(number > 0 ? "Positive number" : "Negative number or Zero");
Difference between if-else and ?: Operator
| Point | if-else Statement | Ternary ?: Operator |
|---|---|---|
| Syntax | लंबा और readable | छोटा और compact |
| Usage | जब multiple statements हों | जब एक ही statement हो |
| Readability | अधिक readable | कम readable (complex case में) |
| Speed | कोई फर्क नहीं | कोई फर्क नहीं |
| Return Value | return value की ज़रूरत नहीं होती | value को return करता है |
Nested Ternary ?: Operator in Hindi
What is Nested Ternary Operator?
जब हम ternary operator के अंदर फिर से एक और ternary operator का उपयोग करते हैं, तो उसे nested ternary कहते हैं। इसका उपयोग तब किया जाता है जब decision-making multi-level की हो।
Syntax of Nested Ternary
condition1 ? expression1 : (condition2 ? expression2 : expression3);
Example of Nested Ternary
int number = 0;
printf(number > 0 ? "Positive" : (number < 0 ? "Negative" : "Zero"));
ऊपर दिए गए code में हमने तीन स्थितियों को एक ही लाइन में check किया:
- अगर number > 0 है तो "Positive"
- अगर number < 0 है तो "Negative"
- अन्यथा "Zero"
When to use Nested Ternary?
- जब decision simple हो लेकिन multi-conditional हो
- जब हमें एक ही लाइन में multiple value check करनी हो
- परन्तु complex logic के लिए nested ternary से बचना चाहिए, readability कम हो जाती है
Use of ?: Operator in Short Conditions in Hindi
Why use Ternary in Short Conditions?
जब हमें किसी छोटी condition के आधार पर कोई value assign करनी हो या कोई simple result print करना हो, उस समय ternary operator if-else से बेहतर विकल्प होता है।
Example - Assigning Value
int age = 18;
string category = (age >= 18) ? "Adult" : "Minor";
Example - Printing Directly
int marks = 75;
printf(marks >= 33 ? "Pass" : "Fail");
Example - With Function Call
int a = 10, b = 20;
printf((a > b) ? "A is greater" : "B is greater");
Advantages of using Ternary Operator
- Code छोटा और साफ दिखाई देता है
- One-liner decision लेने में मदद करता है
- Read-only conditions के लिए perfect है
Limitations of Ternary Operator
- Complex conditions में readability कम हो जाती है
- Multiple statements के लिए उपयोग उचित नहीं
- Nested ternary को समझना मुश्किल हो सकता है
FAQs
int result = (a > b) ? a : b; इसमें result को condition के आधार पर value मिलती है।