Bug? Do While test ... Loop and Do Until test .... Loop

mading1309

Member
Licensed User
Longtime User
I have questions to the following subroutine demo

I use the debug mode and Version 9.8 of B4A
Both variants with do until hint=.... loop and do while hint<>.... loop does not work as I expected.
In my understanding in conditional loops like do while or do until, the condition is checked for every loop
The function hint returns a byte value. Capital letters are public constants of a class module.
The code of the subs demo, hint is listed and the declaration of the constants too.

When I use the build in debug functions of B4A to check the program flow of the sub demo I encounter the following

A} The function cheat is called
B} The function hint is called and returns an value 5 means GAME_HAS_SOLUTION
The function hint set for two buttons of an array the color to COLORHINT (equals to blue)
C} Because the while condition is fulfilled the program flow continues with the sleep(5000) instruction
D) After the sleeping time, the color of the two changed buttons are setback to gray

I expect now the program flow back to Do While program line and calling the function hint again to check the condition.
D} But the program flow continues with the Sleep(5000) instruction instead checking the While condition again.

E} Commenting the sleep instruction gives the same result. The while condition is not checked again, means the function hint is not called

F} Same program flow I experienced with Do Until ... Loop which is commented in the source code now

What is the problem?
What I am doing wrong?



B4X:
Sub demo
    cheat
'    Do Until hint=Starter.llk.GAME_NEEDS_SHUFFLE
'        Sleep(5000)
'        Buttons(Starter.llk.lastfound(0).row+1,Starter.llk.lastfound(0).col+1).Color=Colors.gray
'        Buttons(Starter.llk.lastfound(1).row+1,Starter.llk.lastfound(1).col+1).Color=Colors.gray
'    Loop
   
    Do While hint<>Starter.llk.GAME_NEEDS_SHUFFLE
        Sleep(5000)
        Buttons(Starter.llk.lastfound(0).row+1,Starter.llk.lastfound(0).col+1).Color=Colors.gray
        Buttons(Starter.llk.lastfound(1).row+1,Starter.llk.lastfound(1).col+1).Color=Colors.gray
    Loop
    game_over(True)
End Sub

B4X:
Sub hint As Byte
    If Starter.llk.status<>Starter.llk.GAME_RUN Or Starter.llk.hint=0 Then Return Starter.llk.GAME_PROBLEM
    Starter.llk.hint=Starter.llk.hint-1
    If Starter.llk.no_solution=Starter.llk.GAME_HAS_SOLUTION Then
        'solution in lastfound!
        Buttons(Starter.llk.lastfound(0).row+1,Starter.llk.lastfound(0).col+1).Color=Starter.llk.COLORHINT
        Buttons(Starter.llk.lastfound(1).row+1,Starter.llk.lastfound(1).col+1).Color=Starter.llk.COLORHINT
    End If
    Return Starter.llk.status
End Sub

B4X:
Sub Class_Globals
    Public CONST DIFFICULTY_EASY As Byte = 0
    Public CONST DIFFICULTY_NORMAL As Byte = 1
    Public CONST DIFFICULTY_HARD As Byte = 2
    Public CONST DIFFICULTY_EXPERT As Byte = 3
    Public CONST DIFFICULTY_RESUME As Byte = 4
    Public CONST DIFtoString() As String = Array As String("DiffiCULTY_EASY", "DiffiCULTY_NORMAL", "DiffiCULTY_HARD", "DiffiCULTY_EXPERT", "DiffiCULTY_RESUME")
    Public CONST MAX_PLAYTIME() As Long = Array As Long(200000,250000,300000,500000)
   
    Public CONST GAME_STOPPED As Byte = 0
    Public CONST GAME_NEEDS_SHUFFLE As Byte = 1
    Public CONST GAME_LEVEL_FINISHED As Byte = 2
    Public CONST GAME_PROBLEM As Byte = 3
    Public CONST GAME_FINISHED As Byte = 4
    Public CONST GAME_HAS_SOLUTION As Byte = 5
    Public CONST GAME_NO_SOLUTION As Byte = 6
    Public CONST GAME_RUN As Byte = 7
    Public CONST GAMEtoString() As String = Array As String("GAME_STOPPED", "GAME_NEEDS_SHUFFLE", "GAME_LEVEL_FINISHED", "GAME_PROBLEM", "GAME_FINISHED", "GAME_HAS_SOLUTION", "GAME_NO_SOLUTION", "GAME_RUN", "GAME_MAXLEVEL")
   
    Public CONST GAME_MAXLEVEL As Byte = 8
    Public CONST PLAYSOUND As Boolean = True
    Public CONST HIDE As Byte = 0
    Public CONST MAX_SHUFFLE As Byte = 3
    Public CONST COLORHINT As Int = Colors.Blue

    Public CONST NO_CHANGE As Byte = 0
    Public CONST MOVE_DOWN As Byte = 1
    Public CONST MOVE_LEFT As Byte = 2
    Public CONST MOVE_UP_DOWN_SEPARATE As Byte = 3
    Public CONST MOVE_LEFT_RIGHT_SEPARATE As Byte = 4
    Public CONST MOVE_UP_DOWN_CONVERGE As Byte = 5
    Public CONST MOVE_LEFT_RIGHT_CONVERGE As Byte = 6
    Public CONST MOVE_UP_LEFT_DOWN_RIGHT As Byte = 7
    Public CONST MOVE_LEFT_DOWN_RIGHT_UP As Byte = 8
    Public CONST MOVE_DISPERSE As Byte = 9
    Public CONST MOVE_CENTRAL As Byte = 10
    Public CONST CHANGEtoString() As String = Array As String ("NO_CHANGE", "MOVE_DOWN", "MOVE_LEFT", "MOVE_UP_DOWN_SEPARATE", "MOVE_LEFT_RIGHT_SEPARATE", "MOVE_UP_DOWN_CONVERGE", "MOVE_LEFT_RIGHT_CONVERGE", "MOVE_UP_LEFT_DOWN_RIGHT", "MOVE_LEFT_DOWN_RIGHT_UP", "MOVE_DISPERSE", "MOVE_CENTRAL")


Any hint is welcome! The program will not work as I expected in this way
What I have to change to correct the program flow?
I will not believe that this is a bug of B4A, because I guess others, more busy with B4A, will recognize this earlier than me. Right?
I still post in BUG? because I expect a more helpful answers from here.

EDIT
Playing around with the problem, I found
The sub hint is called for every loop to check the condition as I expected, but the program flow calling the sub hint is not shown in the debug session, means when the while condition is checked a further time the debugger does not step through the sub hint. I add a log instruction in the sub hint and discover the sub hint is called. I set a break point at the log instruction in the sub hint. When the sub hint is called a further time to check the While condition the debugger does not step through the lines of the sub hint. The sub hint is running without support of the debugger.

More general
When the condition of the While loop is checked the first time, the debugger steps through the sub hint. If the while condition is checked a further time during looping the debugger does not step through a sub part of the while condition. Breakpoints in the sub needs to be evaluated for the while condition are also not recognized.

So the program flow for my application is right, I can continue debugging the application keeping in mind that the debugger seems to have a small problem

May be a bug?
 
Last edited:

mading1309

Member
Licensed User
Longtime User
Thanks your reply
Sorry because of the CORONA influenced situation here the Internet isn't stable, seems overloaded
So i replay a little late
Let me prepare please I will uplaod
 

mading1309

Member
Licensed User
Longtime User
In the meantime I made modifications on my application
I want come back to the problem I described above and got an error message during compilation
Hopefully this can give a hint also to the debug problem I described above.

1585547856657.png


B4X:
Sub Activity_Resume
    Log ("===============================")
    Log ("Resume")
    Log ("===============================")
    ph.KeepAlive (True)
    PanelGame.Visible=True
    'reload llk
'    wait for (Render_game) complete (unused As Boolean)
    Render_game
    Do While True
        demo                'this is line 63
    Loop
End Sub

I just used the VLC Media Player to record the screen when I starts the compilation of the B4A project
The screen recording is uncut. I just packed the MP4 into a zip because I was not able to find out how to upload a small video
The ZIP file seems with about 800kByte to large to upload, right? So I cannot upload for now

The project lianliankan is an older one not finished in the past, I just restart.
So some hints about msgbox for example is left to redo, to adapt for changes to newer android requirements
I uploaded the full project I am busy with for these days.

later on I prepare a project ZIP file showing the situation I described before
 

Attachments

  • lianliankan.zip
    266.2 KB · Views: 128

mading1309

Member
Licensed User
Longtime User
I just recognized that the OBJECT with the .java files is not part of the ZIP file
Find attached the OBJECT folder with includes .java files

1585551772072.png


The line 692 ist just a semicolon
 

Attachments

  • Objects.zip
    170.1 KB · Views: 120

Erel

B4X founder
Staff member
Licensed User
Longtime User
The code above is incorrect. The compiler correctly detected an infinite loop. I don't know what you are trying to do however if you want to create an infinite loop then add Sleep(0) to prevent the main thread from hanging.

I have a feeling that you are not familiar with how resumable subs work. Watch the video tutorial: https://www.b4x.com/android/forum/threads/b4x-resumable-subs-sleep-wait-for.78601/#content
For further discussion please start a new thread in the questions forum.
 
Top