Use break to exit a for loop in CSharp

Description

The following code shows how to use break to exit a for loop.

Example


using System; /*  w w  w. j  a va2  s  . c o  m*/
 
public class MainClass {     
  public static void Main() {  
 
    // use break to exit this loop 
    for(int i=-10; i <= 10; i++) {  
      if(i > 0) break; // terminate loop when i is positive 
      Console.Write(i + " ");  
    }  
    Console.WriteLine("Done");  
  }  
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




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