A What's up everybody.
Mark Price at Dev slopes dot com plugging our way through foundational swift and programming and not
only teach India SWIP but I'm teaching the underlying core programming principles that you use with
you in any programming language.
So this is really good stuff.
Take it in here.
Go ahead and open up your X code 8 and we're going to get started with a playground.
And this one we're going to call bulls not to be confused with like a bull with horns bulls and conditionals
and comparison operators to create a folder for this swift and say boules and comparison's
and create.
So a boolean is true or false.
Simple enough so far.
We kind of done this once before.
Right.
Bar.
Am I Am i the best teacher ever equals.
What's that.
Jennifer says true.
Thanks Jennifer.
OK.
So am I the best teacher ever.
Well thank you.
I wouldn't say that about myself because there will be narcissistic.
So that's a boolean.
It's true.
And you know but it can also be false can't be anything else so.
So am I the best teacher ever.
Well let's say the ExCo just keeps crashing and you're getting sick of watching me have X code crash
and you're just angry.
And so now you want to set this to false because it's not my fault it is crashing.
Don't get on my case don't get on my case.
So am I the best teacher ever.
I'm no longer the best teacher ever because this is up to us and you know I actually do cut out a lot
of the a lot of the crashes but I leave some of them in there so you can know that again real life development
you see in the flesh there.
So am I the best teacher ever is now false because it keeps crashing.
OK and I'm not cutting it out of the video.
OK.
Anyway so true or false.
So what does that mean.
Why does it.
Why does it mean that we are storing a very true or false variable.
Well the funny thing is actually most of the time you're not storing true or false values in a variable.
You will and I do it frequently but not all the time.
Boolean values are sometimes more so used to just compare things or to to do something based on some
condition.
OK.
Right.
Does that make any sense you want to do something based.
Should I do this.
You know if you know when you were a kid remember when you were a kid and your parents were like Yo
if you clean your room and do your chores we're going to go to Disneyland otherwise I'm going to take
this big black belt with these spikes on it.
I'm going to beat you a rough childhood anyway.
Cause and effect or true or false.
Is a foundational principle for that.
And this is again type inference here we're inferring it as a bull but the actual key word if you want
to explicitly declare it is bull.
OK.
Not bullying.
OK.
Not bullying literal type convert what nothing else OK just bull and I'm not going explain what these
things are.
If you're wondering what's the difference between bullying bullying and swift on the part when them
autocomplete comes up.
Go search on Google.
I'm telling you all you need to do is blog.
OK.
So true or false.
And how can you use these things.
Well
to make decisions for you.
So that's it.
That's bull.
Let's put them in action.
Let's actually see how we can use comparisons with.
Well I can say one more thing actually.
Just one more thing.
OK.
Just to blow your mind confuse you completely.
If true equals false or if true equals true print WDF this is what the fish.
By the way.
All right.
What's going on here.
So we're using the double equals sign here.
This is a comparison operator and this is called equal to.
So if true is equal to false.
OK.
If true or false then this whole side of the equation would be true but it's not.
So this whole side of the equation is false so we're saying if false or OK this is the or OPERATOR OK
and or if true equals true.
So if this is true or if this is true then go into here.
OK.
So true is definitely equal to true.
So this is going to return the entire section over here to true.
And so again you can start to see maybe how you can start comparing things in your code.
And this is a great example because you probably wouldn't write this in your code but a lot of coding
tests and brainteasers out there will actually ask you to decide for things like this.
So you're you can they can see how your brain works and see if you can read the code.
This is really good for learning how to read code.
OK.
So true or false.
You know a better example is for instance var has data finished downloading.
You know how when you get an app and there's a spinner bar was explicitly declare it.
So you know it's a boolean.
So basically when you get that spinner bar it's hitting up a server and it's downloading data.
OK.
So you know we've got the spinner and dot dot dot you know.
Little color that was nice.
Got.
That's funny.
Let's just pick a car.
I don't know what it's doing to be honest but I like it.
It's a feature of a feature of the playgrounds anyway.
So we're downloading data from the internet then after it's done downloading we can say has data finished
downloading equals true.
All right.
And then we can do something like no load UI and other features.
So this is an example of when you actually use a ball in your code and in your projects with for things
like this and I've done things like this before it's nothing wrong with it it's a common thing to do.
So it's boolean.
True or false.
It's one way you can use it again anytime anytime you need to get a true false yes or no Bullen's the
way to go for short the bulls go hand in hand or boolean logic goes hand in hand with comparison operators
now can comparison operators will return a boolean.
OK.
When I say and it is bull but bullying is a long term for it with just condense it here.
So for instance this is really easy if one is equal to two print should not see this.
So will never be executed.
Thank you.
It's smart.
So if one equals two.
Well one is not equal to two in the recall.
So again this returns a boolean.
OK.
That's really interesting huh.
I wonder do you think you could do something like that over here.
Actually an in a in a boolean itself.
So what if we said instead of saying false we'll say Well said This has data finished downloading equals
one equals two
interesting.
So like I said this is returning a boolean true or false value that's been stored in here.
So like I was telling you earlier if you use them less here and variables and you use them more in conditional
logic.
OK.
They return them but only return a boolean for the sake of performing.
Making a decision.
OK.
So if one equals to print you should see this if two equals two.
Now we're seeing it.
OK.
So it returns a true or false value conditional conditional logic.
OK.
So let's talk about the different operators here.
All right.
So we've got equal to and it's the equal sign.
We've got not equal to which is the exclamation point and the equal sign not equal to.
All right we've got greater then and this is you know a that's greater than B.
So I say that's greater then.
And I forgot my call and be consistent.
And then we've got greater greater than or equal to which is you know the big Pacman mouth to the left
equal to and then we have less than or equal to
like so.
And then we've got less than two.
OK.
So obviously there's a lot going on here but let's see how it actually works.
Let's see it in action.
OK.
So you know of course the common example here that we've used already is a bank account so we can obviously
do that here again so our bank balance right equals are equals five $400 gave our item to buy equals
$100.
OK.
So then what we can do is we can say if bank balance bank balance is greater than or equal to item to
buy Why wouldn't we just say if it's greater then well the buyer was $400 this would turn out to be
false and we would be able to buy it.
That's not right.
We need to be able to go to zero dollars in our account but not into the negative unless you turn unless
you put an overdraft protection you know which.
Why did people do that.
No frigging clue.
Just what I wanted to do.
I wanted to spend money I didn't have get the item and then have you charge me a negative fee.
I mean I guess if you want to be embarrassed at the register because your card didn't go through and
you're like I'm sorry sir your card didn't go through Was everything OK.
Do you need any help with your family.
No.
So anyway my point is we need the equal sign so we can actually buy that item the item to buy.
OK.
Then print purchased item.
OK.
So if you've got more money or equal to or an equal amount of money I'd buy print purchase item.
Another thing we could do you know we could say if item to buy is greater than the bank balance
you need more money.
OK.
So
if Ireland buy is great for the balance you need more money for this and we're using a greater than
only sign.
So let's say that having to buy is now five hundred dollars.
Sure enough it prints out right here.
OK this is cool so so far we've used greater than or equal to.
We've also used the greater than sign here.
OK.
And maybe maybe your bank whenever you whenever you hit zero for instance it wants to give you a notification
saying hey you're in no money you spend it all.
You know so you know this is a silly example but we can say if item to buy is equal to bank balance.
OK.
And then what we can do is we can say print Hey buddy your balance is now zero.
OK.
So I'd advise the bank balance Hey buddy your balance is now zero so if I say I'm the buy is $400.
Hey buddy your balance is now zero.
So if this is equal to this OK then do this.
Now remember remember these operators here OK are returning a true or false value.
OK.
So remember you could actually store this in a variable.
So for instance var am I at zero equals I want to buy people's bank balance.
OK.
So it's going to run this operation first and then what it's going to do is it's going to return a true
or false value into this.
And this is a bull.
And so you can actually store the result if you want it to.
But again like I said in most cases you don't.
OK.
And then let's see here.
The only other one of great importance is the not equal to sign so I'll show you that as well to friends
and also we want to compare instead of numbers we want to compare strings.
So if let's say correct spelling is not equal to.
And let's actually let's better save our word one equals Let's get say let's say this are book titles.
So book title one equals let's say Harry Potter and the Moffit of Mier on my book.
OK.
Anyway and then we have a book titled two equals unless we have the same thing here but this one's entered
by a user.
OK this one was entered by a user they knew would be the same thing right.
So let's say that the user action in lowercase this and you know if you got the A.
You know this bad spelling.
So we can do is we can say if Book Title One is not equal to book title to you know print need to fix
spelling before printing whatever.
OK.
So if this one is not the same as this one then do something else.
OK.
So it's basically the opposite of using these here.
OK.
And you can you'll see programmers use these interchangeably.
And you can kind of sometimes you can do to get the same result in decision using different operators
just depending on what you put first or second.
But you can do whatever one you want which is cool.
So this is all really cool.
This is really cool stuff.
Another thing you can do is remember how we did this example up here has data finish downloading.
You can also use that unary operators so we could say if our first let's let's do it with stood up here
before it's been set to true.
So if not has data finished downloading.
OK then print loading data.
So at this point in time if the data has not finished downloading let's print to the screen loading
data.
So this is the operator put before the boolean value.
And so it's saying the opposite right.
So that's why it's so important to word your variables with your Bulleen so carefully so has data finish
downloading.
That's the question we're asking.
So if the data has not finished eling that's what this is saying here.
Print this here.
OK print loading data.
Pretty cool stuff right.
So what we've done here so far is we've just used some of the basic operators of course we've used the
greater than or equal to you can also use the less than or equal to.
And you can start making decisions based on those operators.
OK the result of those operators which is I think pretty dang cool.
One more thing I want to show you and then we'll be done.
So if book A 1 is not what the book title to.
OK then we need to fix spelling before printing great else.
Here's the key word.
So if this happens or if this is true great.
If not go to the next block of code and this will always always always be executed.
If this is false and this will never ever ever be executed if this is true it very important to know.
And so you say print book looks great send to printer.
Pretty cool.
Put her on.
It's not what the book title too great we can do more though like we can also say so.
If so the first thing we want to do is check the spelling then what we want to do is we want to make
sure that the size of the book title is a proper size what we can say.
Else if.
OK.
Book Title One dot see her length on the string here.
See
and far here oh characters has the string and the characters and the cat her goodness so if the count
of the entire string is greater then let's say 10 like then we need to do is print find a new title
for the book.
OK.
So that's interesting.
So look what we did here in the book one title or the book title one is not equal to book title to them
we need to send us the printing.
Well let's say that we fixed it.
All right so this is great we've just fixed it.
The book is now perfectly edited whatever.
But look here.
Find a new title for your book is now called.
OK.
So we passed the first one this is true.
So then we go to the next thing or excuse me this is false and therefore it's not called.
This is false.
So ELSE IF book titled two characters that count is greater than 10 oh no we need to find a new title
for the book.
This is too long it's not going to fit on the book.
OK so we need a book that has nine characters in order in order to get to here to the else clause so
let's say one two three four five count the space that the character six seven eight nine.
Oh wow so Harry Potter and the muppet of fire was ternary below.
There's books about anymore but the editors thought it might be a little more appropriate to add a G
there instead.
So we've got some crazy book that we've released the title on and now look see if we pass it here.
So if book titled to the characters that count is greater than 10.
Print a new title for the book and of course we need to copy this over here because it needs to be the
same.
So a book title is not equal to a book title to.
Well it is so this is false we're not going to call this LCF book title that characters that count is
greater than 10.
We fix that problem too.
So this isn't going to be false.
So here's the end all catch all so once we pass those conditions it is now true and we're able to print
the book.
OK.
So we work with if statements else if statements else statements.
We've worked with these conditional operators here.
OK.
That double equals the greater than the greater than equal to.
We've also talked about less and less than or equal to we know a lot of stuff here.
We've talked about the urinary not OPERATOR OK the prefix here.
If it's not finished downloading we're talking about booleans and true and false and how this will equate
to a true or false.
Some really good stuff so we're going to call this video done.
It's a lot to take in.
Watch it again and you're going to start using conditions in your applications because that's how applications
think and make decisions is by using tools.
Mark Price Loeb's dotcom.
Moving on and for.