Notes in Hindi

Introduction to ?: Operator in Hindi

/ BCA / Programming with C and CPP

?: Operator in C Language in Hindi

Introduction to ?: Operator in Hindi

C Language में कई प्रकार के Operators होते हैं जैसे Arithmetic, Relational, Logical आदि। इन्हीं में से एक विशेष Operator होता है ?: Operator, जिसे Conditional Operator या Ternary Operator भी कहा जाता है। इसे "ternary" इसलिए कहा जाता है क्योंकि यह तीन operands के साथ काम करता है।

यह operator decision लेने के लिए प्रयोग किया जाता है, जहाँ हमें एक condition के आधार पर किसी value को select करना होता है। अगर आपने if-else statement पढ़ा है, तो आप इसे एक short version की तरह समझ सकते हैं।

C भाषा में यह operator code को छोटा और readable बनाने में मदद करता है। यह खासतौर पर तब उपयोगी होता है जब एक simple condition के आधार पर कोई assignment करना हो।

Why it is called Conditional or Ternary Operator?

  • इसमें तीन parts होते हैं: condition ? expression1 : expression2
  • यह condition को check करता है और true होने पर expression1 का value return करता है, अन्यथा expression2 का।

Syntax and Use of ?: Operator in C in Hindi

Syntax of ?: Operator

condition ? expression1 : expression2;

ऊपर दिए गए syntax में सबसे पहले condition check की जाती है। यदि condition true होती है तो expression1 execute होता है, और यदि false होती है तो expression2 execute होता है।

Explanation in Hindi:

  • condition: यह कोई भी logical या relational expression हो सकता है जिसे TRUE या FALSE में evaluate किया जा सकता है।
  • expression1: जब condition सही (TRUE) हो, तो यह हिस्सा execute होता है।
  • expression2: जब condition गलत (FALSE) हो, तो यह हिस्सा execute होता है।

Use of ?: Operator in Assignments:

Conditional Operator का सबसे सामान्य उपयोग variable को किसी condition के अनुसार value assign करने में होता है।

int a = 10, b = 20, max; max = (a > b) ? a : b; // यहाँ max को a या b में से जो बड़ा है, वो assign किया जाएगा।

Use in Print Statement:

int x = 5; printf("%s", (x % 2 == 0) ? "Even" : "Odd"); // x अगर even है तो "Even" print होगा, नहीं तो "Odd"।

Use Without Assignment:

(a > b) ? printf("A is greater") : printf("B is greater or equal"); // सिर्फ decision लेने और message print करने के लिए।

Simple Examples of ?: Operator in Hindi

Example 1: बड़ा नंबर ढूंढना

#include <stdio.h> int main() { int a = 10, b = 20, max; max = (a > b) ? a : b; printf("Maximum number is: %d", max); return 0; }

Output: Maximum number is: 20

Example 2: पास या फेल चेक करना

#include <stdio.h> int main() { int marks = 75; printf("%s", (marks >= 40) ? "Pass" : "Fail"); return 0; }

Output: Pass

Example 3: Even या Odd चेक करना

#include <stdio.h> int main() { int num = 9; printf("%s", (num % 2 == 0) ? "Even" : "Odd"); return 0; }

Output: Odd

Example 4: Nested ?: Operator

#include <stdio.h> int main() { int a = 10, b = 20, c = 5, max; max = (a > b) ? (a > c ? a : c) : (b > c ? b : c); printf("Greatest number is: %d", max); return 0; }

Output: Greatest number is: 20

Example 5: Absolute value निकालना

#include <stdio.h> int main() { int num = -8; int absValue = (num < 0) ? -num : num; printf("Absolute value is: %d", absValue); return 0; }

Output: Absolute value is: 8

?: Operator की विशेषताएं

  • Code को छोटा और clean बनाता है।
  • Simple conditions को एक लाइन में लिखने में मदद करता है।
  • Nested form में उपयोग करना थोड़ा tricky हो सकता है, इसलिए clarity ज़रूरी है।

?: Operator vs if-else

बिंदु ?: Operator if-else Statement
लंबाई (Length) कम अधिक
Readability Simple conditions के लिए बेहतर Complex logic के लिए बेहतर
Use एक लाइन में निर्णय Multiple statements के लिए
Nested Condition थोड़ा Confusing Readable

Beginner के लिए Important Points:

  • अगर आपकी condition बहुत simple है, जैसे marks check करना, maximum निकालना, तो ?: Operator perfect है।
  • लेकिन अगर condition complex है या बहुत सारे conditions check करने हैं, तो if-else बेहतर रहता है।
  • Code को unnecessarily complex न बनाएं nested ?: Operator से।
  • हर बार syntax में तीन हिस्से होने चाहिए: condition ? true-result : false-result

FAQs

?: Operator को Conditional Operator या Ternary Operator भी कहा जाता है। यह एक shorthand तरीका होता है किसी condition को check करने और उसके आधार पर दो में से एक value return करने का। इसका syntax होता है: condition ? expression1 : expression2;
?: Operator को ternary operator इसलिए कहा जाता है क्योंकि यह तीन operands के साथ काम करता है - एक condition, एक true expression और एक false expression। यही इसकी खास पहचान है।
इसका syntax होता है: condition ? expression1 : expression2;
यदि condition true होती है तो expression1 execute होता है, अन्यथा expression2 execute होता है।
?: Operator एक short और compact तरीका होता है decision लेने का जबकि if-else एक complete block होता है। ?: Operator एक line में ही काम कर सकता है, जबकि if-else multiple lines में होता है। लेकिन complex conditions के लिए if-else बेहतर होता है।
हाँ, हम ?: Operator को nest कर सकते हैं लेकिन nested ternary operator थोड़ा complex और confusion पैदा कर सकता है, इसलिए readability को ध्यान में रखते हुए इसका उपयोग सावधानी से करें।

Please Give Us Feedback