Variables, operators, & how computers work

Devslopes by Mark Wahlbeck
A free video tutorial from Devslopes by Mark Wahlbeck
Learn programming & app development
4.3 instructor rating • 25 courses • 331,991 students

Learn more from the full course

iOS & Swift: Become An App Developer

Beginner's Guide to Learn iOS, Swift, ARKit, CoreML, App Design, Advanced Programming and Much More

90:06:06 of on-demand video • Updated August 2020

  • Submit iOS apps to the Apple App Store
  • Understand the basic concepts of programming
  • Apply for JR iOS developer jobs
  • Work fluently with Swift and iOS
  • Work as an iOS freelancer
  • Master Swift from basics to advanced
  • Learn mobile app design with Sketch
  • Build ARKit & CoreML apps
English [Auto] What's up everybody in this exciting episode we're going to talk about variable's operators and a little bit about how computers work. Now this is not a theoretical course but I do want you to understand some of the basic principles that are happening underneath the hood so you can have a foundation to build upon. And of course you're going to want to learn these things at some point in time anyway and dive deeper into them. So let's go ahead and get started. The first thing I want to do is talk about variables so go ahead and pull up in a browser here and we're going to say what is a variable in programming. Common question popped right up and we're going to do this one right here launch schoolbooks Ruby read variables. What is a variable. Variables are used to store information to be referenced and manipulated in a computer program. That's a good definition. They also provide a way of labeling data with a descriptive name. So our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers as a keyword as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program. OK so they're like a container that can hold data. Well what does that even mean. Well let's think about what's going on under the hood here and he'll hold my Photoshop here for you. Or you go and what does that mean. Get my pen. So let's say you've got your RAM and your computer. Ok this is your rim you know. You know how when you're like I need to upgrade my RAM my computer needs to go faster. You know you've heard somebody say that and you don't really you don't really know what it means or you just say it and say well we have more RAM means a faster computer. Well what does that even mean. You know ram ram What has that to do with variable swell. RAM is random access memory random. So it means that things are going to be stored and it randomly accessed and retrieved and things will be cleared out of it. OK so what happens is when you create a variable which you haven't seen yet but when you create a variable OK it is representing data that has been stored in a memory location. So let's say we've got a number. OK. Like the number 23. OK. And I don't know what the binary value of this but a computer is read zeros and ones. True or false. You know so this is all pretend but you know let's say this was 0 1 0 0 0 1 0. Again that's not the right value for that I could look it up online I can do it in my head sorry. Anyway so this is kind of hard to remember right. Do you do you can remember binary for every single thing that needs to happen in your program. Probably not. So what happens is we use variables. And so we say oh 23. Well let's let's say this variable's name is age. Age is a lot easier to remember than you know 0 1 0 0 0 0 1 0 right. So we give a variable a name and it has a value OK a name. So the container the value. And this is stored in a memory location somewhere and who knows what that is. You know x 1 0. You know whatever because some random access memory location in your program stores it in there and then when it needs it let's say you have a calculator app OK and you type in a number will stores a variable somewhere in your in your app which goes onto your RAM and it's stored there. And then when you clear it out maybe it's gone. You don't need any more so then it's clear now to your memory. OK. So when somebody says oh I need to upgrade my computer get more RAM so my programs can run faster. What that really means is you may have multiple programs. OK. Say this is photoshop. OK let's say this is X code let's say this is Android studio. OK well all of these. OK all these programs they need spots in memory. So the more memory you have the more variables you can store in the faster they're going to run because if you don't have enough what's going to happen is they're going to go in there and they're going to go on a waitlist. You can't go in until memory is cleared out. So it's going to be in a waitlist. So if you ever loaded a program and it's like loading and taking forever you're like what's going on. Well it could be that you don't have enough RAM and all these programs are trying to save data in your random access memory and it can't because there's not enough space. OK so a variables a container that has a value of some kind. And the container references a point in memory a memory location and it helps us to write readable code so we don't have to do things like this so we don't have to understand this kind of stuff because I surely can't understand it. So that's kind of how computers work with variables and applications they store data. They retrieve data but you get to use readable variables appear at the top that will make your life much easier than having to remember anything else. OK. So it's variables in a nutshell and I'm going to close out now. So we've talked about variables. Well let's show you what a variable actually is. Go ahead and open up your X code x go to it and we're going to go ahead and get started with a playground. I'm going to call this variable's and let's store this in the folder. Oh great new folder called variables and create so by default your playground your ex-coach playground will actually give you a variable inheritance bar Guevara's short for variable. You're telling the program hey this is a variable. It's a container that I want to put data in. And what's this. This is the name we can call this whatever we want. There are some things that are aren't allowed. We can't put numbers in the front of a variable name. OK. But you can use words and then number like day 1 you can do things like that. But in our case we want to write the word message. Variables should always be descriptive. They should be descriptive. They should tell you what's being stored in there. And then we're storing some words and this is called a string specifically when there's two quotes. That is called a string. And the key term if you're really interested is a string literal. This is a string literal that is being created right here on the fly. And it says hello playground and the playground is kindly printing this up to the screen for us. So this is your very first variable and this is a string so the data type is a string characters words. And this is being stored in the variable and then underneath the hood when your program is running this will actually even right now. This program is running on our Mac and so this is actually being stored. This is being stored in the memory on the computer somewhere which is pretty cool. I think it's cool. And so that's happening for us automatically and maybe you're wondering up here what is import you like it. Well import means somebody else wrote a lot of code for us. In this case Apple There are a lot of code and we're importing it so we can use it. So you let's just use the cool features of iOS like views and pop ups and screens and view controllers and that's all included here inside of this a bunch of code that they wrote and we're bringing it in. It also includes a framework called Foundation which includes Swift which lets us do things like this which is pretty cool so that your very first variable will give it a descriptive name. We started it off with a keyword specifying var and then we said hello playground variables can be changed. OK. Areas can be changed. Whereas something called a constant cannot say very very well. We change as many times as you want though. And the other thing is that we didn't cover was this the equal sign. This is called an assignment operator and that's the keyword if you want to write that down as the assignment operator. And what the assignment operator does is it assigns a value from the right hand side into something on the left hand side. Typically a variable. So that means we are taking something over here and we are putting it over here. OK. The assignment operator. So a song about operators for a minute. We've talked about variables and how they work. Let's talk about operators. So there are three types of operators in swift or most languages anyway. So there are very binary and ternary. What does that even mean. That is a very good question. Ternary operator only works on one target. So if you look at this right here OK this is a urinary operator. No it's actually a binary because it works on two. And look at the keyword or the prefix here. By no means two. OK so this works on two so this is a binary but what is a urinary. Are your urinary works on a single operator. And so for instance you can have a variable that's called a boolean So you know am I cool. This is a variable called emic cool. And we're going to say equals true. OK. That's pretty cool. I'm cool. But you know then some new phase or fad comes out and new clothes and I haven't upgraded yet. So now I'm uncool. Well we can use a urinary prefix operator. You can write that down the generic prefix operator and we can say well we can say Am I cool equals not am I. Cool. So the opposite of what I am. So right here is this operating on one target or two targets. Well it's only operating on one target this one right here. So this is a urinary OPERATOR You know you'd like unicycle. Ok one. So what we're saying here is am I cool that's true. We're saying the opposite. Not Am I cool so we're saying false. And then we're shoving it back into here. So now my cool is false. I'm no longer cool because some new fad came out. You know maybe back in the day like in 2006 when Paris Hilton like put on this big huge aviator sunglasses and before that no one had seen those since like the 70s and then all of a sudden every girl on the street in the world like within one week had big aviator glasses. OK. So if you didn't have them you're uncool just like I'm cool here because this new thing came out and I didn't adopt it. So you're Neriah Hey operates on one target. Binary operates on two targets OK a left and right. As you can see right there. And when you're programming you're not going to use these terms. But if you're going for an interview or something like that they may bring these up and ask you to define them so it's good to know good stuff to know. Actually a ternary operator works on three objects and in swift There are only there's only one ternary operator. And I'll show you the syntax you'll use. As you become a more advanced programmer but let's just show you for sake of completeness. Again you don't have to memorize all this this is just an overview. So what we can do here is let's see here we have another variable called feel good about myself. OK. Feel good about myself. Equals true. So that's a variable of type Boolean. OK to bully and type which we'll talk about later. And we're storing true into this container here this variable. So let's do a ternary operator. Just for fun. OK. So feel good about myself. Equals and we're going to say Am I cool. So if I am cool. OK. Question mark if I'm cool then feel good. All right then. True otherwise false. That's what we want to do. Maybe you're thinking what's going on here. This is so confusing and you shouldn't be confused if you're absolutely new to programming. Should that make any sense. But I'm showing you what a ternary operators with three targets are what this is saying is. So right now I feel good about myself right. Right. But then that new fad comes out and I'm not cool anymore so or doing here's are saying is this true or false. If this is true. OK. So that's what we're saying here with the question mark. If this is true then let's put True into feel good about myself because then I feel good about myself because I am cool. OK. And if I'm not cool otherwise false or I shove false into here and I no longer feel good about myself and I'm a very shallow person. So that's a turnaround operator because it works on three targets right here right here and right here. Does that make sense. Another example is just to show you the ternary operator over time without the true and false. Let's say I've got a variable called bank account balance equals let's say 100. And then what we'll do is we'll say bank account balance. Actually you know what we'll do is we'll save our mess. Bank message KTM cash register message cash register message. Let's say we want a message. We're going to print out to someone who wants to buy something at a store. Right. Cash register message. OK. So it's going to equal what we're going to do is we're going to say if a bank account balance is greater then greater than or equal to let's say 50 has a bank account balance is greater than or equal to 50. And these are logical operators which we haven't covered yet. If it's greater than or equal to 50. OK what we're going to do is we're going to say you just bought the item otherwise you are broke as a joke. OK. So what we're saying here is we run we run a check here we do a conditional statement. If your bank account balance is greater than or equal to 50 maybe this is a pair of shoes you're going to buy then here's the question mark. I we say you just bought this item so if this is true let's do this. Otherwise you are broke as a joke. So watch as I turn this item to $150 you're now broke as a joke. So this is again a ternary operator OK. There's three things happening here. One two and three trinary three. OK. And again this last one ternary. You use it less often you will use it though and you don't have to know right now if this is confusing at all. That's OK. This stuff takes time to get down. But that's a ternary operator. Other operators that will learn about later such as arithmetic operators that's a key word or key phrase arithmetic operators such as adding and subtracting and dividing. There's also a remainder operator for doing division and then getting the remainder of the result. Because division does not give you remainders things like that but we'll talk about that later. This was just a synopsis of how variables work what a variable is. We talked a little bit about your unary operators binary operators and ternary operators and you learned that you can create a container called a what if you said variable. That's right. And so you create a variable by starting with the Barkey word then you give the container a name message and then you can store a value into that. You know such as a string This is called a string of characters in words or a boolean true or false or a number even. You can assign in there using the assignment operator which is a binary operator because it works on two targets. And again this is a unitary operator because it works on one target. So cool stuff here. It's fun to learn about these things you should dig more into it go on the Internet and search about your operettas binary operators turn or go back to that Google search on what is a variable in programming it's important to know these things. And I'll always say that over and over again because you want to become a good programmer. So that's it for now. Mark Price of slopes dot com and we are putting the doors into a brand new world of magic and excite you later.