Can anyone tell me how this program works | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone tell me how this program works

public class Count { public static void main(String[] args) { int count=0; for (int i=0;i<3;i++) resume : for (int j=0;j<4;j++) for (int k=0;k<5;k++) { ++count; if(i==1 && j==2 && k==3)break resume ; } System.out.println("\t count ="+ count); } }

24th May 2020, 6:05 PM
Raku
Raku - avatar
2 Answers
+ 1
In normal cases you let "break" stop the inner loop, so the one where it is defined in. In some cases you want to break also one or two outer loops. Example: You are going to find the king of one color on a chess board. You have two loops, one for an x-coordinate and one for y-coordinate. When you find the king, you can stop both loops. You could create an extra variable or manipulate the variable of the outer loop. Java offers the possibility to label loops. In your case that is "resume:". With appending the label to the break statement it is defined to which outer level you want to stop the loops.
24th May 2020, 6:29 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
0
break just break current loop. But there resume is label in outermost loop.. It is concept from C- labels. break resume; => breaks the outermost loop when if condition is true... In java, you can use labels with break and continue....
24th May 2020, 6:34 PM
Jayakrishna 🇮🇳