For Loop


Repeat strings in an Array

if we have an array of strings, we can print each string out like this

import SwiftUI

let platforms = ["iOS", "macOS", "watchOS", "tvOS"]

for os in platforms {
    print("Swift works great on \(os).")
}
Swift works great on iOS.
Swift works great on macOS.
Swift works great on watchOS.
Swift works great on tvOS.

Loop over a fixed range of numbers.

for i in 1...12 {
    print("5 x \(i) = \(i * 5)")
}
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60

Nested Loops, we can also put loops inside loops.

for i in 1...3 {
    print("The \(i) times table:")
    
    for j in 1...5 {
    print(" \(i) x \(j) = \(i * j)")
    }
    
    print() //this helps break up our output so it looks nicer on the secreen. just add a new line.
}
The 1 times table:
 1 x 1 = 1
 1 x 2 = 2
 1 x 3 = 3
 1 x 4 = 4
 1 x 5 = 5

The 2 times table:
 2 x 1 = 2
 2 x 2 = 4
 2 x 3 = 6
 2 x 4 = 8
 2 x 5 = 10

The 3 times table:
 3 x 1 = 3
 3 x 2 = 6
 3 x 3 = 9
 3 x 4 = 12
 3 x 5 = 15

..<

this is a type of range that counts up to but excluding the final number. “..<” 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. we’ll see differences between “1…5” and “1..<5”

for i in 1...5 {
    print("Counting from 1 through 5: \(i)")
}

for i in 1..<5 {
    print("counting from 1 upto 5: \(i)")
}
Counting from 1 through 5: 1
Counting from 1 through 5: 2
Counting from 1 through 5: 3
Counting from 1 through 5: 4
Counting from 1 through 5: 5
counting from 1 upto 5: 1
counting from 1 upto 5: 2
counting from 1 upto 5: 3
counting from 1 upto 5: 4

_

when we don’t actually want the loop variable, we can replace it with and underscore_.

var lyric = "Haters gonna"
for _ in 1...3 {
    lyric += " hate,"
}
print(lyric)
Haters gonna hate, hate, hate,

we can use it in this way too.

let count = 1...3
for _ in count {
    print("There's no place like home.")
}
There's no place like home.
There's no place like home.
There's no place like home.


While loop

While loop will continually execute the loop body until the condition is false.

var countdown = 10

while countdown > 0 {
    print("\(countdown)...")
    countdown -= 1
}

print("Blast off!")
10...
9...
8...
7...
6...
5...
4...
3...
2...
1...
Blast off!

Random. Create a new random interger or decimal.

let id = Int.random(in: 1...10000)
print(id)

let amount = Double.random(in: 0...5)
print(amount)
3076
3.406257562896402

While loop with random functionality.

Let’s roll some virtual 6 sided dice again and again, ending the loop only when a 5 is rolled.

var roll = 0 create an integer to store our roll

while roll != 5 {
    roll = Int.random(in: 1...6)
    print("I rolled a \(roll)")
}
print("Finally 5!")
I rolled a 2
I rolled a 3
I rolled a 4
I rolled a 5
Finally 5!

loops that prints 5 lines of text.

var counter = 2
while counter < 64 {
    print("\(counter) is a 2")
    counter *= 2
}
2 is a 2
4 is a 2
8 is a 2
16 is a 2
32 is a 2
var page: Int = 0
while page < 5 {
    page += 1
    print("I'm reading page \(page).")
}
I'm reading page 1.
I'm reading page 2.
I'm reading page 3.
I'm reading page 4.
I'm reading page 5.
var cats: Int = 0
while cats < 10 {
    cats += 1
    print("I'm getting another cat.")
    if cats == 4 {
        print("Enough cats.")
        cats = 10
    }
}
I'm getting another cat.
I'm getting another cat.
I'm getting another cat.
I'm getting another cat.
Enough cats.


Continue, Break. How to Skip loop items

Continue,if we call “continue” inside the loop body, Swift will immediately stop executing the current loop iteration and jump to the next item in the loop, where it will carry on as normal.

let filenames = ["my photo.jpg", "Work.txt", "someone.jpg", "logo.psd"]

for filename in filenames {
    if filename.hasSuffix(".jpg") == false {
        continue
    }
    print("Found Picture: \(filename)")
}
Found Picture: my photo.jpg
Found Picture: someone.jpg

Break, that exits a loop immediately and skips all remaining iterations.

let number1 = 4
let number2 = 14
var multiples = [Int]()

for i in 1...100000 {
    if i.isMultiple(of: number1) && i.isMultiple(of: number2) {
        multiples.append(i)
        
        if multiples.count == 10 {
            break
        }
    }
}
print(multiples)
[28, 56, 84, 112, 140, 168, 196, 224, 252, 280]