Arithmetic Operators; Assignment and Comparison; Operator Precedence; Unary Operators
So I am coming to the end of the recap of my 'Swift Basics' course. Once I've finished that, I should feel pretty secure with the essentials. The next task after this will be to go over the other courses I have completed. Realistically, by this time next week, I should be ready to continue with learning 'new' information!
Arithmetic Operators
There is some technical mathematical information about operands. We now move on to the four main operations - add, subtract, multiply and divide. The other function that can be used is the remainder operator (% is used). E.g. 9 % 4 would give 1.
Step 2: When value obtained using a remainder operator is 0, this means that the value is a perfect multiple of the divisor. Compare the value of result to 0 using the equality operator and assign the resulting value to a constant named
Note: Only submit your answer after you've completed both steps 1 and 2.
Arithmetic Operators
There is some technical mathematical information about operands. We now move on to the four main operations - add, subtract, multiply and divide. The other function that can be used is the remainder operator (% is used). E.g. 9 % 4 would give 1.
let height = 12
let width = 10
let area = height * width
Simple stuff for using multiplication, in this case.
The next part is dividing the area by 10.764, to have the answer in metres. However, this is a double. The most logical thing to do is changing height to 12.0 and width to 10.0. Or, you can declare height and width as doubles, using the syntax of the colon then Double.
Assignment and Comparison
I think the idea here will be to show the difference in using = to assign a value to variable/constant, whereas == checks if something is equal to something or not.
let string1 = "Hello!"
let string2 = "Hello!"
let string3 = "hello"
string1 == string2
This is a basic example of linking Booleans to the == comparison feature. True will be returned in the example above as String1 and String2 are exactly the same. The next part is about the equal to, not equal to, greater than, less than etc. etc.
Challenge:
In the editor below, you have two constants -
value
and divisor
. Step 1: Using the remainder operator, compute the remainder given the value and a divisor. Assign this value to a constant named result.Step 2: When value obtained using a remainder operator is 0, this means that the value is a perfect multiple of the divisor. Compare the value of result to 0 using the equality operator and assign the resulting value to a constant named
isPerfectMultiple
.Note: Only submit your answer after you've completed both steps 1 and 2.
The first step is easy! That's just using the % operator, which I've done no problems. The second one should be fine too....Nailed it! Initially, I forgot to include the assignment and comparison operators together. Makes sense, all good!
Operator Precedence
This is BODMAS! Though I'm sure with Swift that the order is slightly different....
The order of preference/precedence is multiplication, division and modular THEN addition and subtraction. So a bit different to BODMAS/BIDMAS. According to Pasan, it's not totally necessary to know this off by heart. It could come in handy. All this is fine and logical for me, as I'm quite mathematically minded. No problems!
Unary Operators
var levelScore = 0
levelScore += 1
The above is known as the unary plus operator. Using += 1 is much more elegant than writing levelScore = levelScore + 1. It's the same with -= 1. The other one is to see if something is not equal: !=.
And that's the end of this particular course! Apparently that was 149 minutes, so that was 2 and a half hours, just on the videos, plus a bit more the quizzes and challenges. It felt great to remind myself of how much I do know - very rewarding! The last few videos were all about types and operators. These are two areas that I feel pretty confident with now so am not going to do more work on the basics; the next set of videos I'm going over will be Pasan's course on Swift Collections and Control Flow. This is 192 minutes in total, so realistically, that will be my target for the coming week. Basically, by this time next week, I want to have completed that. Then there's another course, Functions (only 80 minutes), then Object-Oriented Swift (136 minutes) - then I am up to date! So perhaps, two weeks from now rather than one is the most realistic target for recapping everything from Pasan's courses. Good stuff!
Comments
Post a Comment