How can I make sense of the `else` clause of Python loops?

An if statement runs its else clause if its condition evaluates to false. Identically, a while loop runs the else clause if its condition evaluates to false. This rule matches the behavior you described: In normal execution, the while loop repeatedly runs until the condition evaluates to false, and therefore naturally exiting the loop runs … Read more

In Java, what are the advantages of streams over loops? [closed]

Interesting that the interview question asks about the advantages, without asking about disadvantages, for there are are both. Streams are a more declarative style. Or a more expressive style. It may be considered better to declare your intent in code, than to describe how it’s done: return people .filter( p -> p.age() < 19) .collect(toList()); … Read more

Emulating a do-while loop in Bash

Two simple solutions: Execute your code once before the while loop actions() { check_if_file_present # Do other stuff } actions #1st execution while [ current_time <= $cutoff ]; do actions # Loop execution done Or: while : ; do actions [[ current_time <= $cutoff ]] || break done

foreach vs someList.ForEach(){}

There is one important, and useful, distinction between the two. Because .ForEach uses a for loop to iterate the collection, this is valid (edit: prior to .net 4.5 – the implementation changed and they both throw): someList.ForEach(x => { if(x.RemoveMe) someList.Remove(x); }); whereas foreach uses an enumerator, so this is not valid: foreach(var item in … Read more

Should try…catch go inside or outside a loop?

PERFORMANCE: There is absolutely no performance difference in where the try/catch structures are placed. Internally, they are implemented as a code-range table in a structure that is created when the method is called. While the method is executing, the try/catch structures are completely out of the picture unless a throw occurs, then the location of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)