0

When I was going through a program of my friends, suddenly I came across a strange for loop, I was wondering about the for loop since then, the loop looked like this

for(1=0,ii=result.length;i<ii;i++)

Is this for loop a valid for loop? Can any one please help me?

3
Contributors
2
Replies
20
Views
4 Years
Discussion Span
Last Post by JamesCherrill
0

This probably:

for (i = 0, ii = result.length; i < ii; i++)

Yes, it's valid. The initialization can contain multiple items (the others too probably).

2

Almost. The syntax allows for a list of expressions in each part of the for, so for(i=0,ii=result.length;i<ii;i++) is OK. See Java Language Spec section 14.14
It's probably written that way to avoid re-evaluating result.length on each pass of the loop in a (mistaken?) attempt to optimise execution speed.
But your code has a 1 instead of an i, resulting in 1=0, which is invalid syntax.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.