Does case-switch work like this?

This is called case fall-through, and is a desirable behavior. It allows you to share code between cases.

An example of how to use case fall-through behavior:

switch(blah)
{
case a:
  function1();
case b:
  function2();
case c:
  function3();
  break;
default:
  break;
}

If you enter the switch when blah == a, then you will execute function1(), function2(), and function3().

If you don’t want to have this behavior, you can opt out of it by including break statements.

switch(blah)
{
case a:
  function1();
  break;
case b:
  function2();
  break;
case c:
  function3();
  break;
default:
  break;
}

The way a switch statement works is that it will (more or less) execute a goto to jump to your case label, and keep running from that point. When the execution hits a break, it leaves the switch block.

Leave a Comment