Home  >  Article  >  Java  >  Java Example - break keyword usage

Java Example - break keyword usage

黄舟
黄舟Original
2017-02-16 10:20:471503browse

Java break statement can forcefully exit the current loop directly, ignoring any other statements and loop condition tests in the loop body.

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

/*
 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 output result of the above code is:

5678 元素的索引位置在: 6

The above is the Java example - the content of the break keyword usage , for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn