Day 4 of 100 Days of SwiftUI
Tags: swift, swiftui, beginners
Published: 2020-01-18 22:32:27

#Introduction

This is the fourth day of learning [SwiftUI](https://developer.apple.com/xcode/swiftui/) using [Paul Hudson](https://twitter.com/twostraws)'s [100 Days of SwiftUI challenge](https://www.hackingwithswift.com/100/swiftui). 

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.

#Summary
<ul>
  <li>
    <strong id ="loop">For loops</strong>
  </li>
</ul>
``` swift
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 {
        continue
    }
    if number == 6 {
        print("Human, I'm done counting!")
        // to exit a loop, use the break keyword
        break
    }
    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 {
    print("Number")
}
let artists = ["Don Moen", "M W Smith", "Sinach", "Jamie Grace"]

for artist in artists {
    print("You're currently listening to: \(artist)")
}
```

<ul>
  <li>
    <strong id ="while">While loop</strong>
  </li>
</ul>
``` swift
// while loop
var age = 1

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

print("Now you are well able!")
```

<ul>
  <li>
    <strong id ="">Repeat loop</strong> - same as while loop except that the condition comes at the end.
  </li>
</ul>
``` swift
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")

```
<ul>
  <li>
    <strong id ="">Nested loops operators</strong> - Tthis is when you put a loop inside a loop.
  </li>
</ul>
``` swift
// 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
        }
    }
}
```

<ul>
  <li>
    <strong id ="">Infinite loop</strong> - 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 <strong>"while true"</strong>.</li>
</ul>
``` swift
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👼🏿")
        break
    }
}
```

Thanks for reading🙏🏿.

You can find me on Twitter [@RealKennyEdward](https://twitter.com/RealKennyEdward)

I'm on to Day5!