Break out of a while loop that contains a switch statement
I’d try to avoid it, but you could use… goto However, angry mobs with pitchforks become an occupational hazard if you choose to do so.
I’d try to avoid it, but you could use… goto However, angry mobs with pitchforks become an occupational hazard if you choose to do so.
If you read the comments at top of the file, just below the class declaration, there is a section which explains the use of this construct: Style notes =========== […] There are several occurrences of the unusual “do {} while (!cas…)” which is the simplest way to force an update of a CAS’ed variable. There … Read more
The correct options are (in increasing order of recommendation): # Single POSIX test command with -o operator (not recommended anymore). # Quotes strongly recommended to guard against empty or undefined variables. while [ “$stats” -gt 300 -o “$stats” -eq 0 ] # Two POSIX test commands joined in a list with ||. # Quotes strongly … Read more
Try this: import time t_end = time.time() + 60 * 15 while time.time() < t_end: # do whatever you do This will run for 15 min x 60 s = 900 seconds. Function time.time returns the current time in seconds since 1st Jan 1970. The value is in floating point, so you can even use … Read more
Semantically, they’re completely equivalent. It’s a matter of taste, but I think while(true) looks cleaner, and is easier to read and understand at first glance. In Java neither of them causes compiler warnings. At the bytecode level, it might depend on the compiler and the level of optimizations, but in principle the code emitted should … Read more
You can label your while loop, and break the labeled loop, which should be like this: loop: while(sc.hasNextInt()){ typing = sc.nextInt(); switch(typing){ case 0: break loop; case 1: System.out.println(“You choosed 1”); break; case 2: System.out.println(“You choosed 2”); break; default: System.out.println(“No such choice”); } } And the label can be any word you want, for example … Read more
If you want to implement a while loop, you will need to use recursion in the preprocessor. The easiest way to do recursion is to use a deferred expression. A deferred expression is an expression that requires more scans to fully expand: #define EMPTY() #define DEFER(id) id EMPTY() #define OBSTRUCT(id) id DEFER(EMPTY)() #define EXPAND(…) __VA_ARGS__ … Read more
UPDATE: This question was inspiration for my article on this subject. Thanks for the great question! “continue” and “break” are nothing more than a pleasant syntax for a “goto”. Apparently by giving them cute names and restricting their usages to particular control structures, they no longer draw the ire of the “all gotos are all … Read more
There is a discrepancy between the two examples. The first will execute the “do something” at least once every time even if the statement is never true. The second will only “do something” when the statement evaluates to true. I think what you are looking for is a do-while loop. I 100% agree that while … Read more
How to do a loop against stdin and get result stored in a variable Under bash (and other shell also), when you pipe something to another command via |, you will implicitly create a fork, a subshell that is a child of current session. The subshell can’t affect current session’s environment. So this: TOTAL=0 printf … Read more