Ray Wenderlich Course Part 8 (up to lesson 70)

Second entry today - woohoo! Cracking on with Ray's guide to all the key language features of Swift. First up will be control flow for 8 lessons then 9 for functions and optionals. I want to get at least all of those done in this entry. Hopefully there will be some more challenges as these undoubtedly help me understand the information at a greater depth. I'm going to be going through the main info at a quicker speed: 1.5 x again. Ok here we go!

While Loops

var i = 1

while i < 10 {
    
    print(i)
    
    i += 1

}

In this example, numbers from 1 to 9 will be printed. 

So the difference with repeat loops is that it executes the code the first time, no matter what...

repeat {
    print(i)
    i += 1

} while i < 10 

Ray says that the first one is used generally more. 

repeat {
    print(i)
    if i == 5 {
        break
    }
    i += 1
} while i < 10

The break statement means that it will break out of the loop after 5 - early in this case. 


/*:
 ### WHILE LOOPS
 Create a variable named `counter` and set it equal to 0. Create a `while` loop with the condition `counter < 10` which prints out `counter` is `X` (where `X` is replaced with counter value) and then increments `counter` by 1.
 */

// TODO: Write solution here

var counter = 0

while counter < 10 {
    
    print("counter is \(counter)")
    
    counter += 1
}


/*:
 Create a variable named counter and set it equal to 0. Create another variable named roll and set it equal to 0. Create a repeat-while loop. Inside the loop, set roll equal to Int(arc4random_uniform(6)) which means to pick a random number between 0 and 5. Then increment counter by 1. Finally, print After X rolls, roll is Y where X is the value of counter and Y is the value of roll. Set the loop condition such that the loop finishes when the first 0 is rolled.
 */

// TODO: Write solution here

var counter = 0

var roll = 0

repeat {
    
    roll = Int(arc4random_uniform(6))
    
    counter += 1
    
    print("After \(counter) rolls, roll is \(roll)")
    
} while roll != 0

Cool! I made ONE error. The last line, I put while roll == 0 initially, as I thought it was when the first 0 is rolled as the condition, but it's actually while it is NOT equal to 0, so the not equal operator was needed! Good challenges Ray, bravo!

For Loops

Closed and half open range - I know these already. Closed is ... whereas half open means the less than symbol is used before the new number...

CLOSED: 2...5
HALF-OPEN: 2..<5

var sum = 0
var count = 10

for i in 1...count {
    
    sum += i
}

for _ in 0..<count {
    
    print("Hodor!")
    
}

*Testing odd numbers - I can do this without Ray's example...

for i in 1...count {
    if i % 2 == 0 {
        print("\(i) is even")
    } else {
        print("\(i) is odd")
    }
}

for i in 1...count where i % 2 == 0 {
    print("\(i) is even")
} else {
    print("\(i) is odd")
}

OK...using Ray's 'where' keyword makes it slightly more succinct!

The 'continue' keyword means that you will continue a condition until a certain point - not totally sure about this. Ray's last example is very complicated too - am skimming over this for now!

/*:
 ### FOR LOOPS

Create a constant called range and set it equal to a range starting at 1 and ending with 10 inclusive. Write a for loop which iterates over this range and prints the square of each number.
 */

// TODO: Write solution here

let range = 1...10

for i in range {
    print(i * i)
}

For the above, Ray did an extra constant for 'square' which does admittedly make more sense. 

for i in range {
    
    let squareRoot = sqrt(Double(i))
    
    print(squareRoot)
}

OK I've used a LITTLE bit of help to create the extra constant - a pattern emerging!

Switch Statements

let number = 10

switch number {
case 1...2:
    print("Low!")
case 8...10:
    print("High!")
default:
    print("Not low nor high!")
}

You can do the same with strings (not going to do an example!). 

You can also use the where keyword again:

*This will probably not come out but good to see for now!

Ray's next example is FAR too complex! He has a habit for this!!

Challenge - Switch!

enum LifeStage {
    case Infant
    case Child
    case Teenager
    case Adult
    case Middleaged
    case Elderly
}

var age = 44

switch age {
    
case 0...2: print(LifeStage.Infant)
case 3...12: print(LifeStage.Child)
case 13...19: print(LifeStage.Teenager)
case 20...39: print(LifeStage.Adult)
case 40...60: print(LifeStage.Middleaged)
case 61...100: print(LifeStage.Elderly)
default: print("Not a valid age!")Sy
}

So I actually created the enum first (something beyond this part of the Ray's course), just so I wouldn't make an error with the strings and it made sense to group them first!

*Something really useful here - rather than the 61...100 (I was assuming that would be the max age) I can use this - 

case _ where age >= 61 

*This means that anything equal to or greater than 61 can be taken account for. Neat!

OK, second challenge wasn't THAT hard but it's something I don't think is totally necessary for now at least!

And that's it! Lots of good stuff here and worth recapping! Some key takeaways:
  • Syntax for while/repeat loops
  • Use of the 'where' keyword to simplify e.g. when using if statement for a value
  • Break keyword (not too sure about 'continue')
  • Using _ and where syntax for a comparison in switch statements
  • Use of sqrt and how constants/variables are needed to convert to the Int
  • Use a constant to store a value rather than putting the alogrithim in the print statement

I'm not going to look at the next bit after all. That's enough for now! Next time it will be functions and optionals...



Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)