Swift笔记(四)之控制流(Control Flow)

Control Flow(控制流)

Swift 提供了类C语言的所有控制流结构.包括循环结构: forwhile;选择支结构: ifswitch.和一些让你业务流程转向的关键字: break, continue等...
除了传统的 for-condition-increment循环外,Swift还新增 for-in循环结构,使得在Arrays,Dictionaries,Ranges,Strings以及其他的sequences能更好更方便的迭代,而不在像以前写一篇迭代代码.现在只需要一行短小的代码即可搞定.当然该for-in就相当于Java中的For(增强for循环).
  • For Loops(for循环)
    Swift提供2种for循环:
    for-in 用于迭代range,sequence,collection or progression.的每一项.
    for-condition-increment执行一条语句使得condition条件达到才结束,通过increment计数来结束循环
    For-In 
    使用for-in循环来迭代:
    ranges://迭代一个范围
    for index in 1...5{
    <span style="white-space:pre">	</span>println("\(index)time 5 is \(index*5)")
    }
    for-in遍历ranges的原理是把1...5得到的数1,2,3,4,5依次载入index中,直到index为5,那么终止循环.
    Note:这里我们需要主要,这个index是一个常量,它的值是循环内自动赋予.它不需要定义就可以使用.因为是在循环体里隐式定义的.不需要主动申明let关键字
      如果我们不需要这个index,我们忽略掉这个值可以使用一个下划线"_"来替代这个index.(这种方式我第 一次是在go语言中见到,第一次看到就喜欢上了这种写法.真的很好)
    let base = 3
    let power = 10
    var answer = 1
    for _ in 1...power {//这里我们不需要在乎每次得到range值.我们只在意每次循环的过程,使用_来忽略它.
        answer *= base
    }
    println("\(base) to the power of \(power) is \(answer)")
    // prints "3 to the power of 10 is 59049"
    Arrays://迭代一个数组
    let names = ["Anna", "Alex", "Brian", "Jack"]
    for name in names {
        println("Hello, \(name)!")
    }
    // Hello, Anna!
    // Hello, Alex!
    // Hello, Brian!
    // Hello, Jack!
    Dictionary://迭代一个字典
    let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
    for (animalName, legCount) in numberOfLegs {
        println("\(animalName)s have \(legCount) legs")
    }
    // spiders have 8 legs
    // ants have 6 legs
    // cats have 4 legs
    除Array和Dictionary之外,你还可以使用for-in来遍历String的每一个Character
    String://迭代字符串
    for character in "Hello" {
        println(character)
    }
    // H
    // e
    // l
    // l
    // o
  • For-Condition-Increment
    for-in循环外,swift还支持带有条件以及增量器类似C语言的for循环:

    for var index = 0; index < 3; ++index {
        println("index is \(index)")
    }
    // index is 0
    // index is 1
    // index is 2
    语法为:
    for  initialization ;  condition ;  increment  {
         statements
    }

    该循环遵照一下规则:
    1:当第一次进入循环时,initialization表达式会且只执行一次,定义一个循环类要使用的变量或者常量.
    2:条件表达式
    Condition会在循环开始进行一次计算,如果计算结果为true那么继续执行,如果为false直接跳出循环.
    3:当循环体代码执行完毕后,计数器开始被执行,赋予初始化的变量新值,然后再进行步骤2,检查条件是否满足.

  • While以及Do-While
    这2种循环就不再多提了,大多数语言都有的.与For循环规则一致:
    While语法:
    while  condition  {
         statements
    }
    Do-While语法:
    do {
         statements
    } while  condition

  • Conditional Statements(条件语句)

    If 条件语句
    该语句不是很难也没加什么新东西,就不再多讲,我们主要关注下Switch条件语句.


    Switch条件语句
    Swift语言提供的Switch相比其他语言Java,C#相比,Switch语言更简洁,首先让程序员不用显示调用break来直接跳出Switch块,其次,一个Case可以包含多个条件用逗号隔开.最好的一个功能是提供自定义参数来获取switch块条件的值,这个特性我在我所学过的语言是没看到的.

    Syntax:
    switch  some value to consider  {
    case  value 1 :
         respond to value 1
    case  value 2 ,
    value 3 :
         respond to value 2 or 3
    default:
         otherwise, do something else
    }
    Simple example:
    let name = "shoper"
    switch(name){
    case "shoper","shawn":
        println("Shoper or Shawn")
    case "chloe":
        println("Chloe")
    default:
        println("not at all")
    }
    //print "Shoper or Shawn" and not
    //print "Shoper or Shawn 
    //Chloe" 
    这个例子中,我们看到2个比较新的东西,
    1.支持多条件,只要满足一个条件即可进入case块(Note:如果是数字也可以使用Ranges替代)
    2.不需要现式申明break了(这种特性在Go语言已经存在),当执行完case块中代码自动个跳出switch语句.如果需要进入下一个case块,加上fallthough即可

Value Bindings(值绑定)
Swift在Switch条件语句中提供了值绑定功能,在case块中能绑定符合条件的临时变量或者常量,但是只能用于当前的case块种,这就是所谓的值绑定.
Simple Example:

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

附加条件: where//我们可以使用再添加附加条件来再次筛选
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    println("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    println("(\(x), \(y)) is on the line x == -y")
case let (x, y):
    println("(\(x), \(y)) is just some arbitrary point")
}
// prints "(1, -1) is on the line x == -y"

  • Control Transfer Statements(控制转移语句)
控制转移语句包含 continue, break, fallthrough, return;用法与其他语言一直,就不再多说.说一下这个 fallthrough,就是在是 switch块中如果要进入下一个 case块就是用 fallthrough
  • Labeled Statements(标签语句)
其实也就是goto语句,不再赘述.









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值