Switch-case statements: break and default

switch સીધો એ case પર કૂદે છે જે કોઈ int કે char value સાથે મેળ ખાય, break execution ને આગલા case માં પડતું અટકાવે, અને default દરેક વગર-મેળ વાળાને પકડે, menus માટે ખાસ બનેલું ઓજાર.

10 min read · 9 cards · 2 checks

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


Theory

Menu વાળી સમસ્યા

Marks program હવે એક app બનતો જાય છે, અને apps માં menus હોય છે:

1. View marks

2. View percentage

3. View grade

4. Exit

એક if-else ladder એને સંભાળી શકે: if choice 1 છે... else if 2... પણ એ સરકારી કાગળ જેવી વંચાય છે, અને દરેક rung choice ને ફરી ચકાસે છે.

"સીધા MARA option પર કૂદો" માટે C એક ખાસ બનેલું ઓજાર રાખે છે: switch-case, એના 2 જાણીતા સાથી break અને default સાથે.

Theory

Lift, દાદર નહીં

if-else ladder એ દાદર છે: તમારા માળથી નીચેના દરેક માળ પસાર કરો, દરેક પર એક સવાલનો જવાબ આપતાં. switch એ lift છે: 3 દબાવો, સીધા માળ 3 પર પહોંચો. પણ આ lift ની એક લાક્ષણિકતા છે: એના દરવાજા તમારી પાછળ બંધ થતા નથી. જ્યાં સુધી તમે stop button (break) ન દબાવો, તમે ચાલતાં માળ 4 માં, પછી 5 માં ઘૂસી જાઓ... અને જે button કોઈ માળ સાથે મેળ ન ખાય, એના માટે lift પાસે એક બચાવ-landing છે: default.

Practical

Marks નું menu

#include <stdio.h>

int main() {
    int choice, marks = 78;
    printf("1.Marks 2.Percentage 3.Grade : ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Marks: %d\n", marks);
            break;
        case 2:
            printf("Percentage: %.2f\n", marks * 100.0 / 100);
            break;
        case 3:
            printf("Grade: %c\n", marks >= 70 ? 'A' : 'B');
            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.

Theory

Lift ના નિયમો

  • switch (expression): expression એક integer type હોવું જોઈએ (int કે char; chars ચાલે કારણ કે એ ASCII numbers છે). ક્યારેય float નહીં, અને ક્યારેય string નહીં.
  • case CONSTANT:: labels constants હોવા જોઈએ: case 1:, case 'A':. case marks >= 35: જેવી condition illegal છે, switch VALUES સાથે મેળ ખાય છે, ranges ચકાસતું નથી.
  • break;: switch માંથી બહાર નીકળે છે. એના વગર execution આગલા case ના code માં પડી જાય છે, matching અટકે પણ ચાલવાનું નહીં.
  • default:: ત્યારે ચાલે જ્યારે કંઈ મેળ ન ખાય, C નું "આમાંનું કોઈ નહીં". જગ્યા લવચીક છે; અંતે રાખવું એ રિવાજ છે.

Quiz

Menu code માં ધારો કે case 1 માં break નહોતું. User 1 નાખે છે. શું print થાય છે?

  1. Marks ANE Percentage: execution case 2 માં એના break સુધી પડે છે
  2. ફક્ત Marks: cases આપોઆપ પૂરા થાય છે
  3. Compile error: break ફરજિયાત છે
  4. Invalid choice
Show the answer

Marks ANE Percentage: execution case 2 માં એના break સુધી પડે છે

Matching એક વાર થાય છે, પણ break વગર code આગલા case ના statements માં CHALTU રહે છે, ફક્ત case 2 ના break પર અટકે છે. Fall-through legal C છે (ક્યારેક જાણી જોઈને વપરાય છે), એટલે compiler ચૂપ રહે છે અને એટલે જ break-ભૂલીને-predict-output એ દુનિયાનો સૌથી વારંવાર પુછાતો switch સવાલ છે.

Think first

switch grade કેમ ન આપી શકે

એક student પ્રયત્ન કરે છે: switch (marks) { case marks >= 70: ... }. એ compile નહીં થાય. Lift વાળા ચિત્ર પરથી તર્ક કરો કે KEM, અને grading નું સાચું ઓજાર કહો.

Show the answer

Lift ના buttons નક્કી માળ નંબરો છે, સવાલ નહીં: switch પોતાની value ને CONSTANT labels સાથે સરખાવે છે, એટલે case marks >= 70: (એક condition, અને constant નહીં) illegal છે. Ranges એ દાદરનું કામ છે: ગયા lesson ની if-else ladder grade આપે છે; switch menu આપે છે. Exam માટે એક line: switch exact values સાથે મેળ ખાય; if ranges અને conditions સંભાળે.

Watch out

Marks ક્યાં કપાય છે

ભૂલેલા breaks: એ જ અમર fall-through, predict-output ના સવાલોમાં દરેક case trace કરો. float પર switch (switch (percentage)): illegal, ફક્ત integer types. conditions ને case labels બનાવવા: illegal, ફક્ત constants. અને menu programs માં default ન ભૂલો; examiners invalid-input handling શોધે છે, અને એ એક line ના સહેલા marks છે.

Theory

2 selectors માંથી પસંદગી

તમારી decision toolkit હવે પૂરી છે: ranges માટે ladder, menus માટે switch, અને બંને ખરા programs માં સાથે દેખાય છે (એક switch menu જેના case 3 માં grading ladder હોય, બરાબર આજના code જેવું). જે બંને ન કરી શકે એ છે પુનરાવર્તન: 60 students process કરવા એટલે 60 વાર manual run. પુનરાવર્તન, structured-programming નો ત્રીજો આકાર, હવે પછી આવે છે: while, do-while અને for.

Summary

Key takeaways

  • switch સીધો એ case પર કૂદે છે જે કોઈ int/char value સાથે મેળ ખાય.
  • case labels ફક્ત constants છે: ન floats, ન conditions, ન ranges.
  • break switch માંથી બહાર નીકળે; એના વગર execution આગલા case માં PADI જાય.
  • default વગર-મેળ વાળી values પકડે, આમાંનું-કોઈ-નહીં વાળી branch.
  • switch exact-match menus માટે; if-else ladder ranges માટે.
  • યાદ રાખવાની યુક્તિ: એ lift જેના દરવાજા જાતે ક્યારેય બંધ થતા નથી.

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

Switch-case statements: break and default · Computer Programming and Programming Methodology (CPPM) · Gri-Learn