C# Continue Statement

The continue statement in C# is employed to proceed with a loop, enabling the program to bypass the subsequent code based on a specified condition. When used within an inner loop, it exclusively advances the inner loop, directing the program to execute the subsequent iteration of the loop. This statement essentially compels the program control to move on to the next loop iteration.

The continue statement is employed within the program's loop. Essentially, when the continue statement is encountered in a loop, the code block following it is bypassed, and the loop proceeds to the next iteration.

Syntax:

It has the followings syntax:

Example

//loop statements

continue; //continue statement

//Code lines, which are to be skipped

Flow Diagram of the Continue Statement:

The styling for the continuation diagram includes a linear gradient background with specified colors and properties like border radius, padding, and margin. Each component within the diagram, such as the title, flow nodes, and arrows, is styled uniquely with different background colors, text alignment, font weights, and colors to distinguish various parts of the flow. Styling for decisions, start nodes, and continuation nodes are customized to stand out with specific colors and font weights. The layout of the diagram is designed to be visually appealing and easy to follow, with consistent spacing and alignment throughout.

Follow these steps outlined in the flowchart to incorporate the continue statement in C#:

Step 1: To begin, navigate to the loop body of the program.

In the subsequent step, it verifies the loop condition to proceed with the next iteration.

If the condition evaluates as true, the loop proceeds to run the body repeatedly until the condition becomes false.

Step 4: When the condition evaluates to false, the loop proceeds to execute the rest of the code within its body.

Step 5: Finally, it prints the output.

Working of Continue Statement:

In C# programming, the continue statement is compatible with various types of loops such as the for loop, while loop, do-while loop, for-each loop, and nested loop structures.

1. Continue Statement with for Loop in C#:

In C#, when a continue statement is used within a for loop, it skips the current iteration, directing the program flow to the subsequent iteration within the loop body.

Syntax:

It has the following syntax:

Example

for (initialization; condition; update)

{

    if (expression)

    {

        continue; //continue statement

    }

    // Code to be executed if 'continue' is not hit

}

Example:

Let's consider a scenario to demonstrate the use of the Continue statement within a for loop in C#.

Example

Example

using System;  

public class ContinueExample  

    {  

      public static void Main(string[] args)  

      {  

         for(int i=1;i<=10;i++) //for loop body

{    

	//condition to continue statement

            if(i==5){    

                continue;    

            }    

            Console.WriteLine(i);  //it prints the value of i  

        }    

      }  

   }

Output:

Output

1

2

3

4

6

7

8

9

10

Explanation:

In this instance, numbers from 1 to 10 are displayed, excluding 5. The continue statement is employed to skip the output of the number 5 and move to the subsequent iteration. This code snippet illustrates the utilization of the continue statement to circumvent specific iterations.

2. Continue Statement with while Loop in C#:

When a continue statement is employed within a while loop in C# programming, it skips the ongoing iteration and directs the control flow to the subsequent iteration of the while loop.

Syntax:

It has the following syntax:

Example

while (condition)

{

    if (expression)

    {

        continue; // continue statement

    }

    // Code to be executed if 'continue' is not hit

}

Example:

Let's consider an example to demonstrate the use of the continue statement within a while loop in the C# programming language.

Example

Example

using System;

class C# Tutorial

{

    static void Main()

    {

        int i = 0;        

        while (i < 6) // start while loop

        {

            i++;            

            if (i == 4) //continue condition

            {

                continue;  

            }           

            Console.WriteLine(i); //print the value of i

        }

    }

}

Output:

Output

1

2

3

5

6

Explanation:

In this instance, we are working with an integer variable i and setting it to an initial value of 0. Subsequently, a while loop is initiated within the code to evaluate a specific condition. In the event that the condition (1 == 4) is satisfied, the continue statement will propel the loop to the subsequent iteration.

3. Continue Statement with do-while Loop in C#:

In C# programming, when a continue statement is encountered within a do-while loop, the flow of control moves directly to the next iteration of the loop, bypassing any remaining code within the loop body.

Syntax:

It has the following syntax:

Example

do

{

    if (expression) //condition statement

    {

        continue; // continue statement

    }

    // Code to be executed if 'continue' is not hit

} while (condition);

Example:

Let's consider an example to demonstrate the continue statement within a do-while loop for identifying odd numbers in C#.

Example

Example

using System;

class C# Tutorial

{

    static void Main()

    {

        int num = 0;

        Console.WriteLine("Odd numbers from 1 to 10:");

        do

        {

            num++;            

            if (num % 2 == 0)

            {

                continue;

            }

            Console.WriteLine(num);

        } while (num < 10);

    }

}

Output:

Output

Odd numbers from 1 to 10:

1

3

5

7

9

4. Continue Statement with for-each loop in C#

In C#, the continue keyword can also be used within a foreach loop to skip the current iteration and move on to the next element in the collection.

Syntax:

It has the following syntax:

Example

foreach (var item in collection)

{

    if (expression) //condition statement

    {

        continue; //continue statement

    }

    // Code to be executed if 'continue' is not hit

}

Example:

Let's consider an example to demonstrate the use of the continue statement with a foreach loop in C#.

Example

Example

using System;

class C# Tutorial

{

    static void Main()

    {

        int[] num = { 8, -5, 4, -1, 9, -3, 20 };

        Console.WriteLine("Positive numbers in the array are:");

        foreach (int i in num)

        {

            if (i < 0)

            {

                continue; 

            }

            Console.WriteLine(i);

        }

    }

}

Output:

Output

Positive numbers in the array are:

8

4

9

20

5. Continue Statement with Inner Loop in C#

The continue statement in C# continues the execution of the inner loop only when it is used within that inner loop.

Syntax:

It has the following syntax:

Example

for (int i = 0; i < outerLim; i++)

{

    for (int j = 0; j < innerLim; j++)

    {

        if (expression)

        {

            continue;  // it skips the current iteration

        }

        //Code to be executed

    }

}

Example:

Let's consider an example to demonstrate the continue statement with a foreach loop in C#.

Example

Example

using System;  

public class ContinueExample  

    {  

      public static void Main(string[] args)  //main function

      {  

          for(int i=1;i<=3;i++)  //for outer loop

{      

                    for(int j=1;j<=3;j++) //for inner loop

{      

                        if(i==2&&j==2){      

                            continue;      

                        }      

                        Console.WriteLine(i+" "+j); //print the value of i and j

                    }      

            }      

      }  

 }

Output:

Output

1 1

1 2

1 3

2 1

2 3

3 1

3 2

3 3

Key differences between Continue and Break Statement:

Several variances between the continue and break statements in C# are outlined below:

Continue Statement Break Statement
It skips the remaining code in the current iteration and moves to the next iteration. It exits the loop entirely, regardless of whether the condition is met or not.
The loop continues to execute the next iteration. The loop is terminated immediately.
It moves control to the loop's update expression (for for loops) or condition check (for other loops). It transfers control to the statement immediately after the loop.
It is used when we want to skip specific iterations but continue looping. It is used when we want to terminate the loop early based on a condition.

Benefits of using Continue Statement:

Several benefits of using continue statement in C# are as follows:

  • It makes the code neater and faster when avoiding unnecessary operations.
  • It helps in minimizing nested if-else statements, enhancing readability.
  • It is most beneficial when used in filtering operations and data validation situations.
  • C# Continue Statement MCQ's

  1. What does the continue statement do in C# loops?
  • Skips the current iteration and continues with the next iteration.
  • Stops the program execution.
  • Restarts the loop from the beginning.
  • Terminates the loop execution.

a) Skips the current loop iteration and proceeds with the next iteration

  1. What is the result of the code snippet below?
  2. Example
    
    using System;
    
    class Program
    
    {
    
        static void Main()
    
        {
    
            int i = 1;
    
            while (i < 7)
    
            {
    
                i++;
    
                if (i == 4)
    
                    continue;
    
                Console.WriteLine(i);
    
            }
    
        }
    
    }
    
  • 1 2 3 5 6
  • 2 3 4 6 7
  • 2 3 5 6 7
  • 1 3 5 6 7
  1. Can the continue statement be utilized outside of a loop in C#?
  • Only in While Loop
  • Only in For Loop
  1. Which of the following option is correct about the continue statement in C#?
  • Continue statement can be utilized inside switch-case statements.
  • Continue statement is utilized to exit a loop completely.
  • Continue statement skips the current iteration and jumps to the next iteration.
  • Continue statement works only in for loops.
  1. What is the primary difference between break and continue statements in C#?
  • Both statements stop the loop.
  • Break statement stops the entire loop, while continue statement skips the current iteration.
  • Continue statement stops the loop, while break statement skips the iteration.
  • None of the above

(b) The break statement halts the entire loop, whereas the continue statement bypasses the current iteration.

Input Required

This code uses input(). Please provide values below: