How do I exit a nested loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I exit a nested loop?

4th Apr 2018, 8:34 PM
Alex Go
Alex Go - avatar
7 Answers
+ 3
bool ok = false for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (something){ ok = true; break; } } if (ok) break; }
4th Apr 2018, 8:44 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 1
a loop nested in what,another loop? function?well I know of the break keyword
4th Apr 2018, 8:38 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
Can you show us some code ? I do think breaking out of a nested loop is bad practice. And you can organise you code different so you do not need the nested loop.
4th Apr 2018, 8:45 PM
sneeze
sneeze - avatar
+ 1
A flag. Good. Thanks
4th Apr 2018, 8:46 PM
Alex Go
Alex Go - avatar
0
After a nested loop. One break; exits to external loop. Two break; will not work.
4th Apr 2018, 8:42 PM
Alex Go
Alex Go - avatar
0
Very nice Christian. Perfect. Great!
4th Apr 2018, 8:43 PM
Alex Go
Alex Go - avatar
- 7
U turn off the device
4th Apr 2018, 8:42 PM
Christian t Davies
Christian t Davies  - avatar