Java break statement with label

  1. /*

  2.   Java break statement with label example.

  3.   This example shows how to use java break statement to terminate the labeled loop.

  4.   The following example uses break to terminate the labeled loop while searching two

  5.   dimensional int array.

  6. */

  7.  

  8. public class JavaBreakWithLableExample {

  9.  

  10. public static void main(String[] args) {

  11.  

  12.  

  13. int[][] intArray = new int[][]{{1,2,3,4,5},{10,20,30,40,50}};

  14. boolean blnFound = false;

  15.  

  16. System.out.println("Searching 30 in two dimensional int array..");

  17.  

  18. Outer:

  19. for(int intOuter=0; intOuter < intArray.length ; intOuter++)

  20. {

  21. Inner:

  22. for(int intInner=0; intInner < intArray[intOuter].length; intInner++)

  23. {

  24. if(intArray[intOuter][intInner] == 30)

  25. {

  26. blnFound = true;

  27. break Outer;

  28. }

  29.  

  30. }

  31. }

  32.  

  33. if(blnFound == true)

  34. System.out.println("30 found in the array");

  35. else

  36. System.out.println("30 not found in the array");

  37. }

  38. }

  39.  

  40. /*

  41. Output would be

  42. Searching 30 in two dimensional int array..

  43. 30 found in the array

  44. */

0 comments: