For-Next. Why variable grows?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

For-Next. Why variable grows?

Post by Psychophanta »

Code: Select all

For o=o To o
  Debug o
Next
Who is telling here that the variable must count up?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post 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]
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 :!:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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:
%1>>1+1*1/1-1!1|1&1<<$1=1
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Well, in the code we're not telling the variable should be increased, so it shouldn't, no ?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: For-Next. Why variable grows?

Post 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. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Strange...
But anyway i think it's a bug, the variable shouldn't grow, but the code should run :)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 ... ?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
Last edited by Psychophanta on Thu Feb 24, 2005 10:17 am, edited 1 time in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
Last edited by PB on Thu Feb 24, 2005 10:21 am, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
IceSoft
Addict
Addict
Posts: 1698
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

JaPB is the problem!
Works not with 3.93 beta3 compiler.
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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?).
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Hehehe... IceSoft beat me by 2 minutes with the answer. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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
%1>>1+1*1/1-1!1|1&1<<$1=1
Post Reply