|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to translate C++ 'for' loop into VB.NET?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 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 > > 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 >> >> > > 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 >>> >>> >> >> > >
Show quote
Hide quote
"Scott M." <s-mar@nospam.nospam> wrote in I think the poster already knows this. I'm no VB.Net expert, but I wouldnews:#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). 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). 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). > "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in Is that not what I said?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. Actually, i've just realised, no it's not... I stand corrected. 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). >
Wrapper classes
Windows Service using VB.Net automatic startup problem ! error handle preventing default value in vb.net make a form shared - called from another form - how? catch the process terminate from the task manager FAST date string conversion ? playing & re-playing 7 .wav files Best Suite Documentation Resizing |
|||||||||||||||||||||||