Bug? for next with calulations

mrred128

Active Member
Licensed User
Longtime User
If you use for / next loops based on vars, the vars can change and what you expect will happen. When you use vars that have calculations (ex: var - 1), it seems to calculate it the first time through, and then ignore any changes.

Here is a quick example of both cases....

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim high As Int = 20
    Dim data As List
    data.Initialize
    Dim count As Int
    For count = 0 To 100
        data.add(Bit.AND(count,1))
    Next

    ' list size test
    Try
        For count = 0 To data.Size - 1
            If data.Get(count) = 0 Then
                data.RemoveAt(count)
                count = 0
                Continue
            End If
        Next
        Msgbox("Completed dynamic list (calc)","No Bug")
    Catch
        Msgbox("Failed list size test","Error")
    End Try
    ' dynamic int test
    For count = 0 To high - 1
        If count = 5 Then high = 10
        If count = 15 Then Msgbox("Count too high, failed dynamic int test (calc)","Bug")
    Next

    ' list size test without calc
    Try
        For count = 1 To data.Size
            If data.Get(count - 1) = 0 Then
                data.RemoveAt(count - 1)
                count = 0
                Continue
            End If
        Next
        Msgbox("Completed dynamic list","No Bug")
    Catch
        Msgbox("Failed list size test","Error")
    End Try
    ' dynamic int test
    For count = 0 To high
        If count = 5 Then high = 10
        If count = 15 Then Msgbox("Count too high, failed dynamic int test","Bug")
    Next
End Sub
 

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do in these loops ?
B4X:
For count = 0 To data.Size - 1
    If data.Get(count) = 0 Then
        data.RemoveAt(count)
        count = 0
        Continue
    End If
Next
Mybe you should try this:
B4X:
For count data.Size - 1 = To 0 Step -1
    If data.Get(count) = 0 Then
        data.RemoveAt(count)
    End If
Next
The problem in your code is that the max limit data.Size changes during looping and I'm afraid the a For / Next loop doesn't like it.
 

mrred128

Active Member
Licensed User
Longtime User
That's not a problem with the code. That is a demonstration of what is broken.

if the test for end of loop has a calculation imposed on it, it doesn't recalculate. If it is solely looking at a variable, it will look for the change.

edit:

The two chunks of code should either fail in the same way, or not fail.
 
Last edited:

MaFu

Well-Known Member
Licensed User
Longtime User
No bug. This behaviour is identical in the most programming languages (i can't say all because i don't know all) and it's ok. If the value is recalculatet on each loop it slows down. If you need this you must go another way (do while loop).
 

MaFu

Well-Known Member
Licensed User
Longtime User
It's not inconsistent, your test code is wrong.
Let's have a look:
Your first loop ends with an catch error. This is correct because the loop end condition isn't recalculated and the list is shorter after data.RemoveAt().
Your second loop tells "Count to high...". This is also correct (no recalculation, high = 10 has no effect).
Loop 3 doesn't fail. I wonder why :D. You have removed all 0 values from the list in the first loop. Therefore the condition is always wrong and no data is removed.
Loop 4 also works fine. But you had set high = 10 in loop 2 ;)
 
Top