Page 1 of 2

For-Next. Why variable grows?

Posted: Wed Feb 23, 2005 9:55 pm
by Psychophanta

Code: Select all

For o=o To o
  Debug o
Next
Who is telling here that the variable must count up?

Posted: Wed Feb 23, 2005 10:00 pm
by Polo
same here.
same stuff for :

Code: Select all

For o=1 To o 
  Debug o 
Next 
since o is not defined, it should be :

Code: Select all

For o=1 To 0
  Debug o 
Next 
But apparently it doesn't act like that...
You should have posted it in the bug section :) [/code]

Posted: Wed Feb 23, 2005 10:05 pm
by Psychophanta
Polo wrote: You should have posted it in the bug section :)
Mmmm... May be :roll:

Tested on MSX Basic Ver 3.0, and it only writes 0. Just what must be :!:

Posted: Thu Feb 24, 2005 12:31 am
by Froggerprogger
No, there's no bug, all is fine.

The test is done new at each loop's beginning and the upper-bound, the TO-value, might be a variable, so e.g. something like the following will run 3 times, not just 1:

Code: Select all

q = 1
For p=1 To q
  q = 3
  Debug p
Next

In your code o is initialized with 0 (because o=o and o is zero if undeclared before) which is smaller or equal to o=0. Then o is increased by one.
The loop returns to the head and again o (now with value = 1) is checked against the variable o (with value = 1), and so the loop's body will start again, after o is increased one more.
....and so on.

So this loop always starts again for the last time :wink:

Posted: Thu Feb 24, 2005 8:51 am
by Polo
Well, in the code we're not telling the variable should be increased, so it shouldn't, no ?

Re: For-Next. Why variable grows?

Posted: Thu Feb 24, 2005 9:04 am
by PB
> For o=o To o
> Debug o
> Next
> Who is telling here that the variable must count up?

Dunno, but this doesn't compile at all under v3.93 beta 3 -- it gives an error. :)

Posted: Thu Feb 24, 2005 9:07 am
by Polo
Strange...
But anyway i think it's a bug, the variable shouldn't grow, but the code should run :)

Posted: Thu Feb 24, 2005 9:55 am
by Psychophanta
Well.
PB, please don't say lies and don't talk nonsenses.

Froggerprogger, in my opinion you aren't right.
Try in C:
For (o=o;o<=o;o++) {}

And in my opinion it is a bug.
Tested with MSX Basic 3.0, BlitzBasic2 (Amiga) and QuickBasic 4.5. and works fine.

Can someone try it with VB, Powerbasic, Blitz ... ?

Posted: Thu Feb 24, 2005 10:16 am
by Psychophanta
Mmmm...
Or Froggerprogger is right ? :roll:
Then PB is well done and the other Basic compilers are buggy? :?

Mmmm... In C:
When for (o=o;o<=o;o++)
the "<=" is just what must do internally a Basic For-Next loop, i think.
So then all is pointing that PB is well done and the others are fake :?: :shock:
Is it possible :?:

Thanx a lot, Froggerprogger :D

Posted: Thu Feb 24, 2005 10:17 am
by PB
> PB, please don't say lies and don't talk nonsenses.

Well, why not see for yourself before you accuse people of being liars! :evil:

File:1->error.gif

> Can someone try it with VB, Powerbasic, Blitz ... ?

With Visual Basic 5, it just prints 0 in the debug window, over and over.

Posted: Thu Feb 24, 2005 10:20 am
by Psychophanta
PB wrote:> PB, please don't say lies and don't talk nonsenses.

Well, why not see for yourself before you accuse people of being liars! :evil:

File:1->error.gif
Sorry PB. My fault :oops:
I didn't believe you. :?
So, there must be something strange. Here it works with 3.93b3, some user lib, or something there?

Posted: Thu Feb 24, 2005 10:22 am
by IceSoft
JaPB is the problem!
Works not with 3.93 beta3 compiler.

Posted: Thu Feb 24, 2005 10:24 am
by PB
> PB. My fault :oops:

Sorry that I snapped at you... I had a bad day and that tipped me over the edge. :?

> Here it works with 3.93b3

I worked it out -- it does compile with the PureBasic editor, but not jaPBe.
That's why it worked for you but not for me (unless you're using jaPBe too?).

Posted: Thu Feb 24, 2005 10:24 am
by PB
Hehehe... IceSoft beat me by 2 minutes with the answer. :)

Posted: Thu Feb 24, 2005 10:49 am
by Froggerprogger
Perhaps there are some languages that compile a for-loop in that way, that at the time when the line

Code: Select all

For i=0 to a
is processed, the value 'a' is stored in a seperate place so any changes to 'a' inside the loop will not change the loop's TO-value.
But this would make the for-loop less powerful.
Well, in the code we're not telling the variable should be increased, so it shouldn't, no ?
'o' is the countervariable! Why does 'i' count up in "For i=0 to 100" ?

A For-loop (with positive STEP) is processed in this way:
- check the value of the countervariable against the TO-variable's value
- if both are same, or countervariable < TO-variable then increase the countervariable by step

So if countervariable and TO-variable point to the same variable, e.g. 'i' then both are increased, and will always have the same result => endless loop