Control flow
Generally used in the same way as c,c++,objective C, etc.
The explanation here may be less, we actually write the code will be used, very simple, you can focus on the swift function in Switch,swift more
One thing to note in particular is that the result of a conditional expression in swift must be of type bool
Other words
var a = 19
If a{
}
Such an expression is not correct, because a is of type int
1. If
Commonly used if there are three kinds of
if bool{
}
if bool{
}
else{
}
if bool{
}
else if{
}
else{
}
2. for
The For loop in Swift is divided into for and for in two types
For the For in
The index implicitly declared here is a constant and does not need to be declared in advance.
for index in 1...10{
println(index)
}
If you do not need the exact value of index, you can use an underscore instead
for _ in 1...10{
println("hello hwc")
}
The most common use of for in is to iterate over the array, the dictionary
var dictionary = [1:"one",2:"two",3:"three"]
for (key,value) in dictionary{
println("\(key)->\(value)")
}
Of course, you can also use the for condition to increment
for var index = 0;index < 10;++index{
println(index)
}
3. while
while condition{
statements
}
var i = 0
while i < 10{
println(i)
i++
}
do{
}while condition
var i = 0
do{
println(i)
i++
}while i < 10
4 switch
switch temp{
case value1:
println(value1)
case value2:
println(value2)
defalut:
println(default)
}
The switch statement in Swift can automatically jump out of the
In Swift, each case statement executes at least one local domain
So
Case value1:
Case value2:
println ("")
It will be an error.
In Swfit, if you want to match multiple values,
switch oenValue{
case value1:
println("1")
case value2...value4:
println("234")
default:
In Swift,
switch can directly match the tuple, this is very convenient
A tuple can be a range, or you can match any value with _
Case (0,0): Match (0,0)
Case (0..3,1) match (0,1) (2,1)
Case (_,1) matches any (int,1)
As an example,
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")
}
The branch statement of a case can add additional judgment by where
let somePoint = (1, 1)
switch somePoint {
case let(x,y) where x == y:
println("x is equal to y")
default:
println("x is not equal to y" )
}
5 Continue break Fallthrough return
Continue, terminates this operation in the loop body and continues the next operation
for index in 1...10{
if index % 2 == 1{
continue
}
println(index)
}
Break to immediately end the execution of the current control flow
let x = 10
switch x
{
case 10:
println("right")
default:
break
}
Fallthrough throughout, from one case in Swift to the next case
In Swift, the default is to jump out of the switch statement after a case has ended, regardless of break, if you want to use the Fallthrough keyword like any other language
let x = 0
switch x
{
case 0:
println(0)
fallthrough
case 1:
println(1)
case 2:
println(2)
default:
println("defalult")
}
Execution will find output 0 1
Tagged statements
When we have multilayer loops, or multi-layer switch, how do we know if a continue or break is terminating that layer of control flow? Swift provides a convenient way to: Tagged statements
var x = 1
var y = 1
firstLoop:while x < 5{
secondLoop:while y < 6{
//continue firstLoop or continue secondLoop
}
}
Swift Getting Started Tutorial Series 7-Control flow