Small Steps I — Creating with Text and Numbers
Repeat Instructions
Repeat Instructions
Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.
Repeating the same instructions
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
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
Repeating while a condition holds
What you will learn
while repeats the code inside braces while its condition is true. It checks the condition before every iteration.
Try it
void small_main()
{
int count = 3;
while (count > 0)
{
print(count);
count = count - 1;
}
}
The loop ends when count becomes 0. If it starts at 0, nothing is printed. Removing the line that decreases count makes the loop run forever; use the IDE's Stop button in that case.
for is convenient when counting iterations; while is convenient when waiting for a condition to change.
Expected output:
3
2
1
Stopping or skipping an iteration
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
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
Drawing several rows of stars
What you will learn
You can put a loop inside another loop. The outer loop counts rows; the inner loop counts the stars in each row.
Try it
void small_main()
{
for (int row = 0; row < 2; row = row + 1)
{
for (int col = 0; col < 3; col = col + 1)
{
write("*");
}
print();
}
}
write does not move to a new line after output. print() moves to a new line without printing content. The program therefore prints three adjacent stars and then a newline, twice.
Expected output:
***
***
Exercise
Change the program to print 1 through 5.
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);
}
}
Try More (Optional)
Exercise
Print backwards from 5 to 1.
void small_main()
{
int count = 3;
while (count > 0)
{
print(count);
count = count - 1;
}
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
int count = 5;
while (count > 0)
{
print(count);
count = count - 1;
}
}
Exercise
Skip 3 instead of 2, and stop at 5.
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);
}
}
Exercise
Draw three rows with four stars each.
void small_main()
{
for (int row = 0; row < 2; row = row + 1)
{
for (int col = 0; col < 3; col = col + 1)
{
write("*");
}
print();
}
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
for (int row = 0; row < 3; row = row + 1)
{
for (int col = 0; col < 4; col = col + 1)
{
write("*");
}
print();
}
}