Branching: If-Else, Switch

if-else કોઈપણ true/false condition પર decide કરે છે, switch એક matching case પર jump કરે છે: BookBridge નું menu અહીં action pick કરે છે, અને 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 or deny, fine or no fine, valid or invalid.

Java તમને 2 branching tools આપે છે: if-else, જે કોઈપણ condition ને test કરે છે, અને switch, જે matching case પર સીધું jump કરે છે. right one ને choose કરવું એ અડધા marks છે.

Formula

2 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 સાથે choosing

public class Menu {
    public static void main(String[] args) {
        int choice = 2;   // ધારો કે librarian એ 2 type કર્યું
        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 actually શું કરી રહ્યું છે

switch એ if-else ladder ની જેમ cases ને one by one test નથી કરતો. એ matching case label પર સીધું jump કરે છે અને ત્યાંથી execute કરવાનું start કરે છે, નીચે તરફ, જ્યાં સુધી એ break અથવા switch ના end ને ન મળે.

એ downward slide ને fall-through કહેવાય છે. breaks સાથે, choice 2 ફક્ત "Return book" print કરે છે. એને delete કરો, અને execution case 2 પર match થઈને 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 પર lies કરે છે. 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 ના handful સાથે 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

Old friends, one upgrade

Syntax જાણી જોઈને BCA104 ના C જેવું જ છે: તમારું if-else અને switch knowledge line for line transfer થાય છે. Java 2 things add કરે છે જે remember કરવા જેવા છે: if (x = 0) એ compile error છે silent bug ને બદલે (condition boolean હોવી જોઈએ), અને Java 7 થી switch String પર work કરી શકે છે, એટલે switch (command) case "issue" અને case "return" સાથે legal Java છે, C માં impossible. BookBridge નું menu loop very next lesson માં returns કરે છે.

Summary

Key takeaways

  • if-else boolean conditions ને test કરે છે અને ranges અને compound logic ને handle કરે છે.
  • switch single expression ના one matching case પર jump કરે છે: byte, short, char, int, enum અથવા String (Java 7+), ક્યારેય boolean, long, float, double નહીં.
  • break વગર, execution every following case અને default માં fall through થાય છે.
  • case labels compile-time constants હોવા જોઈએ; default unmatched everything ને catch કરે છે.
  • Rule of thumb: discrete fixed values, switch; ranges અથવા combined conditions, if-else.
  • Java if (x = 0) ને refuse કરે છે: conditions boolean હોવી જોઈએ, C ના classic = vs == bug ને kill કરે છે.
  • Memory hook: switch jumps in, break jumps out.

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

Branching: If-Else, Switch · Java Programming Language · Gri-Learn