[fpc-devel] Fwd: While - Otherwise Statement

Dmitry Boyarintsev skalogryz.lists at gmail.com
Mon Oct 12 19:47:54 CEST 2015


On Mon, Oct 12, 2015 at 1:35 PM, Ralf Quint <freedos.la at gmail.com> wrote:

>
> Either the while loop is executed or it isn't, depending in the
> expression. I don't see an actual use case for any else/otherwise extension
> to it...
>

You probably want to reread python while-else implementation. (
https://docs.python.org/2/reference/compound_stmts.html)
"Else" becomes sort of "part of the loop". Thus if you break out of the
loop, "else" would not be executed.
However, if no break occurs "else" part would be executed.

It might be a rare case where it's needed. (I cannot think of any), but to
achieve exactly the same functionality in Pascal either of two options
should be used.

1) make an extra check if break occurred.

breakflag:=false;

while cond do
  if someothercond then begin
    breakflag : =true;
    break;
  end;

if not breakflag then
  while_else_code

2) use goto! :)

label postElseLabel

while cond do
  if someothercond then begin
     goto  postElseLabel
  end;

while_else_code
:postElseLabel

thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20151012/a33ca3dc/attachment.html>


More information about the fpc-devel mailing list