Kryvich 165 Posted July 23, 2019 I encountered a strange behavior when using the Continue procedure in a repeat statement. The simplified snippet: program RepContinue; {$APPTYPE CONSOLE} {$R *.res} var i: Integer; begin i := 5; repeat Writeln(i); Dec(i); if i > 0 then Continue; until True; Readln; end. It was expected that this program will print 5 numbers: "5 4 3 2 1". But in fact, it stops immediately after “5”. Is it a compiler error, or is it the way it should be? Share this post Link to post
Uwe Raabe 2057 Posted July 23, 2019 Continue is supposed to jump to the end of the current loop (re-evaluating the condition) and not to the beginning. Thus the until statement is evaluated which in this case ends the loop. So, yes, it is the way it should be. Quote Use the continue statement within loops to pass control to the end of the innermost enclosing end brace belonging to a looping construct, such as for or while; at which point the loop continuation condition is re-evaluated. 1 1 Share this post Link to post
Kryvich 165 Posted July 23, 2019 (edited) @Uwe Raabe Thank you. Although in my shining new Delphi 10.3.2 the wording is a little different: Quote Allows the flow of control to proceed to the next iteration of for, while, or repeat statements. In Delphi code, the Continue procedure causes the flow of control to proceed to the next iteration of the enclosing for, while, or repeat statement. Moreover, in the IDE a little green arrow behind Continue suggests that the control flow will go to the beginning of the cycle. Edited July 23, 2019 by Kryvich Share this post Link to post
David Schwartz 426 Posted July 23, 2019 I would expect it to go to the until statement and evaluate the condition rather than blindly jump to the top. Since both for and while loops have their condition test at the top, and repeat isn't used that often, the green up-arrow is meant to point to the condition, not the top of the loop. While the verbiage of the explanation may seem a bit ambiguous, the "iteration" is always gated by the condition. "until true" and "while true" are not the same, and they can be deceiving to a casual reader! Indeed, I think it fooled you as well. Personally, I'd use this because it's more obvious: while true do begin . . . end; Share this post Link to post
Uwe Raabe 2057 Posted July 23, 2019 2 hours ago, Kryvich said: Although in my shining new Delphi 10.3.2 the wording is a little different: That's interesting! I quoted right from the Rio DokWiki, but perhaps the offline help is a bit different. Although, my German offline help is a translation pretty close to the English DokWiki one. Nevertheless there are other places in the docs that support the wording you quoted, so I guess it is probably completely unclear which behavior is actually intended. So the least we can expect from reporting that issue is a documentation consistent with the compiler - however that turns out then. Share this post Link to post
Remy Lebeau 1393 Posted July 23, 2019 7 hours ago, Kryvich said: It was expected that this program will print 5 numbers: "5 4 3 2 1". But in fact, it stops immediately after “5”. Is it a compiler error, or is it the way it should be? As others have stated, it is working as designed. You need to change your 'until True' condition to 'until False' instead so the loop can run more than once. Personally, I would just re-write the condition to not use 'Continue' at all: program RepContinue; {$APPTYPE CONSOLE} {$R *.res} var i: Integer; begin i := 5; repeat Writeln(i); Dec(i); until i <= 0; Readln; end. Share this post Link to post
Kryvich 165 Posted July 23, 2019 (edited) @Remy Lebeau It was a simplified snippet to highlight the problem. In my code I tried to use a repeat-until statement instead of a label and a goto statement. This statement is intended to direct an execution back to the top of code block in a special case. Now I add a control variable to bypass the check. repeat var DoRepeat := False; ... if ... then begin ... if ... then begin DoRepeat := True; Continue; end; ... end; ... until not DoRepeat; Edited July 23, 2019 by Kryvich 1 Share this post Link to post
Fr0sT.Brutal 900 Posted July 25, 2019 repeat-until is slightly hard to realize construction because of its negative condition check. I use the following mnemonic: repeat stuff until condition is true. Share this post Link to post