發新話題
打印

[SWIFT] ** Basic Swift Show Case

** Basic Swift Show Case

Demonstrate for Basic swift showcase

Control Flow

[ 本帖最後由 Net 於 2018-7-19 20:54 編輯 ]

TOP

For-In Loops

For-in Loops
引用:
let baskets = ["Apple", "Orange", "Banana", "Pear"]
for basket in baskets{
print("Hello, \(basket)")
}

/*
Hello, Apple
Hello, Orange
Hello, Banana
Hello, Pear
*/
引用:
// Iterate over a dictionary to access its key-value pairs. (key, vaule) tuple

let numberOfEyes = ["spider": 6, "Cat": 2, "Monster": 1]
for (creatureName, eyeCount) in numberOfEyes {

print("\(creatureName)s have \(eyeCount) Eyes")
}

/*spiders have 6 Eyes
Cats have 2 Eyes
Monsters have 1 Eyes
*/
引用:
for index in 1...5 {

print("\(index) times 5 is \(index * 5)")
}


//1 times 5 is 5
//2 times 5 is 10
//3 times 5 is 15
//4 times 5 is 20
//5 times 5 is 25
引用:
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
        answer *= base
}

print ("\(base) to the power of \(power) is \(answer)")
// Prints "3 to the power of 10 is 59049"
/////////////////////////

[ 本帖最後由 Net 於 2018-7-19 22:09 編輯 ]

TOP

發新話題