Small Steps I — Creating with Text and Numbers
Changing a stored value
Set the score to 3, then change it to 8, printing it each time.
What you will learn
You can keep a variable's name and change the value inside it. Storing a new value is called assignment.
Try it
Example · example.cpp
void small_main()
{
int score = 10;
print(score);
score = 20;
print(score);
}
Running score = 20; replaces the previous value 10 with 20. The variable already exists, so do not write int again. = puts the value on the right into the variable on the left.
Expected output:
10
20
Exercise
Set the score to 3, then change it to 8, printing it each time.
Exercise starter · exercise_starter.cpp
void small_main()
{
int score = 10;
print(score);
score = 20;
print(score);
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
int score = 3;
print(score);
score = 8;
print(score);
}