Else clauses for loops

spir denis.spir at gmail.com
Thu Apr 14 03:19:46 PDT 2011


On 04/13/2011 06:48 PM, bearophile wrote:
> Bernard Helyer:
>
>> You could wrap the loop in an if clause:
>>
>>      if (condition) while (true) {
>>          // ...
>>      } else {
>>          // ...
>>      }
>
> This is the semantics of the else clause of Python for (and while) loops:
>
> bool broken = false;
> for (...) {
>      if (...) {
>          broken = true;
>          break;
>      }
> }
> if (!broken) {
>   ...
> }
>
> I agree with BCS in the bug 2304, I'd like something more semantically descriptive instead of just an "else".

True. Python tends to overload its keywords, which leads to weird expression.
Also weird, again using "then":

	try:
	    n = 1 + 1
	except Exception, e:
	    print ("*error*")
	else:
	    print (n)

actually means

	try:
	    n = 1 + 1
	then:
	    print (n)
	except Exception, e:
	    print ("*error*")

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list