Working of switch construct in c programming

radha gogia
@radha-BTDzli โ€ข Oct 26, 2024

In a switch construct ,why does the compiler doesn't show any error and skips the statements written within the cases like for example I write printf("hello"); in between case 1 and case 2 ,so why is there no error in this ,and how come the compiler skips this step

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • radha gogia

    @radha-BTDzli • Feb 20, 2015

    For example I write
    case 1: int a=9; break;
    printf("hellooo");
    case 2 : int y =98;
    printf("kill");
    break;
    So here when parsing is done so either the compiler should throw any error because of printf statement or it must print the statement which is written between the two cases but it skips it ,so why does it happen?

  • shiva6

    @shiva6-6QW9Ti • Feb 22, 2015

    Because the compiler never reaches that printf statement. When it encountered the break statement, it just skips all the remaining lines of code in that switch block and goes to the next immediate line after the switch block. That is how the break statement works.

  • Neeraj Sharma

    @neeraj-iAaNcG • Feb 24, 2015

    You have simply broken the construct here:

    case 1: int a=9; break;

    break statement breaks the switch construct thereon and comes out of that construct