Ok, very nice BUT... if I have something done after the switch that I want to skip...
while(condition) { char z; int first=0;
z=getchar(); getchar();
switch(z) { case 'a': first=1; break; case 'z': goto end; /* cause I dont want it tu print the message */ break; } if(first) /* I dont want spend CPU cycles with this, just to jump over it.. */ puts("You typed the first letter of the alphabet"); }
I know this is pretty ugly code and could be fixed in other ways, but I can assure the there are situations that look like this.. so what would you suggest for replacing goto here, if you have a very strong need to skip some part of you loop..
Well, you'd put the "puts("You typed... line inside the case block! I think what you've written above is contrived. You're saying: check condition 1 do something1 else check condition 2 do something2 else check conditon 3 do something3 check condition 1 do something else Just put "something else" in with "something1". I can't remember when I've ever seen a piece of code like that which you have above. If I did come across one, I'd structure it like you have above, with an extra condition. You're talking about a tiny number of clock cycles to check the extra condition, versus the cost in readability of jumping over a block of code. Jumping over a line or two, as in the little example, is OK, but when the code is expanded you might end up jumping over a large block. That's the problem with using gotos: they tend to spread out and multiply as the code develops. Don't get me wrong, I'm not religious about the issue. I've used gotos before, where I consider them necessary. But they tend to produce slower code, they make code harder to format because they break logical structure, and their use promotes violation of structured programming principles. I don't consider them evil, but I don't use them unless there's a really good reason. -- "...our desktop is falling behind stability-wise and feature wise to KDE ...when I went to Mexico in December to the facility where we launched gnome, they had all switched to KDE3." - Miguel de Icaza, March 2003