Sunday, September 27, 2015

Constant Declaration & Mathematical Operations in Swift -- IOS

In swift, use the "let" Keyword to declare constant. For constant , value cannot be changed after declaration .

let number = 100

//This is not allowed, value cannot be changed after declaration




//similarly for constant, you can explicitly declare the data type
let numbers: Int = 7




Mathematical operations -

For numbers , the usual mathematical rules apply . You can use any of the following operations : *, +, -, /, % on numbers. 

//Division operations 

// Int Divide Int = int 

var x = 1/2
result = x = 0


//Int divide decimal value = decimal value 
var y = 1/2.0
result = y = 0.5


//Operations on String

let string = "hello" + "world"
result=  "helloworld"

let str = "3" + "4"
result = "34"

   Code - Checking for even number
// If the reminder of "evenNumber" dividing my 2 is 0 , "evenNumber is an even number"

Var evenNumber = 8
print (evenNumber % 2)

result =  0






No comments:

Post a Comment