Small Steps I — Creating with Text and Numbers
Drawing several rows of stars
Draw three rows with four stars each.
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
Example · example.cpp
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
Draw three rows with four stars each.
Exercise starter · exercise_starter.cpp
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();
}
}