C# loop - break vs. continue
break
will exit the loop completely, continue
will just skip the current iteration.
For example:
for (int i = 0; i < 10; i++) {
if (i == 0) {
break;
}
DoSomeThingWith(i);
}
The break will cause the loop to exit on the first iteration - DoSomeThingWith
will never be executed. This here:
for (int i = 0; i < 10; i++) {
if(i == 0) {
continue;
}
DoSomeThingWith(i);
}
Will not execute DoSomeThingWith
for i = 0
, but the loop will continue and DoSomeThingWith
will be executed for i = 1
to i = 9
.
Difference between break and continue statement
break
leaves a loop, continue
jumps to the next iteration.
When to use break, and continue in C language?
Break,as the name suggests will break the execution of current loop once and for all while Continue will skip the execution of following statements and start new iteration.
It is known that loop may have it's termination condition, but sometimes it is possible that you achieve your goal before all the iteration has been executed (for example, consider best case scenario of Linear Search. It is possible that you found your element on 1st or 2nd iteration.)
In case of CONTINUE, sometimes it's possible that we need to skip some statement to get executed, but don't want to break the loop.
(For example as given in the link, the requirement is to sum the positive elements of array.In this case you need to skip negative elements, which can be achieved by Continue keyword.)
Yes, you can use them both with loop and without loop (for i.e in if..else or else..if ladder).
And yes, it is certainly a good practice to use them as both can save lot of execution time according to requirements.
For more info: Click here and here
Hope it will help!!
break and continue in a loop enclosed switch block
If there are some lines of code after the switch, continue keyword will ignore them. Try this and you will see the different:
while(condition)
{
switch(anumber)
{
case 0:
//do something
break;
case 1:
//do something
break;
//and so on
}
Console.WriteLine("it's a message");
}
and
while(condition)
{
switch(anumber)
{
case 0:
//do something
continue;
case 1:
//do something
continue;
//and so on
}
Console.WriteLine("it's a message");
}
ForEach() : Why can't use break/continue inside
Because ForEach
is a method and not a regular foreach
loop. The ForEach
method is there for simple tasks, if you need to break or continue just iterate over lstTemp
with a regular foreach
loop.
Usually, ForEach
is implemented like this:
public static ForEach<T>(this IEnumerable<T> input, Action<T> action)
{
foreach(var i in input)
action(i);
}
As it is a normal method call, action
doesn't know anything about the enclosing foreach
, thus you can't break.
Related Topics
Using Reflection in C# to Get Properties of a Nested Object
5.7.57 Smtp - Client Was Not Authenticated to Send Anonymous Mail During Mail from Error
How to Keep the Delimiters of Regex.Split
How to Post Using Httpclient Content Type = Application/X-Www-Form-Urlencoded
In Unity, How to Pass Values from One Script to Another
How to Encrypt Selected Properties When Serializing My Objects
How to Read User Input from the Console
Read Fixed Width Record from Text File
Ef Code-First One-To-One Relationship: Multiplicity Is Not Valid in Role * in Relationship
How to Configure Swashbuckle to Ignore Property on Model
Data Binding in Wpf User Controls
How to Add a .Dll Reference to a Project in Visual Studio
Page.Datacontext Not Inherited from Parent Frame
How to Get First Record in Each Group Using Linq
How to Multi-Target a .Net Core Class Library with Csproj