寒城攻略:Listo 教你 25 天学会 Swift 语言 - 07 Control Flow
importFoundation //*********************************************************************************************** //1.Control Flow(控制流) //_______________________________________________________________________________________________ //介绍 //除了C里面的for条件递增循环,Swift还增加了for-in循环,用来更加简单的遍历数组,字典,范围,字符串和其他序列类型 //Swift中的switch语句也更多的用在C,switch语句的不需要写break,switch语句并且包含范围匹配,元组和投到一个特定的类型 //2.For Loop(for循环) //For-In forindexin1...5{ //最简单的for in语句来实现循环 println(index) } //_______________________________________________________________________________________________ //当不需要使用循环中的值的时候,可以使用_来替代index letbase =3 letpower =10 varanswer =1 _...power{ //使用_来替代index元素实现循环 answer*=base } println("(base)to the power of)isanswer)") //使用for in获取数组中的元素 letnames = ["Listo","Pin"] fornameinnames{ println("Hello,(name)!") } //使用for in获取字典中的元素 letnumberOfLeg = ["Spider":8,"Ant":6,27)">"Cat":4] for(animalName,legCount)numberOfLeg{ "(animalName)s have(legCount)legs") } //使用for in获取字符串中的每个字符 forcharacterin"Hello"{ println(character) } //传统的for循环 forvarindex =0; index <3; ++index{ "index is(index)") } //3.While Loop(while循环) //while的使用 var number =2 //while循环举例 whilenumber>=1{ println("nice") number-- } //do while的用法 varnumber1 =2 do{ ) number1-- }whilenumber1>=1 //4.Conditional Statements(条件语句) //if语句实现条件判断 vartemperatureInFahrenheit =30 iftemperatureInFahrenheit<=32{ ("It's very cold,consider wearing a scarf") } //if else语句实现条件控制 temperatureInFahrenheit=40 ("It's very cold. consider wearing a scarf") } else("It's not that cold.wear a t-shirt is ok" //if else if else语句实现条件控制 90 ) } else86("It's really warm.Don't forget to wear a sunscreen"("It's not that cold.wear a t-shirt" //Swtch特性:与C语言和Objectve-C中的switch语句不同,在swift中,当匹配case块中的代码执行完毕后,程序会终止switch语句,而不会继续进行case块。也就是不需要显式的添加break语句,但是每个case必须包含至少一条语句,否则无效 //简单的switch语句应用举例 letsomeCharacter:Character="e" switchsomeCharacter{ case"a""e""i""o""u": //当switch语句中的case语句条件并列的时候,用逗号隔开即可 someCharacter)is a consonant") "b": )is a consontant") default)is not a vowel or a consonant" //在switch语句中应用范围匹配 count =3_000_000_000_000 countedThings ="stars in the Milky Way" varnaturalCount:String switchcount{ //switch语句中加入范围判断 case0: naturalCount="no" case1...3: "a few" 4...9: "several" 10...99: "tens of" 100...999 naturalCount="hundreds of" 1000...999_999="thousands of" default="millons and millons of" } "There are(naturalCount)(countedThings" //在switch语句中使用元组 letsomePoint = (1,1) somePoint{ case(0,216)">0): ("(0,0) is at the origin") case(_,0)">("(somePoint.),0) is on the x-axis"_): )) is on the y_axis"case(-2...2,-2): )) is inside the box")) is outside the box" //值绑定 letanotherPoint = (20) //case块的模式允许将匹配的值绑定到一个临时的变量或者常量上,这些常量或者变量在该case块里就可以被引用了,这就是值绑定 anotherPointletx,0)">("on the x-axis with an x value of(x)"lety): ("on the y-axis with a y value of(y)"caselet(x,y): ("somewhere else at ((x),0)">(y))" //where语句 letyetAnotherPoint = (1) //使用where添加额外的判断条件 yetAnotherPointwherex == y: (y)) is on the line x == y"wherex == -y: (y)) is on the line x == -y"(y)) is just some arbitrary point" //5.Control Transfer Statements(控制转移语句) //使用continue语句跳过本次迭代 puzzleInput ="great minds think alike" varpuzzleOutput ="" puzzleInput{ switchcharacter{ "a": continue //只要匹配到元音字母或者空格字符的时候,就调用continue语句使本次迭代结束,从开始下次的循环迭代 : puzzleOutput.append(character) } } (puzzleOutput //使用break语句跳出框架 puzzleInput1 ="great minds think alike" varpuzzleOutput1 =forcharacter1puzzleInput1{ switchcharacter1{ break //当在switch代码快中使用break语句时,只要匹配到元音字母或者空格字符的时候,就调用break语句使跳出switch语句,继续进行循环进行循环;当在for循环中使用break语句时,直接跳出循环不在进行循环 puzzleOutput1.append(character1) } } (puzzleOutput1 //综合使用控制转移语句 letnumberSymbol:"三" varpossibleIntegerValue:Int? numberSymbol"1"一" possibleIntegerValue1 "2"二"2 "3"三"3 "4"四"4 : break break来跳出switch } integerValue =possibleIntegerValue("The integer Value ofnumberSymbol(integerValue)"("An integer value could not be found for //6.Fallthrough //使用fallthrough并列两句case letintegerToDescribe =5 vardescription ="The number(integerToDescribe)is " integerToDescribe3,216)">5,216)">7,216)">11,216)">13,216)">17,216)">19: description+="a prime number,and also" fallthrough +=" an integer" } () //这个例子定义String类型的变量description并且给它设置了一个初始值。函数使用switch逻辑来判断integerToDescribe变量的值。当integerToDescribe的值属于列表中的质数之一的时候,该函数添加一段文字在description后,来表明这个数字是一个质数。然后它使用fallthrough关键字来"落入" default分支中,default分支添加一段额外的文字在description后面,然后代码结束 //7.Labeled Statements(标签语句) //在Swift中,我们可以在循环体和switch代码块中嵌套循环体和swith代码块来创造复杂的控制流结构。然而循环体和switch代码块两者都可以使用break语句来提前结束整个方法体。因此显式的指名break语句想要终止的是那个循环体或者switch代码快,这样会很有用。显式的声明continue语句想要影响那一个循环体也会很有用。 //为了实现这个目的,你可以使用标签来标记一个循环体或者switch代码块,当使用break或者continue时,带上这个标签,可以控制该标签代表的对象的中断或者执行。 //产生一个带标签的语句是通过在该语句的关键字的同一行前面放置一个标签,并且该标签后面需要带着一个冒号。以while循环体举例 /* label name: while condition{ statements } */ //标签语句实例代码 varscore = [97,216)">87,216)">34,216)">100,216)">88,216)">49,216)">4,216)">80] First:forsscore{ switchs/10{ 10: continue 9: "(s)分为优秀") 8: 分为良好7: 分为中等") case: //题目要求碰到异常的分数终止循环而不是跳过,当不添加标签使用break时,我们终止的是switch语句,而不是for循环,所以我们需要标签语句来标签for循环然后break for循环的标签 breakFirst : 分为不及格") } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |