[fpc-devel] Fwd: While - Otherwise Statement
Steve Smith
fski12 at yahoo.com
Mon Oct 12 20:26:14 CEST 2015
On 12/10/15 18:47, Dmitry Boyarintsev wrote:
>
> On Mon, Oct 12, 2015 at 1:35 PM, Ralf Quint <freedos.la at gmail.com
> <mailto: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
>
>
I can't think of a use case either. And just because another language
provides such a solution, does not imply that FPC should. As to achieving
the functionality in FPC:
if cond then
while cond do
foo
else
bar;
should suffice; Breakflags and gotos not required!
More information about the fpc-devel
mailing list