Small Steps I — Creating with Text and Numbers

Stopping or skipping an iteration

Skip 3 instead of 2, and stop at 5.

What you will learn

break ends the innermost loop. continue skips the rest of the current iteration and moves to the next one.

Try it

Example · example.cpp
void small_main()
{
    for (int i = 1; i <= 5; i = i + 1)
    {
        if (i == 2)
        {
            continue;
        }
        if (i == 4)
        {
            break;
        }
        print(i);
    }
}

At 2, print is skipped. for increases i and checks the next condition. At 4, the entire loop ends, so neither 4 nor 5 is printed.

Expected output:

1
3

Exercise

Skip 3 instead of 2, and stop at 5.

Exercise starter · exercise_starter.cpp
void small_main()
{
    for (int i = 1; i <= 5; i = i + 1)
    {
        if (i == 2)
        {
            continue;
        }
        if (i == 4)
        {
            break;
        }
        print(i);
    }
}

Hint

Change the parts of the example that you need.

show solution
void small_main()
{
    for (int i = 1; i <= 5; i = i + 1)
    {
        if (i == 3)
        {
            continue;
        }
        if (i == 5)
        {
            break;
        }
        print(i);
    }
}
← Previous lessonNext lesson →