How to translate C++ 'for' loop into VB.NET?

G

gmou

Dear group,
I am building a translator from C++ into VB (and into C#).
At the moment, I have a hard time figuring out the equivalent of a 'for'
loop in VB.

Given C++ code:
for( int i=0; test_fct(i); increment_fct(i) )
{
... // body of the loop
}

Would you opt for a 'While..End While' loop, or 'Do...Loop'?
Neither of these are clean translations, since the 'increment_fct' would
appear twice in VB.

Please visit the translation web site,
http://code2code.net?lang2=vb , and choose sample='VB_for_loop.cpp'.

Any suggestions welcome.
Regards,
/george moudry
 
S

Scott M.

dim i as integer
do while test_fct(i)
...loop body...

increment_fct(i)
loop

....Or...

dim i as integer
for i = startValue to endValue

...loop body...
'No need to increment the counter as VB.NET will do this
'automatically in a "For...Next" loop

next
 
G

gmou

Scott, thanks for your suggestion.
The problem with the 'do ... while' approach is that in case of a C++
'continue' statement,
the 'increment_fct' needs to occur multiple times in VB:

C++:
for( int i=0; test_fct(i); increment_fct(i) )
{
... Statements 1
if(i==2) continue;
... Statements 2
}

VB:
Do While test_fct(i)
... Statements 1
If i=2 Then
increment_fct(i) ' first copy
Continue Do
End If
... Statements 2
increment_fct(i) ' second copy
Loop

But it's not the end of the world, and I can implement the translation that
way.
Thanks!
/george

PS. The 'increment_fct' in C++ can be any statement, and in general is not a
plain increment.
If it were a simple increment, then the statement ''No need to increment the
counter ...' holds true.
 
S

Scott M.

I'm not sure that interpretation is correct. I'm trying to understand what
it is you want to do...

I am assuming from your original code that *test_fct(i)* is a function that
returns a boolean.
Your original code seems like the loop should start at zero, keep running as
long as test_fct(i) returns true and increment the counter (i) by the amount
returned from calling a function called increment_fct(i).

Do you want this loop to simply increment by 1 each time it runs?
Do you want this loop to terminate when test_fct(i) returns False?

In VB, you don't need any "continue" statements, the loop will continue
until your condition is met. If you'd like to get out of the loop early, you
can throw "Exit Do" in there (if you must).
 
M

Martin Milan

I'm not sure that interpretation is correct. I'm trying to understand
what it is you want to do...

I am assuming from your original code that *test_fct(i)* is a function
that returns a boolean.
Your original code seems like the loop should start at zero, keep
running as long as test_fct(i) returns true and increment the counter
(i) by the amount returned from calling a function called
increment_fct(i).

Do you want this loop to simply increment by 1 each time it runs?
Do you want this loop to terminate when test_fct(i) returns False?

In VB, you don't need any "continue" statements, the loop will
continue until your condition is met. If you'd like to get out of the
loop early, you can throw "Exit Do" in there (if you must).

I think the poster already knows this. I'm no VB.Net expert, but I would
think the continue do is very much like the continue in C++ - in other
words is immediately jumps to the next iteration of the loop (assuming
the test is valid).
 
M

Michael D. Ober

In VB 2005, the continue statement actually jumps to the loop closing
statement (next, loop, etc), evalutates it, and then restarts the loop based
on that evaluation.

Mike Ober.
 
M

Martin Milan

In VB 2005, the continue statement actually jumps to the loop closing
statement (next, loop, etc), evalutates it, and then restarts the loop
based on that evaluation.

Mike Ober.

Is that not what I said?

Actually, i've just realised, no it's not... I stand corrected.
 
S

Scott M.

My point was that in VB, we don't need a continue statement, we just put
statements inside an if statement and problem solved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top