swift if statement code example

Example 1: if statement swfit

// if expression {
// 	do something
// }

var a: Int = 3
if a == 3 {
	print("Hello World")
} else if a < 3 {
	print("dlroW olleH")
} else {
	print("42")
}

Example 2: swift if statement

// check the number is positive or negative
let num = 15
var result = ""

if (num > 0) {
     result = "Positive Number"
}
else {
     result = "Negative Number"
}

print(result)

Example 3: swift 5 if statement

let a = -5

// if the condition is true then doThis() gets called else doThat() gets called
a >= 0 ? doThis(): doThat()

func doThis() {
    println("Do This")
}

func doThat() {
    println("Do That")
}