c sharp

C# Continue Statement

C# is a programming language that restricts its programmers to follow the proper format strictly. In C# language, the compilation is done line by line. But sometimes when we are using a loop and we need to stop the loop or we want it to go back to the loop and skip a value, we cannot do that. But there are a few methods that help us to do so. The break method breaks the loop, it is utilized where a condition turns true and we need to terminate the loop. The ‘continue’ statement is another way but in ‘continue’, it does not stop the loop. It just goes back to the loop and executes the next iteration. Iteration is the number of times, the loop repeats itself until the given condition is encountered. In this guide, we are going to deliberate the ‘continue’ statement. The ‘continue’ statement cannot terminate the loop, it just skips iteration for the required number of times. The ‘continue’ statement is simple to utilize, just write the term continue; after the condition. When the compiler reaches this ‘continue’, it goes back to the loop and starts the new iteration by incrementing the iterator. This statement can be used when we have more than one iteration.

Example no. 1:

We will observe the working of the ‘continue’ command in the ‘for’ loop.

using System;

class Program_0
{
    static void Main() {
       for(int i=0; i<9; i++)
       {
           if(i == 5 )
           {
               continue;
           }
          Console.WriteLine("The index "+i+" is here");
       }
          Console.WriteLine("\n5 is missing");
    }
}

In this instance, we utilize a ‘for’ loop to iterate as many times as we want. So, we declare and initialize an iterator that will repeat the ‘for’ loop. And give a condition that the loop will repeat itself till ‘i’ reaches 8 because the condition says that ‘i’ must be less than 9. Inside the ‘for’ loop defines a condition by utilizing the ‘if’ conditional statement, where we set i == 5. The body of the ‘if’ statement utilizes the ‘continue’ statement. Outside the ‘if’ statement, employ Console.WriteLine() method to display a text with the value of ‘i’.

Now, the code will work, the loop starts and the value of ‘i’ is 0. As the loop condition is true, then it goes inside the body of the ‘for’ loop, there is an ‘if’ condition, and it checks whether the specified condition is true or not. Here the condition of the ‘if’ statement is false because the value in ‘i’ is 0 so it ignores the ‘if’ statement and executes the next statement which is the Console.WriteLine(). Hence a message and the value of ‘i’ are printed on the console. Now, the value of ‘i’ is incremented and the process will repeat. When the value of ‘i’ becomes 5, then the ‘if’ statement becomes true and its body will be implemented. The body of ‘if’ contains only one statement which is the ‘continue’ command. The control will go back to the ‘for’ loop and will repeat the process till the condition of ‘for’ turns false. When the condition becomes false, then the control will move out of the body and execute the next statement (a message for the console) and the program ends here.

Example no. 2:

Here the ‘continue’ command is utilized in the ‘while’ loop.

using System;

class Program_1
{
    static void Main() {
        int i= 5;
        while(i10 && i<13)
        {
        Console.WriteLine("The Number "+i+" is missing ");  
           continue;
           
        }
         Console.WriteLine("The Number "+i+" is encountered" );  
    }
  }
}

Define an integer variable with value 5. Then, apply a ‘while’ loop and set the condition of ‘while’. In the body of ‘while’, increment the ‘i’ by 2 and then utilize the ‘if’ loop. Here, we define the condition of ‘if’ and inside its body, we have called a function Console.WriteLine() and ‘continue’ statement. Outside the ‘if’ loop, we again use the Console.WriteLine() method to represent the message and values that are executed. The program will work as it will first check the condition of ‘while’. If it is true, then it will move inside the body of ‘while’ and increment the ‘i’. Then, it checks the condition of the ‘if’ loop. And whenever its condition is false it will move next and display the required text on the console. But when the condition of ‘if’ is true, it will execute the body of the ‘if’ loop, so it first shows a message with the value of ‘i’. After this, it will execute the ‘continue’ statement and it would take the control back to the ‘while’ loop and this process will repeat itself until the given condition is met.

The outcome shows all the numbers with the same message but one statement is different because of the ‘if’ condition. The ‘if’ condition becomes true on that value so it turns the control back to the while loop.

Example no. 3:

This example code will clarify the employment of the ‘continue’ command in the ‘do-while’ loop.

using System;

class Program_2
{
    static void Main() {
        int i= 5;
        do
        {
            i+=5;
        if(i == 10 || i == 20)
        {
           continue;
        }
          Console.WriteLine("The Number "+i+" is encountered" );
    }while(i<50);
  }
}

The code starts by declaring an integer value and a ‘do-while’ loop. In the body of the ‘do-while’ loop, we incremented the iterator by 5 and specify an ‘if’ condition. Inside the ‘if’ condition, we utilize a ‘continue’ statement. It will send the control to the beginning of the loop and increment the iterator. When the condition of ‘if’ turns true, it will skip the defined values and will move on to the next iteration, this is what ‘continue’ does. And when the condition is false, the ‘while’ body will be implemented. The output screen will show the values from 5 to 50 and it includes all the multiples of 5 except 10 and 20 because for these two values the ‘if’ condition becomes true and skips these values.

Example no. 4:

This program will demonstrate the usage of the ‘continue’ statement and ‘foreach’ loop.

using System;

class Program_3
{
    static void Main() {
        char[] array_0 ={'a', 'b', 'c', 'd', 'e'};
       foreach(char ch in array_0 )
       {
           if(ch == 'b')
           continue;
           Console.WriteLine("The character stored is "+ch );
       }
         
  }
}

Let us look at the last code. First, define an array of characters named ‘array_0’ and store five characters in it. Then, utilize the ‘foreach’ loop and set a variable with a char type that will be used to get the values of the char array. In the body of the ‘foreach’ loop, we specify an ‘if’ condition (ch == ‘b’) and then employ the ‘continue’ statement. It skips character ‘b’ and moves on to the next character. When we write one statement in the body of ‘if’ we don’t need to enclose that in curly braces. When the defined condition of ‘if’ is false run the statement next to it. The next statement will represent the characters with a message on the console by utilizing the Console.WriteLine() method.

Conclusion

We have covered the ‘continue’ statement that we apply within loops to skip a few values until a certain condition is completed. With numerous practical examples, the use of the ‘continue’ statement in the loops (for, while, do-while, and foreach) is thoroughly explained. This command is used in the loop to send the control back to the beginning of the loop. The ‘continue’ statement is a useful method where we limit the execution to a few iterations.

About the author

Ann-ul Hayyat

I'm a professional SEO and technical content writer with extensive experience in these fields as well as writing content for blogs, websites, reports, and other topics related to education, science, sports, and many other areas. I am skilled and experienced in creating SEO-friendly blog posts, technical articles, and other content on themes that you may find interesting.