Chris Ching Course Part 1 (Data types, If/Switch statements, Loops and Functions)
Here we go! Back to Udemy and consolidating with this course from Chris Ching. Spending a couple of weeks on Treehouse has not proved fruitful, though it was interesting to look at properties, initializers and a few other aspects at a higher level. Anyway, coding is one of these things where my understanding needs to be topped up. A bit like going to the gym! Some bits will be intact but I need constant topping up to make it at its optimum.
Start time - 15:02
Data Types
*Just skimming over this!
Different types of data are stored on the data differently. If the system knows it is an int, then it allows different things to a string, double etc.
Type inference - he hasn't mentioned this but there is no need to specify the type. Xcode figures that out.
Converting types -
var number = 4
number = String(4)
var/let - know all about these.
Rounding function. Use the round() with the variable in the brackets.
If Statements
*Again, skimming through!
Something I need to remember with if statements is that they DON'T have to be exhaustive! Switch statements do.
Else if - second/third/up to penultimate condition
Else - final condition - the default.
Something else to remember is that several conditions can be true. It will check each condition at a time - if false, will check the next condition and will stop checking when a true condition is found.
All of the == && || != I know all of this very well!
Switch Statements
*Skipping away!
Yes, no surprises here. Switch is elegant and clean and less verbose than if statements.
You can do multiple values for each case - using a comma.
E.g.
var character = "a"
switch character {
case "a", "b", "c":
This is easier than doing an individual case for every single option - could come in handy!
Loops
Now this is what I REALLY wanted to go over. Using loops and applying them is something I've found tricky. Gonna start from the ground up!
For in loops -
This is just me practising. Both very simple.
Use the _ if you don't have a counter and were just printing a string without the counter.
Scope is importnat - you can only access within the scope o the curly braces.
While Loop -
I've never really seen the point of repeat while loops. So the while loop checks the condition first. Whereas the repeat while loop does the code then checks the condition. What does that result in?
OK, so some clarification from the Swift documentation explains that the repeat loop will execute the code ONCE, then consider the condition. Ah that makes more sense!
Paused at 15:43 (41 minutes for the session)
OK some useful stuff here. I've refreshed my understanding of loops and realised that I'm very hot on if/switch statements! Going to pop out but continue shortly!
Started again at 16:02
Functions
So functions carry out a particular task. I know with this that there are a lot of variations with the parameters, what is returned etc. etc.
This is a way of creating a function with the print statement. A, b and c are local variables- they cannot be accessed outside of the scope of this function.
Start time - 15:02
Data Types
*Just skimming over this!
Different types of data are stored on the data differently. If the system knows it is an int, then it allows different things to a string, double etc.
Type inference - he hasn't mentioned this but there is no need to specify the type. Xcode figures that out.
Converting types -
var number = 4
number = String(4)
var/let - know all about these.
Rounding function. Use the round() with the variable in the brackets.
If Statements
*Again, skimming through!
Something I need to remember with if statements is that they DON'T have to be exhaustive! Switch statements do.
Else if - second/third/up to penultimate condition
Else - final condition - the default.
Something else to remember is that several conditions can be true. It will check each condition at a time - if false, will check the next condition and will stop checking when a true condition is found.
All of the == && || != I know all of this very well!
Switch Statements
*Skipping away!
Yes, no surprises here. Switch is elegant and clean and less verbose than if statements.
You can do multiple values for each case - using a comma.
E.g.
var character = "a"
switch character {
case "a", "b", "c":
This is easier than doing an individual case for every single option - could come in handy!
Loops
Now this is what I REALLY wanted to go over. Using loops and applying them is something I've found tricky. Gonna start from the ground up!
For in loops -
for i in 1...5 {
print(i)
}
for i in 1...5 {
print("\(i) times champion!")
}
This is just me practising. Both very simple.
Use the _ if you don't have a counter and were just printing a string without the counter.
Scope is importnat - you can only access within the scope o the curly braces.
While Loop -
var counter = 10
while counter > 1 {
print("Hello!")
counter -= 1
}
Right so breaking this down. You start with the while condition, then you have some code executed. The -= means that the counter value is going down each time. So "Hello" is printed each time, until the value of counter gets down to 1.
Repeat - While loop
repeat {
print ("Hello!")
counter -= 1
} while counter > 1
I've never really seen the point of repeat while loops. So the while loop checks the condition first. Whereas the repeat while loop does the code then checks the condition. What does that result in?
OK, so some clarification from the Swift documentation explains that the repeat loop will execute the code ONCE, then consider the condition. Ah that makes more sense!
Paused at 15:43 (41 minutes for the session)
OK some useful stuff here. I've refreshed my understanding of loops and realised that I'm very hot on if/switch statements! Going to pop out but continue shortly!
Started again at 16:02
Functions
So functions carry out a particular task. I know with this that there are a lot of variations with the parameters, what is returned etc. etc.
func addTwoNumbers() {
}
func addTwoNumbers() {
let a = 1
let b = 2
let c = a + b
print(c)
}
This is a way of creating a function with the print statement. A, b and c are local variables- they cannot be accessed outside of the scope of this function.
func addTwoNumbers(a: Int, b: Int) {
let c = a + b
print(c)
}
This would be step up and I would have two parameters to input - a and b. Even better I can do one without the print statement...
func addTwoNumbers(a: Int, b: Int) -> Int {
return a + b
}
So what Chris has done is on a more basic level and it's good to know that I'm ahead of this.
Methods/Functions - these are the same thing!
Assigning values of functions to a var/let:
let sum = addTwoNumbers(a: 4, b: 7)
So now we can directly access sum!
Argument labels and parameter names - if these are different for the code and when calling the function, then the argument label is on the left, then it's the parameter name.
OK, all of this is very familiar. All good!
Argument labels are used because it makes it more of a sentence - easier code to read and understand.
Likewise, you don't want parameter names to be vague.
Just before I finish this, I remember that there is a way to have multiple parameters - an unspecified number...
With a LITTLE bit of help from stack overflow - couldn't remember all of the syntax...
func sumOfNumbers(numbers: Int...) -> Int{
var sum = 0
for number in numbers {
sum += number
}
return sum
}
That is pretty powerful!
Stopped. Time: 16:26 (24 minutes this session)
1 hour and 5 minutes total
So no major breakthroughs. More consolidation which is what I needed. The loops make more sense in terms of the syntax and the differences between them. It was good to see the build up of complexity in functions. I know that these are all powerful features of Swift so it was good to go over them. Next time it's going to be: Classes, UIKit, Inits, Optionals, Properties, Desig/conven inits, arrays and dictionaries. I may have to split those up into a couple of entries. THEN I'm going to attempt the two challenges that looked a bit much last time. At that point, I want to crack on with a course which looks practical and useful. More tomorrow!
Comments
Post a Comment