Swift if statement
A simple example is shown below. As the name implies the block of statement for if executes if the condition is satisfied.
let a = 20 if a == 20 { println("\(a)") }
Output
20
if else example
A simple if else example is shown below.
let a = 10 if a == 20 { println("\(a)") } else{ println("Not 20") }
Output
Not 20
if else ladder example
let a = 3 if a == 1 { println("Number is 1") } else if a == 2 { println("Number is 2") } else if a == 3 { println("Number is 3") } else{ println("UnKnown number") }
Output
Number is 3
Optionals and if statement
var name: String? if let tempName = name { println("value present") } else { println("NIL") }
Output
NIL
You can see that optionals can be checked with if let syntax. Also, it copies the variable locally and helps us not making changes to the original value.