逻辑分支和循环

12 篇文章 0 订阅

逻辑分支和循环语句

逻辑分支
if 语句
let a = 1
if a == 1 {
    print("a等于1")
} else {
    print("a不等于1")
}

if语句中条件没有小括号,执行语句就算只有一句也不可以省略花括号

guard 语句

guard是Swift2.0之后新增的语法,它与if语句非常相似,他的目的是提高程序的可读性。
guard必须包含else语句,else中一般使用return、break、continue、throw跳出。
guard当条件表达式为true是,跳过else中的语句,false执行else中的语句

func checkGuard(age: Int, hasCard: Bool) {
    
    guard age >= 18 else {
       print("未成年,不可以上网")
        return;
    }
    
    guard hasCard else {
        print("拿身份证去")
        return;
    }
    
    print("可以上网")
}

guard语句必须在函数中使用

switch 语句
let a = 10

switch a {
    case 1, 11:
        print("1");
    case 2:
        print("2")
    default:
        print("NO")
}

1、switch 后面可以不跟()
2、switch 后面可以跟多个条件,用,分开
3、case 不具有穿透效果,需要穿透效果需要加fallthrough,且不需要加大括号
4、必须要加default
switch的判断条件可以是任何类型,但case中相应的条件的类型也要一致

新特性:

1、区间匹配:值可以在一个区间中匹配。

let approximateCount = 62
let countedThings = "moons orbiting Saturn"
var naturalCount: String
switch approximateCount {
case 0:
    naturalCount = "no"
case 1..<5:
    naturalCount = "a few"
case 5..<12:
    naturalCount = "several"
case 12..<100:
    naturalCount = "dozens of"
case 100..<1000:
    naturalCount = "hundreds of"
default:
    naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
// prints "There are dozens of moons orbiting Saturn."

approximateCount  在 switch 语句中进行评定。每个 case 都与数字或者区间进行对比。由于 approximateCount 的值在12和100之间, naturalCount 被赋值 “dozens of” ,并且执行结果传递出了 switch 语句。

2、元组匹配:使用元组来在一个switch语句中测试多个值。每个元组中的元素都可以与不同的值或者区间进行匹配。另外,使用下划线( _)来表明匹配所有可能的值。

let somePoint = (1, 1)
switch somePoint {
case (0, 0):
    print("(0, 0) is at the origin")
case (_, 0):
    print("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
    print("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
    print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
    print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}

// prints "(1, 1) is inside the box"

3、值绑定:可以将匹配到的值临时绑定为一个常量或者变量,来给情况的函数体使用。这就是所谓的值绑定,因为值是在情况的函数体里“绑定”到临时的常量或者变量的。

let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
    print("on the x-axis with an x value of \(x)")
case (0, let y):
    print("on the y-axis with a y value of \(y)")
case let (x, y):
    print("somewhere else at (\(x), \(y))")
}
// prints "on the x-axis with an x value of 2"

4、where:可以使用 where 分句来检查额外的情况。

let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
    print("(\(x), \(y)) is just some arbitrary point")
}
// prints "(1, -1) is on the line x == -y"
循环
For-in循环

循环遍历序列,比如一个范围的数字、数组中元素、字符串中的字符

let names = ["Anna", "Alex", "Jack"]

for name in names {
    
    print("Hellow, \(name)!")
}

// 如果你不需要序列的每一个值,你可以使用下划线来取代遍历名以忽略值。
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
// prints "3 to the power of 10 is 59049"

while循环

while 循环通过判断单一的条件开始。如果条件为true,语句的合集就会重复执行直到条件变为false

while condition {
    // Code
}
repeat-while循环

while 循环的另一种形式,在判断执行条件前,会先执行一次循环体内的代码,然后继续重复循环直到条件为false。

repeat {
   // Code
} while condition
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值