Latest web development tutorials

Java Examples - break Keyword Usage

Java Examples Java Examples

Java break statement can be directly forced to exit the current loop, the loop body is ignored in any other statements and cycling conditions tested.

The following example uses the break keyword to break out of the current cycle:

/*
 author by w3cschool.cc
 Main.java
 */

public class Main {
   public static void main(String[] args) {
      int[] intary = { 99,12,22,34,45,67,5678,8990 };
      int no = 5678;
      int i = 0;
      boolean found = false;
      for ( ; i < intary.length; i++) {
         if (intary[i] == no) {
            found = true;
            break;
         }
      }
      if (found) {
         System.out.println(no + " 元素的索引位置在: " + i);
      } 
      else {
         System.out.println(no + " 元素不在数组总");
      }
   }
}

The above code is run output is:

5678 元素的索引位置在: 6

Java Examples Java Examples