View Single Post
Old 02-02-2021, 10:25 AM   #39
jbjb
Somewhat clueless
jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.jbjb ought to be getting tired of karma fortunes by now.
 
Posts: 773
Karma: 9999999
Join Date: Nov 2008
Location: UK
Device: Kindle Oasis
Quote:
Originally Posted by murraypaul View Post
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.
jbjb is offline   Reply With Quote