文章目录
  1. 1. Swift学习-05 循环结构
    1. 1.1. 循环结构
    2. 1.2. 选择结构
    3. 1.3. switch语句的高级用法
    4. 1.4. 控制转移和二维数组

Swift学习-05 循环结构

循环结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//---------------5.1 循环结构---------------//
/**
* 顺序结构
* 循环结构
* 选择结构
* for-in
* 遍历区间
* 遍历字符串
* 遍历数组
* 遍历字典
*---------------
* for
* for inittialization ; condition; increments
* {
* statements
* }
*---------------
* while
*
* while condition
* {
* statements
* increments
* }
*---------------
* do-while
*
* do
* {
* statements
* increments
* }while condition
*/
/**
//for-in遍历区间
for i in -99...99
{
i*i
}
//for-in遍历字符串
let str = "Hello, iMooc!"
for c in str
{
println(c)
}
//for-in遍历数组
let coursesArray = ["慕课网2048私人订制","CSS3 3D 特效","玩儿转Swift"]
for item in coursesArray
{
println(item)
}
//for-in遍历数组-元组方式显示索引
for (index,item) in enumerate(coursesArray)
{
println("\(index): \(item)")
}
//for-in遍历字典
let coursesDictionary = [76:"慕课网2048私人订制" , 77:"CSS3 3D 特效" , 127:"玩儿转Swift"]
for (index,item) in coursesDictionary
{
println("\(index): \(item)")
}
//for-in遍历区间-不关心索引
let base = 2
let power = 10
var result = 1
for _ in 1...power
{
result *= base
}
result
/***************************************/
//for循环
for var i = -100; i <= 100; i++
{
i * i
}
/***************************************/
//while循环
var arr:[Int] = []
//往数组中插入100个随机数
for var i = 0; i < 100; i++
{
arr.append( Int(arc4random()%1000))
}
//对数组从小到大排序
arr.sort(<)
arr
*/

选择结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//---------------5.2 选择结构---------------//
/**
* switch some value to consider {
* case value1:
* respond to value1
* case value2:
* respond to value2
* case value3:
* respond to value3
* default
* otherwise, do something else
* }
*/
/*
var rating:Character = "A"
switch rating
{
case "a","A":
println("greet!")
case "B":
println("Just so-so")
default:
println("it is bad!")
}
var score = 90
switch score
{
case 0:
println("egg")
case 1..<60:
println("faild")
case 60...100:
println("passed")
default:
println("something wrong")
}
*/

switch语句的高级用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//---------------5.2 switch语句的高级用法---------------//
//判断元组
/**
var coordinate = (1,1)
switch coordinate
{
case (0,0):
println("It's at orign!")
case(1,0):
println("It's an unit vector on the positive x-axis.")
case(-1,0):
println("It's an unit vector on the positive x-axis.")
case(0,1):
println("It's an unit vector on the positive x-axis.")
case(0,-1):
println("It's an unit vector on the positive x-axis.")
default:
println("It's just an ordinary coordinate")
}
//使用下划线(_)忽略部分数值
let loginResult = (true, "QingU")
let ( isLoginSuccess , _) = loginResult
if isLoginSuccess
{
println("登录成功")
}
//判断元组数据
let coordinate2 = (1,1)
switch coordinate2
{
case (0,0):
println("It's at oright!")
case(_,0):
println("(\(coordinate2.0),0) is on the x-axis.")
case (0,_):
println("(0,\(coordinate2.1)) is on the y-axis.")
//判断区间
case (-2...2,-2...2):
println("(\(coordinate2.0),\(coordinate2.1)) is near the origin.")
default:
println("(\(coordinate2.0),\(coordinate2.1)) is just an ordinary coordinate.")
}
//value binding
let coordinate3 = (2,0)
switch coordinate3
{
case (0,0):
println("It's at origin!")
case (let x,0):
println("The coordinate is on the x-axis.")
println("the x value is \(x)")
case (0, let y):
println("The coordinate is on the y-axis.")
println("the y value is \(y)")
case (let x, let y):
println("The coordinate is (\(x),\(y))")
}
//where条件
let coordinate4 = (3,3)
switch coordinate4
{
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 arditrary point.")
}
let courseInfo = ("3-2","区间运算符")
switch courseInfo
{
case (_,let courseName) where courseName.hasSuffix("运算符"):
println("课程《\(courseName)》是介绍运算符的课程")
default:
println("《\(courseInfo.1)》是其他课程")
}
//where条件
let courseName = ("区间运算符")
switch courseName
{
case let str where str.hasSuffix("运算符"):
println("课程《\(courseName)》是介绍运算符的课程")
default:
println("《\(courseName)》是其他课程")
}
//连续判断 fallthrough,但case不能声明变量
let coordinate5 = (0,0)
switch coordinate5
{
case (0,0):
println("It's origin!")
fallthrough
case (_,0):
println("(\(coordinate5.0),0) is on the x-axis")
fallthrough
case (0,_):
println("(0,\(coordinate5.1)) is on the y-axis")
fallthrough
case (-2...2,-2...2):
println("(\(coordinate5.0),\(coordinate5.1)) is near the orign.")
fallthrough
default:
println("(\(coordinate5.0),\(coordinate5.1)) is just an oridinary coordinate")
}
*/

控制转移和二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//---------------5.3 控制转移和二维数组---------------//
/**
* fallthrough//Swift新引入关键字
* break
* continue
*/
//fallthrough当判断落入一个case后还有机会落入下一个case,但case不能声明变量
let coordinate5 = (0,0)
switch coordinate5
{
case (0,0):
println("It's origin!")
fallthrough
case (_,0):
println("(\(coordinate5.0),0) is on the x-axis")
fallthrough
case (0,_):
println("(0,\(coordinate5.1)) is on the y-axis")
fallthrough
case (-2...2,-2...2):
println("(\(coordinate5.0),\(coordinate5.1)) is near the orign.")
fallthrough
default:
println("(\(coordinate5.0),\(coordinate5.1)) is just an oridinary coordinate")
}
//break,跳出当前循环体而不是跳出花括号{},跳出判断
//素数计算
var primes = [2]
for var i = 3; i <= 100; i += 2
{
var isPrime = true
for aPrime in primes
{
if i%aPrime == 0
{
isPrime = false
break
}
}
if isPrime
{
primes.append(i)
}
}
for aPrime in primes
{
println(aPrime)
}
//寻找非元音字母的个数,break跳出switch
let test_str = "This is a test sentence."
var num = 0;
let str = test_str.lowercaseString
for c in str
{
switch c
{
case "a","e","i","o","u":
break
case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
num++;
//其他字符也跳出
default:
break
}
}
//continue
//单词中非元音字母个数
let str2 = "floccinaucinihilipilification"
var num2 = 0;
for c2 in str2
{
switch c2
{
case "a","e","i","o","u":
continue//继续for循环
default:
num2++
}
}
// break mainloop加上mainloop可以对上一层循环作用
var board = Array< Array<Int> >()//声明二维数组
for a in 0..<10
{
board.append(Array(count: 10, repeatedValue: 0))
}
let randx = Int(arc4random()%10)
let randy = Int(arc4random()%10)
board[randx][randy] = 1
board
var i = 0, j = 0
mainloop:for i = 0; i < 10; i++
{
for j = 0; j < 10; j++
{
if board[i][j] == 1
{
break mainloop
}
}
}
println("board[\(i)][\(j)] = 1")
文章目录
  1. 1. Swift学习-05 循环结构
    1. 1.1. 循环结构
    2. 1.2. 选择结构
    3. 1.3. switch语句的高级用法
    4. 1.4. 控制转移和二维数组