SwiftUI_visionOS_Note

Loop, While & Skip

Jan 5, 2024

Loop

 

We can repeat and fix in array, dictionary, or set

Heres an array of string, and print each string like following code

We call the value of os from platforms.

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

1...12 is "range"

 
 

we can put loops inside the loops, "nested loops"

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
  • When countdown is not bigger than 0, it executes the next function “Blast off”
 
 
Callout icon'
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.
 
  • We can create 20 sided-dice to execute the code when it hits 20
  1. create an integer to store the value
 
  1. loop until dice hits 20
    1. when roll is not equal to 20, we roll the dice, and shout out the number
 
  1. Say “Critical hit” when it hits 20
 
 

Skip

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

 
  1. Create an array of strings with different file types
 
  1. Create a loop
 
  1. when filename doesn’t end with jpg, skip
 
  1. and print the founded pictures
 
 
 
 

As for break, that exits a loop immediately and skips all remaining iterations.

 
 
  • 이거 해석해보자
 
 

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.
 

You might also like

BlogPro logo
Made with BlogPro