View Full Version : for loop question

01-25-2012, 09:42 PM
How do I re-write this from VBA to JAVASCRIPT?

For i = 0 To num_fields - 1

Forgive me, im still new at this stuff.



01-25-2012, 10:00 PM
for (i = 0; i < num_fields - 1; i++)

Old Pedant
01-25-2012, 10:03 PM
for ( var i = 0; i < num_fields; ++i )
} // this closing brace is the equivalent of NEXT

Old Pedant
01-25-2012, 10:06 PM
for (i = 0; i < num_fields - 1; i++)

No, not quite.

The VBA loop ends on the value that is *EQUAL* to the expression after the TO. (Less than or equal, actually, but equal when the STEP value is 1, which is the default.)

So you need to use i <= num_fields - 1

Or, as I did, get rid of the -1 and use i < num_fields

01-25-2012, 10:07 PM
Worked like a charm, still working on loop parts...I still have lots to learn.

Thanks all!



01-25-2012, 10:09 PM
What about when theres a:

next i

How would that be written in Javascript?

Old Pedant
01-25-2012, 10:20 PM
Read my first post. Again. Pay attention to where it says

// this closing brace is the equivalent of NEXT

If you mean, how do you specify Next i versus Next j, you don't. You will notice that the ability to specify a variable with Next has been removed from dialects of BASIC more modern than VBA. It never was a good idea.