Why Java, C and C++ (maybe other languages also) do not allow more than one type on for-loop variables? For example:
for (int i = 0; i < 15; i++)
in this case we have a loop variable i, which is the loop counter.
But I may want to have another variable which scope is limited to the loop, not to each iteration. For example:
for (int i = 0, variable = obj.operation(); i < 15; i++) { ... }
I'm storing obj.operation()
return data in variable
because I want to use it only inside the loop. I don't want variable
to be kept in memory, nor stay visible after the loop execution. Not only to free memory space, but also to avoid undesired behaviour caused by the wrong use of variable
.
Therefore, loop variables are useful, but aren't extensively supported because of its type limitation. Imagine that operation()
method returns a long value. If this happens, I can't enjoy the advantages of loop variables without casting and losing data. The following code does not compile in Java:
for (int i = 0, long variable = obj.operation(); i < 15; i++) { ... }
Again, anybody know why this type limitation exists?
Often the variable that controls a for loop is needed only for the purposes of the loop and is not used elsewhere. When this is the case, it is possible to declare the variable inside the initialization portion of the for.
In Java, there are three types of loops.
Yes, I can declare multiple variables in a for-loop. And you, too, can now declare multiple variables, in a for-loop, as follows: Just separate the multiple variables in the initialization statement with commas. Do not forget to end the complete initialization statement with a semicolon.
In Java, multiple variables can be initialized in the initialization block of for loop regardless of whether you use it in the loop or not.
This limitation exists because your requirement is fairly unusual, and can be gained with a very similar (and only slightly more verbose) construct. Java supports anonymous code blocks to restrict scope if you really want to do this:
public void method(int a) {
int outerVar = 4;
{
long variable = obj.operation();
for (int i = 0; i < 15; i++) { ... }
}
}
Your example:
for (int i = 0, long variable = obj.operation(); i < 15; i++) { ... }
is illegal for the same reason that:
int i = 0, long variable = obj.operation();
would be illegal by itself. The comma doesn't start a new statement. Both parts, before and after the comma, are part of one statement. This statement is declaring and initializing a list of int
variables. Well, that's what the int
identifier at the start of the line tells the compiler, anyway. The long
identifier after the comma is an error, as to declare variable(s) of a different type, you must start a new statement.
Since you cannot declare variables of two different types in one statement, you must declare one of them outside of the for
initializer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With