if語句
var m:Int = 1;
m = 3;
if m>2 {
print("m大于2");
} else {
print("\(m)");
}
switch
swift不需要加break跳出
var name:String = "小明";
name = "系哦啊";
switch name {
case "小明":
print("牛逼");
default:
print("二逼");
}
區(qū)間匹配
let age:Int = 10;
switch age {
case 0...10:
print("蘿莉");
default:
print("未知");
}
元祖匹配
let point = (2,0);
switch point {
case (0,0):
print("點在坐標原點");
case (_,0):
print("點在x軸上");
case (0,_):
print("點在y軸上");
case (-3...3, -3...3):
print("坐標在長寬為6的正方形內(nèi)")
default:
print("點");
}
case中還可以使用where關鍵字來做額外的判斷條件
//var聲明
var n:Int = 6;
switch n {
case 0...10 where n==6:
print("n====6");
case 0...10:
print("11");
default:
print("");
}
for循環(huán)
//已棄用
//for var i=1; i<100; i++ {
// print("\(i)")
//}
for i in 10..<20 {
print("i============\(i)");
}
let GPLArr = [1,2,3,4];
for a in GPLArr {
print("GPLArr======\(a)");
}
forEach
(1...10).forEach {
print($0)
}
while
// 格式:while 布爾值 {}
// 說明:只有當while后面的布爾值為false克懊,才停止while語句谭溉,否則一直執(zhí)行while語句
var h = 0 //只有滿足條件(i<5)才跳出while語句
while (h<5) {
print("h=\(h)")
h += 1
}