Loop
We can repeat and fix in array, dictionary, or set
Heres an array of string, and print each string like following code
let platforms = ["iOS", "macOS", "tvOS", "watchOS"]
We call the value of os from platforms.
for os in platforms { print("Swift works great on \(os).") }
it will print 4 lines of text
- code inside the braces is "loop body"
- one cycle through the loop body is "loop iteration" -> this one has 4 loop iteractions
- os is "loop variable", it only exists inside the loop body and change to a new value
we can loop over the fixed amount of number
for i in 1...12 {print("5 x \(i) is \(5 * i)")}
1...12 is "range"
we can put loops inside the loops, "nested loops"
for i in 1...12 {
print ("The \(i) times table")
f or j in 1...12{
print(" \(j) x \(i) is \(j * i)")
}
print()
}
print() will just print a new line, it just looks nicer
We can also exclude the last number
for i in 1...5 {
print("Counting from 1 through 5 : \(i)")
}
for i in 1..<5 {
print("Counting from 1 to 5 : \(i)")
}
Tip: ..< is really helpful for working with arrays, where we count from 0 and often want to count up to but excluding the number of items in the array.
instead of looping, we can run a code a certain number of times.
by replacing i with an underscore.
var lyric = "Haters gonna"
for _ in 1...5 {
lyric += " hate"
}
print(lyric)
While
Swift has a second kind of loop called while
- provide it with a condition, and it will execute until the condition is false
- initial integer is 10
- while countdown is bigger than 0, it will execute the following functionswift
whilte countdown > 0 { print("\countdown)...") countdown -= 1 }
- When countdown is not bigger than 0, it executes the next function “Blast off”
var countdown = 10
while countdown > 0 {
print("\(countdown)...")
countdown -= 1
}
print("Blast off")
Int
and Double
both have: random(in:)
- We can give the range of numbers to work with.
let id = Int.random(in: 1...1000)
let amount = Double.random(in: 0...1)
- We can create 20 sided-dice to execute the code when it hits 20
- create an integer to store the value
var roll = 0
- loop until dice hits 20
- when roll is not equal to 20, we roll the dice, and shout out the number
while roll != 20 {...}
roll = Int.random(in: 1...20)
print("I rolled \(roll)")
- Say “Critical hit” when it hits 20
print("Critical Hit!")
Skip
continue
skips the current loop iteration, and break
skips all remaining iterations
- Create an array of strings with different file types
let filenames = ["me.jpg", "work.txt", "sophie.jpg"]
- Create a loop
for filename in filenames {...}
- when filename doesn’t end with jpg, skip
if filename.hasSuffix(".jpg") == false { continue}
- and print the founded pictures
print("Found picture: \(filename)")
As for break
, that exits a loop immediately and skips all remaining iterations.
- 이거 해석해보자
let number1 = 4
let number2 = 11
var multiple = [Int]()
for i in 1...100_000{
if i.isMultiple(of: number1) && i.isMultiple(of: number2){
multiple.append(i)
if multiple.count == 10 {
break
}
}
}
print(multiple)
That does quite a lot:
- Create two constants to hold two numbers.
- Create an integer array variable that will store common multiples of our two numbers.
- Count from 1 through 100,000, assigning each loop variable to
i
.
- If
i
is a multiple of both the first and second numbers, append it to the integer array.
- Once we hit 10 multiples, call
break
to exit the loop.
- Print out the resulting array.