Page 1 of 1
Problem with for - next
Posted: Sat Jul 05, 2003 2:30 am
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
Posted: Sat Jul 05, 2003 2:33 am
by Karbon
Yep, same here on XP / PB 3.71 Beta 2
Posted: Sat Jul 05, 2003 5:17 am
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.
Posted: Sat Jul 05, 2003 6:19 am
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.
Posted: Sat Jul 05, 2003 8:20 am
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.
Posted: Sat Jul 05, 2003 3:27 pm
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.
Pb 3.94 of year 2005
Posted: Wed Dec 07, 2005 9:58 pm
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.
Re: Problem with for - next
Posted: Wed Dec 07, 2005 10:18 pm
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
Re: Pb 3.94 of year 2005
Posted: Wed Dec 07, 2005 10:19 pm
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
Posted: Wed Dec 07, 2005 10:40 pm
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
Re: Problem with for - next
Posted: Thu Dec 08, 2005 12:13 am
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:
Re: Problem with for - next
Posted: Thu Dec 08, 2005 1:19 pm
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.
Posted: Thu Dec 08, 2005 1:31 pm
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.
Posted: Fri Dec 09, 2005 2:09 pm
by remi_meier
Not a bug.
Java has the same 'problem':
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
Posted: Fri Dec 09, 2005 3:23 pm
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