What is Conditional Operator in C Language in Hindi
/ BCA / Programming with C and CPP
Conditional Operator in C Language in Hindi
What is Conditional Operator in C Language in Hindi
Conditional Operator जिसे हम ternary operator भी कहते हैं, C Language में एक विशेष प्रकार का operator होता है जो हमें एक condition के आधार पर दो में से कोई एक value return करने की सुविधा देता है। यह एक short-hand तरीका होता है if-else condition को लिखने का। यह operator तीन हिस्सों में बंटा होता है, इसलिए इसे ternary कहा जाता है।
What is Conditional Operator? (Conditional Operator क्या है?)
Conditional Operator एक ऐसा operator है जो किसी condition को check करता है और उस condition के true या false होने के आधार पर अलग-अलग परिणाम देता है। इसका प्रयोग तब किया जाता है जब हमें केवल एक लाइन में condition check करनी हो और उसी के अनुसार result देना हो।
इसका basic syntax इस प्रकार होता है:
condition ? expression1 : expression2;
इसका मतलब यह है कि यदि condition true होती है तो expression1 execute होगा, और यदि condition false होती है तो expression2 execute होगा।
Why is it called ternary operator? (इसे ternary operator क्यों कहते हैं?)
- क्योंकि इसमें तीन भाग होते हैं – एक condition, एक true value और एक false value।
- यह C language का एकमात्र ternary operator होता है।
Syntax and usage of ?: operator in C in Hindi
Syntax of Conditional Operator (Conditional Operator का Syntax)
condition ? expression_if_true : expression_if_false;
Example 1: Check if number is positive or negative
int number = -10;
char *result;
result = (number > 0) ? "Positive" : "Negative";
printf("%s", result);
इस example में, यदि number > 0 होता है तो result में "Positive" store होता है, अन्यथा "Negative" store होता है।
Example 2: Maximum of two numbers
int a = 5, b = 8;
int max;
max = (a > b) ? a : b;
printf("Maximum is %d", max);
यहां पर a और b की तुलना की जा रही है, और जो बड़ा है उसे max में store किया जा रहा है।
Where to use Conditional Operator? (Conditional Operator का उपयोग कहां करें?)
- जब आपको छोटा सा condition check करना हो।
- जब आप if-else की जगह कम कोड लिखना चाहते हों।
- किसी variable में value assign करते समय condition लगानी हो।
Examples of Conditional Operator in real scenarios in Hindi
Example 1: Vote eligibility check
int age = 20;
char *message;
message = (age >= 18) ? "Eligible to vote" : "Not eligible to vote";
printf("%s", message);
इस example में, अगर user की age 18 या उससे अधिक है तो उसे vote देने योग्य बताया जाएगा।
Example 2: Even or Odd number
int num = 7;
char *result;
result = (num % 2 == 0) ? "Even" : "Odd";
printf("%s", result);
यहां पर number को 2 से divide करके remainder check किया जा रहा है। यदि 0 आता है तो "Even", अन्यथा "Odd"।
Example 3: Student Pass or Fail status
int marks = 45;
char *status;
status = (marks >= 40) ? "Pass" : "Fail";
printf("Student is %s", status);
अगर student के marks 40 या उससे ज़्यादा हैं तो Pass, नहीं तो Fail।
Example 4: Find absolute value
int num = -25;
int absolute;
absolute = (num < 0) ? -num : num;
printf("Absolute value is %d", absolute);
Negative number को positive में बदलने के लिए इस logic का प्रयोग किया गया है।
Nested Conditional Operators in C in Hindi
What is Nested Conditional Operator? (Nested Conditional Operator क्या होता है?)
जब एक Conditional Operator के अंदर ही दूसरा Conditional Operator लगाया जाता है, तो उसे nested conditional operator कहते हैं। इसका प्रयोग तब होता है जब हमें एक से अधिक conditions को एक साथ check करना हो।
Syntax of Nested Conditional Operator
condition1 ? (condition2 ? value1 : value2) : value3;
इसमें पहले condition1 check की जाएगी, यदि वह true होती है, तभी condition2 check होगी, और उसी के अनुसार value1 या value2 return होगा। यदि condition1 ही false हो जाती है, तो सीधे value3 return कर दिया जाएगा।
Example: Grade Evaluation System
int marks = 85;
char *grade;
grade = (marks >= 90) ? "A" :
(marks >= 75) ? "B" :
(marks >= 60) ? "C" :
(marks >= 40) ? "D" : "F";
printf("Grade: %s", grade);
इसमें हम एक से अधिक levels check कर रहे हैं और उसी के आधार पर grade दे रहे हैं।
Example: Find largest of three numbers
int a = 10, b = 20, c = 15;
int max;
max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
printf("Largest number is %d", max);
यहां पर हम तीन numbers में से सबसे बड़ा number निकाल रहे हैं nested conditional operator के प्रयोग से।
Advantages of Nested Conditional Operator
- कोड छोटा और readable बनता है।
- multiple if-else को एक लाइन में लिखा जा सकता है।
- logic साफ-सुथरा रहता है यदि सही indentation किया जाए।
Disadvantages of Nested Conditional Operator
- यदि बहुत अधिक nesting हो जाए तो code समझने में कठिन हो सकता है।
- Debugging में problem आ सकती है अगर logic गलत हो।
Tips to use Conditional Operator
- हमेशा parentheses () का सही इस्तेमाल करें ताकि logic clear रहे।
- Simple conditions के लिए ही use करें, complex logic में if-else का प्रयोग करें।
- Readability का ध्यान रखें ताकि दूसरा developer भी code को समझ सके।
FAQs
condition ? expression1 : expression2; यदि condition true होती है तो expression1 execute होगा, अन्यथा expression2।