Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Usage of the break keyword

Java Examples - break Keyword Usage

Java Examples Java Examples

The Java break statement can directly force an exit from the current loop, ignoring any other statements in the loop body and the loop condition test.

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

Main.java File

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

"

The element's index position is at:

"

i

)

;

}

else

{

System

.

out

.

println

(

no

"

The element is not in the array

"

)

;

}

}

}

The output of the above code is:


5678 元素的索引位置在: 6

Java Examples Java Examples