I want to jump from the middle of a switch statement, to the loop statement in the following code:
while (something = get_something())
{
switch (something)
{
case A:
case B:
break;
default:
// get another something and try again
continue;
}
// do something for a handled something
do_something();
}
Is this a valid way to use continue? Are continue statements ignored by switch statements? Do C and C++ differ on their behaviour here?
It’s fine, the
continuestatement relates to the enclosing loop, and your code should be equivalent to (avoiding such jump statements):But if you expect
breakto exit the loop, as your comment suggest (it always tries again with another something, until it evaluates to false), you’ll need a different structure.For example: