Yes, it is necessary to include a break
or a return
after every switch case.
An example where it is usefull, that not every case has an automatical break, is when you get key-events and certain keys should do the same thing:
switch (current_key)
{
case KEY_W:
case KEY_UP:
case KEY_SPACE:
player->jump();
break;
case KEY_S:
case KEY_DOWN:
case KEY_SHIFT:
player->cover();
break;
default:
//Do something
break;
}
and you may write a function for you code like this:
const char* make_x_to_string(int x)
{
switch (x)
{
case 0:
return "x is zero";
case 1:
return "x is one";
default:
return "x is neither zero or one";
}
}
and then simply call
cout << make_x_to_string(0) << endl;
You don't need break
there, because return
exits the function.