Python has an interesting for
statement which lets you specify an else
clause.
In a construct like this one:
for i in foo:
if
You could use a lambda function for this:
[&](){
for (auto i : foo) {
if (bar(i)) {
// early return, to skip the "else:" section.
return;
}
}
// foo is exhausted, with no item satisfying bar(). i.e., "else:"
baz();
}();
This should behave exactly like Python's "for..else", and it has some advantages over the other solutions:
But... I'd use the clunky flag variable, myself.