Swift variable and constant

In Swift, Constants and variables must be defined prior to using them.
 

Variable

A variable’s value can be updated to a new value.

To define a variable, use the “var” keyword.

For example,

var num1 = 10

num1 = 20 // no error

 

Constant

Constant values can be assigned only once.

To define a constant, use the “let” keyword.

let num2 = 10

num2 = 30 // compilation error

 

Here is a screenshot from Swift Playground for the same :

swift variable and constant
 
We can declare multiple constants or variables in a single line by separating them with a comma.

var num1 = 10, num2 = 20

let freezingTempCelcius = 0, freezingTempFarenheit = 32

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags