Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type limitation in loop variables in Java, C and C++

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?

like image 285
fjsj Avatar asked Aug 26 '09 21:08

fjsj


People also ask

Can you declare variables in a for loop Java?

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.

How many types of loops are there in Java?

In Java, there are three types of loops.

Can we use two variables in for loop?

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.

Can you have multiple variables in a for loop Java?

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.


2 Answers

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++) { ... }
  }
}
like image 174
jsight Avatar answered Oct 06 '22 01:10

jsight


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.

like image 43
P Daddy Avatar answered Oct 06 '22 01:10

P Daddy