Tuesday, 28 April 2015

Defining Constants And Variables In Swift

Syntax

 var <variablename>
 let  <constantname>

or

var <variablename>: datatype
let <constantname>: datatype

Examples

var variable
let constant

or
var stringVariable: String
let  constantString: String

Assigning values

variable=2
constant="Hello World"  // value can not change.


stringVariable="Hello World"
constantString="Hello World"

Explanation
   
   The above examples show the var keyword is used declare variables and the let keyword is used to declare constant.we can declare variable or constant with data type or without data type. When we declare constant or variable without data type, the appropriate type of the variable or constant is defined when value is assigned to it.
Note
   1. The above examples show (;) not needed in end of the statement
   2. The value can be assigned at declaration itself.
   3. If once the value for constant assigned, can not be changed.


 

No comments:

Post a Comment