In Java, you can declare a variable in the for -loop initialization part:
for ( int i=0; i < 10; i++) {
But with a while statement, this seems impossible.
Quite often, I see this code when the conditional value of the while loop should be updated after each iteration:
List<Object> processables = retrieveProcessableItems(..); // initial fetch while (processables.size() > 0) { // process items processables = retrieveProcessableItems(..); // update }
Here at stackoverflow, I found at least a solution to prevent code duplication to extract processed elements:
List<Object> processables; while ((processables = retrieveProcessableItems(..)).size() > 0) {
But the variable still needs to be declared outside the while loop.
So, since I want my variable areas to be clean, is it possible to declare a variable in a conditional expression, or is there some other solution for such cases?
source share