Wish While or Until at end of Do..Loop (Bug -> Wish)

kiki78

Active Member
Licensed User
Longtime User
If I put While or Until at end of Do..Loop I receive Syntax Error.

B4X:
Dim i As Int = 0
Do
    i = i + 1
Loop While i < 10
 

kiki78

Active Member
Licensed User
Longtime User
Sorry eps, but it's not the same.
Both syntax are normally correct, but with test at end, first loop is always execute.
This is rarely used but sometime interesting.
 

DonManfred

Expert
Licensed User
Longtime User

Troberg

Well-Known Member
Licensed User
Longtime User
I agree, this is sometimes useful, and would be nice to have.

For example, a typical "try until you succeed"-scenario:

B4X:
Do
    InitMeasurementProbe()
    StartMeasure()  
    Do Until MeasuredValueReady()
        DoEvents
    Loop
    Measurement=GetMeasurement()
    CloseMeasurementProbe()
Loop Until Measurement<>BadValue
LogFile(Measurement)

In VB, there were also several places where the framework was based upon a "do until you fail"-scenario. Read files listings with Dir, which returned "" when there was no more files left to list, or when looping in database recordsets. I don't know how common this is in B4A, but I suspect they can happen.
 

Troberg

Well-Known Member
Licensed User
Longtime User
It's just pseudocode to make a point. Just remove the doevents and it would work (depending on what made the measurement value ready).

My point is that there are several legitimate cases of "Do this at least once until we get a desired result".
 

cimperia

Active Member
Licensed User
Longtime User
A bit late on this one, but I was looking for the same loop construct as kiki78. No joy.

Fortunately it's possible to keep the same logic with what we have at hand. For example re-using Troberg example

B4X:
Do While True
  InitMeasurementProbe()
  StartMeasure()  
  Do Until MeasuredValueReady()
    DoEvents
  Loop
  Measurement=GetMeasurement()
  CloseMeasurementProbe()
  If Measurement<>BadValue Then Exit 'Loop
Loop
LogFile(Measurement)
or
B4X:
dim On As Boolean = True
Do While On
  InitMeasurementProbe()
  StartMeasure()  
  Do Until MeasuredValueReady()
    DoEvents
  Loop
  Measurement=GetMeasurement()
  CloseMeasurementProbe()
  If Measurement<>BadValue Then On = False
Loop
LogFile(Measurement)
 

Troberg

Well-Known Member
Licensed User
Longtime User
Well, you can, of course do it in other ways, but they tend to be somewhat oblique and harder to follow, increasing the risk of bugs.
 

cimperia

Active Member
Licensed User
Longtime User
You won't get an argument from me. I agree that the built-in construct is cleaner and less prone to buggy code, I was merely pointing out that it was possible to get a near identical logic in this specific case.
 

emexes

Expert
Licensed User
+1

Microsoft may be the devil to some, but their orthogonal loop construct is a beautiful thing:
B4X:
DO [{WHILE | UNTIL} condition]

    [EXIT DO]

    [CONTINUE {DO | LOOP}]

LOOP [{WHILE | UNTIL} condition]
No, wait, maybe it's PowerBASIC. Or VAX BASIC. Regardless: is good.
 
Last edited:

emexes

Expert
Licensed User
But we can live even without it :)
Yes, but we can live even better with it - the more that code can be like natural language, the less translation effort is needed from code to thought and vice versa.

And, in Australia at least, Italians are known for enjoying life.

:)
 

emexes

Expert
Licensed User
There are always exceptions :(:D
If you are talking about programming language structure - agreed. But we can resist. That is the beauty of making a language more orthagonal and regular - it reduces the number of exceptions, increases your options, reduces your mental workload, leaves you more thought-power to apply to solving the actual problem rather than fitting within language constraints.

If you are talking about the Italian natural bent towards enjoying life - all I can say is that the Italians I've grown up with here are a very cheerful bunch, even before adding wine.

:)
 

LucaMs

Expert
Licensed User
Longtime User
The second one. You should watch an italian movie: https://en.wikipedia.org/wiki/A_Girl_in_Australia
(its original title is very different, you can read it there).

I think that in this case, "do - loop while", this fact does not complicate the programmer's life much or even the readability of the code, but it is useful to avoid duplicating one or two lines of code.

Rather, I feel the need for something else, for which perhaps a few threads have already been created in the "Bug & Wishlist" forum but I still want to open a new one, because just yesterday I happened to need it and I consider it much more important, its "absence" involves many problems.
 

emexes

Expert
Licensed User
The second one. You should watch an italian movie: https://en.wikipedia.org/wiki/A_Girl_in_Australia
That looks like an interesting movie. Probably a bit heartbreaking in places - seems to be mostly decent people each trying to improve their situation, during a period when life was difficult anyway. But still holds now: Life is what you make of it.

the readability of the code
It is easier to create art when the medium is pliable :)

avoid duplicating one or two lines of code
It's not the volume of code I care about (otherwise I'd voting to for the brevity of C, or even APL) but how well that code aligns with its intent, and thus how easy it is to understand what the code means to do. Eg, if I see FOR I = 1 TO 10 : stuff : NEXT I then I know understand stuff is meant to happen 10 times. If I see I = 1 : DO WHILE I < 10 : stuff : I = I + 1 : LOOP then I have to think a bit harder to arrive at the same understanding.

Rather, I feel the need for something else ... I consider it much more important, its "absence" involves many problems.
What is that something else? Given that we both seem passionate about B4A being a better language, I'll probably hop on that bandwagon with you too :)
 
Last edited:
Top