Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I define a variable inside a for loop in this way? [duplicate]

Through a test I found out that,

This is not legal :

for (int i= 0; i < 1000000000; i++) 
    int j = i & i+1 ; // j cannot be declared here!

but this is :

for (int i= 0; i < 1000000000; i++) { 
    int j = i & i+1 ;
}

Why?

like image 702
mohsen kamrani Avatar asked Mar 22 '14 09:03

mohsen kamrani


People also ask

Can I declare a variable inside a for loop?

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.

Can we use double variable in for loop?

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.

What happens if we declare variable in loop?

Declaring Loop Control Variables Inside the for Loop, When you declare a variable inside a for loop, there is one important point to remember: the scope of that variable ends when the for statement does. (That is, the scope of the variable is limited to the for loop.)


2 Answers

The first way is not legal, as it is clear to the compiler that you can't use j that you declared there, as you can't have another statement inside that for loop. Basically the new declaration of variable at that place will go out of scope the very next statement, thus is not doing any good.

While in the second case, the loop is followed by braces, which creates a new scope, and you can use the variable.

like image 168
Rohit Jain Avatar answered Sep 28 '22 09:09

Rohit Jain


A local variable declaration needs to be in a block. In the first case, you don't have it in a block. In the second case, j is within a block, so it is legal.

The local variable declaration isn't a statement, it is a local variable declaration statement. From the reference below, a local variable declaration can appear only as a statement within a block. (A block is a pair of curly braces containing statements and declarations.)

for (int i= 0; i < 1000000000; i++) 
    int j = i & i+1 ; // Local variable j declaration not within a block ==> ERROR


for (int i= 0; i < 1000000000; i++) { 
    int j = i & i+1 ; // Local variable j declaration within a block ==> NO ERROR
}

Quoting from 14.4. Local Variable Declaration Statements:

...

Every local variable declaration statement is immediately contained by a block. Local variable declaration statements may be intermixed freely with other kinds of statements in the block.

...

And from 14.2. Blocks:

A block is a sequence of statements, local class declarations, and local variable declaration statements within braces.

 Block:
   { BlockStatementsopt }

 BlockStatements:
   BlockStatement
   BlockStatements BlockStatement

 BlockStatement:
   LocalVariableDeclarationStatement
   ClassDeclaration
   Statement

A block is executed by executing each of the local variable declaration statements and other statements in order from first to last (left to right). If all of these block statements complete normally, then the block completes normally. If any of these block statements complete abruptly for any reason, then the block completes abruptly for the same reason.


Moreover, this behavior isn't quite limited to Java. You'd observe an error in a similar situation even in C:

for(int i=0; i<=100; i++)
  int j = i;     /* Error: error: expected expression before 'int' */

for(int i=0; i<=100; i++) {
  int j = i;     /* Perfect */
}
like image 31
devnull Avatar answered Sep 28 '22 08:09

devnull