to FreeBASIC but the STEP has got me well beaten. I have tried every which way except the one that works. No doubt some of you will take one look at it and write the FB version and I will kick myself, or maybe not.
#include <iostream>
using namespace std;
int left = 15;
int n = 13;
int size = 1;
int main()
{
for (int left = 0; left < n; left += 2*size){
cout << "Hello world!" << endl;
}
return 0;
}
No I think you're mistaken about the C++ code. Or you missed something when you copied and pasted. The loop incrementer in the C for() is definitely "left = left + 2 *size". I'm not sure where you're getting a step of "left + 2*size."
The while loop I provided is the exact transliteration of the C for() loop as you posted it. My while loop indeed does something with n. "left < n" is the loop termination condition. Which is there. Did you try making a small example program with the C++ for loop to see exactly what it does? That's what I did.
I'm not sure what BasicCoder2 implemented, but it's definitely not the same effect as the C++ for() as you gave it.
Perhaps you should post a bit more of the C++ code if I' missed something.
'for (int left = 0; left < n; left += 2*size)
' int is long (32/64 bit)
dim as long lSize = 10
dim as long lN = 100
dim as long lLeft=0
while lLeft<lN
print lLeft
lLeft+=2*lSize
wend
sleep
It might be worth remembering that a C++ for() loop does not increment the loop counter itself. That's what the 3rd term is for. You can do an infinite loop with for(;;). It's basically a fancy syntax for a type of while loop.
caseih wrote:
It might be worth remembering that a C++ for() loop does not increment the loop counter itself. That's what the 3rd term is for. You can do an infinite loop with for(;;). It's basically a fancy syntax for a type of while loop.
Ok thanks for that. My limited knowledge of C++ has faded over years of using FreeBASIC and I didn't pick up on the probable mistake in the C++ example.
I find examples of FB to C++ and C++ to FB helpful as I would like to do more C++ coding.
I thought the problem required using a for loop not the while loop.
A piece of code out of context and the desired behavior left undefined doesn't help.
'for (int left = 0; left < n; left += 2*size)
' int is long (32/64 bit)
dim as long lSize = 10
dim as long lN = 100
dim as long lLeft=0
dim as long steps
dim as long count
while lLeft<lN
print lLeft
lLeft+=2*lSize
wend
print
lSize = 10
lN = 100
lLeft=0
count = lN - lSize*2
steps = lLeft + 2*lSize
for lLeft = 0 to count step steps
print lLeft
next lLeft
sleep
'#include <iostream>
'
'using namespace std;
'
'int lSize = 10;
'int lN = 100;
'int lLeft=0;
'
'int main()
'{
' for (int lLeft = 0; lLeft < lN; lLeft = lLeft + 2*lSize){
' cout << lLeft << endl;
' }
' return 0;
'}
#macro Cfor(a,b,c,instruction)
scope
a
#define labl label:
labl
instruction
c
if b then goto labl
#undef label
end scope
#endmacro
var n=23
var size=3
Cfor(dim as long lft=0,lft<n,lft+=2*size,print lft)
print
var lSize = 10
var lN = 100
Cfor(dim as long lLeft=0,lLeft<lN,lLeft=lLeft+2*lSize,print lLeft)
sleep
' Joshy's code
'for (int left = 0; left < n; left += 2*size)
' int is long (32/64 bit)
dim as long lSize = 10
dim as long lN = 100
dim as long lLeft=0
while lLeft<lN
print lLeft
lLeft+=2*lSize
wend
print
For lLeft = 0 To lN - 1 Step (lLeft + 2*lSize)
Print lleft
Next
sleep
The 1Left in the Step is redundant - as caseih intimated in his first post. Redundant code can be confusing.
It would seem then that the issue I have is elsewhere.
The C++ code is by no means perfect - I had to do some editing to avoid a runtime array out of bounds error.
deltarho[1859] wrote:The 1Left in the Step is redundant - as caseih intimated in his first post. Redundant code can be confusing.
Yes in this case by shear luck that specifying a step of "left+2*size" works at all (left is 0) in the FB for loop. However it was not in the original C++ for() loop, which is why I was confused as to where it came from--the original for loop clearly increments left by "2*size" only every pass.
In nearly all languages I've ever seen, the "FOR" loop is just a convenient form of a WHILE loop. That includes FB. Therefor all FOR loops can be implemented as WHILE loops with some extra initialization code. Even Python's for, which works via iteration, can be implemented as a while loop. There are some uses of a C for loop that cannot be represented by FB's FOR statement. For those you have to use WHILE to implement it.
There was one issue I had to resolve in that foo[5], for example, in C++ has indices 0 to 4 whereas foo(5) in FB has indices 0 to 5. I have a feeling that I haven't fully corrected all instances and that is my issue.
The website I got the C++ from had code for C++, Java, Python3 and C#. I have not dabbled much with other languages but what struck me was how similar they all were. Porting from anyone to another is exasperated by differences such as above.