Conditional (Ternary) Operator (? :) in C Notes in Hindi & English : टर्नरी ऑपरेटर (? :): सिंटैक्स, कार्यप्रणाली और if-else का विकल्प और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Conditional (Ternary) Operator (? :) in C (टर्नरी ऑपरेटर (? :): सिंटैक्स, कार्यप्रणाली और if-else का विकल्प) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: The conditional (ternary) operator (? :) is C's only ternary operator, evaluating a boolean condition, enforcing a sequence point, and executing strictly one of two result branches. Its right-to-left associativity, branch type promotion, and rvalue output distinguish it from if-else and C++.

अध्याय सार: कंडीशनल (टर्नरी) ऑपरेटर (? :) C का एकमात्र तीन-ऑपरेंड वाला ऑपरेटर है, जो शर्त की जांच के बाद सीक्वेंस पॉइंट बनाकर दो में से केवल एक ही शाखा को निष्पादित करता है। इसका दाएँ से बाएँ साहचर्य, टाइप प्रमोशन व C में rvalue परिणाम इसे if-else तथा C++ से अलग बनाते हैं।

Conditional (Ternary) Operator (? :) in C: Detailed Notes & Architecture (टर्नरी ऑपरेटर (? :): सिंटैक्स, कार्यप्रणाली और if-else का विकल्प)

1. Ternary Syntax, Expression Power & Lazy Short-Circuiting (टर्नरी सिंटैक्स, एक्सप्रेशन क्षमता व शॉर्ट-सर्किटिंग)

[EN] With syntax `condition ? expr_true : expr_false`, the ternary operator evaluates `condition` first. ISO C enforces a sequence point right after `condition`, then evaluates ONLY `expr_true` if `condition` is non-zero or ONLY `expr_false` if `condition` is zero—never both. Because `? :` is an expression rather than a control statement like `if-else`, it returns a value and can initialize `const` variables (`const int max = (a > b) ? a : b;`), sit inside `return`, or appear inside `printf()` arguments.

[HI] `condition ? expr_true : expr_false` संरचना वाला टर्नरी ऑपरेटर पहले शर्त को जांचता है, फिर एक सीक्वेंस पॉइंट के बाद दो में से केवल एक ही शाखा का मूल्यांकन करता है (दूसरी शाखा बिल्कुल नहीं चलती)। चूंकि `? :` एक स्टेटमेंट नहीं बल्कि एक एक्सप्रेशन है, इसलिए यह मान लौटाता है और इसका उपयोग `const` वेरिएबल को इनिशियलाइज करने, `return` में या सीधे `printf()` के भीतर भी किया जा सकता है।

2. Right-to-Left Associativity & Chained Ternary Parsing (दाएँ से बाएँ साहचर्य व नेस्टेड टर्नरी चेनिंग)

[EN] The ternary operator associates strictly from right to left. Consequently, a chained ladder such as `a ? b : c ? d : e` is grouped by the compiler as `a ? b : (c ? d : e)`, creating a compact expression equivalent to an `if ... else if ... else` chain. For instance, `(n > 0) ? 1 : (n < 0) ? -1 : 0` cleanly returns the sign of `n`. Note that parentheses around the true-branch (`a ? (b ? c : d) : e`) remain valid, and putting the colon `:` inside a nested second branch never causes ambiguity because `?` and `:` pair like brackets.

[HI] टर्नरी ऑपरेटर का साहचर्य दाएँ से बाएँ (Right-to-Left) होता है। अतः `a ? b : c ? d : e` को कंपाइलर `a ? b : (c ? d : e)` मानकर हल करता है, जो बिल्कुल `if ... else if ... else` सीढ़ी की तरह काम करता है। उदाहरण के लिए `(n > 0) ? 1 : (n < 0) ? -1 : 0` एक ही लाइन में किसी संख्या का चिह्न बता देता है।

3. Type Conversion Rules Across Both Result Branches (दोनों शाखाओं के बीच टाइप रूपांतरण नियम)

[EN] Even though only one branch executes at runtime, the result type of `cond ? expr1 : expr2` is determined statically at compile time from BOTH branches using Usual Arithmetic Conversions! For example, in `1 ? 5 : 2.5`, the integer `5` is implicitly promoted to `double` because the second branch has type `double`; thus `sizeof(1 ? 5 : 2.5)` equals `sizeof(double)` (8 bytes) and printing it with `%d` causes Undefined Behavior. If one branch is `void` (like calling a `void` function or `(void)0`), the other must also be `void`; pointer branches must be compatible or involve `NULL`.

[HI] भले ही रनटाइम पर केवल एक ही शाखा चले, लेकिन पूरे टर्नरी एक्सप्रेशन का डेटा टाइप कंपाइल-टाइम पर दोनों शाखाओं के प्रकार देखकर तय होता है। जैसे `1 ? 5 : 2.5` में यद्यपि `5` चुना जाता है, फिर भी दूसरी शाखा `2.5` के `double` होने से पूरा एक्सप्रेशन `double` प्रकार का बन जाता है—अतः `sizeof(1 ? 5 : 2.5)` हमेशा `sizeof(double)` (8 बाइट) लौटाता है।

4. C vs C++ Lvalue Distinction, Precedence & Macro Hazards (C बनाम C++ एल-वैल्यू अंतर, प्राथमिकता व मैक्रो सावधानियाँ)

[EN] A famous C vs C++ exam distinction: in C, the ternary operator ALWAYS produces an rvalue, so `(cond ? a : b) = 10;` causes a compile-time lvalue error (though `*(cond ? &a : &b) = 10;` works via pointers). In C++, if both branches are lvalues of the same type, `(cond ? a : b) = 10;` is valid! Furthermore, `? :` sits at precedence tier 13—above assignment (`=`)—so `int x = a > b ? a : b;` needs no outer parentheses, while `(a > b ? a : b) + 1` DOES need parentheses because `+` binds tighter than `? :`. Avoid side effects inside ternary macros like `#define MAX(a,b) ((a)>(b)?(a):(b))` because `MAX(x++, y++)` evaluates the winner twice!

[HI] C और C++ का प्रमुख अंतर यह है कि C में `? :` सदैव एक rvalue लौटाता है जिससे `(cond ? a : b) = 10;` कंपाइल एरर देता है (जबकि C++ में यह वैध है; C में इसके लिए पॉइंटर्स `*(cond ? &a : &b) = 10;` उपयोग होते हैं)। टर्नरी ऑपरेटर की प्राथमिकता असाइनमेंट `=` से अधिक पर `+` से कम होती है। मैक्रो `#define MAX(a,b)` में `MAX(x++, y++)` लिखने पर बड़ी संख्या दो बार इंक्रीमेंट हो जाती है।

Syntax, Formula & Technical Example (Conditional (Ternary) Operator (? :) in C)

#include <stdio.h>
int main() {
    int a = 12, b = 25, n = -7;
    int max = (a > b) ? a : b;
    int sign = (n > 0) ? 1 : (n < 0) ? -1 : 0;
    printf("max=%d sign=%d sz=%zu\n", max, sign, sizeof(1 ? 5 : 2.5));
    return 0;
}
// Output: max=25 sign=-1 sz=8

Conditional (Ternary) Operator (? :) in C Quick Reference & Comparison Table

Ternary AspectC Language BehaviorExam Distinction
Branch Evaluation RuleExecutes strictly one branch after condition sequence pointUnselected branch side effects never run
Associativity DirectionAssociates right-to-left across chained ? : expressionsParses a?b:c?d:e as a?b:(c?d:e)
Static Result TypeApplies usual arithmetic conversions across both branches1 ? 5 : 2.5 yields double even when 5 is picked
C vs C++ Lvalue OutputYields non-modifiable rvalue in C unlike lvalue in C++(c ? a : b) = 1 fails compile in C

Conditional (Ternary) Operator (? :) in C Top 20 Bilingual MCQs with Answers

Q1. What is the output of `int a = 1, b = 5, c = 5; int r = a ? ++b : ++c; printf("%d %d %d", r, b, c);`?

(`int a = 1, b = 5, c = 5; int r = a ? ++b : ++c; printf("%d %d %d", r, b, c);` का आउटपुट क्या होगा?)

(A) 6 5 6

(B) 6 6 6

(C) 6 6 5

(D) 5 6 5

Correct Answer: (C)

[EN] Because condition a (1) is true, only ++b runs (b becomes 6, r = 6), while ++c is skipped so c remains 5.

[HI] शर्त a (1) सत्य होने से केवल ++b चलता है (b=6, r=6) और ++c बिल्कुल नहीं चलता, इसलिए c = 5 बना रहता है।

Q2. Assuming `sizeof(int) == 4` and `sizeof(double) == 8`, what does `printf("%zu", sizeof(1 ? 10 : 2.5));` print?

(`sizeof(int) == 4` और `sizeof(double) == 8` मानकर `printf("%zu", sizeof(1 ? 10 : 2.5));` क्या प्रिंट करेगा?)

(A) 12

(B) Compile error

(C) 4

(D) 8

Correct Answer: (D)

[EN] Both branches undergo Usual Arithmetic Conversions at compile time: int 10 promotes to double because 2.5 is double, so result size is 8.

[HI] कंपाइल-टाइम पर दोनों शाखाओं के टाइप मिलाने से int 10 भी double बन जाता है, अतः पूरे व्यंजक का आकार 8 बाइट निकलता है।

Q3. What is the output of `printf("%d", 0 ? 10 : 0 ? 20 : 30);` in C?

(C में `printf("%d", 0 ? 10 : 0 ? 20 : 30);` क्या प्रिंट करेगा?)

(A) 10

(B) 30

(C) Compile error

(D) 20

Correct Answer: (B)

[EN] Right-to-left associativity groups it as 0 ? 10 : (0 ? 20 : 30). Outer condition is 0, inner condition is 0, returning 30.

[HI] दाएँ से बाएँ साहचर्य के कारण यह 0 ? 10 : (0 ? 20 : 30) बनता है और दोनों शर्तें असत्य होने पर 30 प्रिंट होता है।

Q4. What happens when compiling `int a = 1, b = 2, c = 1; (c ? a : b) = 99;` in standard C?

(मानक C में `int a = 1, b = 2, c = 1; (c ? a : b) = 99;` को कंपाइल करने पर क्या होगा?)

(A) Assigns 99 to b cleanly

(B) Compilation error: lvalue required as left operand of assignment

(C) Assigns 99 to a cleanly

(D) Undefined Behavior at runtime

Correct Answer: (B)

[EN] Unlike C++, C defines the ternary operator to return an rvalue, which cannot appear on the left side of =.

[HI] C++ के विपरीत, C में टर्नरी ऑपरेटर हमेशा एक rvalue लौटाता है जिसे असाइनमेंट (=) के बाएँ पक्ष में नहीं रखा जा सकता।

Q5. How can you assign 99 to either `a` or `b` using the ternary operator in standard C?

(मानक C में टर्नरी ऑपरेटर की सहायता से `a` या `b` में से किसी एक में 99 स्टोर करने का सही तरीका क्या है?)

(A) &(cond ? a : b) = 99;

(B) *(cond ? &a : &b) = 99;

(C) (cond ? a : b) = 99;

(D) cond ? a = 99 : b;

Correct Answer: (B)

[EN] Selecting between pointers &a and &b and then dereferencing via * produces a modifiable lvalue.

[HI] दोनों वेरिएबल के पते (&a और &b) में से एक चुनकर उस पर * डीरेफरेंस लगाने से वैध मॉडिफाएबल एल-वैल्यू प्राप्त होता है।

Q6. What does `int a = 10, b = 20; int x = 5 + a < b ? 100 : 200;` assign to `x`?

(`int a = 10, b = 20; int x = 5 + a < b ? 100 : 200;` कोड में `x` को क्या मान मिलेगा?)

(A) Compile error

(B) 105

(C) 200

(D) 100

Correct Answer: (D)

[EN] Precedence: + binds tighter than <, and < binds tighter than ?:. Thus ((5 + 10) < 20) ? 100 : 200 -> 15 < 20 (true) -> 100.

[HI] प्राथमिकता क्रम (+ > < > ? :) के अनुसार ((5 + 10) < 20) अर्थात 15 < 20 सत्य होने पर x को 100 मिलता है।

Q7. Look closely at `#define MAX(a,b) ((a) > (b) ? (a) : (b))`. What happens with `int x = 3, y = 5; int m = MAX(x++, y++);`?

(`#define MAX(a,b) ((a) > (b) ? (a) : (b))` के साथ `int x = 3, y = 5; int m = MAX(x++, y++);` लिखने पर क्या समस्या आती है?)

(A) Neither x nor y is incremented

(B) Syntax error during macro expansion

(C) The larger variable y is incremented twice, causing wrong result and possible UB

(D) Both x and y are incremented cleanly once

Correct Answer: (C)

[EN] Macro expands to ((x++) > (y++) ? (x++) : (y++)), evaluating y++ once in comparison and again in the selected branch.

[HI] मैक्रो विस्तार में बड़ी संख्या y++ तुलना में एक बार और चयनित शाखा में दूसरी बार चलती है, जिससे y दो बार बढ़ जाता है।

Q8. Which ternary expression finds the maximum of three distinct integers `a`, `b`, and `c`?

(तीन पूर्णांकों `a`, `b`, और `c` में सबसे बड़ी संख्या निकालने का सही टर्नरी व्यंजक कौन सा है?)

(A) a > b ? a : b > c

(B) (a > b > c) ? a : c

(C) (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)

(D) (a > b) ? a : b : c

Correct Answer: (C)

[EN] Checking a > b splits into comparing a with c on the true branch and comparing b with c on the false branch.

[HI] पहले a > b जांचकर, सत्य शाखा में a व c की तुलना और असत्य शाखा में b व c की तुलना करना सभी स्थितियों में सही अधिकतम मान देता है।

Q9. Why is `const int limit = (mode == 1) ? 100 : 500;` valid with ternary `? :` but awkward with `if-else`?

(`const int limit = (mode == 1) ? 100 : 500;` टर्नरी के साथ वैध है जबकि `if-else` से सीधा संभव नहीं—क्यों?)

(A) Because ternary executes at compile time always

(B) Because ? : is an expression returning a value usable in a const initializer, whereas if-else is a statement

(C) Because if-else only tests constant conditions

(D) Because const variables cannot appear inside if blocks

Correct Answer: (B)

[EN] A const object must be initialized at declaration time by an expression, and ? : produces an expression value directly.

[HI] const वेरिएबल को घोषणा के समय ही एक एक्सप्रेशन मान चाहिए होता है; ? : एक एक्सप्रेशन है जो सीधे मान लौटाता है जबकि if-else एक स्टेटमेंट है।

Q10. What does `printf("%d", 1 ? 2 : 3 + 4);` print in C?

(C में `printf("%d", 1 ? 2 : 3 + 4);` का आउटपुट क्या होगा?)

(A) 9

(B) 6

(C) 7

(D) 2

Correct Answer: (D)

[EN] + has higher precedence than ?: so it parses as 1 ? 2 : (3 + 4), returning 2.

[HI] + की प्राथमिकता ?: से अधिक है, अतः यह 1 ? 2 : (3 + 4) बनता है और शर्त 1 होने से 2 प्रिंट होता है।

Q11. Given `void logErr(void);`, what happens when compiling `int x = 1; (x > 0) ? logErr() : 5;` in standard C?

(`void logErr(void);` दिए जाने पर मानक C में `int x = 1; (x > 0) ? logErr() : 5;` को कंपाइल करने पर क्या होता है?)

(A) Converts logErr() to return 0

(B) Prints 5 at runtime

(C) Compiles cleanly and discards 5

(D) Compilation error due to incompatible types (void vs int)

Correct Answer: (D)

[EN] When one result branch of ? : has type void, the other branch must also have type void (e.g., `(void)5`).

[HI] जब ? : की एक शाखा का टाइप void हो, तो दूसरी शाखा का भी void होना अनिवार्य है (जैसे `(void)5`); अन्यथा कंपाइल त्रुटि आती है।

Q12. How many operands does C's conditional operator take, and what symbols form it?

(C का कंडीशनल ऑपरेटर कितने ऑपरेंड लेता है और इसके चिह्न क्या हैं?)

(A) Two operands separated by ? and :

(B) Three operands separated by ? and :

(C) Three operands separated by : and ;

(D) Four operands separated by ? : and =

Correct Answer: (B)

[EN] It is the sole ternary operator in C, following the form `condition ? true_expr : false_expr`.

[HI] यह C का एकमात्र टर्नरी (तीन-ऑपरेंड) ऑपरेटर है जो `?` और `:` चिह्नों से बनता है।

Q13. What does `int a = 1, b = 2, c = 3; printf("%d", a > b ? b > c ? 1 : 2 : 3);` output?

(`int a = 1, b = 2, c = 3; printf("%d", a > b ? b > c ? 1 : 2 : 3);` का आउटपुट क्या होगा?)

(A) Syntax error

(B) 1

(C) 2

(D) 3

Correct Answer: (D)

[EN] The inner ternary sits in the true-branch of the outer `a > b ? (b > c ? 1 : 2) : 3`. Since 1 > 2 is false, the outer false branch returns 3.

[HI] बाहरी शर्त 1 > 2 असत्य होने के कारण प्रोग्राम सीधे बाहरी असत्य शाखा में जाकर 3 प्रिंट कर देता है।

Q14. In `int x = 0; char *p = (x != 0) ? "Yes" : NULL;`, why is mixing `"Yes"` and `NULL` valid in C?

(`int x = 0; char *p = (x != 0) ? "Yes" : NULL;` में `"Yes"` और `NULL` को मिलाना वैध क्यों है?)

(A) NULL is a null pointer constant compatible with any object pointer type in ? :

(B) "Yes" becomes an integer 1

(C) NULL converts implicitly into the empty string ""

(D) Ternary skips type checking for pointers

Correct Answer: (A)

[EN] ISO C allows one pointer branch of ? : to be a null pointer constant (NULL or 0), yielding the other branch's pointer type (char *).

[HI] ISO C में टर्नरी की एक शाखा किसी पॉइंटर व दूसरी शाखा NULL (नल पॉइंटर कॉन्स्टेंट) हो सकती है, जिससे परिणामी टाइप वही पॉइंटर मिलता है।

Q15. Which statement is NOT true about the conditional (? :) operator in C?

(C में कंडीशनल (? :) ऑपरेटर के बारे में इनमें से कौन सा कथन असत्य है?)

(A) It always evaluates both result expressions before selecting which value to return

(B) It enforces a sequence point after evaluating the condition

(C) In C, it returns an rvalue rather than an lvalue

(D) Its associativity is right-to-left

Correct Answer: (A)

[EN] Only the chosen branch is evaluated after the condition; the unselected branch is completely skipped.

[HI] शर्त जांचने के बाद केवल चुनी गई शाखा ही चलती है; दूसरी शाखा का मूल्यांकन बिल्कुल नहीं होता।

Q16. What is the output of `int k = 4; printf("%d", (k % 2 == 0) ? k * 2 : k + 1);`?

(`int k = 4; printf("%d", (k % 2 == 0) ? k * 2 : k + 1);` का आउटपुट क्या होगा?)

(A) 8

(B) 0

(C) 4

(D) 5

Correct Answer: (A)

[EN] 4 % 2 == 0 is true (1), so only k * 2 executes, outputting 4 * 2 = 8.

[HI] 4 % 2 == 0 सत्य (1) है, इसलिए पहली शाखा k * 2 चलकर 8 प्रिंट होता है।

Q17. Within Conditional (Ternary) Operator (? :) in C, which ternary aspect directly maps to: "Executes strictly one branch after condition sequence point"?

(Conditional (Ternary) Operator (? :) in C में कौन सा Ternary Aspect सीधे "Executes strictly one branch after condition sequence point" से संबंधित है?)

(A) Associativity Direction

(B) C vs C++ Lvalue Output

(C) Static Result Type

(D) Branch Evaluation Rule

Correct Answer: (D)

[EN] In Conditional (Ternary) Operator (? :) in C, 'Branch Evaluation Rule' handles 'Executes strictly one branch after condition sequence point' (Unselected branch side effects never run).

[HI] Conditional (Ternary) Operator (? :) in C में 'Branch Evaluation Rule' का कार्य/विवरण 'Executes strictly one branch after condition sequence point' (Unselected branch side effects never run) है।

Q18. Which option accurately describes 'Associativity Direction' in Conditional (Ternary) Operator (? :) in C?

(Conditional (Ternary) Operator (? :) in C के संदर्भ में 'Associativity Direction' की सही विशेषता कौन सी है?)

(A) Associates right-to-left across chained ? : expressions

(B) Applies usual arithmetic conversions across both branches

(C) Yields non-modifiable rvalue in C unlike lvalue in C++

(D) Executes strictly one branch after condition sequence point

Correct Answer: (A)

[EN] 'Associativity Direction' provides 'Associates right-to-left across chained ? : expressions' — key point: Parses a?b:c?d:e as a?b:(c?d:e).

[HI] 'Associativity Direction' का मुख्य विवरण 'Associates right-to-left across chained ? : expressions' है (Parses a?b:c?d:e as a?b:(c?d:e))।

Q19. In Conditional (Ternary) Operator (? :) in C, identify the correct parameter for the specification: "Applies usual arithmetic conversions across both branches".

(Conditional (Ternary) Operator (? :) in C में "Applies usual arithmetic conversions across both branches" विवरण किस विकल्प के लिए सही है?)

(A) C vs C++ Lvalue Output

(B) Branch Evaluation Rule

(C) Static Result Type

(D) Associativity Direction

Correct Answer: (C)

[EN] 'Static Result Type' corresponds to 'Applies usual arithmetic conversions across both branches' (1 ? 5 : 2.5 yields double even when 5 is picked).

[HI] 'Static Result Type' का संबंध 'Applies usual arithmetic conversions across both branches' (1 ? 5 : 2.5 yields double even when 5 is picked) से है।

Q20. Considering Conditional (Ternary) Operator (? :) in C, what key behavior defines 'C vs C++ Lvalue Output'?

(Conditional (Ternary) Operator (? :) in C में 'C vs C++ Lvalue Output' की मुख्य भूमिका या मान क्या है?)

(A) Associates right-to-left across chained ? : expressions

(B) Executes strictly one branch after condition sequence point

(C) Applies usual arithmetic conversions across both branches

(D) Yields non-modifiable rvalue in C unlike lvalue in C++

Correct Answer: (D)

[EN] 'C vs C++ Lvalue Output' represents 'Yields non-modifiable rvalue in C unlike lvalue in C++' ((c ? a : b) = 1 fails compile in C).

[HI] 'C vs C++ Lvalue Output' का अर्थ/मान 'Yields non-modifiable rvalue in C unlike lvalue in C++' है ((c ? a : b) = 1 fails compile in C)।

Conditional (Ternary) Operator (? :) in C — Exam FAQs

Q1: Why does `sizeof(1 ? 10 : 3.5)` return 8 bytes even though `10` (an int) is selected?

`1 ? 10 : 3.5` में `10` (int) चुने जाने के बाद भी `sizeof` 8 बाइट्स क्यों लौटाता है?

[EN] Result type determination happens at compile time by applying Usual Arithmetic Conversions across BOTH result operands (`int` and `double`), promoting the whole expression type to `double` regardless of runtime branch choice.

[HI] पूरे टर्नरी एक्सप्रेशन का टाइप कंपाइल-टाइम पर दोनों शाखाओं के मान देखकर तय होता है; `int` और `double` मिलने पर पूरा व्यंजक `double` (8 बाइट) में बदल जाता है।

Q2: Can the ternary operator `? :` appear on the left-hand side of `=` in C vs C++?

क्या टर्नरी ऑपरेटर `? :` को C और C++ में असाइनमेंट (`=`) के बाईं ओर रखा जा सकता है?

[EN] In C, `? :` always returns a non-modifiable rvalue, so `(c ? a : b) = 5;` fails to compile (use `*(c ? &a : &b) = 5;` instead). In C++, if both branches are lvalues of identical type, it returns an lvalue and compiles cleanly.

[HI] C में `? :` सदैव एक rvalue लौटाता है जिससे `(c ? a : b) = 5;` कंपाइल त्रुटि देता है (पॉइंटर द्वारा `*(c ? &a : &b) = 5;` लिखा जाता है), जबकि C++ में समान टाइप के एल-वैल्यू होने पर यह वैध है।

Q3: When should I choose `if-else` over the ternary `? :` operator?

टर्नरी `? :` के बजाय `if-else` का चुनाव कब करना चाहिए?

[EN] Use `if-else` whenever branches contain multiple statements, loops, declarations, or deeply nested conditions where readability would suffer. Reserve `? :` for concise value selection inside expressions, returns, or `const` initializers.

[HI] जब शाखाओं में एक से अधिक स्टेटमेंट हों, या नेस्टेड शर्तें जटिल होकर कोड को अपठनीय बना रही हों, तब `if-else` चुनें। `? :` को केवल संक्षिप्त मान चयन व `const` इनिशियलाइजेशन तक सीमित रखें।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)