473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
May 25 '06 #1
7 2892
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" <gm*****@yaho.co> wrote in message
news:e$**************@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

May 25 '06 #2
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-***@nospam.nospam> wrote in message
news:%2****************@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" <gm*****@yaho.co> wrote in message
news:e$**************@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


May 26 '06 #3
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).


"gmou" <gm*****@yaho.co> wrote in message
news:eh**************@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-***@nospam.nospam> wrote in message
news:%2****************@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" <gm*****@yaho.co> wrote in message
news:e$**************@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



May 27 '06 #4
"Scott M." <s-***@nospam.nospam> wrote in
news:#Y**************@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).

May 27 '06 #5
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.

"Martin Milan" <I8**@m.i.do> wrote in message
news:Xn**********************@194.117.143.38...
"Scott M." <s-***@nospam.nospam> wrote in
news:#Y**************@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).


May 27 '06 #6
"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in
news:Yj****************@newsread2.news.pas.earthli nk.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.

May 27 '06 #7
My point was that in VB, we don't need a continue statement, we just put
statements inside an if statement and problem solved.
"Martin Milan" <I8**@m.i.do> wrote in message
news:Xn**********************@194.117.143.38...
"Scott M." <s-***@nospam.nospam> wrote in
news:#Y**************@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).

May 29 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bengt Richter | last post by:
Just thought None as the first argument would be both handy and mnemonic, signifying no translation, but allowing easy expression of deleting characters, e.g., s = s.translate(None,...
5
by: Mariusz Sakowski | last post by:
Can someone translate this code to a C++? repeat file.Read(bufor, 1); for p := 1 to KeyCount do begin buf := buf xor Keys; end; targetFile.Write(bufor, 1); until file.Size = file.Position;
6
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
18
by: KraftDiner | last post by:
I'm porting a routing from C++ to python. There is a complex for loop that I don't know how to code in python for (i = nPoints-1, j = 0; j < nPoints; i = j, j++) Thanks.
6
by: Anders K. Olsen | last post by:
Hello group I'm trying to list the users and groups who has read access to a file. I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then loop through the...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
6
by: Paulchen | last post by:
Hello, I have found a perl script and need to "translate" this to PHP. I try to do it step by step and the first part of it is this function (the whole script is found at...
0
by: Laurana82 | last post by:
Hi guys, I'm new to Oracle and I need to transfer some scripts from Oracle to SQL SERVER. I've done most of them but this is where I'm stuck: 1. How do I put OPEN FOR USING in Sql Server? ex: ...
3
by: Kenneth McDonald | last post by:
I have the need to occasionally translate a single word programatically. Would anyone have a Python script that would let me do this using Google (or another) translation service? Thanks, Ken
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.