swift switch选择结构
2016-10-14 01:26
267 查看
1、一般用法
swift语言的switch中自带break,所以使用时可以省略不写
swift语言中case必须包含所有情况,否则必须有default关键字
2、在一个case中判断多个值
3、使用switch判断其他类型
别的语言switch只能判断整形,swift能判断所有类型
4、case后面可以写一个范围
5、switch与元组的结合使用
//与元组解包结合使用
6、fallthrough的使用
fallthrough可以取消break,是程序语句执行下去
注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。
// 注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。
// 所以,首先,fallthrough不能跳转到一个有逻辑的case中
// 下面的程序报错
//let num = 3
//switch num{
//case -3…3:
// print(“small number”)
// fallthrough
//case let x where x%2 == 1:
// print(“odd number”)
//}
// 其次,不要用switch和fallthrough组合复杂的判断逻辑
// fallthrough应该用于从一般到特殊的判定
// 以下代码的输出,就是错误的。
swift语言的switch中自带break,所以使用时可以省略不写
swift语言中case必须包含所有情况,否则必须有default关键字
// if - else let rating = "A" if rating == "A"{ print("Great!") } else if rating == "B"{ print("Just so-so") } else if rating == "C"{ print("It's Bad") } else{ print("Error") } // switch switch rating{ case "A": print("Great!") case "B": print("Just so-so") case "C": print("It's Bad") default: print("Error") }
2、在一个case中判断多个值
// 在一个case中判断多个值 switch rating{ case "a","A": print("Great!") case "b","B": print("Just so-so") case "c","C": print("It's Bad") default: print("Error") }
3、使用switch判断其他类型
别的语言switch只能判断整形,swift能判断所有类型
// 使用switch判断浮点数 let x = 2.8 switch x{ case 2.8: print("I'm 2.8") default: print("I'm not 2.8") } // 使用switch判断布尔值 let y = true switch y{ case true: print("I'm true") default: print("I'm false") }
4、case后面可以写一个范围
let score = 90 switch score{ case 0: print("You got an egg!") case 1..<60: print("You failed.") case 60: print("Just passed") case 61..<80: print("Just so-so") case 80..<90: print("Good") case 90..<100: print("Great!") case 100: print("Perfect!") default: print("Error score.") }
5、switch与元组的结合使用
let vector = (1,1) switch vector{ case (0,0): print("It's origin!") case (1,0): print("It an unit vector on the positive x-axis.") case (-1,0): print("It an unit vector on the negative x-axis.") case (0,1): print("It an unit vector on the positive y-axis.") case (0,-1): print("It an unit vector on the negative y-axis.") default: print("It's just an ordinary vector.") }
//与元组解包结合使用
let point = (1,1) switch point{ case (0,0): print("It's origin!") case (_,0): print("It on the x-axis.") case (0,_): print("It on the y-axis.") default: print("It's just an ordinary point.") } switch point{ case (0,0): print("It's origin!") case (_,0): print("It on the x-axis.") case (0,_): print("It on the y-axis.") case (-2...2,-2...2): print("It's near the origin.") default: print("It's just an ordinary point.") } // Value binding switch point{ case (0,0): print("It's origin!") case (let x,0): print("It on the x-axis.") print("The x value is \(x)") case (0,let y): print("It on the y-axis.") print("The y value is \(y)") case (let x,let y): print("It's just an ordinary point.") print("The point is ( \(x) , \(y) )") }
6、fallthrough的使用
fallthrough可以取消break,是程序语句执行下去
注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。
switch point{ case (0,0): print("It's origin!") fallthrough case (_,0): print("It's on the x-axis.") fallthrough case (0,_): print("It's on the y-axis.") fallthrough case (-2...2,-2...2): print("It's near the origin.") fallthrough default: print("It's just an ordinary point.") } /** It's origin! It's on the x-axis. It's on the y-axis. It's near the origin. It's just an ordinary point. */
// 注意:fallthrough 并不会判断下一个case(或者default)是否符合switch的条件,而是直接跳到下一个case(或者default)。
// 所以,首先,fallthrough不能跳转到一个有逻辑的case中
// 下面的程序报错
//let num = 3
//switch num{
//case -3…3:
// print(“small number”)
// fallthrough
//case let x where x%2 == 1:
// print(“odd number”)
//}
// 其次,不要用switch和fallthrough组合复杂的判断逻辑
// fallthrough应该用于从一般到特殊的判定
// 以下代码的输出,就是错误的。
let num = 3 switch num{ case -3...3: print("small number") fallthrough case 0: print("zero") fallthrough case -100..<0: print("negative number") fallthrough case 1...100: print("positive number") fallthrough default: print("a number") } /** small number zero negative number positive number a number */
相关文章推荐
- C语言学习-选择结构if语句、switch语句
- 选择结构(if..else..,switch,try..catch..)的拆分
- 选择结构的程序设计——if和switch使用
- switch-case选择结构
- 20.switch选择结构
- Java语法基础之选择结构的if语句、switch语句详解
- switch语句与多重if选择结构的异同点
- java基础——流程控制(选择结构)switch语句
- 跟着姜少学Java基础编程之七:选择结构之switch
- 黑马程序员__选择结构if语句_switch语句
- java学习之switch选择结构
- Switch选择结构(等值条件)
- Java_18_三大结构_多选择结构_switch_case穿透
- 由switch选择结构理解局部变量
- javascript中的选择结构if和switch的简单使用方式及实际操作的易错点
- 流程控制语句——switch选择结构语句
- java学习之switch选择结构
- 选择结构--if和switch语句
- (转)选择结构if语句和switch语句的区别