Friday, 4 June 2021

Switch statement follow through without break

 switch (option}{

    case 1:
    do A;
    case 2:
    do B;
    case 2:
    do C;
    break;  
    default:
    do C;
}

if your option is 1 it executes everything til it finds the break keyword... that mean break end the excution of the switch --> case Output :A then B then C so it is recommended to put break after each case like :

switch (option}{
        case 1:
        do A;
        break;
        case 2:
        do B;
        break;
        do C;
        break;        
        default:
        do D;
    }

if your option is 1 the Output will be : just A ....


https://stackoverflow.com/questions/33222437/switch-case-statement-without-break

No comments:

Post a Comment