For loop
Swift provides two kinds of for-loop forms: (1) for-in is used to traverse a range (range), sequence (sequence), set (collection), and all elements within the series (progression) execute a series of statements. (2) for conditional increment statement. Used to repeat a series of statements.
for-inYou can use for-in to iterate through all the elements in a collection. For index in 1...5 {
println ("\ (index) times 5 is \ (Index * 5)")
}
In the example above, index is a constant that is automatically assigned each time. In this case, index does not need to be declared before it is used, it can be implicitly declared without using the Let keyword, just by including it in the declaration of the Loop. If you do not need to know the value of each item in the interval, you can use an underscore (_) to override the variable name to ignore access to the value: let base = 3
Let power = 10
var answer = 1
For _ in 1...power {
Answer *= Base
}
println ("\ (base) to the power of \ (power) is \ (answer)")
Use for-in to traverse an array: let names = ["Anna", "Alex", "Brian", "Jack"]
For name in Names {
println ("Hello, \ (name)")
You can also access its key-value pairs by traversing a dictionary. As you traverse the dictionary, each element of the dictionary is returned as a (key,value) tuple, and you can interpret (Key,value) tuples using the displayed constant names in the for-in loop.
Let Numberoflegs = ["Spider": 8, "Ant": 6, "Cat": 4]
For (Animalname, Legcount) in Numberoflegs {
println ("\ (animalname) s has \ (legcount) legs")
In addition to arrays and dictionaries, you can also use for-in to iterate through the characters in a string: for character in "Hello" {
println (character)
}
For condition LoopSwift provides a traditional C-style for loop: for var index = 0; Index < 3; index++ {
println ("index is \ (index)")
Format
for initialization; Condition Increment {
Statements
}
For in Swift does not need to use parenthesesSwitch Format: Example:
Switch some value to consider {
Case value 1:
Respond to value 1
Case value 2,
Value 3:
Respond to value 2 or 3
Default
Otherwise, do something else
}
Example: 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 consonatnant")
Default
println ("\ (Somecharacter) is not a vowel or a consonant")
}
The Switch statement must be complete. Each possible value must have at least one case branch corresponding to it. At the end of switch, there must be a default statement. Each case branch must contain at least one statement. The following code is invalid: let Anothercharacter:character = "a"
Switch Anothercharacter {
Case "a":
Case "A":
println ("The Letter A")
Default
println ("Not the letter A")
A case can contain multiple schemas: switch some value to consider {
Case value 1,
Value 2:
Statements
}
Note: If you want to run through a specific case branch, use the Fallthrough statement.
interval MatchingThe case branching pattern can also make a range of values. Let count = 3_000_000_000_000
Let countedthings = "stars in the Milky"
var naturalcount:string
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"
}
println ("There is \ (Naturalcount) \ (countedthings)")
Meta-groupYou can use tuples to test multiple values in the same switch statement. An element in a tuple can be a value, or it can be an interval. In addition, use "_" 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 ont 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")
Unlike the C language, Swift allows multiple case to match the same value. If there are multiple matches, only the first matched case branch is executed, and the remaining case branches are ignored.
Value BindingThe pattern word order of a case branch binds a matching value to a temporary factory that or variable that can be referenced in the case branch, which is called value binding. 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, let Y):
println ("somewhere else at (\ (x), \ (y))")
NOTE: This switch statement does not include the default branch. This is because the last case can match the tuple of all values, which makes the switch statement complete.
WhereThe case branching pattern can use the where statement to determine additional conditions. Let 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 lone x = =-y)")
Case let (x, y):
println ("(\ (x), \ (y)) is just some Arbitray")
}
Control Transfer Statements
Break of the switch statementWhen using break in switch, execution of the switch code block is immediately interrupted and jumps to the first line of code after the switch code block ends. This feature can be used to match or omit one or more branches. Because Swift needs to include all branches and does not allow empty branches, it is sometimes necessary to deliberately match or omit a branch in order to make the intent more obvious, and when you want to ignore a branch, you can write a break statement within that branch.
Let Numbersymbol:character = "three"
var possibleintegervalue:int?
Switch Numbersymbol {
Case "1", "?", "One", "?":
Possibleintegervalue = 1
Case "2", "?", "Two", "?":
Possibleintegervalue = 2
Case "3", "?", "Three", "?":
Possibleintegervalue = 3
Case "4", "?", "Four", "?":
Possibleintegervalue = 4
Default
Break
}if Let IntegerValue = possibleintegervalue {
println ("the integer value of \ (Numbersymbol) is \ (IntegerValue)")
} else {
println ("An integer value could not being found for \ (Numbersymbol)")
}
throughIf you do want a C-style cross-cutting feature, you can use the Fallthrough keyword in each case branch that needs the feature. 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)
Swift Learning notes-5. Control flow