Operators and Conditions

Today was day 3 of the 100 Days of Swift challenge, which meant operators and conditions. Like the past few days, I expected to already know almost everything, but I was delightfully surprised by a few new things.

First of all, I was familiar with using range in for loops, but not in switch statements. This adds to the flexibility of pattern matching in switch statements that I learned about yesterday. It might seem obvious but I’d also never seen ranges as named variables, either.

let score = 85
switch score {
case 0..<50:
    print("You did poorly.")
case 50..<85:
    print("You did ok.")
    print("You did well.")
let passingRange = 70...100

The other thing that was new to me was the fallthrough statement. Unlike most C-base languages, Swift case statements don’t automatically fall through to the next case if there isn’t a break. If you want the C-style behavior, you can get it by adding fallthrough to your case block:

switch weather {
case "fog":
    print("Wear layers, it might be cold later")
case "rain":
    print("Stay home")
case "sunny":
    print("Must be fall")
    print("Enjoy your day!")

It was Oscar’s birthday today so I didn’t have time to work on anything after first 30 minutes. Tomorrow I plan to make up for it by working for an extra 30 minutes on MFArtist.