Do WHile true bug ?

rpsj

Member
Licensed User
Longtime User
Hi !

I started a brand new project and did this :

B4X:
Sub Activity_Resume

   Do While True
   
   Loop
   
End Sub

Give an error :

Compiling code. 0.01
Compiling layouts code. 0.00
Generating R file. 0.21
Compiling generated Java code. Error
B4A line: 22
Do While True
javac 1.7.0_05
src\b4a\example\main.java:240: error: unreachable statement
;
^
1 error

But if I do :

B4X:
   Dim i As Boolean
   i = True
   
   Do While i
   
   Loop

No errors !!

Is this a bug or a feature ?

Version 2.30

Thanks

Rubens Jr.
 

mc73

Well-Known Member
Licensed User
Longtime User
If you could explain to me what exactly is supposed to do this loop apart from looping to infinity thus crashing the os in some secs, I would reply that it's a bug. Yet, I never used a do...while without parsing a variable
 
Upvote 0

rpsj

Member
Licensed User
Longtime User
This was only a sample to show the bug.
In my real aplication there is a IF ... then inside the loop to return from sub

I spent some time to discover that was the DO While TRUE that was causing an error and not my sub !

Thanks


Rubens Jr.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I have to insist on this. What was the do... while checking? True would always stand true, no question about that. This means, you wanted your sub halted. Am I correct on this? If that is so, you should simply use a boolean flag, warning all other subs to not be executed while your process inside the if... then is still running. This way, you would avoid crashing the app, due to inactivity (I have to suppose there was no doEvents statement inside this loop). Another way, would be to load a transparent panel, consuming all user interactions, while your sub was still processing data. I hope I am making clear why I insisted. Yet, everyone has his own coding preferences, no doubt about that
 
Upvote 0

rpsj

Member
Licensed User
Longtime User
We are going out of scope
My original question was that the statement DO WHILE TRUE gives an error in the JAVA generated code not in B4A, but when write DO WHILE some_var the error do not exist ...

But, responding to your question :

My sub have to read all items in a array and test some situations.
I can do :
B4X:
DO WHILE xxx(i) = "some val"  or xxx(i) > other_val and xxx(i) < another_val 
  do some proc with valid data
  i = i + 1
LOOP
or I can do
B4X:
DO WHILE TRUE
  if xxxx then return
  if xxxx and yyy then return
  if bla bla bla then return
  do some proc with the valid data
  i = i + 1
LOOP
I think the second example is more easy to read and understand (for me of course)
The array is only 1000 items and I thing that is very fast to read all.

Thanks

Rubens Jr.

PS : Sorry, english is not my native language
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You could use
B4X:
do until xxxx or (something and yyyy) or blaBlaBla
loop

 
Upvote 0

Beja

Expert
Licensed User
Longtime User
startit:
IF xxxx OR (something AND yyyy) OR blaBlaBla THEN EXIT SUB
GOTO startit
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
I think the OP point is that both statements are infinite loops. However, the one with the variable never errors out (like it should).
 
Upvote 0

rpsj

Member
Licensed User
Longtime User
If you look it's a Java compiler "unreachable statement" error. The Java compiler has detected the infinite loop and errored it because it doesn't know if any statements after the loop can ever be executed.

That is the point !
Now I understood the error. If I use some var in DO WHILE xxx JAVA assume that somewhere inside the loop xxx will became FALSE and end the loop. JAVA did not saw that inside the loop has RETURN, EXIT or so ....
Some C compilers do the same, but with WARNING, not ERROR ...

Thanks for the help.

Rubens Jr.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
However, the one with the variable never errors out (like it should).
Why "should it"? You would be asking the compiler to do expression evaluation at runtime to detect assignments to that variable and predict the resulting values. The compiler can only reasonably detect uses of literals at compile time. Whether this should be treated as an error at all in Java when the compiler can only identify static cases is a matter for debate. C# for example, while similar to Java in many respects, does allow this kind of infinite loop and does not regard it as an errror.
 
Upvote 0

Sortec

Member
Licensed User
Longtime User
What was the do... while checking? True would always stand true, no question about that. This means, you wanted your sub halted. Am I correct on this?

No. He just wants While True to work correctly. Its a bug.
 
Upvote 0

nicholas.jj.taylor

Member
Licensed User
Longtime User
There is not a single good reason for such loop.

I hope that you don't mind being wrong, because one excellent reason to include it is either during development of a while loop content before the condition has been developed, or during a debug to temporarily remove the condition e.g.

Do While true 'BlnCondition1 = BlnCondition2

etc...

Loop

Still, whether you implement it or not is up to you, but that doesn't stop it from being undesirable behaviour.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…