Small Steps I — Creating with Text and Numbers
Shorter ways to update a value
Start the score at 10, double it, then increase it by 1 and print it.
What you will learn
You can shorten commonly used calculations and assignments. For int or double, score += 5; makes the same update as score = score + 5;.
Try it
Example · example.cpp
void small_main()
{
int score = 10;
score += 5;
score--;
print(score);
}
++ increases by 1; -- decreases by 1. At first, use them as separate statements as in the example. -=, *=, and /= work similarly. Integers also support %=. Integer /= still performs integer division.
Expected output:
14
Exercise
Start the score at 10, double it, then increase it by 1 and print it.
Exercise starter · exercise_starter.cpp
void small_main()
{
int score = 10;
score += 5;
score--;
print(score);
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
int score = 10;
score *= 2;
score++;
print(score);
}