Isn't it an example of spaghetti code condemned by general programming public?
I don't think the case statement is a good candidate when you need something very fast. If you want fast, you just create an array of addresses (labels) and then select the "goto" address from the array based on the "switch" variable. I guess that's spaghetti too
Yes, but it shows something that 'if else' can't do efficiently because the switch statement 'fallthrough' can emulate (computed) goto into the middle of a loop without saying the 'dirty' word within the switch input/output node. I would (almost) never use tricks like that with a good gnu type compiler but the 8 bit uC versions are a different animal.
The guys who created C liked spaghetti neat.
i have found switch to be very handy for event loop handlers, sub menus and multi-function buttons,
I have also found not using break is very helpful aswell, when used properly..
x=3;
switch (x){
case 1: y=2;break;
case 2: y=4;break;
case 3: y=6;
case 4: y=y++;break;
case 5: y=100; break;
i have found switch to be very handy for event loop handlers, sub menus and multi-function buttons,
I have also found not using break is very helpful aswell, when used properly..
x=3;
switch (x){
case 1: y=2;break;
case 2: y=4;break;
case 3: y=6;
case 4: y=y++;break;
case 5: y=100; break;
}
system will guarantee to output y as 7!!
I take good style and safety quite seriously, so I would suggest that when you leave out the break; at least comment it very well:
C:
x=3;
switch (x){
case 1: y=2;break;
case 2: y=4;break;
case 3: y=6; /* NO BREAK */
case 4: y=y++;break;
case 5: y=100; break;
}
I believe C is only language that requires the breaks in its switch-cases. If some Pascal programmer would edit your code, he might not understand what is going on and that is a dangerous trap.