Learn
Suppose we are building a game, and we want to keep track of the player’s name, the player’s score that goes from 0 to 10, the player’s level, etc. We need some variables!
Before we can use a variable, we must declare, or create, it.
So to declare a variable called score
and set it equal to 0
, we need to write:
var score = 0
var
is a keyword used to declare variables.score
is the name of the variable.=
is the assignment operator.0
is the value of the variable.
In Swift, a single equal sign =
means to “assign” values (not compare). In the code above, we are assigning the score
variable a value of 0
.
So now, we can print out the value of the variable score
.
print(score)
The output would be:
0
Note: Variable names should be in camelCase format.
Instructions
1.
Declare a variable named year
with a value of the current year.
2.
Output year
using print()
.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.