I have the following situation:
IF condition THEN IF condition THEN sequence 1 ELSE break //? ENDIF ELSE sequence 3 ENDIF
What is the result of the break statement? Does it break the outer if statement? Because this is what I actually need.
In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.
We can use an alternative method to exit out of an if or a nested if statement. We enclose our nested if statement inside a function and use the return statement wherever we want to exit.
breaks don't break if statements. But break s don't break out of if s. Instead, the program skipped an entire section of code and introduced a bug that interrupted 70 million phone calls over nine hours. You can't break out of if statement until the if is inside a loop.
If you label the if statement you can use break. You can even label and break plain blocks. It's not a commonly used pattern though, so might confuse people and possibly won't be optimised by compliers. It might be better to use a function and return, or better arrange the conditions.
If you label the if statement you can use break.
breakme: if (condition) { // Do stuff if (condition2){ // do stuff } else { break breakme; } // Do more stuff }
You can even label and break plain blocks.
breakme: { // Do stuff if (condition){ // do stuff } else { break breakme; } // Do more stuff }
It's not a commonly used pattern though, so might confuse people and possibly won't be optimised by compliers. It might be better to use a function and return, or better arrange the conditions.
( function() { // Do stuff if ( condition1 ) { // Do stuff } else { return; } // Do other stuff }() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With