Small Steps I — Creating with Text and Numbers

Repeating the same instructions

Change the program to print 1 through 5.

What you will learn

A loop runs a block of code several times. for specifies the initial setup, the condition for continuing, and the update to the next value.

Try it

Example · example.cpp
void small_main()
{
    for (int i = 1; i <= 3; i = i + 1)
    {
        print(i);
    }
}

i starts at 1. If the condition is true, the program prints, increases i by 1, and checks again. After printing 3, i becomes 4 and the false condition ends the loop. i is a variable name we chose.

Expected output:

1
2
3

Exercise

Change the program to print 1 through 5.

Exercise starter · exercise_starter.cpp
void small_main()
{
    for (int i = 1; i <= 3; i = i + 1)
    {
        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)
    {
        print(i);
    }
}
← Previous lessonNext lesson →