-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_4_Loop_Loop_And_Loop!.swift
135 lines (94 loc) · 2.77 KB
/
Day_4_Loop_Loop_And_Loop!.swift
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Day 4 : Learning loops!
- For Loop
- While Loop
- Repeat Loop
- Exiting Loop
- Exiting Multiple Loop
- Skipping Item
- Infinite Loop
*/
// ------------------------------ For Loop ------------------------------
// Here's a range of a number
let count = 1...10
for number in count {
print("Number is \(number)")
}
let albums = ["Red", "1989", "Reputation"]
for album in albums {
print("\(album) is on Apple Music")
}
// If you don't usa a constant that for loops give you, consider using underscore " _ "
for _ in 1...5{
print("play")
} // this will print "play" for 5 times
for _ in 1..<5{
print("play")
} // this will print "play" 4 times
// ------------------------------ While Loop ------------------------------
// it will run until the condition turns false
var number = 1
while number <= 20 {
print(number)
number += 1
}
// ------------------------------ Repeat Loop ------------------------------
// This is not commonly used, repeat while
var number1 = 1
repeat {
print(number1)
number1 += 1
} while number1 <= 20
// It looks like while loop?? Exactly, they are the same
while false {
print("Hello")
}
// ^^^ Both syntax are the same vvv
repeat {
print("Hello")
} while false
// ------------------------------ Exiting Loop ------------------------------
var countDown = 10
while countDown >= 0 {
print(countDown)
countDown -= 1
if countDown == 4 {
break // go out of the while loop immediately
print("I can't wait")
}
}
print("Happy New Year!!!!")
// ------------------------------ Exiting Multiple Loop ------------------------------
// If you put loop inside a loop it is called : Nested Loop
for i in 1...10{
for j in 1...10{
let product = i * j
print("The product of \(i) and \(j) is \(product)")
}
}
// If you want to exit part-way through we need to do two things
// 1 ) Give outside loop a label " outerloop : "
// 2 ) Add out condition inside the inner loop, then use break outerloop to exit loop at the same time
outerLoop : for m in 1...10 {
for n in 1...10{
let product = m * n
print("The product of \(m) and \(n) is \(product)")
if product == 50 {
print("It is a bullseye!")
break outerLoop
}
}
}
// ------------------------------ Skipping items ------------------------------
// From the previous topic, we got break
// FOr this topic, we have " continue "
for q in 1...10 {
if q % 2 == 1{
continue // move on the the next condition
}
print(q)
}
// ------------------------------- Infinite loop -------------------------------
while true {
print("Hello, world!")
} // This infinite loop will run forever