Quote:
Originally Posted by murraypaul
Clearly you are both wrong, and the only possible correct answer is this:
switch( current_state )
{
case 0: set_lights_green(); break;
case 1: set_lights_red(); break;
default: set_lights_blue();
}

|
For safety, I'd suggest adding a 'break' at the end of the default case - just in case somebody adds another case to the switch and puts it at the end, without noticing the lack of break on the default:
switch( current_state )
{
case 0: set_lights_green(); break;
case 1: set_lights_red(); break;
default: set_lights_blue(); // oops, no break - falls through
case 2: set_lights_yellow(); break;
}
You could (reasonably) argue that putting the 'case 2' after the default is messy, but best to protect against it just in case.