Why must a variable be declared in a for loop initialization?

mightyandweakcoder :
int v=0;

for(v;v<2;v++){

...

}

Why is this not allowed in Java? Why do we have to declare variable v in the for loop initialization ? I know it's not a statement if i do it like that but why doesn't Java allow the above?

Eran :

If v is declared prior to the loop, you should leave the first part of the for statement empty:

int v=0;

for(; v < 2; v++){

...

}

There's no meaning to just writing v;.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=4253&siteId=1