Let's consider:
switch(x)
{
case something:
{
for(something_else : some_container)
{
if(a_condition)
{
// BREAK OUT OF THE SWITCH
}
}
some_statements();
break;
}
}
How can I escape in the most elegant way from the loop which is inside a switch
statement since I do not want to run some_statements();
.
Certainly a well placed goto
would nicely solve this problem, but the solution can be considered anything but elegant:
switch(x)
{
case something:
{
for(something_else : some_container)
{
if(a_condition)
{
goto escape_route;
}
}
some_statements();
break;
}
}
escape_route:
// Hurrah, freedom
And also a flag would solve this problem:
switch(x)
{
case something:
{
bool flag = true;
for(something_else : some_container)
{
if(a_condition)
{
flag = false;
break;
}
}
if(flag)
{
some_statements();
}
break;
}
}
But let's just say, that I am looking for other solutions for this problem for the challenge's sake (Please note: There are more statements after the switch, return
ing is not an option).
Any ideas?