break statement exiting a for statement. - CSharp Language Basics

CSharp examples for Language Basics:break

Description

break statement exiting a for statement.

Demo Code

using System;/*from  www .  j a va2  s  .co  m*/
class BreakTest
{
   static void Main()
   {
      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($"{count} ");
      }
      Console.WriteLine($"\nBroke out of loop at count = {count}");
   }
}

Result


Related Tutorials