Start learning free

LESSON 151 OF 347 · LOOPING OVER ARRAY INDICES

Continue in C# Loops: Skipping an Iteration in for and foreach

Lesson Read the full explanation

With the continue statement we can stop the current iteration of the loop, and continue with the next.

When C# hits continue, it skips (not execute) any code left, and jumps directly to the next iteration instead.

In this example, we will not let anyone inside a bar if their age is less than 21:

int[] ages = {10, 30, 21, 19, 25};

for (int age : ages) {
	if (age < 21) {
		continue;
	}
	Console.WriteLine("Someone of age " + age + " entered the bar.");
}

The output of this program is:

Someone of age 30 entered the bar.
Someone of age 21 entered the bar.
Someone of age 25 entered the bar.

As you can see, our program doesn't print for ages 10 and 19.

Here's what happens during this loop:

1. First iteration:
	a. age = 10
	b. Is age < 21? Yes:
	    continue => Go directly to the next age
	
2. Second iteration:
	a. age = 30
	b. Is age < 21? No.
	c. Console.WriteLine
	
3. Third iteration:
	a. age = 21
	b. Is age < 21? No.
	c. Console.WriteLine

4. Forth iteration:
	a. age = 19
	b. Is age < 21? Yes:
	    continue => Go directly to the next age
      
5. Fifth iteration:
	a. age = 25
	b. Is age < 21? No.
	c. Console.WriteLine

Assignment
Let's allow only the people with height at least 170 into the arena.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    C# · LOOPING OVER ARRAY INDICES

    Continue In For Loops in C#, explained

    What continue does

    The continue statement ends the current pass of a loop and jumps straight to the next one. Every statement after continue in the loop body is skipped for that pass only. The loop itself doesn't stop. In a foreach loop, the next element is taken. In a for loop, the update part runs (for example i++) and then the condition is checked.

    Use it to filter. Instead of wrapping the real work in an if, you test for the case you want to skip, call continue, and write the main work below without extra nesting.

    An example: the age check

    This program lets someone into a bar only if their age is at least 21:

    int[] ages = {10, 30, 21, 19, 25};
    
    foreach (int age in ages) {
        if (age < 21) {
            continue;
        }
        Console.WriteLine("Someone of age " + age + " entered the bar.");
    }
    

    The output is:

    Someone of age 30 entered the bar.
    Someone of age 21 entered the bar.
    Someone of age 25 entered the bar.
    

    Here is what each pass does:

    1. Age 10 is less than 21, so continue skips the print.
    2. Age 30 isn't less than 21, so the print runs.
    3. Age 21 isn't less than 21 (the comparison is strict), so the print runs.
    4. Age 19 is skipped.
    5. Age 25 is printed.

    The loop is written foreach (int age in ages). C# uses the keyword in there. A colon, as in Java's for (int age : ages), doesn't compile in C#.

    The exercise, step by step

    The lesson gives you the array heights, and the task is to print only the heights that are at least 170.

    1. Loop over the heights. Use foreach (int height in heights). Each person is considered once, in order, and no index is needed.
    2. Skip the short ones. Write an if that tests height < 170 and calls continue in its body. Those people are skipped without reaching the print.
    3. Print the rest. Below the if, call Console.WriteLine(height). Only heights of at least 170 get that far.
    int[] heights = {180, 139, 174, 190, 162};
    
    foreach (int height in heights) {
        if (height < 170) {
            continue;
        }
        Console.WriteLine(height);
    }
    

    The output is:

    180
    174
    190
    

    The condition tests the case to skip (< 170), which is the opposite of the case to keep (>= 170). A common slip is to write the keep condition together with continue. That prints exactly the wrong people.

    The same thing without continue

    You can write the exercise with a plain if and no continue:

    foreach (int height in heights) {
        if (height >= 170) {
            Console.WriteLine(height);
        }
    }
    

    Both versions print the same thing. With one short check the second one is just as clear. continue pays off when the body is long or has several filters, because each filter becomes a single line at the top and the main work stays at one level of indentation.

    Common mistakes and gotchas

    • Continue in a while loop. In a while loop, the update isn't part of the loop header, so a continue that jumps over a line like i++ can leave the counter unchanged and make the loop run forever. In a for loop the update is in the header and always runs.
    • Continue only affects the innermost loop. In nested loops, it skips to the next pass of the inner loop only.
    • Continue is not break. break leaves the loop entirely, while continue only skips the rest of one pass.
    • Statements after continue never run. Anything below an unconditional continue is dead code, and the compiler warns about it. It belongs after a check, as in the examples.
    • A continue outside a loop is a compile-time error.
    • Loop variable assignment. You can't assign to a foreach variable. If you want to skip an element, use continue and don't try to change the variable.

    Key takeaways

    • continue skips the rest of the current pass and moves on to the next element.
    • Test for the case to skip, then write the main work below it.
    • In a for loop the update step still runs after continue.
    • Use break to stop the loop and continue to skip one pass.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free