Java Break

When a break statement is executed inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.

We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.

The break statement can also be used to jump out of a loop.

Syntax:

break;   

 

Example :

import java.util.*;

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

int k = 0;
while (k < 7) {
System.out.println("A Number is :" + k);
k++;
if (k == 5) {
break;
}
}
}
}

Output :

A Number is :0
A Number is :1
A Number is :2
A Number is :3
A Number is :4

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

17087