Else clauses for loops

bearophile bearophileHUGS at lycos.com
Wed Apr 13 09:48:52 PDT 2011


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".
It's handy, I use it now and then in Python, but I am able to live without it in D.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list