Working of switch construct in c programming

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

  • radha gogia
    radha gogia
    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
    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 Sharma
    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

You are reading an archived discussion.

Related Posts

If you thought that Microsoft’s next aim is only marketing Windows 10 and build apps for it, then they just proved you wrong. Microsoft has been working on and have...
Not all may be applicable to all locations. One can always come up with one's own ideas. https://www.instructables.com/id/My-Money-Saving-Habits/?ALLSTEPS
Open to Indians also. https://www.instructables.com/contest/exploresciencecontest/
Project Abstract / Summary : Google has developed a wearable computer with an optical head-mounted display (OHMD), the research and development project. Project Glass with the intension of producing a...
Project Abstract / Summary : OBJECTIVE : To design and assemble a pollution-less concept demonstrator model for extraction of solar energy trapped in a green house and to create awareness...