Android Question Why does this byte loop fail?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Could somebody tell why this fails?

B4X:
Sub TestByteLoop
 
 Dim i As Byte
 Dim arrBytes(128) As Byte
 
 For i = 1 To 127
  Log(i)
  arrBytes(i) = i '<< how can i go past 127 here?
  Log(arrBytes(i))
  'If i = 127 Then Exit 'this fixes it, but why is it needed?
 Next
 
End Sub

RBS
 

LucaMs

Expert
Licensed User
Longtime User
Could somebody tell why this fails?

B4X:
Sub TestByteLoop

Dim i As Byte
Dim arrBytes(128) As Byte

For i = 1 To 127
  Log(i)
  arrBytes(i) = i '<< how can i go past 127 here?
  Log(arrBytes(i))
  'If i = 127 Then Exit 'this fixes it, but why is it needed?
Next

End Sub

RBS
I don't understand the problem. If you declare your array that way (128) it will have positions (index) between 0 and 127.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It seems that despite this documentation:

1591574847803.png


For Next loops only work with Int or higher (long)
 
Upvote 0

Thabo Maqelepo

New Member
Could somebody tell why this fails?

B4X:
Sub TestByteLoop

Dim i As Byte
Dim arrBytes(128) As Byte

For i = 1 To 127
  Log(i)
  arrBytes(i) = i '<< how can i go past 127 here?
  Log(arrBytes(i))
  'If i = 127 Then Exit 'this fixes it, but why is it needed?
Next

End Sub

RBS
Your array has a has a static width (128) if you want more, initialize it with a larger value 256, 512 etc etc
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Bytes are signed values in Java/B4X so have a maximum positive value of 127 and a minimum negative value of -128. The loop exits when the loop counter is incremented past the limit. If you increment a byte value of 127 it overflows to -128.

Yes, I understand that, but why does it enter one more iteration if the iterator has already reached 127?
You can see this if you step through the code in debug mode.

RBS
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The loop exits when the loop counter is incremented past the limit.
The test for exiting the loop is greater (or less) than, not equality. The counter is incremented (or decremented) at the end of the loop and the test then made whether to continue the loop or not.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
The test for exiting the loop is greater (or less) than, not equality. The counter is incremented (or decremented) at the end of the loop and the test then made whether to continue the loop or not.

Still don't get this. If the counter is -128 it is outside the specified range and it still continues.

RBS
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Can the problem be that i is declared as Byte and then used as the For variable?
Maybe just use a different variable in For Cycle.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Ya all bytes in Java are signed. Unlike C where you can declare a uint8

Also your index needs to start at 0, not 1. so 0 to 127.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
The loop ittself is ok but you can't set a byte to more than 127
Could somebody tell why this fails?

B4X:
Sub TestByteLoop

Dim i As Byte
Dim arrBytes(128) As Byte

For i = 1 To 127
  Log(i)
  arrBytes(i) = i '<< how can i go past 127 here?
  Log(arrBytes(i))
  'If i = 127 Then Exit 'this fixes it, but why is it needed?
Next

End Sub

RBS

The loop is ok but you can't store a value >127 in a byte. So when you need to fill all 255 possible values make a loop from 0 to 254 and subtract 127 from i. Vice versa add 127 to the value to get unsigned bytes.

You can create a byte array as long as you want but the VALUES in it can be from -127 to +127. 127+127+0=255 different values unsigned.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
because arrBytes(0) would be unused otherwise. Unless this is your intent.

Without the context, I have no idea what youre trying to achieve.

I copied it from a bit of real code where array element 0 needed to be untouched.
The Sub as posted doesn't do anything and all the array elements remain unused.

RBS
 
Upvote 0
Top