Switch-case statements: break and default

switch सीधे मेल खाने वाले case पर कूदता है और break न हो तो नीचे के cases में चलता चला जाता है; कोई case मेल न खाए तो default चलता है।

5 min read · 11 cards · 5 checks

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


Theory

एक drink या 2?

User ने इस canteen menu पर 2 दबाया:

int choice = 2;

switch (choice) {

case 1: printf("Tea ");

case 2: printf("Coffee ");

case 3: printf("Juice ");

}

Programmer को एक drink की उम्मीद थी। Screen पर क्या आता है?

Think first

पहले आपका अंदाज़ा

जो print होगा, वह हूबहू type कीजिए।

Show the answer

Coffee Juice। switch case 2 पर कूदा, फिर चलता ही रहा: break के बिना मेल वाले case के नीचे का हर statement चलता है। इसे fall-through कहते हैं।

Theory

switch, break, default

switch (value) सीधे उस case label पर कूदता है जो value के बराबर हो। break; switch से बाहर ले जाता है; उसके बिना execution अगले case में चला जाता है। default: तब चलता है जब कोई case मेल न खाए, ladder के आख़िरी else की तरह।

Practical

सुधरा हुआ menu

#include <stdio.h>

int main() {
    int choice = 2;

    switch (choice) {
        case 1:
            printf("Tea\n");
            break;
        case 2:
            printf("Coffee\n");
            break;
        case 3:
            printf("Juice\n");
            break;
        default:
            printf("Invalid choice\n");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

कोई मेल नहीं

ऊपर के सुधरे हुए menu में choice 2 की जगह 7 है। जो print होगा, type कीजिए।

Show the answer

Invalid choice। 7 किसी case से मेल नहीं खाता, इसलिए default चलता है। हर menu में default रखिए।

Theory

case क्या हो सकता है

Case label एक integer constant होना चाहिए, जैसे case 2: या case 'y': (char एक number के रूप में store होता है)। case marks >= 35: illegal है, क्योंकि marks एक variable है। Float पर switch भी नहीं हो सकता।

Think first

Illegal lines पकड़िए

इनमें से 2 compile नहीं होंगी। उनके letters type कीजिए।

  • A: case 'Y':
  • B: case 3 + 1:
  • C: case marks >= 35:
  • D: switch (f), जहाँ f एक float है
Show the answer

C और D। 'Y' (89 के रूप में store) और 3 + 1 (जिसे compiler 4 निकाल लेता है) constants हैं। C में variable marks है; D एक float पर switch करता है।

Think first

सही औज़ार चुनिए

switch या if-else ladder? (a) Day 1 से 7 के लिए weekday का नाम print करना। (b) Age के हिसाब से Child, Adult या Senior citizen print करना।

Show the answer

(a) switch: 7 exact values। (b) if-else ladder: हर age group एक range है, जिसे case label नहीं जाँच सकता।

Practical

Exam-style: output बताइए

#include <stdio.h>

int main() {
    char grade = 'B';

    switch (grade) {
        case 'A':
            printf("Excellent ");
        case 'B':
            printf("Good ");
        case 'C':
            printf("Pass ");
            break;
        default:
            printf("Fail ");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

Exam-style: ऊपर वाले program का output क्या है?

  1. Good Pass
  2. Good
  3. Excellent Good Pass
  4. Good Pass Fail
Show the answer

Good Pass

switch case 'B' पर कूदता है (Good), case 'C' में चला जाता है (Pass), और वहाँ का break switch ख़त्म कर देता है, इसलिए default कभी नहीं चलता।

Summary

Key takeaways

  • switch उस case पर कूदता है जो int या char value से मेल खाए।
  • break के बिना execution नीचे के cases में चला जाता है।
  • कोई case मेल न खाए तो default चलता है।
  • याद रखने का hook: exact values के लिए switch, ranges के लिए ladder।

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 Decision Making and Iterative statements

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