寒城攻略:Listo 教你 25 天学会 Swift 语言 - 07 Control Flow

importFoundation


//***********************************************************************************************

//1.Control Flow(控制流)

//_______________________________________________________________________________________________

//介绍

//除了C里面的for条件递增循环,Swift增加for-in循环,用来更加简单的遍历数组,字典,范围,字符串和其他序列类型

//Swift中的switch语句也更多的用在Cswitch语句的不需要写breakswitch语句并且包含范围匹配,元组和投到一个特定的类型


//2.For Loopfor循环)

//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 Loopwhile循环)

//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"_):

ottom:0px; padding-top:0px; padding-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(209,)) 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

:

分为不及格")

}

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...