Swift Variables | Basic Introduction to Swift Variables

Here, we have discussed a tutorial related to Swift Programing language. In this article you have learnt about Swift Variables, how to Declare...

Welcome to the another most interactive article on Swift Programing language.

Swift Variables | Basic Introduction to Swift Variables

In this article you will learn about Swift Variables, how to Declare Variables in Swift, Assign and change value of a variable. So without wasting any time let's get started!!

Swift Variables

Swift Variables are like container (storage area) which holds data that can be changed. These variables are used to store data in memory so that we can used them to manipulate our program. In other words, we can say that the name given to memory location in Swift is known as Variables. Each variable has unique naming which are also known as Identifier. 

How to declare Variables in Swift

Variable declaration is an important part of the program that tells the compiler where and how much to create the storage for the variable. In Swift programming language,  keyword is used to declare a Variable. 

Syntex:

var variableName = <initial value>

Let's understand the declaring of variables in swift with the help of following example;

var SiteName = "Answersjet.Com"

print (SiteName)

Output

Answersjet.Com

How to Assign value in Swift variable

In Swift program, Assignement operator is used to assign values. Let's understand Assigning of value in Swift variable with the help of following example;

var course = "Swift Programming Language"

print (course)

Output

Swift Programming Language

How to change value of a variable

Swift programming allows programmers to change the value of existing variable. This can be done only if you use Assignement operator but without adding var keyword.

Let's understand change value of a variable with the help of following example;

var siteName = "Answersjet.com"   

// Assigning a new value to siteName

siteName = "Swift.org"  

print(siteName)  

Output

Swift.org

Print Current value of Variables

In the swift programming language, in order to print the current value of the variable, programmer uses the print function. For that, before opening the parenthesis, you have to wrap the value of the variable with the backlash.

Let's understand this with the help of following example;

var varX = "Swift"  

var varY = 3000

print("The course fees of \(varX) is \(varY) Per/Month.") 

Output

The course fee of Swift is 3000 Per/Month.

Conclusion

Above we have discussed a tutorial related to Swift Programing language. In this article you have learnt about Swift Variables, how to Declare Variables in Swift, Assign and change value of a variable. Variables are like container (storage area) which holds data that can be changed. Each variable has unique naming which are also known as Identifier. I hope this article will be helpful to you all.