break and continue in java can jump out of the specified loop
break and continue in java can jump out of the specified loop. If no loop name is added after break and continue, it will jump out of the loop by default. If you add the specified loop name after it, you can jump out of the loop. Specify a loop (the specified loop is usually an outer loop nested within a loop).
Break jumps out of the specified loop sample code:
loop1:for(int x = 0; x < 4; x++) { loop2:for (int y = 0; y < 5 ; y++ ) { System.out.println("x="+x+",y="+y); if (y==2) { break loop1; } } }
Code running results:
Continue Jumps out of the specified loop sample code:
loop1:for(int x = 0; x < 4; x++) { loop2:for (int y = 0; y < 5 ; y++ ) { if (x==2) { continue loop1; } System.out.println("x="+x+",y="+y); } }
Code running results: