SwiftUI_visionOS_Note

Loop, While & Skip

Jan 5, 2024
3 min read|

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"]
swift

We call the value of os from platforms.

for  os  in  platforms { print("Swift works great on \(os).") }
swift

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)")}
swift

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()

}
swift

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 function
    whilte countdown > 0 {
    	print("\countdown)...")
    	countdown -= 1
    }
    swift
  • 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")
swift

It’s useful when we don’t know how many times the loop will go

Int and Double both have: random(in:)

  • We can give the range of numbers to work with.
let id = Int.random(in: 1...1000)
swift
let amount = Double.random(in: 0...1)
swift

  • We can create 20 sided-dice to execute the code when it hits 20
  1. create an integer to store the value
var roll = 0
swift

  1. loop until dice hits 20
    1. when roll is not equal to 20, we roll the dice, and shout out the number
while roll != 20 {...}
swift
roll = Int.random(in: 1...20)
print("I rolled \(roll)")
swift

  1. Say “Critical hit” when it hits 20
print("Critical Hit!")
swift

Skip

continue skips the current loop iteration, and break skips all remaining iterations

  1. Create an array of strings with different file types
let filenames = ["me.jpg", "work.txt", "sophie.jpg"]
swift

  1. Create a loop
for filename in filenames {...}
swift

  1. when filename doesn’t end with jpg, skip
if filename.hasSuffix(".jpg") == false { continue}
swift

  1. and print the founded pictures
print("Found picture: \(filename)")
swift

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)
swift

That does quite a lot:

  1. Create two constants to hold two numbers.
  1. Create an integer array variable that will store common multiples of our two numbers.
  1. Count from 1 through 100,000, assigning each loop variable to i.
  1. If i is a multiple of both the first and second numbers, append it to the integer array.
  1. Once we hit 10 multiples, call break to exit the loop.
  1. Print out the resulting array.

Subscribe to our newsletter

Get the latest news and updates from our team