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 after it has been set, whereas 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 whereas variables are declared with the var
keyword.
Let's explore how constants and variables work by implementing them in your playground. Follow these steps:
- Add the following code to your playground to declare three constants:
let theAnswerToTheUltimateQuestion = 42 let pi = 3.14159 let myName = "Ahmad Sahar"
- Click the Play/Stop button to run it.
In each case, a container is created and named, and the assigned value...