[fpc-devel] Fwd: While - Otherwise Statement

wkitty42 at windstream.net wkitty42 at windstream.net
Mon Oct 12 21:06:02 CEST 2015


On 10/12/2015 01:47 PM, Dmitry Boyarintsev wrote:
> 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.

that just doesn't make grammatical sense...

foo := 0;
while foo < 100 do
   inc(foo);
else
   dec(foo);
end


so, if foo is less than 100 we inc(foo)... if foo is not less than 100 we 
dec(foo)... that's the only way that an "else" can be read... either the boolean 
is true or /else/ it is false... only if it is false can the else portion be 
executed...

(#2) but then the question is does dec(foo) execute only once or does it execute 
as long as foo is greater than 100??

what i seem to see is pure laziness of a sort... folks too lazy to write an "if" 
or "if/else" statement and put the while inside...


foo := 0;
if foo < 100 then
   while foo < 100 do
     inc(foo);
else
   dec(foo);


OR depending on the answer of (#2) above...


foo := 0;
if foo < 100 then
   while foo < 100 do
     inc(foo);
   while foo > 100 do
     dec(foo);


or maybe this is the actual intent?


foo := 0;
if foo < 100 then
   while foo < 100 do
     inc(foo);
if foo > 100 then
   dec(foo);


seems like a pretty tough way to have foo equal to 99, eh? ;)

> 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.
[...]
> 2) use goto! :)

neither is needed at all in most cases and then not if they have proper logic in 
place ;)

-- 
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list* unless
        private contact is specifically requested and granted.



More information about the fpc-devel mailing list