.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

C# break and continue statement

Posted By: Deco     Posted Date: September 27, 2010    Points:2   Category :C#
what is the use of break and continue statement in c#

in what scenario will we use that?

Responses
Author: Suthish Nair          Accepted Answer   
Posted Date: September 27, 2010     Points: 10   


break statement leaves a loop when a condition met,
continue statement jumps to the next iteration, skipping the condition o/p.


refer an example: http://www.meshplex.org/wiki/C_Sharp/Break_and_Continue

suthish nair
Author: Zinnia             
Posted Date: September 28, 2010     Points: 5   

If we want to exit the loop on meeting certain conditions, then we should use BREAK statement. BREAK statement executes the rest of the statements, even after exiting the loop.
Whereas, if we want to restart the loop, we should use CONTINUE statement. CONTINUE statement restarts the loop, increases the loop counter and skips the statements which are within the loop but after the CONTINUE statement.

Zinnia Sarkar
http://sarkarzinnia.blogspot.com
Author: Zinnia             
Posted Date: September 28, 2010     Points: 5   

My same answer had been posted twice by mistake. so i deleted one.

Zinnia Sarkar
http://sarkarzinnia.blogspot.com
Author: Pradeep Kumar Gupta             
Posted Date: October 03, 2010     Points: 5   

The break statement alters the flow of a loop. It immediately exits the loop based upon a certain condition. After the loop is exited it continue to the next block of instructions. Exiting a loop early can boost program performance, it avoids unnecessary loops.

Lets take a look at some real code. The code is suppose to loop ten times but will exit prematurely and only loop five times.

CODE FOR BREAK

using System;

class Program
{
static void Main(string[] args)
{
for(int count = 1; count <= 10; count++)
{
if (count >= 6)
break; //if condition is met
//exit the loop

Console.WriteLine(count);
}
Console.Read();
}
}


As you can see when the if condition is evaluated to true the break statement is executed. The for loop is prematurely exited immediately and it does not execute any other code in the loop. The break statement also works for while, do/while, and switch statements. \



C# Continue Statement



The continue statement jumps over the following code within a loop. In other words it skips the proceeding code in the loop and continues to the next iteration in the loop. It does not exit the loop like the break will. Just like the break to work properly the continue statement needs an if condition to let the program know that if a certain condition is true the continue statement must be executed.

When the continue and break statements are used together it can greatly increase program performance within the loop structure. In the program example below there is an algorithm that will count from 1 to 10. The program will skip the fifth iteration to prove that the continue statement actually works.

CODE FOR CONTINUE STATEMENT

using System;

class Program
{
static void Main(string[] args)
{
for(int count = 1; count <= 10; count++)
{
if (count == 5)
continue; //condition is met
//skip the code below

Console.WriteLine(count);
}
Console.Read();
}
}

The output skipped the number 5. When the condition is true the continue statement is executed and the remaining code is skipped. That means Console.WriteLine(count); command is skipped on the fifth iteration.





Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend