+ 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?

5th Dec 2018, 7:45 PM
ABADA S
ABADA S - avatar
8 Antworten
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
5th Dec 2018, 10:49 PM
vlada
+ 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; } } } } //...
5th Dec 2018, 7:55 PM
Babak
Babak - avatar
+ 4
Goto is a very interesting statement! Reminds me of the big plate of spaghetti! 8D
5th Dec 2018, 8:14 PM
Babak
Babak - avatar
+ 4
Amazing idea mr. valda I forgot it Thanks to all for helping
9th Dec 2018, 8:53 PM
ABADA S
ABADA S - avatar
+ 3
int check = 1 for (...) { for (...) { if (breaking_condition) { check = 0; break; } } if (!check) break; }
5th Dec 2018, 8:08 PM
HonFu
HonFu - avatar
+ 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:
5th Dec 2018, 8:11 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
When the goto-target is still in the same screen, I can't really see the harm (fusilli, more like ^^).
5th Dec 2018, 8:19 PM
HonFu
HonFu - avatar
+ 2
Brother make boolean from python
24th Nov 2020, 6:52 AM
✿★ミศอศཡརƴศཏ彡★✿
✿★ミศอศཡརƴศཏ彡★✿ - avatar