Home All Groups Group Topic Archive Search About

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

Author
25 May 2006 8:44 PM
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

Author
25 May 2006 8:52 PM
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


Show quoteHide quote
"gmou" <gmou***@yaho.co> wrote in message
news:e$5ZlvDgGHA.3996@TK2MSFTNGP03.phx.gbl...
> 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
>
>
Author
26 May 2006 11:54 AM
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.

Show quoteHide quote
"Scott M." <s-mar@nospam.nospam> wrote in message
news:%230ISFzDgGHA.3456@TK2MSFTNGP05.phx.gbl...
> 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
>
>
> "gmou" <gmou***@yaho.co> wrote in message
> news:e$5ZlvDgGHA.3996@TK2MSFTNGP03.phx.gbl...
>> 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
>>
>>
>
>
Author
27 May 2006 12:20 AM
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).




Show quoteHide quote
"gmou" <gmou***@yaho.co> wrote in message
news:ehIR9rLgGHA.3996@TK2MSFTNGP03.phx.gbl...
> 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.
>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:%230ISFzDgGHA.3456@TK2MSFTNGP05.phx.gbl...
>> 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
>>
>>
>> "gmou" <gmou***@yaho.co> wrote in message
>> news:e$5ZlvDgGHA.3996@TK2MSFTNGP03.phx.gbl...
>>> 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
>>>
>>>
>>
>>
>
>
Author
27 May 2006 12:08 PM
Martin Milan
Show quote Hide quote
"Scott M." <s-mar@nospam.nospam> wrote in
news:#YoJzLSgGHA.3996@TK2MSFTNGP03.phx.gbl:

> 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).
Author
27 May 2006 1:17 PM
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.

Show quoteHide quote
"Martin Milan" <I***@m.i.do> wrote in message
news:Xns97D085BE13180I8spmido@194.117.143.38...
> "Scott M." <s-mar@nospam.nospam> wrote in
> news:#YoJzLSgGHA.3996@TK2MSFTNGP03.phx.gbl:
>
> > 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).
>
Author
27 May 2006 5:45 PM
Martin Milan
"Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in
news:YjYdg.8419$y4.5496@newsread2.news.pas.earthlink.net:

> 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.
Author
29 May 2006 2:03 PM
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.


Show quoteHide quote
"Martin Milan" <I***@m.i.do> wrote in message
news:Xns97D085BE13180I8spmido@194.117.143.38...
> "Scott M." <s-mar@nospam.nospam> wrote in
> news:#YoJzLSgGHA.3996@TK2MSFTNGP03.phx.gbl:
>
>> 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).
>