C++ Tutorial for Beginners - Full Course
Key Takeaways
Introduces C++ programming language using Windows and Mac installations
Full Transcript
Hey, welcome to Draft Academy. My name is Mike. In this course, I'm going to be teaching you guys everything you need to know to get started writing C++. C++ is one of the most popular programming languages around, and for good reason. It's an awesome programming language. It's actually a language that is closely related to the C programming language. In fact, C++ is essentially just like the next level of the C programming language. So, if you're already familiar with the C programming language, then you're going to have no problem picking up C++. But even if you're not, C++ is a great first programming language to learn. And in this course, I'm going to teach you guys all of the core concepts for C++. We're going to start off just with the basics. We're going to, you know, get everything set up. We'll install uh C++. We'll get you guys set up with the text editor and I'll show you guys how to write your first program. Then we're going to get a little bit more advanced. So, we'll start looking at things like variables and we'll work with data. We'll even write a couple different programs and some little games. And then we're really going to get into the thick of it. We're going to learn all sorts of different programming structures, things like loops and if statements and classes and objects. It's going to be awesome. We're going to cover all of the core concepts, not just in C++, but all the core concepts for programming in general. So, the knowledge you learn in this course will actually be able to carry over to potentially other programming languages as well. I'm really excited to be able to bring you guys tutorials on C++. It's an awesome language. It's super fun to develop in and it's a good first language to learn if you're just getting into programming. So stick around for this course. Um you can kind of work through the videos at your own pace and hopefully you guys can learn something about this awesome language C++. In this tutorial, I'm going to show you guys how to get everything set up to start working with C++. So in order to start working with C++ and start writing programs and doing all that fun stuff, we actually have to do a little bit of setup. We're actually going to need two things on our computer. The first thing we're going to need is a text editor. And we're basically just going to use this text editor in order to write our C++ programs. Any text editor is going to work. Generally, you don't want to use a text editor like Microsoft Word or Google Docs. You're going to want to use just like a basic text editor. It could be something like Notepad. Um, I'm going to show you guys a special text editor called an IDE, which stands for integrated development environment. And we can use that text editor. Uh, and it's basically just a an environment that's really awesome for writing uh C++ code. Um, and then we're also going to need in addition to a text editor, we're going to need a program called a compiler, specifically a C++ compiler. And this is a program that'll basically take the C++ code that we write and it will translate it or transform it into a language that the computer is going to be able to understand. So, as long as we have a text editor and we have our C++ compiler, then we're ready to start programming in C++. So, I'm going to show you guys how to get all that set up. First thing we're going to do is head over to our browser. And I'm over here on this website called Codeblocks. It's just www.codbelocks.org. And Codelocks is what's called an IDE. So, it's an integrated development environment. It's essentially just a special environment, special text editor that can be used to write uh C++ code. So, this is going to be exactly what we need. I'm just going to click downloads over here. And you'll see there's a few options. One says download the binary release. Download the source code. We're going to click download the binary release. And this is going to bring us to uh this page. You can see there's a link over here for Windows, XP, Vista, 7, 8, etc. And there's also links for Linux and Mac. Let's click on this Windows option. And over here, you'll see there's a bunch of these different options. So, what we're going to do is we're going to download this one over here that says Codelocks601 MinGW setup. And actually, what's cool about this is it's going to include not only the CodeBlocks text editor, but this is also going to include that C++ compiler that I was talking about. So, we can download both of those things in this one download for CodeBlock. So, this is the easiest uh way to do this as a beginner. So, let's go ahead and download this. I'm just going to come over here and click on this link for SourceForge. And this should start downloading the file onto our computer. When CodeBlocks is finished downloading, let's go ahead and open that up. So, I'm just going to head over to my downloads folder. And we can open that guy up. So, you can see over here it says Codelocks601 MINGW setup. Let's open this. And this should open up into an installer window. So, let's click through this installer window. And we can just select all the default options. And this should start installing Codelocks on our computer. So once CodeLocks is finished downloading, then we have everything we need on our computer to start writing in C++. And you can see over here I just opened up CodeBlocks. And the next tutorial we're going to talk about how to set up your first CodeBlocks project and get started writing C++. In this tutorial, I'm going to show you guys how to get everything set up to start working with and start writing programs in C++. In order to set this up, we're actually going to need two different things. The first thing we're going to need is a text editor where we can write our C++ programs. And really for C++, you can use any text editor that you want. Um, so a lot of people might use like something like text edit or maybe a different text editor. In our case, we're going to be using something called an IDE, which stands for integrated development environment. And there's an IDE, which is called Codelocks, that is specially designed for writing C++ programs. So, I'm going to show you guys how to download that. We're also going to need one more thing, which is going to be a C++ compiler. And basically, this is just a program that takes our C++ code that we write and it translates it into a language that the computer can understand. So when we want to run our programs, when we want to, you know, execute the programs with the computer, we can use this compiler to translate the C++ that we write into computer code. So the first thing I want to do is show you guys how to get that compiler. And it's possible that if you're on Mac, you might already have it. Um, but what we want to do is go up here to this u search bar, and we're just going to type in terminal. And I'm just going to click enter. And this should open up the terminal. This is basically just a window or a program that we can use to interact with the computer using text commands. So down here, we want to check to see if we already have the C++ compiler installed. So you just want to type in GCC-V and just click enter. If you have GCC, which is the C compiler we're going to be using installed, then all this stuff should come up with like a version number and and everything. Um, if this doesn't come up, in other words, if you know it says that you don't have it, all you need to do to get this is just say Xcode select hyphen install. And this is going to go off and install everything that we need for C++. So, I'm just going to click enter. And I already have these installed. So, these are like command line tools. You can see I already have them installed. But if you don't already have them installed, then this will basically just prompt you to install them. Once that installer is done running, then you can just check to make sure that you got GCC. So you can just say GCC-V and make sure that you have it. So once we have this uh C++ compiler installed and we've run this Xcode select install command, now what we want to do is get a text editor. So like I said, we're going to be using a special text editor called CodeLocks, but really you can use any text editor that you want. Um, so I'm going to go down here into my browser and I'm over here on this website, uh, codelocks.org. And this is the website where we can download this program, codelocks. I'm just going to click downloads. And over here, there's a couple options. You want to click on the option that says download the binary release. So I'm going to click this. And you'll see there's this little like list here. Just click on Mac OSX. And this will bring us down here. So it's basically just a zip file that contains the Codelocks application. over here. We can download it from SourceForge. I'm just going to click this and this should redirect us to SourceForge and download the file for us. When CodeBlocks is done downloading, I'm just going to go over to my downloads folder and we'll see what we get. So, we just got this zip folder. I'm just going to double click it and you'll see over here we get the CodeLocks application. So, what you should do is take this and drag it over to your applications folder. That way OSX knows that this is an application we want to use. then you have everything that you need to start writing C++. So, in the next tutorial, we're going to set up a project in CodeBlocks. We're going to set up our first C++ file and then we're going to run it and we'll just get everything set up and ready to go. In this tutorial, I'm going to show you guys how to get your first C++ project up and running in CodeLocks. So, over here, I just opened up CodeBlocks, and this is sort of like the welcome screen that we get when we first open it. You see over here there's a couple options. Uh one says create new project. And that's actually what we're going to do. So before we're going to start writing our C++, I'm just going to show you guys how to get a C++ project up and running and uh ready to go in CodeBlocks. So let's click create new project. And this should open up this little window over here. So you'll see there's all these different options. These are essentially just uh different types of C++ applications that we can create. We're going to just do the bare basics. So you want to come over here and click console application and then just click go. And we're just going to click next. And over here you'll see we can select between a couple different languages. We want to click C++. So I'm going to click next. And we're just going to give this project a title. So I'm just going to call it draft. And you'll see down here I'm storing it inside of my desktop. If you want to change it, you can come over here and you can put it wherever you want. Let's click next. And you'll see over here, we just have a bunch of options. You can just leave these as the default. And we're going to click finish. Once we click finish, then our C++ project should have been created. So over here in our little tree viewer, you'll see that we have our C++ project. And down here inside of this sources folder, we have this file main.cpp. And CPP is a file extension that stands for C++. So I'm just going to rightclick this and I'm just going to open it. And you'll see over here we have a bunch of code that's already been included in this file. So this is essentially just like the most basic C++ file that you can write. I mean this is uh what we call like a hello world program. So you'll see down here it's just printing out hello world onto the screen. And uh don't really worry too much about what this stuff up here is. Um really let's just focus on this line down here. This line is actually going to print something out for us. So, if I was to come over here and I'm actually going to come up here in my editor and I'm going to click on this build and run option. So, when I click this, a window should pop up and it should basically just say like hello world. You'll see down here this little black window popped up and it says hello world. So, let's just go over some basic terminology. Um, generally whenever we're going to run a program in C++, we're going to build the program and then we're going to run the program. Building the program basically means that we're taking all this code over here and we're converting it down into a language that the computer can understand. So the first thing we always have to do is build the file. Then what we want to do is run the file, which basically means we're telling our computer to execute all of the instructions that we wrote in our program. And there's a bunch of different ways we can do that here in CodeBlocks. You can click this little um cog wheel up here and that'll build your program. And then you can click this play button over here. that's going to run your program. But a lot of times if you just wrote some new code and you want to test it out, you can just click this build and run option. And for the most part in this course, we're always just going to be whenever I say we're running the program, I'm just going to click this button and we'll build it and run it at the same time. So like I said, this is a very basic C++ file. In the next tutorial, we're going to talk about like what all this stuff is. We're going to look at writing some of our own instructions, maybe modifying these instructions, and we'll kind of dive a little bit deeper into, you know, just sort of the the bare basics. But for now, we have everything set up. So, we have our C++ project set up. We have our first C++ file called main.cpp. And we're ready to start programming. In this tutorial, I'm going to talk to you guys about the basics of writing a C++ program. We're just going to talk generally about what programs are and how we can write them. And we're going to write a little program which is going to print out a shape onto the screen. So, this is going to be pretty fun. Down here, I just have this basic program. And when I first created my C++ project in the last tutorial, we sort of got this basic program. And this was kind of given to us. And this is, you know, essentially just a very simple C++ program. So, I'm going to walk you guys through essentially what we have here. We're going to talk about a couple different things and then we'll start writing some code. So up here we have these two lines of code. Um this is like #incclude io stream and this one says using namescape std. Essentially what this does is it's kind of like configuration options for our C++ file. As a beginner like you know don't really worry too much about what this stuff is or what it's doing. We're going to talk more about all of this stuff later in the course, but for now, just know that you need to have it there in order for us to write our programs. Down here, we have this line. It says int main, and there's an open and close parenthesis. This is actually what's called a function in C++. Again, don't worry too much about what a function is. I'm just kind of throwing out these words so you're kind of familiar with hearing them. But essentially what this is is it's it's a container for us to put the code inside of our C++ programs. So this function called main is a very special function because any lines of code that we put inside of here, in other words, any lines of code that we put in between these open and closed curly brackets is actually going to get executed when we run our program. So down here, I just have this thing. It says return zero. And again, don't worry too much about what this is. Um just know that you need to have this in this main function. Over here though we have an actual line of code. So this says see out and then it says hello world then it says end l and this is a basic line of code in our program. So again any lines of code that we put in between these curly brackets for this main function is going to get executed when we run our program. So, if I was to come up here and run my program, and I'm just going to click this build and run option right here. You'll see when the program runs, it prints out hello world. And you'll see this little window that opened up here. This is what we would call the console. And the console is basically just a little window that will output information. So, whenever we run our C++ code, this console window is going to open up. And sometimes we can tell C++ that we want to print things out onto this console window. So if you ever hear me referring to the console, I'm just referring to this window right here. Now let's get down and start talking about uh programming. So again, any of the code inside this main function is going to get executed. And here we just have one line of code. So what I could actually do is I could copy this and down below here I could say something else. So I could say just like my name Mike. And now when I run this program you'll see it's going to print out both of these things. So here on the first line it prints out hello world. And then over here on the second line, it prints out mic. And this is just sort of like basically how we could print something out to the console. You'll see over here it says C out and that stands for console out. And then over here you'll see we have this little line. It says end L and this stands for end line. And basically what this means is we're ending the line where we're going to print out text. So you'll notice that we printed out hello world and then on the next line we printed out Mike. So, using these little print statements, I'm actually going to show you guys how we can write a little program that's going to draw a shape out onto the screen. So, I'm actually going to go ahead and get rid of this text, and I'm just going to copy this guy here a couple times. So, we'll make like four of these, and I'm going to print out a triangle onto the screen. So, I'm just going to print a forward slash, and then we'll do a space and a forward slash. And I'm just going to keep doing this. And you'll see we're kind of drawing this little diagonal shape upwards. Now I'm going to use vertical bars and we're going to go all the way down. So I'm just going to keep going like this. And down here, why don't we put some underscores and then we'll put the last vertical bar. So you'll see over here, I'm actually writing out a bunch of instructions. And each of these instructions is telling C++ that we want to print out a different line onto the console. So now when I run my program, you'll see we're actually printing out this little triangle. So this is a very basic program, but you'll see just by using those four simple instructions, we were able to actually draw something out onto the screen. So let's talk about this. Essentially, what's happening when we click that run button is C++ is going and it's looking inside of our program. And the first thing it's going to do is it's going to look for this main function right here. So it's going to look for this block of code. And that's basically just what a function is. It's going to look for this block of code called main. And inside of these open and close curly brackets, it's going to execute all of the instructions inside of there. And basically, when we're writing a program, all we're doing is we're just telling the computer a bunch of instructions. So, we're giving the computer a bunch of instructions that we want it to carry out. So, imagine that uh for example, in real life, like you were using a recipe or something. A recipe is a lot like a program. A recipe has a list of instructions and if you follow those instructions correctly then you end up with like something delicious. That's basically what a program is. Program is just a collection of instructions that we're giving to the computer. And we can give the computer simple instructions like this and we can do something simple like draw out a shape. But as we go through this course and we learn more and more complex instructions and we learn how to use them together in unison with each other, we're going to be able to tell the computer to do a bunch of complex things. So the next thing I want to talk to you guys about is the order that these instructions get executed. So just like if you were following a recipe and you would start with the first instruction and you'd go down to the last instruction, the computer is going to execute these instructions in order. So, it's going to start with this instruction and basically here we're just telling the computer or we're telling C++ that we want to print this line of text out onto the screen. So, C++ is going to do this and then once it's done with that, it's going to move on to this instruction. It's going to print this out to the out onto the screen, etc. It's going to keep doing that. So, actually, let me show you guys. If I was to get rid of this line and I was to put it up here, now you'll see when we run our program that we're going to print out a funky looking shape. So instead of this base being down at the bottom here, we're going to print it out on the top. And that's because we change the order of the instructions. So again, all a program is is it's just a set of instructions that we give to the computer. That's it. It's very simple. The more complex instructions that we can give the computer and the more ways that we're able to combine those instructions in different ways, the more complex our programs are going to be. So, as we go through this course, I'm going to be teaching you guys all sorts of more complex instructions. We're going to learn different ways to do different things, and we'll use real world examples to build little applications, and you'll end up learning a bunch along the way. In this tutorial, I'm going to talk to you guys about using variables in C++. Now, a lot of times when we're writing programs in C++, we're going to be dealing with all different types of data and information. And a lot of times when we're dealing with that data and information, it can be kind of hard to keep track of. And a variable is basically just a container where we can store different pieces of information or different data values in our programs. And it makes it a lot easier for us to manage and maintain and use that data. So I'm going to show you guys an example and we'll I'll show you guys basically how variables are useful and how we can use them in C++. So down here I have a very basic program that I've written out. You'll see down here I'm basically just printing out a bunch of lines of text. It says there once was a man named George. He was 70 years old. He liked the name George but did not like being 70. So this is basically like my little story here. And you'll see I can run my program and this is a valid program in C++. It basically just prints out the story and we have all of our information. So you know this is a a pretty nice C++ program. it serves its purposes. But let's say that I'm looking at my story and I'm thinking, hm, maybe I want to change the character's name, right? So maybe I don't like the name George. Maybe I want to change it to a different name. So what I could do is I could go through and I could manually change it in each line of code. So I could come here where it says George and I could change it. Let's say we want to change the character's name to John, right? So I can change it to John. I'll keep looking through the story. Down here it also says George, so I'll change it to John. And there we go. We've changed uh the character's name. We've officially updated it. Let's say maybe now I'm thinking to myself, I think I want to make the character a little bit younger. Instead of 70, why don't we make John 35? So, I can do the same thing. I can go in and manually change the value of 70 to 35. So, we can come over here and we can say he was 70. We'll change this to 35 and we'll say did not like being 70. Okay. So, we'll change that to 35 as well. So, now we've officially updated our story. We've changed the character's name and we've changed the character's age. Here's the problem, though. In order to make those changes, I had to manually go through and individually edit each one of the places where the character's name showed up or the character's age showed up. And imagine that instead of having a story that was only four lines long, I had a story that was like hundreds of lines. And we mentioned the character's name hundreds of times and we mentioned their age hundreds of times. Well, all of a sudden it becomes a lot more difficult for me to manage those pieces of information. So, it's difficult for me to manage the character's name and the character's age. Right? If I had a story that was hundreds of lines long and we mentioned the character's name 100 times, having to, you know, go and modify and update that name would be a very tedious task because I'd have to go through and essentially just do it manually. This is where something like variables come in. A lot of times in our programs, we're going to have different pieces of information, different data values that we want to keep track of and we want to be able to manage. So, what we can do is we can take those pieces of information and we can put them inside of containers called variables. And a variable, like I said, it's just a container where we can store a piece of data and it'll make it a lot easier for us to use and manage that piece of data in our programs. So, I'm going to show you guys how we could create a variable that could store the character's name and the character's age. And you'll see why this can be useful in something like this. So, over here, I'm going to create a couple variables. When we create a variable in C++, we actually have to tell C++ a couple things. The first thing we have to tell C++ is what type of information we want to store inside of the variable. Now, in the next video, I'm going to talk to you guys all about the different types of data that we can use in C++. But for now, I'm just going to show you guys two types of data. The first type of data that we can store inside of a variable is called a string. And that basically means it's a string of characters. So, it's like plain text. So, this down here where it's saying there once was a man named John. This is a string, right? It's plain text in our program. A lot of times we're going to be dealing with strings. So I'm going to create a string variable. In other words, I'm going to create a container that can store a string value. So I'm just going to say string. And that's the first thing that we have to tell C++. The next thing we have to do is give this container, give this variable a name. So what we want to do is give this a descriptive name, which will basically tell us what is inside of the variable. So I'm just going to call this character name. Just like that. And what I can do now is I can give this a value. So I could say character name is equal to and now we can type in the character's name. So I could say John just like that. All right. So once we've created this character name variable, now this value John, this string value is now stored inside of this character name variable. The next thing we're going to do is create another variable to store the character's age. In addition to storing data in the form of a string, we can also store numbers. Now, age is a whole number. So, what I can do is I can store it inside of something called an integer. And an integer is basically just a whole number. So, I could just say int and I can just call this character age. Now, I want to show you guys another thing that we can do. So, up here, what we did is we said string character name and we set it equal to a value right away. But what I could also do is put a semicolon here. And also I do want to point out um whenever we're writing lines of code in C++, every time you finish writing a line of code, you want to put in this semicolon. I'm not sure if I mentioned that in the last video, but the semicolon basically tells C that we're done with that line of code. So this separates one line of code from another. So you need always make sure you put these semicolons. But with a variable, what I could do is I could say int character name. I could do the same thing for string. And then I could go on to a new line and I could give this a value. So I could say like character age is equal to and now I'm just going to type in a number. So we said that John was going to be equal to 35. And you'll notice when we use numbers, we don't have to surround these with quotation marks. We just can type out the number. So now we have two variables. And I showed you two different ways that we can create them. And what we can do is we can use these variables inside of our story. And you'll see how this makes it a lot easier for us to maintain this program. So what we want to do is we want to replace every instance of the character's name and the character's age with the variable. So instead of just printing out John here, I want to refer to this variable. And the way that we need to do that is we basically need to include this variable. So over here I'm printing out this string of text. I'm printing out a bunch of plain text, right? But let's say instead of just printing out John here as plain text, I wanted to instead print out the value that was stored inside of the character name variable. What I can do is I can just get rid of John and I can say less than sign less than sign. And what this is basically going to tell C++ is that we want to take the value that we're going to type out here. So I'm just going to type out character name. And it's basically telling C++ that we want to take this value and we want to insert it right here inside of this line of text. So when I go ahead and run my program now, you'll see that we're still printing out there once was a man named John, except now I didn't actually type out John. All I did was include this variable right here. And I basically just said that we want to put the variable right in there. So that's how we can include a variable inside of one of these print statements. So over here I'm going to do the same thing. So I'm just going to say less than less than character name. And now this is going to insert the character name. In other words, it's going to insert the value stored inside of the character name variable at this position. We can do the same for the age. So over here we have the character's age. I'm going to get rid of that and I'm going to say less than less than. And now character age. And so the value inside of the character age variable is going to get placed right in there. And we have one more place where we have the character's age. So over here, I'm going to get rid of this. And now this is going to be special. So you'll see over here, we want to insert the value inside of the character age variable right in between all of this text. So I want to put it right here. What I can do is I can make two quotation marks. And essentially what this is doing is it's saying this is going to be its own string of text and this is going to be its own string of text. And I can make two less than signs. I can type out the character age variable. And then I'm going to make two more less than signs. And this is essentially just going to string all of these together. So it's going to say print out this text, then print out the value inside of the character age variable, then print out this text. So, I've now replaced every instance of the character's age and every instance of the character's name with those variables. Let's run our program and see what happens over here. You'll see we have the same exact story as we did before. There once was a man named John. He was 35 years old. John 35. So, without having to manually ch do anything, we were able to include those values. And now what's cool about variables is if I wanted to update the character's name or update the character's age, all I have to do is change it up here in one spot. So if I wanted to change the character's name to Tom and I wanted to make Tom, let's say 50 years old, I only have to modify the values that are getting stored in the variables and they'll automatically update down in our story. So now when I run my program, you'll see it's using the name Tom and it's saying that he's 50 years old. So that's kind of an awesome way that we can use these variables. Another cool thing that these variables allow us to do is modify the values. So let's say that halfway through our story, I wanted to change the character's name, right? So halfway through I wanted to make the name be a different name. All I have to do is say character name and I can actually assign this a different value. So I could give this the value of Mike. And again, I'm going to need a semicolon here at the end of this line of code. And now you'll see halfway through the story, the character's name is going to change. So it says there once was a man named Tom. And down here it's using the name Mike. So, not only was I able to just use those variables to insert these values, but I can actually modify those variables at different places in my program. So, that's pretty awesome. Now, this is just sort of like the bare basics of variables. Variables are containers. They allow us to maintain and keep track of the data and the values in our programs a lot better. And they also give us the advantage of only having to assign a value once. So I can assign a value once up here and then I can use it and refer to it in different places down here. I can also modify those values at different places in my programs. So in this tutorial we talked about storing values as strings of text and as integers which are whole numbers. In the next tutorial, I'm going to show you guys all of the different types of information and the different types of variables that we can create in our programs. In this tutorial, I'm going to talk to you guys about the different data types in C++. A data type basically just means a type of data or a type of information that we can use and work with inside of our C++ programs. So, there's all different types of uh information we can store like text, different types of numbers, decimal numbers, true false values, all sorts of stuff. And I'm going to kind of walk you guys through what all of those are and how we can use them. So, the easiest way for me to demonstrate this is just going to be to create a bunch of different variables. So, any of the different types of data that we can work with in C++, we can actually store inside of variables. So, I'm just going to create a couple different variables and I'll kind of show you guys exactly how this is going to work. So, the first data type I want to talk to you guys about is called a character. And a character basically allows us to represent one single character. So, the way we can create a character variable is just by saying ch. And now I want to give this a name. So, let's just call it like grade or something. And whenever we create a character, we're going to use these single quotation marks. So I can store any like regular character that I would want inside of here. So you know essentially any character that you can think of you could store in here. And then we're going to include this semicolon. So that's basically like the character uh data type. And in addition to just storing one single character, there's going to be a lot of situations where we're going to want to store like more than one. And what we can actually do is use something called a string. And a string is basically just a string of characters. So it's instead of just being one single character, it's like a bunch of different characters. So this would be like essentially just plain text that we would see in a program. So I could just say string and we could just call this like phrase. And when I create a string, I can use these um double quotation marks. So I could say like draft academy or something. And now we're going to be able to store instead of just one character a bunch of different characters. So there's different situations where you might want to use either like a char or a string, but for the most part, I I think strings are probably a little bit more commonly used than chars. So instead of just plain text, we can also store and work with numbers. So essentially, there's two different types of numbers, two basic types of numbers. There's whole numbers and then there's decimal numbers. Some people also call those floatingoint numbers. A whole number is like a counting number. So think like 1 2 3 4 5, right? They're just whole numbers, solid numbers. There's no decimal points, right? Then we have decimal numbers. So it' be like 0.5 or 1.267 or 10.11. You know, basically a number that has a decimal point after it. When we work with these different numbers, C++ is actually going to distinguish between them. So the first type of number we can work with is an integer. So I could just say int and this could be like an age or something. So whenever we're creating a number, we can just type out the number. So I could say like 50. And you'll notice I don't need any quotation marks. I don't need anything special surrounding this. I can just type out the number. In addition to positive numbers, you can also use negative numbers. But anytime we're using an integer, you can't have a decimal point. So you can't do anything with decimals. These are just going to be solid whole numbers. If you want to work with a decimal, you have two basic options. So there's two data types that represent decimals in our programs. The first is called float and this basically just stands for like floating point number. The second one is called double. Now you'll hear different people talk about these different uh data types. The main difference is just how many decimal points they can store. So a double can store more decimal points than a float. So, if you need a number to be very specific as far as like how many decimal points you can take it to, then you definitely want to use a double. And I would say for the most part as a beginner, just only worry about doubles. Um, floats will be used more in specific circumstances. But, you know, as you're just learning this language, really just worry about doubles. So, we could just say double like GPA. And I could set this equal to like 4.5 or like 2.3. Basically, I could make this any decimal number that I wanted. Keep in mind, you could also do like 2.0. So, it doesn't have to be um like a different number decimal. And again, you can make these negative. That's no problem. So, ints are going to be what we're going to use for whole numbers for the most part. Doubles are going to be what we're going to use for decimal numbers. So, that covers text and numbers. And just with those two data types with text, we can use characters and strings and then ins and doubles, you can represent like just about any type of information in your programs. But C++ is awesome. So they're actually going to give us another data type which is called a boolean. And a boolean is maybe not as intuitive as uh like text and numbers. A boolean is actually what we would call a true false value. So when we're writing our programs, there's actually going to be a lot of situations and circumstances where we want to represent true or false data. And a boolean is just a special word for true or false, right? So I could say B O L that stands for boolean. And we could create a variable like is male, right? So this variable is male is going to store a true or a false value inside of it. And this will basically tell us whether or not someone is male. So in my case, I could say true because I am a male, right? So you'll see how this can kind of come in handy for different things like uh we could say something is true or we could say something is false and that allows us to represent a certain type of information. I could also say false over here and that's going to be the opposite. So these are going to come in handy a lot these true false values and we can use booleans to represent them. So for the most part these are the basic data types. Now, there's a couple other data types that we could also get into, but I think 99% of the time as a beginner, as somebody who's learning C++, you know, don't concern yourself with anything that you don't have to. So, booleans, doubles, which are just uh decimal numbers, ins, which are whole numbers, strings, which is plain text, and chars, which are just single characters. That's what we're going to be working with. So, as we go forward in the course, we're going to be working with all this different type of information. Now, I want to point out one more thing. So, I'm actually just going to make a little um print statement here. And you know, this is a basic statement. So, if I wanted to, I could print out any one of these variables. Like, if I could print out grade, for example, and this is going to go ahead and print that out onto the screen. You see over here, we're just printing out A. But we don't have to store this information inside a variable. So if I wanted, I could just type in a string down here, right? I don't have to store it in a variable. And this is what we would call a constant. So I don't need to store it inside of a variable. I could also, you know, type out like false or I could type out a number like 4.5 or I could type out an integer or I could type out a character. Like you don't have to put these things inside of variables. A lot of times you can just use them like straight up like that. And this is what we would call a constant. But um a lot of times you're going to want to store information in variables because it's really useful. And obviously when you do that, you have to declare the data type and you have to tell C++ what type of data you want to work with. In this tutorial, I'm going to talk to you guys about the ins and outs of working with strings in C++. Now, in the C++ programming language, one of the most common types of data that you're going to be working with are going to be strings. And strings are basically just plain text. So any type of plain text that we want to represent or work with in our program is going to be considered a string. So down here you'll have see I have this little program set up basically just printing out a couple lines. So the way I can create a string is just by using an open and closed quotation marks like this. So I can basically type out whatever I want. If I wanted to I could type out draft academy. So you'll see now I'm printing out draft academy and hello. If I was to run my program, you'll see that we print out draft academy and hello. First thing I want to show you guys is uh doing has to do with this see out line of code over here. So you'll notice here it says end l right here. And basically when we put end l right there, it tells C++ that we want to print a new line after we print whatever is in here. So if I was to get rid of this and I just said see out draft academy. Now, what you'll see is that hello is going to get printed on the same line as Draft Academy because we didn't put end l so we didn't put a new line. If I wanted to though, I could actually manually come in here and specify a new line by saying back slashn. So back slashn basically means that we want to print out a new line. So this inside of a string represents the new line character. So now when I run this program, you'll see that hello gets printed out on a new line. So that's how we can use that back slashn inside of our string in order to do essentially the same thing as end l is doing. And if I wanted, I could put this like in between these two words and it would put them on a new line. So in addition to just printing out a string like we did over here, I could also store a string inside of a variable. So in order to create a string variable, I can just say string. We'll give it a name. So we could just call this phrase. And I could set it equal to whatever I want. So, let's set it equal to Draft Academy. And then I can do exactly what I did down here. So, here I can just print out phrase. And now it'll be printing out Draft Academy onto the screen just like that. So, in addition to just printing out strings and kind of working with them that way, storing them inside of variables, we can also use what's called string functions. Now, a function is something that we're going to get into more later in the course. We're going to write our own functions, but for now, just know that a function is basically like a little block of code that we can call which will perform a specific task for us. So these functions do all sorts of things and there's a lot of functions that we can use with these strings and they're called string functions. So these functions will either like modify the string or they'll give us information about the string. So I'm going to show you guys a couple. So the first string function that I want to show you guys is the length function. And all I have to do to use this is I can just come down here and I'm actually just going to print out the result of using these functions. So I'm going to put it down here. I can just say phrase.length and I can make an open and close parenthesis. And generally when we're calling a function in C, we're going to use this dot and then we're going to type the name of the function. So this is the length function and we're going to make an open and close parenthesis like that. So now if I was to run this program, you'll see we're getting 15. So this is basically just telling us how many characters are inside of this phrase string. So how many characters are inside Draft Academy. Now if I wanted to, I could actually access individual characters inside of here. So let's say that I wanted to access just this G. So I wanted to print out the first character in this string. Well, I can make an open and closed square brackets just like this. And inside of here, I can put a zero. And zero is going to refer to this first character in the string. So now when I run my program, you'll see we're just printing out this capital G. If I wanted to access this R, for example, I could say two and two is going to refer to this R. So now when I run my program, we're printing out R as you can see over here. So if you haven't caught on yet, whenever we're indexing a string, we're starting at zero. So if I was to assign index positions to each one of these characters in the string, I would say that G is at index position zero. I is at index position one, R is at index position 2, A3, F4, etc. So whenever C++ is indexing a string, it always starts indexing it at zero. So it'll start counting basically 0 1 2 3 4. Even though G is the first character in Draft Academy, it's technically at index position zero. So whenever we're using something like this, we're referring to like a specific character. We want to refer to the index position which is going to start at zero. Another thing I could do is I could actually like modify a specific character in a string. So I could say like phrase and I can refer to an individual character inside of this string. So we could say like phrase zero. I could assign this a new value. So I can assign this the value of a new character. So you'll notice I'm using these single quotes. So I could say like B. And so now instead of saying G I R A FF, it's going to say B I R A FF because I'm essentially changing one of the characters in this string. So now when we print this out, you'll see it says Baraf Academy instead of giraffe academy. So that can be kind of handy just to modify a specific uh character inside of a string. We could also find out information about this string. So I showed you guys how we can find out the length of the string. But imagine if we wanted to find out whether or not a specific string or a specific character was inside of this string. I could say phrase.find and I can make an open and close parenthesis. And actually inside of these open and closed parenthesis, I'm going to give this find function a couple different pieces of information. So I'm going to give it some information for it to perform its task with. And these are called parameters. So anytime I give a function different pieces of information, we call it passing parameters or you also hear people call them arguments. So passing arguments or passing parameters. Basically, it's just information that we give to this function. So this find function I need to give a couple pieces of information. The first thing I can do is give it a string or a character that I want it to find. So let's say I want it to find um academy. So basically I want to check to see if the word academy is inside of this phrase string up here. The next argument I want to give this the next parameter is going to be an integer and it's going to be the index where I want to start looking. So if I wanted to just check to see if it was in the string, I could say zero. If I wanted to check to see if academy occurred after the third index, then I could say three. Let's just say zero. So now this is actually going to give us back a number and it'll tell me at what index position inside of this string academy occurs. So now I'm going to run my program. You'll see over here it's giving us an eight back. That's because academy starts at index position 0 1 2 3 4 5 6 7 8. So academy starts at index position 8 and that's why it gave that back to us. So I could do this with anything. I could say like FF or something. And now this will tell me where FFE starts inside of this string. So it starts at index position 4. So that can be pretty useful and it's a really useful way to find to figure out if different things or different strings show up in the string you're working with. There's one more string function that I want to show you guys which is called substring. So it's just phrase sub str. And this is also going to take two parameters. So we're going to give this two pieces of information. The first piece of information we're going to give this is a starting index. Now substring allows us to take, you know, just part of this string. So I could specify that I want to take like all the characters after this a or I could specify that I wanted to take like from index position z from index position one all the way to the end. Basically, it'll allow me to take like just a subsection of this string. So the first parameter I want to give this is a starting index. So I could say like eight. And so basically this is going to start grabbing a new string at index position 8. Then I can give this a length. And this is basically going to tell substring how many characters I want to grab. So if I said like three, this is basically going to start at index position 8. So it's going to start up here at this A. And it's going to grab three characters. So it's going to grab this A, this C, and this A. So now when I print this out, you'll see we get ACA just like that. And that's basically what we do. So this is the starting index and this is the length. And one of the cool things we could do is we could actually take this and store this substring in another string. So I could say like string phrase sub just like that. And then I could come down here and I could say phrase sub is equal to and then what we just had before. So phrase.ubstring. And now if we print it out phrase sub it'll print out ACA. So I'm essentially storing the value of the substring in another string. So that can be pretty useful. And that's really the basics of working with strings. I mean, obviously there's certain things that I left out. Um, you know, I could spend an hour just talking about all the different things we could do with strings, but I think that kind of covers the basics. Kind of shows you all the basic things that you can do. We looked at some uh basic functions. We talked about grabbing individual characters. We talked about string indexes. So I think for now that should be a good introduction into working with strings in C++. In this tutorial I'm going to talk to you guys about working with numbers in C. Now whenever you're writing programs in C, one of the most common types of data that you're going to be dealing with are going to be numbers. So these could be things like whole numbers or decimal numbers. Basically I'm going to give you guys a full overview of how to work with numbers. We'll talk about the basics. We'll look at how we can use different mathematical functions in order to do uh different mathematical operations with our numbers. So, this is going to be a pretty cool tutorial. Now, down here, I'm just going to talk to you guys about the basics. Um, there's really two types of numbers in C that we deal with. Whole numbers and decimal numbers. And whole numbers are basically referred to as integers. And decimal numbers can be referred to as two things, either floats or doubles. And essentially the only thing you need to know about that as a beginner is that doubles allow you to store more specific decimal points. So with a double you could store, you know, potentially more uh decimal points than you could in a float. And there's more differences, but if you want to get more into that, you can kind of look it up. But the basics of using numbers is you just type them out. So if I wanted to, for example, um print out the number 40, you see I have this C out here. I can just type it in and we can print it out. We can work with it. In addition to positive numbers, we could use negative numbers. If I wanted, I can make this a decimal. So really numbers are are very simple. You just kind of type out the number. Um, but we can also do things like math. So for example, I could say like 5 + 7. And in addition to printing out five and seven, this is actually going to do this math operation. So this will actually print out the result of 5 + 7. So you can see we get 12 over here. So we can use addition. We can also use subtraction. We could use division which is going to be this forward slash. And we could use multiplication which is going to be this asterisk. So if I was to multiply these two numbers. Now you'll see over here we get 35. So those are the four basic math you know operations. So that's going to work really well. Also one other thing I want to show you guys which is called the modulus operator. And the modulus operator will basically give us the remainder of dividing two numbers. So if I said like 10 and then I made this percent sign and actually this is read 10 mod and then I said three. So we would read this 10 mod 3. What this is going to do is it's going to take 10 divide it by 3 and then it's going to give us the remainder. So this modulus operator will give us the remainder. So 10 / 3 is going to be 3 with a remainder of one. So now we just we should just get one and you can see over here we get one. So sometimes that modulus operator can come in handy. We can also represent order of operations in C. So C is going to adhere to like the normal order of operations like PE DMS I think it is. Um please excuse my dear aunt Sally. Basically like uh multiplication and division are going to come before um addition and subtraction. So if I said for example like 4 + 5 * 10, this is going to do 5 * 10 first. So it's going to be 50 + 4. So we get 54. But if I wanted to do the addition first, I could just put parentheses around this. So now it's going to do 5 + 4 9 * 10. So now we should get 90. And you can see we do. So if you need to separate order of operations, I mean, it's essentially just following basic math order of operations rules. Um, but you could represent those like that. So in addition to doing all that stuff, um, and just printing out numbers, we can store numbers inside of variables. So I can make an int. We'll just call it like w num for whole number and we'll just make this five. Um I could also use like a double and this will be like d num for decimal number and this could be like 5.5, right? I mean now we're storing these numbers inside of variables. And if I wanted I could just, you know, print them out uh naturally like we did down here. I want to show you guys one cool thing we can do though, which is incrementing a number that's stored in a variable. So I could say like w num and I could say plus+. And what this is going to do is it's going to add one onto w num. So now when we print out w num instead of just being five it's going to be six because we're adding one to it. So you can see we get six and that's a short hand that'll come in handy a lot. There's a lot of situations where you want to increment uh a value. Um you could also do like minus minus and that will subtract one from it. You could also do like w num plus equals and we could say like 80. And so what this is going to do is it's going to take w num and it's going to add 80 to it. So now we should get 85. And you see we do um you can do plus equals um multiplication equals minus equals. I think you can do division equals and all of that will it's it's just basically shorthand so you don't have to type out all that stuff. So now that we kind of looked at all the different operators, let's talk about how decimal numbers and integers work together. So here's a little experiment. Let's say I came down here and I added 5.5 + 9. So I'm adding a decimal number and I'm adding it to a integer number. Right? So let's see what happens over here. You'll see that we're going to get a decimal number back. So anytime we're doing math between a decimal number like a double or a float and an integer, a whole number, we're always going to get a decimal number back. So it's always going to give us the decimal back. Um but it's important to note though if I did math with two integers. So for example, let me show you guys. If I was to say like 10 / 3, and these are both integers, keep in mind, I'm actually going to get an integer number back. So, we're going to get like three back because that's technically the answer. But here's the thing. This isn't actually the answer. Really, what it is is it's three with a remainder of one. But because we did the math with two integers, we're going to get an integer value back. If I was to make one of these a um a decimal number or even if I made both of them a decimal a decimal number now we're going to get back the actual like full answer. So it's going to be three with three repeated just like that. So you can see if we do math between just two integers we're always getting an integer back even if that's not like fully the correct answer. In different circumstances you're going to want to do that and other circumstances you're not. But just keep that in mind. So that's kind of how uh integers and decimal numbers interact with each other. So now what I want to do is show you guys how we can use different math operations. So it generally just with math there's all sorts of different um like I guess operations. So you could do like square root, you could take a number to a power, we could round a number. And in C there's actually these things called functions which can do all that stuff for us. A function is basically just a collection of code that we can call that will perform a specific task. And we're going to talk more about functions later, but for now I'm going to show you some basic math functions that we can use. In order to use these math functions, I actually have to do something called importing them. And essentially, when we import something, we're basically going we're telling C++ that we need to go out and grab um code from other files. So up here, you can see we're using this include statement and we're grabbing something called IO stream. I'm going to add another line here. We're just going to put a hashtag. We're basically just going to copy this guy up here. I'll make an open and closed uh greater than less than sign. And in here, I just want to type in Cmath. And basically what this is going to do is it's going to tell our C++ program that we want to use some math functions. That's kind of all you need to know at this point. Just know that you need to put this up here in order to follow along with what I'm going to be doing. So down here, we can now use a bunch of different math functions. Essentially just math operations. So for example, I could say like p and what this will do is it'll take two arguments. So in here I could pass two numbers like I could pass a two and I could pass a five. And what this will do is it'll take 2 raised to the power of five and it's just going to print that out. So when I run my program you'll see we're getting 32. So 32 is 2 raised to the 5ifth power. And you can kind of do that with any number. So I could also say like 3 raised to the 3 power. So 3 cubed and now we should get 27 which we do. So that p function can be pretty useful. There's another one uh square root sqrt essentially doing the opposite. Um so we could say like square<unk> 36 and now this is going to give us the square root of 36 back which is going to be six. So that can be pretty useful. And inside of these um functions we you can put decimal numbers too. So I could put uh both integers like whole numbers and also decimal numbers. Um there's another one which is called round which is going to round a number. So if I put like 4.3 inside of here, this will return the rounded number. So you see we just get four and this will follow normal rounding rules. So if I change this to 4.6, now we should get five back. Uh there's a couple other of these functions which will similarly like round decimal numbers. I can say c e i l. And what this will do is it'll automatically just round the number up. So even if this was like 4.1, this will round the number up to the next highest whole number. So um you'll see here we get five. You could do the opposite which is floor. So if I just say floor here, if I put a 4.8 in here, normally it's supposed to round up, but now it's going to round down because we're using that seal or that floor function. So those can be pretty useful. And there's one more I want to show you guys which is called fmax. And fmax is going to take two numbers. So I can pass in like a three and a 10. And this will tell me which one's bigger. So this will return back to us the bigger of the two numbers. So now when I run this program, you'll see it's giving us a 10 back because 10 was the bigger number. And a lot of circumstances um in C you're going to have two numbers. You might not know which one is bigger. So this can be really useful to tell us. And you could also use fmin which will do the opposite. So this will tell us what the smallest number that we passed in was. And you can see we get three over here. So those are some basic math functions. There's a lot more if you just go online and search um C++ math functions like you'll find a huge listing of all them that you can use. There's things to do like uh s cosine tangent. You can do like logarithmic stuff. You can do um you can use like exponentials all different stuff like that. It can be really useful. But that's kind of the basics of how you can use that. And you know, really numbers are extremely useful. And that's kind of been like an a broad overview of sort of all the stuff you can do with them. In this tutorial, I'm going to show you guys how to get input from a user in C++. A lot of times in our programs, we're going to be working with all different types of information, but one of the most important types of information is going to be information that the user inputs. So, a lot of times in our programs, we're going to want to allow the user to input information and then we're going to want to use that information in order to do different things. So, in this tutorial, we'll just give a broad overview of how that's done. We'll talk about how to get different types of information and you know, you'll kind of learn everything you need to learn. So, down here, whenever we're getting information um from the user, the first thing we always want to do is store that information somewhere. So whenever I'm asking the user to give me a piece of information, if they give me that information and I don't put it anywhere, like it's kind of useless, right? So usually what we want to do is create a variable. And I'm actually going to write a little program that will allow the user to enter in their age. So I'm going to create a integer just called age. And I'm not going to give it a value. So I'm just going to put a semicolon there. We're essentially declaring the integer. So we're telling C++ that we want to use this integer, but we're not giving it a value. We're going to let the user give this variable a value. The next thing I want to do is I want to prompt the user to enter in something. Right? So, if I don't give them a prompt, if I don't tell them what information I want, then they're not going to know what they should input. So, I'm just going to say see out. And I'll just print out a little prompt. So, we'll just say enter your age. And we're basically just prompting them to enter in their age. All right. So once we've created the variable and once we've prompted them to enter in the information, now what we can do is we can actually get the information from them. So the way that we get information is actually the opposite of the way that we print out information. So instead of saying C out, I'm just going to say C in and I'm going to make two greater than signs. So when we use C out, we use these less than signs. When we use C in, we use greater than signs. And that's really important. So over here, I'm just going to type in the name of the variable that I want to store what they enter inside of. So I'm just going to put age here because I want to store whatever they input into this age variable. So assume, we're assuming that they're going to enter in an integer number. And I'm going to store that integer number inside of this integer that we created. Now what we can do is we can just print out something. So we'll print out a little message to them basically saying like you are and then we'll say like age and we can say like years old. Cool. So now we're basically just asking them to enter in their name. We're taking whatever they input and we're storing it inside of this age variable. We can do that using this cin uh command here. And then we're just printing out you are the age years old. So, let's go ahead and run our program and we'll see how we did. So, I'm going to build and run. And you'll see here it says enter your age. So, we can just put like 30. And now when I click enter, it's going to take that integer that we entered in 30. It's going to store it inside of the age variable. It's going to print out the prompt. So, when I click enter, you'll see it says you are 30 years old. So, that's awesome. So, we can do that. And we can get uh an integer. I could also get a double. So, if I made this a double, um it would be the same thing. So here I could run the program and I could enter in like 4.5. So maybe someone's like four and a half years old and here they're entering in 4.5. So it's going to work the same exact way. You could also do the same thing for characters. So if I made this a char. Um here it'll be the same. So we'll be able to get a character from them as well. So now when I run the program I could just enter in um you like a G for example, whatever. Obviously, that's not the age, but it does the same thing. So, it's able to take in that character and it's able to use it. Um, now here's the thing. That's how we can get characters and numbers. But, if we want to get a string of text, we're actually going to do something different. So, if I wanted to get a string, I'm actually not going to use this C in command. So, I'm going to show you guys how we can get a string. So, instead of entering in the age, why don't we have them enter in their name? So, over here, I'm going to make a string, and I'm just going to call it name. Instead of using C in, I'm going to use another command which is called get line. And get line will basically allow us to get an entire line of text. So instead of just getting like one number or one character, we're going to get like the entire line that the user enters. And in here, we need to pass this a couple different parameters. The first is going to be CN. And CN is basically just that little like command prompt. So like whenever we enter stuff into that command prompt, that's basically what this is saying. Um, and then we want to enter in the name of the variable where we want to store the line of text that we get. So I'm just going to store it inside of name. So over here we could just say hello to the user. So I could say hello and it's going to be name. So now we'll be able to get the name from the user. So we'll basically get the entire line. So for example, I could come over here, I could say like John Smith. And now when I click enter, it says hello John Smith. So that's how we can get uh strings of text. And that's really the difference is if I wanted to get like an integer and store it inside of an integer variable or if I wanted to get a char and store it inside of a char variable, I have to use C in. But if I want to just get like a string of text, then I can use this get line function and they're both going to be useful. So that's kind of an overview of how we can get input from the user. And you could get as many values as you wanted. So, you know, you could essentially like copy this whole line, paste it a bunch of times below there and get uh, you know, a bunch of different pieces of information and use them. In this tutorial, I'm going to show you guys how to build a basic calculator in C++. Essentially, we're going to build a calculator where the user can enter in two numbers and then we'll add those numbers together and we'll spit out the answer. So, this is going to be kind of cool and it'll show you guys a little bit more about getting input from users. So, down here, the first thing we want to do before we do anything is create two variables where we can store the two numbers that we want to add together. So, I'm going to create an integer and I'm just going to call it num one. And I'm not going to give this a value because we're actually going to let the user give this variable a value. I'm going to create another one called num two. And same thing, and actually, let me show you guys something cool we could do. If we're creating two variables like this and they're the same data type, I could actually put them on the same line. So I could say num one, num two. And a lot of people find this to be really useful. Um, so for example, I could do like as many as I wanted. In our case, we're just going to have two numbers though. So now that we've declared our two number variables, the next thing we want to do is prompt the user and get some information. So I'm going to say see out. And the first thing we'll do is prompt them for the first number. So I'll say enter first number and now we want to actually get the first number from the user. So I'm going to say C in and over here I'm just going to specify num one. So we're going to store the number that they enter inside of this num one variable. I can actually just copy this and we're going to do the same thing down here. So I'm just going to say enter second number. And now instead of num one it's going to be num two. So at this point in our program, we should have both of those numbers. We should have gotten both of those numbers from the user. The last thing we want to do is just print out the answer. So we can actually just say see out. And I'm just going to add num one and num two together. So we should be printing out the result of adding those two numbers together. So let's go ahead and run this program and we'll see how we did. So I'm going to build and run. And actually, whoops, I put the wrong direction for these arrows here. So, this is probably a really common mistake with C in. We want to make two greater than signs, not two less than signs. So, that's something that could easily uh trip up a new user, and it tripped me up. So, when we're using C in, we're using these two greater than signs. When we're using C out, we're using these two less than signs. That's my mistake. But I guess that does kind of highlight a common mistake for beginners in C++ and even for someone like me who's programmed in C++ a lot before. All right, so now let's run our program. And it says enter first number. So we'll enter in a five. Enter second number. We'll enter in a 10. And now when I click enter, it should add both these numbers together. So it does. And we get 15. So that's pretty cool. Um, another thing we could do is instead of using integers, we could also just use doubles. And that's as easy as just changing these variable declarations. So now when we run the program, we can work with doubles. So I could enter in like a four and I could also enter in like a 9.8. And now we'll be able to add those guys together. So that's kind of cool. And really that's a basic calculator. So essentially we're declaring the variables up here. We're using C out. And with C out, remember we're using these less than signs. Then we're using C in with the greater than signs. And this line is basically getting the us the input from the user and it's storing it inside of this variable. We're doing that twice. And because we store these as doubles or before integers, we're able to add them together and print out the answer. So that is the basics of building a calculator. Actually later in the course, I'm going to show you guys how we can build an even cooler calculator that'll be able to add, subtract, multiply, and divide, and the user will get to decide. In order to build something like that, we're going to have to learn some more stuff. So stick around and we'll learn how to do something like that later in the course. In this tutorial, I'm going to show you guys how to build a little Mad Libs game in C++. So a Mad Libs is basically a little game where you enter in a bunch of random words. Could be like nouns, verbs, adjectives, and then all those words get taken and get sprinkled into a story. And generally since you entered in random words, the story ends up being pretty funny. So let me show you guys over here in my browser. I just have an example of a mad lib. You know, essentially there's this story and then you would sprinkle in all those random words that the person enters into the story. So, we're going to build something like this in our C++ program. So, down here I have my little program set up. It's it's just this little poem. It says, "Roses are red, violets are blue, I love you." So, this is a you know, sort of a classic poem, but I think this could be a lot better if we turn it into a mad lib. So, let's say instead of um saying roses are red, why don't we let the user enter in a color? Instead of saying violets are blue, we'll let them enter in a plural noun. And instead of saying I love you, let's say I love celebrity. So, some random celebrity they can enter in. So, basically, we're going to ask the user to enter in all these different values. We'll store them in variables and then we can print them out inside of our story. So, let's get started. This should be kind of cool. The first thing we want to do is actually create variables for all these things. So I'm just going to say string and we'll create a variable for the color, for the plural noun, and for the celebrity. All right. So now we've basically told C++ that we're going to use all these variables, but we need to give them values. And we're actually going to let the user give them values when they enter in all those things. The first thing we want to do when we want to get input from the user is we're just going to say see out and we're going to print out a prompt. So I'm basically just going to say enter a color. So we're going to have them enter in a color. Now we want to actually get the color that they input. So I'm just going to say get line and in here we're just going to say cin. And that's basically just standing for like that console input. So whenever we like uh input text into the console. So it's going to get whatever gets inputed into there. And then we're going to store it inside of this color variable. So this will get the line of text that they enter. And I'm actually just going to copy this and we'll do the same thing for all the other uh values. So we're going to say enter a plural noun and we'll store that inside of the plural noun variable. And then down here we're going to do the same thing for the celebrity. So we'll store that in the celebrity variable. All right. So we're declaring the variables. We're printing out our prompts. And then we're getting the input from the user using this get line function. Last thing we need to do is modify this part of the program. So instead of saying roses are color, we're actually going to put the color that they entered into the story. Same thing for this plural noun. So I'm just going to say plural noun. And you can see how that gets placed in between these less than. And then I love celebrity. We're going to actually put in the celebrity variable. Cool. All right. So now our program should be set up. Everything's wired up. We're getting values into all these variables and we're printing them out down here in our story. So let's run this program and we should be able to play our Mad Libs. Enter a color. Why don't we enter in magenta? Enter a plural noun. Let's do microwaves. And enter a celebrity. Why don't we do Tom Hanks? So now when I click enter, all of the values are going to show up in our story. So it says roses are magenta, microwaves are blue. I love Tom Hanks. Awesome. So our Mad Lib worked and we are ready to go. So that's essentially how you can build a Mad Lib. I mean it's it's as easy as getting different inputs from the user and printing them out inside the story. But this is a very simple Mad Libs. I mean, I'm sure you guys could see that you could ask the user for like 20 different words and print them out into some huge long story and it's going to work out the same way. So, this is an awesome way to create our Mad Libs game. In this tutorial, I'm going to talk to you guys about using arrays in C++. A lot of times when we're writing programs in C++, we're going to be dealing with large amounts of data. And one way that we can keep track of and manage that data is by using something called a variable. But the one thing about variables is that generally you can only store one value inside of a variable. So if I create like a character v variable, I can only store one character in there. If I create an integer variable, I can only store one integer in there. A lot of times though in C++, we're going to be dealing with huge amounts of data. Um, and that's where arrays can come in. Array is basically a container. a lot like a variable, but unlike a variable, arrays can hold multiple data values. So, an array could hold like a list of, you know, thousands or millions of pieces of information. And generally, we could put, you know, pieces of information that are related to each other in some way inside of the same array. So, I'm going to show you guys how we can create arrays and how we can work with them. You create an array a lot like you create a normal variable. So the first thing we have to do is tell C++ what type of data we want to store inside of the array. So I'm just going to make an array of integers. So I can just say int. And again, just like a variable, we're going to give this a name. So I could say like lucky nums, right? And this would be an array of lucky numbers. Now, here's where this gets different from a variable. When we're creating an array, we always want to put an open and close square bracket after the name. So after lucky nums, we're going to put this open and closed square bracket. And that basically tells C++, okay, they want to create an array. So they want to be able to store multiple pieces of information inside of this container. Now I'm just going to say equals. And one, the easiest way to create an array is just to assign it some default information right off the bat. And I can just make an open and close curly bracket. And inside of here, I can just start typing out the information that I want to store. So let's say I want to store like a list of numbers. So I could say like 4 8 15 16 I don't know 23 42, right? So I'm storing all of these numbers inside of this single container. So unlike a variable where we could only store one number, now I'm storing a whole list of numbers. And we would refer to these as elements in the list. So four would be the first element in the list. And then we would delineate the next element using this comma. So now we have the second element 8, 15, 16, etc. So these are all going to be elements inside of our array. Now down here, I'm going to show you guys how we can access individual elements. So I'm just going to say see out. And let's say that I wanted to access one of these particular elements. So let's say I wanted to access like this first element here inside the array because what good is the array if we can't access the information inside of it, right? I can just say lucky nums and I can make an open and close square bracket. Now, inside of this open and close square bracket, I can put the index of the element inside the array that I want to access. So, if I want to access this first element in the array, I just need to put its index in here. That's going to be zero. So, now when I run my program, you'll see we're printing out four. We're printing out that first element in the array. If I wanted to get access to this 15, it's going to be 0 1 2. it's going to be at index position two inside of this array. So now when I run my program, we get that 15. So if you haven't caught on by now, when we index these arrays, we start with index position zero. So I would say that four is at index position zero, 8 is at index position 1, 15 is at index position 2, 16, 3, etc. So we would always say that the first element in the array is actually at index position zero. And if you're familiar with how strings are indexed in C++, it's the same exact thing. So that's how we can access an individual element in the array. We could also modify an element in the array. So I could say like um lucky nums and like let's say I wanted to change the first element. I could say lucky num zero and I could give this a different value. So I could set this equal to 19 for example. Now, down here, when I print out lucky num zero, I'm actually going to be printing out and actually typo. Whoops. I'm actually going to be printing out um 19. So, you can see we're able to modify one of the um indexes inside of that array. Another thing we can do with these arrays is I can give them a size. So, normally if I just create the array like this where I just say int lucky nums in an empty square brackets, the array is only going to be able to store the elements that I declare over here. But a lot of times when you're making these arrays, you might not know what all the elements should be. So over here, I could put in a number like I could say like 20. And essentially what I'm doing here is I'm telling uh C++ that I want to be able to store 20 elements inside of this lucky nums array. So over here I don't have 20 elements yet. I only have um elements 0 1 2 3 4 5. So what I could actually do is I could add more elements into here. So I could say like lucky nums 6 or lucky nums 10. And I could give this a value. So I could give lucky nums 10 the value of 100. And now down here if we printed out lucky nums 10, it's going to be printing out 100 as you can see. Um another thing you can do is just not give this any info right up front. So I could get rid of all of these and I can just put a semicolon here and then down here I can give all these different values. So I can say lucky num zero is equal to 100. And now I can you know essentially just assign all the elements um after we declare it. So like I said a lot of times you might not know exactly what's going to go in the array when you declare it. And so you can just basically tell C how many um items you want to hold in there. And then you can just, you know, fill the the array up as you go. But that's really the basics of arrays. And arrays are very simple. So this is a very simple way to store multiple pieces of information. In this tutorial, I'm going to talk to you guys about using functions in C++. A function is basically just a little collection of code that performs a specific task. So a lot of times in C++ when you're writing a bunch of code out, you'll have code that's designed to do a certain thing, right? So you might have like four or five lines of code that's supposed to do something. And a function is basically a container where you can put that code and then you can reuse it throughout your entire program. So I'm going to show you guys how we can create a function in this tutorial. So down here in my program, you'll see I have this little block of code here. It says int main. There's an open and close parenthesis. And then there's these open and closed curly brackets. This is actually a function. This is a little block of code. And this block of code performs a specific task. And the purpose of this main function is it's the function that gets executed when we run our program. So um any code that we put inside of this main function is going to get executed when our program runs. I'm going to show you guys how we can create another function. So up here above this main function, I'm going to create another function. And the task that this function will be performing is it's going to say hi to the user. So whenever we're creating a function in C++, we need to give uh C++ a couple pieces of information. The first piece of information we need to give is called a return type. Now, whenever we create these functions, a lot of times the functions will go off. They'll perform a specific task and then they'll return a value back to the caller. And we're actually going to talk more about returns and return types in the next video. But for the purposes of this video, you can kind of just follow along. And I'm just going to go ahead and put void here. And when we put void here, it basically means that this function is not going to return anything. So this is kind of like the most basic type of function. And after we put void, now we need to give this function a name. Remember, function is a block of code that performs a task. So, generally when we're naming a function, you're going to want to name it according to the task that it's performing or according to the purpose of the function. So, I'm just going to call mine say hi because our function is going to say hi to the user. Now, I'm going to make an open and closed parenthesis. And after this, I'm going to make an open and closed curly bracket. any code that I put inside of this open and close curly bracket is going to be considered inside of the function. So inside of here, why don't we just say like see out and we'll just print out like hello user. And that's basically all we'll do. So this is a very simple function. I just have one line of code in here. I could have as many lines as I want. This is a simple function, but I could have, you know, a dozen lines or a 100 lines if I wanted to. You can put as many lines of code in the function as you want. Now, let's run our program and we'll see what happens. So, I'm going to go ahead and run my program. And you'll see over here, nothing's getting printed out. So, hello user isn't actually getting printed out when we run our program. Here's the problem. When we want to execute the code that's inside of these functions, in other words, when we want to execute a function, we have to do something called calling it. So if I want this code inside of here to be executed, I have to call the function. Now remember the code inside this main function gets executed by default, right? So no matter what, this is going to get executed. So inside of here, I can call the say hi function. I can just type out say hi. And I can type an open and close parenthesis. And when I do this, when I type this out, this tells C++ that I want to execute all of the code that's inside of this say hi function. So when C++ sees this, it's going to jump up to this say hi function. It's going to execute all of the code inside of there and then it's going to come back down. So let's go ahead and run our program and we'll see what happens. So you can see over here now we're printing out hello user. And real quick, real quick, I just want to show you guys the flow of these. So if I said like see out over here and I said top and then I did the same thing over here and I said bottom when I run my program you'll see that we're printing out top hello user and then bottom. And actually this probably would have been better if I put new lines in there. But the point is that when this program executes C++ is going to execute this line of code. It's going to see that we want to call the say hi function and it's actually going to leave this main function. It's going to jump up here to the say hi function. It'll execute all of the code inside of here. Then when it runs out of code to execute inside of the say hi function, it's going to jump back to the main function and execute this line. So that's basically like the flow of what's happening. So this is a very basic function, but we can make this more complex. And one thing you can do with these functions is you can actually give them information called parameters. So this say hi function I could give this a piece of information and the say hi function could use that piece of information to you know perform its task differently or better or whatever. So these are called parameters. And if I want to specify that this say hi function should be given a piece of information I can just come over here and specify what piece of information it should take. So in our case, instead of saying hello user, why don't we say hello? Hello to someone specific. So up here I could say like string name. And now this function is going to accept one parameter, a name. And down here instead of saying hello user, we can just say hello name. Now whenever I call this say hi function now, because it's specifying that it needs to take a parameter, I need to pass it a parameter. I need to pass it a value. So in here I can just pass it like Mike. And now it's going to print out hello Mike because the value Mike is going to get stored inside of this name variable. All right, let's go ahead and run our program. And now you'll see it's printing out hello Mike. So that's kind of cool. And you can take any piece of information as a parameter. And you can also take multiple parameters. So why don't we specify another parameter like age? So now the caller is going to have to pass in their name and their age. So now we can say hello name and we'll say like you are and then we'll just say how old they are. So we'll say like age. So now we're passing in two pieces of information inside of this function. And down here when I call the function I have to give it two pieces of information. So now I could just say like Mike and let's say that I'm 60. So now when we call this function, it's going to be able to take in both of those pieces of information and use them to perform the task differently. And what's cool about these functions is I could call this as many times as I want. In other words, I can reuse all of the code that's up here. So I could come down here, I could copy this, and why don't we do this a bunch? So we'll say like Tom is going to be 45 and let's say Steve is going to be 19. And so now I'm actually going to run this code three different times. And actually over here I'm just going to put a end line so that gets printed out on new lines. All right, let's run the program. And you'll see it says, "Hello, Mike, you're 60. Tom's 45 and Steve's 19." So functions are great because we can reuse the code that we write inside the function. So basically like I wrote this function one time and I can reuse it as many times as I want inside my program. So anytime you have code that's going to be reused a lot, that's a good candidate for a function. All right, so I want to show you guys one more thing. You'll notice that I'm creating this function up here above my main function, right? But if I was to take this and move it down here. So for example, if I moved it below the main function, now when I run my program, we're going to get an error. You can see we're getting a little red block here. This is the problem. When we create this function below the main function, it doesn't actually know about it. So like all this code up here is getting executed. So C++ is trying to execute the say hi function, but it has no idea like what that is because we created it down here. So what we can do is we can actually create what's called a function stub. So up here, we're basically just going to write out the functions signature and we're going to tell C++ about it. So if you guys remember, like if I created a variable, like if I created an int and we just called it num, I don't have to give this a value right away. I could then come down here and say like num is equal to four or something. This is basically what we're going to do with this function. We're going to essentially just declare the function and then somewhere else in our program, we can define it. So over here I'm just going to say void say hi and it needs a string name and it needs an int age. And so now when I create this little function stub up here or this function signature I'm basically declaring the function and I can give it a value down here. And now C++ is going to be able to call it because it's going to have some information about it. So when I run my program now we're able to call it no problem. So that's kind of just how we could do that with functions. And really you can create as many functions as you want. In fact, a good program will have lots and lots and lots of functions. In the next tutorial, um I'm going to show you guys how we can actually get information back from functions using the return keyword. In this tutorial, I'm going to talk to you guys about returns in C++. So when I'm talking about returns, I'm talking about returns from functions. So in C++ we can write functions which are basically like little containers that store a bunch of code that performs a specific task and with those functions we can call them and we can also pass them information. So I can give them all sorts of like parameters and different information that they can use to perform their task better. But in addition to giving functions information, functions can actually give us back some information. So when I call a function, not only can I give it parameters, but that function can give information back to me. I'm going to show you guys how we can use this and how we can use the return keyword in C++. So why don't we create a function up here? And I actually want to create a function that's going to cube a number. So when I cube a number, I'm basically taking it to the third power. So if I was to say like 2 cubed, it's basically just 2 raised to the 3 power or it's just going to be 2 * 2 * 2, right? That's what cubing a number does. So why don't we create a C++ function that's going to cube a number. Now, when we create a function in C++, the first thing that we have to do is declare something called a return type. And a return type basically tells C++ what type of value or what data type this function is going to return. Now in the last tutorial, if you're following along with the course, we just put void here. And void basically meant that our function wasn't going to return any information. In this tutorial though, we definitely want to return some information. So you can put any of the generic C++ types here. You could put like int, double, uh you could put character, you could put string. Essentially, any of those values can go here. In our case, we're going to cube a number. So, why don't we just make it a double? And I'm going to say double. Now, we need to give this a name. So, I'm just going to call it cube. And we're going to take one parameter into this function. So, why don't we take a double and we'll just call it numbum. Now, inside of this function, all we want to do is cube the number and then return the result that we get. So, I'm actually going to create a double. I'm going to call it result. And I'm going to set this equal to the cubed value of num. So, I'm just going to set it equal to num time num* num. So, this is basically me cubing num. So, now result has inside of it the result of cubing all these numbers. Okay. down here, I want to return the value that's stored inside of result. So I can just say return result. And now this is going to tell C++ that we want to return this value that's stored inside result back to the caller. Now down here in my main function, I can actually call this function. So I could say cube and I could pass as a double. Let's say like 5.0. Right? Now actually what's going to happen is when we call cube and we pass it this information it's actually going to get a value back. So if I wanted I could basically say like double answer is equal to cube 5.0 and now the value that gets returned back from this cube function is going to get stored inside of this answer variable. So let me show you guys. I can say see out and I'm just going to print out answer. And so now when I run my program, what you'll see is we're going to be printing out the result of cubing five. And you'll see over here we get 125. So 5 * 5 is 25. 25 * 5 125. So we are able to cube the number. And you can see here this is getting a value back. So I'm able to store the value that gets returned from this function inside of this variable. If I wanted, I could also cut out the middleman and I could just um print out cube 5.0. So, let's just paste this bad boy down here. And now it's going to do the same exact thing. So, we should get 125. Cool. And actually up here, if we wanted, instead of storing num* num* num inside of this result variable, I could instead just return these guys up here. So, we again cut out the middleman and we'll just return that. and we should get the same exact answer. All right. So, there's also one more thing I want to talk to you guys about, which is this return statement. This return keyword right here. This is a very special word in C++. And basically, whenever we type out this return keyword, it's essentially telling C++ that we're done executing the code inside of this function. So, if I was to put like a C out here and I just print it out like hello, when I run my program now, you'll notice that it's not printing out hello. Even though we're executing that cube function, even though we're executing all that code, it's never printing out hello. That's because whenever we use this return keyword, it's going to break us out of the function. So this line of code is actually never going to get executed because it's never going to get reached. When C++ sees this line, it's just going to break out and we'll head back down to the main method. So that's essentially what this is doing. And that's sort of the basics of using returns. So like I said, if you didn't want to return a double, you could return any type of data. I mean a string. You can even return something like an array. Um so really you can return anything you want. Um and then that value will get stored over here. In this tutorial, I'm going to talk to you guys about if statements in C++. And an if statement is basically a structure that we can use in our code which will allow our program to respond to different situations. So when one situation occurs, we can do one thing and when another situation occurs, we can do another thing. Essentially, we're able to check different conditions and when those conditions are true, we can do certain things and when those conditions are false, we can do other things. So, I'm going to show you guys exactly how this works. Down here in our main function, I'm just going to create a couple different things. So, the first thing I'm going to do is create a variable. It's going to be a boolean variable. Remember, a boolean variable stores a true or a false value. So, I'm going to create a boolean called is male. And I'm going to give this a value initially of true. So we're basically creating this variable is male. It's storing whether or not someone is male. And I'm giving it a value of true. Now I want to show you guys how we can use an if statement. And an if statement, like I said, allows us to respond to different situations. So what I'm going to do is I'm going to be able to write an if statement that will respond to the situation where the user is a male and the situation where the user is not a male. So what I can do is I can actually say if and I'm going to make an open and close parenthesis and then I'm going to make an open and closed curly bracket. Now inside of this open and close parenthesis I can specify a condition. So I can actually check a condition. And if the condition that I put inside of those open and closed parenthesis is true then we're going to execute the code inside of here. If it's false then we're going to move on. So over here we're essentially putting a true or a false value. And that's basically a condition, right? It's a true or a false value. So I'm going to come down here. I'll say if and I'm just going to type out is male. And essentially what this is saying is if the value stored inside of is male is true. In other words, if the value in here is true, then we're going to execute the code that's inside of these curly brackets. So I could say like see out and I'm just going to type out like you are a male. And now when I run my program you guys will see what happens. So I'm going run the program and you'll see over here it says you are a male. And that's because the boolean is male was true. So the value that was inside of this condition, the value inside of the parenthesis which was true and we executed this code. If I made this false now, you'll see that this code isn't going to get executed. So, we're not going to be printing anything out. So, because the value inside of this parenthesis, because the condition was false, we're actually not going to execute this code. But let's say that if the person wasn't male, we also wanted to handle that situation. I can use another keyword in C called else. So, I can type out else just like that. and I'm going to make an open and closed curly bracket. Now, inside of this open and close curly bracket, I can put a message that's going to be displayed when this is false. So, I'm just going to say see out and I'll say you are not male, right? So, I'm responding to this situation. So, now even though is male is false, when I run my program, it's going to be able to respond to that. So it's just going to say you are not male. Essentially what's happening here is my program is now smart enough to respond to this variable. So if this variable is male is true, it can handle that. If it's not true, it can also handle that. And so basically inside of these parentheses, I specified a condition. In our case, it was a true or false value that was stored inside of this is male variable. So that's kind of cool. Now let's up the ante a little I want to show you guys how we can make these more complex. This is a very simple example, but let's say I added in another boolean. So, I had another boolean called is tall. And I'm going to set this equal to true. And actually, I'm going to set this equal to true as well. So, now in addition to is male, I also have is tall. So, let's say that we wanted to check to see if the person was both male and if they were tall. So down here what I can do is I can use something called the and operator. I could say is male and is tall. And basically what's happening now is C++ is saying if the person is male and the person is tall then we're going to print this out. So I'll just say you are a tall male. So this code, this line of code down here, and any of the code inside of these open and closed curly brackets is only going to get executed when is male is true and is tall is true. Essentially, this and operator is allowing me to check two separate conditions. And both of them have to be true in order for this line to get printed out. So you'll see is male is true and is tall is also true. So now when I run my program, you'll see that it says you are a tall male. Here's the thing though. If I set one of these to false. So if I set is tall to false, this whole condition is no longer going to be true. So then we're going to go down here to this else block. And this should probably say like you are not male or not tall. But we'll just leave it like that just for demonstration purposes. All right. So now it says you are not male, right? And obviously like I said that message should be different. But the point is that this condition was false because one of these values up here was false. And that's how we can use this and operator. There's another operator called the or operator and that is similar to and except only one of these conditions has to be true in order for the whole thing to be true. So even though is tall is set to false and is male is set to true, this is still going to execute. So, it's still going to say you are a tall male because one of them is true. So, let's go ahead and run this program now. You'll see it says you are a tall male. So, it executed the code that was inside this initial if block and that's because one of them was true. If I set both of these to false now because we're using the or now it's going to go down to you are not male. So, you'll see down here it says you are not male. So that's basically how we can use and and how we can use or. With the and operator, we can put two conditions in and both of them have to be true for the whole thing to be true. With the or operator, only one of the conditions has to be true for the whole thing to be true. And they can both be true as well and it'll be true. But if both of them are false, then it's going to be false. All right, let's make this even more complex, though. So I want to show you guys another thing we can do. I'm going to bring this back to and. So, I'm going to say is male and is tall. But let's say that we wanted to check to see if they were male and they weren't tall. So, if they were like a short male. Well, I can actually check another condition. So, I can use another keyword in C++. It's called else if. And I'm going to make an open and close parenthesis and open and close curly bracket. And I'm just going to bring this down to a new line. So else if is basically saying if this condition up here is false then we're going to come down here and we're going to execute this condition. So if this condition up here is false instead of just going to else we're going to come over here and we're going to check another separate condition. So over here I want to check to see if they're male and they're not tall. So they're not tall. So I can say is male just like before. So I'm saying else if is male and is tall. But remember I want to check to see if they're not tall. And here I want to introduce you guys to another operator. It's called the negation operator. It's basically an exclamation point. And whenever you put this exclamation point before a condition like is tall, it's going to actually negate that entire condition. So if is tall is true and we put this negation operator over here, it's going to set it equal to false and vice versa. So essentially what I'm saying here is else if is male and is not tall. That's basically what this is saying. So now over here we could just print out you are a short male, right? Because if this code, in other words, if this condition is true, that means they're male and they're not tall. So they're short male. And there's one more that we can check. So I'm going to say another else if over here. And there's one more possible scenario with these two variables up here. That's when they're tall, but they're not male. So I could say not is male and is tall. So this would be like, you know, it could be like a tall female or something, right? So down here, I'm going to go ahead and say see out and I'll just say you are tall but not male. Right? So essentially, I'm just printing that out onto the screen. So now we have all of these different situations covered. So over here in this if else block, which is what we would call this, and actually down here in this else block, we would want to change this. So this is going to be you are not male and not tall. So that's just, you know, semantically going to be correct. All right. So we're able to respond to every possible combination of these two variables up here. So let's try this out. I'm going to set both of these equal to true. And now I'm going to run my program. So they're both equal to true. So our program will be able to respond to that situation. So you'll see it says you are a tall male. Awesome. It was able to respond to that. Let's set is male equal to false. So now we have is male false and is tall true. Our program once again is going to be able to handle that. So it's going to say you are tall but not male. So the program's smart enough now to be able to tell us what we are depending on the value of these variables. Let's try another one. So we'll set is male to true and is tall to false. And again it's going to be able to respond. So it'll say you are a short male. And then finally we'll set them both false. And we should get the answer which is going to be you are not male and not tall. Awesome. So we were able to figure this out just by using all this stuff. So let me walk you guys through this one more time. We have this if statement and it's checking if they're a male and if they're tall. Now these are both conditions, right? So I'm checking this is male. This is a condition. Essentially it's a true or false value, right? And then I'm checking is tall. And if both of these are true, then we'll execute the code inside of here. If this is false, then we'll come down here and we'll check this other condition. And we're going to keep doing that until we get down here and we figure out, okay, they're not male and they're not tall. So, that's the basics of if statements. And really, this is just scratching the surface of if statements. I talked to you guys about a lot of the different things we can do, but there's one more thing I want to talk to you guys about in the next tutorial, which are called comparisons. And down here, we're using these boolean variables in order to get our conditions. Right? So, I'm using these, and these are either going to be true or false, right? But we can also use something called comparisons. And with comparisons, we can check to see if different values relate to each other in different ways. is so I can check to see if like a number is greater than another number or something and we can use those comparisons as our conditions and that's going to be pretty cool. So stick around for the next tutorial and I'll talk to you guys more about these if statements. In this tutorial I'm going to talk to you guys some more about if statements. More specifically, we're going to be looking at using comparisons inside of our if statement conditions. So this should be kind of cool. In this tutorial, I want to create a function. So, we're going to write a function and it's going to basically tell us the max of two numbers. So, this function will take as parameters two numbers and then we'll basically return whichever number is the largest. And this is a great example of how we can use comparisons with if statements. So, over here I have my main function. And up here, I'm going to create another function. And let's just say it's going to return an integer. And I'm just going to call it get max. and we'll make an open and close parenthesis and an open and close curly bracket. So inside of this function, we're going to take two parameters. One is going to be an integer and we'll get another integer. So essentially, we're going to take two integers as parameters as input. And the goal will be to figure out which of the integers is the biggest and return that back to the caller. The first thing I want to do is just create a variable called result. And I'm not going to give this a value right away, but eventually at the end of this function, we're going to return result. So, whatever we do in here, we have to store whichever of these is bigger inside of this result variable. So, this should be kind of interesting. Now, we need to figure out which is bigger. So, we can use an if statement to do that. So, I'm just going to say if open and close parenthesis and an open and close curly bracket. Now, inside of this parenthesis, we need to put a condition. In other words, we need to put a true or false value, right? If the value is true, then we'll execute whatever is down here. If the value is false, then we'll just move on. Here's the thing, though. In the last tutorial when we were learning about if statements, we were using booleans, right? And a boolean is a true or false value. So, it's really easy, right? You just throw it in there and it's true or it's false and you can just kind of do the if statement. But in a lot of cases, we're not going to have any boolean information available to us. So it's not going to be as easy as just like throwing a boolean in there. All the only information that we have are these two numbers. So how can we create like a true or a false value using just those two numbers. Well, what we can do is we can use a comparison. So if I want to figure out which of these numbers is bigger, I can just compare them, right? So over here inside of this if condition I can basically just say if num one is greater than num two. If num one is greater than num two then we can just set result equal to num one. And the reason I'm doing that is because we know for a fact that if this condition if this comparison is true then num one is the bigger. So you can see this comparison num one greater than num two. It's actually going to get resolved down to a true or a false value. Right? Num one is either greater than num two or it's not. Technically this is going to be a boolean value. Right? Once you evaluate the comparison once we figure out if it's true or not it's going to be a true or false value. Right? So I can basically check to see if num one is greater than num two. And if it is we'll set result equal to num one. And we can just say else and we'll set result equal to num two. Right? And the reason we're saying this down here is because if this condition is false, if num one isn't bigger than num two, then we know that num two is the bigger of the two numbers or in certain circumstances, the numbers might be equal, but it's still going to work anyway. So then down here, we're going to return the result. So let's go ahead and test this function. And I'm just going to come down here and why don't we print out the answer. So I'm just going to say get max and we'll throw like a two and a five in there. Right? So pass in a two and a five. And now when we run this program hopefully we should get that five back. So I'm going to run the program and yep over here we get our five. So that's working perfectly. So this is basically how we can use comparisons inside of these if statement conditions. Right? technically this comparison is going to get resolved down to a boolean value. It's going to get resolved down to a true or a false. Like either the comparison's true or it's not, right? And that's kind of interesting. So that's sort of the basics. And in addition to using greater than, we could also use less than. Um we could use less than or equal to. We could use greater than or equal to. We can use equal to. And equal to is a double equals. So when we use the equal sign down here, we're using it for assignment. So I'm assigning the variable result, the value stored inside num one. When I use this double equals, it's a comparison operator. So I'm comparing the value of num one with the value of num two. And again, that's either going to be true or false, right? They're either going to have the same value or they're not. And then the last one we can use is a not equals. And not equals does exactly what you think it does. This whole thing is going to be true if num one is not equal to num two. So those are sort of uh the basics. Now let's make this a little bit more complex. So this get max function takes two integers. What if we had it take three? So let's say int num3. Now all of a sudden instead of just comparing two numbers, we have to figure out which is bigger among the three numbers. And this is going to make our if statement a lot more complicated. So why don't we just start over and I'll show you guys how we can do this. So, inside of this comparison, I need to check to see if num one is bigger than num two, just like we did last time. But I also need to check and see if num one is bigger than num three. If num one's bigger than num two and it's bigger than num three, then we know it's the biggest. So, let's start there. I'm just going to say if num one, and instead of just saying greater than, I'm going to use a greater than equals, right? Because there is that chance where they're all the same number, in which case this will cover that. So we can say if num one is greater than or equal to num two and so I'm going to use this and operator and this is going to allow me to check another condition. So I can say num one is greater than or equal to num three. So now if num one's greater than or equal to num two and it's greater than or equal to num 3 then we know num one's the biggest. So I'm going to set result equal to num one. Now down here we need to check another condition. So I'm going to say else if and we basically want to check to see if num two is greater than or equal to num one and num two is greater than or equal to num three. And if this is the case, in other words, we're going to check this condition. If this condition up here is false, so if this condition is false, we know that num one's not the biggest, right? But if this condition is true down here, then we know num two is the biggest. So we'll say result is equal to num 2. Now all we have to do is just say else and down here we can just say result is equal to num three because if this condition is false that means num one's not the biggest. If this condition is also false that means num two is not the biggest. So if num one's not the biggest and num two is not the biggest then num three must be the biggest. So we're going to default to this value. All right. So that is the if else block that we need. And you see I'm using a bunch of different stuff here. So not only am I using this and operator, I'm also using these comparison operators. And that's what these guys are called. So let's go ahead and test our little function out. So now instead of saying two parameters, we need to pass in three. So I'm going to pass in two, five, and 10. And we should get 10 back, which we do. If we wanted, we could test out each of the positions. So, for example, I could test out um we'll make this one the biggest now. The second one. And we'll see if it works. And it does. And we'll make this one the biggest now. And we'll see if it works. And it does. Cool. And then also there was that condition where like two of them were the same. So let's try this where two of these guys are going to be the same. And we run the program and yeah, so it's still going to be able to return 200. So there is our max function. And hopefully this gives you guys a little introduction into using comparisons. Now we can compare all different types of data. So using these comparison operators, we can compare integers. We can also compare like doubles and floats. And we can compare characters. So you can compare all of those guys just by using all that stuff in there. In this tutorial, I'm going to show you guys how to build a four function calculator in C++. Now, if you've been following along with this course, you'll know that in the beginning of the course, we created a very simple calculator. And basically, that calculator was able to take in two numbers as input and it added them together and printed out the answer. But in this tutorial, now that we've learned a little bit more in the C++ language, we're going to build a more complex calculator. So, this calculator is going to allow the user to enter in two numbers, but it's also going to allow the user to specify which operation they want to perform. So they can specify that they want to add the numbers or multiply the numbers, divide them, multiply them, whatever. So it's essentially going to be a more powerful calculator. So down here inside of my main function, I want to start writing the code for this. And the first thing I'm going to do is create a few variables. So I want to create a couple variables. And why don't we just create these as ins. You could also make these doubles if you wanted. Um we'll just call them num one and num two. And these are going to represent the two numbers that we want to perform the operation on. And then down here, we're also going to want to create a character. And I'm going to call it OP for operator. So, we're going to store the two numbers here, and we're going to store the operator here. So, it's either going to be plus, minus, division, or multiplication. All right. So, now that we've created those two variables, we want to start writing the code to get the input from the user. The first thing that we want to do is print out a prompt. So, I'm just going to say see out, and I'll basically just print out enter first number. So, we'll prompt them to enter in the first number. And then down here, we're going to use C in. And remember, you want to use these two greater than signs. And we want to store the user's input inside of num one. Just like that. Okay, cool. So, now that we got num one, I'm actually just going to copy this code. And then down here, we can paste it two more times. So, down here, we're going to ask them for the operator. So, I'll say enter operator. and we're going to store the operator inside of that op variable. Just like that. And then finally, we're going to enter in the second number and we'll store it inside of num two. So basically what I'm doing is I'm prompting the user for all of the information that we need. So we're going to get that first number, we're going to get the operator, and then we're going to get the second number. So they'd give me like 5 + 2 or 6 / 7 or something like that. All right. So now that we have all of that information, we have one more thing that we need to do, which is we need to figure out what the user wants to do. Right over here, we have the operator. So ideally, they would have entered in like a plus sign, a minus sign, whatever. But we don't know what exactly they entered in. Like we don't we have no idea what what they were going to enter in. So we need to figure that out. And this is a perfect scenario for an if statement. So I'm going to use an if statement. And I'll show you guys how we can check this. So, I'm just going to say if and we need to put a condition in here. Um, so basically what we want to do is we want to figure out what that operator is. So, the first thing I'm going to do is just check to see if the operator is equal to a plus sign, right? So, if the operator is equal to a plus sign, then we're basically just going to print out the result of adding the two numbers together. So, why don't we come up here and I'm going to create an integer called result. And then down here, we'll set result equal to num one plus num two. And we know that we want to add them because if this condition is true, then we're going to execute this code. All right. So now let's do an else- if. And down here, I basically just want to check to see if it's a minus sign. So I'll say if OP is equal to a minus sign. And if it is equal to a minus sign, then we can do exactly what we did up there, but we can subtract the two numbers. So I'll say num one minus num two. All right. So we can do the same thing for multiplication and division. So I'm actually just going to copy this else if and I'll paste it down here. And now we'll say op is equal to forward slash that means division. And then one more time we're going to do this for multiplication. So use this asterisk. And then here we're going to multiply the two numbers. So now we're basically covering all of the operators. So if they enter in a plus sign, we got that covered. Minus sign, division sign, and multiplication sign, right? We can cover all of those standard scenarios, but there's always the chance that the user entered in an invalid operator. So instead of entering in one of these, they just entered in some like, you know, random character. In which case, I'm going to use an else block over here. And this else block is going to get executed whenever none of these conditions are true. So if none of the conditions are true, then we know they entered in an invalid operator. So I'm just going to go ahead and print that out. I'm going to say see out and we'll just print out invalid operator. All right. So basically by the end of this if block we should have a value stored inside of result. So down here we're just going to print it out. So I'll just say see out and we'll print out result. All right. So let's see if our program works. I'm going to go ahead and run the program. And it says enter first number. So why don't we enter in a five? Enter operator. Let's enter a plus sign and second number. Why don't we do 30? So now when I click enter, hopefully our if statement is going to execute. We'll be able to figure out which operation I was trying to do and then we'll be able to perform it correctly. So you see down here we printed out 35. So looks like everything worked. So why don't we try a couple other ones? Let's do like 50 divided by five. So down here we get 10. Yeah. So it looks like that's working. All right. Let's check one more thing. Let's check to see if we enter in an invalid operator. So I'm going to put like a five. We'll enter the operator. Let's make it like a capital G. And then we'll put a nine for the second number. And you'll see down here it prints out invalid operator. And then we're also getting this other little print out here. And actually that's just because we didn't give result. We didn't give result an initial value. All right. So that's basically our four function calculator. You guys can see how we can combine getting input from the user. Then we can use these if statements to basically figure out what the user input and that can be extremely powerful. In this tutorial, I'm going to talk to you guys about using switch statements in C++. Now a switch statement is basically a special type of if statement which allows us to check one particular value against a bunch of other values. So this is a very special use case um when we're trying to compare different things. So I'm going to talk to you guys about what switch statements are and we'll do an example which will kind of illustrate what they're doing. So in my program in this tutorial I actually want to create a little function which is going to be able to convert a number into the day of a week. So basically if the user passed in a zero to this function I'd want to be able to print out like Sunday or if the user passed in a one I'd want to be able to print out Monday etc. So I want the function to be able to convert an integer number to a day of the week just like that. So, I'm going to show you guys how we can do this inside of our C++ program. So, I'm going to create my function, and we're basically just going to have it return a string. And I'm just going to call it get day of week. Just like that. And I'm going to make an open and close parenthesis. And this is going to take one parameter, which is going to be an integer. And we'll just call it day num. So, this is going to be the number of the day of the week. So, like 0 through six, basically. All right. So, inside of our little function now, we need to figure out how we can do this. So, depending on the day of the week, in other words, depending on the day num that they give us, we want to be able to return a different value. So, the first thing I'll do is I'm going to create a variable and it's just going to be a string and I'm just going to call it day name. So, this will be like this will end up storing the name of the day that they requested. And then down here at the end of this function, we're going to return it. So I'm just going to say return day name. So now we know what we're going to end up returning. So our goal inside of this function now is to populate this day name variable with the correct day of the week. So let's think about this. I mean one way that we could build this function is by using an if statement. So I could say like if day num is equal to zero and if day num is equal to zero then I can say dayame is equal to Sunday. Right? That works. And then I can just keep doing this for every day of the week. So I can say like else if and again I have to check to see if day num is equal to something else. So day num is equal to one. And then down here we'll do the same exact thing. So we say day name is equal to Monday. Right? I can keep doing this for every single day of the week. Here's the problem though. I'm gonna have to do this like seven times, right? Potentially eight times if we want to check for an invalid input. So I'm going to have to create eight of these different if else structures in order to figure this out. And that is extremely inefficient. And actually this is a perfect circumstance for us to use something called a switch statement. Now a switch statement essentially takes what we're doing over here with these if else statements and it makes it a lot easier. So one thing I want you guys to notice is in every single one of these conditions, in other words, in every single one of these uh if conditions, we're checking to see if the same value dum is equal to a different value. So in here we're saying dum is equal to zero. Here we're saying dum equal to one. In the next one, it would be dum equal to two, then dum equal to three. All of those conditions are checking to see if dum is equal to something else. And when you have a situation like that, it's a perfect candidate for a switch statement. So I'm going to show you guys how we can use a switch statement to make this a lot easier. So I'm just going to write out switch an open and close parenthesis and an open and close curly bracket. So so far it looks a lot like an if statement. Up here in these open and closed parenthesis, I'm going to put in a value. And we're basically going to put the value in here that we want to compare to all those different values. So in my case, it's going to be dum, right? I'm comparing dum to 0 1 2 3 4 5 6 etc. Right? Dum is the value that I want to compare to all the different values. Now inside of this switch statement I can write cases and basically I can say case and then I can just put a value in here. So I could say zero and a colon and then down here I can basically type out what I want to happen. So I could say day name is equal to someday. Now, essentially what this is doing is it's saying in the case that day num is equal to zero, we want to set day name equal to Sunday. And then right below here, I'm just going to say break. And I'll explain what break does in a second, but I can basically make one of these cases for every single number. So I could say like case one. So in the case that day num is equal to 1, I'm going to set day name equal to Monday. And then again I'm just going to say break. And then I can keep doing this for 2 3 4 5 6 etc. Right? So what this break is doing down here, this break statement is essentially breaking us out of the switch statement. So if I don't put this break statement here, C++ is actually just going to keep looking through all these cases. So even if DAM is equal to zero and it sets it equal to Sunday, it's still going to keep going and checking all these different values unless I say break right here. And generally it's a good idea to say break unless you want to be able to check for more than one uh condition. We're basically saying in the case the day num is equal to zero, we're going to do all the code down here. And I'm actually going to make one of these for each day of the week. So I'm going to go off and do that and then we'll meet back here and I'll kind of show you guys what I have. All right. So I went ahead and made one of these little case blocks for every day of the week. So you can see down here we have 0 through six and they're all corresponding with the appropriate days. So like case 3 uh day name is Wednesday etc. Right? So everything seems to be working. Now there's one more thing that we have to do in this switch statement. So down here there's always the chance that the user enters in an invalid day of the week. So in other words, if the user doesn't enter 0 through six, then we want to be able to handle that. In other words, I want to be able to handle the situation where the number that they entered isn't in here. And inside of a switch statement, we have a special little block which is called default. So we can just say default a colon, and then down here, this code is going to get executed if none of the cases up here are true. So if the number isn't equal to any of these numbers up here, then this code inside the default block will get executed. So down here I could just say day name is equal to invalid day number. All right, so now we have our switch statement set up and you can see how this is a lot cleaner than something like an if statement. So this is a lot clearer what's going on. We don't have to type out else if like a thousand times. We don't need all those parentheses and all that stuff. Basically, it's just really clear. it's really easy to visualize what's going on. All right, so let's see if our get day of the week function is working. I'm going to go in here and well, we're actually going to print out the value. So I'll say see out and we'll call this. So I'll say get day of week and we need to pass this an integer. So why don't we pass it a zero and hopefully we should get printed out here Monday or Sunday. Whoops. Yeah. So over here, Sunday. Okay, so Sunday would be the first day of the week. Well, let's try let's see if we can get Monday. So, I'm going to put one here instead of a zero. And now we should get Monday. Yeah. Awesome. All right. Let's try one more. We'll try six. So, six is going to be Saturday. And let's see if it works. Cool. So, Saturday worked as well. All right. There's one more situation, which is where we pass in an invalid number. So, if I pass in like a 10 here, now we'll be able to see that this isn't going to work. So it should tell us invalid day number. Awesome. So this is a pretty cool function and it's working. And you can see how we can use these case statements in order to make this a lot easier. Now here's the thing. I could have easily done this with an if statement. Nothing is stopping me from doing that. But it's a lot easier to use something like a switch statement. And that's why C++ included it in the language. In this tutorial, I'm going to show you guys how to use while loops in C++. Now, a while loop is basically just a programming structure that allows us to loop through a block of code while a certain condition is true. So, with this tutorial, we're starting into something called loops. And loops are extremely useful. And there's a lot of situations in our programs, specifically in C++, where we're going to want to be able to loop over code and do things a bunch of times. So, I'm going to introduce you guys to the most basic type of loop, which is a while loop. And we'll sort of see what it does and see how it works. All right. So, down here, the first thing I'm going to do before I create my while loop is I'm actually just going to create an integer. And I'm just going to call it index. And I'm going to set it equal to one. So, this isn't 100% necessary when you're creating while loops. We're just going to use this in our example in this tutorial. Now, when I want to create a while loop, I can basically just say while I can make an open and close parenthesis and an open and closed curly bracket. And this is sort of like the main structure, the main outline for a while loop. Now, here's what's going to happen. Okay, over here inside of these parentheses, I'm going to specify a condition. Now, if you remember uh learning about if statements in C++, um this is essentially what we're doing here. So in here we're putting a condition just like we would an if statement. And as long as this condition is true, I'm going to loop over and continually execute all of the code that's inside of these curly brackets. As you'll see in a second, we're going to put a bunch of code inside of these curly brackets. And as long as the condition up here is true, we're going to keep looping through that code. And every single time we loop through that code, we're going to check this condition. So, let me just show you guys this and we can kind of get a feel for what's going on. So, up here in these parenthesis, I can basically just write a condition. So, what I want to do is I'm going to say while index is less than or equal to five. So, this is now my condition. While index is less than or equal to 5, I'm going to execute the code inside of this loop. So, I'm going to execute the code inside of these curly brackets. So down here, what I want to do is I basically want to print out the value of index. So I'm just going to say see out index and then I'm also going to say end l. So we print out a new line. And then down here I'm going to increment the index. So I'm just going to say index++. So you remember index++ just adds one to index essentially. All right. So what I'm doing is I'm specifying my condition. While index is less than or equal to 5, I want to execute all of this code. So, I'm going to run my program and maybe you can sort of see already what this is going to do. But if not, we're going to run it and we're going to take a look at what's happening. So, you'll see over here when I ran my program, we basically got 1 2 3 4 5. Here's what's happening over here. I created this variable called index. I set it equal to one. And I basically told C++ that while index is less than or equal to 5, I want to keep looping over this code. So basically what happened was the first time we went through we checked this condition. So we checked to see if index was less than or equal to 5 since that was true. In other words since index was one on the first iteration of the loop. We executed this code. So we printed out one and then we incremented index. So index was now equal to two. Then we came all the way back up here and we checked this condition again. So we check to see if index is less than or equal to five. In this case index is equal to two. So the condition is true. Index is less than or equal to five. So we're going to execute all of the code inside of here again. So we printed out two and then we incremented two up to three. And then we came back up. We checked the condition again. Every single time we go through the loop, we check that condition. So before we execute the loop, we execute that condition. We go back up, check it again, and execute the code again. Eventually we got to the point where index was equal to six. So we came up here to check this condition and six is not less than or equal to five. So then we just came down, we broke out of the while loop and everyone was happy. So that's essentially what we did and that's what a a loop is. Basically you specify a condition. As long as that condition is true, then you're good to go and we can pretty much just keep looping through all of the code in there. And there are tons of situations where using loops is going to come in handy. And in the next tutorial, we're actually going to build a little game where we can use a while loop. But before we do that, I want to talk to you guys about a couple more things with these loops. Uh the first thing you need to watch out for when you're using these loops is something called an infinite loop. And an infinite loop is a situation where this condition up here never becomes false. So in certain circumstances, like the condition that you specify up here won't become false ever, and then your loop will just continually loop through. So, for example, if I was to get rid of this line of code right here where we're incrementing index, now index is always going to be one. And so, this condition up here is never going to be false. So, when I run my code, you'll see over here that we're just continually printing out one. You can see the screen keeps flashing. Really, what's happening is we're just looping, looping, looping, looping through that while loop, and it's never going to stop. I mean, I could leave this on for years and it's never going to stop, right? Cuz that condition is never going to be false. So, you always want to make sure that any conditions that you write with these while loops at some point are going to become false. Now, there are certain circumstances where you're going to want a while loop, but I would say for the most part, while loops um aren't going to be necessary, and they can actually be dangerous, especially if you're doing something where you're like creating variables or allocating memory, like you can end up crashing your program or even crashing your computer if you're not careful. So, that's just a little tip about infinite loops. There's one more type of loop that I can show you in this tutorial and it's a different type of while loop. It's kind of like a different flavor of while loop. Um, but before I show you that, I just want to show you a demonstration. So, if I make this int index equal to six, when I run my program, the code inside of the while loop is never going to execute. And that's because before we loop through all of this code, we check this condition first. So before I'm allowed to execute any of this code, I check the condition up here in these parentheses. So I check to see if index is less than or equal to 5. Since index is starting out at six, we're never ever going to execute this code, right? So if I ran my program, you guys will see nothing gets printed out, right? So we didn't do anything. But there's another type of loop which is called a dowh loop. And I want to show you guys how we can make one. I'm just going to cut this and I'm going to paste it down here. And I need to include a semicolon. And then up here where that while used to be, I'm just going to say do. And this is what's called a do while loop. And a while loop is essentially just a normal while loop, but it's reversed. So instead of checking the condition and then executing the code, we're going to execute the code and then we're going to check the condition. So it's just sort of like the while loop reversed. So now even though index is equal to six, I'm still going to be able to execute this code first. then we're going to check the condition and it's not going to let me do it again. So, let me show you guys what happens when I run this. And you'll see over here we're able to print out that six. That's because with the dowh loop, you execute the code in the loop before you check the condition. And there's a bunch of different situations where you can use uh while loops or dowh loops. And to be honest with you, everything that you can do with a dowh loop, you can do with a normal while loop. So dow loops aren't technically necessary, but um in a lot of circumstances they are useful. So that's why they're included in the language. But yeah, I mean that's a basic overview of loops. There's actually another type of loop that we can talk about in a future tutorial called for loops. But for now, you know, practice with these while loops. You can kind of see like how they work. In this tutorial, I'm going to show you how to build a little guessing game in C++. And this is going to be cool because we're actually going to be able to use some of the stuff that we've been learning up to this point in the course. So we'll use things like while loops, if statements, we'll use variables, we'll use all sorts of stuff in order to build this game. So essentially how this is going to go is the user is going to try to guess a secret number, right? So I'm going to have a secret number stored here in my program and the user's job will be to try and guess what that number is. So, the concept of the game is pretty simple, and as you'll see in a second, it's actually pretty simple to program something like this. Um, but here's the one caveat is if the user doesn't get the guess right on the first try, I want to keep asking them, keep prompting them to enter in another guess. So, I'm going to show you guys uh how we can do this. So, the first thing I want to do is create our secret number. So, I'm just going to make an integer and I'm just going to call it secret num. I'm just going to set this equal to, I don't know, let's say seven. So seven is going to be our secret number. And then the next thing I want to do is create another integer which we're going to call guess. And I'm not going to give guess a value right up front. Um so now we have our secret number and we have our guess. And we want to be able to keep asking the user to guess what the secret number is until they get it right. So whenever something needs to be done continually, that's usually a good sign that we want to use a while loop. So I'm going to go ahead and say while I'm going to make an open and close parenthesis, and I'm going to make an open and close curly bracket. So inside of this while loop, we want to continually ask the user to enter in the number until they get it right. So what I want to do here is specify a condition. In other words, I want to keep looping while the secret num is not equal to the guess. So as long as the guess is not equal to the secret num. In other words, as long as the user hasn't guessed correctly, I want to keep going through this loop. Now, every time through the loop, I want to prompt the user to input a number. So, I'm just going to say see out, and we'll basically just say enter guess. So, they're going to enter the number that they want to guess. And then down here, we want to be able to get that answer. We want to store it in a variable. So, I'm just going to say C in two greater than signs. And I'm just going to put guess here. So basically what we're telling C is that we want to take whatever the user enters, store it inside of the guess. And so basically every time we go through this loop, the user is going to be guessing and then we're going to check their guess against the secret num. If they got the secret num right, then we'll just break out of the loop. So down here, if they end up getting out of the loop, that means they got the guess right. So I can just print out you win. So we'll give them a success message. All right. So, this is essentially all we need to build our guessing game. So, I'm going to go ahead and run this program and we'll see how we did. All right. So, it says enter guess. So, I'm just going to enter in some numbers. So, you can see I entered in a three. That wasn't the secret number. So, I can keep entering in different guesses. And no matter how many times I guess, uh, it's going to keep asking me to guess until I get it right. Now, let's try to get the guess right. So, the secret number was a seven. So when I enter a seven in now, all of a sudden the condition that was in that while loop is going to be false. So the secret num is going to be equal to the user's guess. And we'll break out of the while loop and it's going to say you win. So that's essentially how we can build our little guessing game. Now I will say down here I created this game using a while loop, right? But there's actually another way that we could build this using a dowh loop. So, your homework for this tutorial is to go off and see if you can do this using a dowhile loop instead of just a normal while loop. All right, so here's the thing. This is actually a pretty fun game and it works out. We were able to program it really easily, but the game's a little bit unfair. It's unfair in the sense that the user gets essentially unlimited guesses. So, they can guess as many times as they want. And honestly, that doesn't seem fair. So, what we could do is we could impose some sort of a guess limit. So, I could say, "Hey, the user is only able to guess like three times and then they lose the game, right?" So, I'm going to go ahead and show you guys how we can actually program something like that. We're going to need to add in a couple extra variables. Uh, the first variable I'm going to add in is going to be called guess count. And this variable is basically just going to keep track of how many times the user has tried to guess the secret number. So, every time the user guesses a secret number, we want to increment the guess count. And I'm actually going to set this equal to zero initially because the user will have guessed zero times. Then down here, every time the user guesses, I'm going to increment the guess count. So I'm just going to say guess count plus+. So now we're basically able to keep track of how many times the user has tried to guess the secret number. All right, we're going to make another variable over here. And I basically just want to call this guess limit. And guess limit is going to specify how many times the user is able to guess until they lose. So why don't we set this equal to three. So we'll say that the user gets three guesses. They can't get the word in three guesses, then they're going to lose the game. All right. And then one more thing, we want to create another variable which is going to be a boolean. And this is going to be called out of guesses. And I'm going to set this equal to false initially. So out of guesses will tell us whether or not the user has any guesses left. If out of guesses is false, that means the user has guesses left, right? Because they're not out of guesses. If it's true, it means they don't have any guesses left because they ran out of guesses. All right, so now that we have these variables, I want to show you guys um how we can modify this while loop. All right, so down here, every time we go through this while loop, you'll see that we're letting the user guess the number. But here's the problem. If the user doesn't have any guesses left, then we don't want them to be able to guess the number, right? In other words, if they've reached the guess limit, we don't want to give them another guess. So, what I can do here is I can actually use an if statement. And what I'm going to say is if guess count is less than guess limit. So, if the guess count is less than the guess limit, then I'm going to let them guess, right? Because if this condition is true up here, that means that they still have some guesses left, right? They haven't reached the guess limit. So, then they're going to be able to guess. will increment the guess count etc. Otherwise though, if the guess count isn't less than the guess limit, that means that they ran out of guesses, right? So what I want to do is I want to toggle this boolean. So basically I want to say out of guesses is equal to true. So I'm telling my program that they ran out of guesses. All right. So once we've set this equal to true, now we're basically done inside of this while loop, right? So every time we go through, we're checking to see if they have any guesses left. If they do, then we let them guess. Otherwise, we tell our program that they're out of guesses. There's one more thing that we have to do to this while loop, which is modify the condition. And up here in this while loop condition, we can use uh this and operator. And this and operator will allow me to put two conditions inside of there. So, I could say while secret num is not equal to guess. And we can check another condition. So, what I want to do is I want to check to see if they're out of guesses. So, I want to check to see if out of guesses and I want to check to see if they're not out of guesses. So, we're going to keep looping as long as they haven't guessed the secret word and they're not out of guesses. Right? If one of those becomes false, so if they run out of guesses or if they guess the secret word, then we're going to break out of the loop. So, that's essentially what's happening here. All right? So, one more thing down here below the while loop, we have this see out and we're just printing out you win. But this isn't technically correct because if the user runs out of guesses, then they're going to break out of the loop, but they didn't win. So, we shouldn't tell them that they won. We should tell them that they lost. So, what I'm going to do is make another if statement. And I'm going to check to see if out of guesses. So, if out of guesses is equal to true, then I just want to print out a you lose message. And otherwise, if it's not equal to true, in other words, if they had guesses left, that means that they won. So that means they guess a secret word. All right. So let's go ahead and run this program and we'll see how we did. All right. So down here it says enter a guess. So why don't we try to lose the game? So I'm just going to say three, four, and two. So this is our last guess. If we don't get it on this try, that means that we lose. So you lose, right? So we lost the game. All right. So looks like it's working. So now instead of being able to guess infinite times, we can only guess three times. Let's see if we can win the game now. So we're going to guess like a three, a two. Now we're on our last guess. I'm going to say seven. And when I enter this, it says you win. So we were able to win the game uh as well. All right. So this is sort of how we can use all of these different programming structures together. So you'll see down here, not only did I use this while loop, and not only did I use all these variables, but I also used if and else in combination with all that stuff. And really, this is what real programming is going to look like. You know, it's not just going to be like little simple examples. It's going to be a little bit messy just like this. Not that this is messy code, but we want to be able to use all of these different programming structures in combination. There's also one more thing I want to tell you guys which is the way that I wrote this little algorithm, this little function here. We could have written this like a dozen different ways. So I could have programmed this game in, you know, I can think of like at least two or three other ways that I could have done this. And this is another lesson in programming is that generally there's going to be like five or six different ways to do the same thing. So I could have done this with a dowh loop. I could have used different things. I could have used like a break statement or something else. And so that's one of the cool things about being a programmer is it's going in and trying different ways uh to do the same thing. But that's basically how we can build this guessing game. Hopefully you guys learned a little bit about seeing all those different structures like ifs and whiles and variables and all that stuff used together. In this tutorial, I want to talk to you guys about for loops in C++. A for loop is a special type of loop which actually allows us to keep track of how many times we've gone through the loop. So in a for loop we can use something called an indexing variable. And basically that indexing variable will allow us to keep track of how many times we've gone through the loop. And you can also use the indexing variable to do a bunch of other stuff too. So I'm going to show you guys how for loops work. We're going to look at why they're useful and in what situations you're going to want to use them. So over here you'll see that I have this basic program set up and essentially I just have a a while loop over here. So I have this uh integer. It's called index and I just set it equal to one. And then while index is less than or equal to 5, I'm just printing out the value of index and then I'm incrementing it. So I'm going to go ahead and run this program. You guys can kind of see what's happening. So you'll see we're basically just printing out 1 to 5. Right? So this is a very simple while loop. And if you've been following along with this course, then you've, you know, hopefully you're familiar with what while loops do at this point. Um, but I want you guys to notice a couple things. So this while loop is special because every time we go through the loop, this variable index tells us what iteration of the loop we're on. So when I run this program, you'll see over here like the first time we go through the loop, index represents one, right? Index has a value of one. The second time we go through the loop, index has the value of two. Third time has the value of three, etc. Right? So this index variable is essentially telling us what iteration of the loop we're currently on. Right? In other words, on every iteration of the loop, the value inside of this index variable is changing. So it's in our case, it's incrementing by one. But the point is is that we have this variable, this index variable, and it's essentially just keeping track of how many times we've gone through the loop. Now in C++ and in just about every major programming language, this is a very very powerful looping structure. In other words, there's a lot of situations where keeping track of, you know, how many times we've gone through the loop or even just having a variable that's going to change every time we go through the loop is very useful. And in fact, this is such a useful situation that there's actually a special loop just to do something like this. So there's a a special type of loop whose sole purpose is to provide us with a structure very similar to this where we have this indexing variable that is going to change every time we go through the loop and in a lot of circumstances is actually going to represent what iteration of the loop we're currently on and that's called a for loop. So I want to show you guys how we can build a for loop and essentially a for loop is just going to be exactly what we have up here. It's just going to be a lot more compact and a lot easier for us to use. So to create a for loop, we can just say for make an open and close parenthesis and an open and close curly bracket. So so far it looks exactly like the while loop. Now the one difference with the for loop is instead of only having one thing up here in the parenthesis. So in this while loop we just have our looping condition, right? A lot of people call this like a loop guard. Um down here in this for loop parenthesis, we're actually going to have three different things that we want to include. So the first thing that we want to put inside of this for loop parenthesis is a variable declaration variable instantiation. So up here you'll notice that I'm creating this indexing variable called index and I'm giving it a value of one. So I'm declaring the variable and I'm assigning it an initial value. Right? I'm initializing the variable inside of this for loop parenthesis. We can do exactly this. So I could actually just copy this line and then down here I can just paste this in here. And instead of calling it index, why don't we just call it I? So I is going to stand for index. We'll set it equal to one. Then you'll notice I have this semicolon here. After I put this semicolon, we can actually put another line. So another thing that we have in this while looping structure is our loop guard, right? This loop condition. It's essentially telling us uh when we should loop. So if this condition up here is true, then we want to keep looping. So that's the next thing that we want to put inside of this for loop parenthesis. So we're putting this variable declaration and initialization. And then down here we're also going to put the looping condition. So this is going to tell the loop when it should stop and when it should go. All right. So then I'm also going to put another semicolon here. So you'll notice I have a semicolon here after we create this variable. And I have another semicolon here after we specify the looping guard. The next thing I want to do is put in a line of code that's going to get executed after each iteration of the loop. Up here in this while loop, you'll notice that we have this line of code over here, index++. This line of code is going to get executed at the end of every single loop iteration. So the last thing that we're going to do when we go through this while loop is increment the index variable. Right? So that's essentially what we're going to put over here. We're going to put a line of code which is going to get executed after every iteration of this loop. And in our case, we're just going to increment this variable I. So I'm just going to say I ++. We're adding one to I. Now again, I could put essentially any line of code here that I want. I mean, I could put like I minus minus. I could do, you know, basically whatever I want, but in our case, we're just going to increment I just like we did up here with index. So now inside of this for loop body, inside of these open and closed curly brackets, we can just put whatever we want to do on each iteration of the loop. So that's going to be this line up here. So I could actually just paste this guy in right here. And now we have a completed for loop. So believe it or not, this loop down here is actually equivalent to this loop up here. You'll notice this took 1 2 3 four lines of code whereas this only takes two lines of code. So like I said this is such a common scenario where we want to have a variable like index which is you know essentially changing every time we go through the loop and can allow us to keep track of things like the current loop iteration. Such a common situation that there's a special loop called a for loop for that. And the for loop the first thing we do is create the variable just like I did up there. So I'm initializing this variable. The next thing we do is specify the looping guard, the looping conditions. That's what I did here. And then we specify a line of code that's going to get executed after every iteration of the loop, which is going to be this I ++. And you'll notice that these are all separated with semicolons. And you'll notice also that I don't need a semicolon over here. So let's actually get rid of this while loop. And I'm going to go ahead and show you guys what happens when we run the for loop. And actually, I need to change these to I. So instead of this being index, I'm just going to leave it as I. And this is also going to be I. So we have int i is equal to 1. We're going to keep looping as long as i is less than or equal to 5. And then at the end we're just going to increment i. So we're going to say i ++. And every time through the loop we are printing out i. So let's go ahead and run this code. And you'll see we get the same exact output as we did with the while loop. So it's just still just 1 2 3 4 5. So what's cool about these for loops is we can essentially like keep track of a value every time we go through. And in our case, we're basically just keeping track of how many times we've gone through the loop. So I want to show you guys how we can use these for loops to actually iterate through the contents of an array. So let's say I created an array of integers. So we'll just call it like nums. And I'm just going to give this an initial value. So why don't we just say 1 2 5 7 3. Right? So I have this array of integers and it just has a bunch of numbers in it. And if you recall with integers, if we wanted to access a specific element inside of this list, I could just say like nums and then put the index in here. So if I said num zero, this is going to give us access to this one. If I said nums 4, it's going to give us access to 0 1 2 3 4. It's going to give us access to this three. What I could actually do is I could modify my for loop down here in order to actually loop through and print out all the contents of this array. So array indexes start at zero. So I'm going to start this I variable at zero. And what I'm going to do is I'm basically going to say I is less than the number of elements inside of the array. So inside of this array we have 1 2 3 4 five elements. So I'm going to say while I is less than five, we want to keep looping. And then I'm just going to say I ++. And down here, instead of printing out I, I could actually just print out nums I. And what this is going to do is on the first iteration of the loop, it's going to print out nums zero. On the second iteration of the loop, it's going to print out nums one. On the third iteration of the loop, it's going to print out nums 2, etc. And it's going to do that all the way up to nums 4, which as we saw is going to be this three. So that's going to be the last element in the array. So we can actually use this for loop to loop through all the elements in the nums array. So let's go ahead and do that. And you'll see over here we're printing out all of the elements. So we're printing out 1 2 5 73. So that is a very common situation. We can use for loops to iterate over the elements inside of an array and it can be really useful. So play around with these for loops. These are extremely useful. Um and one more time I just want to go over the structure. So the first thing we're doing over here is we're um creating a variable or we're initializing a variable. So I'm saying int i is equal to zero. And then I'm specifying my looping guard. So my loop condition, I'm saying we're going to keep looping as long as i is less than five. And then this is a line of code that'll get run after every iteration of the loop. So every time we go through this loop, we'll come up here, run this line of code, and then we'll check the condition again. That is the basics of a for loop. This is a very useful loop. I would say this is almost even more widely used than a while loop in a lot of situations. So, you definitely want to practice and play around with these for loops. In this tutorial, I'm going to show you guys how to build an exponent function in C++. Now, an exponent function is basically a function that will take a number to a specific power. So, um I could write a function that I could pass in two numbers. Like if I passed in three and four, this would basically give me back three raised to the fourth power. So I'm going to show you guys how we can build this and we'll actually get a chance to use uh four loops. So we can use a for loop in order to build this function and it's going to be pretty cool. So up here I'm going to create my exponent function. So we're going to have this use a integer. So I'm just going to say int. And why don't we just call this power cuz it's taking a number to a specific power. I'm going to make an open and closed parenthesis and open and close curly bracket. Now, our power function is going to take two arguments. So, it's going to take a base number and it's going to take a power number. And we're basically going to take the base number to the power of the power number. So, I'm just going to say int and then int power pow num. Now, down here in our power function, we need to figure out how we can do this. So I basically need to take base num to power num. And we can use something called a for loop in order to do that. So the first thing I want to do is just create an a variable. So I'm just going to call it result. And I'm just going to set this equal to one. And then down here I'm basically just going to return result. So essentially our goal inside of this function is to get the result variable equal to the value of the base num raised to the power number. Right? That's kind of our goal throughout this function. Let's just say for the purposes of simplicity and the purposes just for of this tutorial that we're only going to be able to handle positive number exponents. So, we're just going to go ahead and assume that pow num is going to be positive. And that'll just make it a lot simpler and kind of help me to drive home the point a little bit better. What we're going to need is a for loop. And I'm going to go ahead and write out this for loop. Now, up here in the parenthesis, we need to specify three things. The first thing I need to specify is a variable. So I'm just going to say int i is equal to zero. I'll start this out at zero. Now what I need to do is specify a looping condition or a loop guard. So I want to keep looping as long as I is less than the pow num. Essentially what I'm saying here is I want to go through this loop pow num times. So as many times as the pn num specifies I want to go through this loop. So if pn num is three I want to loop through three times. If pound num is five, I want to loop through five times. That's basically what this is saying. And then over here, we can just specify something that we want to do after each iteration of the loop. So I'm just going to say i ++. And basically, we'll just be incrementing the value of i every time we go through the loop. All right. So down here inside of our for loop, we need to think about what we can do. So we're we already know that we're going to be looping through here pound num times, right? So what I think we should do is we should multiply result times the base num every time we go through this loop. So what I'm going to say is result is equal to result times base num. And the reason that this is going to work is because let's just kind of break this down. The first time we go through this loop, right? Result is going to be equal to one. So the first time we go through this loop, we're multiplying base num by one. So we just get base num. The second time we go through the loop, result is equal to base num. Right? In other words, result has the value of base num. So the second time we go through, we're essentially multiplying benum times benum. Basum squared, right? The third time we go through this loop, result has the value of benum squared. So if we're multiplying benum squared time basum, that'll give us benum cubed. So that's kind of why this is going to work, right? So we're looping through this for loop pow num times and every time through the loop we're multiplying result times the base num. And you can see this for loop is very useful because it allows us to only loop through the loop a specified number of times right it makes it really easy for us to only loop through you know pound num times essentially. All right so this is the basic function and this looks pretty good to me. So again we're only going to this is only going to be able to handle positive number uh exponents. So positive number pow nums. Okay. So let's go ahead and call this. So I'm just going to say actually why don't we print out the result. So I'm going to see out um power and I'm going to pass in two numbers. So why don't we pass in two and three. So we're going to go ahead and do um 2 cubed. So now let's run this function and you'll see we're getting eight. So 2 cubed is 8. 2 * 2 is 4. 4 * 2 is 8. Let's try another one. Why don't we do 4^ squared? So now we should get 16. And you can see over there we get 16. So it looks like our little function is working. And basically the point of this tutorial was just to kind of demonstrate to you another way that we can use these for loops. So I'm using this for loop in order to essentially specify how many times I want to loop through something. And for loops are really useful for that because we're keeping this count. So I is representing how many times we've gone through the loop. So on the third iteration of the loop, I can tell us that we've gone through three times essentially and that's why this is useful. And so you can see it's actually really easy to do something like that. It only takes up like two lines of code. So that's uh the basics of building something like a power function. And actually this is kind of useful because you can kind of see how a function like this might get put together. In this tutorial, I want to talk to you guys about two separate subjects in C++. The first subject we're going to talk about are going to be two-dimensional arrays. And a two-dimensional array is a situation where we have an array where every single element inside the array is another array. So, this is a pretty cool situation. And I want to talk to you guys about one other thing, which are nested for loops. And a nested for loop is a situation where we have a for loop inside of another for loop. So this is going to be kind of cool. And you'll see we can actually use these two subjects together, two dimensional arrays and nested for loops in order to build a kind of a cool program. So I'm going to show you guys the basics of doing that stuff. First thing I want to show you is a two-dimensional array. So I'm going to go ahead and create an array of numbers. And I'm just going to call it number grid. And normally when we create a regular array, we're just going to make an open and closed square bracket. But since we're making a two-dimensional array, I want to make another open and close square bracket after this. Now remember, a two-dimensional array is a situation where we have an array where every element inside of the array is another array. And I'm going to show you guys how we can go ahead and set something like this up. So I'm going to give this an initial value. So I'm just going to initialize this. And I'm basically just going to put a bunch of array elements inside of here. So the first thing we're going to do is create the first element in the array. Now normally if I was creating an array, I could just kind of type out elements like this, right? You know, I can kind of type them out, delineate them with a comma, and then we have our array. But with a two-dimensional array, remember each individual element is actually another array. So if I want to create one element inside this two dimensional array, I can basically go like this, right? So I can make another sort of mini array inside of here. And I'm actually just going to format this a little bit. And so now the first element in this number grid array is going to be an array with elements one and two. So number grid first element is this array. And this array has two elements of its own. So, I'm going to copy this and we're going to make another element. So, I'm going to put a comma here because remember we need a comma to delineate the different elements. And then down here, I'm just going to paste. So, now we're going to do three and four. So, we have 1 2 3 4. And why don't we do one more? And we'll just do five six. All right. So, essentially what's happening here is we have our overall array, right? So these two curly brackets represent the number grid array. And inside of the number grid array, we have three elements. So this is the first element. This is the second element. And this is the third element. All of which are arrays. And each one of these arrays has one two elements inside of them. So whenever we're creating two-dimensional arrays, we always want to specify um the numbers over here. So inside of this first open and closed square bracket, we want to tell uh C++ how many total elements are in the number grid array. So in our case, we have three because again there's 1 2 3 elements. Then over here we want to specify how many elements are inside of each array element. So you'll notice we have 1 2 1 2 1 2 etc. So there's going to be two elements in each one of these arrays. And it's very important especially when you're dealing with two-dimensional arrays that you specify these numbers because otherwise you can get really confused you know and so by specifying 32 I know the basic layout like I know that this array has three rows like you can call these rows and then it has two columns so these would be like columns essentially we're building a matrix all right so that is a two dimensional array and let's talk about how we can actually access some of these elements so if I want to access uh some these elements. I'm actually just going to go ahead and print some of these out. So over here, I can just say number grid. And what I can do is I can make two open and closed square brackets. So I can go just like just like this. And inside of these open and closed square brackets, these first ones, I want to specify the row. In other words, I want to specify the element inside of number grid that I want to access. So this would be the element at position zero. This would be the element at position one. And this would be the element at position two. So let's say we wanted to access this element right here. I can put a zero here because it's inside of element 0. And then over here I want to put the index of that element inside of the actual array. So it's going to be at index position 01. So 0 again is corresponding to this overall element and one is corresponding to this element inside of that array. All right. So now when we print this out, we should get a two and you'll see over here we get that two. Let's try one more. Why don't we try to get this five over here? So five is going to be an element 0 1 2. So we're going to put a two over here. And it's going to be element zero. So we're just going to put a zero. And now we should get that five which we do. All right. So that kind of shows you a little bit about how we can create a two dimensional array. And actually this same concept can apply to uh like n dimensional arrays. So you could have a three or four or five dimensional array and all these same concepts are going to apply. Obviously the more dimensions you add the more complex it becomes but you can get pretty complex with arrays uh just by using this technique. All right so that was the first thing I wanted to show you guys and now I want to introduce another topic which is a nested for loop. A nested for loop is a situation where we have a for loop inside of another for loop. And actually nested for loops can be extremely useful because we can use them to print out all of the elements inside of this two-dimensional array. So basically we could iterate through all the elements inside of here using a nested for loop. And I'm going to show you guys how that's going to work. So down here I want to create a basic for loop. So I'm just going to say four and I'm just going to say int i is equal to zero. Again our goal here is to be able to loop through all of the elements in this array. And you know, as I sort of create this for loop, just kind of stick with me. Eventually, this is all going to make sense. Um, but what I want to do is I want to keep looping while I is less than three. And I'm using three here because I want this for loop to loop through all of these individual elements. So all of the arrays inside of the number grid array. Then over here, I'm just going to say I ++. So this for loop is responsible for iterating over all of these elements. So this first element, this second element, and this third element. But remember each of these elements is another array. So what I want to do is for each of these arrays, I want to iterate over each of the elements inside of those arrays. So I can create another for loop to do that. So I can say int and I'm just going to call this j. And a lot of times when you're using nested for loops, people will name these i j. That's kind of a convention. I'm going to set this equal to zero. And I'm going to keep looping as long as J is less than two. And remember, two is how many elements are inside each of these arrays, right? So this loop over here is responsible for iterating through all of these elements. And this one down here is responsible for iterating through all of these elements. So I'm going to say J less than two. And then I'm just going to say J++. So now we have our basic structure set up. For every iteration of this I loop, this J loop is going to fully iterate. So it's going to go through all the way. And down here I'm just going to see out and I'm basically just going to print out all of these different elements. So I'm going to print out number grid I J. And you guys will see in a second um basically how this is going to work. And then real quick down here I'm just going to see out um a new line. So I'm just going to say see out end l and that'll print kind of a new line. So let's go ahead and run this and then I'll give you guys more of an explanation of as far as what it's doing. So when I run this, you'll notice that we're actually printing out all of these elements. So we're printing out 1 2 3 4 5 6. So I'm actually able to go through and iterate over every single element inside of this uh two-dimensional array. And basically what's happening is every time we go through this top loop up here, this i loop, we're fully iterating through the J loop. So this I loop is responsible for iterating over each individual element. So it's responsible for iterating over this, this, and then this. This J loop is responsible for iterating over each of the elements inside of those elements which are arrays. So the J loop is going to loop over this and this. So the first time we go through this loop, I is going to be equal to zero and J is going to be equal to zero. So we're going to print this out. Then we're going to go through this J loop again. I is still going to be equal to zero, but J is going to be equal to one. So we're going to print this element out. And then I is going to be equal to one. So it's going to be 1 0. We'll print this out. And then it's going to be 1 1. So we'll print this out. And that's essentially what's happening here. So nested for loops and even like nested while loops. I mean any sort of nested loop can be very useful and it can be useful for looping through like I said complex structures like that. In this tutorial I want to talk to you guys about using comments in C++. Now comments are little special blocks of code which aren't actually going to get executed or rendered by C++. So, a lot of times in your programs, you're going to want to, you know, basically write things that are meant for human beings. So, whenever we're writing uh programs, like whenever we're writing uh code or instructions, those instructions are meant for the computer. So, I can tell the computer to print something out or I can tell the computer to loop over a block of code or something like that. But a lot of times, we're going to want to write out messages just for human beings. So, either for yourself or for someone else. And we can do that using something called a comment. And I'm going to show you guys what comments are. Essentially, a comment is just a, you know, a little line of code or a block of code that uh C++ is going to ignore. And therefore, we can use it to write little messages for ourselves. So, in order to create a comment in C++, there's actually a couple ways we can do it. The first way is just to make two forward slashes. So, when I make two forward slashes, you'll notice that when I start typing, all of this text is actually colored different than the text over here. That's because this is a comment. So any text that I put after these two forward slashes is going to be considered a comment and it's not actually going to get rendered by C++. So even though I have a bunch of nonsense up here, I can still run my program and it's still going to be able to execute. So you'll see I can run the program and there's no errors. It's just printing out comments are fun. So this is a really great way to kind of write a little note. A lot of people will use this to write notes to themselves or maybe write notes to other developers. Um, you can also use this to like describe a line of code. So, I could like describe this line of code over here. I could say like um prints out text, right? Obviously, you know, you you probably wouldn't need to describe this line, but if you had like a complex function or uh, you know, complex loop or something like that, a comment could come in handy. You can also put these comments like after a line of code. So, I could just um cut this out and paste it down here. And you'll see that everything that comes after these two forward slashes is going to be considered a comment, but I can still put code over here. And again, you could use this to like describe what the line of code is doing or something like that. The one downside to this type of comment though is that it only works on one single line. So if I come down here to the new line, all of a sudden this goes back to normal text. So this is no longer a comment. Um, if you want to have a long block of comments though, you can use something called an opening and closing tag. So if I make a forward slash and an asterisk, then everything that comes after this, even if it's on a different line, is going to be considered a comment. So you can see all of this stuff is now a comment. But I can close this off by saying an asterisk and another forward slash. But now everything that's in between these two tags is going to be considered a comment. And these two types of comments are very common. Um, and you know, pretty much either one you use is going to be uh correct and is going to work out. So that's basically how the you know the two types of comments and how you can use them. Another uh way that comments are useful is for commenting out lines of code. So a lot of times when you're working with your programs maybe you want to try to run your program without a certain line of code. You might uh be running into a problem and you think a certain line of code is the culprit. Well, one option would be to just remove the line of code. So, for example, let's say that this line of code um was potentially like messing up my program and I wanted to see how the program would act without that line of code. Well, I could just delete it, save the file, and then run the program. And now we're running the program without that line of code. The problem with that though is I have to physically delete the line of code, right? If I wanted though, I could just put a comment in front of it and now this entire line is going to get ignored by C++. So, it's essentially the same as removing it, but we don't actually have to physically remove it from the file. It's still going to be there. We can still see it. We still know that it's there. Um, it's just not going to get executed. So, that's another useful way that comments can come in handy. So, yeah, I think a lot of developers use comments. Uh, and you definitely want to take advantage of them in your programs when they're appropriate. In this tutorial, I want to talk to you guys about pointers in C++. Now, a pointer is basically just a type of information that we can work with in our programs. If you've been following along with this course up to this point, we've been working with all different types of data. So, we've been working with things like integers, which are whole numbers. We've been working with like doubles, which are decimal numbers. We've been working with strings and chars, which are like plain text. Um, and in this tutorial, I want to introduce you guys to another type of information we can deal with, which are called pointers. And pointers are basically just memory addresses. So, we're going to kind of get into what that means. What is a memory address? We're going to talk about all that stuff, and I'll give you a full introduction into pointers. So, over here in my program, I have a few different variables that I created. I have an integer called age, and I gave it, you know, a value, a double called GPA, and a string called name. Now, all of these are variables in my program, right? So in other words, all of these are essentially just containers where I'm storing specific information. For my purposes, when I'm writing this program, I'm storing the value 19 inside of this age variable, right? I'm storing 2.7 inside of this GPA variable. But let's kind of, you know, go underneath the hood a little bit and talk about what's actually going on here. So in everyone's computer, you have something called your memory. And another word for this would be RAM. So you might have heard people talk about RAM. stands for random access memory. It's essentially just the active memory that your computer's using when it's running programs. So whenever your computer runs a program, um it's using RAM. So it's storing information inside of this uh memory. Let's just say our program for example. So in the case of this program, if I was to run this program, my computer would actually be using RAM. In other words, my computer is going to use its memory in order to store and keep track of information. In fact, it's going to be storing all of these values inside of its memory. So when I create this variable age and I give it a value, what's actually happening is my program is going to take this value 19 and it's going to store it inside the physical memory of my computer. Right? When I create this double GPA, my computer is going to take this number and it's going to store it physically inside of the memory in my computer. Same goes for that string. And so for all of these different pieces of data, they're all actually getting stored inside of the memory of my computer. So when I write the program, like for my purposes, I just know that 19 is stored inside the age variable. But when we go underneath the hood, 19 is actually stored inside of the computer's memory. So it's physically stored in the memory. Now, one of the cool things about the memory of our computer is there's a bunch of these little containers, right? Each one of these variables, each one of these values is essentially just like inside of one of those containers, right? That's kind of a broadstrokes explanation, but you could basically think of it as like this number 19 is stored inside of a container in memory. This uh value 2.7 is stored inside of a container in memory. And each of the containers inside the memory of my computer where these values are stored has an address, right? So it has an address which uniquely identifies it. So for example, this value 19 is stored inside the physical memory of my computer and it's stored at a specific memory address. This value 2.7 is stored in the memory of my computer and it's stored at a physical memory address. And so when my program wants to access this value, it can access it by using that memory address. When I want to access the variable, I can just access it using like age or GPA. But when my computer wants to access it, my computer has to access it using the physical memory address. So that's kind of like what's going on. And I want to show you guys um how we can go ahead and access that physical memory address. So I could actually print it out. I could just say see out. And if I wanted, I could basically print out the memory address where each of these variables are stored. In other words, I could print out the memory address where this value 19 is stored. I could print out the memory address where this value 2.7 is stored. All I have to do is just make an amperand and then I can type in the name of a variable. So I could type in like age for example. And what this is going to do is when I put this amperand here, it's essentially going to tell C++ that I want to print out the memory address where the age variable is stored. In other words, I want to print out the memory address where this value of 19 is stored. So when I run my program, you'll notice over here that I'm getting this kind of crazy number. So it's 0x6 a fee e 0. This is a hexadimal number. Basically, it's just some long number um that would be very difficult for a human being to remember. Um but the computer's able to remember it just fine. So if you were to go to this physical memory address inside of my computer, you would find the value 19. I mean, that's essentially what this is doing, right? So that's basically like where that value 19 lives inside of our memory, right? So if I wanted to access it or change it or modify it, my program can do it using that address. And that's essentially what we're talking about. And in C++, we have a word for these addresses. So we have like a special word that we use when we're talking about addresses. It's called a pointer. So generally, if I'm working in C++ and I want to talk about a memory address, I'm going to talk about pointers. So I would say that this is a pointer, right? So over here we could say that I'm printing out a pointer. And a pointer is just a type of information. It's just a memory address. So any memory address that we're working with or that we're using inside of our programs is going to be considered a pointer. And the way that we can access the pointer of these variables or in other words, the way that we can access the memory addresses where these variables are stored is by using this amperand. So I have a little demonstration I want to show you guys. Um, this is just a block of code that I wrote out before the tutorial and it's essentially just printing out um all of the memory addresses for all of these variables. So, I'm going to go ahead and run this. So, you can see over here I'm essentially just printing out all the individual memory addresses for all of these variables. So, we have age and it's located at this memory address. We have GPA at this memory address and name at this memory address. So all of these memory addresses if we were to go to them in our like physical RAM in the physical memory of our computer we would see those values and again we would call these pointers. So this is a pointer. This is a pointer. A pointer is just a memory address. That's it's just a type of data. We're just giving it another name. So that's kind of cool. And that kind of shows you guys how we can access like the memory addresses of these different variables. But we can actually take this a step further. So I want to show you guys um another thing that I can do. So I could actually create a variable where I could store the pointer. So over here like I'm creating an integer variable and inside of it I'm storing an integer. Right? Over here I'm creating a double variable and I'm storing a double. Over here I'm creating a string variable. I'm storing a string. Um and actually if we want to manage and keep track of the memory addresses or the pointers inside of our programs, we can create a pointer variable. So I'm going to show you guys how we can do that. And a pointer variable will basically be just a container where we can store a pointer, right? It's a container where we can store a memory address. Generally, when we're working with memory addresses in our programs, we're not just going to work with random addresses, right? In other words, like I don't know any like meaningful memory addresses off the top of my head. So generally when we're using memory addresses, we're going to be using the memory addresses of the different variables in our programs. So therefore, when we create an actual pointer variable, in other words, when I create a container where I'm going to store a pointer, I'm generally going to create it based off of one of these variables. So let's go ahead and create a pointer variable that will store the pointer um for this age variable. I'm just going to say int. And whenever we create a pointer variable, you always want to use this special character, which is going to be the asterisk. And then you want to type in the name of the variable. So I'm just going to call this page. And a lot of times when you're creating pointer variables, you'll use this lowercase p and then you'll type out the name of the variable whose memory address you're storing. So I can say in ph is equal to and now I can just say amperand age. So remember when we use this amperand and then we type out the name of the variable that gives us the pointer. In other words, that gives us the memory address where the value is stored. So, I'm essentially storing this pointer inside of this variable over here called PH. I could do the same thing down here for this double. I could just say double asterisk and I'm just going to say pgpa and I'm going to set this equal to amperand GPA. So now this pgpa pointer variable is storing inside of it a pointer. In other words, it's storing inside of it a memory address, and it just happens to be the memory address of this GPA variable. I can do the same thing for this string down here. So, I could say string pame. And actually, don't forget to put the asterisk. And I can just set this equal to amperand name. And now this pointer variable pame is storing the pointer. In other words, it's storing the memory address of this name variable. So now I can actually work with these different pointer values using these pointer variables. So if I was to come down here, I could say see out and I could just print out like page. And now this is going to go ahead and print out the value that's stored inside of that pointer variable which is going to be a pointer um which should be the memory address of PH. So you can see down there that's exactly what we get. So that's kind of useful. And you know really I think a lot of times people get a little intimidated or maybe confused with pointers but pointers are really simple. A pointer is just another type of data that we can work with in C++. So you know you can work with things like integers which are whole numbers. You could work with doubles which are decimal numbers. You could work with strings which are just a bunch of characters. You could also work with pointers which are memory addresses. You know that's all it is. It's just a different type of data. And we when we create a pointer variable, it's just a container where we can store a pointer. That's kind of all it is. Now, I want to point out to you guys um whenever I created this pointer variable, you'll notice that I used the data type of the variable that I was pointing to. So, this pointer variable is storing the memory address of an integer. Therefore, I said int over here. This pointer variable is storing the address of a double. So, I said
Original Description
This course will give you a full introduction into all of the core concepts in C++.
❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp
⭐️ Contents ⭐
⌨️ (0:00:00) Introduction
⌨️ (0:01:38) Windows Installation
⌨️ (0:04:54) Mac Installation
⌨️ (0:08:44) Setup & Hello World
⌨️ (0:12:29) Drawing a Shape
⌨️ (0:19:55) Variables
⌨️ (0:31:43) Data Types
⌨️ (0:39:15) Working With Strings
⌨️ (0:49:00) Working With Numbers
⌨️ (0:59:41) Getting User Input
⌨️ (1:05:32) Building a Calculator
⌨️ (1:09:28) Building a Mad Libs
⌨️ (1:13:45) Arrays
⌨️ (1:20:03) Functions
⌨️ (1:29:47) Return Statement
⌨️ (1:35:22) If Statements
⌨️ (1:47:15) If Statements (con't)
⌨️ (1:55:58) Building a Better Calculator
⌨️ (2:02:20) Switch Statements
⌨️ (2:10:47) While Loops
⌨️ (2:18:53) Building a Guessing Game
⌨️ (2:29:18) For Loops
⌨️ (2:38:32) Exponent Function
⌨️ (2:45:21) 2d Arrays & Nested Loops
⌨️ (2:54:55) Comments
⌨️ (2:59:11) Pointers
⌨️ (3:13:26) Classes & Objects
⌨️ (3:25:40) Constructor Functions
⌨️ (3:34:41) Object Functions
⌨️ (3:41:43) Getters & Setters
⌨️ (3:54:04) Inheritance
Course developed by Mike Dane. Check out his YouTube channel for more great programming courses: https://www.youtube.com/channel/UCvmINlrza7JHB1zkIOuXEbw
⭐️Other full courses by Mike Dane on our channel ⭐️
💻C: https://youtu.be/KJgsSFOSQv0
💻Python: https://youtu.be/rfscVS0vtbw
💻SQL: https://youtu.be/HXV3zeQKqGY
💻Ruby: https://youtu.be/t_ispmWmdjY
💻PHP: https://youtu.be/OK_JCtrrv-c
--
Learn to code for free and get a developer job: https://www.freecodecamp.org
Read hundreds of articles on programming: https://medium.freecodecamp.org
❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
Dates - Beau teaches JavaScript
freeCodeCamp.org
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
The Definition of Ready - Agile Software Development
freeCodeCamp.org
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
Working Agreement - Agile Software Development
freeCodeCamp.org
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
Definition of Done - Agile Software Development
freeCodeCamp.org
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
The INVEST approach to product backlog items
freeCodeCamp.org
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org
Related Reads
📰
📰
📰
📰
The AI Tools You’re Paying For Have Free Twins Nobody Mentions
Medium · AI
I Accidentally Stumbled Into an AI Side Hustle — Here's Everything I've Learned
Dev.to AI
How I Made $700 in 2 Weeks Selling AI Automation Templates
Dev.to AI
MCP Server Tutorial: Build Your Own AI Tools in 30 Minutes
Dev.to · NeuroLink AI
🎓
Tutor Explanation
DeepCamp AI