Hey, what's up everybody? Mark Price at Devslopes.com plugging our way through foundational Swift and programming. I'm not only teaching you a swift 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 Xcode eight 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, BullsandConditionalsandComparisonOperat- ors, okay? I want to create a folder for this, Swift, and we'll say bulls-and-comparisons. Okay, and create, so a Boolean is true or false, simple enough? So var, and we're kind of done this once before, right, var. AmITheBestTeacherEver equals, what's that? Jennifer says true, thanks Jennifer, okay. So am I the best teacher ever? Well, thank you, I wouldn't say that about myself because that would be narcissistic. So that's a Boolean, it's true, but it can also be false, can't be anything else though. So, am I the best teacher ever? Well, let's say that Xcode just keeps crashing and you're getting sick of watching me have Xcode crash, and you're just angry. And so now you want to set this to false because X cookies, it's not even my fault is crashing. Don't get it 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 case happening to us and, I actually do cut out a lot of the crashes but at least some of them in there so you can know that again real life development. You see it in the flash there, so, am I the best teacher ever is now false because it keeps crashing. Okay, and I'm not cutting it out at the video. Okay, anyway, so true or false. So what does that mean? Why does it mean that we are storing a 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. Okay, right, does that make any sense? You want to do something based, should I do this? 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 because of a rough childhood. Anyway, cause and effect or true or false cape, Boolean's are foundational principle for that. And this is again type inference here. We're inferring it as a bool but the actual keyword if you want to explicitly declare it is bool. Okay, not Boolean, okay, not Boolean literal type converter, but nothing else. Okay just bool, and I'm not going to explain what these other things are. If you're wondering, hey, what's the difference between Bool and Boolean and Swift when I have the pot when I'm autocomplete comes up go search it on Google. I'm telling you, all you ever need to use is Bool. Okay, so true or false, and how can you use these things? Well, to make decisions for you, so that's it. [LAUGH] that's Booleans, let's put them in action. Now let's actually see how we can use comparisons with well, I can show you one more thing actually, just one more thing. Okay, just to blow your mind confuse you completely if true equals false or if true equals true. Print WTF 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 two. So if true is equal to false, okay, if true is equal to 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 okay, this is the/or operator, okay, and or if true equals true, so if this is true or if this is true then go into here. Okay, 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 isn't a great example because you probably wouldn't write this in your code. But, a lot of coding tests and and brainteasers out there will actually ask you to decipher things like this. So 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. Okay, so true or false, a better example is for instance, var has data finished downloading, how when you get an app and there's a spinner bar, okay. I'll just explicitly declare it so you can know it's a Boolean. So, basically when you get that spinner bartending up a server and it's downloading data, okay, so, you know, we got the spinner dot, dot, dot. Little color that was nice, dot, dot, dot, that's funny. Let's just pick a color, I don't know what it's doing to be honest, but I like it's yellow. [LAUGH] It's 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, right. And then we can do something like, load UI and other app features. So this is an example of when you actually use a bool in your code and in your projects for things like this. And I've done things like this before, it's nothing wrong with, it's a common thing to do. So it's Boolean true or false. It's one way you can use it, again, anytime you need to get a true or false. Yes or no, Boolean is the way to go, bool for short. The Bools go hand in hand or Boolean logic goes hand in hand with comparison operators. Okay, comparison operators will return a Boolean. Mmm, okay, when I say Boolean, it is bool, but Boolean is the long term for it. Swift just condensed it here. So for instance, this is really easy if one is equal to two. Print should not see this, okay? So, we'll never be executed, thank you, it's smart. So if one equals two, well one is not equal to two and so it will never be called, so again this returns a Boolean. Okay, that's really interesting? I wonder, do you think you could do something like that over here actually in a Boolean itself? So what if we said, instead of saying false, we'll say, this has data finished downloading equals one equals two. And you're staying, so like I said, this is returning a Boolean, a true or false value that's being stored in here. So like I was telling you earlier, if you use them less here in variables and use them more in conditional logic. Okay, you return them but only return a Boolean for the sake of performing at making a decision, okay. So if one equals two print, you should not see this. If two equals two, now we're seeing it, okay. So, it returns a true or false value, the conditional logic, okay. So let's talk about the different operators here. All right, so we've got equal to and it's the double equal sign. Okay, we've got not equal to, which is the exclamation point and the equal sign, not equal to. All right, we've got greater than, and this is a, it's greater than b, okay. So, we'll just say that's greater than and I forgot my colon may be consistent. And then we've got greater than or equal to, which is the big Pac-Man mouth to the left. K equal to, and then we have less than or equal to. Like so, and then we've got less than, whoo. Okay, so obviously there's a lot going on here. But let's see how it actually works. Let's see it in action, okay. So, of course the common example here that we've used already is a bank account so we can obviously do that here again. So var bank balance, right, equals or equals by $400, okay. Var item to buy equals $100, okay. So then what we can do is we can say, if bank balance. Okay, bank balance is greater than or equal to item to buy. Why wouldn't we just say if it's greater than? Well, if the item to buy was $400 this would turn out to be false and we wouldn't 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 put on overdraft protection, 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 charged me a negative fee. I mean, I guess if you don't want to be embarrassed at the register because your card didn't go through and they're like. I'm sorry sir, your card didn't go through is everything, okay. Do you need any help with your family? No, so anyway, my point is we need the equal to sign so we can actually buy that item, so item to buy. Okay, then print purchased item, okay. So if you've got more money or equal to or an equal amount of money, then item to buy, print purchase item. Another thing we could do, we could say if item to buy is greater than the bank balance. You need mo money foo, okay, so, If I didn't buy is greater than bank balanced you need mo money foo. So this time we're using a greater than only sign. So let's say that item to buy is now $500. Okay, sure enough, it prints out right here. Okay, this is cool, so so far we've used greater than or equal to, we've also used the greater than sign here. Okay, and maybe your bank whenever you hit zero for instance. It wants to give you a notification saying hey, you're at no money, ee spend it all. So, this is a silly example, but we can say if item to buy is equal to bank balance. Okay, you know then what we can do is we can say, print, hey buddy, your balance is now zero, okay. So if I advise equal to bank balance, hey, buddy, your balance is now zero, so if I say item to buy is $400. Hey buddy, your balance is now zero, so if this is equal to this, okay, then do this. Now remember, these operators here? Okay, are returning a true or false value, okay. So remember, you could actually store this in a variable. So for instance var, am I at zero equals item to buy equals bank balance. Okay, 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 Boolean. So you can actually store the result if you wanted to but again, like I said in most cases you don't, okay, and then let's see here. The only other one of great importance is the not equal to sine. So I'll show you that as well too. For instance, let's say we want to compare, instead of numbers, we want to compare strings. So if let's say, correct spelling is not equal to, And actually let's do it better, save our word one equals, let's see those are book titles, okay. So, book title one equals, let's say Harry Blotter and the Moppet of Mire, sign my book, okay. Anyway, then we have a bar book title, too. Equals, and let's say we have the same thing here, but this one's entered by user. Okay. Okay, this one was entered by user, they need to be the same thing, right? So let's say that the user accident lowercase this and forgot the a, this bad spelling. So we can do is we can say, if book title one is not equal to book title two, print need to fix spelling before printing whatever, okay. So if this one is not the same as this one, then do something else, okay. So it's basically the opposite of using these here. Okay, and you'll see programmers use these interchangeably, and you can kind of, sometimes you can do get the same result in a 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 finished downloading. You can also use that urinary operator so we could say, if, well first let's do it up here. Before it's been set the true, so if not has data finished downloading. Okay, 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 urinary 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 Booleans so carefully, so has data finished downloading? That's the question we're asking, so if the data has not finished downloading that's what this is saying here, print this here. Okay, 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. Okay, 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 title one is not equal to book title two. Okay, then we need to fix spelling before printing, great. Else, okay, there's the else keyword. 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's very important to know, and so we can say, print Book looks great, send to printer. Okay, pretty cool, right, book title one, not equal to book title two, great. We can do more though, like we can also say, 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. So we can do is we can say, else if, okay, book title one dot, let's see. Is there a length on a string here? Let's see, And far here. Characters hashed the string then the characters, then the count, okay goodness. So if the count of the entire string is greater than let's say 10, then what we need to do is print find a new title for the book. Okay, so that's interesting. So look what we did here. If the book one title or the book title one is not equal to book title two, then we need to send this to 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. Okay, 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 title two dot characters dot count is greater than 10. No, we need to find a new title for the book. This is too long, it's not going to fit on the book, okay. So, we need a book that has nine characters in order to get to here to the else clause. So let's say, one, two, three, four, five count the space. That's a character six seven eight nine, wow. So [LAUGH] every blot on The Muppet of fire was just ternary blown [LAUGH] don't know what this 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 reduced the title on and now look, let's see if we pass it here. So, if book title two dot characters dot count is greater than 10. Okay, print and new title for the book and of course, we need to copy this over here because it needs to be the same. Okay, so if book titles not equal to book title two, well it is, so this is false or not going to call this else. If book title that characters that count is greater than 10, well, we fix that problem too. So this is going to be false, so here's the end all catch. Also once we pass those conditions, it is now true and we're able to print the book. Okay, so we work with, if statements, else if statements, else statements, we've worked with these conditional operators here. Okay, the double equals, the greater than, the greater than equal to. We've also talked about less than and less than or equal to. We know a lot of stuff here, we've talked about the urinary not operator. Okay, the prefix here, if it's not finished downloading. We've talked about Booleans, and true and false and how this will equate to a true or a 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 decision is by using these tools that we'd talked about. So that's it, Mark Price Devslopes.com moving on and forward. [MUSIC]