Problem with for - next

Just starting out? Need help? Post your questions and find answers here.
woki
User
User
Posts: 23
Joined: Fri Apr 25, 2003 4:48 pm
Location: Germany

Problem with for - next

Post by woki »

Both for loops are endless:

Code: Select all

OpenConsole()
For i.b =0 To -128 Step -1
  PrintN("Wert: "+Str(i.b))
Next i
For i.b =0 To 127
  PrintN("Wert: "+Str(i.b))
Next i
End
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Yep, same here on XP / PB 3.71 Beta 2
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Post by Inner »

Code: Select all

OpenConsole() 
For i =0 To -128 Step -1 
  PrintN("Wert: "+Str(i)) 
Next 
For i =0 To 127 
  PrintN("Wert: "+Str(i)) 
Next
End
this works however.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Inner wrote:

Code: Select all

OpenConsole() 
For i =0 To -128 Step -1 
  PrintN("Wert: "+Str(i)) 
Next 
For i =0 To 127 
  PrintN("Wert: "+Str(i)) 
Next
End
this works however.
Simple to answer, you have changed i.b to i.l.
The bug only appear, when you use a value at the limit of the range of the variable.

btw: Would be nice, when the debugger can catch this.
woki
User
User
Posts: 23
Joined: Fri Apr 25, 2003 4:48 pm
Location: Germany

Post by woki »

endless loop: i.w -32768 / 32767
endless loop: i.l -2147483648 / 2147483647

if values are greater or less the loop never executes without any error.

Yes, would be nice, when the debugger can catch this.
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Post by Inner »

Yes I know, it is a bug.. but you see there is no diferents between doing a

loop.b or a loop.l (.l being default) since the limiter in the for...next loop defines the max count value to can go to, the only real diferents is type, long vs byte, don't quite know why anyone would want to loop like you have but still it is a bug.
User avatar
Falko
Enthusiast
Enthusiast
Posts: 271
Joined: Sat Oct 04, 2003 12:57 pm
Location: Germany
Contact:

Pb 3.94 of year 2005

Post by Falko »

Sorry,
isn't this fixed or make the Debugger endless bytes in for next?

here this code with Debugger

Code: Select all

For a.b= 1 To 127
  b = a & $FF
  Debug b
Next a
you can see this.

And now:

Code: Select all

For a.b= 1 To 254
  b = a & $FF
  Debug b
Next a
nothing.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Problem with for - next

Post by blueznl »

woki wrote:Both for loops are endless:

Code: Select all

OpenConsole()
For i.b =0 To -128 Step -1
  PrintN("Wert: "+Str(i.b))
Next i
For i.b =0 To 127
  PrintN("Wert: "+Str(i.b))
Next i
End
range for a byte: -127 to +127

you're going out of bounds
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Pb 3.94 of year 2005

Post by blueznl »

And now:

Code: Select all

For a.b= 1 To 254
  b = a & $FF
  Debug b
Next a
nothing.[/quote]

again, bytes are signed in pb, so reach from -127 to +127
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

and why does this not end?

Code: Select all

For a.b= 1 To 127
  b = a & $FF
  Debug b
Next a 
edit: okay, to 126 is ok
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Problem with for - next

Post by srod »

blueznl wrote:range for a byte: -127 to +127
I think that a byte can range from -128 to 127. At least the following produces the result of -128:

Code: Select all

a.b = -128
debug a
I may look like a mule, but I'm not a complete ass.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Problem with for - next

Post by Trond »

blueznl wrote:
woki wrote:Both for loops are endless:

Code: Select all

OpenConsole()
For i.b =0 To -128 Step -1
  PrintN("Wert: "+Str(i.b))
Next i
For i.b =0 To 127
  PrintN("Wert: "+Str(i.b))
Next i
End
range for a byte: -127 to +127

you're going out of bounds
No, according to the manual a byte ranges from -128 to +127. He's not out of bounds.
User avatar
Falko
Enthusiast
Enthusiast
Posts: 271
Joined: Sat Oct 04, 2003 12:57 pm
Location: Germany
Contact:

Post by Falko »

@blueznl, please make following

Code: Select all

For a.b= -129 To 127
  b = a & $FF
  Debug b
Next a
You can see the errormessage from debugger and just this

Code: Select all

For a.b= -128 To 127
  b = a & $FF
  Debug b
Next a
is it ok?

Now this is endless, nothing out of range from -128 to 127.
Out of range is -129 or 255, you can see from debug-message.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Not a bug.
Java has the same 'problem':

Code: Select all

for (b = 0; b >= -128; b++)
     ;
A For-Loop is designed like the following:

Code: Select all

Init a.b = 0
|
|
Test a.b >= -128, if false: break
|
|
Decrement a.b
Jump back to Test
So now, when a reaches -128 in 'Test', the loop will be executed and after
the 'Decrement' a will be 127 and 'Test' will be true again. So endless loop.

If Fred would change this, it wouldn't be the standard behaviour of a for-loop
and it would produce some speed decrease...

greetz
Remi
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Falko
Enthusiast
Enthusiast
Posts: 271
Joined: Sat Oct 04, 2003 12:57 pm
Location: Germany
Contact:

Post by Falko »

Ok, java can do this. The old GFABASIC makes unsigned from 0 to 255.
I help me with following source, this do that as GFABASIC.

This is better so

Code: Select all

For a.b= -128 To 127
  b = a  ;& $FF  ; This makes 128...255...0...127 and endless
  Debug b+128 ; This makes 0...255
 If a=127:Break:EndIf ; This stopped endless loop
Next a
greetz
Falko
Post Reply