Java Reference
In-Depth Information
int i = START;
do {
count++;
} while (i++ != END);
Given the paramount importance of clarity and simplicity, it is almost always better to use a long
index under these circumstances, with perhaps one exception: If you are iterating over all (or nearly
all) the int values, it's about twice as fast to stick with an int . Here is an idiom to apply a function
f to all four billion int values:
// Apply the function f to all four billion int values
int i = Integer.MIN_VALUE;
do {
f(i);
} while (i++ != Integer.MAX_VALUE);
The lesson for language designers is the same as that of Puzzle 3 : It may be worth considering
support for arithmetic that does not overflow silently. Also, it may be worth providing support for
loops designed specifically to iterate over ranges of integral values, as many languages do.
< Day Day Up >
 
 
Search WWH ::




Custom Search