Source Code : Use break with a foreach(for each) style for.

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Use break with a foreach(for each) style for.

Use break with a foreach(for each) style for.
public class ForEach2 {  
  public static void main(String args[]) {  
    int sum = 0;  
    int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
 
    // Use for to display and sum the values. 
    for(int x : nums) {  
      System.out.println("Value is: " + x);  
      sum += x;  
      if(x == 5) break; // stop the loop when 5 is obtained  
    }  
    System.out.println("Summation of first 5 elements: " + sum);  
  }  
} 

           
       

Thank with us