Menu
Coddy logo textTech

Continue

Part of the Fundamentals section of Coddy's Rust journey — lesson 40 of 75.

The continue statement stops the current iteration and continues to the next iteration. For example:

for i in 3..9 {
    if i == 5 {
    	continue;
    }
    println!("{}", i);
}

The loop will iterate through all of the numbers. when it reaches i=5 it will skip that iteration and continue to the next one. The output is:

3
4
6
7
8

Notice, number 5 is not in the output.

challenge icon

Challenge

Beginner

You are given a code which prints the numbers from 1 to 20 (including).

Your task is to add if and continue statements so that only the even numbers will be printed (2, 4, 6, ...). 

Cheat sheet

The continue statement stops the current iteration and continues to the next iteration:

for i in 3..9 {
    if i == 5 {
        continue;
    }
    println!("{}", i);
}

This will print all numbers except 5:

3
4
6
7
8

Try it yourself

fn main() {
    for i in 1..=20 {
        println!("{}", i);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals