100 Days of SwiftUI Day 6

100 Days of SwiftUI – Day 6 – Loops

We’re already at day 6 of the 100 Days of SwiftUI, meaning we’re nearing the end of our first week. Yesterday, we tackled conditional statements using if/else, the switch statement and the ternary operator. Today, we’re diving into loops!

For loop

A for loop is used to repeat over a block of code. For example, you could loop over an array until you reach a certain value, or the end of an array. Or, you could loop over a range of numbers. By combining loops with the conditional statements, you can create more complex programs.

// go through numbers 1 through 10 and print the numbers.
for i in 1...10 {
    print("The current number is \(i)")
}

// Create an array of F1 drivers and loop through it, printing each name.
let f1 = ["Max Verstappen", "Charles Leclerc", "Checo Pérez"]

for driver in f1 {
    print("\(driver)")
}

// Create an array of files and print check if it's a jpg or not.
let files = ["icon.jpg", "code.txt", "maverick.jpg", "blog.jpg", "forloop.playground"]

for file in files {
    if file.hasSuffix(".jpg") {
        print("\(file) is an image!")
    } else {
        print("\(file) is not an image.")
    }
}

While loop

Where a for loop is usually used to loop over a finite number of items, a while loop can be used to loop over code until a specific condition is met. Want to know when you would use a while loop instead of a for loop? Paul, as always, has got you covered.

// Create a while loop that will continue looping into counter = 0
var counter = 10
while counter != 0 {
    print("\(counter)...")
    counter-=1
}

print("Blast off!")

Using break and continue to skip a loop

Swift has two functions built in to help you skip a loop or break out of it completely. If you want to skip the current iteration of the loop and start over, you can use continue. If you want to completely break out of the loop, you can use a break.

var multiples = [Int]()

for i in 1...100_000 {
    if i.isMultiple(of: number1) && i.isMultiple(of: number2) {
        multiples.append(i)
        
        if multiples.count == 10 {
            break
        }
    }
}

SwiftUI checkpoint 3

Last but not least, we have the third checkpoint! The third checkpoint consists of the FizzBuzz” challenge. This challenge seems to be pretty well known among seasoned developers and instructs you to loop through numbers 1 through 100. Then:

  • If the number is a multiple of 3, print “Fizz”
  • If it’s a multiple of 5, print “Buzz”
  • If it’s a multiple of 3 and 5, print “FizzBuzz”
  • Otherwise, just print the number.

With the knowledge I’ve gained from the past days, I came to a solution pretty quickly. Let me know what you think!

// FizzBuzz!

for i in 1...100 {
    if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
        print("FizzBuzz")
    } else if i.isMultiple(of: 3) {
        print("Fizz")
    } else if i.isMultiple(of: 5) {
        print("Buzz")
    } else {
        print(i)
    }
}

And that’s it for day 6. Tomorrow, we’ll learn about functions, parameters and return values, all of which sound very interesting. It’s onwards and upwards from here!

Darryl

Hi! My name is Darryl and this is my personal blog where I write about my journey as I learn programming! You'll also find articles about other things that interest me including games, tech and anime.

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

100 Days of SwiftUI – Day 38

100 Days of SwiftUI – Day 31

100 Days of SwiftUI – Day 46

100 Days of SwiftUI – Day 85