Small Steps I — Creating with Text and Numbers
Variables and Constants
Variables and Constants
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.
Giving a value a name
What you will learn
A variable is a named place to store a value. Let us remember a score under the name score.
Try it
void small_main()
{
int score = 10;
print(score);
}
int score = 10; creates a variable named score and gives it the initial value 10. int is an integer type. A type describes the kind of value stored; integers such as 0, 10, and -3 have no fractional part.
Creating a variable is called declaration; giving it its first value is initialization. For now, always give a variable a value when you create it. print(score); prints the stored value, rather than the letters score.
Expected output:
10
Changing a stored value
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
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
Calculating with a remembered value
What you will learn
You can use stored values in calculations. Store the result again to increase a score.
Try it
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
Storing values with fractional parts
What you will learn
Lengths and times may need fractional parts. double is a type for these numbers. Choose int for whole-number counts and double for values that need a fractional part.
Try it
void small_main()
{
double height = 1.45;
print(height);
}
A variable's type is chosen when it is created. Assigning a fractional number to an int later does not turn it into a double variable.
double cannot store every number with unlimited precision, but for now we will start with simple lengths and times.
Expected output:
1.45
Naming a value that will not change
What you will learn
const means the initial value will not be changed later. Use it to name a fixed value such as a game's target score.
Try it
void small_main()
{
const int goal_score = 100;
int score = 80;
print(goal_score - score);
}
Assigning a different value to goal_score later causes a compilation error.
In this course, start names with an English letter and use letters, digits, and underscores. Do not include spaces. score and Score are different names. C++ keywords such as int and if cannot be names.
Expected output:
20
Shorter ways to update a value
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
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
Store 10 in an int variable named age and print it.
void small_main()
{
int score = 10;
print(score);
}
Hint
Use the name age instead of score.
show solution
void small_main()
{
int age = 10;
print(age);
}
Try More (Optional)
Exercise
Set the score to 3, then change it to 8, printing it each time.
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);
}
Exercise
Create lives with the value 3, reduce it by 1, and print it.
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);
}
Exercise
Store a running time of 12.5 in a variable named seconds and print it.
void small_main()
{
double height = 1.45;
print(height);
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
double seconds = 12.5;
print(seconds);
}
Exercise
Set a goal of 200 and print how much more is needed from a current score of 80.
void small_main()
{
const int goal_score = 100;
int score = 80;
print(goal_score - score);
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
const int goal_score = 200;
int score = 80;
print(goal_score - score);
}
Exercise
Start the score at 10, double it, then increase it by 1 and print it.
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);
}