C# tutorial- Loops-break and continue a loop


break and continue a loop

-Break

The break command can be used to exit a loop at any time. In the example code below, the for loop will exit when the value of i is equal to 5. The program will print "I like C# programming language." five times and then the loop will exit.

Example:

int i;
for (i = 1; i <= 10; i++)
{
   Console.WriteLine("I like C# programming language.");

   if(i==5) break;
}

 

-Continue

The continue command lets the program start the next iteration of the loop. See the example code below. The while loop continues if i is smaller than or equal to 5.

Example:

int i=1;
while(i <= 5)
{
   continue;
   Console.WriteLine("I like C# programming lanuage.");

   i=i+1;
}




Comments

Vipin Tyagi comment

 Vipin Tyagi

The continue loop not work in While condition.


2015-12-15
CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.