import UIKit
Switch语句
let sex = 1
switch sex {
case 0:
print("man")
case 1:
print("women")
default:
print("not know")
}
switch sex {
case 0,1:
print("normal")
fallthrough
default:
print("fallthrough, so this may be not right answer, not know")
}
let i = 3.145
switch i{
case 3.14:
print("pai")
default:
print("not pai")
}
let name = "zhangdanfeng"
switch name {
case "zhangdanfeng":
print("the person we are looking for")
default:
print("not the guy we want")
}
let str = "*"
let m = 8
let n = 7.2
var result = 0
switch str {
case "+":
result = m+Int(n)
case "-":
result = m-Int(n);
case "*":
result = m*Int(n);
case "/":
result = m/Int(n);
default:
print("not know")
}
//0...30, 表示0-30
//0..<30, 表示0-29
let score = 30;
switch score {
case 0..<40:
print("low")
default:
print("just so so")
}
for语句
//for var i=0;i<10;i++{//i++方法已经废弃
// print(i);
//}
for i in 0..<10{
print(i)
}
for _ in 0..<10{
print("zhangdanfeng")
}
var a = 10
while a>0{
print(a)
a -= 1
}
repeat语句
- 1,repeat while(原来是do while )
var b = 10
//do while语句用repeat while替换
repeat {
print(b)
b -= 1
}while b>0