Break statement exiting a for statement. - Java Language Basics

Java examples for Language Basics:for

Description

Break statement exiting a for statement.

Demo Code

public class Main 
{
   public static void main(String[] args)
   {//w  w w  . j av  a  2s.  co  m
      int count; // control variable also used after loop terminates
      
      for (count = 1; count <= 10; count++) // loop 10 times
      {  
         if (count == 5)  
            break; // terminates loop if count is 5

         System.out.printf("%d ", count);
      }

      System.out.printf("%nBroke out of loop at count = %d%n", count);
   } 
}

Result


Related Tutorials