+ 7
Super break
When I write a nested loop how can I break the outer loop by a condition in the inner loop for example: for(int i=0;i<5;i++) { for(int j=1;j<5;j++) if(i==j&&j>3) /* a keyword that break the outer loop */ } Is there any keyword like that in java or c?
8 Answers
0
If it is applicable, you can put nested loops in separate method, and than when conditions are met, use return for breakOut from that method. Look at the solution here:
https://code.sololearn.com/cbue0htq8pQH/?ref=app
+ 5
I'd prefer to use auxiliary flags for each level of nesting.
A simple example:
bool f1 = false,
f2 = false;
while ( !f1 ) {
while ( !f2 ) {
while ( true ) {
// do stuff...
if ( some condition met ) {
f1 = true;
f2 = true;
break;
}
}
}
}
//...
+ 4
Goto is a very interesting statement!
Reminds me of the big plate of spaghetti! 8D
+ 4
Amazing idea mr. valda I forgot it
Thanks to all for helping
+ 3
int check = 1
for (...) {
for (...) {
if (breaking_condition) {
check = 0;
break;
}
}
if (!check)
break;
}
+ 3
Java can break to labels:
label:
for... {
for... {
if(...) break label;
}
}
// execution continues here
In C this is one case where 'goto' is used:
for... {
for... {
if(...) goto end;
}
}
end:
+ 2
When the goto-target is still in the same screen, I can't really see the harm (fusilli, more like ^^).
+ 2
Brother make boolean from python