views:

668

answers:

6

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?

+18  A: 

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++) { ... }
  }
}
jsight
+1 you beat me to it.
akf
that is what I wanted. works on C/C++ also. thank you! if I could I would give +1 ;)
fjsj
The answer works, but using anonymous blocks like that is an unusual style, I would not use that in my programs.
Jesper
I encounter this limitation very frequently, for example when I need to transform an Iterator or other sequential structure based on index. Init clause of for loop is a good place to put variables that are needed only in the context of that loop. For example, this would be a good loop to populate random array:for (int i = 0, final Random random = new Random(); i < N; ++i) s[i] = random.nextInt();
Shooshpanchick
A: 

Just guessing - it is so simple to declare "long variable" inside the loop, and since Java is aimed at simplicity, the designers might not feel it is necessary to add support for what you want.

GreenieMeanie
This will scope the variable per iteration, not per loop
Jesse
As Jesse said, I'm interested in loop scope. I don't want to lose variable value on every iteration.
fjsj
+2  A: 

Well, the syntax is for(expr;expr;expr) and you can't declare two variables of different types in one expression. So it doesn't have much to do with loop variable.

Michael Krelin - hacker
Nope, this is legal in Java:for (int i = 0, j = 5; i < j; i++, j+=2) {...}
Sean Owen
Um, I take that back. I think I misread your comment.
Sean Owen
A: 

According to the specification, the initialization of the for statement can be a list of statement expressions which are evaluated and discarded, or a single variable declaration statement. A variable declaration statement can declare multiple variables, but they all are of the same type.

So you could do this:

for (int i = 0, variable = obj.operation(); i < 15; i++) { ... }

But variable would be defined as an int in this case.

mattwright
I'm aware of that, but what I want is to declare more than one variable with different types.
fjsj
+1  A: 

@jsight is right, but I think the real reason is syntactic simplicity. Suppose that Java allowed either a type or a new variable name following the first comma. Since Java doesn't require forward declaration, an identifier following the comma could be a new variable name, an existing class name of the name of a Class that has not been encountered before. Now it should be possible to deal with this, but:

  1. It makes the grammar more complex, possibly in ways that are not immediately obvious.
  2. It makes the parse phase of the Java compiler more complex.
  3. It makes live more complicated for other tools that process Java source code.
  4. It probably results in more vague and/or misleading compiler error messages if there is a syntax error in a for loop.

(IMO, syntactic complexity is one of the failings of C and especially C++. I've done a lot of coding in both, but I still find the syntax error messages hard to decode at times.)

Stephen C
+1  A: 

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.

P Daddy