100 Days of SwiftUI

Day 6

2026-04-23

Loops

For

  • for loop on a number range uses 3 dots (other languages I’ve seen use 2 dots), e.g. 1...23 for the range from 1 to 23 inclusive to get 23 iterations
  • print() creates a linebreak
  • to make the range exclude the last number, use 1..<23 — “1 up to 23” This makes 22 iterations
  • using _ as the loop variable is… confusing. I suppose if I used i or oop it would be harder later to see where i or oop is used in the loop body. (He discusses this in the Optional reading)

While

  • it’s interesting that he says it’s not as useful as a for loop. Ah I misunderstood.

for loops are more common when you have a finite amount of data to go through, such as a range or an array, but while loops are really helpful when you need a custom condition.”

  • what’s this with Random numbers?? oh, he’s using it as the demonstration for while

Skipping Loops

  • continue — stop what you’re doing and move on to the next iteration
  • break — quit the loop altogether
  • in the Optional reading he mentions “labeled statements” which point to particular parts of code, “most commonly used for breaking out of nested loops.”

Quizzes

  • For loops — 10/12 — counting and syntax
  • While loops — 9/12 — counting and syntax I don’t think of 0 as being an even number or a multiple of 2
  • Exiting loops — 12/12
I sense a trend

Checkpoint 3

fizz buzz – loop from 1 through 100
If it’s 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.

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

I considered using switch but I couldn’t quite figure out the cases