Exploring constants and variables
Now that you know about the simple data types that Swift supports, let's look at how to store them, so you can perform operations on them later.
You can use constants or variables to store data. Both are containers that have a name, but a constant's value can only be set once and cannot be changed once it's set, and a variable's value can be changed at any time.
You must declare constants and variables before you use them. Constants are declared with the let
keyword and variables with the var
keyword.
Let's explore how constants and variables work by implementing them in your playground. Work through the following steps:
- Add the following code to your playground to declare three constants, and click the Play/Stop button to run it:
let theAnswerToTheUltimateQuestion = 42 let pi = 3.14159 let myName = "Ahmad Sahar"
In each case, a container is created and named, and the assigned value is stored.
Tip
You...