Am Dienstag, 15. April 2003 05:26 schrieb Raúl Gutiérrez Segalés: [...]
It's horrible. You mean this:
It is very horrible. [...]
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..
looping = 1; while (condition) { switch (any_var) { case 1: break; case 2: break; case xy: looping = 0; break; default: looping = 0; break; } if (looping == 0) { do_something(); #ifdef SKIP break; #endif } In this piece of code you set the variable looping to 0 when you want something else to be proceeded. The If-clause has to be right under the switch block. When looping is 0 it does the code you want to do and stays in the enclosing while loop. If you want to leave the while loop you can do that with the break;. You just have to define SKIP somewhere at the top of your file (or before it is used the first time). Greets Georg