"Every variable in C++ must be declared before it can be used!"
Suppose we are building a game and we want to keep track of a player’s score that goes from 0 to 10. We need a variable!
Before we can use a variable, we must declare, or create, it. To declare a variable, we need to provide two things:
- A type for the variable.
- A name for the variable.
So to declare an integer variable called
score, we need to write:
intis the type of the variable.
scoreis the name of the variable.
;is how we end a statement.
In C++, variable names consist only of upper/lower case letters, digits, and/or underscores.
Note: C++ is known as a strongly typed language. If you try to give an integer value a decimal number, you are going to get unexpected results, warnings, or errors.
Inside the variable.cpp file, declare an
int variable named
Type the following commands in the terminal and press enter:
This checkpoint will pass after you execute.