Thursday, 9 January 2014

JUMP STATEMENTS OR BRANCHING STATEMENTS:break

 Jump statements:break,continue,and return:These statements transfer control to another part of your program.

Using break statement


break:


Break statement is one of the several control statements Java provide to control the flow of the program.Break Statement is generally used to break the loop of switch statement.In java,break statement has three uses.First ,as you  have seen,it terminates a statement sequence in a switch statement.Second,it can be used to exit a loop.Third,it can be used as a : "civilized" form of goto.

Let’s look at syntax of break statement:

break;



The break statement is written as break; without any expression.

Example for simple break  statement

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

This would produce the following results

0
1
2
3