multiple statements in a single for loop

07-18-2007, 02:31 PM
Is there a way in VB to do this?

I am thinking of something that would look like this in c++

int main()
for (int i=0, j=0; i<3; i++, j++)
std::cout << "i: " << i << " j: " << j << std::endl;
return 0;

I know that i could just write the code to do what i want within the loop, but I am just checking to see if there is a cleaner looking way.


07-18-2007, 02:58 PM
From what the description of the syntax is telling me, there is no way to specify multiple counters or conditions in a VB For loop.

07-18-2007, 03:18 PM
It would be kinda interesting if you could do something like this.

Something like:

For j = 0 Step 1; i = 0 To 3 Step 1

Next j; i

07-23-2007, 06:48 AM
That would be cool but wouldn't both For conditions have to be true at the same time to exit cleanly??

07-23-2007, 07:22 AM
That's part of the point, yes, though not always the case.

C-style languages would let you specify multiple stop conditions:

for (int i = 0, int j = 0; i < files.Length && File.Exists(file[i]); i++, j++)

In this case, iterating through an array, halting the loop when a filename that is invalid is encountered, and iterating two variables while this is done. There's nothing preventing VB from doing it but it would require a drastic change in the For loop syntax, basically converting it into the c-style language's form if removing ambiguity was a desire. I mean seriously this just looks awful:

For (i As Integer = 0, j as Integer = 0) To (files.Length AndAlso File.Exists(i))

Next i, j

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum