Swift3 從入門到精通Day4:For、While、Switch
時間:5/10(三)
內容:
- 30. Switch 判斷式7:15
- 31. For 迴圈:配合陣列使用 For 迴圈7:41
- 32. For 迴圈:問題解答4:23
- 33. For 迴圈:配合範圍使用 For 迴圈7:34
- 34. For 迴圈:更多 For 迴圈介紹與 Tuple10:44
- 35. While 迴圈
— — Switch — —
let name = “Brenda”
switch name{
case “Thomas”:
print(“That’s me”)
case”David”:
print(“That’s my fathar”)
case”Helen”:
print(“That’s my mother”)
case”Brenda”:
print(“That’s my sister”)
default:
print(“Who are you?”)
}
let price = 200
switch price{
case 200,400:
print(“too expensive”)
case 100,180:
print(“OK”)
case 50:
print(“cheap”)
default:
print(“price has to be 200 or 100 or 50”)
}
— — for— —
var animalArray = [“cat”,”dog”,”lion”,”tiger”]
animalArray[1]
for animal in animalArray{
print(animal)
}
let numberArray = (1…10)
for number in numberArray{
print(number)
}
let numberAarry = (1…10)
var anthorNumberArray:[Int] = [5]
for number in numberArray{
anthorNumberArray.append(number + 2)
}
anthorNumberArray
//print all the name out
let nameArray = [“David”,”Sophie”,”Thomas”,”Sunny”,”Ryan”,”Hello”]
for name in nameArray{
print(name)
}
//create a lower case name array
var lowerCaseNameArray:[String] = []
for name in nameArray{
lowerCaseNameArray.append(name.lowercased())
}
lowerCaseNameArray
for i in 1…100{
print(“Hello — \(i)”)
}
for index in 1…9{
print(“\(index)* 7 = \(index*7)”)
}
for index in 1…10 where index % 2 == 0{
print(index)
}
for index in 1…10 where index % 2 != 0{
print(index)
}
var fruitDict = [“red”:”apple”,”yallow”:”bannana”,”green”:”mango”]
for (key , value) in fruitDict{
print(key+”:”+value)
}
let colors = (“red”,”orange”,”yellow”,”green”,”blue”)
colors .0
colors .4
let numberTuple = (1,2,3,4,5)
numberTuple.0
let someTuple = (“Hello”,3.14,true,[“apple”,”bannana”] )
someTuple.3[1]
var fruitTuple = (red:”apple”,yellow:”bannana”,green:”mango”)
fruitTuple.red
________________while loop______________
var index = 1
while index <= 10{
print(index)
index = index + 2
//index = index + 2 = index + = 2
}
var animalArray = [“cat”,”dog”,”lion”,”tiger”]
index = 0
while index < animalArray.count{
print(animalArray[index])
index += 1
}
//即使條件不符也能print一次
var myCounter = 1
repeat{
print(“Just do it \(myCounter) time”)
myCounter += 1
}while myCounter<11