Closed Interval Operator
The closed interval operator (a... B) defines an interval that contains all values from A to B (including a and B). It can only be a number.
For index in 1... 5 {println ("\ (INDEX) * 5 = \ (Index * 5)")} var names = ["Anna", "Alex", "Brian ", "Jack"] Names [2... 3] = ["A", "B"] // cannot be addedSemi-closed interval
A semi-closed interval (A. B) defines a range from A to B, but does not include B. Convenient array subscript
Let names = ["Anna", "Alex", "Brian", "Jack"] Let COUNT = names. countfor I in 0 .. count {println ("the \ (I + 1) Name \ (Names [I])")} For I in 0... count-1 {println ("the \ (I + 1) Name \ (Names [I])")}
The closed interval and semi-closed interval replace the traditional. For loop. I ++ can also be used on arrays.
Fon-in
For-in is used to traverse a range, sequence, collection, and all the elements in the progression to execute a series of statements.
// Interval for index in 1... 5 {println ("\ (INDEX) times 5 is \ (Index * 5)")} // array Let names = ["Anna", "Alex", "Brian ", "Jack"] For name in names {println ("Hello, \ (name )! ")} // Dictionary let numberoflegs = [" Spider ": 8," ant ": 6," cat ": 4] For (animalname, legcount) in numberoflegs {println ("\ (animalname) s have \ (legcount) legs")} // character for character in "hello" {println (character )}
In for-in, index is automatically assigned a value at the beginning of each loop traversal.Constant. In this case, the index does not need to be declared before it is used. You only need to include it in the loop declaration, so that it can be implicitly declared without using the let keyword declaration.
The index constant only exists in the lifecycle of a loop. If you want to access the index value after the loop is completed, or you want to make index a variable rather than a constant, you must declare it before the loop.
For Loop
Constants and variables declared in the initialization expression (for example, VAR Index = 0) are valid only in the lifecycle of the for loop. If you want to access the index value after the loop ends, you must declare the index before the cycle begins.
for initialization; condition; increment {statements}
Equivalent
initializationwhile condition {statementsincrement}Switch
After a switch is matched, the next case is not executed and the switch statement is terminated. Therefore, the break statement is not required. If you want to run through a specific case Branch, use the fallthrough statement.
Each case branch must contain at least one statement. You can use "," to match multiple cases with the same value.
Let somecharacter: character = "E" Switch somecharacter {Case "A", "E", "I", "O", "U": println ("\ (somecharacter) is a vowel ") Case" B "," C "," D ",: println (" \ (somecharacter) is a consonant ") Default: println (" \ (somecharacter) is not a vowel or a consonant ")} // The case condition can be a range to check whether a number is in a range switch count {Case 0: naturalcount = "no" Case 1... 3: naturalcount = "a few" case 4... 9: naturalcount = "several" case 10. .. 99: naturalcount = "tens of" case 100... 999: naturalcount = "hundreds of" case 1000... 999_999: naturalcount = "thousands of" Default: naturalcount = "millions and millions of"} Use tuples to test multiple values in the same switch statement. The elements in the tuples can be values or intervals. In addition, underlines (_) are used to match all possible values. Let somepoint = (1, 1) Switch somepoint {Case (0, 0): println ("(0, 0) is at the origin") Case (_, 0 ): println ("(\ (somepoint.0), 0) is on the X-axis") Case (0, _): println ("(0, \ (somepoint.1 )) is on the y-axis ") Case (-2... 2,-2... 2): println ("(\ (somepoint.0), \ (somepoint.1) is inside the box") Default: println ("(\ (somepoint.0), \ (somepoint.1 )) is outside of the box ")} // bind the value to 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) ")} // output" on the X-axis with an X value of 2 "// use wherelet 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")} // output "(1,-1) is On The Line X =-y"Label
Use break and continue to jump in multiple loops or switch nesting
Gameloop: While square! = Finalsquare {If ++ diceroll = 7 {diceroll = 1} switch square + diceroll {Case finalsquare: // get to the last square, when the game ends, break gameloop case Let newsquare where newsquare> finalsquare: // go beyond the last square and throw the dice again. Continue gameloop default: // valid square + = diceroll square + = Board [square]} println ("game over! ")