For vs. while in C programming?

A while loop will always evaluate the condition first. while (condition) { //gets executed after condition is checked } A do/while loop will always execute the code in the do{} block first and then evaluate the condition. do { //gets executed at least once } while (condition); A for loop allows you to initiate a … Read more

Timer & TimerTask versus Thread + sleep in Java

The advantage of TimerTask is that it expresses your intention much better (i.e. code readability), and it already has the cancel() feature implemented. Note that it can be written in a shorter form as well as your own example: Timer uploadCheckerTimer = new Timer(true); uploadCheckerTimer.scheduleAtFixedRate( new TimerTask() { public void run() { NewUploadServer.getInstance().checkAndUploadFiles(); } }, … Read more

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