Home
|
Tutorial
|
Articles
|
Forum
|
Interview Question
|
Code Snippets
|
News
|
Fun Zone
|
Poll
|
Web Links
|
Certification
|
Search
Welcome :
Guest
Sign In
Register
Win Surprise Gifts!!!
Congratulations!!!
Top 5 Contributors of the Month
ffttt
Home
>>
Forum
>>
C#
>>
Post New Question
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
Related Questions Related Questions
What is foreach Statement
HTML Line Break tag is not working in Chrome browser
Break Point in 2010 VS .net
Please differentiate these sql statement
Can we use Case Statement in Update Query?
Oracle CASE When statement
How do we use CASE WHEN statement in ms access query and please give me sample?
CASE WHEN Statement in MS Access query
Edit and Continue in vb.net
Insert Statement doesn''t works from a web application
Latest Forum Questions From The Same Category
To Achieve draw wave form from speaker outputs.
How to display the text in the second ck editor with strikes while extracting from database
Mail is working but I want send two excel file to each different person
how to display child nodes with headers and keeping the child nodes in a table
In DataGrid I have Two Dropdownlist, One in Header template and Item Template
Display data directly from database table to PDF using stored procedures in asp.net C#
How to install and use Dev Express ?
Dialogbox Custom Control
Custom Control in Winforms
I just started learning c#.net console application
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more
Here
Quick Links For Forum Categories:
ASP.Net
Windows Application
.NET Framework
C#
VB.Net
ADO.Net
Sql Server
SharePoint
OOPs
Silverlight
IIS
JQuery
JavaScript/VBScript
Biztalk
WPF
Patten/Practices
WCF
Others
www.DotNetSpark.com
UnAnswered
All
Hall of Fame
Twitter
Terms of Service
Privacy Policy
Contact Us
Archives
Tell A Friend