2019年5月29日 星期三

control Flow

判斷
if...
else if ...
else...

switch

guard ... else

迴圈
(明確知道要執行的次數)
for in

(不知道要執行的次數)
while

(不知道要執行的次數)
repead
while

let names = ["Anna", "Alex", "Brian", "Jack"];
for name in names {
    print("Hello!\(name)");
}
let numberOfLegs = ["spider":8, "ant":6, "cat":4];
for (animalName, legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs");
}

for index in 1..<5{
    print(index);
}
let base = 3;
let power = 10;
var answer = 1;

for _ in 1...power {
    answer *= base;
}

let minutes = 60
let minuteInterval = 5
//從0開始,每次+5,到60(不包含60)
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
    print(tickMark);
}

//從0開始,每次+5,到60(包含60)
for tickMark in stride(from: 0, through: minutes, by: minuteInterval) {
    print(tickMark);
}

while  判斷 {
 
}
//相當於C的for(int i=25,i>=0;i--)
var finalSquare = 25
while finalSquare >=0 {
    print(finalSquare);
    finalSquare -= 1;
}

finalSquare = 25;
repeat {
    finalSquare -= 1;
} while finalSquare >=0

單向式
if 判斷式 {

}

雙向式 (一定執行其中一個)
if 判斷式 {

} else {

}

多向式(一定執行其中一個)
if 判斷式 {

} else if 判斷式{

} else if 判斷式{

} else {

}

var temperatureInFahrenheit = 30;
if temperatureInFahrenheit <= 32 {
    print("非常冷");
}
temperatureInFahrenheit = 35;
if temperatureInFahrenheit <= 32 {
    print("非常冷");
} else {
    print("不會冷");
}
 temperatureInFahrenheit = 90;
if temperatureInFahrenheit <= 32 {
    print("非常冷");
} else if temperatureInFahrenheit >= 86{
    print("非常熱");
} else {
    print("舒適!")
}
//siwft的switch只會執行其中一個區段,執行完跳出switch!
let someCharacter:Character = "z";
switch someCharacter {
    case "a":
        print("a");
    case "z":
        print("z");
    default:
        print("b~y");
}

let anotherCharacter:Character = "a";
switch anotherCharacter {
    case "a","A":
        print("a,A");
    case "b","B":
        print("b,B");
    default:
        print("其他");
}

//interval Matching
let approximateCount = 62
switch approximateCount {
    case 0:
        print("0");
    case 1..<5:
        print("1~4");
    case 5..<12:
        print("5..11");
    case 12..<100:
        print("12..99");
    case 100..<1000;
        print("100..999");
    default:
        print(">1000");
}
//範圍多值比對
let somePoint = (x:1,y:1);
switch    somePoint {
    case (0,0):
        print("0,0");
    case (_,0):
        print("y=0");
    case (0,_):
        print("x=0");
    case (-2...2,-2...2):
        print("-2...2,-2...2");
    default:
        print("其他");
}
//switch value binding
let anotherPoint = (2,0);
switch anotherPoint {
    case (let x, 0):
        print("y是0,x是\(x)");
    case (0, let y):
        print("x是0,y是\(y)");
    case let (x, y):
        print("x是\(x),y是\(y)");
    //因為一定會執行上方三個區塊其中一個,所以default可以省略!
    /*
    default:
        break;
    */
}
//switch value binding + where
let yetAnotherPoint = (1, -1);
switch yetAnotherPoint {
    //where 後面一定接bool
    case let (x, y) where x == y:
        print("x==y");
    case let (x, y) where x == -y:
        print("x==-y");
    case let (x, y):
        print("x=\(x), y=\(y)");
}

提早離開function
guard ... else,只執行false區段,true時不動作!
guard (false) else {
    false 區段
}
//guard else
func greet(person:[String:String]) {
    guard let name = person["name"] else {
        print("key沒有name");
        return;
    }
    print("Hello!\(name)!");

    gurade let location = person["location"] else {
        print("key沒有location");
        return;
    }
    print("name=\(name),location=\(location)");
}
greet(person: ["name":"John", "location":"Cupertino"]);