‫Instructor: Hi, within this lecture,
‫we are going to continue working on playgrounds
‫and we're going to learn about variables.
‫So far, we have been creating variables
‫as an example, like this.
‫So like, var ctr is a variable.
‫myNumber is a variable.
‫So let me take a note here.
‫If you do two slashes,
‫you can take a note like this, variables and constants.
‫We're going to learn what a variable is right now
‫but this is important as well.
‫If you add two slashes before a line
‫it means this is a command
‫so you cannot type whatever you want in a coding section.
‫Okay, so this is all relevant codes
‫but if I do something like this
‫so if I write something random like this,
‫if I click the play button over here
‫it won't play because it won't recognize what I'm writing.
‫So if you take a note, just add two slashes.
‫So we call this a command string,
‫a command line in our code, okay?
‫So what is a variable?
‫As you can see, we can create variables with keyword, var,
‫but why do we need them?
‫Because we cannot give everything in the code.
‫Maybe we wanna take some input from the user
‫or maybe we want to get some data from the internet.
‫We want to download something,
‫so we cannot know what's gonna happen.
‫We cannot anticipate everything, so we need to
‫store those values in order to work on them later on.
‫For example, if you're going to build an app like this
‫you're going to take the input of the user for their name.
‫So the user will just enter their name into a text box
‫and then later on they're going to enter their surname,
‫and we are going to just display this name
‫and surname together.
‫So in order to do that, you cannot just come here
‫and you cannot just say name is this, name is that,
‫or just show this, just show that.
‫You have to create something
‫in order to hold this value inside of it.
‫So this is a variable, this is what we call a variable.
‫For example, we are just multiplying this 10 by five here,
‫and it shows us the value of 50,
‫but maybe we want to multiply the age of the user by five.
‫So when I'm writing this code
‫I don't know the age of the user,
‫so I need to create something like, var my age,
‫then multiply it by five.
‫And let me do this by example
‫so you will understand it much better, okay.
‫So when I create a variable, I use var keyword.
‫For example, if I say, var userName,
‫and beware that I'm using the second word
‫with capitalized letters.
‫So if I say userName, the N will be capitalized.
‫Why are we doing this?
‫I'm gonna show you, don't worry.
‫For example, right now,
‫I'm going to assign my userName to be James.
‫So why I'm using a capitalized letter?
‫So this is a convention, okay?
‫This is what we generally do in Swift programming language.
‫Every programming language has their own convention.
‫So let me take a note here.
‫So we have something called snake case.
‫If we follow this convention,
‫we will do this in a different way,
‫like that, user_name
‫and this is a snake case convention, okay?
‫This like snake case naming.
‫So if we do something like this,
‫var user_sur_name, okay,
‫with underscores, it will still work.
‫It actually doesn't matter what we name our variables, okay?
‫But generally we don't do that in Swift.
‫So when you go to the internet
‫when you search for things, you won't see that.
‫You will see this.
‫So I'm going to follow the convention of Swift
‫which is a camel case, okay?
‫So this is called snake case
‫and this is called camel case.
‫And again, this is not mandatory, but we are doing this
‫in order to make our variables
‫more readable by coders, by developers.
‫So I'm going to follow this norm in here
‫throughout the course,
‫so that's why I'm using capitalized letters.
‫So for example, I created a userName called James
‫and I created as userSurname in the name of Hetfield, okay?
‫So anytime I can change those variables
‫because they are variables, okay, I can come here
‫and say, userName is not James anymore,
‫UserName is something else.
‫So I can do whatever I want to do with James.
‫So because this is a variable, right?
‫Because I stored this value into userName
‫and I stored this Hetfield value in the userName string.
‫And the string means basically a text, okay?
‫A text value.
‫So what if I want to change this userName?
‫If you type userName,
‫and if you say this is not James right now,
‫this is Lars, if you hit shift enter
‫rather than hitting the play button
‫it'll automatically run these codes for you, okay?
‫You can do shift enter when you type something,
‫like you can say print userName,
‫and you can say print userName here as well
‫and not userSurname, userName,
‫as you can see, if I type user
‫I can hit tab or I can just double click
‫on whatever I want to choose,
‫and now if I do play or shift enter, it will play it for me.
‫For example, as you can see,
‫for the first time, this run userName, print userName
‫and it just gave me James,
‫and for the second time it gave me Lars
‫because I changed the username in here.
‫And this tells us something.
‫This codes are actually run with order, right?
‫They're running with chronology.
‫So it is running the first comment, then the second comment,
‫then the third comment and the fourth comment
‫and the fifth comment.
‫And because of that,
‫first I have seen the James as a log,
‫as a print output,
‫and then I changed this James, then I solve Lars.
‫So for example, if I go to the top
‫and if I want to change the username, I cannot do that
‫because the username won't be even defined in here, okay?
‫So if you hit play button over here
‫it'll give you an error.
‫It will say this is an unresolved result identifier,
‫because why?
‫Because I have initialized,
‫I have defined this in the 10th row, in the 10th line.
‫UserName isn't even defined in the eight line.
‫So once you define this with a var keyword,
‫then you can change it, you can play it,
‫you can do whatever you wanna do with it.
‫And this is for variables, okay?
‫We also have constants and we use let keyword for that.
‫So it means that we are going to define this value
‫and we are not going to change it afterwards.
‫So for example, let's say that let user age is 50.
‫Now I can reach the user age when I type user age, okay?
‫But I cannot do this.
‫I cannot say user age is now 30,
‫because I have defined this as a constant.
‫And as you can say, this is unresolved
‫because I have misspelled it.
‫As you can see, I have to run A with capitalized A, okay?
‫As you can see, it says that you cannot assign this value
‫'cause user age is a let constant.
‫So this is not a variable anymore, this is a constant.
‫So we have to create the variables
‫with the var keyword if you want to change them afterwards.
‫And we want to create variables or constants
‫with let keyword if we don't want to change them afterwards.
‫Okay, so why do we need constants?
‫For example, if we are going to use something
‫like pi number in our app,
‫we won't change it forever, right?
‫So pi is constant, like 3.14.
‫And it doesn't make any sense to define this variable
‫with a var keyword, so we need a let in here.
‫So we multiply 10 by five before,
‫can we multiply userAge with pi?
‫In fact, we cannot do that
‫because they're not even the same type.
‫So here comes a new concept.
‫So there is something called type, okay?
‫And as you can see, one is int, integer,
‫and another one is double.
‫So we are gonna learn what those mean
‫in the next lecture, don't worry.
‫But for right now, know that variables are not the same.
‫So we have text strings like James or Hetfield or Lars,
‫and we have doubles or in integers.
‫So if you put dot O over here in the userAge value
‫then it'll compile
‫and it'll just do the multiplication for you
‫and show the results.
‫As you can see, now, they are the same type.
‫So now maybe you understood that numbers with decimals
‫and numbers without decimals are not the same type
‫and that's correct.
‫So we are gonna stop here,
‫and within the next lecture we are going to discuss
‫what are some different kinds of variables
‫that we can operate with in Swift.