How to optimize for-comprehensions and loops in Scala?

The problem in this particular case is that you return from within the for-expression. That in turn gets translated into a throw of a NonLocalReturnException, which is caught at the enclosing method. The optimizer can eliminate the foreach but cannot yet eliminate the throw/catch. And throw/catch is expensive. But since such nested returns are rare … Read more

Using getchar() in a while loop

Because you typed ‘a‘ and ‘\n‘… The ‘\n‘ is a result of pressing the [ENTER] key after typing into your terminal/console’s input line. The getchar() function will return each character, one at a time, until the input buffer is clear. So your loop will continue to cycle until getchar() has eaten any remaining characters from … Read more

Looping through find output in Bash where file name contains white spaces

Though Dennis Williamson’s answer is absolutely correct, it creates a subshell, which will prevent you from setting any variables inside the loop. You may consider using process substitution, as so: while IFS= read -d ” -r file; do grep ‘<image’ “$file” > /dev/null && echo “$file” | tee -a embeded_images.txt done < <(find people -name … Read more

Does Java recognize infinite loops?

I’ll just quote the Java Language Specification, as it’s rather clear on this: This section is devoted to a precise explanation of the word “reachable.” The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement … Read more

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?

They are equivalent, even if you turn the optimizer off. Example: #include <stdio.h> extern void f(void) { while(1) { putchar(‘ ‘); } } extern void g(void) { for(;;){ putchar(‘ ‘); } } extern void h(void) { z: putchar(‘ ‘); goto z; } Compile with gcc -O0 gives equivalent assembly for all 3 functions: f: ; … Read more