for 循环
for ... in ... 循环
forindexin 1...5{ println("(index)times 5 is * ) } // 1 times 5 is 5 // 2 times 5 is 10 // 3 times 5 is 15 // 4 times 5 is 20 // 5 times 5 is 25 letbase= 3 power10 varanswer1 for _ in...*=base to the power of is// prints "3 to the power of 10 is 59049" names= ["Anna","Alex""Brian""Jack"] namein names { println"Hello,name!"// Hello,Anna! numberOfLegs"spider":8"ant"6"cat"4for(animalNamelegCount)innumberOfLegs printlns have legs"// spiders have 8 legs // ants have 6 legs // cats have 4 legs characterin "Hello" character// H // e // l // o
For-Condition-Increment
for var0;< 3; ++"index is// index is 0 // index is 1 // index is 2
:Int } // index is 2 "The loop statements were executedtimes"// prints "The loop statements were executed 3 times"
while 循环
square0 diceRollwhile< finalSquare // roll the dice if++==7 { diceRoll 1 // move by the rolled amount square+=diceRoll ifboard.count// if we're still on the board,move up or down for a snake or a ladder [] } }
println"Game over!")
do// move up or down for a snake or ladder // move by the rolled amount }square finalSquare )
条件语句
if
var temperatureInFahrenheit 30 iftemperatureInFahrenheit<= 32 "It's very cold. Consider wearing a scarf."// prints "It's very cold. Consider wearing a scarf." =40 else"It's not that cold. Wear a t-shirt."// prints "It's not that cold. Wear a t-shirt." 90 else iftemperatureInFahrenheit >= 86 { "It's really warm. Don't forget to wear sunscreen."// prints "It's really warm. Don't forget to wear sunscreen." 72 if <= } else if }
switch
let
someCharacter
:
Character
=
"e"
switch
someCharacter
{
case
"a"
,
"e"
,
"i"
,
"o"
,
"u"
:
println
(
"
(
someCharacter
)
is a vowel"
)
case
"b"
,
"c"
,
"d"
,
"f"
,
"g"
,
"h"
,
"j"
,
"k"
,
"l"
,
"m"
,
"n"
,
"p"
,
"q"
,
"r"
,
"s"
,
"t"
,
"v"
,
"w"
,
"x"
,
"y"
,
"z"
:
println
(
"
(
someCharacter
)
is a consonant"
)
default
:
println
(someCharacter) is not a vowel or a consonant") } // prints "e is a vowel"
没有隐性陷入
let
anotherCharacter
:
Character
=
"a"
switch
anotherCharacter
{
case
"a"
:
case
"A"
:
println
(
"The letter A"
)
default
:
println"Not the letter A"// this will report a compile-time error
范围匹配
let count = 3_000_000_000_000 countedThings = "stars in the Milky Way" naturalCount: String switch case : naturalCount = "no" "a few" 9: "several"
case 1099 "tens of" 100999 "hundreds of" 1000999_999 "thousands of" default "millions and millions of" }
"There are ) (countedThings.") // prints "There are millions and millions of stars in the Milky Way."
元组匹配
somePoint = (case (): "(0,0) is at the origin"(_"(somePoint.,0) is on the x-axis") is on the y-axis"(-2): ) is inside the box") : ) is outside of the box" // prints "(1,1) is inside the box"
值绑定
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 ycase let x "somewhere else at ()"} // prints "on the x-axis with an x value of 2"
附加条件
yetAnotherPoint ) where x == y) is on the line x == y"== -) is on the line x == -y") is just some arbitrary point"// prints "(1,-1) is on the line x == -y"
控制流变换语句
continue
puzzleInput "great minds think alike" puzzleOutput "" for character in character case "a""e""i""o""u"" "continue += character }
break
numberSymbolCharacter "三" // Simplified Chinese for the number 3 possibleIntegerValueInt? numberSymbol "1""?""一""?"possibleIntegerValue 1 "2""?""二""?"2 "3""?""三""?"3 "4" "?" "四""?"possibleIntegerValue 4 : break }
iflet integerValue = { "The integer value of is (integerValue) } else { "An integer value could not be found for // prints "The integer value of 三 is 3."
fallthrough
integerToDescribe 5 description "The number integerToDescribeis" 711131719+= " a prime number,and also" fallthrough += " an integer." } description// prints "The number 5 is a prime number,and also an integer."
标签语句
gameLoop: while square != finalSquare if diceRoll == 7 switch + case finalSquare// diceRoll will move us to the final square,so the game is over break gameLoop case let newSquare where newSquare > // diceRoll will move us beyond the final square,so roll again continue gameLoop default: // this is a valid move,so find out its effect square diceRoll square += ] } }
) (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|