🟡 Control Flow  ·  Lesson 11

Switch Case

Switch Case

The switch Statement

switch chooses one block from many based on a value — cleaner than long if-else chains.

Example

int day = 3;
switch (day) {
    case 1: System.out.println("Monday"); break;
    case 2: System.out.println("Tuesday"); break;
    case 3: System.out.println("Wednesday"); break;
    default: System.out.println("Other");
}
Wednesday

Don't Forget break

Without break, execution "falls through" to the next case. Always add break unless you want fall-through.

Summary

  • switch picks a case by value; break stops fall-through.
  • default runs when no case matches.

switch Statement

switch एक value के आधार पर कई में से एक block चुनता है — लंबी if-else chains से साफ।

Example

int day = 3;
switch (day) {
    case 1: System.out.println("Monday"); break;
    case 2: System.out.println("Tuesday"); break;
    case 3: System.out.println("Wednesday"); break;
    default: System.out.println("Other");
}
Wednesday

break मत भूलें

break बिना execution अगले case में "fall through" हो जाता है। fall-through न चाहें तो हमेशा break लगाएं।

सारांश

  • switch value से case चुनता है; break fall-through रोकता है।
  • कोई case match न हो तो default चलता है।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n