Collections and Control Flow - Loops
The momentum continues! It's been really rewarding being so productive and regular with my Swift work. Something I did yesterday - which I am wont to do - was purchase a course from Udemy. I've done this many times before - been naive enough to believe that this is the solution to my problems: a new course! I've counted around 24 courses I've purchased - pretty much all of which are incomplete or barely started! So, the new one I bought because it was only £10 and has a LOT of content on it. I will switch to this, and do some of the others I've bought before, when the time is right to move on from Treehouse...
For In Loops
These can be VERY difficult to understand. For now, Pasan explains that a loop will happen until a condition is met. There are many real life examples of this.
A loop is something that does something over and over until it has done so for each item in a group.
On a sidenote, I've for the first time realised how you add another page to a Playground file - it's actually really easy! All you need to do is bring up the left hand pane, then click on the + button! Easy!
Interestingly, rather than start with a simple for in loop with numbers, Pasan links together what we learned about arrays. I've used the same 'toDo' array from before. The print function is next mentioned.
For In Loops
These can be VERY difficult to understand. For now, Pasan explains that a loop will happen until a condition is met. There are many real life examples of this.
A loop is something that does something over and over until it has done so for each item in a group.
On a sidenote, I've for the first time realised how you add another page to a Playground file - it's actually really easy! All you need to do is bring up the left hand pane, then click on the + button! Easy!
Interestingly, rather than start with a simple for in loop with numbers, Pasan links together what we learned about arrays. I've used the same 'toDo' array from before. The print function is next mentioned.
let firstItem = toDo[0]
print(toDo[1])
Either a new constant/variable can be created, then printed, or straight to print. The point is that this is cumbersome and long-winded; a for-in loop is going to be used!
for name in toDo {
print(name)
}
This is skipping ahead, but basically the for-in loop iterates over each item in the array and will print each one. The 'name' could be called anything; it could be 'task', 'item' etc.
Closed Range Operator
I think this is now going to move onto numbers. It is to do with the range of the numbers (obviously!).
a...b - this represents the starting and finishing numbers in the range e.g. 1...7: 1 through to 7 etc.
A half open range goes up to b - 1: a..<b - that's the syntax for it.
They can be used in conjunction with a for-in loop.
for number in 1...10 {
print("\(number) times 5 is equal to \(number * 5)")
}
This needed some string interpolation! But it makes sense. Pasan makes the point of when would you use the half open range in real life? Surely the closed range makes the most logical sense?
var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier*6)
}
This is the solution to the challenge of creating a for-in loop for the first 10 multiples of 6 - not that easy but I got it!
While and Repeat While
The while loop is different to the for-in. It performs a set of statements until the condition becomes false.
var x = 0
while x <= 20 {
print(x)
x += 1
}
Xcode does not like this at all! Possibly a new version of Swift required - different keywords/syntax needed? Forget that - closed and reopened Xcode - now it's working!
Next is the Repeat While loop. The syntax of this is a bit different.
var counter = 1
while counter < 1 {
print("I'm inside this while lopp!)
counter += 1
}
And Repeat While:
repeat {
print("I'm in this repeat loop!")
counter += 1
} while counter < 1
The challenge at the end was rather baffling! It was basically to use the while loop for number in an array, involving a count and sum etc. I think I get the principle of 'While' loops - they "perform a set of statements until a condition becomes false".
So, basically, you need to have created a variable first of all e.g. points = 5. Then...see below!
var points = 5
while points < 10 {
print(points)
points += 1
}
Easier as above to show the code! So, then the keyword while is used, with a condition - in this case, points being less than 10. Then points is printed/whatever variable is added to. I could do a sum variable for this. Anyway, after that, the increment/change occurs - points += 1. So printed is: 5, 6, 7, 8, 9.
That makes more sense, but loops were always something which I thought I got, then got confused by when I had to do various other things with - the premise of the condition and statements I get. I will leave that one there; next time it will be 'if statements!'.
Comments
Post a Comment