Table of Contents

    Understanding the Continue Statement in C#: Syntax and Usage Explained

    Understanding the Continue Statement in C#: Syntax and Usage Explained

    Loop Control Statements

    Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

    C# provides the following control statements. Click the following links to check their details.

    Sr.No. Control Statement & Description
    1 break statement

    Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

     

    2 continue statement

    Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

    The continue statement in C# works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.

    For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...whileloops, continue statement causes the program control passes to the conditional tests.

    Syntax

    The syntax for a continue statement in C# is as follows −

    continue;

    Flow Diagram

    Flowchart of break statement

    if statement in C

    How break statement works?

    if statement in C

    Example

    
    using System;
    
    namespace Loops {
       class Program {
          static void Main(string[] args) {
             /* local variable definition */
             int a = 10;
             
             /* do loop execution */
             do {
                if (a == 15) {
                   /* skip the iteration */
                   a = a + 1;
                   continue;
                }
                Console.WriteLine("value of a: {0}", a);
                a++;
             } 
             while (a < 20);
             Console.ReadLine();
          }
       }
    } 
    

    When the above code is compiled and executed, it produces the following result −

    value of a: 10
    value of a: 11
    value of a: 12
    value of a: 13
    value of a: 14
    value of a: 16
    value of a: 17
    value of a: 18
    value of a: 19