Break from an infinite loop in CSharp

Description

The following code shows how to break from an infinite loop.

Example


 using System;//  ww w . j a va  2  s  .  co m
 public class MainClass
 {
     public static void Main()
     {
         int counterVariable = 0;  // initialization

         while (true)
         {
             Console.WriteLine(
                 "counter: {0} ", counterVariable++); // increment

             if (counterVariable > 10) // test
                 break;
         }
     }
 }

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception