Control flow / Conditional statements

class Stack<T> {
    var items: [T?] = [T?]()

    func push(_ item: T) {
        items.append(item)
    }

    func pop() -> T? {
        guard !items.isEmpty else {
            return nil
        }
        return items.removeLast()
    }
}

let stack = Stack<Int>()
stack.push(1)
let n1 = stack.pop()
//n1 is Optional(1)
let n2 = stack.pop()
//n2 is nil

print("n1 is \(n1!)")
print("n2 is", n2 as Any)


guard statement, like an if statement, executes statements depending on the Boolean value of an expression. You use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed. Unlike an if statement, a guard statement always has an else clause — the code inside the else clause is executed if the condition isn’t true.