Small Steps I — Creating with Text and Numbers
Calculating with a remembered value
Create lives with the value 3, reduce it by 1, and print it.
What you will learn
You can use stored values in calculations. Store the result again to increase a score.
Try it
Example · example.cpp
void small_main()
{
int score = 10;
score = score + 5;
print(score);
}
score = score + 5; first reads the current value 10, adds 5, then stores the result 15 in score. It does not mean that both sides are equal, as a mathematical equation would.
Running only print(score + 5); displays the calculated result but does not change score itself.
Expected output:
15
Exercise
Create lives with the value 3, reduce it by 1, and print it.
Exercise starter · exercise_starter.cpp
void small_main()
{
int score = 10;
score = score + 5;
print(score);
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
int lives = 3;
lives = lives - 1;
print(lives);
}