Monday 29 August 2016

Java break

5. break statement
  • we can use only inside loops and switch statement to terminates the execution.
Syntax:

        break ; 
--------------------------------------------------------------------------------
Program         :     A program by using break statement
Program name  :     BreakDemo1.java
Output            : 
                            3

class BreakDemo1
{
       public static void main(String args[])
       {
           for(int i = 1; i<=10; i++) 
           {
              if( i == 3 ) 
              {
                  System.out.print(i);
                  break;
              }
           }
       }
}

Compile     :           javac BreakDemo1.java
Run           :           java BreakDemo1
Output       : 
                            3
  --------------------------------------------------------------------------------