swift:continue,break,fall through,return 简单说明

swift 有4个 控制转移句: continue , break, fall through, return。

  1. continue:结束本次循环,执行下个循环,不跳出循环。

let puzzleInput = "hello world“

var puzzleOutput = ""

for character in puzzleInput {

switch character {

case "a", "e", "i", "u", " " :

continue

default:

puzzleOutput += character

}

}

打印结果: hllwrld


2. break:立即结束整个流程控制语句执行。

let numberSymbol: Character = "三"

var possibleIntegerValue: Int?

switch numberSymbol {

case "1","١", "","":

possibleIntegerValue = 1

case "2","٢", "","":

        possibleIntegerValue = 2
    case "3" , " ٣ " , " " , " " :

        possibleIntegerValue =3

    case "4" , " ٤ " , " " , " " :
        possibleIntegerValue = 4
    default :

   break

}

iflet integerValue = possibleIntegerValue {

             println ( "The integer value of \(numberSymbol) is \(integerValue) ." )
    } else {
             println ( "An integer value could not be found for \(numberSymbol) " )

}

}


3.Fallthrough:在switch中,在不判断下个case的条件下,执行下个case里的语句

let integerToDescribe =5

       var description = "The number \(integerToDescribe) is"

         switch integerToDescribe {
         case 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 :
            description += " a prime number, and also"
             fallthrough
         default :
            description += " an integer"

        }

       println(description)

打印结果:The number 5 is a prime number, and also an integer


4.return: 是方法的返回。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值