Dart Programming in 4 hours | Full beginners tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everyone in this video i'm going to be walking you through everything that you need to know to get started with the dart programming language this is a course that a lot of people have been requesting over the last uh couple days so i wanted to just make it really quick specifically a lot of people have been asking to learn the flutter framework so the flutter framework is a framework that was created by google and it's used to build mobile apps and also like websites and in order to get into flutter you have to first know the dart programming language because that's what it's all done in so this video is mainly targeted at people who are new to computer programming or who are new to the dart programming language and you just want to learn all the core concepts with the idea being that you could go on and build stuff in flutter or just continue building stuff in dart now dart believe it or not is actually one of my favorite programming languages i learned it a couple years ago and i just think it's really really fun to program in mostly because it's a new language so a lot of the programming languages that people use nowadays have been around since like the 90s so they've been around for a long time like you know a couple decades dart is fairly new and because of that i think there's a lot of features and a lot of things about it that are really polished and that you know sort of take lessons from these other programming languages and add them in if you know javascript dart is very similar to javascript there's a lot of similar syntax a lot of similar ways of doing things so you'll be able to pick this up real quick but this tutorial is designed for people who have absolutely no experience to programming so if you're coming in and you've never written a lot of code in your life this is the video for you or if you've done some programming and you just want to learn about dart this is also going to work as well but i'm going to be stepping you through each and every core concept in computer programming and eventually then you can go off and build some mobile apps or do something with dart that you know basically do whatever you want with it so that's a little introduction about what we're doing today now let's take a look at our environment now in this video i'm going to be actually coding the entire video in this repel so there's this site called reple and it allows you to write code in this very consistent environment so all this code is actually going to run on replace servers so basically i can write code over here in my little text editor and then i just click the run button and then it'll run so you don't have to set anything up you don't have to download anything you just literally i'll put the link in the description for where you need to start you just click on that link and then you have the exact same environment that i do and then you can follow along with me the idea being that once you get comfortable programming in dart then you can go off and set up your environment and do all those things that you want to do but this is just a nice environment for this video perfect okay now with that said let's go ahead and get into the tutorial so once again we are going to be starting from the absolute basics of computer programming so i'm going to be walking you through all the core concepts and we're going to start right now so when you open up this rebel environment or if you're doing this sort of in your own environment you maybe you already know how to set that up when you open this up you're going to see with dart that there is this thing called main and this is actually what we call the main function and we're going to talk about what functions are we're going to get into what all of this is but what you need to know for now is that the inside of these two open and closed curly brackets this is where we're going to be writing like 90 of our code in this tutorial okay so any code that you put inside of this main function is what's going to get executed by dart so when we write code we're basically writing instructions for the computer to carry out and it's the instructions that i'm going to be teaching you in this video so i'm going to walk you through all the different types of instructions that you can give to the computer and i'll even teach you how to build your own instructions using like smaller level instructions and things like that so all you need to know for now as we start off is that you're going to want to write all of your code here inside of what we call the main function so inside of these two open and closed curly brackets and we can get more into like what this does later once we have some more context but for now just know that so i'm going to be working in here now whenever we write computer programs like i said we're giving the computer a list of instructions and that's really what we do as computer programmers we're basically writing out just really really really complex sets of instructions and like i said we can build like we can take a bunch of instructions and bundle them together and then send those off somewhere so we can take these instructions and write them and organize them in different ways but at the end of the day we're just writing instructions now generally when you start coding the first instruction that you're going to give the computer is you're just going to tell the computer to say hello to the world and this goes back to like some of the first programs that were written and they displayed out onto the screen hello world so that's where we're going to start now there's a concept in dart and in all of computer programming of printing and this is basically having the computer give us information so when i say that i want the computer to print something out or i'm asking dart to print something out basically what i'm asking dart to do is give me some information onto the screen and this is the most basic way that the computer can give us information if you learn like web development the computer can generate all these like inputs and all these cool components and elements but when we're working in dart we're just going to be printing things out onto the screen so the first instruction that i'm going to teach you is the print instructions so you're just going to type out print like that and then you're going to make an open and closed parentheses now this is a very very common i guess like syntax in computer programming where we have this name here in this case it's print and then we have an open and closed parenthesis so we do this a lot and this is actually what's called calling a function and we're going to talk more about functions later but just know that this is a really common syntax that we're going to use so i'm going to say print i'm going to make an open and closed parentheses and then an open and closed quotation mark and then inside the quotation mark i'm just going to write hello world exclamation point so this is your first line of code that you're going to write so we'll just write this out and then the last thing we need to do is add a semicolon so any time that we write an instruction in the dart programming language we always want to end it off with one of these semicolons and that's really important now here's the thing you're going to see me forgetting to add in the semicolons you are going to forget to add in the semicolons semicolons are the bane of computer programmers existence but there is a good reason that they're in here just know that probably the most common mistake you're gonna make and like i said you're gonna see me make this mistake is forgetting to add these in so right here we have our first line of code and i'm going to click the run button up here and you'll see that it's going to go off and it's going to run it so this environment over here on the right is what we call the console and this is basically the most primitive way that the computer can communicate to us so like i said the com the computer can like draw things on your screen this whole website this repli website is basically the computer like drawing things out onto your web browser but the most basic way that we can get info back from the computer is over here on this console and that's really all you need to know about this for now but just know that this is the way that the computer is going to be communicating with us and we'll also be able to like type things into here as well as as we go on okay so we have our first instruction here and like i said this is a really common instruction always is going to end with a semicolon but the thing about computer programming is not only do we want to give the computer one instruction we want to give the computer many instructions and really as a computer programmer all you're doing is just figuring out which instruction should i give the computer and what order should i put them in and so that's the next concept that i want to talk to you guys about which is writing multiple instructions and the execution flow of the computer so what we're going to do is we're actually going to write a computer program that's going to draw out a little triangle onto the console over here i'm going to show you how to do that so let's start with this first print statement and in here i'm going to start drawing like the beginnings of a triangle so i'm going to draw the base of the triangle first so we'll draw this guy right here and then i'm going to put like three of these like underscore characters and then i'm gonna put one of these bar characters okay now what i'm gonna do is i'm gonna copy this line of code so i'm just selecting this and then you can hit command c if you're on a mac or ctrl c you'll see me doing this a lot copying the code i'm going to paste this right above it and then we're going to continue on with the little drawing so you'll see here now i'm continuing on like drawing this little pyramid and my computer's adding in a period so now we're going to take this next line we're going to copy this and we're going to paste it over here and once again we will do this so you can see this little pyramid is kind of starting to come together and this is going to be a really good way for me to explain how the computer is executing all of this okay so we've drawn out this little com or this little triangle and you'll see here that on each line of code i have this text that's inside of these quotation marks now this is what's called a string so anytime that we have sort of plain text in our program we call it a string and that's generally going to be in between these open and close paren or these open and close quotation marks and so what i'm doing here is i'm telling the computer on each one of these lines to print out a different part of this pyramid so if we run this computer program now you'll see that the computer actually prints all of this out and this brings me to i think one of the most core concepts in computer programming which is the execution flow of a program so when your computer goes through and executes all this code right this code is written in dart the computer can understand how to execute that so when the computer goes and executes a dart program it starts here in this main function and like i said you don't need to worry too much about what a function is this is essentially like a container that we can put different lines of code so your computer is always going to start here in this main function and then what it's going to do is it's going to go down and execute each line of code individually so it's going to start with this line right here and it's going to print this out so it's going to print out all of this text now you can see that that's printed out over here onto the console so not only this sort of forward slash and the little bar character but also all of this space there on the side so whatever you tell the computer to print out it's going to print out so check this out if i got rid of some of these spaces here now our triangle is going to be all messed up because this is going to be over on the left so when your computer goes through it'll start with this first line of code once it's done executing that then it's going to come down to this second line of code and if we run this again you'll see so the computer starts up here and it executes this line and then the next thing it does is it goes on to the next line and executes that so your computer can only execute one instruction at a time at least in the code that we're writing right now sometimes you can have it execute multiple things at once but for right now the computer can only execute one thing at a time so the computer starts here and then it goes down here and then finally down to these last two so the order that you put these instructions in the order that you write your code in is very very important in fact it's probably the most important thing when you're starting off with programming is making sure that things are in the right order so for example if i was to take this line of code and i was to put it up here now you'll see that the program is all messed up right because the instructions are in the wrong order so the order of things like i said your first big lesson here is that the order is very important now one thing that you'll notice here is how i'm formatting my code and when we first start off coding we're generally going to be writing programs that are really small so the way that you format things isn't that important but as you get better at programming and as you start writing more and more code it's gonna start to get really messy if you don't order things and if you don't uh use things like indents here so generally whenever we have a container like this main function where things are inside of these curly brackets you always want to put a little indent and that just indicates that these print statements are inside of this container so you can use spaces or you can use tabs it doesn't really matter but that's another thing as well as you're going to see me formatting my code as i go it's something that you want to do as well okay so we've talked about some of the basics right so now we know how to have the computer give us information and we also talked about the execution flow of the program so we execute one instruction at a time now the next thing i want to show you is how we can start working with data when we have whenever we write computer programs we're always always always working with different pieces of data so if you're building like a website this could be like the users that are in your website if you have like a finance app maybe this is like different stock values or something but there's always pieces of data that we're going to work with in our programs and so what i'm going to do is i'm going to show you how to write a little program and we'll look at how we can start managing the data that's that's in our code here so what i'm going to do is i'm going to get rid of some of those print statements and i'm actually going to write a program that prints out a little story so the story is there once was a man named and why don't we call this man stanley so there once was a man named stanley now what i'm going to do is i'm just going to copy this line of code this is something that programmers do all the time so if there's a line of code that looks like something that you need you can just copy it and then paste it so here i'm going to paste this story down so the first line of our story is there once was a man named stanley and then we're going to say he was 70 years old so stanley is 70 years old and once again i'm just going to copy this and you can see sometimes when you paste things like all the tabs you get weird and then down here we're going to say he really liked the name stanley and then finally on the last line we're going gonna say and whoops okay so sometimes rebel will do that because we're connected to the internet here so it'll like mess up a little bit but that's fine so there was a man named stanley he was 70. he really liked the name stanley but he didn't like being 70. okay so this is our little story obviously like kind of a stupid story but you can see here if we run it now all this code is run out so this is you know if you want to write a program that just prints out like a little poem or a story this is exactly how you do it but i want to use this as an example to talk to you about different data that's in our program so if you look here at this program there's actually two pieces of data that we are keeping track of and maybe you can see what they are but i'm going to walk you through exactly what they are so you can see here that we have the name of the character in the story which is stanley and then we also have how many years old he is so these are two pieces of data that are really important to this story and that are repeated multiple times in the story and generally it's these pieces of data that we need to use in multiple parts of our program which we're going to need to take extra care of keeping track of so because the character's name is used multiple times and because it's so important to the story and same with the age these are two pieces of data that we want to i guess take extra precautions when we're working with them in our program and the reason is because let's say that in this program we wanted to change the character's name so instead of the character being named stanley maybe i wanted the character to be named oscar so i want to change the character's name from stanley to oscar but you'll notice that not only do i have to change it up here in this first line of code i also have to change it down here in this other line of code so there's two places here now where i have to change the character's name two places that i need to keep track of where the character is same thing for the age so let's say that instead of 70 oscar is actually like 35. so once again i have to change it here and i have to change it down there so there's two places now where i have to change the character's age and whenever you're in a situation like this when we're writing code and this is a perfect example because we're repeating these values whenever you're in a situation where these values are being used multiple times this is a good place to use what's called a variable and a variable is essentially a way that we can take the character's name and we can take the character's age and we can store them somewhere and then we can sort of reuse them throughout our program because the difficult part about this is that in this story it's very short right it's four lines so if i want to change the character's name i only have to change it in two places but let's say that the name oscar or stanley is in a thousand places in our program across multiple files this is something that's very common so if this name the character's name is in multiple places in order to change it we would have to go to all of those places and change it individually and this is not a good way to do things when we're talking about code so let's take a look at what we can do with variables now the first thing we want to do is store the character's name and i'm going to show you just the syntax for doing this and then we'll talk a little bit more about it but i'm going to say string and then i'm going to say name is equal to and then i'm going to make an open enclosed quotation mark and then in here we're just going to put the name of the character oscar now make sure that you end this off with a semicolon as well once again you're going to forget the semicolons everyone does it's just a part of learning how to code so we're saying string name is equal to oscar and what this means is that this string of text like i said anytime we have quotation marks and there's text inside of it we call it a string it's like a string of characters that's kind of where that name comes from so i'm saying that i want to store a string this string oscar in a variable which is called name and what's cool about this when we create this variable now if i want to refer to the character's name i no longer have to say oscar down here instead i can use this special syntax so i can put a dollar sign and then an open enclosed curly bracket and then in here i can put the name of this variable so this variable has a name of name and obviously maybe this is kind of confusing because i called it name we could call this character name so the name of the variable is character name and what i can do is i can do what's called interpolation so instead of having to write out like the text oscar i can just use this syntax and the program is automatically going to insert whatever value is stored inside of the character name variable here into the story so we can do the same thing i'm going to copy this and down here we have oscar as well and i'm going to paste that so now instead of typing out oscar we're just doing what's called interpolating the character's name so what i'm going to do now is i'm just going to hit run and take a look at this so you'll see here that the story still has the name oscar in both of these places so when the computer goes and executes this code let's say it comes down here to this print statement and when it sees character name right here it grabs the value that's stored inside of the character name variable and just puts it in there so check this out if i wanted to change the character's name uh maybe well it shouldn't be angela because it's a guy so maybe we can change it to michael and now i'm gonna hit run and you'll see here that now the character's name michael is updated in both of these places and i only had to change it in one place so unlike before when we wanted to change the name and i had to go through and like manually change it to oscar right in both of these places and i can't type this right instead of me having to go do that and then me messing it up because if it's in like a thousand places i'm definitely gonna mess it up instead i can store it in this variable and then the program will just take care of it so then when we wanna update it right if we change it to gym now you can see that it updates in both of those places so we can store the character name in here and like i said whenever you want to insert like the value of a variable inside of a string you can use this dollar sign and then open enclosed curly bracket notation we call that string interpolation because we're interpolating a variable's value into the string that's kind of like the technical definition so we can do that with our string we can also do that with our age now in this case the age 35 can also be a string right because it's just kind of stored in here and we're going to call this character age and i'm just going to make this 35 so we'll replace all the instances of 35 in here with the character age so 35 there and then 35 there so if i run the program you'll see that nothing has changed and here we go first time i'm forgetting one of these semicolons i'm definitely going to keep doing this throughout the video uh the thing with semicolons is that there's some programming languages where you don't need them and those are mostly the programming languages that i'm working in so i'm not like in the habit of doing it but hopefully i'm pretty good with that throughout the video so in this case you can see that nothing about it changed it's still it's still 35 but if i wanted to change it to you know 64 i just change it in one place and then it gets updated throughout the entire program so these are really uh what variables are okay these are little containers where we can store values and then whenever we want to refer to those values instead of referring to the value itself like gym we just refer to the variable name and then the computer will sort of like put that value in there and really the main reason that we do this is so that when we want to change the variable we only have to change it in one place instead of having to change it in multiple places down there cool now another thing that we can do with variables is we can reassign them so there's really two ways that we can work with variables we can initialize the variables and create them and that's what we're doing up here so when we initialize a variable when we declare a variable we're basically saying okay this is going to store a string it's going to be called character name and the initial value is going to be jim what i could do though is in another place in my program i could modify that value so let's say that down here in between the first part of the story and the second part of the story i wanted to change the character's name i could do that by just updating the variable's value so now i can refer to the variable's name character name and then i can set it equal to something else so now we instead of you know this being equal to jim we could set this equal to dwight or something like that and once again we're gonna remember to put that semicolon there so we don't get yelled at by the uh by the compiler so let's hit the run button now and you'll see that because i modified the character's name now when we start the story the character's name is jim and then when we're finishing the story the character's name is dwight so whenever we modify the variable any code that occurs after that is going to use the updated value and this brings us back to that first lesson that we learned which is the order of execution right it's very important that we put this code in the right order if i was to put this character name reassignment down here below the story now the character's name doesn't change to dwight it's only when i put it up here before i do this print statement that it changes so these are really like a couple core lessons right we need to make sure that the order of the program is sound and then we also want to use these variables to store those pieces of data now most of the time if you have a piece of data in your code that you want to work with or that you're interested in working with it's a good idea to store it inside of one of those variables now so far we've just been working with one type of data which is this text and text is kind of the most basic and i guess flexible type of data because you can just put anything in it so again we call these strings and they're just in between these open and closed quotation marks and you can put any characters that you want in here it doesn't really matter but there are other types of data that we can work with as well so i'm actually going to delete this little story here and we're going to talk about the different data types that we have in dart and really just in programming in general so there's three main data types in all programming languages that we can work with okay there's strings which is plain text so an example of a string we kind of just saw a couple but you know a string could be like you know first name right and i could set this equal to mic so this is a like a string of plain text we're going to put the semicolon in there and anything go in here so you could put like characters you could put numbers whatever you want to put in there you can put emojis if the language supports it i think dart probably does so you can just put any sort of plain text in there and that's a string it's the most versatile it's the most flexible data type that we have and probably one of the most common that you're going to be using there's also two other data types though there's number data types and then there's what's called boolean or true false data types so strings plain text numbers and then true false information which we're going to talk about the the true false stuff because that's a lot of times confusing so when we're talking about numbers in computer programming there's two types of numbers okay we have what are called integer numbers which are whole numbers so the counting numbers things like 1 2 3 4 5 negative 1 negative 10 whatever these are numbers that don't have decimal places we also have floating point or double numbers and these are numbers that have decimal places after them okay so there's these two types of numbers generally that we're going to be working with and this is for basically all programming languages so an example of an integer and you'll see here i'm going to create an integer variable so i'm going to tell dart what type of data i want to store in this variable so up here we said string first name is equal to mike down here i'm going to say int and a good example of like an integer would just be age so like in my case i'm 26 and this is a good example of an integer because i would always represent my age as the whole number i would never say like oh i'm 26.4567 you know years old or something like that i would just say that i'm plain old 26 it's like a counting number these could also be negative if you wanted to be negative that's fine and then we also have what are called doubles so i could create a double variable and a good example of a double would be like if you have a gpa like a grade point average or something like that so maybe your grade point average is like 3.5 so this has this decimal place after it and the reason that we have to distinguish between an integer and a double and i guess i don't know exactly why we do this in dart but generally it's because they're stored differently in the memory of our computer so a string is stored different than an int which is stored different than a double and so when we tell dart exactly what type of data that we're using uh dart will go ahead and be able to store that correctly and and use it so that's that's generally why we we would do this okay so strings integers doubles and double doubles can also be negative doubles could also be like .0 but it couldn't be like this so you always need to have the the decimal place after it and then the final data type that we're going to be working with in computer programming is probably the least intuitive to a beginner which is called a boolean so if we wanted to create a boolean we could just say bool and booleans are true or false values so these can only have two possible values strings can have basically an infinite number of values right infinite number of combinations of characters whatever numbers also pretty much could have an infinite number of values right booleans though can only have two values true or false and this boolean data type is like the most important data type in all of computer programming it's actually what all computing logic is based on if you get to like the you know like we're talking about like different logic gates and stuff in the processor of a computer but a boolean is true or false so a good example of a boolean piece of data would be like is registered voter so maybe we're writing a program that keeps track of like who's a registered voter in your country and so this could either be true right they're a registered voter or they're false so there's no situation where they're like neither of those things like they either are registered to vote or they're not there's no middle ground where they're like kind of registered or whatever this is a a boolean uh you know a boolean value right another thing you could you know you could also say like is above 18 or something like that right maybe this is for like drinking in your in your country whatever so uh is registered voter that's fine and a lot of times when we name these variables you'll start with like is or something like that just as kind of a convention so these are the like i said the three main types of data we have strings we have numbers and there's two subset two different types of numbers and then booleans as well now all of these values can be printed out over here so if i wanted to i could print and you remember when we printed before we just printed like some text that was inside of here but anytime that you have a variable and you can see here already forgot the semicolon but anytime that you have a variable you can also print that out as well so i could print out like first name we could do that over here you'll see that that prints out i could print out you know gpa whatever and then we could also print out is registered voter cool so we're storing data inside of these variables and it's so that we can reuse it throughout our programs and when we want to change it we only have to change it in one spot so this is really like the core basics of computer programming right we talked about the order of execution you know how we execute this line before we execute this line before we execute that line we're storing data and then we're using it throughout our programs so these are all like some really important concepts now the next thing that i want to do is i want to dive a little bit deeper into some of these different data types so we're going to dive a little bit deeper into strings numbers and then also booleans and we're going to look at how we can use them in different ways now keep in mind that when we're writing computer programs there's a lot of things that you have to learn before you can start writing some actual cool and like interesting programs we're actually going to write some cool stuff as we go but just know that as we're introducing these concepts like there's just some prerequisite knowledge that we need to get out before we can start like really building some cool stuff okay so with that said i want to start talking about strings so let's just go ahead and delete all this stuff and let's create a string which is a variable so i'm going to call this greeting and i'm going to set it equal to hello so this is a pretty common greeting now the first concept that i want to introduce to you with strings and actually we're going to print this out too i'm not going to forget the semicolon the first concept that i want to introduce to you is this concept of an index so whenever we're talking about strings each character in a string has what's called an index and a string itself has what's called a length now let me show you this really quick so uh i'm going to put what's called a comment here so if you put two forward slashes in your code this is actually a comment so anything that you type in here you can see it's kind of greened out the computer is just going to straight up ignore this so anytime the computer sees a comment it just ignores it and i just want to use this to kind of notate so when we have a string each character in this string is going to have a particular index and those indexes are going to start at zero now this is a little weird and if you've never coded before this is something that's going to be kind of foreign to you but whenever we are writing computer programs whenever we're counting things we always start from zero it's just kind of something that we do so the first character this h is going to have an index position of zero so we would say that the zeroth character in the greeting string is this h the next one is going to have a 1 2 3 and 4. so this first l is at index position 2 in the string this o is at index position 4 in the string it's just something that you kind of need to get used to thinking about so whenever we talk about like the first character we're talking about index position zero and this is the source of like just so much confusion and so many like bugs and beginners with beginners who are writing code like this but just you need to keep this in mind that if i say like oh the character at index position 1 i'm actually referring to the e not this h the h is at index position 0. so just something to keep in mind something to wrap your head around as we go forward here so each character has an index position but the entire string itself also has a length now here's another thing that's just going to throw you off a little bit so the length of the string is actually one more than the last index position so check this out i have this greeting string and i'm actually just gonna put this on two lines here but i i'm gonna print this out now i could say greeting dot length just like that and if i do this check this out i'm going to run the program and you'll see here and actually sorry we need to remove these open and closed parentheses so i'm going to run this and you'll see here that we print out a five so i say greeting dot length and it prints out a five so one of the things that we're interested in a lot of the times with these strings is how many characters they have now you'll notice that the length of the string is different from these index positions so the string technically has five characters right one two three four five it's just that the last character is at index position four again this is just one of those things that we you kind of just need to get used to as a new programmer it's you just kind of need to wrap your head around it but we have these index positions and then we have the length and the length is always one more than the last index position so when we're counting the length we start at one two three four five so that's where we get five over here but when we're talking about the index positions we start at zero so we say zero one two three four okay once again you'll get more comfortable with this as you learn more about coding and as you write more code but that's kind of like the first big lesson that we need to learn so this length is what we call a property of the string so this length tells us information about the string and a lot of times when we're writing code we're going to want to be able to get information about the different strings that we have especially if we don't necessarily know especially if we're not writing them out just like this maybe they're coming from a user or something now if you want to refer to a particular character in the string you can do that using this open and closed square bracket syntax so you can say greeting and then an open and closed square bracket and then inside of here we're going to put an index position so if i put a 0 in here check this out it's going to print out the character at index position 0. so we'll run this and you see that it prints out the h right there i could also say print out the character index position three so that's going to be this second l over here or we could say four and that's going to end up being the o so once again when we want to refer to one of these individual characters we refer to it using its index if i was to put a five here and i ran this code you'll see that we get this exception and that basically means that hey we can't print out the fifth character because the last index position is four so the computer doesn't know where to look for that okay so that's a little bit about accessing a specific character sometimes we're going to want to do that it's just a good idea to be able to to be able to grab one of those now in addition to being able to get the length and being able to get individual characters we can also use what are called string functions we're going to talk a lot more about functions in you know in this video so don't worry too much if you don't understand what that means but a function is essentially like something that the string is able to do so what i could do is i could say greeting and then i can say dot and here i'm going to type in the name of a function that we can use with this string and one of the most common is to uppercase so you're going to type out grading dot to uppercase and then an open and closed parentheses anytime you see this open and close parentheses it means that we're like calling a function basically so i'm going to click run here and you'll see what happens is this hello string down here is converted to be all uppercase so we've taken all these characters and we've converted them entirely to uppercase you could also do two lowercase that'll make them all lowercase um so these are ways that we can take this string and we can sort of modify it right so we can we can change it up and so now this is a completely lowercase and there's a couple other of these you can actually just google like dart string functions and a bunch of these things should pop up these two are really common and also i just wanted to show you like just how that works but there are some other ones as well that are pretty useful and i just want to introduce these so another thing that we can do is we can figure out if a character is inside of this string and at what index position it is so i could say grading dot index of open and closed parentheses and then in here i'm just going to put a particular character so if i put an e in here check this out when i run the code it's going to say 1. now what this means is that when i run greeting.index of it's telling me okay the e is at index position one and so now i know where the e is in the string so i can do different things with that and this comes in handy a little bit but this is just like a really common thing that you'll see developers doing which is trying to figure out like at what index position a particular character is so i could also put like an l in here and you'll notice that there's two l's in hello so if i put an l in here it'll just return the position of the first l which is that two right there so it'll always return the position of like the the first so that's a little bit about index of that one's really useful there's also another one which is called contains and contains is going to allow us to check to see whether an actual character or a string of characters exists inside of here so i could say contains and in here we're going to say contains and i could put for example an e here now this is different from index of because instead of telling us the index position of the e it's actually going to give us a boolean value back and this isn't like one place now where you're seeing where these booleans are coming into play so i'm asking does the greeting contain the letter e and in this case we get a true back but for example if i put like a seven here now you'll see that the greeting doesn't contain a seven so then we get false so those are just like some different ways that you can do that and we might look at some other ways of using contains uh later in the future in fact i think we definitely will cool so that's just some basics about strings there's a lot more things that we can do with this there's a lot more that we could talk about with strings but it's kind of the main basics and really what you want to do is just kind of start playing around with with some of these different things so the last thing i want to talk about with strings is something called concatenation and then also something called interpolation which we've already looked at a little bit so strings are very unique because we can actually add them together and this process of taking one string and adding it to another string is called concatenation so check this out i could create another string variable over here called greeting2 and i'm going to set this equal to world and i'm going to put my semicolon there i could do this i could say greetingplus greeting2 and i'm actually going to put a space here and if i run this code you'll see that now we're printing out hello world just like that so we have this first string hello which is getting printed out there and then we have the second one that has the space and then world that's going to print out after so we're able to like add those two together we call that concatenation okay another thing that we can do and we already saw this is called interpolation and interpolation is where we sort of insert a variable's value into an existing string so here i could say the greeting is and then i'm going to make this dollar sign and open and close curly bracket now this is all taking place inside of these quotation marks i can put the name of the variable right so i could even say like the greeting is and then we could do like concatenation here so greeting two and so now the greeting is hello world this is actually interpolated into here and we could get rid of greeting two if we wanted to that doesn't really matter but you get the point so there's different ways that we can add strings together we can concatenate them this is really really really really useful for when we want to like format out a line of text or something like that with variables in it but really i think those are the the main kind of core concepts of working with string so probably the most common data type that we're going to be using any sort of plain text you know usernames whatever it's all stored using strings so these things are really good cool so i think that just about wraps up what i wanted to talk about with strings and so now let's go on to our next data type which is numbers so like i said we have two different numbers two different types of numbers we have doubles also sometimes called floats and then we also have integers called ins so these two numbers you can work with them almost exactly the same way it's just that one of them has a decimal point and the other one doesn't so let's go ahead and create a couple of different numbers here so i'm going to create an integer why don't we call this quantity and i'm going to set it equal to 300 and then i'm also going to create a double so this is a number with a decimal point and why don't we call this price and the price will be like 5.99 so maybe we're writing a little app for an e-commerce site and we're storing the quantity of items that we have inside of an integer and then we're storing the price of each one of those items inside of a double pretty common use case right quantity is an integer because we're not going to have like half of something available we're just going to have it in in full increments now when we're working with numbers and first of all i can actually just show you this without these variables when we're working with numbers we can do all the main things that you would expect to do with numbers so we'll leave these variables alone for a second and i'm just going to show you you could do any type of math so here i'm just printing this out just so we can see the result but i could do any sort of arithmetic that i want so i could do like five plus three we would get eight you could do subtraction we could also do multiplication and that's going to be using this star character and then we could do division which is going to be a forward slash so all the four main math operations are in there you can also do things like order of operations so once again i'm just going to put this on kind of its own line so you could say like five plus three times you know i don't know two divided by seven or something like that and the program will automatically do like the order of operations that you would expect so it would do like anything that's in the parentheses then it would do the multiplication and then finally the addition so like whatever numbers that we're using in here uh the dart will be able to handle that like it can do all the main stuff that you would expect another thing that we can do is something called the it's something called the modulo operator so there's this operator in programming we call it the modulo operator also commonly referred to as like the remainder operator and there's some cases where you'll see people using this so you could say so for example if we said 10 divided by 3 right 10 divided by 3 this is going to be 3.333 repeating etc now one thing you'll notice is that when we have two integers and then we divide them we actually get a double back just something to notice but the modulo operator is basically like the opposite of what's not the opposite of division but it's like a complement to division so the modulo operator is going to tell us what the remainder of something is so i could say 10 and then i'm going to put this percent sign so we would say this is 10 mod 3 and you'll see here that we just get a one back so 10 mod 3 is gonna tell us what the remainder is so three goes into ten three times right three six nine with a remainder of one and that's what we're getting back here which is that remainder so module operator is something to play around with it's not super intuitive when you first see it but it's it's used in in programming in i would say like some cases so it's definitely worth being exposed to okay so we have our integer and we have our double and these are both stored in variables and i'll show you some other ways that we can work with these as well so you could do math just on these values so you could say you know quantity divided by 2 or something we should get like 150 back right so we can we can do all the same math stuff with these variables but specifically with variables is we can like increment them or change their value so let's say that instead of making this 300 let's say that for our ecommerce store we just got like 50 new items in so we want to increase the quantity by 50. one way i could do that and i'm just going to print the quantity down here one way we could do that is we could say quantity is equal to quantity plus 50. and this is pretty common so we saw before with the story how we can change the value of a variable so we can re we call this reassignment right so i can say the value of quantity is now equal to the current value of quantity plus 50 and this will give us should give us 350. you can see that over here but there's an easier way that we can do this and it's another syntax that you'll see people using so if you're doing this thing where you want to add 50 onto like the existing value you could just say quantity plus equals 50 and then it'll do the same thing so this is like just kind of a shorthand that you'll see people use and you could do subtraction there too and i'm pretty sure you can do multiplication as well so this would give us yeah like 1500 and then you could do division two so those are different ways that we could just go about like you know adding a value to the existing value you'll see people do this a lot another thing that we can do is you can add one to these and so if we wanted to add one we could say plus equals one right so now the quantity is going to be 300 but there's a shortcut for this as well this is really common in programming so you could just say plus plus so quantity plus plus what this is going to do is it's essentially doing the same thing as quantity is equal to quantity plus one or quantity plus equals one same exact thing so we just say plus plus and we run this and boom we get 301 so once again we're not really writing any like interesting code here all i'm trying to do is just kind of expose you to some of this like syntax that you're gonna see and the syntax is just like the the way that these instructions are written so these are all different instructions that dart understands i want to expose them to you a little bit so that as we go throughout the video and we start doing more advanced stuff then you know you at least have some reference point okay now in addition to being able to do all of that stuff and you can do all of these with doubles too it's the same thing there are some more like i guess advanced math things that we can do with dart as well but they're actually not going to be in here by default so dart has something called a math library you're going to hear this term a lot in computer programming a library it's essentially just a collection of code just like a library is a collection of books or you know normal library a coding library is a collection of code or collection of little like functions basically just a collection of code that lets you do things that you normally wouldn't be able to do so we're going to actually have to import this now this is going to be some kind of funky syntax here but you're going to say import and you'll notice that this is outside of our main function so this is actually going to be up at the top of the document you want this to be the first thing that you put up here so you're going to say import and then inside of quotation marks we're going to say dart colon math and what we're doing here make sure you put your semicolon what we're doing here is we're saying okay i want to import all of dart's math functionality into my code and when we put this line up here now there's going to be we're going to basically have access to a bunch of different like mathematical functions that we can use and i'll show you some of those so we can do this down here in this print statement just so i can print this out but now i could say uh for example one of the things that we're able to do now is we can use the min function so min right here you can just type out the name min and then an open and closed parentheses and actually so maybe some people are confused by this why i'm not putting a semicolon here this is actually going inside of this little print statement you can see this is inside of these parentheses so we don't need a semicolon i don't know if that's confusing anyone um and you know you'll get more comfortable with this with this syntax as we go but the min function is going to take uh two values basically so we're gonna put two numbers in here i could put like a one and then i could put a nine and you'll see that i'm separating these with a comma and if i hit run this is going to tell me which one of these is smaller so you can see here one is smaller like if i change this to 100 now it's going to give me 9 back so sometimes this is useful you'll see people using the min function there's a couple other ones too there's like the max function this does the opposite so it'll tell you which one's the maximum there's another one which is called square root sqrt and in here you can put a number like 144 it will give you the square root so we're going to get 12 back et cetera so that kind of works now once again we need to have this line of code up here in order for this to work so i'm going to delete that if we hit run now we should get an error it says method not found method's another word for function so we're going to put that back now i'm going to put this back if you want to undo some code that you wrote you can use command z if you're on a mac or control z if you're on a windows and that's kind of a useful thing like a lot of times you know you'll delete code by accident you want to get it back just command z control z okay so i think that's everything that i wanted to talk about with numbers now i want to spend just a little bit of time talking about booleans before we move on um we're going to talk more about booleans later when we get into conditionals these things called if statements but i just want to show you really quick just a couple of things about boolean so remember a boolean is like a true or false value so it's either true or it's false and sometimes you're going to want to use these explicit values you're going to want to type out false or you're going to want to type out true but there's actually some other ways that we can get variables or sorry get booleans which are called boolean expressions so once again i'm just going to make a new line here so we can see this easier so you could put true in here and you can see that true pops out over here but you could also put like a comparison so you could say for example 5 greater than 3. now in this case 5 is greater than 3 so you'll see here that we also return true you could say if we said 5 is less than 3 now this is going to return false because this is false so we would refer to this as a conditional or a i don't know this would be a comparison or a um a boolean expression that's another way that we that we could call it but i just want to show you that so there's you know you could type out true but it true is equivalent to something like five greater than three or anything like that so you can do like greater than you could do greater than equal to you could do equal to which is like these uh double equal sign etc so we'll get a little bit more into these later but i just wanted to point that out really quick okay so the next thing we're going to do is we're actually going to start writing some programs that are a little bit interactive like so far we've just kind of been talking about the different data types different ways we can work with these just to familiarize you with some of this syntax get you comfortable writing code now i want to show you how we can write programs that are taking input from us as the user and then like giving us output so this is when our programs are really going to start to come together and you can start like building some pretty cool stuff which we're going to do so i'm actually going to leave this line of code up here we can just leave that there it's not really bothering anyone but i want to show you how we can get input from the user so so far all we've been doing is having the computer tell us things right we're having the computer tell us a string or tell us some you know equation or whatever like the computer's printing things out at us now i want to show you how we can give the computer information so that the computer has like different data that it can use and the first thing that we're going to do is we're going to import something called dart colon io and you'll see this a lot in computer programming io stands for input output so this is the you know the way that we interact with the computer right i give the computer input whether it's through like clicking a mouse typing text in whatever clicking a button and then the computer gives me output in the form of like text like this or something else so this is like low level input output that we can do in in the dart programming language now we're going to write a little program that just says hi to the user so this is like the most basic sort of getting user input program that you can write so i'm going to make a print statement here and this is going to be a little prompt so i'm going to say what is your name and really what i'm doing here is i'm sort of prompting the user to enter in their name so if we run this code and you'll see here it just says like what is your name but now what i want to do is i want to be able to have the user type in their name and then be able to say hello to whatever their name is this is like you know kind of the most basic thing that we can do and the way that we can get user input is by using something called the standard input read line sync and you're going to see how this works in a second but anytime that we're trying to get input from a user we always want to store it in a variable generally we always want to store it in a variable we talked about how variables are like a great way for us to organize our data and for us to store values and so when we get a piece of information from the user we want to store that so i'm actually going to create a string now any information that we get from the user from this little console over here is going to be in the form of a string so it's always always always going to be stored in a string like this so i'm going to call this string and we'll call this username and i'm going to set this equal to and now there's going to be a little bit of funky syntax here so i'm going to set this equal to s-t-d-i-n so that stands for standard input dot read line sync just like that now this code obviously what's going on here this is a little bit confusing this is essentially the line of code that we need to write so that dart is able to like get information from the user so when we when we put this instruction here we're telling dart that we want the user to be able to type something in and what's going to happen is the user is going to type something in and then we're going to store whatever they typed in inside of this username variable so let's go ahead and run this code and just see what happens so you can see it says what is your name but you'll see that the program kind of stopped executing here right we didn't get any indication that the program was done running the program is almost like halted now if you come over here on the console you'll see that you can actually start typing in here so if i for example type out my name mic like that i can hit the enter key and you'll see here that the code actually finished running so whenever we do this standard input read line sync this essentially pauses the execution of the program allows the user to enter something and then when the user hits the enter key or the return key then the computer continues on executing so what we could do now is if i was to print out and we can use string interpolation here we could say hello and then in here we could say username so i'm just printing out the you know hello to whatever is stored inside of this variable now if i do this it says what's your name i type in mic and now it says hello mic so we get this input from the user remember the program's execution stops at this point and waits for us to type something in and hit enter and then it stores that value as a string now it always always always stores it as a string inside of this variable called username and then we can print that out and use that okay so this is the basic flow of how we can go about getting input from a user we're always going to do it just like that now in this case we're getting input from this console over here you might be thinking like oh that's kind of lame like i want to be able to type into like a search bar and stuff like that there's other ways that we have to go about getting user input with like search bars and graphical user interface elements but in this case we're just working here on the console cool so now that we have a basic understanding of standard input and how we can get values from the user i want to show you guys how we can build a basic calculator this is a really really good first program and this will be kind of our first like real kind of program that we're gonna build and you'll be able to see how it works so we're gonna build a calculator that's like i said very basic it's capable of adding two numbers together so we're gonna get a number from the user and then we're gonna get another number two pieces of input and then we're going to add them together and print out the answer to the to the user okay so let's go ahead and do that why don't we just start completely from scratch because sometimes it's it's a good practice to like rewrite all this stuff just get your brain working so the first thing we want to do and anytime we get user input we always want to do this is we want to prompt the user so i want to tell the user like what piece of information i'm looking for once again we're building a calculator so in here i can say enter first number and i'm going to put a colon there and what we want to do then is we want to allow the user to enter their number and then we're going to like you know grab that number and store it in a variable and so once again any time that we're like getting input from the user it's always going to come in as as a string so we're always going to get it in the form of a string even if they enter in like a 4 or a 5 or something so we're going to call this num1 and i'm going to set this equal to the same thing that we did before so standard input.read line sync and i'm going to make an open and closed parentheses good so now we should have that first number that the user enters stored inside of the num1 field and if we want we can just print this out real quick and i forgot a semicolon here it's going to yell at me so we'll go ahead and just print out num1 we'll kind of run this program okay so now i'm gonna go ahead and enter the number so if i entered like a four for example you'll see there that it just kind of prints out the four so this is actually working and we're gonna do the same exact thing for the second number so i can actually just copy all of this code and i'm going to paste it down here now check this out we'll just say enter second number and then we'll store this as num2 so we've now gone ahead and gotten the input for the first number and then we've gotten the input for the second number we've been able to store both of those in variables right num1 and num2 and then the last thing we want to do down here is we just want to print out to the user the result of adding those two things together so i'm just going to say num1 plus num2 so we're gonna print out both of those and then we'll also remember to add that semicolon at the end so we're gonna run this and let's go ahead and enter the first number so maybe we'll put a five and then we'll enter the second number maybe a ten and now it should add them together and print out the result now what you'll notice is that it didn't actually do that so 5 plus 10 is not 510 and what ended up happening is remember num1 and num2 are both stored as strings and we talked about this before whenever you try to add two strings together it does what's called concatenation so it takes the first string and then it concatenates the second string onto the end of it this isn't ideal right this isn't going to work for us as a this isn't going to work for us as a calculator and this brings us to another really important point which is called integer parsing or double parsing basically what we want to do is we want to be able to take a string and we want to be able to convert it into a number so there's a lot of situations like this where we'll get like a number for example from the user and then we'll want to be able to you know but it's stored as a string and we'll want to be able to convert that into a number so let's go ahead and look at how we could do that so uh in order to convert a number into a string there is a method that we can use or a function we can use so i could actually say int dot parse and then inside of an open and close parentheses i'm going to put this string that i want to parse a number out of so if i say in dot parse num1 this is actually going to take num1 and it's going to convert it into an integer so whatever number for example the 5 that i put here it'll convert that into a number that we can then add to another number so i could do the same thing over here in dot parse num2 now if i run this code check out what happens so we'll enter the first number five we'll enter the second number ten now when i hit enter what's going to happen is we're going to convert this 5 which ended up coming into our program as a string and then we're going to convert this 10 which comes into our program as a string into integers and then we'll be able to add them together and then we'll be able to you know use those to do math now you can do that with integers you could also do that with doubles so down here now we could add in like 5 4.5 and we can add in you know 20.9 or something like that it'll add both of those together now the one thing about this is that you have to enter in an actual number so if i was to put like hello as the first number and then world is the second we're going to get this exception because we can't convert hello into a double we can't convert world into a double okay so this is a really really really common thing that we do in programming is we take one data type and we convert it into another data type most of the time it's strings to numbers sometimes you'll do numbers to strings so this code is good and down here we're like using double dot parse that's awesome we could actually shorten this code though so i could make this a little bit more concise and i'll show you how we can do that so instead of storing these as strings initially what i could do is i could actually store these as doubles and i could do this conversion over here on the same line so i could actually say i'm going to copy this and whoops i could say double dot parse and then inside of parentheses i can put standard input dot read line sync now as you can see we're having all these parent like there's all these parentheses here like as a beginner this might start to get a little confusing but it is good to see this because this is going to be really common so then we can do the same thing over here so i'll say double dot parse just like that and then down here when we print them out i could just say num1 plus num2 like that and then when i run if we did you know 40.5 or 20.9 now it'll be able to add those up and it's just like a little bit more concise so we're essentially doing the conversion right after we get the user input okay and that's basically how this is structured here so that's going to allow us to do that cool so that's our basic calculator and you know you could play around with this so if we wanted this to be subtraction we could just add the subtraction thing in there whatever it would be able to do it just the same so you can do whatever operation you want actually later on in the course we're going to look at building this where the user can specify which like addition subtraction multiplication whatever they want to do but for now that is our first little basic calculator and that should give you an idea not only of how to get user input but also from how to convert for example a string into a double okay so play around with that if you're still a little confused on it but otherwise i think we're going to move on so the next thing that we're going to do is we're going to build a mad libs game so this is another thing that we're going to do with user input i really want to kind of like hammer this down because this is a really core concept when we're talking about dart which is like getting input from a user and then being able to parse through it that's like half of what we do in computer programming so we're going to build a mad libs game if you're not familiar with mad libs i would recommend looking it up it's a fun game basically you write down like a list of words that could be kind of just random or anything like adjectives verbs nouns colors names whatever you write those down kind of blind and then mad libs will like sort of intersperse them into a little story which kind of ends up being funny i don't know a lot of people like mad libs i i used to love them as a kid so i'll show you how we can build a mad libs game right here in dart from our program so with the mad libs game there's two parts to it right the first part is we need to be able to get words from the user so i need to be able to be like hey i need a noun i need a name i need a color a verb whatever and then the second part is we need to be able to sort of intersperse those random words into a story so the first thing that we need to do is obviously like get the information from the user so we need to be able to say hey i need a noun and then get that noun from the user so we should already kind of know how to do this and if you want to try to do it on your own that's fine but i'm going to walk through all of the steps so for our madlibs game we're going to get a color we're going to get a plural noun and we're going to get a celebrity so those are the three things color plural noun celebrity so let's start with the color i'm just going to create a little prompt here so i'll say enter a color and then down here we're going to get that input from the user so i'm going to store all these as strings we don't need these to be numbers so we can just use strings but i'm going to call this color so once again i'm creating a variable called color which is going to store a string and i'm going to set that equal to the user's input so we're going to say standard input dot read line sync okay so now we're able to get the color from the user we're storing it in this color variable i'm going to repeat this process for the plural noun and the celebrity so here we can say enter a plural noun and this will be plural noun cool and then once again i'm just going to paste this down here you should get in the habit of copying and pasting code like this is a perfect example of when we want to copy and paste i don't want to have to write all this out again um and since i know this is written correctly it's just easy for me to copy it and paste it so then finally we're going to say enter a celebrity and then we're going to store this in celebrity okay so we have our color our pronoun and our celebrity we're able to get that info let's go ahead and test the program real quick so we can put like blue plural now might be waffles and then celebrity is like tom hanks so we have all of those things that looks like it's working we didn't get any errors that's only step one though so the second step to building this mad libs game is we have to write out our little story and sort of intersperse each of these in there so you can make up your own story if you want i have one written down so this is a classic roses are red right and then we can say vi oh let's are blue and then down here we can say i love you right so this is our little poem like this is kind of a classic poem but what i want to do is intersperse the color the plural noun and the celebrity in here so instead of red we're going to put the color so we're going to use string interpolation instead of violets we are going to put the plural noun okay and then instead of i love you it's going to be i love the celebrity so i'm using string interpolation which we've talked about in order to put these variables into these print statements and now if i run my code so i'm going to enter a color so we could say like i don't know roses are whatever yellow so yellow is our color the plural noun might be pancakes and then the celebrity could be bono so now you can see we have our little story it says roses are yellow pancakes or blue i love bono so this is kind of our little mad libs game and hopefully now you can see how we would do that so once again there's two parts to this we get the information from the user and then we use that information inside of the mad libs now if you're working on this on your own you can make up your own mad libs you can make this even longer you could have the user enter in like a bunch of different words that could be kind of fun but that should at least give you an idea of how this would work and then also give us some more practice with the getting user input okay so that kind of wraps up the getting user input section we're going to come back to this we're going to keep getting input from the user this is like the whole you know flow of these programs is the user enters something in and then we use that to do something so we're going to come back to this but that hopefully gives you a nice idea of how it works the next thing that we're going to talk about is a really really really core and important concept in computer programming which is this idea of storing multiple pieces of data so we've seen how we can store a single piece of data like a single string or a single number right so we have these variables and the data type is a string right so i can say like name is equal to mic i can store this in there in this case though we're only storing one piece of data inside of this variable but in a lot of cases we're going to want to store multiple pieces of data inside of kind of the same container and when you have a situation like that where you want to store multiple pieces of related information inside of the same container we're going to want to use what's called a list or an array so the words list and array i guess they technically mean different things but they're pretty much interchangeable in the context of computer programming so if you hear someone talking about a list or you hear someone talking about an array they're generally talking about the same thing and this is what we call a data structure so the list is our first big data structure that we're going to be looking at it's also the most basic data structure in all of computer programming it is a structure which can store multiple pieces of data so instead of just being able to store one string it would be able to store multiple strings sort of side by side so we're going to look at how we might be able to create an array like that and i'll show you how it works so what i want to do is i want to create an array or a container a data structure is what we call it that's able to store some of my favorite numbers and this is going to look a little something like this so if we wanted to store a normal end right i could say my fave num is equal to like four or something like that i would just say int here but if i want to store these in a list or an array instead i need to say list and then an uh sort of a less than sign and then a greater than sign and then over here i can call this fave nums and the syntax is an open and closed square bracket so in here i could put some of my favorite numbers so like 4 8 15 16 23 42 so maybe these are like all of my favorite numbers and you'll see here that i'm storing them inside of this list so i'm making this open square bracket this close square bracket and then each one of these values is sort of delineated using a comma so 4 comma 8 comma 15 comma 16. and the data type over here is a list so we're saying that fave nums is going to store a list of integers and this is generally how we want to how we would want to structure something like this out okay so i'm explicitly telling dart this is going to be a list that has integers in it it's called favorite numbers and it has all of these things now you'll hear me using the word list or the word array pretty much interchangeably i've taught a lot of different programming languages some programming programming languages call these lists others call them arrays just know that it's the same thing in this context you don't have to worry i just might you know make a slip of the tongue and call it an array now what we can do with these arrays is we can work with them just like we worked with some of those more primitive values like strings integers and booleans so i could actually print out one of these so if i printed out my fave nums array over here you'll see that the computer just kind of prints out that structure now just like strings these arrays also have index positions and those index positions also start at zero so we would refer to the first element in the array as the element at index position zero the second one is index position one this is two this is three four and then five so even though this is the first element in the array it's at index position zero even though this is the second element it's it's at index position one etc okay so this is very similar to those strings i'm just going to put this on a new line and so just like with the strings we can also get a length property so i could say fave nums length and this is going to tell me how many elements are inside of it right so we have six elements technically right one two three four five six it's just the last element is at index position five and just like strings as well we can refer to individual elements just like this so i could say for example array at index position 3 and this is going to be the 16. so another thing just like with strings we need to get comfortable using these index positions which start at zero so the fourth element is actually at index position three etc cool so all of that like we kind of seen before with strings it's very similar you could actually almost think of a string as like an array of individual characters there's a lot of parallels there but another thing we can do with arrays is we can reassign these things so for example i could come down here and i could say fave nums at index position 3 and i could actually change it so i could change it from 16 to like 17 or something like that so if i do this and then i go to print out fave nums 3 you'll see that it actually changed to 17 and if we print out the entire array i've actually modified element at an x position 3 so that it's 17. so anytime you want to access one of the elements that's inside of the array you do it like this and you can reassign it you could also use it in operations so i could say for example fave nums 3 plus fave nums 0 so that's going to end up being 17 plus 4 because 4 is at index position 0. and then we should get 21 out here okay so with these arrays yes they're storing all these elements side by side but they're also a we're also able to access individual elements as well and that's just like super common something that you're going to be using all the time when you're coding okay so we talked a little bit about the index positions about the length and about accessing individual elements there's also some other things that we can do so for example i could say fave nums if i want to add an element to this i could say favenums.add and then in here i can put a new element so let's say i wanted to add in you know like 64 into this right what this will do is it'll essentially add this element onto the end of the array so if we print out fave nums you'll see instead of ending at 42 now it ends at 64. so we've sort of appended this element onto the end once again this is super super common super useful there's some other things that we can do as well though you could also remove an element so if you wanted to remove a particular element you would just have to refer to that element so i could say here i want to remove element 16. that'll actually end up removing this from the array so now if i was to print out the array you'll see that we no longer have 16 there that's been removed and there's some other more i guess complex ways that we can go about removing things from an array but this is definitely the most common we call this removing by value so whatever element has the value 16 that's what gets removed and you can play around with these you know create your own array and do all sorts of stuff but i think that's probably good for that now there's a couple other ones that i want to show you and these are pretty similar to the things that we could do with strings so just like with the string we can also say index of and in here we could put an element so i could say like 15 for example and this will tell me at what index position 15 shows up so we should get 2 back right because 15 is at index position 2. now one thing to note is that if you put an element in here that doesn't exist in the array so if i put a 40 in here like there's no 40s in this array it's gonna give me a negative one back and this negative one is just an indicator that it doesn't exist in the array that kind of lets us know so you can say index of that will give you that index back that's pretty common and then another one just like with strings is contains so i could say contains 15 and because the element 15 is in here we should get a true back if i was to say you know 18 though that's not in there so then we should get false back okay so these are the same methods that we used with the string these are the most common and it's it's very similar to strings now you can have a list of integers you could also have like a list of strings you could have a list of booleans whatever you want to have but you just have to make sure that they're all added in over there cool so that's a little bit about arrays we're going to get more into using arrays a bit later but like i said this is the most common uh data structure so this idea of a data structure it's a it's a way that we can structure the different data in our application so that it's more organized the array or the list and i'm using those terms interchangeably those are the the most common okay cool so the next section then we are going to be introducing a another just like super core concepts in computer programming which is called functions and you've heard me talk a little bit about functions in the past so we talked about how like this main thing this is actually a function okay we've also talked about how something like contains this is a function so what are these functions like what can we do with them and how do they work now before we get into this i do just want to make another disclaimer a lot of times in these programming languages we have multiple words for things so just like with arrays and lists how they're kind of the same basically the same word in some programming languages there's a distinction um the word function and the word method are essentially interchangeable there is like a technical difference between the two but in this context if you hear someone say method or you hear someone say function they're pretty much referring to exactly the same thing okay so just so you have that in mind like if i use the word method instead of the word function i mean exactly the same thing okay just like a little terminology so what is a function well a function is a collection of code which can be reused multiple times and this is just so foundational to computer programming we have this concept in computer programming called the no or what is it called the don't repeat yourself principle um and they call like the dry principle don't repeat yourself dry and it's basically this idea that if you write code that can be used in multiple places you only want to write it once and then you want to reuse it throughout your program so i'm going to show you an example of this real quick and we'll kind of build this up slowly so we can get a feel for these different functions now if you remember before we had this little triangle that we printed out so i'm actually just gonna just real quick kind of do this we'll make this kind of a really tiny triangle so we can print out a little triangle here and then this guy is going to go there and then we can have some underscores okay so this is like a really basic version of that triangle that we did in the beginning of the tutorial so if i print this out i get it here and let's say though that instead of printing out one triangle i wanted to print out like five triangles so i don't just want one i want five so what i could do and this is all we've learned so far is i could just do this five times right so print this out five times it's obviously not formatted correctly but i could run this and now booyah we got five of these triangles that are printed out now this is okay and it works right i mean technically we fulfilled the goal right i mean we printed out five triangles but look at this code i mean first off not only do we have five times the amount of code that we had with this first one but we're also repeating ourselves we're using this these same two lines of code over and over again and so once again just like with those variables imagine that i wanted to change the way the triangle looked maybe instead of being a triangle i want it to be like a square now so i wanted it to look something like this you know whatever like maybe this is the new shape that we want to use now i need to essentially go through and change each and every one of these individually and hope that i don't mess up so that i can get five of them this is a perfect example of a situation where we want to use a function it's when we have code like this you know like this little triangle that we want to reuse multiple times and so let me show you just real quick how we can put this into a function so that it's more reusable now when we declare these functions we're actually going to put these outside of this little main function so you can you can put them in the main function too but it's just better to put them outside and there's going to be some syntax here that you're not going to understand i'm going to explain it so don't worry but for now whenever we create a function we're going to say void and then we're going to say the name of the function so in this case why don't we call this draw triangle and i'm going to make an open and closed parentheses and then i'm going to make an open and closed curly bracket now you don't need a semicolon after this so anytime you have these like curly brackets you generally don't need a semicolon but what i'm going to do then is i'm going to take this triangle code and i'm going to put it in here now check this out this right here is what we call a function this is a container that holds a collection of code that we're able to reuse multiple times so because i have this inside of a function now instead of me having to print out all these triangles individually i can do what's called calling the function so i can actually call this function and what that means is that the program is gonna go over it's gonna run all that code and then it'll sort of hop back so let me just show you real quick we can use this to draw five triangles really easy so i'm gonna take this line of code and i'm just gonna put it down here five times three four five okay so these are all down here and cool so now if i run this you'll see that we have five triangles that should show up and so right there you can see all the triangles print out it's exactly the same output as we got from the code before it's just now we don't have to repeat ourselves and what's cool about these functions is that if i want to try and change this so if we wanted to change it to that square right the square that we had before i can do that in one place and now it's going to update for all of these so you can see now we're printing out all of those squares okay so that's the basics of a function it's a container where we can put code that we want to reuse and whenever we want to use that code we can just call the function like that and when to call the function you need to put this open and close parentheses after it now i want to highlight another point real quick which goes back to the execution flow of our program if you remember before i talked about how we execute one line of code right and then we go down to the next line and then we go down to the next line when we're working with functions it's a little bit different so let me just demonstrate this real quick i'm going to make two print statements this one's going to be called before function and then we're going to make another one down here which is called after function so let's take a quick look at what's happening in the execution of our program so i'm going to run this and you'll see that it says before function then we print out our square and then it says after function so the way that the computer is executing this is once again it's always going to start in this main function right here so this is always going to be the entry point for our program inside of main so the computer is going to go down it's going to see okay here's this print statement i'm going to print that out here onto the console then it's going to see that we want to execute this function and when it sees that we want to execute a function it's going to go and it's going to jump over to the function and then it's going to execute each line of code in the function in turn just like we talked about before and then once it's finished executing the last line it's going to come down here and it'll move on to the next line here in main and that's exactly what you're seeing right so we have before func and then the computer jumps over to this function executes all the lines of code inside of it and then jumps back down and continues on with the execution of the main function so functions there's basically like the computer will just jump over to them execute the code and then sort of come back okay and that's really how these work in the execution flow of the program all right so that's a little bit about the basics of functions we've talked about how they're just sort of these reusable containers but these can get a lot more powerful and the way they get more powerful is through something called parameters and arguments and parameters and arguments are essentially the same thing it's another one of those things where it's like two words technically they are a little bit different but a lot of people use them interchangeably and these allow our functions to just be so much more powerful and reusable so i want to show you how this works let's just get rid of all this code and we're going to start kind of from scratch here i'm going to create a function which is able to say hi to a user okay so it's going to be a very simple function once again i'm going to say void and then i'm going to give this a name called say hi i'm going to make an open and close parentheses and an open and closed curly bracket now we could have this function here and all it does is it could just say hi mike so it'll say hi to me and then down here i could execute this right so boom just like that and then it'll go ahead and say hi to me and i could run this you know multiple times like i said this is really reusable and it'll do that three times but let's say that instead of saying hi to mike i wanted this to be able to say hi to essentially any user or any like user name that we could imagine the word mic is kind of hard coded here into this print statement i can't change it and this is a situation where we would want to use parameters and arguments so a parameter is a value that we specified that the function needs to do its job so sometimes with these functions they're going to need pieces of information values in order to do the job that they're sort of assigned to do and those are called parameters so the parameters are all the values that the function needs in order to do its job correctly now in this case we could define a parameter which is called username and the function will use that and it'll say hi to the username so let me show you how to do that up here in this open and close parentheses this is where we're going to define all the different parameters all the different pieces of data that the function needs to do its job and the first one is going to be a string which is called username so that's just going to go right here and this is me defining that this function needs something called a username and so down here i'm actually going to use string interpolation and i'm going to interpolate that into here so this parameter is what we call it it's basically a variable it's literally the same thing we're just calling it a parameter because it's part of a function it's not just sort of like free floating but you can use this you can think of it exactly like the variables that we've been looking at now check this out because i've specified that this function takes a parameter if i try to run this you'll see that we get an error it says few too few positional arguments one required all that means is that i've specified that say hi needs a username parameter to do its job and so when i call the function i have to give the function that piece of information and this is what we call an argument so an argument is basically a value that's passed in and gets stored as a parameter and like i said they're pretty much the same thing um there's just that small distinction so what i can do here is i could say say hi mike and now if i run this program it says hi mike i could also copy this and instead of saying hi to mike we could have it say hi to joe so hi mike hi joe it's doing both of these now what's happening here is we're calling the say hi function the say hi function is specifying that it needs a username to be able to do its job so we're going to pass in a username which is a string into the function so in this case when we call say hi this value mic is going to get stored inside of this username parameter and then we can use that just like a variable to do string interpolation and to say hi to the username and this is really powerful because now we can tell the function what the name is that it should say hi to we now have control over the input into the function so i can say hi to joe i can say how to mic and it's able to do both of those so this function just became a lot more flexible because now it's able to take in arguments and or you know it's able to specify parameters here it's the same thing and then it's able to use them okay so that's really the main use case of these uh arguments and parameters so the argument is the value that gets passed in the parameter is sort of the value that we specify up here that's really the only difference now you can have one argument or you can also have multiple arguments and multiple parameters right same thing so let's say that in addition to the username i also wanted to specify an age and the age is going to be an integer so i can say okay the first parameter the first value that this function needs to do its job is the username the second is this integer which is called age and over here we could say hi username u r and then here we can interpolate the age so now if i run these you'll notice that we get another error and i'm sure you can guess why it's because we've specified that say hi needs two values to do its job but down here we're only passing in one so now i have to pass in like the age and then you know maybe this one's 50. and so now because we're passing in these two arguments the program is able to do its job so we call these like positional parameters or positional arguments they're basically separated by the comma this is the most common way to do this there are some other ways to pass in arguments as well but this is really like the most basic this is what we're going to be using so that is a little bit uh about arguments and parameters and as you can see it makes these functions just like so much more flexible because we can change up all of this data all right now believe it or not that's not everything with these functions there's another concept that we need to talk about which is this idea of a return value so functions like these functions that we're using right now all they're really doing is just like printing stuff out and that's fine like that's a good a good function but just like we can give functions information in the form of arguments and parameters we can also get information back from functions so we can call a function it can go and do a bunch of cool stuff and then it can actually give us a piece of information back that we can use in our program and this is through what's called a return statement so we're actually going to look at another example i'm going to get rid of this and we'll look at how we can use a return statements now you'll remember that when we've been creating these functions up to this point we've been using this void keyword and that's also what's used here in main so we could say void and then i'm going to create this function it's going to be called add numbers just like that now this void over here is what we call the return type and the return type is a specification of what data type this function is going to return now up until this point we've been writing functions which have returned void this void data type basically means that the function's not going to return anything it's not going to return any value back to us but now that we're talking about return statements i want this to be able to return back to me a double so we're going to write a function which is able to add two doubles together and then it's actually going to return the answer from adding those together back to us and i'll kind of talk through how that works so this add numbers function is going to take two doubles so the first is going to be num1 and the second is going to be num2 and then i'm going to use this special keyword which is called return and then i'm going to return num1 plus num2 so this function is very simple we just take in these two numbers and then we return the result of adding them together now let's see what happens if we execute this program so i'm going to call add numbers and let's pass in 5 and then we'll pass in 10. so i'm going to run this and you'll notice that nothing actually shows up over here on the console we're not printing anything we're not doing anything we're just calling this function so you're probably thinking like what's the point like i don't get it what's actually happening here well if we go ahead and print out the result of calling this function so we're going to print out the result of calling add numbers i'm going to run this and check out what happens we actually get the answer back so what's going on here is whenever we have this return statement and we call this add numbers function this is essentially going to get a value returned back to it so as far as dart is concerned we call add numbers and then this ends up becoming the value of whatever got returned so add numbers now is gonna be sort of executed and then this result is gonna end up getting printed out this is sometimes a little bit hard to understand i'm going to show you another example of like how this kind of works so we could create a double over here called answer and i'm going to set this equal to the result of calling the add numbers function with 5 and with 10. now check this out if i was to print out the answer we should get 15 again so when i call this it essentially becomes the value that gets returned from the function and so the value that gets returned from the function is what ends up getting stored here in this answer double so that's kind of how this works and this is really useful because a lot of times like we're not just going to be printing out the answer to something like we've been doing in the past most of the time we're going to write these functions they're going to return values back to us and then we're going to store them inside of like variables or we're going to use them in in some other way shape or form so that's like a really important concept in these functions okay so that is the return statement and that's really nice now one thing that you need to be aware of is that the return statement should always be the last line of code so this is a really simple function and we're going to look at some more advanced ones later but like if i had this like i'll print out before return we'll put our semicolon and then over here i'm going to try to print out after return and i wouldn't be surprised if we just get yelled at for doing this but let me show you what happens um okay so we didn't get yelled at but look at what happens right so we run this add numbers function okay we print out before return and then we print out this 15 which is actually down here we never end up printing out after return and that's because this return keyword is going to break you out of the function so we talked about the execution flow of programs like earlier the execution the execution flow for this is that we call add numbers we print out before return when dart sees the return keyword it's going to return this value back to the caller and then it's going to stop executing the function so we'll never actually get down to this line of code so it's you never want to like put code after the return statement because it just it just won't get executed okay so that's a little bit about return statements now we're gonna build another function which is using these same concepts just to kind of drill this in your heads a little bit more so we can go ahead and get rid of all of this now i'm going to create another function which is called prompt now if you remember before when we're writing all this code like when we built the madlibs game right we had the text here so like enter a color and then we also had you know where we're saying like okay so we have a string which is called color and that's equal to the standard input dot read line sync right we're doing all this so there's this whole process where i had to prompt the user and then i had to like get the input from them like on the command line or whatever so this is actually something that could be put into a function because remember we had to reuse it so we had this same thing for like the celebrity right whatever so there was this same code that we were repeating we could take this and we can put it into a function which would end up allowing us to reuse it so let's take a look at how we could do that so up here we're going to create another function and this is going to be called prompt now i'm going to give this a return type so this is actually going to return a string the last function that we used returned a number this one's going to return a string so we have the return type which is string i'm going to call this prompt and this is going to take one parameter one argument which is going to be a string and it's going to be called prompt text okay so remember we talked about these parameters right we can use that this is a value that the function needs in order to do its job and then down here we're basically going to write that same code that we were writing down here so instead of me printing out like enter a color or something like that instead i'm going to print out the prompt text and then maybe over here we can put like a colon so i'm using string interpolation and actually we don't even need to use string interpolation we could just type out prompt text but basically i'm going to print out the prompt text so this would end up being like what you know what's enter a color enter a pronoun interest library whatever so we're going to prompt the user and then we're going to create a string called answer we're going to set that equal to the standard input dot read line sync and then finally we're going to return the answer so these three lines of code we prompt the user okay we get their answer and then we return it now check this out all i have to do now if i wanted to get the color so i could say string color is equal to instead of me having to go through this whole process where i prompt the user and then get the answer all i have to do now is i could just say prompt and in here i just need to pass in the prompt so i could say what's your or sorry we'll say like enter a color right enter a color boom and then that'll be end up that'll end up being stored in there so then we could print out like the color is color so now when we run this because we're using this function if i want to do this then we'll actually be able to reuse all of this so we run this it says answer enter a color so we can enter in blue now what's going to happen is that blue is coming in from the standard input it's going to get stored in this answer variable and then we're going to return that that's going to end up getting stored in this color variable you can see this is like maybe a little bit hard to follow now so i'll hit enter on here and then you'll see here it says the color is blue now the reason this is so useful is because now if i wanted to get like the color and i could also get the what is it plural noun so enter a plural noun and then we could do the same thing for celebrity right so all like before this would have taken a bunch of code and we would end up reusing a lot of code now i can do this in just like three easy lines of code and uh it works out okay so this is why it would be like really easy for us to or it'd be really good for us to put this in a function because then we can reuse it and once again if i wanted to change this i could change it really easily so maybe we wanted to change like how the prompt text was displayed so i could say like the prompt is prompt text and now this will actually change it for each one of those so now if i try to run this it says the prompt is enter a color so blue the prompt is enter a plural noun whatever so we could go through that whole process and uh it's just really easy so really like the main point of these functions is that you define the code once and then you reuse it multiple times just with the different arguments parameters different values that it needs to do its job and it just makes them so much more reusable okay so we've covered most of the basics with functions and really like this is all functions are like you can add more parameters and arguments they can become more complex but at the end of the day these are the core concepts and so the more functions that you write the more comfortable you'll get but really the big thing with functions is just being able to determine which code should go into its own function and which code shouldn't like basically how those things are organized and that's something you'll pick up as you continue programming and as you you know continue watching other people program and stuff like that but uh i think that kind of wraps up our little section on functions now the next thing that i want to teach you is actually going back to those booleans that we looked at in the beginning of the video and we're going to be looking at something called conditionals or if statements this is another one of those like the programming bread-and-butters like one of those things that you're just going to be using all the time when you're coding and it's such a core fundamental concept so i'll just be walking you through exactly how that works now i have an example here of some if statement so an if statement is a way in our computer program right where we can give it some intelligence some logic so it's these if statements that allow us to allow our program to make decisions so if you add in if statements into your code then it's able to pick and choose and decide what it should do based on the inputs and the data that your program has so that's really what we're doing here we're sort of injecting some intelligence into our code and i just want to give you kind of an idea of what these if statements are at a high level so believe it or not as a human being every day you are dealing with if statements you are actually kind of an expert at these if statements it's just now we need to be able to translate them into something that the computer can understand so here we have an example of some if statements that you might encounter every day so here it says i wake up if i'm hungry i eat breakfast so this right here perfect example of an if statement that a lot of us encounter every morning so we have a situation right we have a particular set of you know things that are going on in our life right i wake up and then here's the if statement it says if i'm hungry so we're saying if and then over here we have what's called a condition and the condition is whether or not you're hungry now this can be true or false so if you're hungry that means that this is true and that means that you're going to come down here and you're going to eat breakfast now if this is false so for example if i'm hungry is false that means that you're not going to eat breakfast you're going to completely skip over that and you're going to just kind of keep going on with your day right so perfect example of an if statement and to kind of break this down we have what we call the condition this is either going to be true or false and if it's true then we run this action right you eat breakfast otherwise you just ignore it and you move on with your day so there's another one down here it says i leave my house so this is like our situation and then we have an if statement it says if it's cloudy now once again it's cloudy this is a condition this can be true or false if there's clouds outside then it's cloudy if there's not clouds outside then it's not cloudy if this condition is true then we're going to come down here and we're going to bring an umbrella now this one's a little bit different because you see down here it says if it's cloudy i bring an umbrella otherwise i bring sunglasses so this condition up here if it's cloudy if that's true that means we're going to bring the umbrella but if this is false that means that we're going to bring sunglasses so there's this otherwise keyword in programming we call this the else keyword which is going to sort of add in something that we should do if the original condition isn't true okay and then finally we have one more if statement down here it says i'm at a restaurant if i want meat i order a steak otherwise if i want pasta i order spaghetti and meatballs otherwise i'll order a salad so this is even more complex than the previous one so we have our if statement right if i want meat now if this is true then we're going to come down here and we'll order a steak but if this is false it says otherwise if i want pasta so in this case there's actually another conditional that we can look at if i want meat is false and if you want pasta then you're going to order spaghetti and meatballs and then otherwise if you want neither steak or if you want neither meat nor pasta then you would come down here and order a salad so this is like these three if statements obviously like this one's super simple and then this one down here is like a lot more complex these represent the different types of if statements that we're going to have in our computer programs it's just instead of us as humans you know looking to see if it's cloudy and bringing an umbrella we're going to have the computer do some stuff but hopefully this gives you an idea right that there's some intelligence in this right we're checking is it cloudy and if it is then we need to do something like bring an umbrella otherwise we'll do something else so there's like decision making that's sort of being injected into this and that's what we're going to look at with our code over here and i just want to move that okay so let's go ahead and get rid of all of this stuff we want to just kind of clear all of this out and actually i'm going to keep this prompt so we're actually going to end up i think using this a little bit later on so i'm going to keep this you can just put this down here at the bottom of the file just so it's kind of out of the way but we're going to bring that back so if you want to keep that then i would recommend doing that because i think we're going to use it again so now that we're talking about if statements and like i said the if statement has two main parts there's a condition which is true or false like if it's cloudy and then there's an action that we want to take if that's true or if that's false so let me just show you real quick how we write out an if statement so you can just say if and open and close parentheses and then an open and close curly bracket and this is like the standard syntax for writing if statements so in here we're going to put our condition and then down here we're going to put our action so this needs to be a boolean value this needs to be either true or it needs to be false now check this out i'm going to make this true okay so this is what we call our if condition and then if this is true then we'll come down here and we will print something out so we could say it's true and apparently i can't type that so now when i run my program check out what happens it's going to execute this code it's true so the computer goes down it sees this if statement and it looks at the uh the boolean value here it looks at like the condition if this is true then it's going to come down here and it's going to print this out now if i move this to false check it out i'm going to run and you'll see that nothing gets printed out and that's because this is false so whenever the condition that's inside of these parentheses is false this code doesn't get executed whenever it's true the code does get executed and that in like a nutshell is how if statements work now these get a little bit more complicated so for example let's say uh when this is false i want to do something right so when this is false i want to do something that's where we can use this else keyword so this else keyword is the same as that otherwise you know it's like if it's cloudy i bring sunglasses otherwise i or no if it's cloudy i bring an umbrella otherwise i bring sunglasses same thing so that's just else is another word for otherwise so down here we could say it's false cool so we can run this and you'll see now it says it's false if i was to change this to true then it becomes true so if the condition up here is false then we're going to execute whatever's in the else if it's true we execute whatever is in the if okay and this syntax here is like pretty standard so what i would recommend doing is just play around with this if statement structure because being able to wrap your head around like how this works is so important in computer programming i can't stress enough how much people use conditionals so uh there's some other things that we can do with this as well so you almost never are you just going to be putting it like true or false directly into here it doesn't make any sense but what we will do is we will use this when we're working with variables particularly with booleans so i could create a boolean over here called is smart and i could set this to true and i could actually use this boolean here as the condition so i could say if is smart then down here we could say you are smart and then down here this would mean that they're not smart so we can say you should study more so let's run this and you'll see here because the value of this boolean is true then we execute this i could change this and then you can see over here that that works so with the conditionals we just need some sort of a boolean and we talked in the beginning of the video about how like yes we can use true or false values but we can also use comparisons so i could say like five greater than three in here this can actually get reduced down to a boolean so this five is greater than three that's true because five is greater than three so this actually ends up becoming like a boolean value you can say and you know obviously the text doesn't make sense um but we could put booleans in here we could also put like these little expressions and like i said with these we call these comparison operators you can do like greater than greater than or equal to less than or equal to you can also do equals which is just this so in this case like five equals three that's going to end up being false uh whereas we could say five equals five and then that's going to end up being true okay so that says you are smart there's another one as well which is this uh if you say like this is called the negation operator you can say not equals so this would be like five is not equal to five so i would recommend just like playing around with all these operators like see how they work and it's really easy to see that with in the context of these if statements okay so we could just have like a boolean there like is smart another thing that we could do as well is we could have like so one of the things we saw like in the beginning of the video we had this greeting so maybe the greeting is like hello i could use if you remember we could do something like we could say you know greeting dot contains and e and we saw that this is going to end up returning a true or false value like if the greeting contains the letter e then this will be true if it doesn't contain the letter e then this would be false so this is another thing that you could do right is like you could say something like this so the greeting has e in its and then this would be the greeting has no e so now if i run this you know you can see over here the greeting has e in it because we're using this so inside of i guess the point i'm trying to make here is like inside of these uh inside of this like if statement conditional there's a lot of different ways that you can get to a true or false value you can do comparisons you could just have straight up booleans you could use some of these like string methods but either way we need to get down to a true or false value while we do that okay so that's a little bit about like if else and this is just the basics of of if statements so we have if else like that now another thing that you can do if you only have one line of code inside of the if statement and this only applies if you have one line of code which is sometimes pretty common you can actually get rid of these curly brackets so i could do something like this only when i have one line of code so you could say and my text editor is rapping a little but we could say something like you know if and let me format this if greeting.contains e print this otherwise two just with the one line of code we're able to to do that but generally like it's a good idea to just put the curly brackets around but you will you will see people like uh emitting them cool so i want to talk about some more concepts with this as well because those if statements are pretty basic we're just checking like one condition and then otherwise we're doing something else but these get more complicated and i also want to introduce to you two operators called the and and the or operators and these make your if statements just like so much more powerful so in order to demonstrate this we're going to create two booleans so one is going to be called is smart and we're going to set that equal to true because i'm pretty sure we're all pretty smart if we're learning to code and then we're going to say is student which is also going to be true so we have two pieces of data now that we're working with in our program is smart and is student and this tells us let's say we have a user right are they smart and are they like currently a student and we can use this information in order to help our program make decisions and that's really what these if statements are for right is we have different pieces of data that we're working with we're always always always working with data when we're doing computer programming and so we have these two pieces of data and depending on the value of these two pieces of data we want to do different things so for example like here we have two pieces of information so there's like almost four different combinations that we could make right they could be smart and they could be a student they could be smart and they could not be a student they could uh be a student and not be smart right there's all these different like cases that we might want to account for in our program and the if statements are perfect for that so let's go ahead and write an if statement block that's going to be able to check all of those things okay now first off we're just going to check so we can say if is smart and what i want to do is i want to check all those different combinations so i want to check first and foremost are they smart and are they a student because that means they're like a smart student for example so i'm going to say is smart and then i'm going to use this special operator called the and operator and it's just a double ampersand like that and then is student now the and operator is really really important it's actually super fundamental to even just the way that the computer works like at a hardware level there's these things called like and gates it's like and based logic we don't need to worry about that but there are these two values that we want to check and this condition is only going to be true if they are both smart and they are a student so anytime we use this and operator it means that this needs to be true and this needs to be true and then the whole thing becomes true so check this out here we could print out you are a smart student because if this line of code down here executes it means that they are both smart and they are a student so let's check this out right now they're a smart student so we could run this and it should say that but the second that one of these becomes false so if is smart becomes false now the sudden you'll see that we don't print anything out or if this is true and this is false now you'll see that we also don't print anything out so this and operator allows you to check multiple conditions multiple boolean expressions right you can check two things together and they both need to be true in order for the whole thing to be true if one of these values is false like if they're not a student or they're not smart then the whole thing becomes false and that's just the way that sort of and logic works okay now there is another operator which is very similar and that's called the or operator and it's just these two vertical bars and that should be well i'm on a mac it's right above the return key is that like a little vertical bar i think they call it the pipe character i don't know but this is similar to and but it's a little bit different so the or operator only needs one of these things to be true for the entire thing to be true so if we're using the or operator here instead of saying you are a smart student we could say you are either smart or a student or both these are all the conditions where this could be true so in this case they're smart and they're a student the or operator is going to say you are either smart or a student or both if one of these is false you'll see that it still runs that code and that's because with the or operator only one of these needs to be true in order for the whole thing to be true so if i made is smart true and i made is student false you'll see that the whole thing still becomes true because one of them's true if these are both false that's the only situation where it's not going to run you can see there we didn't run that code so with the and operator both of these need to be true in order for the whole thing to be true with the or operator only one of these or both of them needs to be true in order for the whole thing to be true and this is like we call this and or logic it's like super super fundamental uh to computer programming there's some other logic to like xor logic and stuff i don't know you can look that up if you want but this is what we're using 99 of the time okay so that hopefully gives you a little idea about these operators now i want to continue on with this example so we're using the and operator which means that if this is true it means that you are a smart student okay i'm going to keep using the and operator but you could also do this with the or operator you know it would just be a little bit different so in this case we're checking the condition where they're both smart and they are a student but let's say that we also wanted to account for all the other situations as well like maybe they're a student but they're not smart well in order to do that we need to check another condition and sometimes in computer programming we're going to have we're going to want to be able to handle multiple like combinations of these different boolean values and whenever we are i guess like multiple conditions basically so in order to do that we can use what's called the else if so you can just type in elsif and i'm just going to write this out and then i'll explain to you what's going on so i'm going to say else if is smart and exclamation point is student now i know there's some new syntax here just give me one second so i'm going to print this out and it's going to say you are smart but not a student so look at this right here this else if is very significant because what's going to happen with the flow of the program is when dart first goes through and is looking at this it's going to look at this condition first and it's going to say okay are they smart and are they a student now if that's true dart's going to come down here and it's going to print this out and then it's going to break out completely from the if statement it's going to be completely done with the if statement if one of these ends up being false though so if this whole condition is false instead of breaking out dart is then going to go down to this else if and it's going to check this condition so the elsif only gets run if the condition above it like in this if statement is false that's the only situation where this is gonna get gonna get run and you'll notice that this could also be false right that's technically possible okay so we're gonna keep looking through this but you know just stick with me for now and this should all like start to clear up a little bit so in this elsif we're checking to see if they're smart and and you'll notice that i'm using this exclamation point so we just kind of talked about this the exclamation point is what's called the negation operator and maybe i can just show you guys this real quick so here i'm going to print something out so i'm going to print out true right here okay so we'll print out true we're going to put that semicolon in there not going to forget that when i was uh like planning for this thing i forgot semicolons like almost every time so i was like kind of worried and you can see i forgot one there too just not used to using them so okay we're printing out true here right now if i put this exclamation point right next to it what it's going to do is it's going to negate that value so it'll basically turn it into the opposite value and with booleans like true is the opposite of false false is the opposite of true so if i do this now you'll see that we get this false so this is just a really easy way to like negate something and we use that a lot so you can see down here like the situation where we want to negate this this is essentially me saying that they're not a student so if they're not a student then this ends up being true this boolean logic takes a little bit to like wrap your wrap your mind around so if i were you i'll just like play around with it a little bit just to kind of internalize it but essentially what we're saying here is that they are smart and they're not a student that's what that negation operator is doing okay so then down here this is appropriate right we could say you are smart but you're not a student because that's what this condition is sort of encoding so let's let's see if we can get that one to trigger right so that would mean is student should be false but smart should be true and if we run this now you'll see that that works so really what the code is doing is it's looking here so the first thing it does is look at the first condition this ends up being false right because they're not a student then we come down here and we check this condition this is true because they're not a student and they're smart so then that's the situation that gets executed now we can keep doing this for the other situations so there's another situation here where we can say they're not smart and they're a student right so this is the situation where okay they're kind of like i guess a dumb student right that's really what we're saying here so we can say you are not a smart student so that's like technically what we want to encode there so let's see if we can get this one to trigger so i'm going to change is student to true i'll change is false is smart to false and one more time i'll kind of run you guys through what's going on so it says you are not a smart student it worked the way we wanted it so dart looks at this first condition this is false and so that it moves on to this elsif it checks this condition next that's false so then only after these two are false does dart move down here and check this one right so these both need to be false before we even check this and if one of these ends up being true then we just break out of the entire if statement cool and then finally there's that last situation now in this case we can just put this else because there's only one more possible situation which means they are you are not smart and not a student so that's right there and so then that would be when like both of these are false so you can see that's the situation down there now you could also you could put another elsif in here right you could say not is smart and not is student but it's kind of unnecessary because if it's not any of these three then it has to be the other one just because like there's no other possible combination of these two so i'm fine with just putting the else here i like that and we'll run this just to make sure it's working cool so this situation as i've said before very common where we have pieces of data and depending on the value of those pieces of data we want to do different things so this is essentially our computer just making decisions uh that's all we're doing and there's some other like types of things you can do with this but this is bread and butter this is like down and dirty just basic if statements so okay i'm i'm pretty happy with this i think now we can move on so the next thing i want to do is use these if statements to make one of our other applications a little bit better so before we built this calculator calculator was pretty cool but one of the downsides of the calculator that we built was that you could only add the two numbers so i would enter in one number i'd enter in another number and then those two would just automatically get added together i wasn't able to specify whether i wanted them to be divided or at you know multiplied subtracted added whatever i didn't have choice and now because we have these if statements we can allow the user to make that choice right we can actually respond to the different data so we could say like okay if they want subtraction then we'll give them subtraction if they want addition we'll give them addition etc so we're going to go ahead and just kind of i guess like code that in that's what we're doing now i also want to bring in some of the other concepts that we've used just to kind of start combining all these things together so you guys get a better idea of how it works so the first thing that i would like to do and this is again part of our more advanced calculator is down here we have this prompt method and or this prop function and i'm actually just going to copy this so if you save hopefully you saved this from before if not just copy this out again well actually don't copy this because we're going to change it but this allowed us to prompt the user and then get the answer and then return it now if you remember when we did our numbers we had to convert the number into a double so we did something like this it said you know enter a enter first number right so we did that and then down here we made this a double and we said you know num1 is equal to and we said double dot parse and then we did this right so this was kind of the way that we converted whatever the user entered into a double and if you're not sure about this i did give a full explanation of it earlier in the video so just go check out that part where we built the calculator but i want to be able to do this thing similar to the way that i'm doing it up here because we're going to need to get multiple numbers right so i'm going to need to be able to get two numbers and i'm also going to want the user to be able to enter in like what sign they want to use so subtraction addition multiplication etc so we're going to use this prompt method again but i want to create another one so i'm going to copy this and this time it's going to be prompt number why don't we call it prompt double and this is going to return a double and over here we can get rid of this prompt text and then i'm just gonna say enter a number right there so this prompt double function this is to end up being reused and we'll be able to use it to get the doubles that we need for our calculator so this is kind of where we're combining some of these different things together right these are what we would call like helper functions they're just helping us do things that we don't want to have to write out multiple times so we're going to print out enter a number and then we're essentially going to use this same line of code here so i'm just going to copy this up here so we're going to create a double let's just call this num or maybe that's a reserve word why don't we call it um mynum i don't know you can call whatever you want but this is gonna parse the double out of that input that the user enters and then why don't we return my num so now we have two functions we have the prompt function which allows us to pass in a prompt text and then we get the answer back and then we also have a prompt double function which tells us to enter a number we convert that into a double and then we return it so these two functions now we can utilize when we're building our more advanced calculator okay so down here in the main we need to get three pieces of information from the user we need to get the first number the second number and we need to get the operation that they want to perform do they want to add do they want to subtract multiply divide whatever so let's go ahead and do that so i'm going to say uh let's create a variable so let's get the the numbers first so i'll call this num1 and i'm going to set this equal to prompt double now because we put this into its own function all i have to do to get the number is just call the function and it's going to return that number back to me and i can store that inside of num1 and again we kind of went over this like in a little bit more detail so if you're still feeling a little bit confused i would recommend going back to the i guess either the function section where we talked about returns or if you're if you're not sure what's going on here then we talked about that in the first building at calculator section so please go back and watch that um you don't want to do this if you're like too confused about it so we have that first double now let's get numb two prompt double boom just like that now i just want to demonstrate real quick how this would run so when i run this again we're prompting for all this stuff now i must have messed up somewhere and let's see oh okay so it's because i i actually copied this so this is why i'm getting an error so just delete that okay so i'm going to run this and it says enter a number okay so i'm going to enter number four and once again all of this code is going to be what's running so i enter my second number i guess these should be doubles right so i'll do that so just from these two lines of code now because i'm able to reuse all of this i'm able to get both of my numbers and then the last thing we want to do is get the operation so this would be addition subtraction whatever so we can just call that op and let's set this equal to prompt so we're going to prompt the user now we wrote this function before we need to pass it a prompt text and we'll just say enter an operation and then in here we could say like plus minus divide and multiply so now we're able to get the first number the second number of the operation let me show you this just so we can make sure it works so two numbers and then the operation this is like wrapping a little bit but we could put like a plus sign there cool so this is all stuff that we've gone over right you we've gone over all this code obviously we made this new function that was pretty fun but this isn't really any new stuff what i want to do now is show you how we can determine if we should add subtract multiply or divide based on this right so we have this piece of information this will tell us what operation the user wants to perform but we need to be able to use that in our code because the code is what needs to make the decision right if someone said i want you to add these numbers i just use my super smart brain and i do that right it's easy but the computer is dumb so we need to like program intelligence into it so let's go ahead and do that now what i want to do is i'm going to create a series of if statements just like we did in the last one and the first thing i'm going to do here is i'm going to check to see if the operation is equal to and this double equals right here will allow us to check if one thing is equal to another i'm going to check to see if the operation is equal to the plus sign so if the operation is equal to the plus sign then down here i'm just going to print out num1 plus num2 perfect so just like that now we have an if statement so believe it or not this program now is smart enough to be able to add two numbers so if we run it i can put in two numbers here like 4.5 and 2.3 and then i'll put addition now because this if statement is checking to see that the user entered a plus sign we'll actually be able to get the answer so i'll hit the plus sign and then it actually prints out the answer now if we do this if we try to do this with like uh you know let's do this and then we try to do subtraction it's not going to work right because we're not checking that condition so let's go ahead and check that so i can say else if the operation is equal to the minus sign and then down here we'll do that same thing so and this time instead of adding them we are going to subtract them and let's just see now if subtraction works always a good idea to just run your code anytime you change it just so that way you stay on top of it because if there's errors then you want to make sure you get them right away okay so then we were able to subtract them looks like that worked pretty well so let's do the same thing we'll add in another elsif now we're checking to see if the operation is equal to the division sign and if that is indeed the case then we will print out num1 divided by num2 and i bet you can guess what the next thing we're going to do is i'll actually just copy this we are going to check for multiplication so now we do the little star whatever that is and then we'll multiply so now we should be able to handle each one of these operations so why don't we just try like multiplication we'll see if that works okay so we're going to enter a number let's do 7.5 and then maybe like 8.0 and then we'll try to multiply them and so there we go it was able to multiply those two numbers cool so this is like a really common thing that we do right we're checking all of these different conditions and depending on the condition we're printing something else out so this hopefully demonstrates like where these elsifs and everything comes in handy now we could add in an else here because there's another situation that we're not accounting for which is the situation where we have an invalid operator so if the user just goes crazy and you know they try to add four or they four and three and then they add in like you know some crazy text then we can just tell them it's an invalid operator and we don't have to worry about it anymore so right here this demonstrates what i think is a pretty good use case for for all of this stuff okay so that is our better calculator and you can keep playing around with this if you want that's totally fine i do want to show you one additional thing that we can do here and it's going to involve introducing a new topic which is called switch statements so a switch statement is actually very similar to an if statement except that it's only used in very specific circumstances and those circumstances are where we're checking to see if one thing is equal to a bunch of other things and believe it or not this situation is exactly the perfect situation for a switch statement now we can do this with if statements this is totally fine you're never in the wrong when you're using an if statement it's just sometimes these switch statements can make our lives a little bit easier so i'm just going to demonstrate real quick what they do you don't have to like these aren't like the most commonly used things in the world so don't worry if it's not super clear but i just want to show this to you now one thing i want to point out about this situation up here is that in each one of these conditions right in each one of these we call these boolean expressions we're checking to see that the operator is equal to something different in each one of these the operator shows up and when you have a situation like that where the same value is getting compared to a number of different values like we're comparing the operator to a plus to a minus to a division to a multiplication when you have a situation like that it's perfect for a switch statement so let me show you how we can replicate this code as a switch statement and you'll see it's kind of a little bit more readable i guess so the switch statement is going to be structured like this so we're going to have an open and close parenthesis open and close curly bracket now in here i'm going to put the variable or the value that i want to compare to multiple values so in this case this is our operator so i'm going to put the operator right in there and now i'm going to say case and in this case i'm going to say addition and then a colon now the syntax for these switch statements is a little tricky and if you get it wrong then it will yell at you so just just be careful but you're going to say case and then in this case we're saying in this case yeah we're saying the plus and then this colon then down here you want to indent and you're going to just put whatever like line of code you want so in this case it's just the print and then you're also going to need to put one more keyword which is called break we'll talk about what break does in a second so we have this case of addition and basically like the way you could read this switch statement is you could say okay we want to take this operator in the case that the operator is a plus i want to do this and then i want to break out of the switch statement that's all this is saying so in the case that we have an addition sign as the operator print this out and then break and we can do this for each one of these so let's do this for addition subtraction and i messed that up multiplication man i can't get this okay addition subtraction and then we're going to change this to subtraction as well multiplication we'll change this to multiplication and division and then we're going to change this to division so i'm basically just replicating the same code and i wonder if i can comment out okay so i'm just going to get rid of this for now we'll bring it back and if i run this you'll see that the code should work exactly the same so 6.0 9.8 and let's multiply them so you can see we were able to get an answer out of that so the switch statement is just doing the same thing it's just a little i guess it's a little bit more concise uh here's the thing about switch statements like there's very specific cases where they're useful this is one of them but in a lot of cases you can just use an if statement like there's certain programming languages like i don't think python has a switch statement um i don't know i think most of the other languages do i don't know maybe rube i don't know that ruby has one anyway it doesn't really matter but uh these are pretty useful now uh the last thing that we would want to add in here would be you could say default and default would just basically be like that else so here we could say invalid operator so let's run this and four five and then we'll do an invalid operator and then it it's able to do that it's able to handle that okay so there's like i said two ways that we could kind of write this out now the last thing i want to do is talk about this break keyword and the break keyword is significant because well let me just show you so let's say we remove the break here and this ends up being a plus again so this this maybe isn't the best case but i just want to show you real actually no we would want to do this so i'm going to remove the break up here so in this case we have two of these case statements which are both going to end up being true now most of the time these aren't going to be like the same character but in this case both of these could be true so if i don't put a break statement right here if i remove this and then we do the addition and what do we got okay i gotta put a semicolon so okay switch case may fail okay so it's actually giving us an error here so this is this is like not helping my point at all but the whole point is that if you put the break keyword here once this code gets executed this is telling it to break out of the switch statement so we'll completely leave the switch statement if this break keyword isn't here then it would keep going it would execute this code and then it would keep going down and then here it finds another one that also matches and then so it would do this again now dart's too smart for us right it knows what we're trying to do here but that's kind of the idea of the break alright so i want to bring back that if statement real quick just like command zing and these two things as i said are like pretty much equivalent okay so that's a little bit about how to build this like more advanced calculator and if you want just you can play around with it you could add in some other stuff like square roots or whatever just add an additional operators but that could be kind of fun okay i'm going to get rid of this and i'm actually going to keep both of these because i think we may end up using them again not exactly sure but now i want to get into what is kind of the next big topic so we've talked about if statements we've talked about some data structures getting input from users one of the last core topics that we need to cover is this idea of looping and once we start adding loops into our programs they just become like infinitely more powerful a loop is a way that we can execute the same code multiple times so there's a lot of times in computer programming where we want to do something multiple times and generally this is related to data so if we have like data for example that's in an array we might want to do something for each element in that array so we might want to just like repeat the code over and over again and there's a couple different ways that we can write loops i'm going to walk you through what i think are some of the most common and hopefully just like try to get uh some of this like into your head so the most basic primitive type of loop is called a while loop in a while loop i really just have to demonstrate it so i'm just going to write it out here and then we're going to walk through exactly what it's doing so this is kind of like the simplest wall loop we could write i'm going to create an int up here which is called i and i'm going to set it equal to zero and in here i'm gonna say i less than five and then down here i'm going to say print i and just stick with me i know some of this is confusing and then down here we're going to say i plus plus so here is my while loop it's pretty much the most basic while loop that we can write and i'm going to go ahead and run it and i want to show you what happens and then we'll talk about each of these different parts so we'll run this and you can see that the numbers between zero and four got printed out over here onto the terminal so or onto the console so what exactly is happening here a while loop is a programming structure that allows us to repeat code as long as a particular condition is true so the while loop has multiple parts obviously we have the name now this is very similar to if statements so inside of these parentheses we have a condition we also call this the loop guard as long as this condition is true we are going to execute the code inside of the while loop and every time we go through and execute the code that's in here we're gonna go and recheck the condition so let me walk you through real quick just step by step how the computer is executing this while loop and we can get a feel for what's going on so up here i'm creating an integer i and this is a really common thing to do we call i stands for index basically like a counter and as long as i so as long as this variable i has a value that's less than 5. now remember this is either going to be true or it's going to be false it's a boolean expression so it can get reduced down to a true or a false value so as long as this is true right then we execute this code so the computer goes through and it says okay we have this integer i it has a value of zero now we have a while loop so before i execute any of the code inside this while loop i'm going to check this condition if this condition this boolean expression is true then i'm going to execute all of this code so the first time that we go through this loop the computer checks the condition i at this point has a value of zero which is indeed less than five so we're going to come down here and we're going to print out 0 right we're going to print out that value that's what's over here and then we're going to increment it remember we talked about this increment operator this is basically the same as saying i is equal i is equal to i plus 1. so we can just do plus plus and okay so the computer goes down it prints out i which is the zero and then it increments i so it adds one to the value of i now the computer is going to come back up to this loop guard it's going to come back up to this condition and it's going to check it again so in this case i has a value of 1 and we can check to see okay is 1 less than 5 it is so then we're going to execute the code again so we go down to this print i in this case we're printing this one over here and then we have i plus plus so we increment it computer's going right back up to the top of that loop so now i has a value of two because we've incremented it two is less than five so then we go through again we print out two now you kind of get the idea we just keep doing this we keep executing the code and then rechecking that condition eventually once we've printed out four so in this case we print out i i has a value of four now we increment i i now has a value of five we come back up to check the condition five is not less than five five is equal to five so it's false which means we break out of the loop so it's only as long as this condition is true that we're going to keep executing the code inside and with these while loops it's very important that whatever is up here in your condition is hopefully changing or potentially changing each time through the loop otherwise you're going to get into what's called an infinite loop now let me demonstrate an infinite loop for you guys i'm just going to and i hope this doesn't break my rebel i'm going to get rid of that i plus plus and i'm going to run this code now check this out it's just going to keep printing out all of these zeros and i actually just stopped it but you'll see here that we just and actually i can scroll down you can't really see it but it's all just zeros what's happening here is we check the condition i has a value of zero which is less than five we print it out and then the computer comes right back up here and checks that again we didn't modify the value of i so i still has a value of zero so this is still true and in fact we're never modifying the value of i so this is going infinitely loop and that's what we call an infinite loop so a rite of passage for all new programmers is to just you know accidentally write an infinite loop i remember when i was in college one time i was doing programming in like this really low level language called c and i was running all the code on this server that we had at the university and my teacher was like we're basically like learning how to spawn processes which is basically like spawn other programs that like take up memory and everything and he was always like okay don't write an infinite loop while you're spawning these processes because then you're going to crash the server and then of course i ended up doing that crash the server so i definitely have my fair share of like trepidation with these uh infinite loops but uh i digress so that's a little bit about a while loop and it's really really important that you're always you know whatever is in this condition it should become false at some point otherwise the loop will go on forever okay so these while loops are good now this is a very simple example i like i like this example it's kind of the classic while loop example but now i want to show you like some things that we can uh that we can do with this so what i want to do is build a little guessing game this is going to be the first obviously we've built calculators which is fun but the guessing game is like i really like this project because it's kind of like more advanced and i think it's it's more of like a program than just like some calculator so the idea of the guessing game is we're going to have like a secret word and the user is just going to try to guess what the word is right almost like they're trying to guess a password or something and then if they get the word correct then we'll say like you one or whatever so we're gonna write out this program i'm gonna write it and i'm gonna be walking through it you know and hopefully showing you how it works so to write this program we're going to need a couple of pieces of information the first thing we need is like the actual answer to the guessing game so maybe the answer to the guessing game can be like michael scott if you watch the office or if you've seen my other programming videos you know i love the office so the answer is michael scott right that's what we're trying to guess and then we're also going to have a variable here which is called guess and this is just going to be an empty string to start off with so an empty string is when there's nothing in there and you can actually see here this is like force of habit i'm using these single quotation marks with these strings you can use either double quotation marks or you can use single quotation marks i feel like i'm more used to using the single ones it doesn't really matter they're both the same exact thing but i guess i can use doubles just because that's what we've been doing so this is what we call an empty string it's a string that has length zero and it has no characters in it and these empty strings are pretty useful so we have our answer which is what the user is going to try to guess and then we have the guess now this is going to store whatever the user is guessing and this will start to come together in a second that you'll see but this is what we're going to use to keep track of like the guesses that the user has given us now the guessing game is unique because the user should be able to guess multiple times so we don't want to just give them one chance to guess we want to be able to give them multiple chances multiple chances to guess and whenever we want to do that like basically we want to let them guess the answer until they get it right perfect situation for a while loop so i'm going to go ahead and write out this while loop now whenever you're writing the while loop you can almost just like read it like you would a paragraph so i want to say like let's just think at a high level like what we want to do while the user hasn't guessed the answer i want to keep prompting them to guess and i want to keep checking their answer so that's really what we want to do and so we could basically say wow the guess so whatever the user's guessed is not equal to the answer then down here i basically want to prompt the user to guess and then i want to check to see if they got it right so we have a way to prompt the user for something with this prompt function you can see this is coming in handy uh even more for us right so down here i can say guess is equal to and we're going to call that prompt function and i'll say enter a guess so what's happening here is we're saying as long as the user hasn't guessed what the answer is as long as the value that's stored inside of the guess variable isn't equal to the value that's stored inside of the answer variable then we want to say that the guess is equal to like whatever we prompt the user for and then down here and we can walk through this some more don't worry i'll keep going over it but we can just say u1 so if they get down here that means that they've broken out of this loop and they've won the game so let's run this code and we'll see what happens okay so it's saying and actually it says the prompt is here so let's get rid of that we don't need this anymore let's just put the prompt text so if you remember we just kind of i think i just added that to demonstrate something so we're going to run this and it says enter a guess so what i'm going to do now is i'll enter in like what i think the answer is right so i could say um you know maybe it's hello right and i'll hit enter now you'll notice that it asked me for a guess again so what's happening here is initially the guess doesn't has doesn't doesn't has doesn't have a value so this guess is an empty string and it's not by default equal to the answer so when dart executes this it's going to go down it's going to create these two variables and it'll say okay while the guess is not equal to the answer it's going to check this condition and in this case the guess is an empty string which is not equal to the answer so then we get prompted to enter a guess and that's exactly what's happening here so i run the program we check the condition it's true you know the guess is not equal to the answer and then down here we say okay the new guess is equal to whatever the user enters in when we prompt them so down here i could type in hello now what's going to happen is i type in hello so the guess variable now has a value of hello we go back up to the top of the if statement we check the condition the if statement the while loop we check the condition again so guess now has a value of hello which is not equal to answer so this condition is true which means we need to go down and prompt again which is why we're getting this so i could you know type in whatever here like i'm just randomly guessing none of it's equal to michael scott when i finally do type out the sacred name of scott himself you see that it says you won so eventually we get prompted we enter in michael scott so now the guest has a value of michael scott and then we come back up we check to see is guess which is michael scott equal to the answer which is michael scott it is so this now becomes false because the guess is equal to the answer and then we come down here and we print out u1 now this this logic here uh this like not equal to logic is really hard to wrap your head around as a beginner that's one of the reasons why i wanted to show you this example just so you can get some experience with it so this whole thing is true when they're not equal but when they are equal this whole thing becomes false which is a little like unintuitive to think about but that's just kind of the way that it goes okay so this is looking pretty good now this is kind of like the basics of of these while loops another thing that we could do as well is we could create like a guest count so this is just one little extra thing you could add in and the guest count is going to be equal to zero so initially the guest count is equal to zero and we'll have this will just keep track of how many times they've guessed and so then we're going to prompt them and then every time we prompt them whoops we will just increment the guess count so guess count plus plus and then down here we could say u1 in and let me interpolate this guess count guesses so now check this out if i run this and i enter a guess like let's just enter in a bunch of you know whatever three and then i finally get it right now it says you want in four guesses so this is just like another little add-on we can we can do but you can see here like what i'm doing with this program is i'm combining all of these things that we've learned so far so we're using this prompt function we're using all these different data types we're using this while loop we're using these boolean expressions these comparisons we're using string interpolation we're using this increment stuff right so all of this is getting combined together and that's what coding is all about and that's really why as a beginner it can be sometimes hard to break into this skill and learn this language is because unlike a lot of other things where you learn this thing and you learn that thing with coding it all builds on each other so in order for me to you to build this guessing game i had to pull on all of the concepts that we've covered so far in this course uh except for i guess if statements but even this is kind of a conditional anyway so i had to pull on all of those different concepts in order to build this so if you are just starting out and you're getting a little bit overwhelmed or intimidated by something like this it's completely normal i mean i remember starting coding and like i wouldn't have been able to write this at all like this would have confused me so much so if you're at least getting it a little bit that's good the way you get better at coding is just by practicing so my advice is if you're a little shaky with this just try to rewrite it a couple times just practice with it and then you should get a little bit better okay the next type of loop so we're still on this looping concept believe it or not loops are a massive topic this type of loop is a while loop this is the simplest most basic type of loop and all the other loops that we're going to talk about because we're going to talk about another type of loop all of them are just like specialized versions of the while loop that allow things to go i guess like a little bit smoother so i'm going to show you two other types of loops we're going to look at a traditional for loop and then we're going to look at i guess what would be called like a four in loop so you'll get two you'll get exposure to two other types of loops as well all right so we can get rid of some of this stuff because we're done with the game now i want to bring back a program that i had before so i had int i is equal to zero and then we said wow i is less than five and then down here we printed out i and then we said i plus plus so you guys remember this program that we wrote like i don't know 10 minutes ago so this allowed us to print out everything from zero to four this believe it or not where we have this variable i and every time we go through the loop we increment it this is super common in computer programming and we're going to take a look at like why it's super common in just a second but i just wanted to kind of make the parallel here so what we're doing here is we have this i every time we go through the loop we increment it and the reason that we call this i is because it stands for index and it's basically an index of what which execution of the loop that we're on i don't know there's probably a better way to say that but so for example zero this would be like the and again in computer programming we always start from zero so this would be like you know the zeroth time that we go through this would be like the second time the third time whatever um so it's just keeping track of like how many times we've gone through the loop and this is really useful if we want to loop through an array so or a list so remember we have this list data structure and let me just do this so why don't we have a list of strings and i'm just going to call this friends this is like maybe this is like a friends list for like facebook so we can have like stanley we can have pam and maybe we have uh karen okay so we got three friends that are on our friends list and if you remember with these lists we could say something like this so i could do this and i could say friends at index position zero so i can use this special square bracket syntax and i can refer to a friend at a particular index so if i ran this program now we should print out stanley because stanley is at index position 0 in this array and if you remember we have our index positions so stanley would be 1 pam would be two and then karen would be three or sorry no totally wrong stanley zero pam's one karen is two see how confusing this is i've been programming for a decade and i still i still get weirded out about these uh starting at zero things so we can refer to like stanley like that but imagine if we wanted to print out each one of these friends individually well believe it or not this i value is going to allow us to do that so let me show you real quick instead of just printing out i i'm going to print out friends at index position i and let's see what happens so now if i run this and actually this should be 3 because we have three friends that's why we got an error so if i run this you'll see that it prints out stanley pam and then karen all in a row like that so we have three friends that are in our friends list and we're saying as long as i is less than three we're going to print out the friend at index position i so the first time we go through this loop i has a value of 0 and we print out friends at index position 0 which is stanley the second time through i has a value of 1 so we print out friends at index position one which is pam and then finally we print out karen now we can make this a little bit easier so remember we have that length property so we could say friends.length and in this case this should give us 3 because we have 3 friends so instead of saying i is less than 3 we could say i is less than friends.length and this is like a really really common thing to do in computer programming where as long as i is less than the number of friends in the list we're going to print out each friend individually so we kind of get that here this is what we call like looping over an array and we can use these loops or yeah we can use these loops in order to like do something for each one of these elements so for example i could say friends.i.2 uppercase is another one we looked at and now it'll like capitalize all of these friends or whatever it'll do something like with each element in the in the list super super common and so whenever we want to do something like this where we want to loop over these friends we can do this in a while loop but this is actually such a common thing that we want to do that there's a special kind of loop that will be able to do this for us and specifically i'm talking about being able to keep track of this index variable which we can then use to look at all the friends i know this is getting a little confusing this is one of those more advanced topics so if you're like really confused that's okay just keep watching i'm gonna try to explain it as best as possible um but really this is just about like practice okay so we have this situation where we have this index variable and every time we go through the loop we're incrementing it and we saw one of the ways that we can use that right to like print out all these friends if you have a situation like this where you want to do it there's a better more concise way that we can using something called a for loop so i'm going to create a for loop it looks exactly like a while loop in its structure except that inside of these parentheses we're going to put three things so i want to first off point out three things that are going on in this while loop first thing is we're creating a variable called i and we're giving it an initial value of zero so this is the first thing that we're doing here in this little while loop structure so i'm actually going to take this and we call this the initialization of the counter variable or initialization of the index i'm going to put that right here so i'm just going to paste that right up here then i'm going to put a semicolon and a space so this is like the first section that's going to go inside of these parentheses the next thing that i'm going to do and the next thing i want to point out over here is the condition of the loop so this loop has a condition which we're going to check every time we go through it basically like have we gone through all the friends yet so i'm gonna take this and i'm gonna put this right up here next to it i'm gonna put a semicolon and a space and then finally you'll notice that we have this i plus plus down here so this is what increments the counter every time we go through the loop increments the index is what i mean to say so i'm going to take this and i'm going to put this up here and i'm not going to put a semicolon after it so we have three things that are going in this parentheses we're initializing the variable i to be zero we have our loop guard or our loop condition and then over here we're incrementing i so this structure now if we put this in a for loop is going to allow us do to do exactly what we did down here just without all of this messy boilerplate so now check this out and i can get rid of the while loop now if i run this it should do the same exact thing so this for loop and this while loop are literally identical and the for loop just makes it more convenient for us to do this so once again the most confusing part about this is generally these things up here so we're initializing the variable and then we have our loop guard our condition and then over here this is a line of code that's going to be executed after each iteration of the loop so generally this is just going to be i plus plus you don't have to change it it's always going to be the same thing okay so this is pretty cool now these four loops are useful but there isn't actually okay there's actually an easier way that we can do this as well so like this is what we call like a traditional for loop this isn't all the old programming languages most of the more modern programming languages have this included but generally it's not used as much generally especially if we're doing something with these arrays we have even more specific ways of doing it but you need to be exposed to this because this is this is like very common code um especially like in particular types of code bases so here we're like printing out all of these friends right essentially we're doing something for each of the elements inside of this array very common you can do it with the while loop you can do it with the for loop but there's an even better way to do it using what's called a four in loop so we can replicate this same code by saying for and then in here we're going to say string friend in friends and then down here i could print out essentially something similar to this except i'm just going to print out and actually probably shouldn't put that there i'm just going to print out friend so let's run this code and you'll see here that this for loop we call this a four in loop this is the execution over here so we printed out stanley pam and karen each one individually and then down here we did this other for loop where we were like uppercasing them whatever but that's a little different but you can see they're basically doing the same thing and we could say like to uppercase here on this friend and then that would kind of replicate it over there so this for in loop is used primarily when we're looping over a data structure like an array and i think i think we can only use it with arrays actually or a list same same thing right array and list so whenever you you want to loop over and do something for each friend like print out each friend you want to use the four in loop the for loop though which is a little bit more flexible it gives you access to the counter right gives you access to this eye in certain specific situations you're going to want that eye and so that's why we still keep this guy around but most of the time we're doing stuff with arrays so you can just use the four in but like i said you need to be exposed to both of these cool all right so this is the basics of for loops okay we've looked at how we can loop over an array we looked at how we can do this with counters now i want to just show you another little quick example and this is going to go back to those triangles so you remember we were drawing those triangles and i'm actually going to get rid of these so this is like going back to the beginning and we had like our triangle here so it looked like this and then let's just do let me see here okay so we'll do this and then wait this needs man i always suck at drawing these triangles okay so here's our little triangle right this is good we can print this out it works i'm going to show you because one of the things that we did with functions right is that we created a function that i can actually i think it was void so this just means it doesn't return anything so we created a function which was called draw triangle and this just did this right so then down here we could say draw triangle we do this a million times right and one of the things we were trying to do is draw like see if we could draw five triangles so here this works right we can draw five triangles but you'll notice that we have to call it five times that's a little inconvenient right i wonder if there's a way that we could tell the draw triangle function how many triangles we want it to draw and of course there is and we're going to use a for loop to do that so the draw triangle function we're going to change it around a little bit and it's going to take in an integer so a whole number non-decimal number which is going to be called num triangles and we're going to use this so now if we call draw triangles we have to pass it a number right i could pass it a 5. and what we'll do is we'll use the number of triangles to determine how many triangles we should draw and we can use a for loop for that so i'm going to create this for loop and once again i want to walk you through what's going on in these parentheses so the first thing we're going to do is we're going to initialize an integer called i which has a value of 0. and 90 of the time this is what you're going to do so just as long as you just memorize that it's fine and then we need a condition so i can say while i is less than the number of triangles that i want to draw and then finally this is going to be a little line of code that gets executed after each iteration of the loop so once again we're initializing our counter our index as long as that's less than the number of triangles then we're going to keep incrementing it and we're going to do the code in here so if i put this print stuff in here now i say draw triangles five this is actually going to draw five triangles because it's going to say okay the number of triangles that i want to draw is 5 right we're passing that in as an argument as long as this index variable is less than 5 is less than the number of triangles that i want to draw i'm going to draw a triangle and so you know we could put like 10 in here it'll just do whatever we want it to do so that's now like this working function and this hopefully gives you an idea of like what we could use this like traditional for loop for okay anytime we need to like do something a certain number of times like unless there's like an array involved or a list like we saw then you can use this okay so that is drawing multiple triangles now there's one more big topic that we need to get to in this tutorial and then we're pretty much done with all the like core concepts but before we get into that i want to show you one more thing which is called comments now we've talked a little bit about comments and we can get rid of this this is this can just go away we talked a little bit about comments i think throughout this video but i really want to just like dedicate some actual time to it so a comment is a basically a tool that us as programmers can use to notate our code and to provide clarity for the different code that's in our program or just to kind of like what we do is called commenting things out so there's two ways that we can do comments there's what's called a single line comment and you'll see here my text editor is changing this so there's these two characters here two forward slashes that indicates a comment so anything that comes after these two forward slashes on this single line that's gonna be a comment and the comment is just like i said it's something that the computer is just going to ignore so this is for humans this is not for the computer so i could put for example if i put a print statement here and i said hello and we well whatever if i can type correctly if we said hello and then over here you'll see it says hello i could do what's called you know we call this commenting it out so i'll put these two uh and generally generally you'd want to put a space here but you i don't think you need to uh so you put these two forward slashes and now it's basically not part of the source code anymore so the computer is just going to ignore it won't execute it so that's a single line comments now if we go to the next line you can see that it it's back to non-comments you can make a multi-line comment by making a forward slash and a star and then you can close it off because you'll see you'll notice that like everything now below this is a comment so you can close it off with another star and then another forward slash so now everything that's inside of here is a comment but everything that's outside of it is not and these double line comments single line comments you can use them for anything one of the best uses for these is to comment out code so sometimes like in your code you might have like this is going to be a very simple example but okay let's say that we have three lines of code and one of them is wrong right so we're saying print instead of print i run this it tells me what's wrong right okay so i kind of know what the problem is let's say that i wanted to like try to run this without this line i could just comment it out or um i guess i could fix it too but i'm kind of making it i'm not really explaining this very well but if there's code that's in your program and you just don't want it to run on that execution of the program you just comment it out and that's uh that's pretty simple now there's a lot of different philosophies around writing comments one thing i will say though as a beginner it can be really useful to use comments to explain each line of code that you write so for example up in this prompt function like when you're first learning how to code it's really important that you understand what each line of code is doing so a lot of times what people will do as beginners is they'll come in and they'll comment each line so they'll describe what each line is doing with a comment so they can say here you know we're printing the prompt text onto the console right and then down here they could say we're getting input from the user and storing it in a string called answer and the reason that this wrapped around is just because my text editor is wrapping it's actually all in one line so you know and then we come down here and you know return the answer and stop execution of the function whatever so i'm just going through and like commenting all of my code and then especially when i'm learning how to code what it does is it forces me to understand each line now this isn't something that you want to do if you're like working at a job people will be really mad at you a lot of people get really upset when there's comments in the code if they're inappropriate comment well inappropriate i just mean they're not productive or anything but as a beginner it's such a good way to like hone your skills so i definitely recommend doing that okay so that's a little bit about comments and like i said comments can be really useful for a number of reasons but now i want to get into our last big topic and this is one of the biggest topics of computer programming but actually before we do this um i just want to say that up until this point we we have covered like just so much stuff so this is really the last piece that's gonna kind of hopefully bring everything together and that's something called object orientation or object oriented programming you also hear people refer to it as oop object object-oriented programming is a way of writing your code and a way of structuring our code so that it resembles objects that are in the real world and this is what's kind of like a revolutionary idea when people first started coming out with it like this idea of object-oriented programming like it's so cool and it allows us to write code and programs that are like oh like better able to model things that are out there in the real world okay so this is traditionally like i think kind of confusing for people but i'm gonna try to break it down like the best way i can so in what we talked about so far is we have all these different data types right so i have like a string which might be my name like mic we have a double gpa whatever like maybe your gpa is 3.5 we also have booleans right so is organ donor whatever right this could be true or false so we have these three main types of datas plain text numbers doubles or ins and then booleans these three types of data can basically represent just about anything that we would want right you could represent like any sort of number any sort of name or plain text or label and then any sort of true or false value but the problem is that not everything in the real world can be described using strings numbers or booleans alone a good example of this is something like a book so a book can't be represented just in a string or just in a number or just in a boolean because books have different properties and things about them right so a book might have a title a book might have a number of pages a book might have an author a book might also like if we're writing a program it might also have a status like has it been read or not or what percent completion do we have right so there's certain things in the real world that we might want to represent inside of our code that we just really can't do with any one of these data types right it's going to take a combination of these different data types in order for us to to be able to kind of represent a book and you know when we want to just represent a name or a number like we have data types for that so we have the string data type we have the double data type we have the boolean data type we don't however have a book data type so there's no in dart like the developers of dart did not build into this language a book data type that is not something that's given to us by default what they did though is they made this language object oriented and when we have object oriented languages something like dart what that means is that we can create our own custom complex data types so even though there's not a book data type built into dart because it's object oriented i can actually create my own book data type and that book data type will be will basically be made up of strings and doubles and boolean so we'll have this more complex more abstract data type which is which contains like strings and numbers and booleans and all those different things so that's really what object oriented programming is it's a programming paradigm that allows us to create our own complex data types that's basically it so let me go ahead and show you how we might do this now i want to be able to represent a book in my program so let's say i'm writing maybe i'm working for you know amazon i'm working on kindle product i want to be able to like store books so there's a couple of different things a couple different attributes that we might want to have in a book and really what we want to do here is we want to create our own book data type and that way we can use that book data type in our program for all sorts of logic so i'm going to show you how to set this up really quick and we'll just kind of go through and explain so anytime we want to create our own data type in dart we need to create something called a class and a class is essentially a blueprint for this new data type so when we define a class we're basically telling dart that this is what our new data type looks like so it's a blueprint it's a specification for this new data type so to create a class there's a keyword we can use called class and i'm going to call this book now generally you want to name these with capital letters so most of the variables we've been oh i mean pretty much all the variables we've been writing like you know my var like whatever it starts with these sort of lowercase letters classes are always going to start with uppercase so you're going to say class book like that and now what we want to do is inside of this book we want to specify what are the different things that make up a book because this is our new data type so we need to tell dart exactly what a book is what are the different properties that a book has and we can define those properties using the data types that we already know so a book for example might have a title and a book might also have an author a book may also have a number of pages why don't we call this num pages i'm gonna type that wrong okay so this is going to be an integer so what i'm doing here is i'm telling dart i want to create my own custom data type the data types that you've given me aren't enough so i need i want to create my own data type but this is going to be a complex data type this data type is going to have different properties or different attributes in it so this book data type is going to have a title this book data type's going to have an author and a number of pages and we're defining that here now this book class is only a specification so this is just us defining what this data type looks like now if we want to use this in our program we need to create something called an object so the class is the specification it's the blueprint an object is an actual book itself that we have in our program so once again this is like a specification this is us registering this data type with dart this is us telling dart hey this is the data type i want to use and then we can create different books in our program which we call objects okay so i'm going to show you how to create an object what you're going to do is you're going to say book and why don't we make this like a harry potter book this is the example i always use and over here you're going to say book and then an open and closed parenthesis so this right here this is the syntax that we're going to use in order to create what's called a book object in other words this is an instance of the book class basically this is like a book itself so the class defines that a book should have a title and author and a number of pages down here we're creating an actual book which is going to have its own title its own author and its own number of pages and let me show you how to do that so we've created this harry potter book and then down here i'm going to say and actually let's just call this hp because i don't feel like typing that out so hp and what i can do now is i can specify what the title what the author and what the number of pages are for this so i can say hp.title is equal to you know maybe like sorcerer's stone and then i can say hp.author is equal to jk rowling and then i don't know how many pages it has but we could say num pages maybe it's like 300 i don't know oh whoops hp dot num pages is equal to 300. so this is a special syntax that i'm using right here which is basically allowing me to specify what the title the author and the number of pages are so you can say hp and then you can use this little dot this is actually what we call the d reference operator and you don't really need to worry about what that means but hp and then you say dot and then you can refer to which one of the properties or the attributes basically which one of these things we want to work with so i can say hp.title is equal to sorcerer's stone hp.author rowling etc i can basically set each one of these and what's cool about that is then i can come down here and if i want to refer to for example the title of this book i could just print out like hp.title and now it should tell me the title of that so and let me see here i definitely messed this up um oh no okay so i have it's pages it's page here not pages okay i'm an idiot anyway it's a long tutorial so you can see down here that i type out hp.title and then it prints out the title i could do the same thing for author or for number of pages whatever it'll do all that so this book data type right this book class we're able to store different properties on it and what this allows us to do is model real world objects a lot easier so this is now like one container that has all the different properties that we might be interested in for a book automatically load it up into it and this is just really easy because now we can just say like hp.title or hp dot author and we can refer to those really easily so this is why like these classes and objects it's just a a better way to organize our code there's nothing magic going on here it's just like this is a better way for us to to organize things now once again i want to make the distinction between the class and the object and i know some of this syntax is funky we're actually gonna we're actually gonna talk about this but um like the class is the specification this is like the the blueprint that we're giving to dart that tells it what the book looks like we can create like actual instances of that book that have actual titles actual authors and actual number of pages which are called objects and it's these objects that we can work with in our program so the class it's not like it's not real it's not something that we can pass into a function or print out or anything like that it's just a specification the object is the actual thing it's the actual book that we're working with that has an actual title an actual author and an actual number of pages okay so object orientation it can be a little confusing but hopefully that makes sense i found that's kind of a good way to explain it there's more to do though we are far from done with with object orientation uh first off let's start with this look at this so right now i'm creating my harry potter book and it takes me four lines of code to create this harry potter book let's say that i wanted to create another object so we can create another book and why don't we always do like lord of the rings so that'll just be lord of the rings and this will be the book so i could say lotr now i have to set the title of this right so this is lord of the rings and then okay we'll do the author so what is this tolkien and then finally you can see this is getting a little tedious right this is not something that i want to be doing every time i create one of these objects i don't know maybe this is 500 pages so you can see here that when we we create this book and then oh and this should be a number yeah see i'm already messing this up this is why it's not a good idea to do it this way so in order for me to create one of these books it takes me four lines of code and i have to individually set like the title the author and the number of pages this doesn't scale very well and this is a problem that can be solved using something called a constructor so i'm going to walk you through what constructors do now the first thing that we want to do is we're going to come into our class here and you're just going to say book and then you're going to make an open and closed parentheses and then an open and closed curly bracket so this is what we call a constructor function okay the constructor function is a function which we can use to initialize each one of our objects with data so the constructor function will basically do this and it'll do this for us whenever we create an object now let me show you what happens here so i'm just going to say print and we're just going to say in constructor okay so i'm going to print in constructor and this is like right inside of this constructor function now if i run my code i want you to see what happens so you'll see here that it says inconstructor twice and the first time that we do that is when we run this line of code right here the second time is when we run this line of code so every time we create one of these books which is basically done using this syntax we are calling this constructor now this constructor is just like any other method or any other function so i could have this pass in like a string name and then down here i could say in constructor and then down here we'll put the name so i could pass in a name so here we could say like this is harry potter and then down here this is lord of the rings now check this out when i run my code you'll see here it says in constructor harry potter in constructor lord of the rings so i can use this just like any other function i can pass arguments into it and then i can use those arguments over here and so because this is just like a normal function we're going to use it the way that it's intended to be used which is to do what's called initializing these variables there's going to be some weird syntax coming up i need you to stick with me i'm going to explain all of it but i'm going to be moving some stuff around so actually first things first why don't we do this so i'm going to show you how we can make it so that we can pass in the title of the book into this constructor so inside of this book i'm going to inside of this constructor function i'm going to specify that it takes a string which is called a title and i'm i'm naming this intentionally with this little a you'll see why in a second so down here in the book i'm going to say this dot title is equal to a title so this is our line of code and now instead of using this line of code i'm just going to pass in the title that i want the book to have in the constructor and then i'm going to get rid of this so we'll do the same for lord of the rings and then we'll get rid of this so now instead of me explicitly saying hp.title is equal to sorcerer's stone i'm now passing this sorcerer's stone value into this constructor function it's going to get stored in this a title variable and then i'm saying this dot title is equal to a title so before we do anything let me just show you that this is working so let's print out hp dot title down here so let's do that and you'll see that it has that same title so we achieved the same end goal which is giving harry potter this title it's just that we did it through this process now the one thing that's probably not making sense here is this thing so it says this dot title the this keyword is a very special keyword that we can use inside of this class so anytime you're in a class you can use the this keyword and it will refer to the title of the object that's getting created i know that's a little wishy-washy we're going to do some more stuff with this so if that doesn't fully make sense yet it's okay but essentially what i'm saying here is i'm creating this harry potter book okay i create it by calling this constructor function now you can specify your own constructor function if you don't have one in there then it'll it just like will create the object for you okay so you don't need to have that there but you can so i'm creating this object and when i do it's going to call this constructor function and i can pass in a value for something now in this case the constructor is going to take a title as its first argument so sorcerer's stone now is going to be stored inside of this a title parameter and then over here when i say this dot title it's basically the same as me saying hp dot title is equal to whatever over here so this line of code and this line of code are basically equivalent the this keyword refers to the object that's getting created so when we call this function with for the harry potter object this dot title refers to the title of the harry potter object when we call the constructor with the lord of the rings object this dot title refers to the title of the lord of the rings object so it just depends on like which depends on like when we're calling this basically again if this doesn't make fully make sense that's okay okay object-oriented programming is like this is intense i mean when i was in college i had like three or four classes just on this so it's definitely a little crazy but hopefully you guys are getting the point at least a little bit so we're doing this with the title now we can do the same thing with the author so this is going to be a string [Music] author and then also for the gnome pages and i'm going to call this actually a author and a num pages we can talk about why in a second so then i'm going to do the same thing i'm going to say this dot author so the author of the object that's getting created is equal to the author that we passed in so over here we're going to pass in jk rowling as the second argument and then token as the second argument as well and then we can get rid of these cool and then we'll do the same thing for the num pages so this one was 500 and then this one was 300. beautiful all right and then last thing is we need to say this.numpages is equal to a num pages so now we've seriously reduced the amount of code that we need to write to create these objects and now we can run this and you can see that it's still working so we're still getting sorcerer's stone and let's try author and num pages as well okay looks like author is working and pages cool so these are the constructor functions they just make it really easy to create these objects now i want to talk about the naming up here so i called these parameters a title a author and a number of pages and once again i want to make the point that this is just like a normal function so when i call this constructor function i pass in the sorcerer's stone which gets stored in a title jk rowling which is stored in a author and 300 which is sort in a number of pages so most of the time you're you're not going to use these a's you're just going to call them author numpages and title i'm just putting these a's in here because for beginners a lot of times it's like it's not clear like the difference between this title and the title that's over here so hopefully you guys get that um but you're not going to see that in like production code bases you're going to see just using like title or whatever so let's keep this for now because we're just starting out so we want to make sure that we don't confuse ourselves but otherwise that is the constructor function so hopefully that makes sense and uh really like this is one of the core like parts of of object-oriented programming and here we're really like creating these classes okay now the last thing i want to do i think this is the last thing yeah last thing i want to do is talk about class functions so this should hopefully shine a little bit more light on kind of what we're doing here but this will be the last topic that we'll cover there's a lot more that we could get into with object-oriented programming but i think for a beginner's course this is probably just just the right amount so we'll leave it at that after this okay so i'm going to get rid of the book class i'm going to get rid of all of this i want to start over we're going to make a new class this one's going to be called student so once again we're specifying what this like complex data type is and the student is going to have two attributes so it'll have the student will have a name and then they'll have a double which is their gpa so it i don't know how it's done all over the world but in the states we have this concept of a gpa it's like a grade point average so it's between zero and four if you have like four your have straight a's if you have zero you're not doing too good so the student has a name and a gpa and then why don't we create a constructor function so this is gonna get passed in a name which is a string and another string which is called a gpa and then we're going to say this dot name is equal to a name and this dot gpa is equal to a gpa okay once again if this isn't fully clear that's okay don't worry about it it's uh it's fine so let's create a couple of variables so we want to create a student variable down here right it's going to store a student object so we're going to say student and then why don't we call this one's going to be jim and jim is going to be so we're going to call the constructor function so his name is jim and he has a gpa of 2.5 and then we're going to create another student called pam and pam is going to have a let's do 3.7 pam's pretty smart okay so we have jim and pam and they're both students and then down here this is just like we did before so we can print out you know gym.gpa whatever and that should work and actually whoa so i messed this up uh i said the gpa was a string this should be an integer or no this should be a double and then down here yeah i'm sorry i messed this up okay so down here is these are both doubles so this data type just needs to be a double common mistake okay so now let's see if we can get jim's gpa to print out cool so the last big concept here is that inside of these classes we can define functions and technically when a function is inside of a of a class we would call it a method so this is the i've been saying the word function and method interchangeably technically if you want to get like really technical with this you know if you're on stack overflow you want to make sure that you that your technical or people are going to yell at you but a method is a function that's inside of a class that's really i think that's like the official definition anyway so what i'm going to do in here is i'm going to create a function inside of this class that both of my students are going to be able to use so you can just create this function like anything else and what this is going to do is it's going to return a boolean value and we're going to call this has honors has honor so what this is going to do is it's going to tell us based on their gpa whether or not the student has honors and so we're going to say that like honors would be like you know they're distinguished in their class or whatever so if a student have has honors it means that their gpa needs to be 3.5 or above let's just say that's the criteria that we're working with so what i could do then is i could return whether or not the gpa this dot gpa is greater than or equal to 3.5 i'm going to explain what this does in a second but let me just uh show you how this works and so now because i've defined this function inside of my class i can call it so i can say gym dot has honors and this should print out true or false so you can see that jim doesn't have honors because his gpa is not greater than or equal to 3.5 if we did this with pam though pam run pam does have honors because her gpa is 3.7 so this whole concept it's this idea that we can create these little helper methods that are inside of our class and any of the objects can use them so the gym object and the pam object can both check to see if they have honors and when jim calls has honors so when the gym object executes this function this dot gpa is going to be jim's gpa when the pam object executes this function right when pam calls has honors this dot gpa is going to be pam's gpa so this function is particularly particularly useful because it can be used on all of the objects that we create from this class and one of the great things about this is that if we wanted to change what the qualifications for honors is so maybe instead of 3.5 we move it down to like 2.0 well now i change this in one place and when i rerun my code now we can see that both of them are having honors so i can change what the qualifications are for honors just by changing the value inside of this function and that is really really powerful okay so we've talked about and we've introduced some of the really core concepts in object-oriented programming and now the last thing that i want to do is really and this is going to be the last thing that we're going to do for this course is i just want to bring everything together so we've looked at like i said all the core concepts of computer programming i've walked you through each one step by step now we're going to take all of these and we're going to build our final program our final project and it's going to use all of these different things together so what we want to build is we want to build a little quiz program and what this program will do is it'll essentially walk the user through taking a quiz and we'll be able to grade that quiz as the user goes so we'll ask the user some different questions they'll answer those questions we'll keep track of how they're doing and and then at the end we'll sort of give them their grade and i want to do this with math questions just because i think it's kind of an easy way to do this so we're going to write a program that can give people math tests so it'll ask them math equations and then they can answer them so i'm going to walk you through this and we'll use object-oriented programming in this as well so let's go ahead and get rid of our little student class here and what i want to do is i want to create a class which is going to allow us to model math questions because the program that we're going to build is a quiz program and one of the most important parts of any quiz right is the questions and a question is a good example of something that we could put into its own class or into its own object because it has multiple properties so there's two properties to our math question and i'm going to create a class for this i'm going to say math or sorry class math question and this like i said it'll model a math question and there's going to be two properties two attributes that are part of this class so the first is going to be the actual question itself and this can be a string so the question could just be you know stored in a string it could be something like 3 plus 5 or you know it could be 20 times 10 divided by 3 whatever just some math equation that we want to ask the user to try and solve see if they can solve it that's going to be this question the second part of any question is the answer so we need to know like what's the what's the actual answer and once again this actually why don't we have this be a number so i'm going to have this be a double and this is just going to be answer so the question is in the form of a string like i said it could be 3 plus 3 the answer is going to be in the form of a number like 6 for example and you could make this a string as well it's not super important but i think it's better that it's a double so we have our question and this will allow us to model a question in our program now what i might want to do down here is i want to actually create some of these questions so we'll go ahead and create all the questions that we want to have in our quiz now remember the way that we can create a math question object is by first specifying the data type so we're actually going to store this in a variable so we want to specify that this is going to be a math question and we're going to store it in a variable called question one and i'm going to set this equal to and remember the way we create this object is just by saying math question like that and then this will call the constructor which we've just talked about a lot so what we want to do is create the constructor so let's go ahead and do that we can say math question open and close parentheses and then an open and closed curly bracket now inside of the parentheses we want to specify that we need to initialize the question and the answer so we're going to take in a question and then we're also going to take in a double called a answer so when i create the math question down here i now need to pass in the question why don't we just do three plus five and then i need to also pass it the answer in this case it's going to be 8.0 right because this is our this needs to be a double so in the constructor i want to initialize the question and the answer so i can say this dot question is equal to a question and remember this line of code here where i'm just saying like this dot question it's the same as if i said question 1 dot question is equal to whatever we'll do the same down here so i could say this dot answer is equal to a answer and it's the same as me saying question1.answer is equal to whatever the answer is so we're calling the constructor to create the object i'm passing in the question i'm passing in the answer both of these arguments are getting stored inside of these parameters up here and then i'm initializing the object with those values so this is all that same stuff that we've been talking about okay so i can create one question why don't we also make another question so we'll make two so this would be question two and why don't we change this one up so this one can be like 10 minus seven and in this case we're going to get 3.0 now you can create as many of these questions as you want i'm just going to show you like kind of how this works but you know theoretically you could have thousands of questions if you wanted now here's a situation where you can see that we're creating like question one question two and actually let's make one more because i think three is really gonna drive the point home so we have question one question two and question three this one is maybe a hundred times i don't know eleven or no let's do 100 times 9 so we can get 900. okay so we have three of these questions now one thing i want you to notice about these questions is that we're using these like numbered indexes right so i'm like okay this is question one this is question two this is question three and it makes sense that we need three questions because we're making a quiz the quiz probably should have more than one question now when you have a situation like this where we have multiple different questions right in this case all of these questions are just question one question two question three this is a great place to use a list so we've talked about these lists a list is a data structure that we can store different values in and it's just one related collection so because i have multiple questions because i have multiple pieces of related data i want to put these in the same data structure inside of the same list so instead of creating three separate variables down here which are storing three different math question objects up here let's just create a list and this is going to be a list which is going to store math questions and you don't want to make this plural you just want to say like math questions so this is a list of math questions and we're going to call this questions and i'm going to set this equal to an array or a list and so inside of this list instead of creating three variables we're just going to put these objects so i'm gonna copy this object i'm gonna put it in here right i'm gonna put a comma and i'm just doing this on different lines just so it's cleaner i'm gonna copy this one comma and then finally we will do this as well so instead of creating three objects and then storing these objects inside of variables instead we're just going to put all of these objects inside of this list now one thing i want to point out because i think this can be confusing for beginners as well is really what we're doing on this line here is we're storing this object so this right here what i have highlighted this is the object and i'm storing that inside of a variable called question one so that's what we're doing here i'm specifying i want to create a variable that stores a math question that's this it's going to be called question one and inside of question one i'm going to store this math question object so this over here on the right side this is the object this over here is all the stuff for the variable now in this case i'm not storing this question inside of this single math question variable i'm storing these objects inside of this questions list cool so now we have this questions list and this is just like any of the other variables that we've looked at so i could print out like questions at index position 0 and you can see that that gets printed out over here now we would want to say instead of just printing out question at index position 0 i could say questions at index position 0 dot and we could you know i could print out the answer or something and this is really common this is something that we haven't seen yet but really what i'm doing is i have my questions list here okay i'm accessing the zeroth question so this would be this first one right so i'm accessing the zeroth question so now this whole thing like what i have highlighted this whole thing might as well just be that this whole thing might as well just be this object that's essentially what it is so we're referencing this first object here and now i want to get the answer out of that so if i wanted to get the answer to this third question or you know yeah i could do questions two and that will give me that so this is really common is we have these complex data types these math questions this is a custom data type that i created up here so i created this and now i'm storing three of these inside of this list so now i have for my quiz i have this list of three questions that i might want to use and i can access specific ones of these for example by saying like questions one dot question and then that'll give me that okay so this is really really common is that we're doing something like that but this isn't this isn't all everything for the quiz right this is the first part and generally when we're building applications like this you're going to have the data and then you're going to have the logic the data is this stuff so this is the data that our program is going to consume and our program is going to use in this case it's just math questions in other applications it might be other different things but we have our data now which is set up so the this is all the data now what we want to do is we want to actually turn this into a little bit of a game so i want to go through each one of these questions i want to ask the user each one of the questions i want to get the user's answer and then i want to keep track of their score so we're going to do this step by step the first thing that i have to do is i have to use a loop and the reason i need a loop is because i want to be able to do the same thing for each one of these questions i want to be able to run the same code for each question basically for each one of these math questions i want to ask the user the question and i want to get their answer okay so let's go ahead and do that the first thing we'll do is we'll actually just loop through this array and let's just start with that so why don't we just loop through the array and print out all the questions so we'll loop through the list and print the questions so we're going to use another one of these for loops now we're going to use this for in loop we talked about this whenever you have an array data structure or list data structure and you want to loop through each element and do something the four in loop is your best bet so we can say for and this is going to be math questions so this is the data type i'm going to say math question and why don't we just say this is called question in questions so i'm creating this little question variable here for each one of the questions and then down here i could print out for example question dot question okay why don't we call this math question just so it's not confusing so we can say math question dot question and when i run this you'll see that for each one of these it should print it out now i spelled this wrong so that's not going to be good but you can see for each one of these math question objects we've printed out the actual question itself now the fact that this is called question maybe this is getting confusing um you could call this like question text if you wanted i'm just going to leave it like that for now because i think it's fine but if you're getting confused you can you can rename these variables okay so this is the first step we have our data and now what i want to do is i want to loop through the data so for each one of these math questions in the questions array i want to print out the question right there's two components to each one of these there's the question which is a string and then there's the answer which is a double so this is good this is our first step but remember we don't just want to show the user the question i mean we want to do this right we want to show them the question but we also want to get their answer and this is going to go back to all that getting user input stuff that we did earlier we have this prompt double function remember that we wrote this before so the prompt double function will print out enter a number and then it will say you know double my num and we like kind of parse this out i went through all this in a previous section so if you're not if you don't remember what we're doing here you can go back and watch that but basically this will allow us to prompt the user for a number and this is something that we will actually want to use in our little uh program here now one thing i want to change about the prompt double function is i don't want it to say enter a number instead i want it to say that the question over here so i'd want it to say like a hundred times nine or ten minus seven or three plus five that's what i would want to say and we have a way of doing that over here in the prompt function so remember the prompt function takes a string which is prompt text and then we print that down here we're going to do the same thing so i'm going to basically i'll just copy this and here in the prompt double this is going to take as a parameter the prompt text and then we're just going to print that out here so i'll say prompt text so this prompt double function now what it does is you can pass it in a prompt text and we're gonna end up passing it in like three plus five or ten minus seven or a hundred times nine it'll print that out and then it'll allow the user to enter a number and then it'll return that number to us so this is a really really useful function now that we can use and i'm going to go ahead and use that down here so every time that we go through this loop so for each one of the math questions inside of this questions array i want to ask the user the question and then i want to record their answer basically i want to get their answer back so let's go ahead and do that and we can use the prompt double function the prompt double function now it takes a prompt text so it takes a string called prompt text and then that's what it's going to use to prompt the user with so down here in prompt double we're actually going to pass in math question dot question so we'll pass this into here and let's see what happens now so theoretically like if i did this correctly which i think i did what should happen now is it'll go through it'll prompt the user with the question and then it'll allow the user to enter in their answer so three plus five is eight i could hit enter so it prompted us for that double now we're not doing anything with that so nothing really happens but you can see how this is working so 10 minus 7 that's 3 and then 100 times 9 would be 900. so i've sort of created this program now where it'll go through each one of these questions and it'll use the question property as the prompt and then it'll allow us to enter in the answer so this code right here is very simple but it's very powerful because now we can loop through each one of the questions and do something but this alone isn't uh like this isn't really the game right we or this isn't the quiz that we want we want to be able to get the user's answer and check to see if they got it correct or not so i'm actually going to take this prompt double stuff now remember prompt double is going to return the number so this will actually give us the number back that the user entered so i can store that inside of a variable so this is going to be a double and i'll call it answer or why don't we call it um user answer some of the these names are going to start to get a little confusing hopefully hopefully you can stick with me um so double user answer so we're going to store whatever the user answered for this particular question inside of here now this is going to happen each time we go through the loop so it's going to happen for each one of these questions now what we want to do is we basically want to check to see whether or not they got the answer correct and we can do this using an if statement so what i'm going to do is i'm going to check to see if the user's answer is equal to the answer to the question so i can say math question dot answer now this is what we want to do because remember user answer this is what the user enters in so if i come over here and i enter this 8 that's going to get stored inside of user answer and i want to check then to see if whatever the user entered is equal to whatever the answer was to the question when i created all these questions i passed in the correct answer so i had to say hey this is the answer to this question so down here i'm just checking to see if what the user entered was what the answer is supposed to be that we entered in up there so if this is the case we can print out correct and then otherwise we'll use this else we can print out incorrect and then we could also say like incorrect the answer was and then we can interpolate in here math question dot answer okay so one more time let me just walk you through what we did here so we got the user's answer using this prompt double function that we wrote earlier in the course that's going to be stored then inside of user answer so this is this it's whatever the user put in over here on the console if the user's answer is equal to the answer to the question which we specified up here when we created these objects then we're going to print out correct otherwise we'll print out incorrect and we'll tell them what the answer was so let's go ahead and see if this code works i'm going to run this okay so it says three plus five so let's try to get one correct so three plus five is eight so i'm gonna hit eight now what should happen is i'll hit return i'll hit my enter key that 8 will get stored inside of user answer and then we'll check that against the answer to the question the answer to the question is this 8 so it should print out correct so i'm going to hit enter and you'll see that it says correct cool so now we have 10 minus 7. i'm going to get this one wrong the answer to this should be 3 but i'm going to put a 6 in here so when i hit enter once again we'll store that 6 inside of the user answer variable that's coming from this prompt double function i'll check to see if the user's answer which is six is equal to the answer to the question which is three that's going to be false so then we'll come down here and print out this incorrect so i'm going to hit enter and you'll see it says incorrect the answer was 3. and then finally 100 times 9 we can just put in 900 and that'll be correct again so right here this is combining all this stuff that we've learned we have lists we have object orientation with these math questions we have for loops we're getting user input we're using if statements all these different things are being used here and really this like brings all this stuff together in i think kind of a nice way now this is fine so this is a good way of of doing it but um instead of saying like correct or incorrect or correct here i want to show you how we could actually like keep track of what the user does and then give them a grade at the end so instead of just telling them right away we can then we can say like hey you got two out of three correct so you got like a 66 or whatever what we want to do in order to do that is we can create an integer which is called score and this is just initially going to be equal to zero so every time the user gets a question correct instead of printing out correct or incorrect instead we're just going to increment this score so when the user gets a question correct we'll add one to their score if they get the question wrong then their score will just stay the same so they won't get an extra point and in order to do this it's actually going to be really easy so you can really get rid of this else and then inside of this if instead of printing out correct you can just say score plus plus so now each time we go through this for loop if the user gets the answer correct we'll increment the score and that's all we're going to do in here so let me clean this up a little bit now down here outside of the for loop i can print out using this information so i can say and we'll use interpolation here you got and i want to say how many they got correct so this is going to be their score and then we'll say like out of and then we want to put how many questions there were total so we can figure out how many questions there were total by saying questions.length so here i'm saying you got and then this is the number that they got correct remember score is incremented every time the user gets the answer correct and then we say you got score out of questions.length this is just going to be how many questions were inside of this array and we've looked at that before in the array section so let's go ahead and run and we'll see how we did so i'm going to get this one correct we'll get 10 minus 7 correct and then we'll get the last one wrong so now it should say you got 2 out of three just like that awesome and then if you wanted to give them a percent like we could just divide these so you could take the score and divide it by the number of questions and actually we might need to multiply that by 102. so eight three whatever okay so yeah and if we multiply that by 100 then we should get a score okay so why don't we try to get a hundred percent so i'll get eight three and then this is nine hundred says you got a hundred and then we can run it again and we'll just get one of them wrong so eight three eight and now we got a 66 perfect so that is the final project in this course this is our little questions thing and let me say this so all this code will be available but let me say this like if you can understand this question game you are like solid you are a solid computer programmer seriously this like this program right here is the culmination of the last whatever it is four hours that i've spent teaching you dart so if you can go through this and you feel like you understand what it's doing and you feel like everything's working like you have learned all these core concepts at least a basic level so this is like a good benchmark to measure yourself against if you can go through and write this entire program without you know referencing this video then i'd say you're on like a pretty good track okay and look if this is confusing to you like absolutely no worries this is a pretty complex program so i put this in here intentionally because it is pretty difficult and because it combines all of those topics okay but it's something to i guess strive towards but really like i said it combines everything that we've that we've done so far and uh yeah i'm pretty happy with this all right this is a long tutorial it's like four hours but we've covered everything that i wanted to cover so i'm really happy about this now let's talk about next steps so this dart programming language really useful i love this programming language i've had a blast filming this video and really what you want to do with dart and really kind of the whole reason why dart exists is there's this other really cool framework which is called flutter and flutter can be used to build mobile apps it can be be used to build web apps i think probably soon it'll be able to build desktop apps this is something that's been developed by google kind of from the ground up and it's a really solid framework i don't have a flutter tutorial at the time of making this video i'm making the dart tutorial first as kind of an introduction into that so i do plan on making a flutter tutorial if you want to see a flutter tutorial you want to learn how to build some apps using this stuff and take it to the next level just leave a comment or a like on this video let me know that you want that and maybe i can get around to it in the next couple weeks but otherwise i'm pretty happy with this so i just want to say thank you so much for watching if you've enjoyed the course and you want to support me and help me to keep doing this you can leave a donation i'll the link is in the description i i always appreciate that but otherwise yeah this has been great thank you for watching and i will see you in the next one
Info
Channel: Mike Dane
Views: 140,882
Rating: undefined out of 5
Keywords:
Id: 5xlVP04905w
Channel Id: undefined
Length: 233min 46sec (14026 seconds)
Published: Mon Feb 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.