Swift Language Tutorial for Beginner 3 - Control statements

In Swift Language Tutorial for Beginner 2 - Data Type, we learned about Swift’s data type, and this time we’ll take a look at the control statements in Swift.

Similar to other high-level languages, Swift also consists of three main control statements, namely: order, branching (conditional), and looping.

Unlike C/C++ or Java, Swift has no entry functions, in this regard it is more like dynamic languages such as Python, Basic, etc. So the swift version of the classic Hello World becomes the following form:

1
print("Hello, World")

For the print function, swift also lets us output variables with placeholders, rewriting the Hello World routine above:

1
2
var name = "World"
print("Hello, \(name)")

Branch - If statement

In swift, the main branch statement is also the if statement that we are familiar with. Just in terms of writing, even the parentheses of the wrapping condition expression can be omitted, such as:

1
2
3
4
var c = 10
if c<10 {
print(c)
}

Of course, there are also if-else combinations, such as:

1
2
3
4
5
6
7
var c = 10

if c>10 {
c-=10
} else {
print(c)
}

Multi-branching:

1
2
3
4
5
6
7
8
9
10
11
if c>0 && c<10 {
print(c)
} else if c<=0 {
c = -c
print(c)
} else if c<=10 && c<20 {
c-=10
print(c)
} else {
print("bigger")
}

Branch - Switch statement

Of course, multiple branches can also use switch statements, and in switch, unlike C/C++, swift can use more than just integers as conditions, such as:

Use the switch statement for character branching judgment

1
2
3
4
5
6
7
8
9
10
11
12
var charac:Character = "b"

switch charac {
case "a":
print("chara is a")
case "b":
print("chara is b")
case "c":
print("chara is c")
default :
print("no charac")
}

Multiple branches can be included in the same case, such as:

1
2
3
4
5
6
7
8
9
10
var charac:Character = "b"

switch charac {
case "a","b","c" :
print("chara is word")
case "1","2","3" :
print("chara is num")
default :
print("no charac")
}

If it is an order value, you can also use a range in a case, such as:

1
2
3
4
5
6
7
8
9
var num = 3
switch num {
case 1...3 :
print("1<=num<=3")
case 4 :
print("chara is num")
default :
print("no charac")
}

Loop - for statement

In swift, the for statement of the control loop is closer to the syntax of a dynamic language (e.g., Python) than to the syntax of C/C++.

A basic for statement routine is as follows:

1
2
3
for index in 1...5 {
print(index)
}

The statement will print 1,2,3,4,5, Note: It’s three “.”, not two

If you do not need to use index in the loop, you can use the placeholder : _” instead of the variable, which you can write:

1
2
3
4
var sum=0;
for _ in 1...3 {
sum += 1
}

Of course, the for statement is also convenient for iterating over collection types (Set, Dictionary, Array). We’ll show you this part when we talk about collections.

Loop - repeat-while statement

When you are unsure of the number of loops, you can use repeat-while statements, such as:

1
2
3
4
5
6
var j=0

repeat {
print("repeat while")
j+=1
} while j<10

Next

Next, we’ll briefly introduce two special types in Swift - tuples and optionals Swift Language Tutorial for Beginner 4 - Tuple .

本文标题:Swift Language Tutorial for Beginner 3 - Control statements

文章作者:Morning Star

发布时间:2022年01月11日 - 17:01

最后更新:2022年01月17日 - 07:01

原始链接:https://www.mls-tech.info/app/swift/swift-tutorial-3-control-flow-en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。