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


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

Recommended Answers

All 2 Replies

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).

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.