您的位置:首页 > 移动开发 > Swift

Swift语法04.逻辑分支

2016-09-13 17:14 302 查看

一. 分支的介绍

分支即if/switch/三目运算符等判断语句

通过分支语句可以控制程序的执行流程

二. if分支语句

和OC中if语句有一定的区别

判断句可以不加()

在Swift的判断句中必须有明确的真假

不再有非0即真

必须有明确的Bool值

Bool有两个取值:false/true

// 演练一:
let a = 10

// 错误写法:
//if a {
//    print("a")
//}

// 正确写法
if a > 9 {
print(a)
}

// 演练二:
let score = 87

if score < 60 {
print("不及格")
} else if score <= 70 {
print("及格")
} else if score <= 80 {
print("良好")
} else if score <= 90 {
print("优秀")
} else {
print("完美")
}

// 演练三:
// 这个是可选类型,因为只有声明成可选类型后,才可以判断是否为空
// 可选类型会在后续讲解,可先了解即可
let view : UIView? = UIView()

// 判断如果view有值,则设置背景
// 错误写法
//if view {
//    view.backgroundColor = UIColor.redColor()
//}

if view != nil {
view!.backgroundColor = UIColor.redColor()
}


三. 三目运算符

Swift
中的
三目
运算保持了和
OC
一致的风格

var a = 10
var b = 50

var result = a > b ? a : b
println(result)


四.guard的使用

guard是Swift2.0新增的语法

它与if语句非常类似,它设计的目的是提高程序的可读性

guard语句必须带有else语句,它的语法如下:

当条件表达式为true时候跳过else语句中的内容,执行语句组内容

条件表达式为false时候执行else语句中的内容,跳转语句一般是return、break、continue和throw

guard 条件表达式 else {
// 条换语句
break
}
语句组


例子

var age = 18

func online(age : Int) -> Void {
guard age >= 18 else {
print("回家去")
return
}

print("可以上网")
}

online(age)


五.switch分支

switch的介绍

Switch作为选择结构中必不可少的语句也被加入到了Swift中

只要有过编程经验的人对Switch语句都不会感到陌生

但苹果对Switch进行了大大的增强,使其拥有其他语言中没有的特性

switch的简单使用

基本用法和OC用法一致

不同之处:

switch后可以不跟()

case后可以不跟break(默认会有break)

例子:

let sex = 0

switch sex {
case 0 :
print("男")
case 1 :
print("女")
default :
print("其他")
}


简单使用补充1:

一个case判断中,可以判断多个值

多个值以 , 隔开

let sex = 0

switch sex {
case 0, 1:
print("正常人")
default:
print("其他")
}


简单使用补充2:

如果希望出现之前的case穿透,则可以使用关键字
fallthrough


let sex = 0

switch sex {
case 0:
fallthrough
case 1:
print("正常人")
default:
print("其他")
}


Switch支持多种数据类型

浮点型的switch判断

let f = 3.14
switch f {
case 3.14:
print("π")
default:
print("not π")
}


支持字符串类型

字符串的使用后面会详细讲解

let m = 5
let n = 10
var result = 0

let opration = "+"

switch opration {
case "+":
result = m + n
case "-":
result = m - n
case "*":
result = m * n
case "/":
result = m / n
default:
result = 0
}

print(result)


switch支持区间判断

什么是区间?

通常我们指的是数字区间:0~10,100~200

swift中的区间常见有两种

开区间:0..<10 表示:0~9,不包括10

闭区间:0…10 表示:0~10

let score = 88

switch score {
case 0..<60:
print("不及格")
case 60..<80:
print("几个")
case 80..<90:
print("良好")
case 90..<100:
print("优秀")
default:
print("满分")
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: