How to use break statement to exit a for statement

using System;
 
public class ExitBreakFor
{
   public static void Main( string[] args )
   {
      int count; // control variable also used after loop terminates
 
      for ( count = 1; count <= 10; count++ ) // loop 10 times
      {
         if ( count == 5 ) // if count is 5, 
            break; // terminate loop
 
         Console.Write( "{0} ", count );
      } // end for
 
      Console.WriteLine( "\nBroke out of loop at count = {0}", count );
   } // end Main
} // end class ExitBreakFor

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.