I have a if statement that I want to "break" out of. I understand that break is only really for loops. Can anyone help?
For those that require an example of what I'm trying to do:
if( color == red )
{
...
if( car == hyundai ) break;
...
}
This question is related to
c++
Nested ifs:
if (condition)
{
// half-massive amount of code here
if (!breakOutCondition)
{
//half-massive amount of code here
}
}
At the risk of being downvoted -- it's happened to me in the past -- I'll mention that another (unpopular) option would of course be the dreaded goto
; a break statement is just a goto in disguise.
And finally, I'll echo the common sentiment that your design could probably be improved so that the massive if statement is not necessary, let alone breaking out of it. At least you should be able to extract a couple of methods, and use a return:
if (condition)
{
ExtractedMethod1();
if (breakOutCondition)
return;
ExtractedMethod2();
}