Variables

Variables & constants in Swift is a basic and fundamental concept. This tutorial is focusing on explaining let(immutable) & var(mutable) declaration. Both variable and constant two terms are self-explanatory, Constant refers to the values which never gets changed during runtime. Whereas Variable refers to the values, which can be changed during runtime.

Declaring a Constant:

let is the keyword, used to declare the constants.

let myName = “ABCD” and let age = 20

var is the keyword , used to declare the variables.

var salary= 2000;

You can change the value of this salary variable since it is declared with var keyword. whereas you can’t change the age since it is declared with let keyword.

for ex:-

salary = 3000 // works fine

age = 22 // compile error

Note: Though it is not mandatory to terminate the line with a semicolon in swift. You can use semicolon to write multiple lines in single line . ex let a = 90; var b = 100 

We will see variables & constants in detail in data type & Tuples

 

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *