3 ms·
Yes, "else" is a strange keyword to use for it, but I have found the existing semantics to be useful in practice. The application is nearly always related to ha
by ggchappell 14y ago
Yes, "else" is a strange keyword to use for it, but I have found the existing semantics to be useful in practice. The application is nearly always related to handling success/failure of some kind of search. In Python, for..else allows elimination of variables whose names are something like "found".
In C++:
bool found = false;
for (...) // Look for something
{
XXX;
if (YYY)
{
found = true;
break;
}
}
if (!found) // Didn't find it
{
ZZZ;
}
but in Python:
for ...: # Look for something
XXX
if YYY: break
else: # Didn't find it
ZZZ