Chapter 9 : Swift Constants

 



Constants is the fixed value of a program that may not be changed during the program execution, means we cannot change or modified its value after its definition.

Constant can be a Enumeration/character / String / Float / Integer type . 

Syntax: 

we have to use let keyword for it

let Name_of_constant =  initialize_value

For Example :

let constantA = 95

print(constantA)

Output 

95



Type Annotations

We have to give type annotation at the time of declaration of a constant

Syntax 

var Name_of_constant : data_Type_here = Optional_initial_value_here

For Example

let constant2:Float = 1.44

print(constant2)

Output

1.44



Constant Naming

It can be composed of digits , letter and underscore and upper or lowercase letters.

Rule : Must begin with letter or underscore. Or Never be start with digits.

let _constant1 = "9to5ios” //valid

let con_9to5 = "sample”   //valid

let 9to5ios = "sample”     //invalid 

print("Value of _constant1= \(_constant1) con_9to5 = \(con_9to5)”)

Output

Value of _constant1= 9to5ios con_9to5 = sample



Add a Comment

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