Start learning free

LESSON 152 OF 338 · LOOPING OVER ARRAY INDICES

Continue in For Loops in Go: Skipping an Iteration

Lesson Read the full explanation

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

When Go 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:

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

for _, age := range ages {
	if age < 21 {
		continue
	}
	fmt.Printf("Someone of age %d entered the bar.\n", age)
}

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. fmt.Printf

3. Third iteration:
	a. age = 21
	b. Is age < 21? No.
	c. fmt.Printf

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. fmt.Printf

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

    GO · LOOPING OVER ARRAY INDICES

    Continue In For Loops in Go, explained

    What continue does

    The continue statement ends the current pass through a loop and jumps to the next one. Whatever is left in the loop body is skipped for this element, but the loop itself keeps going. This is different from break, which ends the whole loop.

    It is most useful as a filter. Instead of wrapping the real work in an if, you test for the elements you don't want, skip them with continue, and let the rest of the body deal with the elements that remain.

    An example: ages at the door

    ages := []int{10, 30, 21, 19, 25}
    
    for _, age := range ages {
        if age < 21 {
            continue
        }
        fmt.Printf("Someone of age %d entered the bar.\n", age)
    }
    

    The output is:

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

    Tracing the loop:

    • For 10, the condition age < 21 is true, continue runs, and the print is skipped.
    • For 30, the condition is false, so the body continues to the print.
    • For 21, the condition is false, because 21 is not less than 21. The person gets in.
    • For 19, the condition is true and the print is skipped again.
    • For 25, the condition is false and the print runs.

    Notice that no else is needed. After continue the rest of the body never runs, so the code below the if only sees the elements that passed the test.

    Go details

    • The condition has no parentheses. Go writes if age < 21 {, and the braces are required even for one statement.
    • The blank identifier. range yields an index and a value. A variable you declare but never use is a compile error, so the index you don't need is written as _.
    • In a three-part loop, the post statement still runs. With for i := 0; i < 5; i++, a continue jumps to i++ and then to the condition check, so the loop doesn't get stuck. Printing only the odd numbers works like this:
    for i := 0; i < 5; i++ {
        if i%2 == 0 {
            continue
        }
        fmt.Println(i)
    }
    
    1
    3
    
    • Only the innermost loop is affected. In nested loops, a plain continue moves to the next iteration of the inner loop. To move on in the outer loop, label it and write continue outer. With the label on an outer loop over i and an inner loop over j, a continue outer at j == 1 prints only the pairs where j is 0: (0,0), (1,0) and (2,0).
    • A while-style loop can hang. Go writes a condition-only loop as for cond { ... }. If the statement that changes the condition comes after the continue, it never runs for skipped elements, and the loop may repeat forever. Update the counter before the continue in such loops.

    The exercise: heights for the arena

    The slice heights holds the heights of the people in line. Only those who are at least 170 get into the arena, and the program prints the heights that pass.

    1. Loop over the heights. for _, height := range heights visits each one and discards the index.
    2. Skip the short ones. At least 170 means the ones to reject are those with height < 170, so the if uses < and runs continue. A height of exactly 170 would not be skipped.
    3. Print the rest. After the if, fmt.Println(height) runs only for heights that were not skipped.
    heights := []int{180, 139, 174, 190, 162}
    
    for _, height := range heights {
        if height < 170 {
            continue
        }
        fmt.Println(height)
    }
    

    This prints:

    180
    174
    190
    

    The values 139 and 162 are skipped.

    Common mistakes

    • Using <= instead of <. That would reject people of exactly 170, who should be admitted.
    • Putting the print before the check. Then every height is printed, and the continue has nothing left to skip.
    • Using break instead of continue. The loop would stop at 139, so 174 and 190 would never be printed.

    Key takeaways

    • continue skips the rest of the current iteration; break ends the loop.
    • Test for what you want to skip, and keep the main work after the if, with no else.
    • In a three-part for, continue still runs the post statement.
    • A label lets continue target an outer loop.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free