Bug in Do Loop block structures

JesseW

Active Member
Licensed User
Longtime User
I've come across a bug where a do loop seems to translate incorrectly to java and thus errors on the java compile stage. Here is a very short example that errors every time I try to compile it:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim mapSubs As Map
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim ix As Int
   Do While True
      ix = ix + 1
   Loop
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

This is a 'stock' default project except for the few lines in Activity_Create. Here is the error I get:

B4X:
Compiling code.                         0.00
Generating R file.                      0.00
Compiling generated Java code.          Error
B4A line: 17
ix = ix + 1
javac 1.6.0_26
src\com\warsoftapps\dolooptest\main.java:216: unreachable statement
;
^
1 error

If I remove the line -> ix = ix + 1, I get this error:

B4X:
Compiling code.                         0.01
Generating R file.                      0.00
Compiling generated Java code.          Error
B4A line: 16
Do While True
javac 1.6.0_26
src\com\warsoftapps\dolooptest\main.java:214: unreachable statement
;
^
1 error

I've attached a pic of the error on my pc. Let me know if I can be of further assistance.

Jesse

ps. as you can see in the picture, I'm using B4A v1.8. I also attached the exported project.
 

Attachments

  • DoLoopTest.zip
    5.4 KB · Views: 240
Last edited:

admac231

Active Member
Licensed User
Longtime User
It is my understanding that this is not a bug. The java spec forbids loops with unreachable breaks (see here).

If you were to do this:
B4X:
Dim b As Boolean
b = True
Dim ix As Int
Do While b
   ix = ix+1
Loop

You would be doing the same thing but also giving the possibility of changing b to False thereby breaking the loop.
 

JesseW

Active Member
Licensed User
Longtime User
I just now realized this with the following code

B4X:
Dim ix As Int
   Dim ok As Boolean
   Do While ok
      ix = ix + 1
      If ix = 100 Then ok = False
   Loop

I also see that if an Exit statement is used, it will not error

B4X:
   Dim ix As Int
   Dim ok As Boolean
   Do While True
      ix = ix + 1
      If ix = 100 Then Exit
   Loop

I will have to keep this in mind. I had intended to use an Exit statement when I found the error, but hadn't coded it yet.

My apologies for the false alarm. Could this be added to the keyword documentation?

-- Jesse
 
Top