How to use break statement in java ?

How to use break statement in java ?

Previous Home Next

 

In this java program we will saw how the break statement is used in java program.

Break statement is used to terminate the loop in the program.
 intarray[i]=8

it check the condition before i became 8

after checking the condition in intarray[i]==8 
the loop will terminate .it will goes till  i became 8

 
public class BreakExample {
public static void main(String[] args) {

int intArray[] = new int[]{1,2,3,4,5,6,7,8,9,10}

System.out.println("number of elements less than 8 are : ");

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

{

if(intArray[i] == 8)

break;

else

System.out.println(intArray[i]);
}
}
}

number of elements less than 8 are
1
2
3
4
5
6
7
Previous Home Next