Is there an equivalent to the "for ... else" Python loop in C++? Is there an equivalent to the "for ... else" Python loop in C++? python python

Is there an equivalent to the "for ... else" Python loop in C++?


If doesn't mind using goto also can be done in following way. This one saves from extra if check and higher scope variable declaration.

for(int i = 0; i < foo; i++)     if(bar(i))         goto m_label;baz();m_label:...


A simpler way to express your actual logic is with std::none_of:

if (std::none_of(std::begin(foo), std::end(foo), bar))    baz();

If the range proposal for C++17 gets accepted, hopefully this will simplify to:

if (std::none_of(foo, bar)) baz();


Yes you can achieve the same effect by:

auto it = std::begin(foo);for (; it != std::end(foo); ++it)     if(bar(*it))         break;if(it == std::end(foo))    baz();