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

How to keep associative array order?

As already pointed out, there is no mistake. Associative arrays are stored in a ‘hash’ order. If you want ordering, you don’t use associative arrays. Or, you use a non-associative array as well as an associative array. Keep a second (non-associative) array that identifies the keys in the order that they’re created. Then step through … Read more

Proper way to release resources with defer in a loop?

Execution of a deferred function is not only delayed, deferred to the moment the surrounding function returns, it is also executed even if the enclosing function terminates abruptly, e.g. panics. Spec: Defer statements: A “defer” statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function … Read more

Insert elements from one array (one-at-a-time) after every second element of another array (un-even zippering)

This example will work regardless of the $a and $b array size. <?php $a = [‘A1’, ‘A2’, ‘A3’, ‘A4’, ‘A5’]; $b = [‘BB1’, ‘BB2’, ‘BB3’, ‘BB4’, ‘BB5’]; for ($i = 0; $i < count($b); $i++) { array_splice($a, ($i+1)*2+$i, 0, $b[$i]); } echo “<pre>” . print_r($a, true) . “</pre>”; Output of this example is : Array … Read more

Java compressing Strings

Loop through the string remembering what you last saw. Every time you see the same letter count. When you see a new letter put what you have counted onto the output and set the new letter as what you have last seen. String input = “AAABBBBCC”; int count = 1; char last = input.charAt(0); StringBuilder … Read more

Mixing files and loops

You get the ValueError because your code probably has for line in original: in addition to original.readline(). An easy solution which fixes the problem without making your program slower or consume more memory is changing for line in original: … to while True: line = original.readline() if not line: break …

Add a delay after executing each iteration with forEach loop

What you want to achieve is totally possible with Array#forEach — although in a different way you might think of it. You can not do a thing like this: var array = [‘some’, ‘array’, ‘containing’, ‘words’]; array.forEach(function (el) { console.log(el); wait(1000); // wait 1000 milliseconds }); console.log(‘Loop finished.’); … and get the output: some array … Read more