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

Swift教程之条件语句

2015-07-10 11:34 459 查看
//MARK:----if条件语句
//if
let isOK = true
if isOK
{
print("我好了")
}
else
{
print("我没好")
}
//三元运算符
var sex = isOK ? "男生":"女生"
print(sex)

//MARK:----switch条件语句
//当匹配的 case 分支中的代码执行完毕后,程序会终止switch语句
let charA: Character = "a"
switch charA
{
case "a", "A":  //如果想匹配多个条件,可以在一个case里面把多个条件用(,)隔开
print("The letter a") //每一个 case 分支都必须包含至少一条语句
case "A":
print("The letter A")
default:
print("default")
}

//case 分支的模式也可以是一个值的区间范围
//使用范围匹配来输出任意数字对应的自然语言格式
let count = 3_000_000
var Str: String
switch count
{
case 0:
Str = "没有"
case 1...9:
Str = "个位"
case 10...99:
Str = "十位"
case 100...999:
Str = "百位"
case 1000...9999:
Str = "千位"
default:
Str = "万以上"
}
print("\(count)是\(Str)数")
// 输出 "There are millions and millions of stars in the Milky Way.

//用下划线(_)来匹配所有可能的值。
//使用一个(Int, Int)类型的元组来分类下图中的点(x, y)
let point1 = (0, 3)
switch point1
{
case (0, 0):
print("(0, 0)在原点")
case (_, 0):
print("(\(point1.0), 0) 在X轴上")
case (0, _):
print("(0, \(point1.1))在Y轴上")
case (-2...2, -2...2):
print("(\(point1.0), \(point1.1))在范围内")
default:
print("(\(point1.0), \(point1.1))范围之外")
}

//值绑定(Value Bindings)
//在一个(Int, Int)类型的元组中使用值绑定来分类下图中的点(x, y)
let point2 = (2, 3)
switch point2
{
//此时x只是一个占位符,用来临时的获取switch条件中的一个或多个值
case (let x, 0):
print("在X轴上,值为 \(x)")
case (0, let y):
print("在Y轴上,值为 \(y)")
case let (x, y):
print("在(\(x), \(y))点")
}
// 输出 "在(2, 3)点”

//MARK:----中断语句
//控制传递语句(Control Transfer Statements)
//continue
//停止本次循环,重新开始下次循环
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters
{
switch character
{
case "a", "e", "i", "o", "u", " ":
continue
default:
//输出字母
puzzleOutput.append(character)
}
}
print(puzzleOutput)

//Break
//循环语句中的 break
//中断循环体
for index in 0..<5
{
if index == 2
{
continue
//      break
}
print("index: \(index)")
}
//Switch 语句中的 break
//中断switch代码块
let numberSymbol: Character = "三"
var possibleIntegerValue: Int = 0
switch numberSymbol
{
//拉丁语、阿拉伯语、中文、泰语
case "1", "١", "一", "๑":
possibleIntegerValue = 1
case "2", "٢", "二", "๒":
possibleIntegerValue = 2
case "3", "٣", "三", "๓":
possibleIntegerValue = 3
case "4", "٤", "四", "๔":
possibleIntegerValue = 4
default:
break
}
print("The integer value of \(numberSymbol) is \(possibleIntegerValue).")
// 输出 "The integer value of 三 is 3.

//贯穿(Fallthrough)
//一般用于满足多个条件
//Swift 中的switch,执行一个case之后,想接着往下执行可以用fallthrough关键字
let number = 5
var description = "数字\(number)是"
switch number
{
case 2, 3, 5, 7, 11, 13, 17, 19:    //质数
description += "一个质数"
fallthrough
default:
description += "也是一个整数"
}
print(description)
// 输出 "数字5是一个质数也是一个整数"

//标签的语句(Labeled Statements)
/*
表明break或continue哪个循环体,循环嵌套的时候使用
*/

//根据分数评等级,超过100分跳过,遇到负数停止
var score = [96, 83, 43, 101, 66, 70, -5, 99]
//外层for循环体First
First: for s in score
{     //定义标签First
switch s/10
{
case 10:
continue First  //使用标签,中断本次for循环
case 9:
print("\(s)分为优秀")
case 8:
print("\(s)分为良好")
case 6...7:
print("\(s)分为中等")
case 0:
break First     //使用标签,终止for循环
default:
print("\(s)分为没及格")
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: