If there was a jump statement "break" inside a nested loop for and it was inside the second for... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

If there was a jump statement "break" inside a nested loop for and it was inside the second for...

will the compiler pass out from the both loops ??

25th Apr 2017, 2:09 PM
Saja Husam
Saja Husam - avatar
3 Answers
+ 4
no. break stops the last loop that started. loop 1 started then loop 2 and then the break. if the break is inside loop 2 loop 1 will continue.
25th Apr 2017, 2:13 PM
Alex Lesage
Alex Lesage - avatar
+ 27
If you need to break the outer loop from inner loop, you can use label like this: http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java
25th Apr 2017, 2:25 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
if u can give code example it will help to get the full picture. Any how I believe this is wht u asking. for(){ //#1 break; //#b1 for(){ //#2 break;. //#b2 } } if #b2 break execute it will break only the #2 for loop and #1 will not break. if #b1 break it break the #1 for loop and it wouldn't not go to next line so #2 for loop will not execute. Simply break statement only break the loop it is belongs to.
25th Apr 2017, 2:21 PM
Eranga
Eranga - avatar