Home > Java > javaTutorial > body text

Java Example - break keyword usage

黄舟
Release: 2017-02-16 10:20:47
Original
1505 people have browsed it

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 + " 元素不在数组总");
      }
   }}
Copy after login

The output result of the above code is:

5678 元素的索引位置在: 6
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template