on Leave a Comment

Java Break Statement with Examples

The java break statement is a control statement and it is used to break the current flow of a program. The break statement can be used within both flow control statement and decision making statement. 

If a break statement is encountered in a loop, it causes the loop to terminate immediately and the control passes at the next statement following the loop. If break statement is encountered in inner loop, it breaks only inner loop.

The break statement can be used to break a decision making statement like if statement, if...else statement, nested if statement and switch statement.

In switch case, break statement can be used to terminate a case.

Syntax of break statement

break;

Example of break statement

class BreakStatement
{
 public static void main(String[] args) {
  int i;
  for (i =0; i<=10 ; i++) {
   if(i==6){
    break;
   }
   System.out.println(i);
  }
 }
}

Output:

0
1
2
3
4
5

0 comments:

Post a Comment