Branching: If-Else, Switch

if-else किसी भी true/false condition पर decide करता है, switch एक matching case पर jump करता है: BookBridge का menu यहाँ अपना action पिकता है, और missing break classic exam bug है।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Menu को Choose करना है

BookBridge librarian को इस तरह greet करता है:

1. Issue book 2. Return book 3. Exit

वह 2 type करती है। आपके code में कहीं, program को returning-a-book path लेना होगा और बाकी 2 को ignore करना होगा। हर useful program में ऐसे forks होते हैं: issue या deny, fine या no fine, valid या invalid।

Java आपको 2 branching tools देता है: if-else, जो कोई भी condition test करता है, और switch, जो सीधे एक matching case पर jump करता है। सही एक choose करना आधे marks है।

Formula

दो Syntaxes, Exam Form

if-else ladder:

if (condition) { ... }

else if (condition) { ... }

else { ... }

switch:

switch (expression) {

case value1: statements; break;

case value2: statements; break;

default: statements;

}

if में condition एक boolean होना चाहिए। switch expression byte, short, char, int, एक enum या (Java 7 से) एक String हो सकता है, कभी boolean, long, float या double नहीं।

Practical

BookBridge Menu, switch से Choose करते हुए

public class Menu {
    public static void main(String[] args) {
        int choice = 2;   // pretend the librarian typed 2
        switch (choice) {
            case 1:
                System.out.println("Issue book");
                break;
            case 2:
                System.out.println("Return book");
                break;
            case 3:
                System.out.println("Exit");
                break;
            default:
                System.out.println("Invalid choice");
        }
    }
}

Theory

break असल में क्या कर रहा है

एक switch cases को if-else ladder की तरह एक-एक करके test नहीं करता। यह matching case label पर सीधे jump करता है और वहाँ से execute करना शुरू करता है, नीचे की तरफ़, जब तक यह एक break न मिले या switch खत्म न हो।

उस downward slide को fall-through कहते हैं। breaks जगह पर होने से, choice 2 सिर्फ़ "Return book" print करता है। उन्हें delete कीजिए, और case 2 पर matched execution case 3 और default में flow करता रहता है। Occasionally fall-through deliberately इस्तेमाल होता है (cases group करना); usually यह एक bug है।

Quiz

int day = 2; switch (day) { case 1: System.out.print("A "); case 2: System.out.print("B "); case 3: System.out.print("C "); default: System.out.print("D"); } क्या print होता है?

  1. B
  2. B C D
  3. B C
  4. A B C D
Show the answer

B C D

कहीं कोई break नहीं है, तो case 2 पर jump करके और B print करने के बाद, execution case 3 (C) और default (D) में fall through करता है। Option A उन breaks को assume करता है जो वहाँ नहीं हैं। Option C भूल जाता है default भी fall-through path पर है। Option D को match case 1 से start होना पड़ेगा, पर day 2 है, तो A कभी print नहीं होता। Predict-the-output switch questions almost always exactly यही test करते हैं।

Think first

Fine के लिए कौन सा Tool?

BookBridge एक fine message compute करता है: 0 rupees "No fine" print करता है, 1 से 50 "Small fine" print करता है, 50 से ऊपर "See librarian" print करता है। Tap करने से पहले: switch या if-else, और क्यों?

Show the answer

if-else ladder। Middle rule एक range है (1 से 50), और case labels single constant values होने चाहिए; आप इस syllabus की Java में case 1..50 नहीं लिख सकते। Ranges और compound conditions (fine > 50 && !cleared) if-else territory हैं। switch तब इस्तेमाल कीजिए जब ONE variable कुछ fixed discrete values के against compare किया जाता है, जैसे menu का 1, 2, 3।

Watch out

3 Mark-Eaters

Forgotten break: number 1 switch bug; match के नीचे सब कुछ execute होता है।

Illegal Switch Types: एक boolean, long, float या double पर switch compile नहीं होता। अगर पूछा जाए "क्या हम एक boolean पर switch कर सकते हैं?", answer no है, if-else इस्तेमाल कीजिए।

Non-Constant Case: case x: जहाँ x एक variable है एक compile error है; case labels compile-time constants होने चाहिए।

Theory

पुराने Friends, एक Upgrade

Syntax deliberately BCA104 के C जैसा ही है: आपकी if-else और switch knowledge line for line transfer होती है। Java 2 चीज़ें add करता है जो याद रखने लायक हैं: if (x = 0) एक silent bug के बजाय एक compile error है (condition boolean होना चाहिए), और Java 7 से एक switch एक String पर काम कर सकता है, तो switch (command) case "issue" और case "return" के साथ legal Java है, C में impossible। BookBridge का menu loop अगले lesson में वापस आता है।

Summary

Key takeaways

  • if-else boolean conditions test करता है और ranges और compound logic handle करता है।
  • switch एक single expression के एक matching case पर jump करता है: byte, short, char, int, enum या String (Java 7+), कभी boolean, long, float, double नहीं।
  • break के बिना, execution हर following case और default से fall through होता है।
  • case labels compile-time constants होने चाहिए; default unmatched सब कुछ catch करता है।
  • Rule of thumb: discrete fixed values, switch; ranges या combined conditions, if-else।
  • Java if (x = 0) से मना करता है: conditions boolean होने चाहिए, C का classic = बनाम == bug killing करते हुए।
  • Memory hook: switch अंदर jump करता है, break बाहर jump करता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Java

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati