We can leave a "for" loop prematurely with the "break" command:
/*---------------------------*/
/* C# Example #24: */
/* example24.cs */
/* */
/* "for" with "break" */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example24
{
public static void Main()
{
int counter;
for (counter=1; counter<=10; counter++)
{
if (counter==5)
break;
Console.Write("{0} ", counter);
}
}
}
The result of this program is:
1 2 3 4
(As the value 5 is reached, the loop is interrupted "for", so the limit value 10 is not reached).
No comments:
Post a Comment