Navigating the Dynamics of Continue and Break Statements in C#
In this concise article, let’s delve into the intriguing disparities between the Continue and Break statements.
The continue statement takes center stage as it gracefully skips a line, seamlessly transferring the process back to the loop’s inception.
for (int i = 0; i < 10; i++)
{
if (i == 7)
{
continue;
}
Console.WriteLine(i);
}
As you can see above, we skipped the number 7 and transferred the process to the beginning of the loop.
The break statement, on the other hand, serves a different purpose — it allows the process to exit the loop or current situation, in other words, acting as the key to liberating the statement from its loop-bound constraints.
for (int i = 0; i < 10; i++)
{
if (i == 7)
{
break;
}
Console.WriteLine(i);
}
As you can see above, the loop was iterated up to 7 and then exited. You can access the code here.