If you don't mind using goto, it can also be achieved in following way. This solutions saves from an extra if check and higher scope flag to track if loop is properly completed.
for(int i = 0; i < foo; i++)
if(bar(i))
goto m_label;
baz();
m_label:
...
Answer from ifyalciner on Stack OverflowIf you don't mind using goto, it can also be achieved in following way. This solutions saves from an extra if check and higher scope flag to track if loop is properly completed.
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();
While loop with else statement - C++ Forum
c++11 - Concise way to write a for/else in C++? - Stack Overflow
Can I use “for … else” loop in C++? - Stack Overflow
C++ language: if-else, for, while, do-while loops are so hard
Videos
Obviously...
if (map.empty())
{
// do stuff if map is empty
}
else for (auto it = map.begin(); it != map.end(); ++it)
{
// do iteration on stuff if it is not
}
By the way, since we are talking C++11 here, you can use this syntax:
if (map.empty())
{
// do stuff if map is empty
}
else for (auto it : map)
{
// do iteration on stuff if it is not
}
If you want more crazy control flow in C++, you can write it in C++11:
template<class R>bool empty(R const& r)
{
using std::begin; using std::end;
return begin(r)==end(r);
}
template<class Container, class Body, class Else>
void for_else( Container&& c, Body&& b, Else&& e ) {
if (empty(c)) std::forward<Else>(e)();
else for ( auto&& i : std::forward<Container>(c) )
b(std::forward<decltype(i)>(i));
}
for_else( map, & {
// loop body
}, [&]{
// else body
});
but I'd advise against it.
No, there is nothing equivalent to for-else of Python in C++.
There are a few alternatives to transform the function. I'll use a simpler example:
for i in range(a, b):
if condition(i):
break
else:
not_found()
The closest transformation can be achieved using goto, but you may find that some colleagues are likely to object (because goto is "evil" and scary):
for (int i = a; i < b; i++) {
if (condition(i)) {
goto found;
}
}
not_found();
found:;
Other alternatives, which are also possible in Python are to introduce an additional boolean variable and if:
bool found = false;
for (int i = a; i < b; i++) {
if (condition(i)) {
found = true;
break;
}
}
if (!found) {
not_found();
}
Or to encapsulate the whole thing within a function, which I think is the most widely accepted alternative both in Python and in C++.
void function()
{
for (int i = a; i < b; i++) {
if (condition(i)) {
return;
}
}
not_found();
}
You can achieve something similar. Simply add a branch to test if the exit condition for the loop is satisfied. Here is an example:
#include <cstdio>
int main() {
for (int n = 2; n < 10; ++n) {
int x = 2;
for (; x < n; ++x) {
if (n % x == 0) {
std::printf("%d equals %d * %d\n", n, x, n / x);
break;
}
}
if (x == n)
std::printf("%d is a prime number\n", n);
}
}
I can understand the basic concepts of if-else, for, while, and do-while loops. But, in which situation, use which kind of loop is always so confusing. You have to mix this loop with another loop, and then sometimes have to perform nested loops. I always jammed and could not overcome this.
Edit: I messed up the title. Should read "else if" is not a keyword.
Original:
Because I first learned Basic's "elseif" keyword, I always assumed C, C#, Javascript's "else if" was just a variant of Basic's keyword with an extra space.
Nope.
Those languages only have if/else. There is no language keyword elseif or "else if".
The reason they don't need it is because the "else" is designed to run whatever statement or compound statement that is after the else. And in the case we care about, "else if(){}" the "if() {}" is treated as a compound statement. It's no different than log(100); or { if(isHundred ) log(100); else logNan();}
So while C/C++, Javascript can "else if" it's really just an else and an if. "else if" is nothing special.
Basic and Python lua require elseif because they don't have the single statement or compound statement rule and every else must be matched to an endif or end. Edit: Python uses elif to reduce indentation.
Anyway, apologies if this was obvious. It's really a distinction without a difference but I guess it goes to show that we are always learning and new concepts are always clicking even after decades of using a language.