Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

From the author of

continue Statements

continue statements are used in loops. They allow a program to jump immediately to the Boolean expression of the loop. Here's a program snippet that shows how to use a continue statement to discontinue processing during a given iteration:

foreach(string site in siteName)
{
 if (response.ToUpper() == "Y" &&
  site != null &&
  site.IndexOf(filter) == -1)
 {
  continue;
 }

 Console.WriteLine("\t{0}", site);
}

This example checks the current array entry against a predefined filter. The IndexOf() method, a predefined string function, returns -1 if the value of filter does not exist in the site string. When the value is -1, the continue statement is invoked. This sends program control back to the top of the foreach loop for another iteration.

For Java Programmers

Java has a labeled continue statement, but C# does not.

Had the continue statement not been used, this program would need alternate or additional logic to avoid executing the Console.WriteLine() statement. With the continue statement, the program explicitly expresses its intent. The continue statement increases the efficiency and understandability of a program by avoiding execution of unnecessary logic.

  • + Share This
  • 🔖 Save To Your Account