Code

continue Statement

Another commonly used jump statement is the continue keyword. When we use continue, the rest of the current iteration is skipped after the keyword and immediately initiates the execution of the next iteration. In other words, passes control straight back up to the conditional expression at the top of the loop to start over.

For loop :

for (int counter = 1; counter <= 4; counter++)
{
    if (counter == 3) // <- A
        continue;

    Console.WriteLine(counter);
}

Run Demo

OUTPUT: 3 skipped

1
2
4

foreach loop :

int[] integerValues = { 1, 2, 3, 4 };

foreach (int value in integerValues)
{
    if (value == 3) // <- A
        continue;

    System.Console.Write(value);
}

Run Demo

OUTPUT: 3 skipped

1
2
4

while loop :

int counter = 0;
while (counter < 4)
{

    counter++;
    if (counter == 3) // <- A
        continue;

    Console.WriteLine(counter);
}

Run Demo

OUTPUT: 3 skipped

1
2
4

do-while loop :

int counter = 0;

do
{

     counter++;
     if (counter == 3) // <- A
        continue;

     Console.WriteLine(counter);

} while (counter < 4);

Run Demo

OUTPUT: 3 skipped

1
2
4

Add Comment

Click here to post a comment

Subscribe

Most Popular

Popular Video