Pages

Sunday 20 November 2016

Problem 31 - Break

  1. public class JavaBreakExample {
  2.  
  3.   public static void main(String[] args) {
  4.     /*
  5.      * break statement is used to terminate the loop in java. The following example
  6.      * breaks the loop if the array element is equal to true.
  7.      *
  8.      * After break statement is executed, the control goes to the statement
  9.      * immediately after the loop containing break statement.
  10.      */
  11.      
  12.     int intArray[] = new int[]{1,2,3,4,5};
  13.    
  14.     System.out.println("Elements less than 3 are : ");
  15.     for(int i=0; i < intArray.length ; i ++)
  16.     {  
  17.       if(intArray[i] == 3)
  18.         break;
  19.       else
  20.         System.out.println(intArray[i]);
  21.     }
  22.    
  23.   }
  24. }

0 comments:

Post a Comment