Statements as expressions [was Re: Undefined behaviour in C]

Ben Bacarisse ben.usenet at bsb.me.uk
Mon Mar 28 08:55:50 EDT 2016


BartC <bc at freeuk.com> writes:

> On 28/03/2016 01:54, Steven D'Aprano wrote:
<snip>
>> What should be the return value of this statement?
>>
>> while True:
>>      x += 1
>>      if condition: break
>
> Many statements such as loops just returned 'void' in A68. The
> interesting statements were if, case-switch and block (where the value
> is that of the last statement/expression).

An alternative would be for a loop to return the last value of its
controlling expression.  This is usually false for a whole loop but
would be true is the loop was exited using break.  In a for loop, the
value would be the last value of its control variable.  This would again
permit you to tell a break from a normal for loop finishing.  This way
you can make the loop variable local and still use its final value
outside the loop.

Both would render some flag variables and some auxiliary variables
redundant though I've not seen a language that does it.  Maybe in
practice it does not work out so well, but it looked useful when I
sketch it out a while back.

<snip>
-- 
Ben.



More information about the Python-list mailing list