Recently, I was going through an open-source project and although I have been developing for several years in .NET, I hadn't stumbled across the continue
keyword before.
Question: What are some best practices or areas that would benefit from using the continue
keyword? Is there a reason I might not have seen it previously?
You use it to immediately exit the current loop iteration and begin the next, if applicable.
foreach (var obj in list)
{
continue;
var temp = ...; // this code will never execute
}
A continue
is normally tied to a condition, and the condition could usually be used in place of the continue
;
foreach (var obj in list)
{
if (condition)
continue;
// code
}
Could just be written as
foreach (var obj in list)
{
if (!condition)
{
// code
}
}
continue
becomes more attractive if you might have several levels of nested if
logic inside the loop. A continue
instead of nesting might make the code more readable. Of course, refactoring the loop and the conditionals into appropriate methods would also make the loop more readable.
The continue
keyword is used to skip the rest of the loop block and continue on. For example:
for(int i = 0; i < 5; i++)
{
if(i == 3) continue; //Skip the rest of the block and continue the loop
Console.WriteLine(i);
}
Will print:
0
1
2
4
It prevents deep nesting.
foreach(var element in collection)
{
doSomething();
doSomethingElse();
if (condition1)
{
action1();
action2();
if (condition2)
{
action3();
}
}
}
could be rewritten as
foreach(var element in collection)
{
doSomething();
doSomethingElse();
if (!condition1)
{
continue;
}
action1();
action2();
if (!condition2)
{
continue;
}
action3();
}
If the code blocks are not trivial but vertically bigger, the use of continue
may improve code readibility. Obviously it should be used with consideration, like every other language construct.
When you don't want to break
out of the loop, but you want the next iteration:
for (int i = 0; i < something; i++)
{
if (condition)
continue;
// expensive calculations, skip due to continue
// or due to the condition above I don't want to
// run the following code
stuff();
code();
}
You should use it sparingly.
The best (= easiest to read) loops do not use break
or continue
, they are a kind of structured goto statements.
Having said that, 1 or even 2 break/continue statements won't make a loop unreadable, but it pays to make their use clear and to keep it simple.
Basically continue
and break
are better (but often just disguised) goto
statements...
Whenever you are inside a loop and know that everything coming next inside the loop should be skipped and continued with the next iteration you could use continue
...
As such they should be used seldomn... sometimes they make code very readable and clear (if the alternative would be several levels of nesting for example)... most of the time they add some confusion similar to goto
.
My guess as to why you haven't seen it previously is that continue
is sort of a cousin to goto
, break
, and early return
s from functions. And we all know that Goto is considered harmful, so many developers probably tend to avoid it.
For me, I tend to use continue
when I want to clean up a loop where there might be some values that I don't care about. Using continue
I can skip those values without enclosing the "important" logic in my loop in a nested if
.
foreach (var v in GetSomeValues())
{
if (ThisValueIsNotImportant(v)) continue;
//Do important stuff with the value.
}
Have I missed out on anything by not using it?
That seems like an odd question to ask. Presumably you know, better than anyone else, whether you've needed to start the next iteration of a loop early.
Think of it as a "return", but only applicable to the context of a loop. A common example is a state machine, looping over all available input.
while(!SomeQueue.Empty)
{
byte nextByte = SomeQueue.Dequeue();
switch State:
{
case A:
if(nextByte == Condition)
{
State = B;
}
else
{
State = ParseError;
}
continue;
case B:
//Test nextByte
State = C;
continue;
case C:
//Test nextByte
State = A;
continue;
case ParseError:
//Do something for this condition
State = A;
continue;
}
You can use continue statement for code optimization as well. when you are sure that out of all conditions, atmost one condition is going to meet. you can use it like below,
foreach (var element in List)
{
if (condition1)
{
//do stuff .....
continue;
}
if (condition2)
{
//do stuff ....
continue;
}
.
.
.
if (condition)
{
//do stuff ....
continue;
}
}
Here, when any of the If statement's condition is met, rest of all if statements will be skipped.
来源:https://stackoverflow.com/questions/7973115/when-to-use-the-continue-keyword-in-c-sharp