DEV Community

Odewole Kehinde
Odewole Kehinde

Posted on • Updated on

Day 4 of 100 Days of SwiftUI


This is the fourth day of learning SwiftUI using Paul Hudson's 100 Days of SwiftUI challenge.

Today's lessons were pretty straightforward. It's all about loops. Loops are used to tell computers to repeat a task multiple times. Examples of loops include - for loop, while loop, repeat loop.


  • For loops
let count = 1...10

// for loop
for number in count {
    // to skip an item and continue to the next one, you'll use the keyword continue
    if number == 3 {
    if number == 6 {
        print("Human, I'm done counting!")
        // to exit a loop, use the break keyword
    print ("Number is now \(number)")

// If you don’t use the constant that for loops give you (as in number in the code above), you should use an underscore instead so that Swift doesn’t create needless values:
for _ in count {
let artists = ["Don Moen", "M W Smith", "Sinach", "Jamie Grace"]

for artist in artists {
    print("You're currently listening to: \(artist)")
  • While loop
// while loop
var age = 1

while age < 25 {
    print("You can't manage the estate yet :(")
    age += 1

print("Now you are well able!")
  • Repeat loop - same as while loop except that the condition comes at the end.
var loginAttempt = 1

// the code will at least run once before the condition is checked
repeat {
    print("Enter your login details")
    loginAttempt += 1
} while (loginAttempt <= 3)

print("Correct details or not, you're now logged in")

  • Nested loops operators - Tthis is when you put a loop inside a loop.
// This is a nested loop
// to break out of two loops simultaneously, we need to add a label to the outside loop, and then use break {name_of_outside_loop label} in the inner loop
outerLoop: for x in 1...5 {
    for y in 1...5 {
        let multiply = y * x
        print("x * y is \(multiply)")

        if multiply == 20 {
            print("We're done here!")
            break outerLoop
  • Infinite loop - This means that your loop will repeat forever. All apps on your iPhone use infinite loops, because they start running, then continually watch for events until you choose to quit them. Please always make sure you have a check that exits your loop, otherwise, it will never end. They are made using "while true".
var counter = 0

// Infinite loop
while true {
    print ("I'll print forever, I'll even crash your system!👹")
    counter += 1

    if counter == 5 {
        print("Yo!, I won't let you👼🏿")

Thanks for reading🙏🏿.

You can find me on Twitter @RealKennyEdward

I'm on to Day5!

Top comments (0)