Control Statements: if, if-else, nested if in Java Notes in Hindi & English : कंडीशनल स्टेटमेंट्स: if, if-else और नेस्टेड if की संरचना और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: Decision-making control statements in Java—if, if-else, if-else-if ladders, and nested if—branch program execution based exclusively on boolean expressions. Mastering Java's strict boolean condition check, dangling-else binding rules, and semicolon pitfalls prevents subtle logic bugs in conditional flows.

अध्याय सार: जावा में निर्णय लेने वाले कंट्रोल स्टेटमेंट्स—if, if-else, if-else-if लैडर और नेस्टेड if—विशेष रूप से boolean व्यंजकों के आधार पर प्रोग्राम निष्पादन को निर्देशित करते हैं। सख्त boolean शर्त जांच, डैंगलिंग-else नियम और सेमीकोलन से जुड़ी त्रुटियों को समझना त्रुटि-रहित कोड के लिए आवश्यक है।

Control Statements: if, if-else, nested if in Java: Detailed Notes & Architecture (कंडीशनल स्टेटमेंट्स: if, if-else और नेस्टेड if की संरचना)

1. The Simple if Statement and Strict Boolean Requirement (सरल if स्टेटमेंट और सख्त Boolean शर्त नियम)

[EN] The 'if (condition)' statement executes its guarded statement or { block } only when 'condition' evaluates to boolean true. Unlike C and C++, where 0 means false and any non-zero integer means true, Java strictly enforces that the condition expression inside if(...) must have type boolean or unboxable Boolean—writing 'if (1)' or 'if (x = 5)' (when x is int) fails at compile time. Curly braces { } are optional when the body contains a single statement, but omitting them often leads to maintenance bugs if a second statement is later added. Also note that 'if (false) { ... }' is explicitly permitted by the JLS (for conditional compilation) without causing an unreachable-code compile error, unlike 'while (false)'.

[HI] 'if (condition)' स्टेटमेंट केवल तभी अपना ब्लॉक निष्पादित करता है जब 'condition' का परिणाम boolean true हो। C और C++ के विपरीत जहाँ 0 false और नॉन-ज़ीरो true माना जाता है, जावा में if(...) के अंदर केवल boolean या Boolean प्रकार ही मान्य है—'if (1)' लिखने पर कंपाइल त्रुटि आती है। केवल एक स्टेटमेंट होने पर कर्ली ब्रेसिज़ { } वैकल्पिक होते हैं, परंतु पठनीयता और सुरक्षा के लिए इन्हें लगाना सर्वोत्तम अभ्यास है। यह भी ध्यान दें कि 'while (false)' के विपरीत, JLS में कंडीशनल संकलन हेतु 'if (false) { ... }' की अनुमति है।

2. The if-else Statement and if-else-if Ladder (if-else स्टेटमेंट और if-else-if लैडर)

[EN] An 'if-else' statement guarantees two-way mutually exclusive branching: when the condition is true, the 'if' branch executes; otherwise, the 'else' branch executes. For multi-way decisions spanning ranges or unrelated boolean tests, conditions are chained in an 'if ... else if ... else' ladder evaluated sequentially from top to bottom. As soon as the first true condition is encountered, its corresponding block executes and the remaining ladder branches are bypassed completely; a trailing 'else' acts as the fallback default when every prior test fails. For simple value-returning two-branch expressions, Java also offers the ternary conditional operator 'result = cond ? exprA : exprB;' as a concise alternative.

[HI] 'if-else' स्टेटमेंट दो परस्पर अनन्य मार्ग प्रदान करता है: शर्त सत्य होने पर 'if' ब्लॉक और असत्य होने पर 'else' ब्लॉक चलता है। कई शर्तों या रेंज की जांच के लिए 'if ... else if ... else' लैडर का उपयोग होता है, जिसमें शर्तें ऊपर से नीचे क्रमशः जांची जाती हैं। पहली सत्य शर्त मिलते ही उसका ब्लॉक चलता है और शेष सभी शाखाएँ छोड़ दी जाती हैं; कोई शर्त सत्य न होने पर अंतिम 'else' डिफ़ॉल्ट रूप से चलता है। दो विकल्पों में से मान लौटाने के लिए टर्नरी ऑपरेटर 'result = cond ? exprA : exprB;' एक संक्षिप्त अभिव्यक्ति विकल्प है।

3. Nested if Statements and the Dangling else Rule (नेस्टेड if स्टेटमेंट और डैंगलिंग else नियम)

[EN] A nested 'if' places one complete 'if' or 'if-else' construct inside the body of an outer 'if' or 'else' block to express hierarchical preconditions (for example, checking age >= 18 before checking citizenship). When multiple nested 'if' statements appear without curly braces, ambiguity arises over which 'if' owns a trailing 'else'—known classically as the 'dangling else' problem. Java resolves this deterministically in its grammar: every 'else' always binds to the nearest preceding unmatched 'if' within the same block, regardless of visual code indentation! Explicitly enclosing nested conditionals inside { } eliminates dangling-else misunderstandings completely.

[HI] नेस्टेड 'if' में किसी बाहरी 'if' या 'else' ब्लॉक के भीतर दूसरा 'if' या 'if-else' रखा जाता है ताकि क्रमिक शर्तों (जैसे पहले आयु और फिर नागरिकता) की जांच हो सके। बिना कर्ली ब्रेसिज़ के नेस्टेड 'if' लिखने पर यह भ्रम होता है कि एक 'else' किस 'if' से जुड़ा है—जिसे 'डैंगलिंग else' समस्या कहते हैं। जावा व्याकरण इसे स्पष्ट नियम से हल करता है: इंडेंटेशन चाहे जैसा हो, प्रत्येक 'else' हमेशा अपने ठीक पहले वाले निकटतम अनमेच्ड 'if' से जुड़ता है। कोड में स्पष्ट { } लगाने से यह भ्रम पूरी तरह समाप्त हो जाता है।

4. Common Compilation and Logic Pitfalls (सामान्य संकलन और तार्किक त्रुटियाँ)

[EN] Two classic bugs frequently appear in competitive exam code-tracing questions involving 'if' statements. First, placing an accidental semicolon immediately after the condition—'if (x > 10); { System.out.println(x); }'—compiles cleanly because ';' represents an empty null statement; the subsequent { ... } block becomes an independent unconditional block that always executes! Second, writing assignment '=' instead of equality '==' on a boolean variable—'boolean flag = false; if (flag = true)'—compiles cleanly because an assignment expression returns the assigned value (true), executing the 'if' branch unexpectedly. Defensive coding styles place constants on the left when checking literals or avoid '== true' comparisons altogether (write 'if (flag)' or 'if (!flag)').

[HI] परीक्षाओं के कोड-ट्रेसिंग प्रश्नों में 'if' से जुड़े दो प्रमुख जाल अक्सर पूछे जाते हैं। पहला: शर्त के ठीक बाद गलती से सेमीकोलन लगाना—'if (x > 10); { System.out.println(x); }'—यह बिना त्रुटि के कंपाइल हो जाता है क्योंकि ';' एक खाली स्टेटमेंट बन जाता है और उसके बाद का { ... } ब्लॉक हमेशा निष्पादित होता है। दूसरा: boolean वेरिएबल पर '==' के बजाय '=' लिखना—'boolean flag = false; if (flag = true)'—चूंकि असाइनमेंट अभिव्यक्ति असाइन किया गया मान (true) लौटाती है, इसलिए कोड कंपाइल होकर हर बार 'if' ब्लॉक चला देता है। सुरक्षित कोड में सीधे 'if (flag)' या 'if (!flag)' लिखना चाहिए।

Syntax, Formula & Technical Example (Control Statements: if, if-else, nested if in Java)

public class ControlIfDemo {
    public static void main(String[] args) {
        int age = 22; boolean hasId = true;
        if (age >= 18) {
            if (hasId) System.out.println("Entry permitted.");
            else System.out.println("ID card required.");
        } else System.out.println("Underage: entry denied.");
    }
}

Control Statements: if, if-else, nested if in Java Quick Reference & Comparison Table

Conditional ConstructExecution RuleKey Exam Fact
Simple ifExecutes body only when boolean test is trueRejects integer conditions like if(1) at compile time
if-elseTwo-way mutually exclusive branch executionCannot have an orphan else without a preceding if
if-else-if LadderTop-down sequential test until first matchRemaining conditions are skipped once a match runs
Nested ifInner conditional placed inside outer if/elseDangling else binds to nearest unmatched preceding if

Control Statements: if, if-else, nested if in Java Top 20 Bilingual MCQs with Answers

Q1. What happens if you compile: int x = 1; if (x) { System.out.println("Yes"); }

(int x = 1; if (x) { System.out.println("Yes"); } को कंपाइल करने पर क्या होगा?)

(A) Prints "Yes"

(B) Throws ClassCastException at runtime

(C) Compile-time error: int cannot be converted to boolean

(D) Prints nothing at runtime

Correct Answer: (C)

[EN] Java strictly forbids integers inside if(...) conditions; only boolean or Boolean expressions are accepted.

[HI] जावा में if(...) की शर्त के अंदर पूर्णांक की अनुमति नहीं है; केवल boolean व्यंजक ही मान्य हैं।

Q2. What is the output of: int a = 5, b = 10; if (a > 10) if (b > 5) System.out.print("A"); else System.out.print("B");

(int a = 5, b = 10; if (a > 10) if (b > 5) System.out.print("A"); else System.out.print("B"); का आउटपुट क्या होगा?)

(A) A

(B) Prints nothing (empty output)

(C) Compile-time ambiguity error

(D) B

Correct Answer: (B)

[EN] By the dangling-else rule, 'else' binds to the inner 'if (b > 5)'. Since outer 'if (a > 10)' is false, neither the inner if nor its else ever executes.

[HI] डैंगलिंग-else नियम से 'else' आंतरिक 'if (b > 5)' से जुड़ता है। बाहरी 'if (a > 10)' false होने के कारण कुछ भी प्रिंट नहीं होता।

Q3. What is printed by: int x = 5; if (x > 20); { System.out.println("Run"); }

(int x = 5; if (x > 20); { System.out.println("Run"); } से क्या प्रिंट होगा?)

(A) Nothing is printed

(B) Run

(C) Compile-time syntax error due to semicolon after if

(D) Throws RuntimeException

Correct Answer: (B)

[EN] The semicolon immediately after if(x > 20) acts as an empty body for the 'if'. The subsequent { System.out.println("Run"); } is an independent block that always executes.

[HI] if(x > 20) के बाद लगा सेमीकोलन खाली स्टेटमेंट बन जाता है; उसके बाद का ब्लॉक सामान्य कोड की तरह हमेशा चलकर "Run" प्रिंट करता है।

Q4. What is the output of: boolean b = false; if (b = true) System.out.print("T"); else System.out.print("F");

(boolean b = false; if (b = true) System.out.print("T"); else System.out.print("F"); का आउटपुट क्या होगा?)

(A) T

(B) F

(C) Prints TF

(D) Compile-time error: assignment not allowed in if

Correct Answer: (A)

[EN] Because b is boolean, (b = true) is an assignment whose expression value is boolean true, causing the 'if' branch to execute and print "T".

[HI] चूंकि b एक boolean है, (b = true) असाइनमेंट का मान true लौटता है, जिससे 'if' ब्लॉक चलकर "T" प्रिंट करता है।

Q5. In an if-else-if ladder where THREE conditions are all true, how many branches execute?

(एक if-else-if लैडर में यदि तीन शर्तें एक साथ true हों, तो कितने ब्लॉक निष्पादित होंगे?)

(A) Only the first matching true branch executes

(B) The last matching branch executes

(C) All three true branches execute

(D) The default else branch executes

Correct Answer: (A)

[EN] An if-else-if ladder short-circuits at the very first true condition and skips all remaining else-if and else blocks.

[HI] if-else-if लैडर में पहली सत्य शर्त मिलते ही उसका ब्लॉक चलता है और शेष सभी else-if व else ब्लॉक छोड़ दिए जाते हैं।

Q6. What happens if an 'else' statement is placed directly after two statements inside an 'if' without curly braces: if(true) a=1; b=2; else c=3;

(बिना कर्ली ब्रेसिज़ के if के बाद दो स्टेटमेंट लिखकर else लगाने (if(true) a=1; b=2; else c=3;) पर क्या होगा?)

(A) Compiles with a warning

(B) b=2 runs inside else

(C) Both a=1 and b=2 belong to the if block

(D) Compile-time error: 'else' without 'if'

Correct Answer: (D)

[EN] Without { }, 'if(true) a=1;' terminates at the first semicolon. Therefore 'b=2;' sits outside the if, leaving the 'else' orphaned without a preceding 'if'.

[HI] बिना { } के if केवल पहले स्टेटमेंट 'a=1;' तक सीमित रहता है; अतः 'b=2;' के बाद 'else' बिना किसी 'if' के रह जाता है और कंपाइल त्रुटि आती है।

Q7. Which construct yields the exact behavior of 'if (false) { System.out.println("Hi"); }' when compiled with javac?

('if (false) { System.out.println("Hi"); }' को javac से कंपाइल करने पर क्या होता है?)

(A) Compile-time error: unreachable statement

(B) Throws AssertionError at runtime

(C) Prints Hi once

(D) Compiles without error and prints nothing

Correct Answer: (D)

[EN] Per JLS §14.22, 'if (false)' is deliberately allowed without an unreachable-statement error to support compile-time flag elimination (unlike 'while (false)').

[HI] JLS §14.22 के अनुसार 'if (false)' को कंपाइल-टाइम अनरीचेबल एरर से छूट दी गई है (जबकि 'while (false)' एरर देता है), इसलिए यह कंपाइल होकर कुछ प्रिंट नहीं करता।

Q8. What is the output of: int x = 10; if (x == 10) if (x < 5) System.out.print(1); else System.out.print(2);

(int x = 10; if (x == 10) if (x < 5) System.out.print(1); else System.out.print(2); का आउटपुट क्या होगा?)

(A) 2

(B) 12

(C) Nothing

(D) 1

Correct Answer: (A)

[EN] Outer if (x == 10) is true; inner if (x < 5) is false, so its matching inner else executes and prints 2.

[HI] बाहरी शर्त (x == 10) सत्य है; आंतरिक शर्त (x < 5) असत्य है, इसलिए आंतरिक else चलकर 2 प्रिंट करता है।

Q9. Which statement about the ternary operator (?:) vs if-else is correct?

(टर्नरी ऑपरेटर (?:) और if-else के बीच निम्नलिखित में से कौन सा कथन सही है?)

(A) Ternary ?: is an expression that returns a value, whereas if-else is a control statement

(B) if-else cannot be nested, whereas ?: can be nested

(C) if-else always requires an else clause, whereas ?: makes the third operand optional

(D) Ternary ?: executes faster only when used with strings

Correct Answer: (A)

[EN] Ternary ?: evaluates to a value usable inside assignments/arguments, whereas if-else directs statement flow and can omit 'else'.

[HI] टर्नरी ?: एक अभिव्यक्ति (expression) है जो मान लौटाती है, जबकि if-else एक कंट्रोल स्टेटमेंट है जिसमें else वैकल्पिक होता है।

Q10. What occurs if you write: Integer flag = null; if (flag) { System.out.println("OK"); }

(Integer/Boolean रैपर: Boolean flag = null; if (flag) { System.out.println("OK"); } लिखने पर क्या होगा?)

(A) Compiles cleanly (for Boolean flag) and throws NullPointerException at runtime during unboxing

(B) Compile-time error on Boolean inside if

(C) Prints OK

(D) Evaluates null as false and skips the block

Correct Answer: (A)

[EN] Note: if flag is 'Boolean flag = null', javac auto-unboxes via flag.booleanValue(), throwing NullPointerException at runtime (whereas 'Integer flag' fails at compile time).

[HI] यदि flag का प्रकार Boolean है और मान null है, तो रनटाइम पर ऑटो-अनबॉक्सिंग (flag.booleanValue()) के दौरान NullPointerException उत्पन्न होता है।

Q11. What is the output of: int n = 15; if (n % 3 == 0) System.out.print("A"); if (n % 5 == 0) System.out.print("B");

(int n = 15; if (n % 3 == 0) System.out.print("A"); if (n % 5 == 0) System.out.print("B"); का आउटपुट क्या होगा?)

(A) AB

(B) B

(C) A

(D) Nothing

Correct Answer: (A)

[EN] These are two independent 'if' statements (not an if-else-if ladder). Since 15 is divisible by both 3 and 5, both blocks execute and print AB.

[HI] ये दो स्वतंत्र 'if' स्टेटमेंट हैं (else-if लैडर नहीं); चूंकि 15, 3 और 5 दोनों से विभाज्य है, इसलिए दोनों चलकर AB प्रिंट करते हैं।

Q12. Which condition checks whether an integer 'year' is a leap year using nested/combined if logic?

(निम्नलिखित में से कौन सी शर्त सही ढंग से जांचती है कि 'year' एक लीप वर्ष (leap year) है?)

(A) year % 4 == 0 || year % 100 == 0

(B) (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

(C) year % 100 == 0 && year % 400 != 0

(D) year % 4 == 0

Correct Answer: (B)

[EN] A Gregorian year is a leap year if divisible by 4 and not 100, OR divisible by 400 (e.g., 1900 is not a leap year, 2000 is).

[HI] लीप वर्ष वह होता है जो 4 से विभाज्य हो और 100 से न हो, या फिर सीधे 400 से विभाज्य हो (जैसे 1900 नहीं है, 2000 है)।

Q13. In Java, which data types are allowed directly inside the parentheses of an 'if(...)' condition?

(जावा में 'if(...)' की शर्त के अंदर सीधे कौन से डेटा प्रकार अनुमत हैं?)

(A) boolean and java.lang.Boolean only

(B) Any non-null Object reference

(C) boolean, int, and String

(D) boolean and int

Correct Answer: (A)

[EN] Only primitive boolean and wrapper Boolean (auto-unboxed since Java 5) are legal inside an if condition.

[HI] if शर्त के भीतर केवल प्रिमिटिव boolean और रैपर Boolean (जो Java 5 से ऑटो-अनबॉक्स होता है) ही वैध हैं।

Q14. What happens if a 'return;' statement executes inside an 'if' block within a void method?

(किसी void मेथड के 'if' ब्लॉक के भीतर 'return;' चलने पर क्या होता है?)

(A) Exits only the if block and continues after else

(B) Throws IllegalStateException

(C) Immediately exits the entire enclosing method, skipping any following code

(D) Compile-time error because return cannot appear in void methods

Correct Answer: (C)

[EN] A plain 'return;' inside a void method immediately terminates execution of that method and returns control to its caller.

[HI] void मेथड में 'return;' तुरंत पूरे मेथड का निष्पादन समाप्त कर देता है और मेथड के आगे की सभी पंक्तियाँ छोड़ देता है।

Q15. What is the output of: int x = 3; if (x == 4) System.out.print(1); System.out.print(2);

(int x = 3; if (x == 4) System.out.print(1); System.out.print(2); का आउटपुट क्या होगा?)

(A) Nothing

(B) 2

(C) 12

(D) 1

Correct Answer: (B)

[EN] Without braces, only System.out.print(1) is controlled by if (x == 4). System.out.print(2) always runs unconditionally.

[HI] बिना कर्ली ब्रेसिज़ के केवल पहला कथन print(1) ही if के अधीन है; print(2) शर्त से स्वतंत्र होकर हमेशा चलता है।

Q16. Which statement about Java if / if-else control statements is FALSE?

(जावा में if / if-else कंट्रोल स्टेटमेंट्स के बारे में निम्नलिखित में से कौन सा कथन असत्य है?)

(A) Code indentation determines which 'if' owns an unbraced 'else' clause

(B) An 'if' statement does not strictly require a matching 'else'

(C) Conditions inside if-else-if ladders are evaluated top-to-bottom

(D) Multiple statements in an if body must be enclosed in curly braces { }

Correct Answer: (A)

[EN] Java ignores whitespace and indentation during parsing; grammar always binds an 'else' to the nearest preceding unmatched 'if'.

[HI] जावा कंपाइलर इंडेंटेशन को अनदेखा करता है; व्याकरण हमेशा 'else' को ठीक पहले वाले निकटतम अनमेच्ड 'if' से जोड़ता है।

Q17. Within Control Statements: if, if-else, nested if in Java, which conditional construct directly maps to: "Executes body only when boolean test is true"?

(Control Statements: if, if-else, nested if in Java में कौन सा Conditional Construct सीधे "Executes body only when boolean test is true" से संबंधित है?)

(A) Simple if

(B) if-else

(C) if-else-if Ladder

(D) Nested if

Correct Answer: (A)

[EN] In Control Statements: if, if-else, nested if in Java, 'Simple if' handles 'Executes body only when boolean test is true' (Rejects integer conditions like if(1) at compile time).

[HI] Control Statements: if, if-else, nested if in Java में 'Simple if' का कार्य/विवरण 'Executes body only when boolean test is true' (Rejects integer conditions like if(1) at compile time) है।

Q18. Which option accurately describes 'if-else' in Control Statements: if, if-else, nested if in Java?

(Control Statements: if, if-else, nested if in Java के संदर्भ में 'if-else' की सही विशेषता कौन सी है?)

(A) Top-down sequential test until first match

(B) Inner conditional placed inside outer if/else

(C) Executes body only when boolean test is true

(D) Two-way mutually exclusive branch execution

Correct Answer: (D)

[EN] 'if-else' provides 'Two-way mutually exclusive branch execution' — key point: Cannot have an orphan else without a preceding if.

[HI] 'if-else' का मुख्य विवरण 'Two-way mutually exclusive branch execution' है (Cannot have an orphan else without a preceding if)।

Q19. In Control Statements: if, if-else, nested if in Java, identify the correct parameter for the specification: "Top-down sequential test until first match".

(Control Statements: if, if-else, nested if in Java में "Top-down sequential test until first match" विवरण किस विकल्प के लिए सही है?)

(A) if-else-if Ladder

(B) Nested if

(C) if-else

(D) Simple if

Correct Answer: (A)

[EN] 'if-else-if Ladder' corresponds to 'Top-down sequential test until first match' (Remaining conditions are skipped once a match runs).

[HI] 'if-else-if Ladder' का संबंध 'Top-down sequential test until first match' (Remaining conditions are skipped once a match runs) से है।

Q20. Considering Control Statements: if, if-else, nested if in Java, what key behavior defines 'Nested if'?

(Control Statements: if, if-else, nested if in Java में 'Nested if' की मुख्य भूमिका या मान क्या है?)

(A) Two-way mutually exclusive branch execution

(B) Inner conditional placed inside outer if/else

(C) Executes body only when boolean test is true

(D) Top-down sequential test until first match

Correct Answer: (B)

[EN] 'Nested if' represents 'Inner conditional placed inside outer if/else' (Dangling else binds to nearest unmatched preceding if).

[HI] 'Nested if' का अर्थ/मान 'Inner conditional placed inside outer if/else' है (Dangling else binds to nearest unmatched preceding if)।

Control Statements: if, if-else, nested if in Java — Exam FAQs

Q1: Why doesn't Java allow 'if (1)' or 'if (ptr != NULL)' integers like C/C++?

जावा C/C++ की तरह 'if (1)' जैसे पूर्णांकों को if शर्त में अनुमति क्यों नहीं देता?

[EN] Allowing integers in conditions hides bugs such as writing 'if (x = 0)' instead of 'if (x == 0)'. Requiring a strict boolean type catches accidental assignment bugs at compile time and improves readability.

[HI] पूर्णांकों की अनुमति देने से 'if (x == 0)' के बजाय गलती से 'if (x = 0)' लिखने जैसी बग्स छुप जाती थीं। सख्त boolean नियम कंपाइल टाइम पर ही ऐसी त्रुटियों को पकड़ लेता है।

Q2: When should I choose an if-else-if ladder over a switch statement?

switch स्टेटमेंट के बजाय if-else-if लैडर का चुनाव कब करना चाहिए?

[EN] Use if-else-if whenever conditions involve relational ranges (score >= 90), floating-point values, boolean combinations (&& / ||), or non-constant runtime values, since traditional switch only matches discrete constants.

[HI] जब शर्तों में रेंज की तुलना (score >= 90), दशमलव मान, तार्किक संयोजन (&& / ||) या डायनामिक वेरिएबल मान हों, तब if-else-if उपयोग करना चाहिए क्योंकि पारंपरिक switch केवल निश्चित स्थिरांकों का मिलान करता है।

Q3: What is an 'early return' (guard clause) pattern with if statements?

if स्टेटमेंट के साथ 'अर्ली रिटर्न' (गार्ड क्लॉज़) पैटर्न क्या है?

[EN] Instead of deeply nesting multiple if-else blocks, guard clauses check invalid edge cases at the top of a method ('if (input == null) return;') and return early, keeping the main logic flat and readable.

[HI] गहरे नेस्टेड if-else लिखने के बजाय मेथड की शुरुआत में ही अमान्य स्थितियों ('if (input == null) return;') की जांच करके वापस लौटने से कोड सरल और पठनीय रहता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)