this complete c plus plus course will take you from being an absolute beginner to using advanced c plus plus features danielle gaguaya teaches this course he is an experienced software developer and has created many popular courses hello guys and welcome to this modern c plus plus 20 course this is a course that is going to take you from the absolute beginning where you know nothing about c plus plus and you're going to go all the way to a point where you can use some advanced features in the c plus plus programming language that includes things like object-oriented programming with inheritance and polymorphism with the c plus plus programming language now i'm not going to spend a lot of time convincing you on why you should use c plus plus but c plus plus is a very powerful programming language you can use to do all kinds of crazy things you can use it to do systems programming you can build games with it and it is used in a wide range of areas so you can do a lot with c plus plus now who is this course for this course is built for complete beginners if you already have some experience with c plus plus you will still find some good things you can learn from the course but i am going to go from the absolute beginning showing you every little thing showing you the tools showing you the compilers and the editor we're going to be using here so we're going to spend a lot of time making sure you understand the fundamentals by the time you are done with this course you will understand the bare fundamentals about c plus we will spend a lot of time exploring the procedure programming side of c plus plus so you're going to be writing programs that run from top to bottom and we're going to spend a lot of time in the editor writing code making sure you really understand what is going on here towards the end we're going to learn about object oriented programming and we're going to touch on classes inheritance and polymorphism and these are the tools you're going to be using a lot in your career as a c plus plus developer another great feature of this course is that we spend a lot of time making sure you understand the tools so if you don't know what a compiler is if you don't know what an ide is we're going to make sure you understand these things and we're going to spend a lot of time using these things and by the time you're done with the course you're going to be very comfortable using the tools the editor of choice for the course is going to be visual studio code so that's an editor that is going to allow us to type in the code and give us some things like a syntax highlighting but we're going to be connecting our editor to a background compiler which is going to make sure our code is transformed to run on our computer but don't worry about all that craziness right now we're going to go in detail about these things for now i just want you to have a bird's eye view on what you can expect from the course here now i hope you know what to expect from the course but i also expect you to be ready to take the course and the requirements are really not that bad the only requirement is that you have time set aside to really practice what you learn from the course here and you need to have a strong drive and willingness to learn now if you want to follow along i am going to share a link to the github repository for the code i use in the course so you can check that out and really follow along if you want if you want some support we have a discord server dedicated for our students so you can join and be part of the community if you have a question you can ask but another thing i would encourage you to do is to also help others so if you see somebody asking a question try to help them you're going to learn a lot that way okay now that you know a bit about the course let's talk about myself here my name is daniel cacoya i am a software engineer i have been writing code since 2011 or something and i am a professional c plus plus developer i do a lot of freelance work i work for a few companies i do all kinds of crazy things i also like to teach others so i have a few courses on udemy if you are interested in cute you can check these things out i also have a trending c plus plus course that you can check out but don't feel the need to go and check this ziplesque course out because a lot is covered in this youtube course here so that's really all about me okay now that you have an idea about the course we're going to head over in the next lecture and learn about the tools we're going to be using throughout the course here go ahead and finish up here and meet me there welcome to this new chapter where we're going to be exploring the c plus plus development tools you need to start writing your own c plus plus applications and you really need two kinds of tools the first one is going to be an editor or an ide through which you're going to be writing your c plus plus code we are going to be using visual studio code as an editor in this course because it is very easy to install it is cross-platform it is going to run well on windows mac and linux and it supports a host of tools that are going to make your job easier as a surplus plus developer but that's not to say you can't use your favorite ide if you can but visual studio code is going to be our main editor in this course so i would recommend installing it to be able to follow along with what we do here now after we have an ide or an editor in place we will need a compiler and the compiler is a piece of software that takes the code we write in the editor and compiles that into code that can directly run on the hardware or binary executable format so this is what we're going to be doing here we're going to be typing our code in an editor like visual studio code we're going to be kicking off a process to compile our program and this is going to turn our program into a format that can run it directly on the hardware so for example if you are on a windows machine you're going to generate a binary executable that can directly run on windows if you are on linux we will generate a binary that can run on linux and the same concepts can really extend on mac or any other kind of operating system you might be working on with the course here so we need two tools we need an ide or an editor and a compiler and the tools you install are going to be depending on the operating system you are using to watch the course if you are on windows you're going to install a set of tools that are specific to windows if you are on linux you're going to install a set of tools for linux if you are on a mac you're going to install a set of tools specific to the apple operating system for desktop now on windows in terms of ides or integrated development environment it is possible to use a host of ides for example you can use code lite it is an ide you can use you can use microsoft visual studio but in this course we're going to be using visual studio code because it is easy to install it has a set of features that are really good it is popular it has a great community around it i think you're going to have a great time as a beginner to start writing your c plus plus programs using visual studio code that's not to say that you can't use another ide you can if you want but i am going to be using visual studio code in this course here now on linux we are also going to be using visual studio code as our editor in the course but there are other ides you can use for example you can use cute creator if you want you can use codelight you can use a host of other ides on linux but again visual studio code is going to be our thing in the course here and i would recommend installing it on your linux box if you are watching this course on the linux machine so for osx we are also going to be using visual studio code as our editor as i said it is cross platforms it runs where regardless of the operating system you might be using to watch the course but if you want you can use the apple ide which is xcode or even code lite all these are going to work if you want but again i would recommend going with visual studio code.plus to start out so that we have a common ground and you don't get confused very easily especially if you are a beginner now after we have an ide rolling we will need a compiler and as we say the compiler is a tool that is going to transform your c plus plus code into a format that can run directly on the hardware so the compiler you use again is going to depend on the operating system where you are watching the course i am going to show you a bunch of compilers you can use but gcc is going to be our common base so on windows i will be using what we call mingw don't worry if you don't what it is i am going to show you in a minute when we install it but this is a project that brings the gcc compiler on windows so that we can use it easily and i am going to show you how to install it but i am also going to show you how to install the compiler from microsoft and the clang llvm compiler these are all available to you on windows and it is really easy to install them these days and i will be using them all if you are on windows i would recommend installing them all so that you really have a chance to try your c plus plus code on the different compilers and that's a really good thing to do if you test your c plus plus code against multiple compilers you will be increasing your chances for your code to really be portable and work on multiple compilers and operating systems and that cannot be a bad thing so i would recommend installing all these compilers if you are on windows i am also going to show you how to install two compilers on linux i am going to show you gcc we're going to install the shiny new feature in gcc we're going to install clang lvm on linux and we will be able to use all these compilers again if you are on linux i would recommend installing all these compilers so that you have a chance to try your code against multiple compilers and that's going to make your code much portable and easier to use on multiple compilers and platforms on the mac i am going to show you how to install gcc and i am going to show you how to install clangle lvm but if you want you can also install xcode and the compiler that comes with that for c plus but development all these are options to you but again in this course we are mostly going to be using the gcc compiler so i would recommend getting a hold of this if you don't want to install other compilers if you only have to install one compiler please install gcc that's going to be our main compiler in the course here okay so these are the tools we need we are going to head over in the next lecture and show you how to install the visual studio code editor on windows and really get started with that go ahead and finish up here and meet me in the next lecture this video i am going to show you how to install a c plus plus compiler on your windows operating system so we're going to be looking at three kinds of compilers we're going to look at the mingw compiler which gives us access to a gcc like compiler on windows we're going to look at clan llvm which is another compiler we're going to look at the compiler from microsoft now before we talk about any compiler i would like you to come to google or your favorite search engine and say c plus plus compiler support okay so we're going to choose this little guy here and this is really going to give us a berg's eye view on which feature is supported by which c plus plus compiler for example if we are interested in c plus plus 20 which is the lattice standard that we have in simplest plus at the time of recording this video i am recording of this in august 2021 but if we go here we're going to see c plus plus 20 core features and we're going to see that we have a list of what each compiler supports we have gcc we have clan we have msvc we have the clan version from the apple company and if we scroll down we can really see the features that are supported by each compiler again gcc is going to be our choice in this course this is going to be our main compiler but nothing really stops you from using the clan compiler or the msvc compiler and if you happen to see that your compiler of choice doesn't support what you need for example you can see that at the current time lambdas in an evaluated context are not supported in clan you can see that it is ready here but this is a feature you can use both in gcc and the msvc compiler from microsoft so keep an eye out on this page here to know which kind of things you can use in your compiler if you try something out and you see that it doesn't work this is a good place to come and figure out if your compiler supports the feature that you are using in this video i am going to show you how to install the three most common compilers and those are gcc clang msvc if you want you can come in your search engine and type gcc to see what this is it is a good thing to do to learn about your tools here you can see that it is a compiler that is mostly used on the lenox platform but we can also use it on windows the latest release that we're going to be installing is eleven two zero i think it was released six days ago so it is really fresh and we're going to be taking advantage of this another thing i should say here is that you should install the latest tools you can get your hands upon this is the version we're going to be mostly using in this course but if you are watching this in the future you're going to have a newer version i would recommend installing the latest version or the newest version you can get your hands upon so the first we're going to do is install a gcc compiler on windows i am going to show you a way we can get both gcc and clang in one go and to get access to that please go in your search engine and type windlabs and this is going to bring us to this project here if you want you can click upon this and this is a cool project by a guy who is providing worlds for these compilers that we can use on windows so if you want you can just go down here and look at the lettuce releases again i recommend using the lettuce you can get your hands upon in this case the lettuce happens to be 1120 but in the future this is going to change you're going to find a version for 12 or 13 so please grab the newest version you can get your hands up on now i want you to install the version that says that it has llvm and clang so if you are on a 32-bit version on windows i would recommend getting this 7-zip archive on this zip archive here if you are on 64 i would recommend getting a hold of this or this little thing here it doesn't really matter but make sure you get the latest version you can get your hands upon and you can see that this conveniently says latest here so i am going to download the 64-bit version on windows i am going to grab the zip archive here and let's click upon this this should kick off my download and this is going to download on my system i can download this if i want and you're going to see that it is going to start downloading here and i am going to wait for this to finish again this is going to give us both the gcc compiler and the clan compiler and this is really like shooting two birds with one stone so this is going to be really good i already have this downloaded so i am just going to head over to the location where i have this downloaded i think i can just go in my drive here so let's crack this open and go in my downloads folder and if i go in my compressed folder i am going to find winlabs llvm and mingw this is what you really want but if you don't want the clan compiler you can grab the one that doesn't say llvm here because we really want both of these compilers we are going to grab the one that has both the gcc compiler and the clan compiler if we crack this open let's open this together so that you can see what is happening we are going to have this little folder here if we go in we're going to find a bin folder if we go in and crack that open we're going to have a folder that says clang you see we have a clang compiler we have our clan plus plus compiler for c plus plus and if we go down we're going to find our g plus plus compiler and the gcc compiler this is what we want so what we are going to do is extract this somewhere on our system i put this on my c drive so if you go here you're going to find mingw64 so all you really have to do is go where you downloaded your compiler and say extract we're going to right click on this and say extract to a location and you're going to specify where you want this to go and this is going to be extracted or even if you want you can extract here this is something i like to do this is going to show up in the current folder and you can grab this and paste that in a location where you want this in my case i put this in my c drive as i said here so this is going to be my compiler now once you have these compilers installed again we have the clan compiler we have the gcc compiler if we go down we're going to find the g plus plus we even have gdb which is a debugger we can use to debug c plus plus applications but we will talk about this later once you have this i would recommend putting this in your environment variables on windows the way you do this you grab this path well we have our compilers here and we're going to copy this this is something i recommend and on windows 10 you can go on the start menu and type env and this is going to give us a window in which we can edit our environment variables we're going to click on this little thing this is going to open up on our system we're going to wait for this to open up and we're going to have a window that says system properties we're going to click on environment variables here and we can put this in our user environment variables or on our system environment variables since i am the only one using this system i like to put this in my path for my system variables so i am going to click on path and click edit here and i am going to add a path to my gcc and cloud installation here you can see that right here if you don't have this end you can click on new you're going to have a window here and you can paste this right in and you can click ok but i'm not going to do that because i already have this in here so what i am going to do is just delete this let's see if i can delete i can delete this little guy and i have my path in here so if you don't have this in please make sure you have this in your environment variables so we're going to cancel out of this but if this is the first time you do this you should click ok and one way to test that your environment variables are working or even if your compilers are working is to crack up a command line terminal so we're going to say cmd on the start menu here and all you need to do is say g plus plus just like this dash dash version if you do this you're going to see information about your c plus plus compiler and this is coming from the installation we just did and for clan you can say clan plus plus version and this is going to tell you the information about your clan compiler installation let's wait for this to come up i don't know why it's slow but you can see that we have a clank compiler the lattice version is 1201 for the gcc compiler the latest version is 1120 and these are the compilers we can use to write c plus plus 20 applications okay so by now we have a gcc compiler through the main gw project we have a client llcm compiler on our system we are also going to see how to get a hold of the compiler from microsoft and to get hold of this you need to install microsoft visual studio to install microsoft visual studio all you have to do is come to your favorite search engine and say microsoft visual studio we can look at the 2019 version which is the latest i think and we're going to click on the download link here we have as the first link and we're going to see ways we can download this little guy now microsoft visual studio has a community version you can download for free but we also have a professional and enterprise versions that we're not going to care about in this course here we are just going to download the community version and that's going to be enough for our purposes here so if you click on free download here this is going to download and install the visual studio compiler you can click on this little guy and it is going to download an installer you're going to crack this open on your system and it is going to install the visual studio installer so once you have it installed you can come to the start menu here and say visual studio installer i think if you say visual studio you're going to have this pop-up somewhere on your system we're going to click on this and if you crack the installer open you're going to see a window like this which is really going to give you a chance to install the tools you want to use with the microsoft visual studio ide and in this case we are interested in desktop development with c plus plus so we're going to click this little guy here we're going to take it open and once you do this you going to click download and install and this is going to install the visual studio compiler with the underlying compiler you see that this is going to give us access to the msvc compiler and all kinds of crazy things we really needed to write c plus plus applications on windows using the compiler from microsoft now i have to say this is going to take some time to install so if you see that this takes up 20 minutes of your time don't worry this is really normal because this id and the compiler behind it and really large they're not like the gcc or the clan llvm compilers that we just installed okay so once you have this ticked or checked you're going to click on download or install whatever this button is saying here this is going to install both the microsoft visual studio ide and the compiler from microsoft that you can use directly to write c plus plus applications so once we have this open i am going to suppose that you have this installed already i am going to close out of this because i don't want to do this and you're going to have visual studio installed on your system once you have this installed you're going to come to the start menu and say developer and this is going to bring up two things one is going to be the developer powershell for visual studio 2019 and the other is going to be a command prompt it doesn't really matter what you choose i am going to click on the command prompt here and once you do this this is going to give you a terminal or a window through which the environment variables are already in to start using the compiler from microsoft and if we come here and type cl.exe we're going to see that we're going to see the information here if this gives you an error or something you're going to know that you didn't really install this properly so please go back and make sure you have this installed if you have a problem you can ask me i can do the best i can to help you out but by this moment we have three compilers on windows that we can use for c plus plus development we have the mingw or the gcc compiler in our hands we have the klanga lvm compiler and we have the compiler from microsoft and now we can install an ide or an editor and that's going to be visual studio code and we can connect that to these compilers so that we can start writing our own z plus bus 20 applications we are going to head over in the next lecture and install the visual studio code editor go ahead and finish up here and meet me there in this lecture i am going to show you how to install the visual studio code editor on windows so if you are watching this course on windows this video here is going to be a must watch we are going to head over to our search engine and type visual studio code this is going to give you a link where we can download this cool editor if you want you can learn all you can about it it is a source code editor made by microsoft it supports multiple languages but we're going to be using that specifically for c plus plus in the course here here i am at the page for the editor and you can read all you want about it but we are interested in downloading this for windows if you want you can click on this download button here but i am just going to scroll down and have download links for windows if i want i can click on user installer system installer you can click on what you want if you are on a 32-bit system you're going to download a 32-bit version i am just going to click on 64-bit here because i am on a 64-bit operating system again in this video we are doing this for windows so this is going to be our area of interest here i am going to click on this download link and i am going to wait for things to kick off and i will have a window to start downloading visual studio code setup here i can save this file and it is going to start downloading once it is downloaded i am going to double click and really click my way through this until i have a visual studio code installed on my computer here okay the download is good i can crack this open let's click on this and you're going to see that it is saying that it has detected that visual studio code is already running on my system because i have it installed but if you don't have it installed you're going to click your way through and install this on windows and after you have it you can start it okay once you start visual studio code you're going to have a window like this if you run it for the first time you are probably going to see a windows with documentation and whatever let's look at the welcome screen you're going to probably see you're going to probably see something like this but i am not sure but the most important thing is that you're going to have a window like this with visual studio code the first thing we want to do for c plus plus development is going to scroll down and click on this little thing that says extensions i am going to click on this and i am going to go in my search box here and type c and c plus i can even type c plus plus it doesn't really matter and this is an area where we can install extensions to make visual studio code do all kinds of crazy things in this case we are interested in making it support c and c plus plus so we are going to click on this extension that says microsoft here cnc plus plus i am going to click on this and if you don't have it installed you can click on the install button and it is going to install this little thing and make your computer ready to use visual studio code for c plus plus so make sure you install this and at the end it is going to say disable or uninstall because it will be already installed and enabled on your computer and this is really the end of our installation video for visual studio code we are going to head over in the next video and show you how you can make a visual studio code use the compilers that we installed in the previous video so go ahead and finish up here and meet me in the next lecture in this video we're going to see how to connect our visual studio code editor to a compiler so that we can compile our projects easily through the visual studio code editor we already have the ide installed from the last two previous lectures when we have a compiler installed in the last lecture we installed the gcc compiler through the mingw project we installed the clangela vm compiler we even installed the compiler from microsoft now we're going to connect the ide to the compiler to make it really easy to compile c plus plus applications using these tools here what i am going to do is really set up a template project we're going to be reusing throughout this course here so i am going to go on a folder in my system and i am going to right click on the folder and say open with visual studio code let's do this this is going to open this folder in visual studio code and i am going to trust this folder here be careful what you trust because some things are going to do some malicious things on your computer make sure you trust the folders that you open up so i am going to trust myself because i am the creator of this folder here and this is going to open visual studio code now in our install video we installed the c plus plus extension for visual studio code again if you don't have this installed please make sure you go back and install and make sure this is saying that this is enabled and now we want to use this to write c plus plus applications here we're going to write a simple toy c plus plus application to just test that our compilers support c plus plus 20 and then we're going to use this as a starting point or as a template project so i want you to click on this little plus icon here and we're going to create a file called main.cpp and once you do that you will be able to type c plus plus code in this year so we are just starting out here i would recommend to type this exactly as i do here so we're going to say include and say opening angle bracket and say io stream and we're going to go down here and say and main and we're going to go inside these curly braces and say auto result and you are going to exactly type what i tap here don't worry we're going to have a chance to learn about all these things here so i am going to put a pair of parentheses and i am going to say 10. and i am going to put a weird operator here this is called the spaceship operator it is just something we can use to test that our compiler supports it plus plus 20 and we're going to put a 20 here and then we're going to try and see if this is greater than zero and we're going to print this out we're going to say stdc out again don't worry if you don't know what this is this is just a way for us to test and see if our compiler is going to be able to use c plus plus 20. we're going to say result here and this is going to be our program now we want to make sure we compile this program so to bring in your compilers in visual studio code all you have to do is come to the terminal menu here so please come here and we're going to have an option to configure tasks i want you to click on this and once you do this you're going to see that visual studio code is going to find the compilers that we just installed you see that it has found the gcc compiler we installed in mingw you see that it has found the clan compiler i know about the g plus plus compiler because you can see the path here you can see c major w ben g plus plus dot exe this is where we installed our g plus plus compiler and i know about the clank compiler because that's what's being found here for example on this entry here you see it says cmajw bin clan plus plus so we wanted to configure these two compilers here and later visual studio could use them to build our c plus plus 20 projects now what we're going to do is click on the g plus plus compiler first and when we do that this is going to create a tasks that json file in our project and you see that it has created a folder called dot vs code if i click on my main cpp file here and do reveal in explorer this is going to open up on my system and you see that this has created a dot vs code folder and if we go and we're going to find this tasks.json file this is going to be our configuration to configure which compilers that visual studio code is using to compile our main.cpp file here so if we go in it's going to really have everything ready for us so you're going to see that it's going to be using our g plus plus compiler this is the path to it and in our args options here we have the options that we pass to the compiler to compile our program here now i want to change this to say which kind of compiler we are using so we're going to change the level here to say world width g plus plus and say the version i think that's going to be more descriptive here so we're going to save world with g plus plus or gcc and we're going to say 11.2.0 because that's our version here and the rest is really going to be enough so let's try and see if we can compile our program here and see the thing here printing out but the other thing we need to do because this program is using c plus plus 20 we need to configure our compiler to use c plus plus 20. and one way to do that in gcc and you can really search these things for example if we come to our search engine here and say configure gcc to use c plus plus 20 or even gcc and say c plus plus 20. you can search these things and find information and it is a really good way to test your things here so let's see if we can find a link to help out we have a stack overflow link here so we can crack this open and if we go and we're going to find a batch of things for example we can say std c plus plus 20 here as an option to our compiler and this is going to bring c plus plus 20 support in our gcc compiler so i am going to copy this and i am going to come back in my visual studio code editor and i am going to pass a flag to boiled in c plus 20 mode and it is something i just got from stack overflow here and we are going to pass this to our compiler and put a comma to make this a valid configuration file and i am going to close this now once we have this open and we want to build this we need to bring a terminal window so we we're going to bring the terminal window so that we can see things happening and we do that by coming to terminal and saying the new terminal and we will come back to terminal again and say that we want to run a task in this case we're going to be running our world with gcc 1120 again if you are using a newer version that's what you should use and if we click on this thing please make sure you have the main cpp file selected before you do this so i have this selected and i'm going to go to run task and i am going to click on this little thing you're going to see that this is going to kick off the process to build our program and we're going to wait for this to finish and you're going to see that it is using c plus plus 20 here we're going to see that the world finished successfully and we have a main.exe file created here so if we go in our terminal in visual studio code and click enter and type clear and hit enter this is going to clear the screen this is something you can do to clear things out we can run this main.exe binary directly here so we're going to run exe here and if you run it it's going to say zero and this is an indication that we have our visual studio code editor connected up to our gcc compiler and we can use that to compile c plus plus applications and this is really the configuration with it now you may ask how do i know this how do i did i know the steps to follow to get this to work well if you come to extension tab here and before we even do that i want to bring to your attention that we have a bunch of tabs here in visual studio code we have a tab that says file i think the one here saying explorer we have a one we can use for version control if you use something like git or whatever you can use this but in this course we're mostly going to be using the explorer tab here and the extensions tab to install things and at a later time i will show you how to use the debug tab here or run a debug tab but in most cases we're going to be using the explorer tab or the extensions tab here if you want you can click on this to close this so that you have some more space you can click again to bring this back these are some things you can do in visual studio code but i am going to show you how to configure your compilers where i got the information so click on extensions here and we're going to find our cnc plus plus extension if we scroll down we're going to find tutorials on how to set up this on different compilers for example you can crack this open to see how to set up a visual studio code to use your compiler from microsoft if we come back we can see a link to see how to use this with mingw on windows let's click on this and let's see what else we have we can come back to here and we can see how to use this with gcc on linux or clango on a mac you can really choose whatever is relevant for your operating system but in this case here we are just using the windows operating system so we're going to see how to use this with the mingw compiler that we just installed now we can use the mingw compiler and i would recommend reading up on this entire thing to have an idea on what is happening we already have our g plus plus compiler so this is this command is going to work again if we come back to terminal here and run g plus plus version i think we can see that we run these commands here to see these things working so we have this step here and if you wonder what gdb is it is just a debugger you can use to debug your programs and again if we come to our terminal here and say gdb version we're going to have this available from our mingw installation you can see that we have this here so this is working just fine you can really follow this and configure your visual studio code installation but i am just showing you how to do this pretty fast you can see that this is what we set up in our configuration file which is our tasks dot json file now one thing you can do is modify the task.json file to build every single file in your project and this is something that is going to make things easier what i'm going to do is go back to our visual studio code instance and find our project we're going to crack open our tasks that json file which is living in our dot vs code folder and i am going to take out the craziness i have in my arguments here i am going to select everything i am going to delete that and i am going to put in a configuration that i am using for my projects here to make this super easy what this is doing it is telling the compiler to build in c plus plus 20 mode it is going to tell visual studio it is going to tell the compiler to build every single cpp file in our project so if we have multiple files they are going to be built together and we're going to specify the name for our output file for example if we don't want our binary executable to be named mainly.exe we can give it a name and i am going to be naming mine rooster.exe in the course here i would recommend doing the same so that we can really have the same habits as we go through the course here but of course you are free to use whatever you want in your c plus plus programs here so once you have this you can click save here and this is going to save our json file here i can close this and i am going to bring my terminal by going to view and the terminal here this is going to bring back my terminal that i had before and i am going to run the clear command and hit enter and i am going to do dir to show the contents of my folder on windows here i wanted to remove the main.exe file from the terminal here so what i am going to do is say rm and i am going to say the name of the file i want to remove i am going to say main dot exe and if i hit enter notice what is going to happen the file is going to go away i don't see it anymore here i want to build again using my new configurations in my tasks the json file i am going to run the task to build with our gcc compiler this is going to build again and you see that it is using the configurations that i passed you see that it is building every single cpp file in the directory here and it is going to generate a program called rooster.exe and we if we look here we're going to find it we can hit enter in the terminal and we can type clear to clear things out and if we do the ir again we're going to see that we have rooster.exe and we can run it by doing rooster.exe here and this is going to do what we want this is really all you need to do to connect visual studio code to your gcc compiler now we're going to connect visual studio code to our clan compiler that we have installed we really want to be able to use it with our project here and we're going to go through the seven steps again so we're going to go to terminal and say configure tasks and we're going to choose something that says the clanger compiler and we're going to find clang plus plus so let's hunt for that i have something that says that here we have cloneplusplus.exe i am going to click on that and if i do this this is going to add an entry in my tasks.json file so watch out for this i am going to click on this and let's crack this test that json file open and you're going to see that this is going to also set up another configuration to world using my clang plus plus compiler and again i can edit this out a little bit to say what we want let's look at the version of cloud we have installed to say that out this is going to be 12 0 1 so we're going to say world with clank plus plus 12 0 1 let's say that here in our level 12.0.1 we can say that right here and you see that it is really doing the same thing we did with our gcc compiler so what i can really do is still everything i did in my gcc compiler i can copy this and i am going to take out the arguments i have in my args here and i use what i have in my gcc compiler you can see that you can use the client compiler and the gcc compiler in the same way and this is something really cool so now we have the option to use two compilers and this is really cool so we're going to close the json file and it's going to be saved automatically and we're going to bring up our terminal window again and let's bring this up a little bit and we're going to do dir on windows and we're going to remove the binary again we're going to remove roster.exe and again what you can do you can start typing the name of the file you want to remove and hit tab if you do that the terminal is going to autocomplete this for you and this is really cool so if i do this the file is going to go away you see that it is going right here and i can clear and what i am going to do is world with the clan compiler can do that by coming to terminal i am going to say that i want to run a task and i am going to be given an option i can either build with gcc so if i choose gcc here it's going to be used but i can also use clan let's use clang here so i'm going to click on this and you're going to see that visual studio code is going to kick off the world process this is going to build my binary executable and notice that it is using c plus plus 20 again the build is going to finish successfully and i will have a binary here and this is going to be all i need to do so if i hit enter and run my binary by tapping the name and hitting tab this is going to auto-complete if i run it it's going to say zero and this is what we expect here if you have some kind of error please try and fix it if you have a problem you can ask me and i will do the best i can to help you out okay now we have our two compilers working we need to configure the compiler from microsoft now the compiler from microsoft is a little bit weird again you can read up on this by coming to your extension documentation and going to using the compiler from microsoft with visual studio code on windows you can read up on all this if you want but i am just going to show you a quick way to get this running so the compiler from microsoft is special in that you have to specify the path to it for example if we open a regular terminal window and say cl.exe to call the compiler we're going to see that the terminal is going to say that the cl.exe compiler is not recognized and the reason is we don't have the proper environment variable settings for the operating system to find this compiler when we call it right here we are able to use g plus plus and clan because we put the environment variables in place if you remember when we installed them we did something like this we came to the start menu and did env we came to environment variables here and we put the path to our compilers in our environment variables here you can see them right here so we don't have the same thing for the compiler from microsoft and that's for a reason because we may have multiple compilers installed from microsoft then if you have multiple compilers it would be really cumbersome to choose which compiler you want the guys at microsoft opted for the option for you the developer to specify which compiler you want to run by coming to the start menu here and typing developer and choose which powershell for the compiler you want to use for example if we want to use the visual studio 2019 compiler we can click on this and then we will have a terminal that can run the c plus plus compiler from microsoft for example if we wait for this to be ready and say cl.exe we're going to see that we're going to have a message saying that this is our compiler we can use it however we want so what we're going to do is set up a terminal like this and use this terminal to start visual studio code and the visual studio code is going to start in the mode where it can find the compiler from microsoft i know this is confusing but please bear with me this is what we need to do to get this to work if we start visual studio code in a mode where it knows about the environment variables for the compiler from microsoft we will have the ability to say cl.exe and we will be able to use that right here but you see that it is not recognized now but we can use the g plus plus compiler if we do this version we're going to see that it's available we can use it we can use clan plus plus and if we do version as well we're going to see that we can use this little guy here we also want the ability to use the compiler from microsoft but to have access to that we will have to close the visual studio code instance we have here and we're going to come back to our project let's see if i can find it here so we're going to go to the location where we have our project and we're going to change to the location of the project here so i can grab the location from my visual studio code project that i just set up i am going to come back to my powershell window that knows about the path to the c plus plus compiler from microsoft and i am going to change it to the location of my project here so i am going to say cd and paste in the path i just copied and if i hit enter this is going to change to my location and i really recommend using powershell if you have access to this because it is a little more convenient to use it supports more commands that you can use in the command prompt but again that's going to be your choice now i am in the location for my project if i do ls or dir you can see that i can see my main cpp file can see the binary we generated earlier but this is not problem so what i can do here is say code and i say a dot and this is going to start visual studio code in the current folder of our project year so if you hit enter we're going to wait for visual studio code to come up it is going to come up with the project we had in that folder and the special thing about starting visual studio code this way is that it's going to know about the compiler from microsoft if we come back to our terminal here and clear and say cl.exe you're going to see that now it knows about the compiler from microsoft and we can use it to do things so let's set this up again if you want you can read up on how to do this on the documentation from visual studio code itself but again i am going to show you how to do this you can see that it is really the same things they are doing here so let's come back to visual studio code and we're going to go to terminal and we want to configure a task now you see that we have the compiler from microsoft available you can see that we can use that right here so what i am going to do is click on cl.exe world active file and this is going to add a new entry in our configuration file and it is going to open up if we go here we are going to find that we have a command to build using the compiler from microsoft so let's change the level here and i say world with msvc i think this is going to be more descriptive and this is really going to build your thing now i am going to change this up so that it works everything in the current folder and again i am going to remove everything here and put in a configuration that i have ready on my system here this is what i use for all my projects again this is just calling the compiler and giving it options to do things we just need to specify that we want to use the lattice standard of the c plus plus standard and it is going to specify the input files we're going to be building everything in the current folder this is what we are saying here and the output file is going to be called rooster.exe if we save this by saying ctrl s or command s and close our tasks dot json file we can bring up our terminal window by going to view and saying the terminal here and we can go down and clear we can say clear and hit enter and if we do rm and say rooster and hit tab this is going to remove the binary we generated with the clang llvm compiler earlier now we want to build with the compiler from microsoft so let's do this we're going to say terminal and we're going to say that we want to run a task but this time we want to build with msvc this is what we want to do here if we click on this little thing we're going to kick off the process to build with the compiler from microsoft and we're going to wait for the compiler to do its thing and if we wait we're going to see that the world finished successfully and if we look in our folder we're going to see that we have our output file and we can come in our terminal and hit to enter and do clear to clear things up and if we say rooster just like this and hit enter you're going to see that it's going to say zero and this is confirmation that we are able to use our visual studio code editor and be able to use it with all kinds of crazy compilers and this is really cool if you are on windows i recommend setting this up because it's it's going to make it easier to run a c plus plus applications from visual studio code using state-of-the-art compilers like msvc clone and gcc here and this is really cool now we have everything we need to start running c plus plus applications but before i let you go let's show you how you can set this up so that you have an easier time with regards to intellisense and whatever so what we're going to do we're going to go to our view menu and we're going to choose command palette and in this we're going to say c or c plus plus we can say something like this and we're going to choose edit configuration ui this is going to give us an option to configure things and i want you to configure things your new system so i am on a windows system let's see the options we have here i am only going to have the option for win32 but if we go down we can specify the things we need visual studio code configured for for example it's going to give us intellisense or autocomplete and all kinds of greater things using the setting we choose here if we want we can choose the gcc compiler or whatever i am just going to leave in the windows msvc compiler because i am on windows here but know that you can configure these things now if we go down we're going to see that we have the option to configure the c plus plus standard and the compiler path that is going to be used to figure things out we're going to be using the compiler from microsoft here but know that you have the option to choose any other compiler for example we can choose the g plus plus compiler we can choose the clank plus class compiler you can configure these things and if you need to change this please come back here and do these configurations here we're going to leave in this whole intellisense and what we will do is configure visual studio code to use the c plus plus 20 standard so we're going to do that right here and if you set this up you're going to see that visual studio code is going to generate a cpp properties file which is going to store all these configurations here so it is really going to be a configuration for what we choose in our configuration here so make sure you choose what makes sense again i would recommend using c plus plus 20 but if you go on top you can really use any compiler you want i am going to use the compiler from microsoft this is okay i am going to use windows msvc configuration for intellisense here and i am going to close this and we are really ready to start using visual studio code to ride c plus plus application so let's clean this up a little bit we're going to view our terminal and we're going to remove the junk that was generated by the compiler from microsoft i am going to say rm and say rooster and i can remove the binary i can say rm and say rooster and say tab this is going to autocomplete and really remove things i don't need i am going to remove the pdb file i am going to remove rooster you know i am going to remove the main object file so let's say main dot obj and i am going to remove main dot or vc we don't want to remove our cpd file so now we have a project we can use as a starting point to write c and c plus plus applications here so what we will be doing in the next few lectures or the next few chapters is reusing this thing here as a template project so what i can do is uh call this c plus plus 20 template project and let's do this here i think i need to close video studio code otherwise it's going to complain so i am going to call this little guy c plus plus 20 template project and later when we start learning about c plus plus proper we're going to be using this as a starting point and we want to have to reconfigure the compilers our project will be ready to use the compilers that we set up here namely the compiler from microsoft the g plus plus compiler and the clang plus plus compiler and that's going to be really cool this is really all i have to share in this lecture i hope you have visual studio code set up to use all these compilers we are going to stop here in this lecture the next one we're going to start and see how to set up your development environment on linux and we're going to start by installing compilers on ubuntu 2004 go ahead and finish up here and meet me there in this lecture we're going to see how to install c plus plus compilers on linux we are going to be working on an ubuntu 2004 system and we will install a new version of gcc and the latest version i can get my hands on for clang llvm so let's do this okay so here i am on my linux box this is ubuntu 2004 if i do you name and say a we're going to see the information on this and you will have to adapt this for whatever linux distribution you are on and to install gcc it's really simple but the problem is the latest version of gcc at the time of recording this video is gcc 11 and it's not available in the ubuntu packages now for example if you do see do apt get install gcc 11 this is how you get it if you do this you're going to put in your password you're going to see that this is really not available yet right we cannot find this so the best version we can install is gcc 10 which i happen to have installed already on this system so we're going to do gcc 10 that's what we're going to install here and if we do this we're going to see that this is already available you can also install g plus plus done i think you can do that okay so we're going to run the command here and we're also going to see that it's already available here but if you don't have these they are going to be installed and you can use them on your linux system another tool that is necessary on linux is the gdb debugger so we're going to do sudo apt get install gtb right if you install it it's going to be installed but i already have it installed already so now if i do gcc 10 on my system here and say version we're going to see the version printed out we can do g plus plus stand we can see the version here let's do that for c plus plus so we're going to see the version here we can even do gdb version and it is going to show up let's wait for this we're going to see my gdp version here and now i can use these compilers to compile c plus plus code on my linux box here so this completes our steps to install a gcc compiler on linux we're going to look at how we can get a hold of the clang compiler on our linux box here and it is really simple all you have to do is to do apt get install and we're going to install clang 12 because that's the latest version at the time of recording this video if we do this we're going to see that it's going to install i already have it installed here but if you don't have it installed it's going to install on your system and this is going to give you all the tools you need to compile with the clan compiler on this particular ubuntu system these installs are going to be installed in my user bin directory so if i see the there and do analysis to show everything in here and scroll up we're going to find all the versions for clan you see i have multiple versions clang 10 11 12 and we have clan plus plus variance of this i think we do okay we have clank plus plus and clan here and this is going to do what we want we have the clan combiner and the gcc compiler installed on our system here now if you want to get the latest version of gcc installed on your system one easy way i found is to go through a package manager called homebrew let's search for this right if you search for this you're going to find it and if you want you can install it on your linux system and when you install it it's going to to be basically there ready to use and to install it i think there's a command there install home brew let's do this install here and if we do install we should find a link to install this let's find a link i think they have a link you can copy aha it is this little thing here you can copy it paste it in your terminal and it is going to install home brew on your system and when you have home brew installed you can go through it to install the latest version of gcc for example if we search for homo blue gcc in our search engine here we're going to find all the information to install this through home brew and you just have to do bro install gcc and this is going to install this on your system and you're going to see that it is going to install the latest version at the time of recording this video so this is what you should do for example if we go back to our linux box we can do let's cd into our home directory and we're going to say brew install gcc and it's going to tell me that it is already installed because i installed this but if you don't have it installed it's going to install it and you do this after you have gone through the steps to install the home brew package manager on your system and you do that by just copying this link and pasting that in your terminal and hitting enter and it's going to give you directions on what you can do to get this properly installed on your system here so this is really cool so if you want the absolute latest version in gcc this is one way you have to get a hold of it without really going through the complex steps to build the compiler yourself on linux okay it is taking some more time here i am just going to kill this because i already have the latest version of gcc here if i do jcc 11 on linux and the do version i am going to see that i have 11 2 and it was installed through homebrew and if you let this run to completion it's going to install this on your computer we don't have to wait for this so now we have the latest version of gcc on an ubuntu system we also have the latest version of clan this completes our tutorial on how to set up compilers on linux we are going to stop here in this lecture this one we're going to install visual studio code on the linux system and then in the next one we're going to link visual studio code to these compilers and be able to use visual studio code type code and compile our programs using these two compilers here so go ahead and finish up here and meet me in the next lecture in this lecture we're going to see how to install the visual studio code editor on a linux system and again we will be working on our ubuntu 2004 system here so let's head over to the website for visual studio code and download our binary and install it on our linux system here okay so here we are at the website for visual studio code you can get there by searching for visual studio code in your search engine and you're going to land here and all we really have to do is go down to find our download links we're going to see that for debian and ubuntu we can download here but you can also find something specific to other distribution okay so we are going to grab the 64-bit version of debian ubuntu here because that's my system and you're going to find whatever makes sense for your distribution so let's grab this here we're going to grab the link so if you click on this this is going to kick off the download process and you will see this downloaded on your system and once you have this downloaded you're going to install it and in ubuntu you can just double click on this to install it or you can install it like you install any debian package on a linux system for example if you are unsure on how to install debian packages all you have to do is do a simple search so let's do google.com and go there and we're going to see how to install to install debian packages on ubuntu 2004 so this is our version here and you're going to find a lot of instructions on how to do this it's going to be really simple so we can go down and find a way to install so all you have to do is says you do dpkga say i and you pass the name of the package and this is going to install visual studio code on your system once you do this you will have visual studio code installed and you will be ready to start it and use it on your system here once it is installed you will have the ability to come in your system here for example and say code dot and this is going to open a visual studio code instance on your system let's wait for this to come up so that you really see this okay so this is going to open visual studio coordinate system and you can start using it like we will do in the next lecture okay once you have a visual studio code installed and started up on your linux system the very first thing you need to do is to install the extension that is going to allow you to do some c and c plus plus and to do that you will come to this tab here that says extension and in our search box you can start typing c and c plus plus and you are going to have an entry that says microsoft here so please select this and this is going to be the extension you need to install you're going to click on install and it is going to install and this is what enables a visual studio code to do some c plus plus programming and this is really cool if you want you can check out how to use it on linux and you're going to have instructions on how to do this but we will do this in the next lecture and show you how to configure visual studio code to use the compilers that we have installed in a previous lecture so we're going to tell visual studio code to use our client compiler we're going to tell it to use our gcc compiler and we will have the flexibility to come to terminal and choose which compiler we want to use and we're going to do all this in the next lecture so go ahead and finish up here and meet me there in this lecture we're going to configure our visual studio code editor to use the compilers that we just installed in a few previous lectures so we will be able to type our code in our visual studio code editor just like we do here and we will pass the code through the compiler to generate a binary that can in turn run on our linux system here this is what we want to do so we want visual studio code to use the compilers that we installed so let's head over to our linux box and do this okay here i am on my linux box this is the current directory where i am at this is where i am going to do my things here so i am going to create a folder and call it a template project let's do this we're going to say mkdir and we're going to say c plus plus 20 template project i think this is going to do or we can even say cpp because i'm not sure the plus signs here are not going to give us a problem let's play it on the safe side and we are going to create this folder and we're going to cd into this folder okay once we are in this folder we're going to open this folder in visual studio code and the easiest way to do that once you have visual studio code installed is just to type code and type dot and you will basically be telling the operating system to open visual studio code in the current folder here so i am going to hit enter and this is going to kick it open and i trust this folder because i just created it and we are in our project here the very first thing we're going to do is to create a main cpp file so let's do that and i did that by clicking on the plus icon here and i typed in the name of the file and i am going to put in some dummy code here that we want to use our starter code we're going to say iostream and we're going to put in our code here we're going to say main and again you don't have to type all this if you want you can get the code from the resource section on the lecture so you can copy the code paste that in here and be on your merry way because we haven't really started learning about c plus plus we are setting up the environment so don't stress yourself here this is just some simple code to test our compilers to see if they support c plus plus 20 because we want to use the letter standard we can get our hands on here so let's do equals type exactly what i typed here so we're going to say 10 and we're going to put this weird operator and say 20 and we want to see if this madness here is greater than zero this is what we want to do and we're going to say stdc out and the same result okay so type exactly what i type here and as we progress in the course we're going to make sense of all these madness we see here so now that we have a cpp file containing the code for our c plus plus program we want to compile this and we need to tell visual studio code which compiler it's going to use to compile this program here to give us a binary that we can run so what we're going to do is come to terminal here and we're going to say that we want to configure tasks and the task is really a set of instructions you give to visual studio code on how to take your c plus plus project and turn that into a binary that can run on that system here so that's what we want to do we want to tell visual studio code which compiler it's going to use we're going to configure task here and you're going to see that it's going to discover many compilers on my system here you see we have clan 10 clang 11 clan 12 and we should also have gcc 11 if we keep scrolling down we're going to find these little guys here i have many compilers on this system so i am going to choose the gcc compiler which is installed through home brew this is what i'm going to choose here you say it's saying g plus plus 11 but if you have access to g plus plus 10 you can also use that it's going to work with what we want to do in this course here mostly so i am going to choose the best i have in terms of gcc and this is going to set up an entry in our tasks dot json file to build and run this program here and that's what we're going to do okay you see it is using this particular compiler and one thing we can do before we try and build this we can change the level here to make it a little bit more descriptive so we're going to save world with gcc 11.2.0 if i am correct and this is all we can save this and we can close this json file and if we bring up a terminal window we can do that by hitting terminal here and choosing a new terminal this is going to open up the current location for example if we do pwd in this terminal here let's use lowercase i think that's what it wants we're going to see the location here this is my project so to build i can come to terminal and i choose run task and i am going to choose the task to compile with my gcc installation here if i choose this it is going to kick off the weld process and you're going to see that the gibberish i have here is not supported because the compiler is not configured to use the c plus plus 20. so all we really need to do is go back to our command and tell it to use c plus plus 20. and we can do that by passing a specific instruction to the compiler to use c plus plus 20 like we do on line 10 here so we're going to say std equals c plus plus 20. we are going to save this a little bit and i really wanted you to see this problem here because many people are going to forget this and we're going to try and build again making sure we are selecting the main cpp file here so let's go to terminal run task and use our compiler and wait for it to do its thing you're going to see that the world now finished successfully and if we come back to our terminal here and do ls we're going to see that we have a file called main and if we run it we should see a zero so let's say main here and if we run it we're going to see a zero meaning that our program is running and working correctly we are able to compile a c plus plus project on a linux system here now if you want to see how i came up with the information to use the tax json file please come back to the extensions tab here and choose the c and c plus plus extension we're going to click on this and we're going to see all the instructions on how to use this guy on the linux system here so gcc on linux we can choose this and we're going to see all the information here now you can read up on all this if you want but what i am going to do is go to visual studio code and show you exactly what you need to do to get this to work what i want to do in there is set this up to compile everything in my what i want to do is to set what i really want to do is to what i really wanted to do is to set this up so that it compiles every single cpp file in my directory here and i can do that by putting in a set of instructions i had prepared beforehand so we're going to tell it to compile in c plus plus 20 mode we're going to tell it to compile every single cpp file in this directory and then we're going to specify the output file name for our binary now this is code i used on windows i need to adapt it to be specific to linux so let's see what they say in the documentation there and we can we can scroll down a little bit and we're going to see that they use backslashes here so that's what we're going to use in visual video code to play it safe so we can say this a backslash and this is a backslash and we will try and build this and we're going to see what happens here this is going to build our project in c plus plus 20 mode we're going to be building every single cpp file in the project and the output will be called rooster this is what we want to do here let's save this up and i am going to close this json file and i will bring up my terminal by coming to view and save a terminal here and i can remove the main.exe file or the main executable i had in there if i do lsuc it's gone but now if we try and compile with the gcc compiler we're going to see what happens you see the world is good and we have a binary called rooster and we can run it here if we do rooster we're going to see it run and give us a zero and this is really all we need to do now we have this working for our gcc compiler but we wanted the same for a clanger compiler and we want the flexibility to be able to switch compilers by coming here and just selecting the right task so let's do this we're going to configure clan we're going to come to terminal we want to configure a task and we're going to choose the best we have in terms of clang that happens to be clang plus plus 12 here so we're going to choose this and this is going to add an entry in our tasks.json file we're going to change this to say that we want to build with clan let's do that we're going to say world with clang i think we have 12.0.1 if i remember correctly but we can ask our terminal here let's go to terminal and we're going to say clang plus plus and 12 that's the version and we're going to ask it what's your version it's going to tell us in detail and if we wait we're going to see this speed out on the terminal here and this happens to be clank version 1200 so that's what we're going to say in our file here weld with clan 12 0 0 to be super clear on this and uh this is really all all we need to do is put in the instructions to build in c plus plus 20 mod exactly what we did for gcc here so i am going to copy this and put that in my arguments here i'm i am going to take out whatever i had in there i am going to put in my new things and let's see if this compiler works now we're going to save our tasks.json file and come back to the cpp file we're going to bring up our terminal window and we're going to clear and remove rooster because we wanted to build a new one with our clank compiler and we will come to terminal run task now we're going to choose to build with clang 12.0 we're going to fix this in a minute but if we do this we're going to see that our weld process is going to start it's going to be building with our clan compiler it's going to be using c plus plus 20. it's going to be building every single cpp file in this location here and the output of the world will be a binary called rooster that we have right here so the build is good if we do ls we're going to find rooster and if we run it it's going to say zero exactly as we expect here and we have visual studio code properly configured to use our two compilers the client compiler and the gcc compiler let's come back to our configuration and change this to say 12.0.0 to be super clear on the version we have we can close our json file here and come and try to build again if we do terminal and run task we will have our compiler here ignore all these things i think visual video code is going to give you duplicates but you can choose the version that says what you want and it is going to work so we're going to choose gcc oh we are saying gcc but we want clang here so let's say that we want to build with clan we're going to go to terminal and run task and we want clang 12.0.0 this is right here so we're going to select it and it's going to build successfully and we will have our binary here if we run it it is going to say zero and this is really what we need to do here so this is really all i had to share in this lecture showing you how you can configure visual studio code to compile using the compilers that we have installed another thing you can do to make your experience better is come back to view and select command palette here and you can do c and the c plus and edit the configurations through a user interface and if we do this you're going to see that this is going to create a cpp properties json file so this is where we can configure what kind of intelligence what kind of what kind of compiler is going to be used we can do all these things here so we're going to leave it to the default it's on our linux system it's going to be using gcc 11 which is the best we have in terms of gcc but uh one thing you should note in this course here is the c plus plus standard we wanted to use so we wanted to use z plus plus 20 and we want visual studio code to give us intellisense or format our main cpp file based on the c plus plus 20 standard year that's what we can achieve with this so once you have this and you can come and close this and if we go back to the main cpp file you see now that this is no longer flagged as a problem with squiggly lines visual studio code is properly understanding what is happening here and this is something you can do to make your experience a little bit better okay now that we have this project set up and ready on linux we're going to be reusing this as a template project every time we start something new on linux so every time we want to create a new project we will grab this entire folder so for example we will come to our terminal here and go up a little bit so cd and go up one level and if we do ls we're going to have our c plus plus 20 template project we will copy it and rename it to start a new project and that's going to give us all the configuration that we did we don't need to reconfigure the compilers or whatever it is going to work right out of the box and that's really what we did here we set up a template project that we will be reusing all over again in the course as we progress learning about c plus plus 20. i am going to stop here in this lecture the next one i am going to show you how you can set up a development environment on the mac os system go ahead and finish up here and meet me there in this video i am going to show you how to install c plus plus compiler on your mac os system and we have a bunch of options on the mac we can install the gcc compiler we can install the clang version of the apple company or we can install the clangle vm version all these are options and i will show you how to install these i don't have access to a mac device so i won't really be able to install xcode and show you but i think you can manage if you are using a mac system it is nothing complicated you just have to go to the apple store and click on xcode and it is going to install for a gcc and clang llvm i will show you how to do this using the homebrew package manager which is also going to make this super easy okay so let's head over to our browser and we have a good link here from digitalocean on how to install and use homobrew on a mac os system this is a really validated link you can see that right here and you can read up on this if you are using a mac system i am going to share the link here in the resources section of the course and to use this we will go through the mac os terminal and it is nothing complicated you can search for it and open it up and after you do this you will have to install the tools needed to build things on the mac os system and that's going to install xcode here so grab the command here and run it in your terminal and this is going to install everything you need and after you have this installed you're going to install home brew i do recommend coming up to the website here for homebrew and clicking on this copy icon here this is going to copy the entire thing and you can copy this and once you copy it it is going to give us a green tick here and you can go in the terminal on your mac os system i am using a linux terminal here and then you're going to paste this command in here and hit enter once you do that you will be prompted to put in the password it's going to tell you that it needs to install homebrew and you are basically going to wait for this to finish i have to warn you this is going to take some time even if you have a good internet connection this is going to take some time so be patient here when it's done it's going to tell you that it is finished and you will be ready to install the gcc compiler and the client llvm compiler through your homebrew installation here i am not going to click on this because i already have this installed on my linux system here let's go back to the link and see what they say next so after you paste that in it's going to install and after that you will see that homebrew is going to be installed on your system and uh you can use home brew to start installing and upgrading packages you can really read up on this but what i want you to see is that you can use home brew to install the gcc compiler and the clang ll vm compiler so if we come to the search here and say gcc homo broom we're going to find instructions on how to install this so all you need to do really is to say brew install gcc this is going to install the latest version of gcc on your mac if we come here and say llvm or clank let's say clan and we can find that right here llvm and it is going to give us instructions on how to install this and all you have to do is brew install llvm and this is going to install these two compilers on your mac system so once you do that you will have them stored somewhere let's go to the install link and we're going to see that homebrew is basically going to be installed into things in your user local directory on a mac device please tell me if this is true for your case and if you have a problem we'll try and go through that and fix that up so if you are on a mac i would really recommend going through this to install homebrew once you have that installed you will go through that to install both of the llvm compiler and the gcc compiler and that's going to give you the latest versions for these compilers to find out if they will installed you can go in your user local directory and you can hunt through these compilers and you're going to find binaries for them and start using them to build your c plus plus applications you okay so by now i am going to consider that you have both gcc and a clang installed on your mac system the next logical thing to do is to want the apple version of clang and i think that's going to come as part of xcode here and it is going to come as a result of running the command that we did before i think it was this little command here which is going to install everything you need from the xcode ide so this is all i have to share in this lecture again i apologize i don't have access to a mac device but if you have a problem you can tell me and i will try to help you out and get this sorted out we are going to stop here in this lecture the next one we're going to see how we can install xcode on your mac device go ahead and finish up here and meet me there in this video i am going to show you how to install the visual studio code editor on your mac os system please know that you can also use other ides like x code or code lite do c plus plus development but visual studio code is going to be our main editor in this course here and i would recommend to install it so that we are doing consistent things that's going to reduce chances for you to get confused so we are going to head over to the website for visual studio code and to get there we can just type visual studio code in here on in our search engine and we will have a link here that we can use to download our fans so we are going to go down and find download links for windows linux and mac please use the mac download button here and you're going to download the binary and install it i think it is going to be in the download folder by default so all you have to do is drag that up in your installation directory so that it becomes a runnable application after you do that you're going to run it and it is going to run and you will have a user interface like this with visual studio code opened up once you have that up you will need to install the c plus plus extension and what you need to do is come to the extensions tab here and type c and c plus plus in here and you're going to have an entry for microsoft click on that and install this extension on your system and your visual studio code instance is going to be ready to handle some c plus plus development and this is really all i had to share in this lecture showing you how you can get this installed again i apologize i don't have access to a mac device so i used my linux box running inside window subsystem for linux to show you this but this is good enough i think if you have a problem you can tell me and i will do the best i can to help you out for now we're going to stop here in this lecture in the next one i will try and show you how to set up a visual studio code to use the compilers that we installed in the last lecture go ahead and finish up here and meet me there in this lecture i am going to try and show you how to configure visual studio code to use the compilers that we installed on our mac system in a previous lecture so we want visual studio code to be able to pick up our gcc compiler and use it we want it to pick up our client compiler and use it we also wanted to pick up our clanger compiler from apple and use it to build binaries that we can then run on our system here so let's see how to do this i have to say i don't own a mac device so the best i can do here is describe the process to you and if you have a problem you can tell me i will do the best i can to help you out okay so once you have visual studio code installed you're going to create a folder somewhere and open visual studio code from that folder and what you will basically do is use the terminal to navigate to a location you're going to create a folder you can even call it cpp template project you're going to hop over into that folder and then open visual studio code from that folder here so for example we can hop over into cpp and run code and say dot that's going to open visual studio code in that folder and from that folder you will create a main cpp file by clicking on the plus button here and you will create the cpp file call it main cpp and you can put in the content here and once you have this the next task is going to be to set up a compiler to build this little thing so once you have this project you will come to terminal i think there's going to be a terminal menu somewhere and you will choose configure tasks once you do that visual studio code is smart enough to scan your system and find any compiler you have installed the first thing that is likely to be found is the apple client compiler so you're going to find a clone plus plus compiler so you can choose that and you can use that for the apple client compiler we are lucky we even have a tutorial on how to do that so if we click on our extensions tab here and choose our c and c plus plus extension if we scroll down we're going to see a link on using the client on mac os here this is going to give us all the instructions we need to use visual studio code with the client compiler from the apple company so if you scroll down you're going to be able to read up on all this but what i really want you to see is the configuration file you're going to see that it's going to be using the clang compiler from the user bin directory and this is mostly going to be the clang version from the apple company and sometimes you don't want this so if you want to use the clang version that we installed through home brew i think you will need to go through the user local directory because that's where homebrew is going to put it you're going to see that that's going to be the location here from our tutorial and you're going to hunt down for the clan compiler and find your binary and that's what we're going to be using next but for the first time if you want to use the clan compiler from apple this is the configuration you need and all you need to do in this case is change this to c plus plus 20. now once you have this configured you will have the ability to come to terminal let's show our files here you will have the ability to come to terminal and run task and choose the compiler that's going to be the clang compiler from apple but you can also configure other compilers and all you really have to do is choose them in the list here and you're going to have a configuration file it tasks json file to be exact let's show that up here and that's going to give you the instructions you need to build this and in most cases you will just need to be careful about the path to the compiler so for example if you are using the gcc version you installed through homo brew you're going to find that in your location you're going to make sure that's the correct one and you will put in these instructions here to tell your compiler to build every single cpp file in the directory here this is what we do if you use clan you're going to have something like this and again i am describing this to you because i don't have a mac available to me right now and if you have a problem you can tell me we can try and sort this out okay and by this time you will have the ability to use your visual studio code instance and compile c plus plus programs using either the apple client compiler the llvm glango compiler or the gcc compiler and all these are going to be compiling in c plus plus 20 mode again to both you need to make sure you have the file selected and choose whatever compiler you want to use for example if you want to use clone 12 from homebrew you're going to you're going to select that and it is going to be used to build your project and you will have the ability to run your projects just like we do here and it is going to work let's make sure we are running rooster here our binary and it is going to run and printer 0 and this is going to work again i am really sorry i don't have a mac device available to me but this is what i have to work with now if you have a problem on a mac device please tell me i will do the best i can to help you out in this lecture we're going to explore the option to use online compilers if for whatever reason we can't install a radio compiler on our system and again the goal is to be able to turn the code we type into what looks like an ide into a compiler and get a binary that we can run and see the output of there are a few online compilers that are very popular within the c plus plus community one box is one of them there is also compiler explorer and kodiro but there may be other ones out there if you do research on your own we're going to go to the browser and actually check each of these out okay here we are in our browser here i am on onebox.org and you see that it really looks like an ide inside we have the code we've been running all along to make sure that it compiles with c plus plus 20. on the left here we have a few options we can select which compiler we want to use you see that there is a couple of gcc versions you can see that this is a really nice way to test things across gcc versions you see we have clan 12 so if you want to test clang out you can select it let's actually try this so we're going to use clang head and try to compile this so let's hit enter and see if clank 12 supports c plus plus 20 and you see that it really works clan version 12 should support c plus plus 20 like this let's go back to gcc because that's what i like and you can try putting different things in your c plus plus code to see if the compiler actually responds for example we can say stdcl hello there stdendl and if we run we're going to see the message output here another good thing with these online compilers is that you can share this code and anybody will be able to look at the code here and run it so this is a really good way to get help from your instructor so i do encourage you if you run into a problem come and use one of these compilers and send me the link the way you can send the link you can hit on share here this button here you're going to get the url that you can copy and send to anybody and they will be able to see your code just like we're going to see here in a minute okay you see that it isn't the exact same thing and anybody on the internet will be able to see your code and try to find whatever the problem is and help you out very fast one box is one compiler you can play with the options that we have here you can load different libraries you see they have a lot of boost libraries but this is of no interest to us right now you can play with it and see what you like about it another one is kaliru which is another online compiler you see it doesn't have a lot of options like we had in one box for example you cannot change the compiler it's going to be using g plus plus but if we hit compile link and run it's going to run our code and we're going to get the output here again we can try and change this so that you know i'm not lying to you so we can say stdcl and say the sky is blue std endl if you want you can hit control plus on your browser and it's going to make this a little bigger and if we compile link and run we should see the sky is blue in the output window here this is really all you can do with kolero and if you like it you can donate to the developers i think we have the same thing on one box but it is in japanese so i can't really read this okay another one is compiler explorer which is a really good one the main purpose of this is really not giving you the output it is actually seeing assembly code that is generated for your c plus plus code this is something a little bit advanced for our purposes here so we're going to ignore this we're just going to be using it to test our code with some of the popular compilers if you look here on this drop box you see that they also have a lot of compilers you can use different gcc compilers i think they have clan they even have msvc how cool is this so you can use a lot of compilers gcc clang you see we have a lot of clan compilers we have clang 11 here so this is a really good way to try things out we're going to come back to gcc 10 2 and i noticed that i passed the flag of c plus 20 to be able to support c plus plus 20. again if i come here in my code and say stdc out the sky is white don't worry about these errors they're going to go away in a minute let's wait for the program to compile you see that it is compiling let's wait a minute the sky is white and everything is fine if you don't see this output here it is possible that you didn't check this checkbox run the compiled output so come to this dropbox and make sure this is checked and you should have a window like i have here compiler explorer also allows you to share your code with people on the internet let me see if i can find the option to share you come to this drop box and you get a link here that you can copy i'm going to copy this and open a private window in my browser here so that we know that it is some other browser that is going to be opening this link and getting access to the same code we have in compiler explorer and this is going to be pretty cool and you see that we get compiler explorer privacy we're going to close this and you see that we have the same code we can try modifying this code for example we can take out this third line here it's going to start compiling you see that it is compiling and it is going to give us the output that we expect from this so this is a really good tool to try out if you want to share your code with some people that are not close to you this is a really good way to do this it is also good to try out different compilers for example if you don't have a gcc compiler you can try it out here and see if it does things for example we didn't install clang on our system but we can try it out and see if it runs or could but make sure you pass the correct flats here because you are responsible for this if you pass the flats that don't work you're going to get arrows for example if we say hello here we're going to get weird arrows and oh and recognize command line option std hello but if we say c plus plus 20 it is going to work so make sure you pass the correct options in this lecture you're going to write your first c plus 20 program and we're going to start and attempt to understand our program let's hop over to visual studio code and actually do that the first thing we want to do is to set up our code so that we can open that in visual studio code we're going to start from our c plus 20 template project that we set up in the last lecture and i'm going to be doing the projects for this chapter in this folder here so we're going to do our very first c plus plus program so what we're going to do is go inside our c plus plus 20 template project and i noticed that we actually left in the boiled files we don't want so what we're going to do is remove this main file and that this vc 140 fan we don't want these here and we're going to copy the main cpp file and the dot vs code folder we're going to copy these things and we're going to put them in our first c plus plus program folder and we're going to open this folder in visual studio code visual studio code is not opened up you see that i have my developer powershell opened if you want you can open visual studio code directly without going through this if you're not going to be using the visual studio compiler but i want to have the option to use that compiler so this is how i am going to open my visual studio code instance i am going to type code.exe to open this up i'm going to hit enter and visual studio code is going to open and it is going to open whatever we did in the last lecture we don't want this project because this is a template project from the last chapter we're going to close this folder go to file and hit close folder and we're going to open the new folder we want to work on we're going to go to file and hit open folder we're going to go to the location where our project is so you're going to go where yours is i'm going to browse to mine and i'm going to go in the current chapter and choose 3 2 first c plus plus program i'm going to select the folder it's going to open up and you're going to see that our settings from the last template project are still valid here they were copied over if we open the main cpp file it's going to be the main thing we had in the last lecture so we have the option to run task you're going to see that we have two compilers available and this is really good we are ready to start using this let's try and build this with gcc and we're going to have our binary created we can click in the terminal here and if we want to open the terminal again we have the option to come in terminal and new terminal this is going to open this up if we do dir in the terminal we're going to see our program generated we can remove this thing here and say rm rooster dot exe and remove this we can clear and we have a clean project we can start learning from okay now we have the project and it is building successfully let's try to understand the code we have in here the first statement we have it this include io stream thung this is a feature c plus plus offers to load the pro built libraries that we can use iostream is specifically going to help us print thanks to the console we've seen that we can for example print hello world if we want let's take out all these things in here and say std column column c out this is coming from i iostream and we're going to put two less than symbols we're going to open two quotes and inside we're going to type hello world and we're going to go outside the quotes and do two less than signs and say std colon colon endl and put a semicolon this is going to print things on the console if we run this program so if we run this program the first thing we need to do is to build this program so we can come to terminal task and we're going to build it with gcc and after we've built it we can hit enter to close the terminal and we can do dirt to see the content of the folder you see that a program has been generated we can run it and if we run it it's going to say hello world this message is coming from this statement here and we have this statement because we have included io stream to really make this super clear we can try to remove this line that says include io stream and the moment we do that you're going to see that this tab says we have problems and if you open this tab you're going to see the message namespace sdd has no member c out name space std has no member a and dl this is because the z plus plus program doesn't know what sddc out means because we don't have the library included and you also see that we have these squiggly lines to make it really clear that we have a problem so to really be able to use this we have to include a third party library that brings these features into our program and that's what the include statement here is really doing so let's bring this in and we're going to say i o stream again and let's wait and see that the problem goes away and the one thing you should really know is that we don't have a semicolon at the end of the io stream statement we don't need to put it there but many c plus plus statements need a semicolon at the end okay i think this really makes it clear what the include statement here is doing the other thing we have in our program is this ant main parenthesis curly braces thing this thing here is called the main function a function in c plus plus is a block of code that does a lot of things but the main function here is special because it is the starting point of your c plus plus program we also call the main function the entry point of the c plus plus program and what that means is that if you open the program the main function is going to be the very first thing that is going to run and the things are going to be executed in the main function in order so the statement on the top is going to run first and the statement after that until we hit the end of the function so this is what we mean here to really drive this home we can come here in our main function and say sdd number one and say sdd engl and by the way this e n d l statement is going to print a new line we're going to see it in effect in a moment let's actually take it out so that we can see its effect we're going to also do sddc out number two and what this is going to do is to print number one and number two on the terminal let's build this program and if you want to build the default task you can hit ctrl shift b for it to happen very fast but i am always going to come here so that it is really clear what i am doing a program should be generated now if we try to run it by typing rooster.exe and again we can hit the app arrow button on our keyboard so that's the last command that we run from this terminal runs and this is going to speed things up a little bit so if we run this program it should say number one and number two as you see here but these two things are cramped on one line and the reason is we are not printing new line characters after we print number one and number two and we can solve this problem by putting in two less than symbols and saying std column column e n d l this is going to put a new line character after number one we can build again for our new changes to tech effect after we do that we click on the terminal hit enter we hit the app arrow button if we run it's going to say number one and number two on two separate lines because we are printing a new line character after number one i really hope this makes sense please do take some time to play with us because if this is the first time you're doing this some things might not be clear and the best way to really make sense of this is to type code run it and see the effect of what you type by running your program so let's do this a couple of times we're going to do sdd endl after the statement for number two we're going to do stdc out number three and we're going to do stdeandl to really show the problem again we're going to take out stdendl on the number two line we are going to weld the build is going to be successful we're going to click in the terminal hit enter hit the app arrow button run the program again and you're going to see it's saying number one it's going on the new line printing number two but we not to jump into the new line from number two so number two and number three are going to be cramped on one line that's what we see here we can solve this again by putting in a new line after number two let's do std e and d l here and uh this should solve the problem again let's build we're going to run our build task the build is going to be successful we're going to hit the app arrow to run the program if we run it it's going to say number one number two and number three and this is really good now it should be clear that statements in the main function are run in order from top to bottom and that the program is going to end when it hits the end of this curly brace that includes our function we're going to have a chance to learn more about functions in detail later in the course but we are just getting started and we need to start somewhere so that's why we're doing this main function thing here another thing you can do is return from the main function and in many examples you will see people do return 0 something like this and this is a way for the main function to send the message to the operating system if it's finished successfully or if there was some kind of problem we're not going to go into any more details about the main function because that's really too much for now we are just getting started we are trying to get a feel of how c plus works the main message here is really that you should be able to print things to the console using sddc out you should be able to print a new line after your message using sdd ndl this stdc out and stdendl thank come from the iostream library that we are including here if we don't include this library we're going to have problems because our surplus plus program is not going to know where they are coming from so we should really put this in if we want to use these features and we can return from a function to let the system know the operating system if the program finished up successfully or if there was some kind of problem and again this project is using the templates we've done before and this is really all we set out to do in this lecture trying to make sense of the main function in c plus plus to really practice these things i would challenge you to write a c plus plus program just like this that is going to print your name 10 times try to do that and if you have a problem please do ask me and i will do the best i can to help you out this is really all we set out to do in this lecture i hope you found it interesting we're going to stop here in this lecture in the next one we're going to start and learn about comments in c plus plus go ahead and finish up here and meet me there in this lecture we're going to learn about comments and c plus plus comments are the way to put some messages in your c plus plus chord that are really meant to be read by humans and one way we have to do comments in c plus plus is through this backslash backslash thing we have here this is going to comment out the line so the compiler is really not going to care about it it's going to think that it is something that is meant for the developer to see so this is a one line comment and this is how you do it in c plus plus you can also comment out a block of code in your c plus bus code and this is something that comes in quite handy many times you put your comment in a backslash star and a star backslash like this this is going to comment things out in your code and the compiler is really not going to care about this let's hop over to visual studio code and actually play with us so here we are in the folder that is going to contain our code the first thing i'm going to do is go through the template project i'm going to copy my files and i'm going to go up again and we're going to be learning about comments so i'm going to put this in the folder on comments i am going to close whatever folder we have opened in visual studio code and i'm going to browse to the location where my project on comment says my project is in this folder that is named three three comments i'm going to open the folder and if i open the main cpp file i'm going to find the starter project we're going to remove this const eval thank because we're really not ready to understand this yet it was in here to help us test that our compilers support c plus plus 20. so we're going to remove this and we're going to put in a message that says hello world you already know how to do that from the last lecture so we're going to say sddc out hello world and we're going to say stdendl and don't forget this semicolon at the end if you do you're going to have problems so make sure your code looks like this try to build it to make sure it's working fine this is going to run the gcc tasked world we can click on the terminal here and it's going to close the terminal and we can open a new terminal here if we want if we do dir we're going to find our rooster program inside we can run it and it's going to say hello world we already know to do this from the last lecture and in this one we are going to be learning about comments the first thing you can do is use slash slash so let's do that and uh let's put some message here this branch and the iostream library this is a nice message that is going to give some more information about this statement to whoever might be reading this program and this is called a one line comment you can really put this wherever you want in your c plus plus file we can go in the main function for example and say this is going to print hello world to the console or the terminal we can really put this anywhere we can even put it after the return statement here and really put in whatever piece of information we think is going to make sense to wherever it's going to be reading our program okay this is how you do a line comment you can also do comments across different lines and the way you do that you put that in the slash star and go down and say star slash like this and whatever is going to be in the middle here is going to be a comment this is usually called a block comment you can put in whatever you want so if you have a piece of text that you would like to use as a comment in your program this is one way you can do this one thing you should know though is that you cannot nest these things but before we do that let's try to build our program to really show that there is no problem if we have these comments in the world is successful and if we click on this terminal and hit enter and hit clear to clear things out in the terminal we can try to run our rooster program and it's going to say hello world you see that all these green lines which are comments are really not affecting the program all the compiler is going to care about is this include thing it's going to care about the main function here it's going to ignore these green things it's going to be like these green lines that are our comments are not in there this is the effect comments have in your program and we're going to be using this a lot in this course one thing you should know though is that you cannot nest these block comments so if you try to do something like this and uh say this is one comment and uh come again and say this is another comment you see that we have problems already nesting is not allowed you're going to get a compiler error and we're going to learn more about these errors in the next lecture but don't do this don't nest these block comments if you do that you're going to get some confusing errors and this is going to be the cause for them so don't nest your blog comments it's going to give you headaches okay this is really all we set out to do in this lecture learning about comments in c plus plus you can do one line comments on a single line like this you can do block comments to comment out a block of text you can't nest your blog comments because that's going to give you a lot of problems you don't want that and really use comments wisely and don't overdo it because it's going to turn your program into a novel and i don't think people want that so use this feature when you need it to give some more information about your code and don't overdo this we are going to stop here in this lecture the next one we're going to learn about different kinds of errors and warnings you can see in your c plus plus program go ahead and finish up here and meet me there in this lecture we're going to learn about errors and warnings in your c plus plus programs there are three kinds of errors or warnings you can get you can get compile time errors you can get runtime errors and you can get warnings but what is really meant by these things remember the ultimate goal of every developer or every c plus plus developer in our case here is to be able to generate a binary executable file from the c plus plus code and we have to run the code through the compiler to be able to do that and one thing you should know is that there is a set of requirements that the compiler enforces on our code for it to be able to compile successfully and those requirements are the rules really that we have to follow in our c plus plus program and if we break those rules we're going to get problems and these problems are going to come in the form of compile time errors run time errors or warnings let's look at compile time errors in detail here we have a simple c plus plus program that has a few problems we have forgotten to put the semicolon at the end of our stdc out statement here and the compiler is going to freak out because of this if we compile this program we're going to get a bunch of errors and the ide is going to show us this problem some ides are going to give you different errors for example here i was using codelite and it was saying error expected semicolon before return or something like this so this is a compiler error and if you have this kind of errors your program is not going to compile compilation is going to fail you're going to get this error and you're not going to get your binary executable and when you have this you basically have to go back and fix this problem compile again and when you get rid of these problems the compilation is going to succeed this is what a compile time error is another thing you can have is the run time error this is not going to cause a compile time error the compilation is going to succeed but when you run the program it's not going to do what you intended it to do in the first place for example you may want it to print green on some user interface it's going to print gray or whatever it's really not going to do what you want so it's a logical error that you have in your program and you have to fix it to get rid of this and another thing you should know is that sometimes these run time errors can cause your program to fail and terminate immediately and we say that the program has crashed we're going to see an example of this in a minute the last kind of problem you can have in your program is a warning this is a problem that is not serious enough for the compiler to halt your compilation compilation is going to succeed but this is basically the compiler telling you you are doing something that has some problems and you should really fix it before it becomes a serious problem so that's going to be a warning for example here you see some compilers can warn you when you divide stuff by zero and we're going to see how this works the main message in this lecture is that you may have problems in your program and they are going to come in three forms some are going to be compile time errors some are going to be run time errors some are going to be warnings we are going to hop over to visual studio code and actually play with this okay here i am in visual studio code the first thing i'm going to do is to set up the template project we're going to be using in our project here the project is going to leave in this folder errors and warnings i am going to copy over the files and i am going to go in the current project we're going to be working on and i am going to open this folder in visual studio code i am going to go to file close folder i am going to close this one and i am going to open our new project i am going to browse to it the project is this one i am going to select it and select folder and it is going to open up in visual studio code i am going to close this welcome screen and open our main cpp file we are going to get rid of this constable fan this is a c plus plus 20 test thing we had and we don't really need it now we're going to get there in due time what we want to see is compile time errors we're going to put in our stdc out statement hello world as we usually do we're going to end this stdndm and we're going to remove the semicolon on purpose and if we try to compile our program like this we're going to get a compile time error this is what we want to see so let's try and build our program we're going to run the task to build with gcc and you see that the world fails we're going to get a bunch of errors in our terminal it's going to say in function it main expected a semicolon before return it's going to try its best to tell you what the problem is but sometimes these errors can be really confusing in this case it is really easy because we introduced this error on purpose so we're going to fix it in a minute but before we do that i want you to see these problems tab it is where the problems are going to show up in visual studio code so you should see your problems in here or through the terminal here okay one thing you should know is that when you have a compiler arrow compilation is not going to succeed and you're not going to have your binary file which is what you really want so to fix this problem we're going to put in our semicolon here we're going to build again we're going to go to terminal run task we're going to build with gcc and this time you're going to see that compilation is going to succeed and we have our program here we can run it through the terminal here so we're going to click the terminal is going to go away but we can open our own and if we do dur we're going to see our files in here and we can run our program we can say rooster and if we run we're going to have hello world so i hope this really makes it very clear what a compile time error as so this is one kind of error you can get the other one is around time error and this comes in the form of things that don't work as you really expected so let's say we are trying to do some computation in our program we try to take 7 and divide this by 0 for example and this is a statement there is a load in c plus plus so let's try and build this and see what we get because this is a common scenario where we have run time errors we're going to run the task to build with gcc okay if we do this you see that world finished with warnings we didn't get a compile time arrow and our build was successful so to really prove this i'm going to go back in our directory i'm going to do dur you're going to see that rooster is there i'm going to remove it i'm going to say rooster.exe this is how we remove stuff from the command line when i do this and do dirt again the executable file should be gone so let's try and build with this statement and and you're going to see that we get a warning and not a compile time error like we had here so we're going to go to terminal run task we're going to build with gcc and you're going to see that we're going to have our executable here but we had a warning and it's saying that division by 0 was detected in our program and it is a recipe for disaster so why is it a disaster let's try and run this program so we're going to dirt to prove it's here and we're going to run it if we run it it's going to say hello world and it's going to end let's try and actually use our own terminal to see if we have a better chance of seeing the problem here so i'm going to reveal in explorer by clicking on main cpp and i am going to try and open a terminal window here powershell that we open ourselves we've done this before this shouldn't be new to you by now so we're going to do dirt in here and we're going to try and run the program and it's going to run so so it's not really giving us a run time error here because we're not capturing this or trying to use the result of this division here we're just doing the division which is a problem so let's try and capture this we haven't learned about variables yet but what we're going to do now is do end value and we're going to assign the result of this division to this value and let's try printing the result of dividing a number by zero so we're going to do stdc out and we're going to say value and this is how you can print a value out on the console okay now that we have this and let's try and weld we're going to run tasks and worldwide gcc we're going to get the same warning but if we try to run the program we're not going to get what we expect let's run rooster exe we expect it to say hello world and printed the value but you see that it's really struggling to print the value and it gave up and ended immediately so this is one example of a runtime error that you can face in your program there are many other kinds that we're going to see as we progress in the course here we are just trying to raise your awareness on these problems so that you can know how to fix them when they come up okay so this is all i can share with you now about runtime errors we also had a chance to see that this is also going to give you a warning and a visual studio code here is also helping out telling us that we may have a problem it's turning the main cpp file yellow and it's saying that there is one problem in this file and if you go here you're going to see that we have a warning here okay so this is one way you have to see this so this is not recommended to do so we're going to comment this out okay this is really all i had to share in this lecture we had a chance to look at compile time errors we also had a chance to look at warnings and run time errors using this example if you get a warning it is a message the compiler is giving you that you should really look it up and fix the problem before it becomes more serious compile time errors are going to cause your compilation to fail and that's a really good thing because the compiler is going to stop you from generating a binary that doesn't really do what you expect it to do so that's a good thing but sometimes the compiler want to give you a compile arrow and you have a chance to run into a runtime error and that's really bad so be careful about these things and when you have problems visual studio code is going to show you the problem in terminal or in this problems tab and you're going to be able to fix it up we're going to stop here in this lecture in the next one we're going to learn a little more about statements and functions go ahead and finish up here and meet me there in this lecture we're going to learn about a little more about statements and functions in c plus plus a statement is a basic unit of computation in c plus plus you can say that it is the smallest thing your cpu can execute in your program every c plus plus program is a collection of statements so you're going to have a bunch of statements that are organized in a certain way to achieve whatever it is you want and statements in c plus plus must end with the semicolon we saw in the last lecture that not putting that semicolon is going to result in a compile time error here is a basic sample c plus plus program that we're going to use to learn about statements in the main function here we have two variables declared we haven't really learned about variables but they are a way to store data in your program and here we are storing two decimal numbers one is called first number the second is called second number we can do something with these variables that we have in our program for example we can add them to have another variable in our program and we can print that so you see here we are saying stdc out the sum of two numbers is sum so this is going to print 21 because it's going to add 12 and 9 and the result of that is going to be displayed on the terminal if we run this program what i really want you to notice is that this and first the number equals 12 thing is a statement in c plus plus so it must end with a semicolon the line here with second number is also a statement so it must end with a semicolon every single line we have here in the main function is really a statement and it must end with a semicolon if we don't put the semicolon in place we're going to get a compiler error okay one other thing that is really important to keep in mind is that statements are executed in order in your program and the order is from top to bottom if you go back to our program here so the first statement is the one with first number here 12 is going to be installed in memory when this statement is executed by your computer and we're going to go to the second statement with second number and we're going to keep going until we hit the end of the main function and the return statement is going to execute and when the main function returns or terminates we're going to get the return value sent to your operating system and it's going to know if your program ended successfully or with a problem and the operating system is going to know what to do with that information execution keeps going until we hit the end of the program or if there is any other condition causing for the program to terminate and we're going to have more details about these things later i just want you to know that the statements are executed in order in your main function another concept i really want you to have an idea about is the concept of functions a function is really like a machine you give it input and it's going to give you output if we look at the operation here to add first a number to second number we can consider first number and second number as input to the function and sum is the output of the function we can visualize that like this so we have a first number and the second number we pass that into our machine which is really a function and we're going to get the result of this function in a return value or something that we get out of the function as our result c plus plus has a special syntax we use to define functions this is our function to add numbers on the left we have an integer which is code name for a whole number in c plus plus it's a number without decimal points and it's a whole number after that we have the name of the function and we can name it whatever we want and after that we're going to have the list of parameters that we can pass to the function and this is going to work as the input to the function after you do that you're going to have a pair of curly braces the one is here the other is here to the end and within those curly braces it's going to be the body of the function inside the function you see that we are declaring a variable which is called sum and we are storing the sum of first number and second number in this variable and we are making this function return the sum as the result and we're going to be able to capture it if we call this function in a function like me you're going to see how this works in a minute a function must be defined before it's used so you can't really call a function you haven't defined first in your c plus plus program and here is a slightly complete program to really put all these ideas together we have our function to add numbers it takes two numbers first number and second number it's going to add them up and store the results and we're going to return some to be used by whoever called this function and if you look in our main program we have a statement here we're going to store the result of add numbers in the sum variable and we pass first number and second number in the function and we're going to have that result stored in here here we also call the function with direct numbers you see that we pass in 34 and the seven and we're going to print out the result here this is the basic idea i want you to have about functions they are reusable pieces of code that group together a bunch of statements to do whatever it is we wanted to do in that function one benefit about functions is that we can reuse the code here if you look here we are calling this function multiple times without really rewriting these statements that are inside the function i know some of these concepts might be cryptic to you right now we are going to go to visual studio code and try this in code and you're going to see exactly how this works okay here we are in our folder where we are storing our projects i am going to grab the code from the template project i am going to put that in the current project which is statements and functions i'm going to paste that in here and i am going to open this up in visual studio code i am going to close the project we have right now and i am going to open our new project let's browse to its location i am going to open the project on statements and functions let's open this folder in visual studio code and we're going to have our starter project here i am going to take out whatever we have in the main function and take out this contival thing we don't really need it and we are going to try and define two variables using two statements we are going to say and first number and we're going to put in a value let's say three and we have to remember to end this with a semicolon so this makes our statement we can encode this in a comment to make this pretty clear we're going to also define a second variable so we're going to say ent to mean that it is a whole number it doesn't have decimal points like 2.5 it is a whole number like one or seven or ten so we're going to say second number we can give it a name and uh let's put in a seven we're going to store these values in our program and we can print these values out let's print them out first so we're going to say stdc out we already know how to do that and we can say first number and we can change these output statements and say the first number is first number you can do that std endl and you can notice that visual studio code is really helping out in auto completing things we have in our program let's print the second number to really practice this so if we do std you're going to see that it's going to give us a bunch of options we can choose from if you want you can scroll through this with your mouse or you can just type whatever you want to type so i am going to do c out and it's going to try and help out if i really want i can come on c out here and click on it and it's going to be auto completed which is pretty cool so we're going to say second number and we're going to say second number here and it's going to auto complete for us endl and we can try and compile this program before we do i want you to guess what we're going to get when we run this program give it a guess we are then going to go to terminal run task and we're going to build this with gcc the world is going to be successful we're going to open this in our terminal the terminal goes away no problem we're going to open it again and we are going to go in our folder and do the ir and you're going to see that we have rooster.exe our program we can run it and it's going to say first number three and second number s7 this is what we have stored in these variables and the benefit of storing your data in variables is that you can change the variables and let the rest of your program really do the same things and pick up the data that you have in those variables for example we can put 13 in the first number and if we build again this number is going to be picked up by this code and we didn't need to go in and manually change data in these sddc out statements here let's weld so that we can see this and we can close this by clicking and if we run rooster again it's going to say 13 and 7. play with this a couple of times until you really feel confident and if you have any problem don't hesitate to ask me i am going to do the best i can to help you out now that we have these numbers stored in our program we can add them up let's say and some we're going to declare another variable and we're going to say it's equal to first number plus second number and we're going to end this with a semicolon and we're going to print the sum out let's do sddc out and we're going to say the sum is sum we're going to print our variable here and we're going to print a new line character with std endl we're going to build our program so that our changes are reflected in our binary here so we're going to go to terminal run task weld with gcc the build is going to be successful we're going to press any key we are going to click here in the terminal and we're going to hit enter and run rooster again and we should see the sum and you see it says the sum is 20 which is what we expect now we are doing the sum here but it would really be better if we're not doing the sum in the main function here what if we actually took this code and wrapped it in a function that we could reuse whatever we want we have seen how we can declare a function so let's do that we're going to follow the same structure we saw in the slides earlier so we're going to say and this is going to be the return value or what we get out of the function then we're going to put in the name of the function we're going to say add numbers and we're going to pass in the parameters the parameters can really have any name you want but you have to specify the type of the parameter first so the parameters are also going to be whole numbers like 1 5 7 or whatever you want and we're going to say first program and the second is going to also be a whole number an integer and it's going to be second param okay after we do this we're going to specify the body of the function which is going to be between these curly braces i am going to jump to the next line so that this is really easy to see so i am going to define our sum variable inside the function it's going to be a whole number and i can call it whatever i want so let's call it result because it's going to be the result of this function and the result is going to be the sum of first param and second param okay i hope this makes sense and we're going to end this with a semicolon because every statement in c plus plus has to end with the semicolon after we have the result we need to give it back to whoever called this function and we're going to do that with a return statement and we're going to return the result to whoever might have called this function here now that we have the function we can actually use it we're going to go back in our main function and what we're going to do is to reuse this sum variable and store in whatever is returned from our function the syntax for that is going to be sum and we're going to say equals and we're going to say add numbers and we can pass in whatever two numbers that we want to add up let's pass in 25 and 7 for example we would expect this to give us 32 so if we print the sum again we can just complete this statement that prints the sum and reuse it here let's align this a little bit and i want you to take a guess at what we are going to get from this program right now we have these two numbers we are adding them up if we get here this should print the first number which is going to be a 13 so here we expect to get 13 and by the time we hit this second number statement we are going to print the second number it's going to be 7. if we get here we're going to print whatever is stored in sum and in sum we have the sum of first number and second number so this should print 20 right and here we are doing another sum of two numbers but we are using a predefined function that we have here before the main function because you have to define the function before you use it okay if we add 25 and seven we are going to fall into this function and it's going to add those numbers up and we're going to get a 32 in here and that's what we're going to return and it is going to be saved in this variable if we print it out it's going to give us 32. i hope this makes sense let's build and run our program run task world with gcc the world is going to go fine we're going to go back to our terminal here that's clear so that things don't disturb us and uh bring in some confusion we're going to run our rooster program and exactly what we have here we have a 13 from the first statement here we have a 7 from this statement here we have a 20 from this sum here which is here and we have a 32 that we got from this function the good thing about functions is that we can reuse them now that we know how to use it we can use it again and we can say sum equals add numbers and we can say 30 and 54 for example you add them up and you're going to get the results so we're going to print the result some and we're going to put in the sum i really want you to take some time and play with this statement so that these things make sense it's really not that difficult but you just have to rock your brains around how c plus plus defines these things so we have this statement and we are adding 30 and 54 so it should get an 84 after the 32 here let's weld we're going to run the task to world with gcc we're going to click on the terminal here and hit enter let's clear so that we don't have things disturbing us and we're going to run our program and we have our trusty 84 here i really hope this makes sense and as a bonus i'm going to show you that you can also call things in the stdc out statement here without having to store them in some predefined variables and sometimes this comes in handy so you can say std see out and we're going to say sum and you might say add numbers right in here and you're going to say 3 for example and uh 42 and this is going to give us a 45 that's what we should expect to see after 84 here so that's world and running the task to build with gcc the world is going to be successful we click on the terminal here we hit enter so that we see our terminal with output we're going to clear so that we see our output without noise we're going to run our program and we're going to see a 45 here i really hope this makes sense the whole point of this lecture was to make you aware of statements and function a statement is the most basic unit in your c plus plus program it basically lives on one line but it can also spread across different lines but we're not going to look at that here maybe we're going to see that later in the course statements are executed in order so when our main function gets to be run by the computer it's going to go from the first statement second statement third statement and it's going to go until it hits the end and we can see that here in the order in which our numbers are printed it is going from top to bottom we have also seen that we can split our code in different functions and wrap whatever functionality we have in a function we have seen the basic concepts on functions a function has a return value it has a name and you have to give it the parameters and you put the body of your function within these two curly braces as an exercise i would challenge you to do a function that multiplies two numbers and call it in the main function here to see the result if you have any problem please do ask me i am going to do the best i can to help you out this covers all we set out to do in this lecture and i hope you found it interesting we're going to stop here in this lecture in the next one we're going to see how we can input and output things from our c plus plus program go ahead and finish up here and meet me there in this lecture we're going to learn about how we can get thanks in our program and get things out of our program and that's basically input and output to and from our program we have actually been doing this all along we have been printing things from our program to the console and with this technique you can really think of stdc out as a highway that goes from your program to the terminal you can basically think of it like this data is going from your program to sddc out and it's going from here to the terminal and from this you might get an idea why these less than signs kind of point to the left because data is going from your program to htdc out and these two symbols point to the left they point to hddc out to signal to the fact that data is going from here to here i really hope this makes sense because sometimes these things can confuse people this is the reason why it's going from your data to stdc out data is basically going from your program into httc out and the sddc out is one way we have to get data from our program to the console there are many others we have std c e r r or std server this is what i'd like to call it and what it does it prints errors to the console we have stdc log which is going to print log messages why do we have these different things different programs might decide to format these messages differently because they are different messages one is a simple message from the program another is the narrows you might want to turn this red to really bring the attention to this message because the user has to solve that problem another might be a log message that you want to store in some file or some database uses for these things differ and c plus plus makes sure to give you different ways to state your intent through the tools that you use we have looked at sddc out sddc error and stdc log what does sd c i n do it does the reverse it's going to take data out of the program and bring that into the program so let's look at a few examples using these here we have an example of printing data from the program to the terminal through c out cr and c log here we have a hello world message going to c out here we have a number going to c out this is what we've been doing all along we can also do a variable storing the edge for example it's a whole number it's an integer so we print this out and the message is going to go from the program to cl so you can think that things are going from the program to sddc out i really wanted this to make sense okay if we go down we're going to see that we can use cr to print errors to the terminal and we might say something went wrong or something along these lines to bring the attention to the problem and we can use c log to print log messages and they work the same way they all use the stream output operator and it is going to take data from the program to the output stream really this is what we mean here here is an example of how you might read data and bring that into the program here we have two variables one is the age it's a whole number or an integer and z plus plus and the other is a string this is a type that allows you to store strings in c plus plus we're going to learn a little more about this later but here i want to use it to really make it clear that you can get things from the outside and bring that data in your program so we're going to let the user type in the last name and this statement here with cin is getting data from the stream or from the highway through which data comes into our program and bringing that in our program and here it should be clear that the flow of data is in this direction data is going from c i n and we're storing that in a variable that we have in our program that is called name we do the same thing and ask the user to print the age and they are going to print that and the data is going to come from cin and we're going to store that in our edge variable here after we collect the data from the user we're going to print that and we're going to say hello we're going to print the name and we're going to say they are whatever years old this is what this program does and i really hope it makes it clear how data input works through streams in surplus plus another thing we can do with sddcin is chaining input and we can use this technique to grab data in one statement look at the program down here to really understand this we have the same variables age and name we let the user type in the last name and age separated by spaces and we grab the data like this we first grab the name after that we grab the age and we're going to print the same things and we can chain input stream operators like this to grab the data in one go and this may come in handy sometimes before we try these things in visual studio code i want to bring your attention to a problem that you are likely to come in contact with and that is reading data with spaces what if the user types the name with spaces for example you asked me to type my name i'm not going to type daniel i'm going to type daniel cafwaya and your program is going to run into problems c plus plus allows to solve this problem using std get line and you use it using this syntax you're going to pass in the stream where the data is going to go and you're going to pass in the variable that is going to read the data and you're going to have this information stored correctly in your program this might be not making sense yet to you we're going to go to visual studio code and play with us and i am sure it is going to make sense okay here we are in visual studio code and we have our project where we're going to store our files data imported output we're going to copy code from our template project and we're going to put the data in and i am going to open this folder in visual studio code so let's close whatever we have now and i am going to open my folder let's browse there i am on this folder data input and output i am going to select that and the project is going to open up in visual studio code i am going to jump to my main function i am going to take out things we don't need and the first thing we're going to do is printing data we have really done this so we're going to practice this a little bit we can say stdc out and we can say hello c plus plus 20 why not and we're going to put an std endl at the end and remember our semicolon and here i want you to really put in your head that data is going from our program here to hddc out and sddc out is going to dump that on the terminal that's how this works you can also do a variable let's say ant age for example let's say 21 and end this with the semicolon and we can also print the h we can say sddc out and we can say age this is going to be sent to the output stream and we're going to do sdd and dl to add a new line character at the end and we can try and build this before the program gets more complicated let's go to terminal we're going to run the test world with gcc and if we hit enter on the terminal here the terminal is going to go away we're going to create a new one no big deal and uh if we do dir we're going to find our rooster program if we run it it's going to say hello c plus 20 and the age is going to be 21. we are successfully taking data from our program and printing that to the terminal this is pretty cool and you can think of sddc out as the highway that is really going to take your data from your program and bring that to sddc out like this we're also going to take the age it's going to go through sddc out and it's going to show up here you can think of this like this okay now that we have used sddc out we can use sddcr and sddc log in the same way so we're going to say sddcr i'm going to say error message and the message is going to be something is wrong you may do whatever you want in here and we have to remember to put a new line character otherwise data is going to be cramped on one line we can also use sddc log it really works the same way c log and we can say log message and we're going to say something happened and we remember to put std endl and our semicolon if we build this program you might guess what it's going to do it's going to print these things on the terminal but this might be useful to terminal programs that support features to format different messages directly the program is going to know that one message is a simple message the other is an arrow the other is a log message let's run our program but before we do that let's clear so that we don't have noise here we're going to run the program and it's going to say the error message and the log message here okay now what we're going to look at is how we can get data into our program and data is going to be going in a different direction it's going to be coming from the terminal and flowing into our program what we're going to do we're going to do ant age one this is a new variable so we're going to put in data and we're going to bring in a string library to allow our program to store strengths the way we're going to do that we're going to include strength this is how you do it in c plus plus we haven't really learned about this type it's a powerful type in c plus plus but we will get it there when we have more powerful tools to really understand it we are just getting started here so what we're going to do we're going to say std strength and we're going to say name and we're going to ask the user to print their name and age you already know how to do that so we're going to say please type your name and age and we're going to do stdndl on this and how we're going to get data in the program we're going to use stdcin and we're going to do std cim and data is going to be going from the stream to the program so the stream input operator here points to the right and the data is going to be going in the name variable because that's what they're going to be typing in first because that's what we told them to do so we're going to store the name and we're going to say stdcim data is going to be flowing from sddcin into our h1 variable okay now that the user has given us their information we can greet them with a personalized message we're going to say stdc out hello and we're going to put out their name and we're going to tell them you are and we're going to put the age one because that's what they're going to give us here and i'm going to say years and we're going to put an std endl here i'm going to grab this and pull this to the left so that we can see our entire program here that's helpful so what we are doing here we are getting data from the user and we are storing that in our variables and we are printing the message so that the user can see it i don't want the first things we did here to disturb us when we world and try this out so i am going to comment this out and comment the first block of code out using blog comments i think this is the first time we use this and uh this is going to be ignored by the compiler now and it is only going to consider this and this is going to be pretty cool let's go down a little bit here and i try to build the program we're going to run the task that welds with gcc and uh looks like the world is good we're going to hit enter and we're going to clear so that we are not disturbed by this we're going to run the program let's say rooster.exe it's going to ask me to type my name and age my name is daniel i'm going to type that hit enter it's going to want me to type my age so i am going to say 33 for example and it's going to tell me hello daniel you are 33 years old i put a slash here we can try and run it again so that i don't have to make that mistake again so daniel that's my name i hit enter i say 33 and if i run it's going to say hello daniel you are 33 years old i really hope this brings the point home that stdcin is a highway that takes data from the terminal into your program so this is really what is happening here when i type daniel it's going to flow from the terminal and it's going to go into my variable which is name here and when i type the age it's going to go in my edge variable here and all these happening through in here okay one other thing we can do is grab the data and put that in our variables in one go so what we can do here is comment this out and we're going to do stdcin and we're going to put that in name and we're going to change these two things and put the other piece of data in h1 if you run the program it's going to work exactly the same way but we are grabbing the data in one line so let's weld to see this in action we're going to run the task that worlds with gcc we're going to click here and we're going to clear if we run rooster it's going to ask for the name and the age i'm going to put in daniel and i'm going to put my h and say 33 for example and it's going to do exactly the same thing but we have one compact line the last thing i want to show you in this lecture is how you can grab data with spaces because if i try to put in data with spaces here the program is going to fail miserably let me show you this i'm going to clear and i'm going to run rooster again and i'm going to say daniel i'm going to put in my full name and uh it's really not grabbing the second name and it didn't give me a chance to input my age and this is really bad so c plus plus provides a way to solve this using std get line so let's see what we are doing here and before we play with this we're going to comment out what we had here so that it doesn't disturb us in what we are about to do so let's come down here and say that we were doing data input this is helpful here and we're going to go down and grab data with spaces so i am going to set up another strength and this is going to store my full name that's what i'm going to say here and to grab it i'm going to tell the user to type in their full name and age so i am going to sddc out so the way i'm going to grab the data i'm going to do std get line you see that we have this function here and it's going to take an input stream so asd dcin is going to work and the second parameter is going to be the variable where this string data is going to be stored we're going to pass in our full name and this is going to work just fine and we're going to also grab the h using the same technique we've used before so we're going to say stdcin and we're going to store that in our h3 variable once we have the data we can greet our user using the same message we had before so i'm going to copy that if i can do that and i'm going to paste that here i'm going to bring this to the same location it's going to be full name here and it's going to say h3 and let's try and weld we're going to terminal run the task to bolt with gcc and we have a problem because we forgot our semicolon see how these problems might come to you uh-huh so we're going to put in our semicolon and another thing i really want you to see is that you can bring these things to the next line if they turn out too long like we see here so i can for example put this to the next line and it is going to work just fine so what i am going to do i'm going to try and weld again run task to build with gcc you see that the world is good we're going to bring in our terminal i'm going to clear so that we don't have noise here and we're going to run our program it's going to ask me to put in my full name i'm going to say daniel and it's going to want the age i'm going to say 33 and it's going to reach me hello daniel gaway you are 33 years old we are able to bring data with spaces into our program and this concludes what we had to say in this lecture i hope it made it very clear what these htd n and stdc out things are they are basically highways that take data out of your program or bring data into your program and the way you use them is what we've done here in this course i would encourage you to play with us a couple of times if this is the first time you're doing this we are going to stop here in this lecture in the next one we're going to try and recap on the c plus plus development workflow go ahead and finish up here and meet me there in this lecture we're going to explore the c plus plus execution model and show you a simplified version of the memory model of your c plus plus program here is how our program looks it is a simple program in our main function it is going to call a simple function that is going to add two numbers we have seen a program like this before and when we compile it it's going to be turned into a binary this black icon here represents the binary executable and it is not in the format that you can read with your eyes if we come back here to what we were doing in the last lecture we can see our rooster executable binary here if you try to open it here you're going to see that visual studio code is going to say the file is not displayed in the editor because it is either binary or uses an unsupported text encoding so it says i can't really say this because it's binary and it says do you want to open it anyway if i try to open it it's going to be just some gibberish because this is binary it is meant to be read by programs it is not meant to be by ides or text editors like visual studio code so this is the binary file we have here i tried to put a simplified version of this program to represent binary so that we can follow it up but it is not in the form that is readable by any human out there unless you are a machine so why is it in binary because binary format is easily understandable by your cpu and we're going to see how your program is loaded in memory and executed statement by statement by the cpu so that you can really understand this so here is a representation of your program here we have our how to drive and we have the program sitting somewhere on our hard drive in binary format and it's waiting to be run by you by double clicking on it or opening it in the terminal and on the left here we see a representation of the memory of our computer and when i say memory i mean random access memory i don't mean memory on your hard drive this should be really clear this is the random access memory of your computer that is running your programs very fast so when you double click on our program or if we open it in the terminal the binary file is going to be loaded up in memory and you see it in red here this is a representation of that and it is ready to be run by the cpu by the time the cpu says in the memory location it's going to know i have a program to run and the cpu is going to start running it statement by statement it is going to start by the top so it's going to see that it needs to allocate space to store our integer a it's going to allocate to that somewhere on the memory in yellow here you see a and it's going to see that it should store in a value of 10 and it's going to do that you can see that here it's going to go to the next statement and the next statement tells it to allocate space for a value b and it's going to store in f5 it's going to go to the next statement it's a variable called c it's going to allocate space for it but we didn't specify a value for c so what it's going to do is store in some junk value the value in there is really unspecified at the moment it's going to jump to the next statement and what it's going to do is print some things on the console and it is going to do that it's going to jump to the next statement and it is going to print statement 2 and here it is going to notice that it is going to call a function i don't know if you have noticed but the cpu has allocated a special memory location for our function the function doesn't leave with our code here or anywhere near the variables that we have here there might be a huge chunk of memory between these two or three things so the cpu knows that it is going to jump to another location it's going to ask itself if i jump to that location to run this function how am i going to come back and the cpu is smart enough to store the return address and that's what it's going to do it's going to say if i go to that function i want to come back to the address 6 and run whatever is after this address so it's going to store 6 and cpu memory and it's going to jump to the function it's going to take the first parameter as a and the second parameter as b so a is done the parameter 1 is going to be 10 it's going to take parameter 2 it's going to be 5 because it is b here and in b we have a 5 and it is going to jump to the next statement in the function and it is going to add up parameter 1 and parameter 2. after it adds those things up it's going to return the result to c we can see that here okay that's what the cpu is going to do it's going to put the results in c and the result is a 15 so we're going to write that in there and now that the function is really done it's going to come back to its address and it is going to jump to the next segment after that it is going to print three statement three it is going to jump through the next line and print statement four it's going to do that and by this time we have reached the end of our program and it is going to be popped off the memory and your program is basically going to end this is how your program is executed by your cpu and why am i showing you this because it is really good to have an idea of how memory is allocated and used by your program another good reason is that some c plus plus feature require you to have an understanding of a memory model like this to understand how they work for example you have seen that to call this function the program had to jump from address 6 to 30 and this is really heavy especially if you are running a simple function like we are doing here to add two numbers so the cpu can realize that and optimize this call to the function to an inline function and add these two numbers up right here at address six without jumping to this address here and this may come in handy and you need an understanding of this memory model to really understand these features this is just one example i am giving you you are going to reuse this model a lot in your career as a class plus developer and i thought it was a really good idea for you to understand it firsthand this is really all i had to share in this lecture i hope you found it interesting in the next one we're going to try and understand the difference between three key components of the c plus plus programming language go ahead and finish up here and meet me there in this lecture i want to highlight the differences between what we call a c plus plus core language feature or a standard library feature or an stl feature because these are terms that come up a lot if you happen to read some documentation about c plus plus a core feature is really a basic building block that makes up the c plus plus programming language you can think of it for example how you define variables the rules that govern how you can define and use a function in c plus plus the rules that say what you can do and not do with the basic features of c plus plus that's really what can be qualified as the core feature of the c plus plus programming language i don't know if that makes any sense if we come back in visual studio code we have a program we have used in the last few lectures for example these basic types that you use in c plus plus the rules that define how you can define a function for example if you put a an angle bracket here and try to compile this program it is not going to work because we have violated the rules of the c plus plus programs the basic rules that define how the c plus plus programming language works the basic types that are defined in the c plus plus programming language these little things make up the raw ingredients you use to make up a proper c plus plus programs and these little row components that you use to put together a c plus plus program are called core features you can think of them as the foundation on top of which we really build daily use c plus plus programs the standard library is a set of ready to use highly specialized components that we can easily use in our c plus plus programs and again coming back to our program in visual studio code iostream here is a standard library feature string here is a standard library thing we have in the c plus plus programming language and again the c plus plus standard library tries to provide highly specialized components that we can use in regular c plus plus programs when you use the c plus plus programming language you don't really want to extend the c plus plus programming language you want to use it to build something of your own and uh the standard library features are things you use to work on your programs very fast for example we have used sddc out without really going into the details of how the data is taken from this program to the terminal that's really advanced and probably complicated but it is wrapped into this reusable components and one of them is iostream and we can use it relatively easily in our c plus plus program okay i hope now you have a better idea of what the c plus plus standard library is now what is the stl the stl is really part of the c plus plus standard library but it is a collection of container types think of them as collections of things and we have a set of types that allow us to store collections we have algorithms or functions that work on these collections and we have specialized types that are called iterators that allow us to navigate through these containers potentially running these algorithms on each element in the collection this is probably not making sense by now but you can think of the stl as a highly specialized part of the c plus plus standard library and we're going to learn about many features of these things throughout the course but i want you to have a basic understanding between these three terms a core feature of the c plus plus programming language is standard library feature and the stl and i really hope this is clear to you by now in this chapter we're going to learn about variables and data types before we talk about variables and data types i would like to take a moment and recap on what we know by now and if you look at these cards many of these things should make sense now you know about the main function you know that it is the entry point of your program you know that your program is going to be made up of statements and statements and with a semicolon in c plus plus you know about functions errors warnings you now have an idea about how you can handle input and output from your program you can use comments you know about the memory model and the execution model that we talked about in the last lecture you know quite a lot now we have been using a program like this to do things in the last chapter but there is one thing that isn't really explained yet and that is this ant this end is a type that is predefined in c plus plus and c plus plus predefines many types here we have a couple of these types for you to see as examples it is one of them we have seen it a lot of times in the last chapter it is used to represent whole numbers like one two and three but we have others for example double and float are used to represent fractional numbers numbers that have decimal points for example 2.5 7.3 and numbers like that and we're going to see the difference between these two another one that is commonly used is char or car and it is used to represent characters in memory and you can store in characters like a b c and d and all the other characters you can really put in we have a bull which is a type that is going to store two possible states the only values you can really store in here are true or false and we're going to see how this works void is uh somewhat confusing because it represents a typeless type and it is really meaningful when we get to talk about functions in detail so we're going to learn more about the void type a little later in the course we also have the auto type which is really not a type it is a keyword you use in c plus plus to deduce other types and we're going to see how this works later in the course these are a few examples of types we can use in c plus plus and we are going to play with most of them in this chapter another thing i really want you to see is what does it mean to really put a 4 in a variable in your program everything you have in this program here is represented by ones and zeros in the computer's memory it really looks like this it's a bunch of ones and zeros in the ram of your computer and there must be a technique to make sense of this gibberish here and store data that makes sense to humans and the starting point is to group these ones and zeros into little chunks that we can manipulate each of these ones and zeros is called a bet so this zero here is a bit this one here is a bet and if we group eight beats together they are going to form what we call a byte okay if it makes sense you can group your bits in bytes or if it makes sense to group them in groups of 16 you can do that like we see here we're going to see a little more details about this in the next lecture okay here you see that if you really want you can treat each bit as a self-contained piece of data but this is really not very useful but there are some corner use cases for this okay now we really have two sides on the right we have data in the format that is really convenient and understandable by humans and on the left we have how data is represented in memory so there must be a way to transform between these two forms of data because humans can't really handle these ones and zeros well and we do the transformation between these two representations of data using number systems we have a few number systems available to us that we can use binary octal hexadecimal are a few examples and we're going to learn about them starting in the next lecture so go ahead and finish up here and meet me there in this lecture we're going to learn a little more about number systems number systems allow us to transform data from the form that is really convenient for humans in a form that is convenient for computers in these ones and zeros that you see on the left we can also use them to do the reverse go from ones and zeros and go in the format that is convenient to humans like this 22 or this steve name here to make sense of how these number systems work let's analyze the decimal system that we really use in daily life and that is usually called base 10. so if you look at this 2371 number here we can really split it in the form that you see to the right here if you try and take this expression we have on the right here and evaluate it you're going to get this number here 2 times 10 to the power of 3 this evaluates to 2000 and this one here is going to evaluate to 300 this one is going to evaluate to 70 and the last one is going to evaluate to one and if you add these things up you're going to end up with this number so this is how we can take a number and uh make up a base 10 representation of it we can do the same thing for 924 here or 47 here it really is basic math you don't need to have a lot of background in math to make sense of this so we're going to use this exact form and understand binary numbers if you look at what we have here we have a few binary numbers that are made up of ones and zeros that's why it's called binary because you can only have two states or zero or one in your numbers so we can really do the same thing we are in base two so instead of using a ten here we're going to be using a two but it is the same operations we really did if you take this and multiply and add these things up what you're going to end up is a base 10 representation of this number if we go in the middle here we have something we can easily do without using a calculator the expression on the left here is going to evaluate to zero because we are multiplying with zero the second one is going to be a two because we are taking one and multiplying this by two the others are going to be zeros but the last one is going to be 2 to the power of 4 which is 16 and we're going to add a 2 and this is going to evaluate to 18. we can actually bring up our calculator on windows 10 i think it can help in this a little bit we're going to bring it up and we want to put it in a programmer mode and we're going to go back on our slide and find our number which is one zero zero one zero so we're going to type it out one zero zero one zero in binary this is binary that we have typed here so let's clear this and do one zero zero one zero and you're going to see that in the decimal it is 18 which is what we came up by computing this in our brain let's try the one we had before that we couldn't really do in our brain we're going to say one zero zero one zero one let's do that we're going to clear one zero zero one zero one this is going to evaluate to 37 in decimal and you can use calculators like this to really do the transformations between these different number representations okay down below we have a one one one which is going to evaluate to a seven in base ten okay if we have three digits we can really represent numbers between zero and seven and you see a table here that is going to prove that zero zero zero is going to evaluate to zero in decimal zero zero one is going to be a one and if you go down to one one one that is going to evaluate to a seven you can try and use a calculator to prove this and really understand how this works if we use the three digits representation our numbers are going to look something like this in memory we're going to take the bits in memory and group them in groups of three and we are going to use binary to actually make sense of these numbers for example here on the left this is going to be a one the second one from the left is going to be a six and this is going to be a six as well this is going to be a zero this is going to be a three because we have one and two here we add those up we get a three if this doesn't make sense you can try and use a calculator you're going to see that it makes sense okay so what if we want to group our digits in groups of four we can do that and we're going to end up with something like this the values we can represent are going to go from 0 all the way to 15 and you can try and prove this using the calculator like we just did and it is going to look like this in memory so what we really are doing is grouping the bits in different sizes of data and we have a kind of formula that we can use to make sense of the data in memory for example on the left here we have zero one one zero if we go back to our table and defined zero one one zero this is a six and we're going to know that these four bits here are really storing s6 and there are ways you can manipulate this in your c plus plus program we're going to see that you can also use five digits and you're going to end up with something like this and the numbers are going to go from 0 all the way to 32 and this is how it's going to look in memory you can try and come up with what this represents in decimal i'm not going to tell you this but we can try and figure out the one on the left here so it is 0 1 1 0 1. if we go back we're going to come up to 0 1 1 0 1 which is a 13 here so this memory location these five bits represent a 17 number so if we want to kind of generalize what we have talked about in the last slides if you have one digit you can really represent two states zero and one if you have three digits you can represent numbers between zero and seven and you can go all the way down and if we generalize this in a mathematical formula we can say that if we have n digits we're going to be able to represent between 0 and 2 to the power of n minus 1. and if you try to plug in any number of digits you're going to see that this formula works okay so these are a few grouping of bits we have in practice we can use eight digits and we can represent numbers between zero and two 255 if we have 16 bits we can represent this range if we have 32 bits we can represent this range if we have 64 bets we can really represent a really huge range i can't even say this number it's really bad okay now you should have a pretty good idea of the binary system and different representations we can use to make sense of binary numbers in a computer's memory the last thing i want you to see is what we call hexadecimal number system and it is a technique we use to shorten the length of how we represent a binary number in memory how does it work well we take our number and split it in groups of four bets and each group is assigned a symbol we can use to represent it if we have four zeros that's going to mean a zero in hexadecimal if we have zero zero zero one we're going to represent a one and you can see that if we go up to nine this is really similar to what we have in the decimal number system but once we hit 10 we're going to represent that with an a in hexadecimal 11 is going to be b 12 is going to be c 13 is going to be d 14 is going to be e and 15 is going to be f so how do we use this to represent binary numbers let's see an example here we have a huge number that we represent in memory it starts with zero and ends with the one here so the first thing we're going to do is to split this in groups of four and the one on the left is going to be a one one one one if we come back to our table here one one one one means f so we're going to represent this with an f if you go to the second group of four bets we have zero zero one one if we go back zero zero one one means three so we're going to have a three here i hope this makes sense and we're going to keep using the same transformations and we're going to come up with one f 0 3 e 6 and we can represent this binary number using these symbols you can see that we go from using 32 bits to using one two three four five six seven eight symbols to represent this and this is shorter and easily manipulable by humans and we use a zero x in front to mean that this is a hexadecimal number i really hope this makes sense so if we have numbers that don't really fit in groups of four we're going to go from the left and split this in groups of four and we're going to end up with a group on the left that doesn't really meet our requirements and what we can do is add the missing bits and make them zeros and we call this operation padding this is what you can do to really represent any number in hexadecimal if you don't have enough bits on the left we also have another system that is really not widely used today but i am mentioning it here for completeness it is called octal system it goes from zero all the way to seven and we can divide our binary number into groups of three and represent using these zeros and ones to shorten the length of the binary number we can really do the same thing we did with hexadecimal so we're going to take our binary number and divide this in groups of three the one on the left is going to evaluate to a 7 because it's 1 1 1 and one one one evaluates to seven in octal system the second one is also going to be a one one one the third one is going to be one zero zero if we go in our table it's going to be one zero zero it's going to be a four and you see we have a 4 here and you can do the same things until you have figured out the entire number here and you see on the left we are doing padding and adding a missing bit and making it to 0 so we are coming up with 0 0 1 and if we go back to our table here 0 0 1 is a 1 and that's what we put here so we're going to put this number here it's going to be 15 61 41 70 47 7 and we're going to add a zero in front to mean that this is a number represented in the octal system i really hope this makes sense okay so we have seen all these binary numbers and different number systems we can use to represent them but how do we use them in a c plus plus program well i'm glad you asked here is a simple example of how you can represent the number 15 through different number systems in c plus plus if you just say 15 like this it's going to be in decimal if you add a zero in front it's going to mean that the number is in octal system so this can be confusing sometimes if you do 0 17 it's not going to be 17. so if you say 0 17 c plus plus is going to interpret this number as if it is in octal system and it is going to turn this into 15. down here you see the same representation in hexadecimal we saw that you use a 0x in front to represent that it is in hexadecimal and this is going to be 15 in memory and here we have representation in binary using zeros and ones and you use zero b in front like this i have to say that this was only possible since c plus plus 14. so if you are using an older standard of c plus plus you want to be able to represent binary like this and if we run this program it is going to print the same thing it is going to print 15 but we stored the number using different number systems let's go to visual studio code and try this out okay here we are in a folder i have on my system where i am going to be storing my projects for this chapter the first thing i am going to do is copy code from a template project i am going to go in and copy things over and i am going to put these in 4 2 number systems i am going to open this folder in visual studio code so i am going to go to file close the folder we have now and i am going to open up a new one open folder and i am going to browse to the location of my project i am going to open this project here that says number systems we are going to get rid of the welcome screen and open our main cpp file we're going to get rid of this constival thing and clear everything we have in the main function here the first thing we want to do is to store an integer that is 15 and we're going to say and number one the exact same thing we had in the slides and we're going to store 15 in this variable here this is going to be represented in decimal and if we want to store the semi number in octo we saw that in our slides we could store this as 0 17. so let's do that we're going to say it number 2 and we're going to store in 0 17. this is not going to be 17 this is going to be a representation of a number in octo system so we're going to say that this is octo and we can represent the number in hexadecimal as well so we're going to say it number three and it is going to be 0x because it is hexadecimal and we're going to say zero f this is how we represent it you can use lowercase or uppercase it won't really matter let's use uppercase here and we can use binary because we are using c plus plus 20. so we're going to say and number four and we're going to store in a representation of 15 in binary and it happens to be four zeros and four ones and we have to prefix this with a zero b this is how it works so this is going to be in binary and if we print all these numbers up we're going to see let me not tell you this what do you think we'll see okay so i went ahead and typed this out what do you think we will see when we build and run this program before we do that we're going to open a terminal so that we see it here and we're going to go to terminal run task we're going to build with gcc let's wait and see if the world is successful world finished successfully we have a rooster program in here we're going to click somewhere in this terminal and our terminal is going to show up if we do dur you're going to see that our rooster program is here and if we run it i want you to take a moment and guess what we're going to see hmm number one 15 number 215 in number 315 and number four 15. this is really the same number we are storing in these variables but we are using a different representation that is going to be convenient depending on whatever it is we are doing if it is more convenient to use binary we are going to use binary if it is more convenient use hexadecimal please do use hexadecimal but the representation of this number in memory is really going to be the same i really want you to take some time and let this sink in because you're going to see these things a lot of times in your career as a software developer so please do yourself a favor and make sure you really understand this okay this is really all we set up to do in this lecture trying to make sense of the binary numbers we have in memory and how we transform between them and different forms of data that is easily manipulable by humans all data is represented by a bunch of grouped cells of zeros and ones we have seen this as the range of your data grows so will be the number of digits you need to represent the data in memory we have seen that the hexadecimal system makes it a little easier for humans to handle streams of data in ones and zeros octo has the same goal as hexadecimal but it's almost no longer used in modern times i just mentioned it here for completeness we are going to stop here in this lecture in the next one we're going to look at integers in a little more detail so go ahead and finish up here and meet me there in this lecture we're going to learn a little more about integers in c plus plus integers are represented by ant and z plus plus it's what you see on the left here and they are used to store whole numbers things like 1 2 and 3. they typically occupy 4 bytes or more in memory but in most times it's going to be 4 bytes so for example if you have a variable called age and it is an integer this is how it's most probably going to look in memory it's going to take four bytes and i really want to make it clear what a variable is it is a named piece of memory that you use to store specific types of data okay so here if we have a variable named age it is a named piece of memory edge is going to be the name of that piece of memory and we can store data in that memory it's going to look like this in memory but the value is going to be stored in here in binary format i really hope this makes sense a variable is a named piece of memory that we can use to store different kinds of data and okay now we're going to look at a few examples of how you can work with integers in your c plus program you can define a few variables here we have one that is going to store the number of elephants we have you can declare it like this and not put in a value and if you do it like this the compiler is just going to put in some garbage value that is really not meaningful for your program so you see the warning we have here as a comment the variable may contain random garbage value and this is something you need to be aware of another way we can define integers in our c plus plus program is this statement here that declares a variable called lion count it's going to initialize to zero if we use curly braces like this it's going to initialize to 0 by default we can also explicitly put in a value we can do the same with cat count and we can use these variables to do operations for example we can declare another variable and it is going to store the number of domesticated animals and we're going to add up the numbers for dogs and cats and we're going to store the result of that in our variable that is called domesticated animals this is something you can do another thing that can be problematic is trying to use variables that you haven't defined or declared in your program that is going to give you a compiler error and the compiler is basically going to say i don't know what doesn't exist one here is i don't know what this doesn't exist to is it is not defined anywhere in the program another thing you can do is try and store a fractional number in a variable that you declared as an integer and this is going to give you a warning because fractional numbers are usually bigger in size than integers and this is going to lose some of your data we don't really have enough tools to understand this now but we are going to understand this a little bit later in the course but this is something you can do you can take a fractional number and store that in an integer variable and we're going to do an example and see how this behaves with our compiler that is gcc10 okay here we have mostly used curly braces to initialize our variables but we can also use parenthesis and this is going to be usually referred to as functional variable initialization because we are using parenthesis and this is mostly going to do the same thing we saw in the last slide with curly braces but when we come to the example here where we are storing a fractional number this is going to silently chop off the data you have in a fractional number and turn that into an integer and this is called implicit conversions but if we go to the last example here where we are storing 2.9 into an integer this is going to take this fractional number chop off the decimal point and only store 2 in this integer and this might not be what you want and it is going to happen silently so this is a really bad way to do this if you want to do something like this it is preferable to use curly braces because you are either going to get a warning that this is going to lose you data or you're going to get a compiler error and the compiler is going to completely stop you from doing this and again if any of this doesn't make any sense we're going to go in visual studio code play with us and see how the compiler behaves with these things we have another way we can initialize our variables in surplus plus we can use assignment and we have done this a couple of times in the course but this is something you can do and we are going to play with this in visual studio code in a minute and you're going to see how it works okay by now we have seen three ways we can initialize our integer variables we can use breast initialization which is basically using curly braces we can use functional notation which is using the parenthesis and we can use the assignment initialization which is what we have here another thing i really want you to see is that it is possible to query for the size of your variable here we are trying to print the size an integer is going to take on our operating system and compiler and if we try to run this on our compiler it's going to tell us exactly how much memory an integer occupies in our program we can either pass and explicitly like this or we can pass a variable name and it is going to work equally well okay ants are mostly going to take up four bytes in new memory so here we have a representation of three ants in memory if we go from this zero to this zero this is going to be exactly four bytes and this is how it may look in memory one other thing i haven't really talked about on purpose is that there are a few rules you have to follow to name your variables in c plus plus one of those rules is that a variable must start with a letter like a b c d up to z or an underscore you can't start a variable for example with a number that's going to be a problem after the first character in your variable name you can really put in anything you can put in numbers you can put in letters you can really put anything you want but the first one can't be a number variable names are also case sensitive so if you have a variable called bike account with the lowercase b and you have another one that starts with uppercase b these are going to be two different variables and uh you have to be aware of this no spaces or spatial characters are allowed in your variables you can use underscores but you can't use something like plus because that's going to be confusing to your compiler and these are a few rules you really need to be aware of and you're mostly going to be fine the general form for declaring and initializing your variable is what you see here you're going to pass the type in this case we have used and because we are learning about ants you're going to pass the variable name and you're going to put whatever it is you are initializing your variable with of course this is braced initialization because it is using curly braces but you can also use functional initialization or assignment initialization and it is going to work equally well okay now we have enough information to go in visual studio code and play with us okay here i am in my folder for the projects of this chapter i am going to hop over in my template project and copy the files that i need i am going to go up again and go on the project that is called integer types i am going to go in and paste in my files and i am going to open up this folder in visual studio code i am going to close the welcome screen here and open my main cpp file and i am going to do the usual take out the things we don't need to save on time i am going to put in a piece of code that i have lying on my drive and we're going to go through it to save on typing time i don't want you to suffer watching me type this the first thing we have is the elephant count this is a variable that is not initialized so this is going to contain garbage data it's not going to contain anything that is useful to us but some compilers are going to usefully put in a zero because that's a logical number to put in here if you didn't explicitly put a number in here the second one is using curly braces and this is going to automatically initialize to zero the third one is going to be explicitly initialized with the 10 and the cat count is going to be initialized with 15. you can try and print this out and see what you come up with after compiling this application another thing we have seen is that we can do something like this and put an expression into our curly braces and the value this evaluates to is going to initialize this domesticated animals variable and now you should know that this is probably going to take 4 bytes in memory because it is an integer this is really how c plus plus works each fundamental type has a size associated to it most times so integers is mostly going to be four bytes on your system and we're going to query this a little later in this video okay let's try and compile this program and see if it actually works we're going to start world and you see that the world is successful another thing we can do is try and use variables we haven't really declared in our program and you can guess what is going to happen if we try to do and new number for example and putin doesn't exist you know it's a number that doesn't exist the compiler is not going to know what this is because we didn't declare a variable called doesn't exist if you try to compile this you're going to get a problem you see that we have a problem before we even compile so visual studio code is this smart it can analyze our code before we compile it and give us hints to problems that might come up you see here it is saying doesn't exist isn't defined so if we try to compile this we're going to get an error that we expect it's going to say doesn't exist doesn't really exist i don't know what that is so you shouldn't really do that you shouldn't try to use variables you haven't declared this is braced initialization we can also do functional initialization as we have seen okay another thing we can do is store a fractional number in an empty variable and we're going to see what this does we're going to try and compile this code you see we have a 2.9 we're trying to store this in an integer if we try to compile this let's try and use gcc let's clear this let's bring up a terminal and try to build this we're going to try and run our gcc compiler on this program it is going to say errol narrowing a conversion of 2.899 from double to integer c plus plus 20 isn't even going to allow this so this is a good thing because you might think that you have a 2.9 and some compilers are going to chop this off and store a 2 instead so this is one of the safe things about this way of doing thanks so i am going to comment this out because otherwise our program wouldn't compile now that we know how to use these things we can try to print the values in here okay i am going to put in a piece of code i have lying around on my drive i am going to remove these spaces inside because it is annoying let's do this and if we try and build this program it should compile successfully and if we run it we should see our numbers we're going to go in our terminal here and do der and we're going to run rooster and we're going to see elephant count is one because this is a garbage value so it could really be anything lion count should be zero dog account should be 10 cat count should be 15 it is what we have and a domesticated animal count is 25 because we have drug account and we add that to cat count and we're going to end up with 25. this is what we expect now that we know how to work with this we can also try and play with functional initialization before i put in my code i am going to comment all this out because i don't want this to disturb us as we work with our functional initialization code so i am going to comment all this out and go all the way down and put in my code for functional initialization and you see here we have a variable called ethel we initialize this to 5 we have orange count which is 10 we have fruit count and we have a bad initialization that is not going to work it doesn't exist 3 and doesn't exist 4 don't really exist so so this is going to give us a compiler error and if we do something like we do in narrowing the conversion function here this is mostly going to compile and this is going to chop off data and store a 2 instead of the 2.9 this is something you should really be aware of and you see the comment here says information lost this is less safe than braced initializer because breast initializer is going to give you a compiler error if you try to do this and that's a good thing so we also try and print these out so i am going to tidy this up a little bit and i am going to try and compile this piece of code run task and world with gcc the world is going to pass through without a problem and if we do clear and run rooster we're going to see apple count is 5 which is what we have in here orange count is going to be 10 so this is right fruit count is going to be 15 because it is 5 plus 10 and what is really worrying is that narrowing conversion is 2 but we didn't store in a 2 we started in a 2.9 and because an integer can really store 4 bytes 2.9 is more than 4 bytes and it is going to store what it can and it is going to chop off this 2.9 and only store in a 2 so this is really something you should be aware of sometimes this is going to cause problems in your program and really really if you are trying to store fractional numbers in an integer please use a braced initialization form because the compiler is going to stop you from doing this if you try okay so this is really the difference between functional notation and breast initializer notation functional notation is not going to give you an arrow if you try to store something that can't really fit in a net it is going to silently chop it off and put in whatever it can and braced initializer is going to give you a compiler error so this is the difference between these two okay i am going to comment this out as well and i am going to put the starting comment here and i am going to go down and show you that you can do assignment initialization as well i am going to put in my code here because i don't want to type it really and i am going to print all this out and we have a bike account variable that we initialize using the assignment notation track count is going to be seven fake account is going to be bike account plus track count and we're going to be narrowing conversion here and we're going to see if this actually works we're going to try and weld this we're going to run the task to world with gcc it is going to pass through you can guess what we're going to see if we run the program bike account is 2 track count is 7 which is what we expect vehicle count is nine and uh the narrowing conversion is also chopping off what we have here so this is something you need to be careful about another thing i want you to see is that you can check the size of your variables in memory so we're going to check the size with size of and i am going to put in a piece of code to really play with this i don't want to type all this because it's really self-explanatory by now we're going to print sites of ant and inside we're going to say size of ant and this is going to give us how much memory is really occupied by an end and we can use this form here by passing in a variable name track count is a variable we have declared and initialized here so if we build this program let's do that with gcc the build is going to pass through and if we run rooster we're going to see that the size of int is four you're going to see that this is in bytes and size of track count is for as well because track account is also an integer so these are all little tools you can really use to know how much memory is really being used and occupied by your program and this is really all we set up to do in this lecture to learn as much as we can about integers integers occupy 4 bytes in memory for the most part and we have seen different ways we can initialize our integer variables we can use breast initializers which is going to use curly braces we can do functional initialization using parentheses like this we can also do assignment notation using the from like this and we can use size of to check the size of our integer variables i have to say that these kinds of initialization also work on other variables but we took the chance to introduce all these concepts using integers this is really all we set out to do in this lecture i hope you found it interesting we are going to stop here in this lecture the next one we're going to start and learn about integer modifiers go ahead and finish up here and meet me there in this lecture we're going to learn about integer modifiers and these are little things you can sprinkle around your integer types to modify their behavior a little bit but before we dive deep into this let's talk about how we can store positive or negative numbers in integers and we can do something like this if we put a tan inside our value 1 variable which is an integer it's going to work we can also put in a negative number like minus 300 and it is going to work so what's the magic here well this is signed by default and this is a modifier we can apply to our variable to signal that we have the possibility to store in negative or positive numbers and it is going to work if you only want to store positive numbers in your end variables you can use another modifier called unsigned and if you put in a negative number you're going to get a compiler error we're going to see how this works in a minute okay regardless of whether your integer variables are unsigned or signed they are going to still occupy 4 bytes in memory so this is how it's going to look in memory and here is a simple formula we can use to come up with the range of values we can store in our int values if they are unsigned or signed if the number is inside you can use this unsigned range formula and plug in the number of bits you have in your data type if you are using a signed integer this is what you're going to use and here is a simple example for example if we have an unsigned and this is the range of values we can store we can basically go from 0 all the way to 4 billion if the number is signed the range is going to basically be divided into half the half is going to go on the left and store negative numbers and the other half is going to go to the right and store positive numbers and you can see that we can basically go from minus two billion to two billion if the number is signed okay by now we have seen two modifiers unsigned and designed but there are others for example you can flag your integer types as short or long what does this do well this is a table that i put together to try and come up with all the combinations you can come up with if you have a short variable it is going to be two bytes in memory this is the modification that this modifier does to your type if you have short and it's going to be two bytes if you have a signed short and it's going to be two bytes if you have unsigned shirt and it's going to be two bites you can see that the effect of short is really shortening the original size in memory for whatever type you applied this on this is really working on an editor even if we don't specify int here for example short short here it's going to implicitly be and that's why we have two bytes because it's basically an end that we have split into here in the second column we have ant which is going to be 4 bytes if it is assigned and it's going to be 4 bytes this is really the sizes you're going to come up with if you use these combinations of modifiers on your integer types one thing i really want you to be careful about is that these modifiers only work for integral types so they work for data types in which you can store decimal or whole numbers you can't use this on types in which you can store fractional numbers like 2.9 that's going to give you a compiler error now that you have an idea about this why don't we go in visual studio code and actually play with us okay here we are in our folder that contains our projects we're going to do the usual and copy the files from our template project and we're going to put those files in our integer modifiers project here let's open it up and paste in the files we're going to go up again and we're going to open this up in visual studio code the first thing i want to play with is storing positive and negative numbers in our integer types so we're going to say end value one and we're going to store in a 10 for example and we're going to do any value 2 and we're going to put in a minus 300. i think this is what we had you know a slide so we're going to keep this if you try to print this out so we're going to say value 1 and we're going to say value 2 and we're going to try and build this so we're going to open our terminal so that we can play with it right away and we're going to run the task to world with gcc the world is going to be successful if we run our program we're going to see that it's going to say value 1 is 10 and value 2 is -3 300 this is really cool we can store positive numbers and negative numbers in our end times and this is going to occupy 4 bytes in memory we can prove this by printing the size of value 1 and value 2. let's do that so we're going to say size of value 1 is size of value 1. we can also do it for value 2 size of value 2 and it's going to be size of value two std and yeah if we build this again we're going to run the test world with gcc and uh we run our application it's going to save four bytes so it doesn't really matter if we store in a positive number or a negative number it's going to be 4 bytes this is the point i want to make here another thing you can do is actually make it very clear that you have signed numbers in here and you can use the signed integer modifier here so this is going to give us the same result and if we try to both again run the task to build with gcc the world is going to be successful if we run the program we're going to get the same result so if you don't put aside or if you put it here it's going to be exactly the same thing another thing you can do is make it very clear that you want to store positive numbers in your variables we can do that by using the unsigned integer modifier so we're going to say unsigned and value three for example and we're going to put in a four this is going to be fine but if you do unsigned and value 4 and put in a minus 5 for example this is going to give you a compiler error because you save this is unsigned and this really means that you want to store in only positive numbers but you are trying to put in a negative number if you try to build this let's try this we're going to run the task to build with gcc we are going to get a compiler arrow saying conversion from -5 to ant this is not allowed you're going to get a compiler arrow your program is not going to compile so this is a compiler error let's say this here and this is really all i had to share about these signed and unsigned modifiers signed is going to mean that you can store negative and positive numbers unsigned is going to mean that you can only put in positive numbers and if you try to put in a negative number you're going to get a compiler error now that this is commented out if you try to run the test to world with gcc the world is going to be good and we will be able to run our program and it's going to do whatever it was doing before as we see here we also saw that we could use the short and long modifiers and we're going to put a piece of code here that is going to allow us to play with them so i don't want to type this and if you don't want to type this as well you can get this code from the resource section it's going to be left you can open the link use the code copy the code and paste it in here and really play with it because the point is really not to type all this the point is to understand what these modifiers do to your variables what we are going to do we're going to try to print them out and print the size they actually have in memory we're going to do this in four groups we're going to do one for short and long we're going to do one for ant here we're going to do one for long and we're going to do one for long long these are things you can do and we're going to see how much memory is actually being used by all these variables we are going to start with the first group here so we're going to go down and print the size is out and this may be really hard to wrap your brain around but they are plain old see out statements that we have been doing all along i am going to split this on different lines so that you can really say this so we have an output statement for a short var which is what we have here so we expect this to take two bytes and all these things are probably going to take two bytes let's try and build the program with gcc the bullet is going to be successful we are going to run rooster and you're going to see that short end is two bytes signed short is two bytes and everything is basically two bytes in this group here and we did guess right the next thing we want to try out is this group of ants so we are going to go down and give ourselves some breathing room and we're going to paste this in and we're going to separate these again so that we can see what is really happening here and i think this is really enough so we're going to see the sizes for the thanks we have in this group here and we expect this to take four bytes so let's try and run the world task to build with gcc the world is going to be good we can run the program like we usually do and you're going to see that ant variable is 4 bytes and everything in that group is 4 bytes and this tells you that regardless of the side and unside modifiers you put here it's going to really use the size of it here okay it's going to take four bytes in memory let's try this group here we are going to go down and put in the code to print the sizes for them and this is what we should use let's split this on different lines so that it is easier on the eyes and again if you don't really want to type this you can get the code from the length resource section you're going to find the code here and you can copy and paste and really play with us but it is helpful to really type these things out and really bring these things in muscle memory because it's going to help okay so we have this and we're going to try and run the world task with gcc the build is good we're going to go in here and run rooster and it is going to say long variable is four bytes and everything in this group is basically going to be four bytes in here the last one is this long long thing it is going to really give you a huge range for your values you can store in the variable but don't really trust my world let's try this out so that you can see for yourself we are going to go down and really put this in so we're going to give ourselves some breathing room and paste the code in so i am going to bring this to the next line and what we are really doing is printing the sizes for the variables that we have in this group here let's build and see if this passes the world the world is going to be good now we can run rooster and we're going to see that long long is eight bytes eight bytes gives us a really huge range of values okay this is really all we set out to do in this lecture to learn about these integer modifiers they modify the way your integer variables behave for example we can specify that we only want positive numbers all that we want to both store positive and negative numbers for that you're going to use designed or unsigned modifiers you can also shorten or lengthen the range of values you can store in your variable by using the modifiers short and long and from the output of a program here you can really see all these effects here this is really all we set out to do in this lecture i hope you found it interesting we are going to stop here in this lecture in the next one we're going to start and learn about fractional numbers go ahead and finish up here and meet me there in this lecture we're going to learn about fractional numbers and as we work with these numbers i want you to keep in mind that any piece of data you work with in your c plus program is ultimately going to be stored in the form of ones and zeros in memory functional number are also called floating types in technical terms they are used to represent numbers with fractional parts in c plus plus and many other languages and we have three types that we can use in c plus plus we have float double and long double and the main difference is in the size they occupy in memory a float takes four bytes a double takes eight bytes and the long double takes 12 bytes typically so as the size goes up you can also notice that the precision goes up so what is the precision the precision is basically the number of bits you can represent with that type starting from the number in front of the decimal point so if we look at this number here we can try and figure out its precision so we're going to count 1 2 3 4 5 6 7 8 9 10 11 12. the precision here is 12. if we go back to our table we're really not able to represent a number like this with a float the smallest thing we can use to represent this number well is a double because the precision is 15 and it is more than what we need for this number which is 12. one thing you should remember is that the number in front of the decimal point is also counted then when you are trying to figure out the precision for your floating point numbers in c plus plus so here we have an example of how we can declare and initialize floating point numbers in our c plus plus program number one is a really huge number so you can guess that this is more than we can handle with the float we're going to see how this is handled by our compiler a double has a precision of 15 so we should expect to store more numbers than we did in a float a long double should be able to handle this without a problem because if we go back to our table here our long double here should be able to handle this better than double but it is possible that it is the same thing as double depending on the compiler implementation and if we try to print out the sizes here we're going to see what the compiler gives us we're going to do this when we try this in visual studio code okay and here we are trying to print out the precision of these numbers we can use a special setting on our stream to control the maximum precision that we can see here you see that we are trying to show the precision for each number there is a special setting we can put on stdc out to make it control the precision to a given number here we are using 20 and we're going to see that for number one this is going to be seven digits for number two it should be 15 digits or something close to that the specifics are really dependent on the compiler implementation and for long double it should at least be the same as double or even more in some cases okay here we want to see that we can also have narrowing errors if we try to store something back in a float and a float can't really handle it here we have a number with a lot of digits one two three four five six seven eight nine and we know that the float can only handle seven digits so this is probably going to give us a compiler error as we've seen for breast initialization if we use functional initialization this problem is not going to be code at compile time so we're going to end up with a chunked piece of data and we're going to see this when we play with this with visual studio code and another format we can use with floating points is that we can use scientific notation and it is basically a way we can use a power of 10 multiplier to multiply with our floating point number so for example here number 5 is going to be this number 1 9 2 4 0 0 0 23 we can use a floating point scientific notation to represent this so what we're going to do we're going to say 1.9 and we're going to multiply this with 10 to the power of 8. this is what this means this 8 here means to multiply with 10 to the power of 8. and if we multiply with that that basically means moving the decimal point 8 digits to the right so we can go one two three four five six seven eight and it is going to end at these three here and it is going to be basically the same number number seven is also another one and if we do this we're going to pad with zeros after these four and we're going to end up at this same location as the three but it is going to be a zero we're going to see about that in a minute and number eight is another floating point number and we can use scientific notation to represent it just like you see with number nine we're basically going to have the same thing when we have a minus in here it means we're going to multiply with 10 to the power of minus 11 and this is going to give us a number below one thing zero point zero zero seven or something like that so we're going to go 11 digits after the zero here and if we count one two three four 5 6 7 8 9 10 11 and the decimal point is going to go after three and we're going to basically end up with the same thing here we're going to play with this in a minute and you're going to see that all this makes sense and don't forget that any piece of data we store in a computer is going to be ultimately represented by ones and zeros in memory and for floating points we don't use the number systems like we've done for integers there is a special system that is used to do that but it is a little bit complicated to cover in a course like this i am just going to point you to it if you are interested and when you are done with this course you can come up and read on this the main point is that any piece of data that you represent in your c plus plus program is going to be represented in terms of ones and zeros in the memory of your computer okay there are a few things you can do with floating points that you can't really do with integers for example you can divide with zero if you take a floating point number and divide that with zero you're going to get what we call infinity if the number is positive you're going to get positive infinity if the number is negative you're going to get negative infinity and you can take two floating point numbers that are zeros and divide them and what you're going to get really is not a number it's something called nan which stands for naughty number and uh your program is going to not crash but very few things you can't do with these things for example you can't add them up you shouldn't really do this it's probably going to end up bad for your program here we have a few examples we have three numbers declared they are doubles they must be occupying eight bytes in memory number 10 is initialized to 5.6 11 is initialized to zero because we are using the braced initialization and we have nothing in here and infinity here is the result of dividing a number by zero so it's going to be positive infinity because number 10 is a positive number if you go down we're going to print number 10 divided by number 11 and we're going to say yields we're going to put the result out we're going to see that down here on the second line you see that we are trying to add something to infinity and this is also going to yield infinity because think of infinity as something super big and if you add something small to something big it's still going to be something big down here you see that result equals number 11 divided by number 12 and these two are zeros so we're going to end up with none and that's what we're going to print in this statement here down to the bottom okay you should really remember to put in the suffixes when you are initializing your floating point numbers otherwise the default is going to be double this is what we mean by this you see this floating number we have a suffix that is f to mean that it is a floating number if you don't put that in this is going to be interpreted as double and the compiler is going to try and turn that into a float by chopping off things that can't really fit in a float so to really make it clear that you are stirring in and loading number please put this f suffix here for double you don't need to put anything because it is the default assumed by the compiler but for a long double you have to suffix this with l just like this so that's what we really mean here we have seen that the precision for float isn't really enough for many of the computations we might want to do in our c plus plus applications okay we have talked a lot about floating point numbers let's head to visual studio code and actually play with them okay here we are in our folder that is going to contain our project the project is going to be called fractional numbers so we're going to copy our files go up a little bit and put those in and i'm going to go up again and open this in visual studio code we're going to close whatever it is we have opened now we're going to close folder and we're going to open our project in visual studio code and we're going to do the usual and remove things we don't need in here the first thing we want to do is to store a few floats in our program we're going to declare and initialize a few floating point numbers we have number one number two and number three and we want to make it clear that the sizes are what we expect we are going to open a terminal window here and we're going to build with gcc run the task to weld with gcc the world is going to be successful we're going to come to our terminal click a little bit and we're going to type dir we're going to see that a program is in place if we run it we are going to see that size of float is 4 size of double is eight and size of long double is 16. this is what we expected another thing we can do is try and play with the precision of these numbers and see what we can really go away with okay so we're going to actually put in a piece of code to play with this we're going to add a setting to our sddc out stream so that we can control the precision but for this to work we need to bring in a library that is called i o manip so we're going to include that i o manip and how do i know this i usually consult the c plus plus standard library documentation and you are not at the point where you can really understand it right now so we're going to be putting in these things here and explaining them as we go and i made the type here it is io manip and you see that now this is recognized our program should compile so we're going to try and weld it so that we can try this out we're going to build with gcc the world is going to go through we can run our program we're going to do dur and clear and rooster to run you see that things start to go off at sex and if we count what we have here it is one two three four five six seven we can only represent seven numbers accurately with a float and after that we're just going to have garbage because the computer can't really make sense of whatever we have after our seven digits that we can legally represent with the flood so the precision for our floating point number here is seven i really hope this makes sense let's check out number two we are using double so precision should be around something like 15. so we are putting in a number one through nine and we are starting over zero one two three four five up to zero again if we look at what we have in the output here we have one two three four five six seven eight nine zero one two three four five six and here things start falling off after our sixth year and if we count we're going to have 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 digits this is the precision we are having this compiler for double 17 so it is something around 15 and we should expect to have something more than that or at least the same thing as double with long double so if we look at this you see that it is one two three four five six seven eight nine zero one two three four five six seven eight nine we are able to represent up to 20 and you can see that long double leaves up to even the precision of 20 that we have set with std set precision here okay i really hope now that you have an idea about these precisions we have with our floating point types in c plus plus programs another thing i want you to see is what kind of narrowing errors we can get if we try to initialize with something that can't really be represented by a floating point number and here we have a float it is called number four and we are trying to put in something that is bigger than we can really fit in it has more than seven digits so one two three four five six seven eight nine it is more than seven digits and we would expect to have a problem with this because we are using braced initialization let's try and build this with gcc we're going to do that and this is going to work successfully so this is going to silently chop off our number if we try to print it out we're going to say sddc out number four and we're going to world again run the test tube world so that our changes are taken into effect i'm going to do dur and do rooster and this is going to print our number you're going to see that after the zeros we have junk our number has been basically chopped off and this is junk because we can't really represent this with the double the 16 here has been chopped off because we can't basically represent this with the floating point number if we change this to double this is going to probably give us what we expect so let's run again world with gcc the world is going to be good if we run the program it is going to show us the same thing okay this is a really good learning change why are we having the same results as before even if we changed the type to double try to think about it the reason is the suffix here is still saying f and this is going to be stored as float in memory so if we want this to really be treated like a double we can take out the float here and if we build again and run the application now you're going to see that we see the 23 this is basically being correctly stored as a double this is a really good thing you should keep in mind if you don't put in your suffixes here the number is going to be interpreted as double by now i hope you see that if you really try to put more than your floating point can handle the number is going to be chopped off and only the precision that it can handle is going to be stored in i hope this makes sense and you see that the behavior for numbers is really different we got the compiler error when we try to do narrowing conversion with integers but now it is silently chopping off our number if we try and use functional notation i think we're going to get the same result and change this back to a float and uh use float here and if we both again let's go to terminal run task use gcc we're going to run this let's run rooster and you see that we get the same results our 23 is chopped off here we can also use the assignment notation here just like we did with integers and this is going to do exactly the same thing it is going to chop off the 23 here and we're going to have some garbage which is 16 in this case if we weld the gun the world is going to go through and if we run we're going to see as it's in here okay narrowing conversions are something you should be aware of when storing your floating point numbers and you should really remember to put in your suffixes here and other thing we have seen that we should really play with is scientific notation as the comment here says what we have seen so far in floating point numbers is fixed notation it is what we have here but we can also use scientific notation which is exactly what we described in the slides and what we have here for example is e8 and it means that we are going to multiply this number here with 10 to the power of 8. if you go down here we have number 9 so we have 3.498 and we have e minus 11 and what this is going to do is multiply with 10 to the power of minus 11. this is how i can represent this here and we can print these numbers out to really play with them to make a difference with what we have been doing before we're going to do sddc out and put a dividing line here this is one way we can do this so we're going to do stdndl we're going to weld this so we're going to run the attached world with gcc 10 the world is going to go through if we do rooster we're going to see that our number here number five is what we expect number six is the same thing but we used scientific notation to do this because we took this number and multiplied by 10 to the power of eight number seven should be almost the same thing but the lower 23 here is going to be chopped off because we haven't specified it we're just multiplying with 10 to the power of 8 so that's why we have 0 0 here i hope that makes sense and if you look at number 8 you see that we stored it like this without using scientific notation we used fixed notation but on the terminal it's going to be printed out in scientific notation and this may be pretty cool the main usage for scientific notation is to make sense of numbers that are really huge so the way you interpret this you're going to move these numbers after the decimal points with 0 in front and it's basically going to be like this so if you count the zeros you have here we're going to have 1 2 3 4 5 6 7 8 9 10 and 11 is going to be directly after three and you're going to get the number here this is what we mean and number nine was explicitly stored in using scientific notation and you see that we have the same thing you can play with these things and really make sense of them and again we didn't specify any suffix here so these are going to be interpreted as doubles but this is not a problem because they are doubles here okay we are making some progress with this lecture which is a little lengthy the last thing we want to do is to play with infinity and nan we're going to put a message here to say what we want to do and we're going to have number 10 which is going to be positive we're going to have number 11 which is going to be zero number 12 is also going to be zero if we take 10 and divide that with 11 we're going to get infinity because we are dividing a floating number with zero if we try to print that out we're going to get infinity and we're going to get infinity for this result here because we are trying to add something small to infinity which is something really big and if we all trying to divide zero with zero in floating point we're going to get what is called naughty number and if we print that out we're going to see this in a minute let's build this with gcc we're going to weld successfully let's clear this out so that we don't have noise in here and we're going to run rooster and you see here it says infinity and none which is what we said here and if we go down here it is saying number 10 divided by number 11 and number 11 is zero you see that here it's going to yield infinity if we add five to infinity we're going to get infinity and if we try to divide a zero by a zero we're going to get none which is really a way to tell you that you're doing something wrong you should really be careful one other thing i want you to see is that if you turn this five into a minus five and build again we're going to build with gcc and we're going to run this rooster we're going to get minus infinity so if you divide a positive number with zero you're going to get positive infinity and if you divide a negative number with infinity you're going to get negative infinity and this is what we see here this is really all we set out to do in this lecture i hope you found it interesting and again the big message is that any data type you're going to be handling in your c plus plus program is going to ultimately be stored as ones and zeros in memory and we have learned about floating point numbers float double and long and they have these properties you see here and you should use them with this in mind otherwise you're going to have problems with your precision make sure you are using the correct precision that you need in your program we are going to stop here in this lecture the next one we're going to try and learn about booleans go ahead and finish up here and meet me there in this lecture we're going to learn about booleans booleans are types that can store two states in c plus plots it can store either true or false and we can use this to really do decisions in our program if you look in the program we have here we have two variables declared the data type is bull and one is called red light the other is called green light and we have true and false inside one thing we can do in c plus plus is make decisions we can say if the red light is true we can print a message that says stop and if it's not red we can print a message that says go through we can do that and booleans are really used in this way to make decisions in your c plus plus program we haven't really learned about if statements in this course but i am throwing this in here to really drive the point home that we can use this to make decisions in our program down here we have another way we can do this we can actually use the variable itself without checking if it is equal to true or false and we are going to get the same message the point here is that if green light is true we're going to say the light is green if it's not we're going to say the light is not green you can do something like this and again the big point here is that booleans data types are used to store two states true or false in your c plus plus program okay by default if you print a boolean that has two in it you're going to get true and this is what you see here and if you print one that has zero in it you're going to get false we can change that with a special setting that we can apply to our std out stream and you do that like this with std bull alpha and we're going to play with this in a minute you're going to see if we try to print with this setting applied we're going to see true on false and not c 0 and 1 like we did before we're going to play with this in a minute and you're going to see another thing you should know is that booleans occupy 8 bits in memory they take up an entire bite so you may think that this is wasteful and it is but with the amount of memory that we have today this is really not a big problem but if you are working on a device where memory is really a problem there are techniques you can use to pack even more data in a byte and we're going to learn about that later in the course okay so this is really all i have to say about booleans we're going to go to visual studio code and play with them in code okay here i am in the folder for my project here i am going to do the usual and copy the files over and i am going to go in my project on booleans i am going to go up and open this up in visual studio code i am going to close the current folder open the new one and browse to my project this is going to open up in visual studio code and i am going to close the welcome screen here i don't need it anymore so i'm going to remove what i don't need and we are going to play with the exact code that we had in the slides we're going to declare two variables one is going to be red light the other is going to be green light they are going to be booleans and in red light we're going to store a true and story false in green light and we can make a decision like this so try to guess what we're going to plant in this program we're going to try and decide and say if the red light is true we're going to print stop if it's not true we're going to fall in this block here and say go through if we look at our program in red light we have a true so this should really win and we should get in this block here i hope this makes sense so let's try and build this program to see that it actually works so we're going to build with the gcc we're going to run our program and our terminal just went away we're going to create a new one no big deal and uh if we do dir we're going to see rooster.exe we run it it's going to say stop okay so in red light we have true the test is going to succeed here and we're going to fall in this block and say stop i hope this makes sense you can try to store a false in here and both again going to world with gcc if we run this program now it's going to say go through because red light is not true anymore this is really what we mean when we say that we can make decisions based on booleans in our c plus plus code we can also try another example we're going to say if green light is true we're going to say the light is green if it's not we're going to say the light is not green and this is another form we can use instead of typing the entire thing comparing red light to true here and again we're going to learn about all these comparisons here it is just because i thought that talking about bulls without showing tests like this is really not going to drive the point home for you so try to understand this as simple tests you can do in math if one value is true i am going to do something if it's not true i'm going to do something else this is what we are doing here okay this program here is going to check if the green light is true so we're going to look here green light is not true so we should fall here and print this message that the light is not green let's build the program the world is going to go through we're going to run this and you're going to see go through and the light is not green this is what we have here another thing we can actually try to see is the amount of memory that a boolean takes up so we can use sizeof to do that we have seen this before if we build with gcc we're going to go through and we can run our program to actually this message and you're going to see that bull is actually going to take up one byte in memory this is what we saw in the slides another thing that can be really confusing is printing out boolean values for example here we are trying to print great light and green light let's go up and actually change them a little bit for example take green light here and make it true and we're going to go down again and here we are trying to print them out red light is going to print whatever value we have stored then and green light is going to store whatever value we have stored in and again true is going to be printed out as one and false is going to be printed out as a zero let's try and build this we're going to build with our gcc compiler the world is going to be good we're going to run this and you're going to see that red light is zero because we have a full sun here and green light is going to be one because we haven't a true here if you try to print out boolean variables you're going to get one and zero by default if you really want to see true and false there is a special setting you can apply to htdc out and you do that by saying stdc out std bull alpha and you do it like this and if you try to print again we are going to get true and false right now okay let's try and print again and we're going to work this with gcc and we're going to run this and now you see that we see red light is false green light is true this is really all i had to share about booleans in this lecture i really hope it drives the point home that booleans are mostly used in making decisions in your program and that's what you're going to be mostly using them for and another thing is that they take up one bite in your memory we are going to stop here in this lecture the next one we're going to try and learn about characters and text go ahead and finish up here and meet me there in this lecture we're going to learn about characters and text and we use a data type called car in c plus plus to represent characters and this is basically how you use it you store in characters like abc and here you see that we have a r r o w and you put your characters in single quotes like this to mean that they are characters if you don't do that you're going to get a compiler error after you have your characters declared and defined like this you can print them out with stdc out and we're going to see them on the terminal car occupies one byte in memory so it's going to take up eight bits okay so one thing we can do is map each value in these 256 possible values to a character and we can do something like this for example we can say that 33 represents the exclamation mark 48 represents the character 0 49 represents the character 1 50 represents the character 2 and we can keep going up and say for example 70 represents f 71 represents g or 112 represents p you can really use a mapping like this and come up with representations of your characters in memory and this is what we do you can learn about this encoding and on the link here this is a real thing and it is called the ascii encoding that takes characters and stores them in one byte and the data type to store these characters in c plus plus is car if this doesn't make sense please bear with me we're going to play with this in visual studio code and it is most probably going to make sense okay it is possible to assign a valid ascii code to a car variable and the corresponding character will be stored in that piece of memory so once you have that you can choose to interpret that either as a character or as a number that is something like an integer this is what we really mean if we declare a variable called value which is of car type 65 is going to be stored in memory but the c plus plus program is going to interpret this as a character if we go back to our table and see what is represented by 65 this is what we're going to find we're going to find that 65 represents a and if we print this out by default it's going to print a it's not going to print 65 but there is a way you can take this character value and turn that into an integer and print it out and you see that here we are saying value and and we are saying static cast and and in the parents we have a value this is basically a way to say we wanted to take the value and interpret that as an integer and we're going to learn a little more about static cast later in the course but it is basically a way to transform between data types and in this case we are trying to transform from car to end and we're going to print this out okay what we just described is the ascii encoding a technique by which we can map the first 128 numbers in a byte two characters and use that to represent characters this technique doesn't work well if you have languages like arabic japanese and chinese because they are completely different beasts there are better ways to represent characters in your c plus program and that's usually unicode but that's really out of scope for a course like this so i'm not going to describe that anymore okay this is really all we need to learn about characters we're going to head to visual studio code and play with them okay here i am in my working folder i am going to go in my template project i am going to copy things over i am going to put that in my characters and text project and i am going to go up and open this up in visual studio code we're going to close the welcome screen here we're going to open up main cpp and we're going to put in the code to play with characters and text the first thing we can do is declare characters like this we can put in a r r o w here and we're going to see this printed out if we run this program we are going to put in some code to play with characters we have a few variables declared that are of car type we go from character 1 through character 5 and these are the characters we are storing and this is and this is the format you're really going to use to declare your characters after we have them declared we can print them out like we print any other variable and we can try and compile this program and see what we're going to get but before we do that i would invite you to try and guess so let's open our terminal and we're going to build with our gcc task we're going to world the build is going to go through and if we run rooster we're going to see that we are printing out a r o w we are storing our characters and and we are able to print them out this is really how you work with these guys another thing i want you to see is the size that a character occupies in memory by that i mean card type it's going to occupy one byte in memory and that means that we can only have 256 different values that go between 0 and 255 if we have decided to store in positive numbers we are using the knowledge we learned from the integers lecture if these calculations we are doing here don't make sense please go back and check that out so down here we are declaring a character type the name is value and we are putting in a 65. the thing is if you try to print this value here it's not going to print 65 it's going to print a which is a character representation of 65. if you remember from our ascii table we had these mappings between characters and integers and 65 mapped to the character a upper case so if we print this out this is going to give us a but we have the option to interpret this as a number and we can pass this through a static cast and it is going to take a value turn this into an integer and we're going to be printing this integer on the console and we're going to see it we're going to try this out so we're going to run the task to build with gcc the build is going to go through we're going to run rooster and you're going to see that value is a for the first line here and the value and is 65 because we decided to interpret whatever is in this value as an integer and not as a character as it is done by default this is really all we set out to do in this lecture i hope you found it interesting we are going to stop here in this lecture in the next one we're going to try and learn about the auto key world in c plus plus go ahead and finish up here and meet me there in this lecture we're going to learn about the auto keyword in c plus plus this is a keyword that is going to let the compiler deduce the type for you and this is going to come in handy when you have longer type names that are really hard to type we can't really give you an example of that because that requires to have a good number of concepts mastered in clause plus but we are measuring this here for completeness because we are talking about variables and data types so let's hear how we can use this here we have an example where we are saying autovar one and what we initialize this with is an integer so the compiler is really going to try and guess the type of this thing and that's the type it's going to assign to variable one this is what ocho is really used for if we pass a double inside and it is a double because we have no suffix here so it is going to deduce a double if we pass a suffix of f it is going to interpret this as a fluid if we pass l it is going to interpret this as long double and if we pass in a character it's going to interpret this as a character and this is really the essence of what auto is used for it is used when you don't really want to explicitly type the type of your variable and you want the compiler to fill in that for you or it gets it for you down here you see that we have a few examples with integers so we can make this unsigned lung through this suffixes and the compiler is going to deduce the type based on what we pass in our initializer here down here you see that we have a few examples with integers if we pass a suffix of u this means that we are storing an an unsigned integer so that's what the compiler is going to deduce if we pass ul it is going to be unsigned long if we pass in ll this is going to be long long and that's what the compiler is going to deduce here you see that we are trying to print the sizes of these variables and we trying to prove to ourselves that the compiler deduced the right type and we are going to go to visual studio code and actually try this out okay here i am in my working folder i am going to open my template project i'm going to copy that and i'm going to save my files in the auto project here so i am going to go in and put in my files i'm going to go up again and i am going to close whatever it is i have in visual studio code because we're going to create a new project i am going to open my new folder we're going to close the welcome screen here and we're going to remove whatever it is we don't need for this project and we are going to put in the code we basically had in the slides i am pasting this code in to save on some time because i don't want you to see me type these things we're going to save some seconds here so the first statement here declares a variable var1 the type we don't really know but we're going to let the compiler deduce that for us and we can try and actually hover over this and we're going to see that the compiler already deduced that this is an integer if we go to vira 2 this is a double if we go to var3 you see it's a float if you go to var4 it's long double if you go to wire 5 it's a character you can see that visual studio code can even see what the compiler is going to deduce before we even compile the code this is the beauty of visual studio code we're going to look at virus 6 you're going to see that it's going to be unsigned and if we go to var 7 it's going to be unsigned long if we go to var 8 it's going to be long long this is really cool and you can see that our types are correctly deduced visuals video code was really helpful in showing us these types but we want to be sure by running this piece of code and seeing whatever size the deduced type is for example for var1 we guessed that it's going to deduce an end so it better be a four in size we're going to try and run this we're going to open our terminal here and we're going to go to terminal and run task we're going to build with gcc and by the way if you want you can build with the compiler from microsoft by coming to terminal here and choose it with world with msvc but gcc is my favorite compiler and i am going to use it if i can so we're going to hit enter and enter we're going to see that our rooster.exe file is in place if we run it we're going to see our sizes so var one occupies four bytes in memory var2 occupies for eight bytes in memory because it is double fire 3 occupies 3 bytes because it is a float var4 occupies 16 bytes because it is a long double and var 5 better occupy 1 byte because it is a character and we see the same data for vera 6 virus 7 and var 8 and this proves that the auto keyword is really doing its job it is deducing the types for our variables without us explicitly specifying the variable type we didn't have to do something like and or double or whatever we can let the compiler do that for us and this will come in handy many times this is really all we set out to do in this lecture and i hope you found it interesting in the next one we're going to look at how we can do assignments and assign data to our variables in a unified way go ahead and finish up here and we'd be there in this lecture we're going to learn about assignments and the basic idea i really want to drive home here is that after a variable is initialized like we've been doing all along you can later assign a new value to it for example here we have our one declared it is an integer we can print it out and it is going to print one two three but later in the program we can put in another value for example here we are putting in a 55 and if we print var1 it's going to be 55 this time the same idea can be extended to doubles you see here we are doing the same thing booleans we are doing the same thing here the state is declared force and we are printing that and after we are changing this to a true value and if we print that it's going to be true one thing you really need to be careful about is if you are doing auto type deduction the type is not very apparent by looking at the declaration of the variable here for example here by looking at o2 var 3 we don't really know what that is and it is very easy to try and put in a value that is illegal for example here we have deduced an unsigned integer which is only going to store positive numbers but we are putting in a minus 22 and this is going to be a disaster we're going to get a compiler error and this is going to be bad so be careful when you are doing a type deduction in your assignments that you do to your variables this is really all we have to share in this lecture let's hop over to visual studio and show you this in action okay here i am in my working folder the project we're going to be working on is four nine here assignments i am going to grab my template files and put that in this folder here and i am going to open this up in visual studio code so let's close whatever it is we have now and open the folder and we're going to close the welcome screen here we don't want it and remove whatever we don't want from this file here the first thing we want to do is put in the code for our integer variable var1 and show you that you can assign a new value to it after it was initialized and if we run this code we're going to see var1 to b123 and the second statement is going to say var1 equals 55. that's what we're going to see let's bring up our terminal and we are going to run the task to build with gcc and if we hop over here in the terminal and do dir we're going to see that our rooster.exe file is in here if we run it we're going to see var one one two three var 155 exactly what we expect we have stored in a one two three at initialization using braced initialization here but after later on in our program we have decided to put a different value in this variable here essentially reusing it to store something else this is the basic idea here we can do the same things with doubles but before we do that let's put in a separation line so that we can see these vents clearly in the terminal and we're going to go down and put in our code for a double so we have a double variable called var2 initially it has 44.50 in and then we're going to assign a new value to it and that's going to be 99.99 if we print that out we're going to see that let's work and see this in action we're going to build with gcc if we run the program now it's going to save r1 is one two three four one fifty five but we're also going to see that viral two starts out 44.55 but after we put in the 99.99 it's going to show up here okay let's put in another separation line because we're going to need that in a minute we can do the same things with booleans here we are using this as just an example but this same concept essentially extends to any other kind of variable you're going to have in your c plus plus code if you initialize it with some value you have the option to change that value later on in your program by assigning that value to that variable essentially what we are doing here the variable state which is a bull was initialized to false we're going to print that out and notice that we have this setting to allow us to see true or false instead of ones and zeros we have seen this in a few lectures back and later on we are assigning true to this state and we are printing that so if we print these things out we're going to see false and true for the state here so let's build we're going to use gcc and the world is good if we run the program we're going to see false and true and it is exactly what we expect again you really need to be careful when you are using auto type deduction because you might have some serious problems if you are not paying attention let's put in the code so that we can play with it here we have a variable called var 3 and the type of it is not specified explicitly we are letting the compiler deduce this and because our initializer here is an unsigned integer the compiler is going to deduce that it is an unsigned integer the problem will come if you try to store in a negative number the compiler is not going to give you a compiler arrow it is going to silently put in some garbage value that we don't really want in there we want a minus 22 and you can see that this is what the compiler is turning our value into we want a minus 22 in this for three but if you hover over this minus sign here in visual studio code you're going to see that the value that we have in is not what we put in we put in a minus 22 and we are getting a really large number close to 4 billion so this is really something that can go silently wrong in your code and you're going to have a hard time trying to figure out why your code is not doing whatever it is you want so be careful about this let's try and weld this so we're going to build with gcc the world is going to be good you're going to see that we're not even going to get a compiler arrow if we run this and we're going to see that viral 3 is exactly what visual studio could guess and it is not good it is this value here and the problem is that the compiler is silently putting in a garbage value and not what we want here because virus 3 is basically not capable of storing negative numbers so the compiler is going to do its best and put in a garbage value and this is not what you want and you need to be aware of this this is really all we set out to do in this lecture i hope you know about assignments and be careful when you are doing assignments on variables whose types are deduced with auto and we're going to stop here in this lecture in the next one we're going to try and recap what we saw in this chapter go ahead and finish up here and meet me there in this lecture we're going to recap what we saw in this chapter and this chapter was really all about variables and data types we had a chance to look at different types that we can use in c plus plus we saw that we have fundamental types we can use to represent integers floating points booleans and we also saw that we can do type deduction with auto we had a chance to make it clear that data is represented in memory as zeros and ones and that there are some kinds of transformations we do on these pieces of data to turn them into a form that is easily usable and manipulable by humans and we saw that for integral types or integers we could do that through number systems and we had a chance to look at a few number systems like binary octal and hexadecimal and we had a chance to play with all these variable types in visual studio code and see the nuances of how you really work with them i would like to welcome you in this new chapter and this one we're going to be looking at operations we can do on our data and there is a bunch of them we can add things up we can subtract numbers we can divide numbers we're going to see all kinds of crazy operations we can do on the data all the variables that we have in our c plus plus program we are also going to have a chance to look at how we can format stuff when we print them out to stdc out and we are going to be using some utilities from the c plus plus standard library to do some operations on our variables this is going to be a fun chapter i can't wait to get started let's go in the next lecture and start doing this in this lecture we're going to be looking at some basic operations you can do on the data in your c plus plus program and we are going to be specifically looking at addition subtraction dividing numbers the modulus operator and multiplying stuff we have seen addition before here we have a simple example we have a variable called sum and we are initializing it with two numbers that we are adding up number one and number two this is something you can do in your c plus bus program you can also do some other things for example we have another one called other sum and we are adding up some other numbers and we are going to collect the result into this other sum variable and we're going to print these things out so that you can see them on your command prompt this is how you can do addition and we're going to have a chance to play with this in visual studio code in a minute we can also do subtraction we can take a number and do minus another number like we are doing here we're going to store the results in this div variable and we can do some other things and down here you see another example in other diff here we're going to subtract numbers and store the result in other def and we are also going to print this out you can also multiply stuff this is how you can do this you use this operator here it is the star operator and this is an operator that many programming languages use to do multiplication you should get familiar with this and again we print out the result so that you can see that this is actually working we can also do division division for integers is a little tricky if you are seeing this for the first time if we take for example 31 and divide that by 10 we're not going to get fractional numbers c plus plus is going to try and figure out how many times it can fit 10 in 31 and we are visualizing this here so in 31 we can only fit 10 three times so the result of 31 is going to be three this is the meaning here okay with integer division you're not going to be getting fractional numbers this is something you should really understand we have another operator that is called modulus operator and this operator is represented by this percentage symbol that we are doing here and the result of this operation here is going to be what remains after you have done the division of 31 and 10. and here the result is going to be one and you might be wondering why this is useful it is going to be useful in many applications and i think we're going to have a chance to play with this later in the course but right now you just need to be aware of this operation and what it does okay these are the operations we're going to be learning about and playing with in this lecture let's head to visual studio code and actually play with this okay here we are in our working folder for this chapter you see that i have prepared all the lectures we're going to do in this chapter and i have my c plus 20 template project in place i am going to do what we have been doing all along so i am going to take these files and put them in the current lecture which is basic operations and i am going to paste those in and i am going to open this folder in visual studio code so let's do that pretty quick we are going to close the welcome screen here and we are going to open our main cpp file and we are going to remove things we don't need here the first thing we want to play with is addition so we're going to say that and we're going to declare two end variables we're going to call this number one and we're going to put in a two for example and we're going to do a number two and we're going to put in a seven why not and what we can do we can say result equals number one plus number two and we're going to store the result of this operation and the result variable here so what we can do is say sddcl and we're going to print the result we can try and compile this and see the result let's open our terminal here and we're going to build with gcc as we always do the world is going to go through and if we run rooster here we're going to see that the result of our operation is 9. so this is how you can do addition you can also do subtraction and here we know to declare a new variable we're going to store the result in our result variable here so what we're going to do here is say subtraction and we're going to say result equals for example let's take number two minus number one this is the operation we want to do this is number two what we are going to be storing in this result here is going to be a 5 because we are taking 7 and subtracting 2. i hope this makes sense so if we grab this line here and paste it in here so that we save on some typing time and i try to build again we're going to run the world task with gcc and we're going to run rooster again it is going to say result is 5 because we are taking 7 minus two here the result is going to be five we can also do a subtraction operation that is going to result in a negative number so for example we can say result equals number one minus number two and this is going to give us a negative number because two minus seven is going to be minus five so we're going to print that here i'm going to paste this end because i still have this on my clipboard if i bolt with gcc and run my program we're going to get -5 as a result of this operation here this is how you can do subtraction we can also do multiplication so let's go down here and say that and we're going to say result equals number one multiplied by number two and this is the operator we used to do multiplication in programming languages we're going to put in our print statement here and we're going to world with gcc and if we run rooster now we're going to get 14 because we are taking 2 and multiplying that with 7. this is how you can do multiplication so what happens if you take seven and divide that with two try to give this a guess based on what we saw in the slides we're going to go down here and do division and we're going to say result equals number two divided by number one and uh we're going to try and print this out if we build this the world is going to go through and if we run this we're going to see that result is three so to really make sense of this let's look at these numbers we are taking number two and dividing that with number one so it is seven divided by two and because we can't store fractional numbers in an integer the fractional part is going to be basically chopped off and we're going to store a three because two can fit in seven only three times i really hope this makes sense the last operation we're going to look at is modulus and it is a way to capture the reminder of a division operation so we're going to do result to play with us and we're going to say number two and we're going to say modulus using the percentage operator here and we're going to say number one and we're going to print the result of this and uh take a look at this here and try to guess what we're going to be doing what we're doing here is really seven modulus two and we're going to take seven divide this by two and the reminder of that is going to be one and that's what we're going to store in this results variable here and if we print that we expect to get a one here this is what modulus does let's build and build with gcc the world is going to go through if we run rooster here we're going to get a one let's do what we used in the slide so that this really makes sense we're going to go down here and i do equals 31 modulus 10. this is what we used in the slides so we're going to print the result here and here we are really asking what is going to be the reminder if we take 31 and divide that by 10 and that reminder is going to be stored in this research variable so here we're also going to get a one so let's weld with gcc and we're going to run the program and we are also going to get a one down here again the modulus operator can be hard to understand for beginners but it is really capturing the reminder of the division again we can see that here we are asking how many times can 10 fit in 31 and we visualize this here it can only fit in three times and the reminder is going to be this red thing you see here it's going to be one and when we do a modulus operation here we are interested in that reminder and that's what we're going to be storing in whatever variable that is capturing the result of this modules operation and that's what we basically have here and this is really all we set out to do in this lecture now i hope you have a good understanding of these operations you can do on integers we have used integers here but you can really do these operations on numbers except for the modulus operator which is really going to make sense for integral types so you can't really do modulus on floating points or fractional numbers that's not going to make any sense at all we're going to stop here in this lecture in the next one we're going to learn about precedence and associativity a very important concept for your operations in your c plus plus programs in this lecture we're going to learn about precedence and associativity so what do we mean by precedence and associativity really what we mean by this is a set of rules that we can follow to know what to do when we have multiple operators in the same expression like in the example we have here if they tell you to evaluate the result of this expression if we don't have clear rules to follow we might have multiple answers and we want to know which one is right or wrong for example one might want to do a and b first and then do c and then do d but one might want to do e minus f and do b multiply by c and then do things in different ways and get different results hopefully you are saying that this may be a big problem what we get from precedence and associativity is the rules we can follow to know which operator we have to do first for example in c plus plus the multiplication and division operator have to be done before you do addition and subtraction and if you want to evaluate this expression here you're going to have to do b and c first and then do d and e once you have those results then you can do addition and subtraction but the question remains still what do we do first if we have only addition and subtraction in an expiration and we solve that problem with associativity associativity defines if you do the operations from the left or from the right and again these things might not make sense yet we're going to do an example and you're going to really see what is meant by this to summarize precedence tells you which operation you can do first and associativity is going to tell you in which direction you're going to go from the left or from the right and some operators are going to be left associative and that means that you're going to start evaluating them from the left some operations are going to be right associative and you're going to start evaluating them from the right and i am not making these things up there is actually a c plus plus operator precedence table that you can consult to know which operators you should do first if you really are evaluating this thing with your mind okay for example if you look in this table there are many operators we haven't seen yet but that's not the problem the most important thing here is to know what precedence and associativity are and when we get to learn about these new operators you can come here and know where they sit in this precedence table for example we know how to do addition multiplication and modulus we see that the precedence for multiplication division and modulus is higher than for addition and subtraction and we can use this in our operators to know which one is going to be evaluated by our compiler first another thing i want you to notice in this table is this column here on associativity if you go back to our familiar operators here you're going to see that these operators are left to right so you should start evaluating things from left to right but some operators have right to left associativity and when we get to these operators you will start doing things from right to the left okay i think it is time we went to visual studio code and actually played with this thanks here we are in visual studio code we are in our working folder we are going to grab our template projects we're going to copy this and put that in our lecture on precedence and associativity we're going to put that in here we are going to open this project in visual studio code we are going to get rid of things we don't need and remove this here in the body and we are going to put in our test code so that we don't have to type this and save on time we have a few variables here a through g and we have an expression here or a statement that is combining multiplication division addition and subtraction and we have to figure out which operation we're going to do first i would challenge you to do this in your mind and try to guess what the result is going to be before we run this in our compiler we're going to compile this with gcc and we are going to open our terminal here and if we do durham we're going to see that we have rooster.exe if we run it we're going to see that the result is 30. how did we come up with that well the precedence and associativity table says that multiplication and division have to be done first before addition and subtraction we are going to take b and c and multiply those so b and c multiplied we're going to get a 24 we're going to try and do this in our brain d divided by a we're going to do that so d divided by e we're going to get a 3 so we're going to have a 3 here and after we have this we're going to do addition and subtraction so we're going to take a and add that to the result of b and c so we're going to say 6 plus 24 which is the result of b and c we're going to minus the results of d and e and we're going to do minus here i hope you are following and then we're going to minus f and plus g we're going to minus f which is 2 and do a plus g which is 5. okay so this is our expression here and now that we have this we have to think about how we're going to do this we can try to do this from the right do two plus five and then do three minus two plus five and go from right to left but our associativity table says that addition and subtraction have left to right associativity so we will start from the left so we're going to do 6 plus 24 we're going to get 30 we're going to minus 3 and we're going to get 27 we're going to minus 2 we're going to get 25 we're going to add 5 we're going to get 30. i hope you are following and we are really following the rules from our c plus plus operator precedence table here okay so this is really how you would use this precedence and the search activity rules to evaluate your expressions okay here i have another example for you so try to evaluate what we're going to get from this expression here we have a divided by b divided by c plus d minus e plus f try to do this in your brain and try to guess what we're going to get from the compiler if we print this out we're going to put in our htdc out statement here and we're going to try and build this with gcc of course we're going to run the rooster and you're going to get a 24. so how did we come up with that we're going to start by division and multiplication and again where do we start do we do b and c first or do we do a and b first well we can check our precedence table here and we're going to check the associativity of multiplication and division we're going to go to this entry here 13 and we see that it is left to right associative these operations here are left to right associative so what we're going to do we're going to start from left and go to right so we are going to take a and divide by b if we take a divided by b we're going to get a 2 so let's type that here then the result of that is going to be multiplied by c and if we go up we're going to see that c is eight so we're going to multiply by eight and get six so we can replace this with a 16 and we should be here then we're going to add d d is nine then we're going to subtract e which is three then we're going to add f which is a five so we have 16 plus nine this is going to give us 25 minus three okay by checking this out i see that i made a mistake f is not five it is a two we have here and if we do this from left to right because addition and subtraction are left associative we have to do them from left to the right we're going to take 16 and add a 9 we're going to get at 25 minus 3 we're going to get 22 plus 2 we are going to get 24 and that's what we have here i really hope this makes sense okay so we have seen how we can use this precedence table but sometimes you want to make it really clear which operations should be done first and you can do that by using parentheses around your expressions let's grab the first expression here and do it again we're going to copy it and go down and put that in place and we're going to store the result in our result variable and notice that we are reassigning these things to the result variable so that we don't have to declare another variable i think this is clean and that's what we're doing here so what we can do here is make it clear that we want multiplication done first by using parenthesis and make it clear that we want the division to be done first and when you do this the compiler is going to see that you want this operation here done first and that's what it is going to do and if we print result again we're going to get exactly the same thing we had before stdc out result and we're going to put result out and let's make it clear that we are using parents here to signal which operation we want to be done first we can compile this with gcc we're going to run the task to build with gcc and if we run the program we're going to see that the result is 30 exactly what we had before but now it is very clear to the reader of this piece of code that we want b and c done first they don't have to go through the precedence table to really try and figure this out potentially wasting precious time another thing you can do with this is for example what if the precedence table is not working according to your needs for example you really want a and b done first you can specify this with parents here and what this is basically going to do is going to force the compiler to do this first it's no longer going to do b multiply by c and d divided by e it's going to evaluate this first then it's going to follow the order in the precedence table that we have seen before so try to guess what we're going to get now and i am going to run the code and we're going to go through this together so let's run task and we're going to run rooster now you see that the result is 72. what is happening here well let's go through this line by line we are doing a plus b which is a 9 so we're going to get a nine here we're going to put that here as a comment then we're going to multiply that by c so we're going to do 9 multiply by 8 and that's going to yield a 72 and by that time we're going to be here and we're going to do the division first d by e then we're going to take nine and divide by three that's going to yield the three so we're going to have a three here minus 3 we're going to minus f which is a 2 and we're going to do plus g which is a 5. if we evaluate this we're going to get 72 minus 3 which is going to be 69 minus 2 it's going to be 67 plus 2 it's going to be 72 and that's what we have here hopefully you can see that using the parents not only can you make it very clear which operation you want to be done first you can also define the rules that are prescribed by the precedence tables that are defined in c plus plus i hope this makes sense and this is really all we set up to do in this lecture to learn a little more about precedence and associativity and my advice to beginning programmers is not to rely on the president's table too much it is to try and make your intent very clear using parenthesis and anyone reading your code you want to have to go check these rules in the reference c plus plus documentation they are going to directly understand what your expression is going to do and that's a good thing readability of your code is really something you should strive for this is really all we set out to do in this lecture i hope you have a better idea about precedence and associativity we are going to stop here in this lecture and the next one we're going to learn about some cool things we can do with addition and subtraction go ahead and finish up here and meet me there in this lecture we're going to be learning about prefix and postfix addition and subtraction we have an example here and it is a simple example we've seen a lot of times by now we have a variable value it is an integer and we have a 5 inside we can add a 1 to it or we can increment it by one this is another term we use in programming languages when we say we are incrementing a value we are adding something to it in this case we are incrementing it by one and we are basically going to store a six and this value here so if we print this out we're going to get a six here this is what we would expect we are resetting the value to five and that we are going to minus one or decrement by one and the result is going to be saved in value again so the second stdc out statement here is going to print a 4. this is something you should expect now another thing we can do in c plus plus is do something like value plus plus here and what do we mean by that when you use a postfix increment operator like this the first thing you should know is that this is going to increment by one it's not going to increment by any other value c plus plus doesn't provide a way for you to specify another number to increment by so value plus plus here is going to increment by one but another thing you should know is that it's not going to increment directly what's going to happen here if you look to the top here we have a value 5 when execution hits this stdcl statement we are going to grab the value here so here we're going to really print a 5 but as soon as this statement is done the value is going to increment by one so the next time somebody is going to use this value here is he's going to find in a 6 and that's what we have here so the first one is going to print a five because we're going to grab the value in here as is but as soon as we have our value this is going to increment by one this is what we mean by this value plus plus here and the second statement is going to print sx because it is already incremented and we call this postfix increment because plus plus is after the value here and this is really what this operator does and sometimes it is going to come in handy in your c plus plus applications we can also do the same thing with minus minus it is going to grab whatever value we have in this variable so we have in a five so that's what we're going to print here and the next statement is going to find it already decremented by one because after we have our five we're going to decrement by one that's what we are saying here and we're going to find the nf4 because 5 decremented by 1 becomes a 4. i really hope this makes sense ok so what we have seen here is postfix increment and decrement but we can also do a prefix version of this thing if you look at the example we have here we have a five and we are saying plus plus value so this is going to increment the value directly so as soon as this statement is executed the plus plus value statement here we're going to have a 6 inside and if we print it we're going to get a 6 and this is what you see here if we go down we are resetting our value and we are going to prefix increment in place so in here we are going to increment the value before we actually print it here and we are also going to get a six year i hope this makes sense and if you have any problem with this i think if we go in visual studio code you're going to have a better way to say this you're going to see the code running and make sense of it okay so i think that now you understand why we're going to have these numbers printed if we go down we can do the same thing with decrementing we have a value here it's five if we prefix decrement it it's going to be a four here and if we print it we're going to get a four if you go down here we are resetting that to five and we are going to prefix decrement in place and this is also going to print a four because we have a five here and if we decrement it by one we're going to get a four and that's what we're going to get here okay this is really all these prefix and postfix increment and decrement operators are about we are going to go in visual studio code and actually see this running okay here we are in our working directory we're going to do the usual we're going to copy code from our template project we're going to put that in here we're going to go up and open this up in visual studio code so we're going to remove things we don't need and we're going to remove this and we are going to put in the code we exactly saw in the slides here we have a value it is an integer and it is five if we increment it by one we're going to get a six here okay and if we print it that's what we're going to get we are resetting this to five and if we decrement it by one we're going to get a four here so if we run this piece of code we should have a six here printed out and a four down here we're going to bring up our terminal and build this with gcc the weld is going to go through and if we run the program we're going to get the value a6 and it is four and this is really what we expect here okay now that this is really clear we're going to go down and play with post fix increment and decrement i am going to put in a separator here so that we can clearly see in our terminal here and we are going to put in our test code that we are going to use to play with this let's bring this down a little bit so that you can see everything we can remove these things we don't need them anymore okay we are going to take the value we had we're going to reset that to a five and if we value plus plus here this is going to be a five because we're going to capture the value as is so here we're going to get a five but next time we get this value it's going to already be a sex because we increment immediately after capturing the value here so here we're going to get a 5 and here we're going to get a 6. i hope this makes sense and if we go down here we are resetting our value to 5 before we do any decrementation we are going to capture the value as is because we are doing post fixed decrement and here we are going to get a 5 because we are capturing the value as s but as soon as we capture our value we're going to decrement it and the other statement here is going to get a decremented value and that's going to be a 4. so here we're going to print 5 6 5 4. let's try and build this with gcc to see that we have no problems we have no problems the world is good if we try to run rooster now we are going to get the results we had before but uh for this code we just put in here the value is going to be a 5 and that's what we have here and we're going to get a 6 which is coming from here after we are going to decrement and we are going to capture the value as is we're going to get this 5 here and for the last statement we're going to get a 4 because the value is already decremented and that's the for we are seeing here i really hope this makes sense and this really covers the postfix increment and decrement operators the last thing we want to see in this lecture is that we can also do prefix increment and decrementation we're going to go down here and set up a place where we're going to be playing with this so we're going to say prefix and we are going to put in our code because i don't want to type all this that would waste your valuable time but i do encourage you to type these things out and really drill them in your brain because that's how you're going to learn okay let's bring this down so that we can understand this piece of code so we are starting by resetting our value to 5 and notice here that we do plus plus value this is going to increment value right away by this time the value is going to be six and if we print it out here we're going to have a six printed down we're going to reset it to five and then we're going to implement that in place this is also going to be printing six and if you go down we're going to do the same version of the same thing but we are going to be decrementing so we're going to decrement the value that we just reset here here it's going to become a 4 and that's what we're going to print and if we go down here we reset it again to a 5 and we prefix decrement it in place so the 5 here is going to become a 4 in here and that's what we're going to print out if we build and run that's what we're going to get we're going to run our task to build with gcc the build is good and if we run our rooster application the results for the code we just put in here are just going to show up here and you see that we get a six six this is six the other six and for the last two statements we're going to be getting fours and that's what we have here i really hope that now you have a better idea of this prefix and postfix increment and decrement operators and you're going to make use of them when they really helped you design your applications better okay so these are your tools use them when they make sense another thing that i want to emphasize here is that these operators only increment or a decrement by one you can't make them do things in increments of five or seven or ten they are going to work in increments or decrements of one we are going to see a way we can do that with other operators in the last lecture but these prefix and postfix operators can't do that and another thing that some of you might be wondering about is if we have the same kind of operators with multiplication or division and we don't have those operators in c plus plus maybe we're going to have them in the future when they make sense but in the current version of c plus plus we don't have them we are going to stop here in this lecture in the next one we're going to learn about compound assignment operators in this lecture we're going to learn about compound assignment operators and these are operators that allow you to do an arithmetic operation like addition subtraction multiplication division and modulus and assign the result to a value in one go let's look at an example here we have value here which is 45 it is an integer and we're going to print that out but if you go down here we are doing value plus equal five and if you look at the comment here this is equivalent to if you did value equals value plus five so what this is going to do it's going to take whatever value is in this value here it's going to increment that by five so that's going to become a 50 and we're going to store that 50 results back in this variable here so by this time if we printed this out we're going to get a 50. again we're going to take the value it is a 45 we're going to add a 5 it's going to become a 50 and we're going to store that result back in this value variable here it's going to become 50 and if we print here we're going to get a 50. this is the addition version of the compound assignment operator we also have a version for subtraction and we have an example of that here so it's going to take whatever value is in this value variable it is going to find in a 50 we're going to subtract 5 we're going to get a 45 and that's what we're going to store in this value here and by the time we hit this stdc out statement we're going to print a 45. again we have another version for the multiplication here so that we can really play with all these things we're going to take whatever value is in here we're going to multiply that by 2 here we're going to get a 90 and that's what we're going to store back in this value and if we print here we're going to get a 90 printed out we're going to go down and do a version for a division the value is 90 we're going to divide that by 30 and we're going to store the result back in value here and we're going to get a 30 stored in here and if we print out that's what we're going to get here we also have a modulus version of this and it works really well here we have unit 30 we're going to take it and do modulus 11 and what is the reminder of dividing 30 by 11. the reminder is going to be 8 because 11 can only fit in 30 two times and we're going to be left with the knight because 30 minus 22 is eight and that's what we're going to store in the value here and if we print out that's what we're going to get again if you have trouble understanding this modulus operator here please go back in a few lectures back we had a chance to learn about it make sure you understand it and then come back here and this is all going to make sense this is really all compound assignment is all about we're going to head to visual studio code and actually play with us okay here we are in our working directory we're going to copy our template project as we always do and we're going to put that in our compound assignment operators lecture here we're going to come up and we are going to open this folder in visual studio code the project is going to show up in here and we're going to open our main cpp file and we are going to remove what we don't need we are going to put in our code so that we can start exploring this we have a 45 and this variable here which is an integer if we print it out we're going to get a 45 and down here we are doing a compound addition so what we are doing here we're going to grab whatever value is in this variable we're going to add a five we're going to get a 50 and that's what is going to be stored in here and if we print this out we're going to get a 50. let's put in a terminal here so that we can see this running and we're going to run the task to build with gcc as we always do here and if we run our rooster executable we're going to get the value is 50 and that's what we expect here okay this is the addition version of our compound assignment operator and it is really equivalent to doing this in two steps saying value equals value plus five you're going to get the same result if you do that here actually let's do this so that you don't think that i am making this up so let's comment this out and we are going to say volume equals value plus five and if we build again run the gcc task and run the application we're going to see the same thing we can also go down and run the subtraction version of this example here here we are taking the value that we have in here which is a 50 we are subtracting 5 we're going to get a 45 and 45 is what we're going to store in this value here that's what we're going to see if we print this out so let's run the test world with gcc so that our changes are reflected in our output we're going to run this and we're going to get a 45 this is what we expect we are going to go down and do a multiplication version of this here we have our compound assignment statement we are taking whatever value we have in here which is a 45 we're going to multiply that by 2 and the result of that is going to be 90 and we're going to store that back in value here and if we print value here we're going to get a 19. let's build this run the gcc task and we're going to run our program and we're going to see that we're going to see this 90 here we can go down by a few lines and put in the code to play with a division version of this again this is going to do really the same thing only it's going to be doing this for division we're going to take whatever value we have in here which is going to be a 90 we're going to divide that by 3 we're going to get a 30 and that 30 results is going to be stored back in value here and if we print this out that's what we're going to get let's build again and the build is going to go through if we run our program we're going to get a 30. we are going to go down by a few lines and put in a modulus version of this we're going to grab whatever value we have and this variable here which is going to be 30 we're going to do modulus 11 on that value and that's going to be basically 30 modulus 11. the result of that is going to be 8 and we're going to store that result in value here and if we print that out that's what we're going to see in this output statement here we can build again run with gcc and if we run our rooster program we're going to get an 8 printed out and this is really all these compound assignments are about and you might be asking why go through the trouble to do something like this if i can do this like this well c plus plus gives you different way to do things different people have different preferences and some people me included like to do things this way because it is compact and once you use this you're going to get used to this you're not even going to think about it in this lecture here i just wanted you to be aware of this possibility because you are definitely going to see it in c plus plus code out there this is really all we set out to do in this lecture i hope you found it interesting we are going to stop here in this lecture in the next one we're going to learn about relational operators in c plus plus in this lecture we're going to learn about relational operators in c plus plus and these are basically operators you use to compare stuff let's look at a piece of code here we have two numbers one is 45 and another is 60 they are stored in integer variables number one and number two we can print them out and we're going to see them on the console or our terminal and if we go down we can really compare them we can use the less than operator less than or equal operator greater than greater or equal operator we can check and see if two numbers are equal by using two equal signs here be careful about these two equal signs because if you use one you're not going to be comparing numbers you're going to be taking the thing on the right and assigning that to the thing on the left so if you want to compare you're going to have to type two equal signs another operator we can use to compare stuff is exclamation mark and equal which is going to basically test if two numbers are equal and we're going to get the result one thing you should notice here is that we are wrapping our comparison expression into parenthesis that's because if we don't we're going to get a weird compiler error that i am going to show you when we get in visual studio code but we don't have enough tools to really understand this error at this moment so what we are going to do is avoid that problem by putting parenthesis around our comparison expression here we're going to go to visual studio code and actually see this in action here we are in our working directory we're going to grab the code from our template here and we're going to put that in the current project which is relational operators we're going to go in and put in our code and we are going to open this up in visual studio code we're going to get rid of things we don't need here we are going to put in our code so that we can explore these things we have two variables in one we have a 45 in another we have a 60 and these are integers the name for these variables are number one and number two we're going to print them out and we're going to see them on the console now we are going to compare them one thing i haven't told you yet is that these comparison operators evaluate to booleans in other words they are going to return true all false if you look down here we want to know if number one is less than number two and the result of that is whether that's true or false and this really makes sense so because of that we are using std bull alpha here to force sddc out to print this boolean value here as a true or false instead of a one or a zero and we have seen this before so if we try to build this and run this let's bring up a terminal we can use and we're going to run the test to build with gcc if we go down here and run our rooster program we're going to get our two numbers 45 and six and we want to know if number one is less than number two and that's true 45 is less than 60 so the result of this expression is going to be true now you know how to use this operator one thing i want you to see is what could happen if you didn't use this parenthesis around this expression here let's try this out we're going to remove this parenthesis and we're going to try and world we're going to run the task to world with gcc and we're going to get a really weird error because the compiler is going to try and evaluate this operator first if we go back to our c plus plus operator precedence table and look at the operators we are trying to use in the sddc out statement we're going to see that the operator to print fans on the console has high precedence than our comparison operators here so the precedence for our operator to print things to sdd out is 11 so it is higher and the precedence for our comparison operators is nine and eight so the compiler is going to try and do this operator first if we come back in visual studio code the compiler is going to try and think that you are printing number one but then again it's going to see this less than sign and things are going to blow up we don't really have enough tools to understand what is going on right now but i just don't want you to see this arrow and show you a way to go around it and move on with your life by comparing stuff in your c plus plus programs if you put parentheses around here the problem is going to go away let's try and build again we're going to build with gcc and everything is going to go back to normal and we're going to see our output if we run our rooster program here this is how you can do this okay now that we have a basic understanding of this we can try the other operators and it is going to be a walk in the park we are going to go down here and put in our remaining operators we can use less or eco operator to test and see if number one is less or equal to number two we can use the greater than operator to test and see if number one is greater than number two we can use the greater or equal operator we can use the equal equal operator to test that number one is equal to number two and we can use this operator here which is made up of the exclamation mark and an equal sign to test and see if two numbers are not equal and all these operators are going to evaluate to a boolean basically saying if what you are testing for is true or false and we can print this out and notice that we have this parenthesis around to avoid the problem i just showed you a few minutes earlier and if we run this we're going to see the results let's weld this with gcc as usual we're going to run our program and you're going to see that number one is less than number two this is true number one less or equal to number two this is also true number one greater than a number two 45 is not greater than 60 so this is false it's not greater than equal so this is also false number one equals to number two this is going to be false and uh the last statement that is going to test and see if number one is not equal to number two is going to evaluate to true because number one is not equal to number two if this is not making sense yet please go through it a couple of times and if you have a problem please do ask me i will do the best i can to help you out another thing i want you to see before i let you go is that you can store the results of these operators into a variable and use that later in your program for example here we can take our expression to compare number one and number two the result of that is going to be installed in this bold variable and we can print it out later like this if we run this we're going to build with gcc and we're going to run the program we're going to see that 45 equals to 60 is false and it is coming from this statement here you can try and change these numbers for example change this to a 20 and see the results that we get and you can try and run these in your mind and see that they really make sense if we run rooster now we're going to get to different results because we are now comparing 45 and 20 and here at the last statement you see that they are not equal if we put into equal numbers we're going to get a true there let's try this out we're going to run the task to build with gcc and if we clear so that we get rid of all this noise and run rooster we're going to get 20 is equal to 20 and this is true this is really all we set out to do in this lecture i hope you have a better idea of these relational operators in z plus plus we're going to stop here in this lecture in the next lecture we're going to learn about logical operators in c plus plus go ahead and finish up here and meet me there in this lecture we're going to learn about logical operators these operators will work on boolean operands we're going to look at end first to really make sense of how they work here is a simple table we can use to try out the end operation it is going to be working on two operands a and b and in the third column here we're going to have the result so if a is false and b is false and we do a and b the result is going to be false if a is false and b is true the result is going to be false if a is true and b is false the result is going to be false if a is true and b is true the result is going to be true the way you can think about this and operator is that if any of the operands is false the result is going to be false and if you really use this way of understanding this it is going to be really easy and the first line here we have a false so the result is going to be false and the second line we have a force it's going to be force in the third line we have a false the result is going to be false but in the line number four here we don't have a false a is true and b is true so the result is going to be true another operator we have is or and it is going to evaluate to true if any of the operands is true let's look at the first line here a is false and b is false so we don't have a true the result is going to be false on the second line we have a true the research is going to be true on the third line we have a true the research is going to be true on line number four we have a true the result is going to be true and you don't really have to memorize these tables if you use these operators long enough these things are going to be drilled in your memory if you have any problem with these operators just look up tables like this and you can find whatever it is you need without really memorizing these things another operator is the not operator which is going to negate whatever you give it so if a is true and we negate that we're going to get a false if a is false and when you get to that we're going to get it true and we use this exfoliation mark to say that we are negating something if you go back to or it is denoted by these two vertical bars and the end operator is denoted by these two ampersand symbols here okay now that we know about these logical tables for these logical operators we can actually try to use them in code here we have three boolean variables we have a b c a is true b is false c is true we can print them out and you see that we are setting up sddc out to give us the results in the format of true and false instead of ones and zeros and down here you see that we are trying to use the and logical operator down here you have a and b a and c and we are doing this on three operators and chaining these things up so we are saying a and b and c and let's try and do this in our mind to see that we really understand this we have seen that the end operator evaluates to true when all operands are true another way to say this is the end operator evaluates to false if any of the operands is false so we're going to end a and b a is true b is false so the result is going to be false because we have a false in here a and c is going to evaluate to true because a and c are true a and b and c is going to evaluate to false because we have a false in here even if you have 1 000 tools and you end that with one false that's going to be dragged down to evaluate to false that's what we are seeing here we can also do all operations on these things and this is going to give you a true if any of the operands is true we know that a is true and b is false so this is going to evaluate to true a and c is also going to evaluate to true because a is true a and b and c is going to evaluate the true because a is true so let's go back and see this these are the values we're going to be using here we can also try out the nut operator and it is going to negate whatever we gave it so if a is true we're going to get a false b is false we're going to get it true c is true we're going to get a false another thing you can do is combine this logical operators so we can do something like a and b and all of that with a c and you should notice that we are wrapping our expressions here in parenthesis so that we don't have the same problem we experienced and explained in the last lecture and one thing you should know is that for the compiler to evaluate these things we are going to follow the rules in our table for precedence and associativity but we used parentheses to really make this easy to follow so we are going to end a and b and the result of that is going to be ored with c and you can try and do this in your mind and you're going to figure this out but we're also going to go to visual studio code in a minute and we're going to see all these things in action the last thing you can do is combine logical and relational operators into expressions for example here we have d e and f they are integers and we can print them out like we are doing here but we can do something like d greater than e and we're going to end the result of that with d greater than f and we know that these relational operators evaluate to booleans so we can use these booleans with logical operators like we have been doing all along in this lecture so for example let's evaluate the first one here in mind so that you really see what this is all about d is 45 e is 20 so d greater than e is going to evaluate to true d greater than f that's also going to evaluate to true so true and true that's going to evaluate to true and this should print a true on the terminal we are going to go to visual studio code and actually play with this thanks okay here we are in our working directory we're going to jump into our template project and copy the files we need and we're going to put them in our lecture on logical operators we're going to put that in here we're going to open up this folder in visual studio code and it is going to show up here in visual studio code we're going to get rid of what we don't need and we are going to put in the code to play with these things we have three boolean variables a is true b is false c is true and we can print them out and we're going to see their values and you see the setup here so that sddc out is going to show thanks in the format of true and false and down here we are ending a few values we are using the and operator and it is denoted by these two ampersand signs so a is true and b is false and c is true a and b is going to evaluate to false because we have a false in here and again if this doesn't make sense please do consult the tables for all these logical operators and all these things are going to make sense this is going to evaluate to false because we have a false in here and a and c we have true and true so this is going to evaluate to true and a and b and c this is going to evaluate to false because we have a false in here remember as soon as you have a false in your and logical operation you're going to get a false in your result so we're going to have a false here we can try and compile this and see if this is actually what we expect we're going to bring up a terminal we're going to use here and we're going to run the tasks to build with gcc and if we run our rooster program we're going to see that a and b is false this is what we expect a and c is true this is what we expect a and b and c is false and this is what we expect this is how you can really play with your and logical operator if you want you can change things up to really try and see if they live up to your expectations now we have changed a to false let's try and build again we build with gcc and we're going to run our program and we're going to get a and b is false because they are all false a and b are false so false and false evaluates to false a and c is false because a is false so we're going to get a false as the result a and b and c is also going to evaluate to false okay we have this tension here let's bring this back to what it was before so a is going to be true b is going to be false c is going to be true and we're going to try and play with the or operator we are going to go down and put in code to actually play with this and it is really nothing special we are just using the or operator here which is denoted by these two vertical symbols and or is going to evaluate to true when at least one of the operands is true okay so we have our values here a is true b is false c is true a or b is going to evaluate to true because a is true a or c is going to evaluate to true because a is true a or b or c is going to evaluate to true because one of these things is true and a is true here we can try and run this we're going to build this first with gcc and if we run this we're going to go down here and run rooster again and you're going to see that all these things are true here because one of these things is shown and as long as you have one true operand your or operation is going to evaluate to true and again this is following the precedence and associativity rules we have if we go back to our table in our browser we're going to try and look at logical or it is left associative so we're going to try and evaluate this from left to right so we're going to or a and b and the result of this is going to be ord with c we have also seen the not operator which is denoted by the exclamation mark here and what it is going to do is basically flip off or negate whatever it is you have if you have it true in your variable the result is going to be false if you have uniforms the result is going to be true and not a is going to be false because a is true not b is going to be true because b is false and not c is going to be false because c is true as we can see here so let's run the application and see if this is what we expect we're going to run and build with gcc and we're going to run rooster and we're going to see that note a is false not b is true and not c is false and this is what we expect we have also seen that we can do combinations of these operators so for example we can look at the expression we have here we're going to take a and b this is going to evaluate to false because b is false we're going to negate this and we're going to get a true and we're going to order that with c but because it's true we know that the result of ordering this with anything is going to be true so we expect this expression here to evaluate to true if we run this we're going to see that this is going to evaluate to true and it is what we expect the last thing we want to see in this lecture is that we can combine these logical operators with the relational operators we have seen in the last lecture we are going to put in a piece of code to play with this this is lengthy but you can play with this and see what this is going to evaluate to to start you off we are going to do the first one together so it does test if d is greater than e and if you look here 45 is greater than 20 so this is going to evaluate to true and we're going to enter that with d greater than f the result of this is going to be true so we're going to be ending a true and a true and the result of this is going to be a true and again you notice that we are wrapping this in parenthesis to avoid problems with the compiler being confused with what we are trying to print this is going to get rid of the otherwise ugly problems we might have to deal with if we didn't wrap this into parentheses like this so we expect to see a true here and we're going to weld and run the task to build with gcc and we're going to run rooster and if we look at the first thing here this is going to be true and you can try and do the other ones in your mind as an exercise and if you have a problem don't hesitate to ask me i am going to do the best i can to help you out this is really all we set out to do in this lecture i hope you have a better understanding of these logical operators again you don't have to really memorize them all you have to do is consult the tables and you're going to basically know the result of your two operands depending on the values you have inside for and or and the not operator we are going to stop here in this lecture in the next one we're going to see how we can format our output to make it really look better go ahead and finish up here and meet me there in this lecture we're going to look at output formatting and c plus and this is the way we can format stuff we send it to the terminal through sddc out and make them show up better as an example let's say we have a table here that has names and ages and you see that they are cramped and through output formatting we can take this and turn this into this and hopefully we agree that this is better there are a set of tools we can use in c plus plus and those we're going to be using in this lecture are going to be coming from these two libraries ios and io manip and there are a lot of things in these two libraries here is a a set of cards that show a lot of them we are going to play with a lot of these in this lecture so you don't have to worry if some of them seem scary right now before we start looking at them i want to bring to your attention that there is a piece of documentation on these things the link is here the website is called cppreference.com and you can open this up and look at some of the things we're going to talk about in this lecture okay the first one we're going to look at is stdendl this is not new we have been using it all day long to print out a new line character so that our htdcl statements print things on different lines and if we don't have this manipulator you see here on the top we have hello and world and if we print them out they are going to show up on one line because we are not separating the lines with std e and dl down below here we have another way we can do this we can post a backslash n after our text and that's going to add a new line character just like std endl does okay stdndl and slash n in here seemingly do the same thing but they are different but we don't have enough tools to really understand the differences right now for now just think of them as two things that do the same thing they print out a new character that allows your text to show up on different lines like we have hello and world here okay stdndl is the first manipulator we're going to play with in this lecture the next one we're going to look at is sdd flush and this is going to send whatever is in the output buffer to the terminal and what the heck is an output buffer let's look at an example suppose we have sddc out here and we want to print on this terminal and the message we want to print is hello world how are you so what is going to happen if the sddc out statement is executed things are not going to be directly sent to the terminal they are going to go into an intermediary buffer you can think of this as some kind of storage in which data goes before it goes to the terminal and this is what's going to happen we print out hello hello is not going to go to the terminal directly we print out world it's not going to go to the terminal we print out how are you it's not going to go to the terminal and when the buffer is full the data is going to be sent to the terminal in one go and this may come in handy in some situations so if you want the data to go to the terminal directly you're going to add an sdd flash after your hddc out statement and we are going to play with this in a minute in visual studio code and you're going to see how this works another manipulator we have which is really cool is set with and it is std set w here and what it does it specifies a width for whatever text you want to print here we have two examples the first one is our table that is not formatted well and you see we just print out the data and it is going to show up like we see here on the right we can use std set with to set the width of the text here and for example the last name here is going to show up in a field which is 10 characters white this is what we mean here and we do the same for the first name and the age and if we do this data is going to show up nicely as we see here and again i hope we agreed that what we have down below here is much better than what we had here which was not formatted with set width and this is how you use this manipulator here we can also use what we call justification to control whether our data shows up on the right or on the left in the width that is allocated to it for example here the justification is sdd right so the data is going to show up to the right in its respective cells we can also use what we call internal justification by which if we are showing a negative number the sign is going to be left justified so it is going to show up on the left but the data is going to be right justified and this may come in handy if you want to display some tabular data in your terminal we can also use std set field to specify the field character in our empty spaces for example here we set the dash as a field character and the result is what you see down below here we have seen std ball alpha and its effect is to force stdc out to show boolean output in the form of true and false if you don't put this manipulator in you're going to see those pieces of data as ones and zeros so for example here we have two variables condition and other condition if we print them out they are going to show up as one and zero we are going to set our manipulator here so the data is going to show up as true and false we can disable this manipulator here with std noble alpha and the data is going to go back to be shown in the form of one and zero this is what this manipulator does we also have one to show the positive sign for positive numbers so if we don't enable it the data here is going to show up like this 34 and minus 45 you see that the minus sign is going to be shown regardless of the setting you have for this manipulator but if we enable std show pose we're going to show the positive number and this may make sense for whatever application you might be doing we can disable this with std no show posts and after we do this the data is going to show up in its default format that we have seen before this is how you use std show posts we also have manipulators to control the base in which the data is shown and we can show the data either in a decimal in octo or in hexodecimal if you look at the data here we have an integer which is positive we have an integer which is negative and we have a double variable here and we have some data n for us to play with we're going to show our positive integer in these three bases decimal hex and octo and the result is what you see here and this should be no surprise you already know these number systems and how they work we can take the negative number and also show it in different bases decimal hexadecimal and octo and it is what you have on the right here in our terminal if we go down and try to show a double or a floating point number in these bases the result is not going to change because these number systems have no effect on floating point numbers if you remember we have seen that floating point numbers are represented in memory following another protocol that is really advanced for a course like this so we are just going to use this but we're not going to try and understand it because it is out of scope for what we are doing here we can also use std show base to show the base of our output so here we have a positive number which is an integer and we can show it in decimal hex and act and we're going to see what we see here to the right but we can choose to also show the base of whatever it is we output if we want to specify that the output is in decimal we're going to say that here but we can also decide to show the base and we enable that with std show base like this and if we do this we're going to see decimal is not going to have anything in front hex is going to have a 0x in front oct is going to have a zero in front and by this it is going to be very easy to see the base in which your data is displayed we can also use std upper case for the data we print out to be shown in uppercase where it makes sense for example here we are trying to print numbers in hexadecimal here in the middle so if we don't use uppercase which is the default the number is going to show up like this you see a and f are lowercase but if we enable uppercase we're going to see this in uppercase and sometimes this is going to be helpful for whatever application you are using the sun we also have a few manipulators to control how floating point data is shown on the terminal and the two of these are std scientific and sdd fixed by default stdc out is going to show in scientific format where necessary and it is going to use fixed by default so what we have here we have three variables abc and if we just print them out they are going to show up in this format here if we use fixed we're going to force the output to be in fixed format and you're going to see that the third number in c here is going to show up as 0.0 and a few zeros after that because in the precision that we have now we can't really show a number that is as low as we have here in c again we have seen this scientific format this is basically 1.34 multiplied by 10 to the power of minus 10 and this is a really really low number so we're going to do 0.000 up to 10 and that's when this decimal point is going to show so it's a number that is really close to zero that's why we are showing it like 0.0 here i hope this makes sense we can also use scientific notation if we want that and if we force scientific notation everything is going to be shown in scientific notation another thing that i really want you to be aware of is that there is no official way to set the floating point output to its defaults and here is a hack i actually learned about when i was making this course the explanation of this is really out of scope for this course but just use this to set the floating point output format to the defaults and if you do this you're going to get whatever we had here with the defaults you're going to see that it is the same things the next thing we're going to look at is std set precision which we have seen before and this is going to set the precision with which the data is going to show up on the terminal if you use a precision of 10 that's what you're going to get here here you see that we have a double number it is a number that has a lot of numbers after the decimal point if we show it by default it's going to show up like this the precision is going to be sex we can increase the precision to 10 to 20 and 50 and it is going to show up at whatever precision that you specify here please know that this precision is going to be limited by the type of this a variable if it is a floating point the precision is going to be something like 7 we have seen that if it is a double the precision is going to basically double to something like 15 and if you have a long double the precision is going to be something bigger than double but it can't go below that of a double we have seen this before we also have show point which is going to force showing the decimal point for floating point values you see here we have a 12.0 this is the most important piece of data we have here if we don't do show point this is going to show up as 12 in the output here but we can use show point to force for the decimal point to be shown and you see that 12.00 here is shown another side effect of this is that show point is going to force the showing of trailing zeros and it is what we see here in d e and f we have seen a lot of manipulators here is a table that is going to show where many of these are going to be living so if you need to use set width this is going to be living in the i o manip header and here we have a few other ones so if you want to use a few of these things these are the libraries that you will need to include with pound include as we have been including i o stream and again the documentation for these things is shown at cppreference.com this is the exact link and you can check this out if it helps out now that we have an idea about these things we're going to go in visual studio code and actually see these in action okay here we are in visual studio code the project we're going to be working on here is output formatting so we're going to be grabbing our template files and put those in our project here and we're going to go up again and open up this folder in visual studio code let's do that we're going to close the welcome screen here and we're going to remove whatever it is we don't need we're going to take this out and we are going to put in the first code we want to play with and this is stdendl you don't really need a lot of explanations for this because we've used this all along if you don't put it out the data is going to show up on one line and uh you're going to have hello world on one line if we put stdndl like this we're going to have the data show up on different lines so we're going to have hello and world on different lines and it is possible to use a backslash n to achieve the same thing but these work very differently underground and we don't have enough tools to really understand the difference so for now just take this as the truth and i know that there are differences and the most important thing is that you recognize these things if you see them in code out there we are going to bring up our terminum and build this with our task that is going to build with gcc you're going to see that the world is going to go through and that we can run our rooster binary to see the output you see that we have hello world on one line we have hello and world on different lines and this is a result of these events we have here you see we have stdndl this is going to force the terminal to go to the next line and print world and after that we're going to force the terminal to go to the next line and that's where the next text is going to show up after that we have hello and world down here in the terminal and it is the output generated by the statements that we have in here and you can see that it is going to basically do the same thing as stdendl but again they work very differently under the hood okay so this is really how std endl works and you get access to it by including the i o stream library here the next thing we're going to play with is std flash this is going to flash the output buffer to its final destination and that's going to be the terminal in this case and if we add that to our stdcl statement like this this is going to be directly sent to the terminal and this may come in handy in some applications if we run this we're not going to really see a difference but underground under the hood the data is directly sent to the terminal and we had the chance to explain this in the slides earlier we can work this program and i see it running the world is going to go through if we run the rooster we're going to see that this is a nice message is sent to the terminal the next thing we're going to look at is set with i am going to put in the code here because i don't want to type all this and again you can get the source code for this in the results section of this lecture you don't really have to type all these things the most important thing is that you really understand how they work okay you see here we are going to use set with but we haven't really included the library that is going to give us access to this and if you don't know which library you will need to include to get access to this well you can check the documentation and to get access to the documentation you might want to consult your favorite search engine i am going to go in google here and type in std set w and i am going to search and this is going to come up with a few entries i am going to click on the one from cppreference.com because this is reliable in my opinion and if we open this up we're going to see that to work with us you need to include i o my net so this is what we need to put in visual studio code to get rid of this problem let's do that i really wanted you to see this process you can go through to know what you need to include we're going to put this in and hopefully the arrow is going to go away and we can focus on studying the effects of these things here this is going to print and formatted so the data is going to be cropped close to each other but we can format this using set width and if we pass in a 10 here we are basically saying last name is going to take the width of 10 characters even if we don't really have 10 characters in this so so you're going to have spaces fill the remaining spaces so that the data shows up nicely this is really hard to say in world so i am going to run this in a minute so that you can see this but before we run i want you to see that you can also store the width information in a variable like this and i use that instead in your std statements the benefit with that is that it is really easy to change this with if you happen to change your mind otherwise you would have to change a lot of things and this is really bad design i did this to show you how you can use this but in real code you are most probably going to store your weft information in a variable and that way it's going to be really easy to change and update if you happen to need that we can build this program with gcc and if we run it let's bring this up a little bit we're going to have our data that is unformatted and you see that it is really ugly but down below here we have our formatted table and it is really nice we have last name first name and age and it is easier to see these things we're going to see that it is possible to force the data to show on the left or to the right with justification and we're going to look at code that does that next if we go down here and put in the next piece of code we want to play with we're going to see that data is right justified by default but we have seen this also you see that gray is cramped to the right of the width of the cell for the first name so data is right justified by default and it is the same here if we change the width of our data here and trying to run we're going to have a better visualization of that so let's run a little bit we're going to build the world is going to go through and if we run this we're going to see that data is going to show up to the right gray is to the right woods is to the right and this is the default justification you get when you set with like this you can also cause for the data to be left justified and you do that by coming here and changing this to left you can do that but let's keep this in here instead for your reference and let's put in another piece of code to justify the data to the left okay let's go down and do that we're going to justify the data to the left and if we print this out the data here gray should come to the left and be aligned with the first name here i hope this makes sense we're going to run we build with gcc and if we run rooster gray is going to be justified to the left this is the meaning of std left and std right that we just saw and know that data is justified to the right by default and it is important to keep the defaults in your mind data is going to be justified to the right by default the next thing we can do is to make our data internal justified and what this is going to do is make the sign left justified and the data is going to be right justified and the best way to save this is really to see an example so here we have the data it's a negative floating point number the width is set to 10 it's going to be right justified but we are also going to set the justification to internal and see the difference let's weld and run this we're going to world with gcc and we are going to run our rooster program and if we look at the output here you see that if we have right justification the sign is going to go with the data whereas if we do internal justification the sign is going to go to the left and the data is going to go to the right and sometimes this is useful depending on the application that you are welded especially in applications where you have to display tabular data on the console or of the terminal we can also specify the field character using set field and this is really how it works the data is going to be left justified but we are going to fill empty spaces with a dash here and we can bring this up so that you see the entire thing but you don't really have to type this out you can use the code i have provided in the resources section of this lecture you can just copy it paste it in here and use it to play with these things okay with this our empty spaces are going to be filled with this dash character let's world and build with gcc we are going to run this and you're going to see that we have a field character here and you can change this basically to whatever you want if i want i can change this to a star let's do that and i can build again if i run this it's going to be filled with stars right now the next thing we're going to look at is std bull alpha we have seen this before but what it's really going to do is force the output to be in the format of true and false and the default is in the form of one and zero here we have a few variables and that they are of bull type the first set of stdcl statements here is going to print in the format of one and zero we are going to set the output to both alpha and it is going to be in the format of true and false and then we can disable the bull alpha with this setting here which says hdd no pull alpha we can vote this and see the effect of this so we're going to run rooster again and you're going to see that condition 1 0 condition true false condition 1 0 we are able to modify how our boolean data is displayed on the console using these two settings here use them if they make sense for your application the next thing we're going to look at is show pose and we're going to just put in the data we have two numbers positive number which is 34 a negative number which is minus 45 and we can decide if we want to show the plus sign for positive numbers by default the plus sign is going to be hidden it's not going to be shown if you want it shown you can use the show pose manipulator and if you want to disable that when it was enabled you can use no show pose and this is going to do what you want we can build this again and if we look at the output we're going to see that post num doesn't have a plus sign in front because by default the show pause is disabled and we set the setting here and we're going to see the plus sign and if we disable that we're not going to see the plus sign again this is the effect of std show posts and std neutral pose manipulators we can also affect the base in which the data is displayed on the terminal and we do this by setting the output base system we can set std deck to force the output in decimal std hex to force the output to be in hexadecimal and std act to force the output in octal and this is only to take effect for integral types this is not going to have effect for floating point numbers for example here we have a few variables a positive integer and negative integer and a floating point number which is the double and we're going to try and print these things in all these base systems we're going to print these things in the default format we're going to see how they look we are going to take the positive number and show that in these different number systems we're going to do the same for the negative number and we're going to show that this doesn't have effect on floating point numbers because these number systems don't really work for floating points we can run this and see the output we are going to run the task that is going to build with gcc and we're going to clear and run rooster and what we're going to see is that by default positive number is going to be this negative number is going to be this and our double var is going to be this and we are going to take the positive number show it in different base systems in decimal it's going to be like this in hex it's going to be like this in act it's going to be like this the negative number is also going to be in this format here so if we show it in decimal it's going to be like this hex it's going to be like this and octo it's going to be like this you can see that the double var which is a floating point is not really affected it's going to be showing in the same format regardless of the base that we set here one thing you should know is that this base setting here is going to stick so here for example you see that we have set output to octo so any other thing that we're going to print out is going to be shown in octo and we have to be sure that's what we want if that's not what we want we will have to reset the output number system and it's going to take effect this is how you can use these things to affect different number systems for output in your terminal we have seen that you can also force things to show up in uppercase if that's important for your application for example here we have a positive number if we show that in hex this is going to contain a few characters and if we don't set uppercase it is going to show up in lowercase and if we want it in uppercase we can set this setting and it is going to show up in uppercase this may be helpful in some applications and i just want you to be aware of these things we're going to world and run our application we're going to see that let's bring this up a little bit so that we have some breathing room our positive integer here is going to show up in lowercase here in hex because this is going to contain characters and if we set uppercase you're going to see that the characters are going to show up in uppercase and this is the effect of this setting here we can also affect how floating point data is printed out on the terminal for example here we have a few variables a b and c and by default they are going to show up with decimal points and use scientific notation where necessary but we can choose to use fixed for example if that's what we need in our application and the output is going to be shown using fixed notation with decimal points if we want we can force the scientific format and we do it like this and all output is going to be in scientific format and we can disable all these settings and reset this to the defaults with this piece of command here there is no official way to set floating point output to the defaults and this is a hack i actually learned about when i was preparing the code for this course and the proper explanations for this are really out of scope for this course so if you want to reset the floating point output format to the default you're going to use this but we don't really have enough tools to understand this yet so just take it by faith and you're going to see that if we run this it's going to work let's try and run this through the compiler and that the world is going to go through if we run this and i bring this up a little bit so that we have a little bit of breathing room we're going to see that our double values are a b and c and that's what we have in our code here and by default it's not going to be able to show these things because of the precision limits so we're going to have 3.14159 it's going to go up to here and the rest is going to be chopped off if we use fixed formats you're going to see that whatever we had in scientific format it's going to show up in fixed format and again this is a really low number that's why we have 0.000 here we can force the output to be scientific as we did right here and all output is going to be in scientific format down below here we use our hack to set to the defaults and you see that it's going to go back to the initial format that we had before this is how you use std fixed and std scientific to manipulate how your floating point numbers are printed to the terminal we have also seen that we can set the precision of our output in the terminal here we have a variable a it is of type double as you can see in a visual studio code here if you hover over a variable you're going to be able to see its type we can choose to see this in whatever precision we want 6 is the default in our case here but we can choose to set the precision to 10 20 or whatever it is really we want please note that this is going to be limited by the precision that your type supports so for example we know that a double has a precision of 15 and if we have more than 15 digits in here the rest is going to be garbage just be aware of that we're going to try and build this and we're going to use gcc as usual and if we run this we're going to get our numbers in different precisions the first one is using the default which is going to be 6. if we use 10 we're going to get 10 digits if we use 20 we're going to get 20 digits but if we go over the precision that our type here supports we're going to get garbage i hope this makes sense the last thing we're going to see is show point and we have seen that this is used to force outputting the decimal point let's say that here and a good example for that is this 12 here if we print it out here we're not going to get the decimal point by default it's going to print 12 but if we go down and force output of the decimal point here we're going to get something like 12.0 and another effect of this is that it's going to add trailing zeros to fill whatever space is allocated for your output variable in the terminal let's try and run this so that we can actually see this running we're going to run our rooster executable and in the first group here we don't have the setting for show point and if you look at the numbers that we had in our code the first number is 34.1 and you see that we are padding a lot of numbers because we had set our precision to a really big number in the last example that we have run let's actually go there and show you that that's why we're seeing the precision of 20 it is really huge but no big deal we can reset this to whatever we want if this is too much for us the important thing here is that we have 34.1 and that's what we are seeing and if you go to our 12 here you see we have 12.0 but our output is not showing the decimal point here if it is important to show the decimal point we can do std show point here and it is going to show up this is really what is important in this piece of code here and this is really all we set out to do in this lecture we had a chance to play with a lot of these manipulators and the best way to learn about these things is really to try them out in code and try to change a few things and see how that affects the output you get in the terminal and we had a chance to play with many of these things in this lecture there is a lot of these guys i tried to make this lecture as extensive as i can that's why it turned out to be pretty long but once you've learned about these fans and played with them in code you're never going to forget them one last point i want to emphasize again is that the documentation is of utmost importance it is really good to come here and check things out for example if we go to show point here we are going to get a nice explanation of what this manipulator does enables all disables the unconditional inclusion of the decimal point character and you can read all this and the nice thing is that if you go down you're going to have a piece of code that you can use to really understand whatever it is they are talking about and this is priceless if you are learning it is a good thing to be able to use the documentation this is really all we set out to do in this lecture it turned out to be really long but i really hope it was worth it for you we're going to stop here in this lecture in the next one we're going to learn about numeric limits go ahead and finish up here and meet me there in this lecture we're going to be playing with a few functions from this library specifically we're going to be looking at the minimum the maximum and the lowest functions and this is really how you use them you can think of this t as a placeholder for the type for which you want to know the minimum the maximum and the lowest point respectively i know this might be cryptic to some of you so we're going to look at a few specific examples here we have a simple explanation of what these functions mean for floating point values the minimum is going to represent the smallest positive number that is representable with that floating point type the maximum is going to be the maximum floating point number you can represent and at the lowest number is going to be the lowest negative number you can represent with that type things change a little bit when you start dealing with integers and these functions are going to look like this the minimum is going to be the minimum number you can represent with that integer and lowest doesn't really mean anything for integral types the maximum is going to be the maximum number one thing you have to keep in mind is that the minimum may be negative if you are using a signed integer if you are using an unsigned integer for example for short minimum is going to be zero and the maximum is going to be the maximum number representable with that integer type here we are looking at numbers for short and but the same story applies to whatever integer type you are using even if you happen to be using long end or end without any modifier remember that we had to do a lot of things to get this range and it was really hard to even do this for floating point types so this library is helpful in getting a sense of what we can represent with the given type b8 floating point or integral let's head to visual studio code and play with us here we are in our working folder we are going to copy code from our template project and we are going to go in the current project which is numeric limits we're going to go and put in our files go up again and open this file up in visual studio code we're going to close this up open the main cpp file remove whatever it is we don't need we are going to include the limits library remember this is the first thing we need to do if we want to play with these facilities that we just looked at in the slides and we are going to go down here and put in the code we want to play with and it is ridiculously simple if you have watched the slides we just did in a minute so what we're going to be doing here we're going to be printing out the range for each possible type we want to play with here so we're going to say the range for short and is from and we're going to say std numeric limits short that's what we want to deal with now and we're going to call the minimum function this is going to give us the minimum value we can represent with short and we're going to say std numeric limits and we're going to call the max function passing in the type for which we want to get the range for and the same story applies for unsigned short you see that's the type we are using here we are also going to play with ant getting the range we're going to use unsigned and to see how these things behave we're going to use long and get to the range and we are also going to be playing with floating point types so we're going to compute the range using the minimum function and the maximum function and we are going to be also using the lowest function to see that this really is the same thing we described in the slides we're going to do this for double and long double and see how these ranges come up in this program what we're going to do is open up a terminal here we are going to run the task to build with gcc and the build is going to be good and we can go down and run rooster and we're going to get our values here the range for short and is from minus 32 768 up to 32 767 and you see that this is exactly the same thing we saw in the lecture where we talked about number systems we saw a way you can do this manually by plugging the number of digits in some formula that we came up you can go back and check that out if you want we do the same for unsigned short and and then we look at floating point types we're going to print the range for float and if we use the minimum function to get the start of the range we're going to get the minimum positive number we can represent with a float if we use lowest we're going to get the lowest minimum number and that the maximum is going to be the same if you think this is really small you have the option to open up this folder in file explorer and you can open a command prompt or powershell window by hitting shift and right clicking and choosing this option here open powershell window here and you're going to get a bigger window in which you can type things we're going to run rooster and we're going to see things in a much easier way here this is an option you can use or you can even use the integrated terminal inside visual studio code here like we've been doing all along there are a few other facilities you can use in this library of limits for example we can try and print out if a number is signed or unsigned or get the number of digits we can represent in an integer type these are a few examples i just chose because they are easy to understand and really helpful but you can find more about this library in the documentation and if you come here at cppreference.com the link is here you can really find all about this library here this library provides a standardized way to carry various properties of arithmetic types for example the largest possible value of type and is this and we have a bunch of things we can do here don't be scared of by this template thing just think of them as placeholders for the type for which you are trying to query information for there are a few functions you can call on these things for example there is mean lowest max that we just played with in visual studio code but uh you can see that there are a lot of things you can play with especially if you have some math background to understand all these concepts here you can play with us and see if it is helpful for whatever application you are doing okay this is really all we set out to do in this lecture to learn a little bit about the limits library it is helpful in trying to understand the ranges for your types if you happen to need these kinds of things this is going to come in handy we're going to stop here in this lecture in the next one we're going to learn about some math functions we can use in c plus plus go ahead and finish up here and meet me there in this lecture we're going to learn about math functions and these are functions that are built into the z plus plus standard library that we can use to do some math if you are doing some math operations you're going to find this helpful they live in the c math library and if you want to use them you're just going to include that and you're going to have access to them what you see here is a simple summary of some of the things you can use in these math functions you can use std flower to round down or hdd seal to round up you can use the abs function to compute the absolute value of a number you can do trigonometry you can do the cosine sign and things like that you can compute the exponential of a number you're going to see what this means in a few lectures you can use std log and again if you have some math background these concepts are not going to be foreign to you many of these functions are documented here at cpp reference so it might be worthwhile to go there and check what functions are available there here is an example of something you might want to do for example you have a variable it's called weight inside we have a 7.7 and if you do std floor of this variable this is going to be rounded down to a 7. if you want to round up you're going to use std seal and you're going to get that and we're going to see what this is going to give us in a minute when we hit visual studio code here is how you would use the absolute value function you have a few variables here and if you try to plug them in this absolute value function you're basically going to get the weight of the number without any sign in front of the number positive or negative the absolute value is the weight of the number without caring about the sign if i may say it like that okay we also have the x function which is going to compute the exponential of a number this is a math concept so if you don't have a background in math this is going to be a little hard to wrap your brains around but in math we have this number which is called e and its value is approximately 2.7 18 28 like you see here the exponential function is going to do what f x here is doing it's going to take e and elevate that to the power of x and for example in our code here we're going to get the exponential of 10 which is going to be e elevated to the power of tan and we're going to play with this in visual studio code and you're going to see the value of this we also have the power function here which is going to take the first parameter and elevate that to the power of the second parameter so this is going to be 3 to the power of 4 and 9 to the power of 3 and we're going to get the answer if we plug this into visual studio code and we have look functions which is basically the reverse of the power function that we just looked at if you do log 8 in base 2 for example it is like you are asking to which power should i elevate 2 so that i get 8. and the answer here is going to be a 3 of course but the catch is that the log function by default works in base e so what we are asking for example if we say log 54.59 is to which power should we elevate e to get this number in here and it is going to be computed and you're going to see this if you plug this into visual studio code or you can try to use one of the calculators available to you and you're going to see that this makes sense we also have a special function that is going to do the same thing but in base 10 and this is going to be easier to understand for example here we are computing log 10 so what we are really saying is to which power should we elevate 10 to get 10 000 and this is easy because the answer is a 4 10 to the power of 4 is 10 000 i really hope this makes sense we also have a square route function we can use so square out of 81 is going to be nine we have a function called std round which can round and uh what it's going to do by default halfway points are going to be rounded away from zero so if you have a 2.5 it's going to be rounded up to a 3 and if you have 2.4 it's going to be rounded down to a 2. we also have a host of functions we can use in trigonometry we have a sine function we have a cosine function we have a tan function if you know these concepts in math this is going to come in really handy if you want to do this i have to say that what we just talked about is a small set of what we can do with these functions here in c math and uh we are here at cppreference.com and we can see all these functions here we can do reminder we can do f men and if you want you can check this out in the documentation for example if you go to can here and click on it you are going to get the description and explanation of what this function does and you're going to have a simple example of how you can play with us okay now i hope you have a better idea of what we can do with these functions in the cmat library we're going to head to visual studio code and play with some of these functions here we are in visual studio code the current project we're going to work on is math functions we're going to copy over our template files and we're going to put them in our directory here math functions and we are going to open this up in visual studio code and we are going to remove whatever it is we don't need and we are going to include our library which is c math and we are going to put in our code so that we can play with us and here we have a variable called weight and we're going to try and use a few math functions on it so we can use sdd floor to round down and std seal to round up so weighted rounded to floor is going to be printed out and way to round it to seal is going to be printed out we're going to have a number here in this savings variable it's going to be a negative number and we're going to try and compute the absolute value of this we're going to see it printed out another thing we're going to compute is the exponential of the value we're going to do stdx 10 and what this is going to do is going to take e and raise it to the power of 10 and that's what we're going to get printed here the other thing we want to do here is compute the power of a number we're going to take 3 and raise it to the power of 4 and take 9 and raise it to the power of 3 and we're going to see what these things evaluate to we're going to open up our terminal here and we're going to run the task to build with gcc the world is going to be good and if we run rooster we're going to get these things here so 7.7 rounded to floor is going to be seven so we're going to round down 7.7 rounded to sale is going to be eight and that's what we're going to get and the absolute value of weight is going to be 7.7 because it is a positive number and the absolute value of minus 5000 is going to be 5 000. and again when you compute the absolute value of something you are interested in the weight of that thing without considering the sign without really worrying if it is positive or negative you just want to get the magnitude of a number that's what we use in math to mean that magnitude okay so if we go down we're going to get the exponential of 10 is exponential and you're going to get that here and again what this function does is take e and raise it to the power of 10 and that's the answer here we can try and prove this with a calculator so i am going to open my windows calculator you can use whatever calculator you have access to i am going to use a scientific calculator here and you see that we can hit e and we're going to get the value of this e number in math here it is simplified but if i take it and raise it to the power of 10 the answer is going to be 22.026 and if we look in our program we have 22 0 26.5 okay it is rounded up a little bit but it is almost the same thing okay we also want to play with log functions a little bit log is basically the reverse of the power function and we just talked about this in the slides but if we do log 18 base 2 we are basically asking to which power should we raise 2 to get an 8 and the answer to that is going to be what this log number in base number is going to evaluate to the catch is that the log function if you use it row like this it is going to be using base e and we just saw that e is 2 to the power of 7 or something let's go back to our calculator and see if we can see that why not why try to guess when we have a good calculator in our hands we are using the scientific calculator so if we hit e here this is the value of e okay so when we do log we are basically asking to which power should we raise e so that we can get this 54.59 and the answer is going to be computed and printed out here we have a version of this that is going to use base 10 so if we use this we are basically saying to which power should we raise 10 to get 10 000 and the answer is going to be printed out here and it is going to be a four because this is easy to do in your mind so let's run this we're going to build the task we are going to run the task to build with gcc and we're going to run our program and uh we're going to get our output here to get 54.59 you would elevate e to the power of this number here this is the answer and if you want you can prove this with your favorite calculator you're going to get something close to this to get 10 000 you'd need to elevate 10 to the power of 4. that's what we're going to get in here let's print this out again many mistakes sorry but i hope you get the point if we build again so that we can see things properly and run we're going to get proper data and this is really what we expected to get we have a few more functions we can play with as we saw in our slides let's play with square root and the std round function here we are going to get the square root of 81 we're going to get it printed down and we're going to see what std round does it is going to round away from zero if we are at half point and if we are not at half point if we are below that we're going to round down if we are higher than that we're going to round up we're going to run this so that you can see this running and we're going to call rooster and the 2.5 is rounded to 3. 2.4 is rounded to 2. 3.6 is rounded to 4. this is what we expect and the square root of 81 is 9. this is really all we set out to do in this lecture to play with a few of these c math function they are not really hard to use once you have the basics of math nailed down and again if you want to check the full list of these functions please come to our reference documentation here you're going to find a lot of them and if you want to see how to use one of these for example if you click on log 10 here you're going to come here you're going to get an explanation of the function and even better you're going to go down and get a nice piece of code you can play with and really make sense of what is happening in these functions here this is really all we set out to do in this lecture i hope you found it interesting we are going to stop here in this lecture the next one we're going to learn about some weird integral types go ahead and finish up here and meet me there in this lecture we're going to learn about some weird integral types and what do i mean by this well if you look here integral types less than 4 bytes in size don't support arithmetic operations and these are operations like addition subtraction multiplication division you can do these operations on those types and here i have an example of some of those types we have a car it is one byte in size and short end is two bytes in size on most processors so you can't do arithmetic operations if your integer value is stored in these types why is that well this has to do with processor design and they decided to choose and as the smallest type integral type for which they can do these arithmetic operations but compilers are really smart enough to notice if you are trying to do arithmetic operations on these types and they are going to implicitly convert from these smaller types to end and this is something you need to be aware of if we look at this program here for example we have var1 and var2 they are stored in variables that are of type short and and we have var 3 var 4 which are stored in a variable of type car we can try and print the resizes and we're going to get two two one one for these variables because a short end occupies two bytes in memory for my computer here and the car occupies one byte in memory but the catch is down here if you try to add them up the result is not going to be the same type as defense you added up and you would expect auto to deduce to the same type as var one and var2 and it's not resort is going to be an integer because and is the smallest type for which we can support these arithmetic operations so if we print the size of result one and reserved two we're going to get four but this is the behavior you're going to get and we need to be aware of it the same behavior can be observed on other operators that we will have a chance to look at in a few chapters ahead and these are called shift operators you use them to shift bets but i don't want to talk anymore about them because we're going to have a chance to learn about them in detail okay now is time to head to visual studio code and actually see this in action here we are in our working directory we are going to copy our template project we're going to copy this quickly put that in our folder on weird integral types this is our current project here and we're going to open this up in visual studio code we're going to open this up and we're going to open our main cpp file remove whatever it is we don't need and we are going to put in our code this must be very familiar because it is what we just saw in the slides so what we are doing here we have four variables var one and var two are short and so these are going to take two bytes in memory on my computer here and uh car is going to take up one byte and we're going to print their sizes we're going to see them printed down and we are adding things up here we are taking two short end variables and adding them up so what the compiler is going to do it's going to take this turn it into an end and then do addition and the result is going to be an end the same story is going to happen for reserved 2 here var 3 is a car so it is one byte in memory but the compiler can't really add up integral types which are smaller than four bytes in memory so what is going to happen var three is going to be transformed implicitly behind the scenes to a net and var four is going to be transformed to a net and then we're going to add that up and we're going to store that in a neat variable which is resort here if we print this up we're going to see that size of result 1 and result 2 is a 4 and this is what we expect here and this is a behavior you need to be aware of because sometimes your application logic is going to depend on the size of data if that's the case you need to care about this and if you don't know this you're going to be beaten by this and it is really hard to find problems like this so what we can do is open up our terminal and we're going to build this with gcc as we usually do and that we can run our program rooster and we're going to see that this is what we expect var1 is two bytes in size var2 is two bytes in size wire three and waterfall are one byte in size but our result is four bytes because the compiler implicitly converted our operands here to x to be able to carry out this arithmetic operation this is really all we setup to do in this lecture i hope you found it interesting we are going to stop here in this lecture the next one we're going to try and recap what we saw in the chapter congratulations on hitting the end of this chapter and this chapter was really about doing operations and manipulating the data that you have stored in your variables the first thing we saw was that we could do arithmetic operations on the data we can add things up multiply divide we also saw the modulus operator which may seem weird for beginners but we had a chance to really play with us after that we learned about precedence and associativity we saw that that's a set of rules we have to follow to know which operation to do first if we have multiple operators in our expression we also had a chance to learn about prefix and postfix increment and decrement operators we had a chance to do compound assignments things like plus equal minus equal multiply equal modulus equal now these things must be making sense to you we had a chance to look at how we could compare things using relational operators like greater than lesser than and equal or not equal we also had a chance to look at output formatting and we saw a host of things we could do to make data show up better with sddc out after that we learned about numeric limits and this is a cool way to know the ranges for your data types but there are a host of other properties you can use in this limits library we learned a little bit about math functions and we played with things like the power of something the exponential the few rounding functions that we have in the cmat library and now you must have a good idea about these functions that you can use to manipulate and do math on your variables we ended the chapter by looking at some weird integral types and the main message in that lecture was that you can't do arithmetic operations on data types whose size is less than 4 bytes in memory if you try to do that the compiler is going to insert implicit conversions and if your application depends on the size of things this is something you need to be careful about otherwise you're going to have a hard time finding these problems i would like to welcome you in this new chapter where we're going to be learning about a new way we can do things in c plus plus and we're going to be doing conditional programming in other words we're going to be able to do different things based on the conditions that we have set up in our code just to give you an example here we have a few variables they are booleans and we have a few initializers in there red is false green is true yellow is false police stop is true and we can do different things based on discipline conditions for example if the light is red we may be told to stop if the light is yellow we might be told to slow down if the light is green we might be told to go and do whatever it is we want to do so we're going to be able to do these kinds of things and c plus plus provides different constructs to be able to do conditional programming in uc code we just have one of them in this slide which is this if statement here this if keyword allows you to do things like this we have a few others we have the else keyword we have the switch keyword we also have a completely separate operator that is called ternary operator that allows you to do these kinds of things and we're going to be learning about all these things and more in this chapter we are going to start and learn about the if statement in the next lecture go ahead and finish up here and meet me there in this lecture we're going to learn about the if statement and this is a statement that is going to allow you to do things based on some condition being true or false here is a simple code example we have two variables number one and number two and we have a statement here that is going to compare number one and number two and we're going to store the result in this boolean variable that we have on the left then we're going to do something based on the value that we are storing in this result here if the result in there is true we're going to print number one is less than number two if the result is not true we're going to say number one is not less than number two and notice in the first if close year we are testing for the case where the result is true but sometimes we need to do something when this condition here is not true one way to do that is to negate what we have here inside the if parenthesis and test for the reverse of whatever we have in here if that is making any sense if you look here we are testing for the case where if not the result is a true and in other words we are testing for the case where the result here is false and we're going to say number one is not less than number two this is one way we can go about this again the if statement is used to do conditional programming this is the syntax we use to do it in c plus plus we say f we put a set of parentheses and inside the parenthesis we put the condition we want to test for and after that we're going to have a pair of curly braces and what we have inside these curly braces is going to be the body of our if statement and it is basically going to be the code we want to run if the test here is successful we can also use an else clause to catch the case where the test hasn't been successful in this case we can say if result equals true we're going to do something and else we're going to do something else in other words if research is true we're going to say is less than if it's not true we're going to say is not less than and this is a more compact way of doing what we just did in the slide here you can also directly use your expression as a condition and the requirement here is that the expression evaluates to something that we can treat like a boolean because the condition has to be a boolean it has to be true or false okay here is another simple example we have seen this in the last lecture we have a few variables containing our conditions and we are saying if the light is red for example we're going to do something if it's yellow we're going to do something if it's green we're going to do something we can also nest conditions for example we can put our outer if statement and inside that if statement set up other if statements and if our logic needs something like this you can do this and you can take advantage of this to achieve whatever it is you want to do in this case if the light is green we're going to fall in here and we're going to do another condition so if the light is green and if the police officer has stopped you you're going to stop and if they haven't stopped you you're going to go because the light is green and you can do something like this we can also use logical operators to kind of achieve the same thing and we say if the light is green and the police officer hasn't stopped you you're going to go else you're going to stop and you can do something like this and you see that you can combine the logical operators we've learned about with the conditional programming techniques we are learning about here to do really powerful stuff and we're going to be doing this all over the place in the course so we're going to head over to visual studio code and play with us here we are in our working directory we're going to be working on if statements and we're going to grab our template project the template files i should say and we're going to put them in here and we're going to open this up in visual studio code as we always do so let's do that open folder and we should fall in here and open our main cpp file let's do the usual and remove what we don't need and we are going to put in our code so that we can really play with us we have two variables in here number one and number two they are integers and we have the values 55 and 60 inside we can do a comparison between these two variables and what this comparison is going to yield is a boolean value that we're going to store in our boolean variable here which is called result and what we can do is use this to do some conditional programming so we're going to say if the result is true we're going to do something and if the result is not true we're going to do something else and this is how we say it this syntax here may be confusing to new students but try to think of it if not research is true and that's really the same thing as saying if result is false try to stream this in your mind a little bit and you really are going to understand okay so now that you have seen this piece of code try to guess what we're going to see if we run this what are we going to see on the console okay give it a try so we're going to run this in visual studio code let's open up a terminal first and we're going to world with gcc as we always do we're going to bring this up a little bit so that we have some breathing room and we're going to run rooster let's see what we have here we have reserved equals true and that's coming from this statement here and we have freestanding if statement which is coming from here and we have our meat of this program what we really are interested in we have 55 is less than 60. so our test here succeeded and we fell in this body and executed this sddc out statement that's why we're saying number one is less than number two and number one is 55 which is what we see here and number two is 60 which is what we are seeing here so what can we do to make the second statement here execute you know it is testing for the case where not result is true so it's going to exactly do the reverse of what this test here is doing and if this fails this one is going to succeed which is what we have now so what we can do to really play with this we can go up and change for example number one to 65 this is going to flip the order of these numbers if i may say it like that and if we run it we're going to build with gcc and run this we're going to see result is false so result became false because number one is not less than number two so this is going to be false and the test here is going to fail result is not true so we're not going to go in here and look at here if not result is true so research is true is false if we negate this this is going to become true and this test here is going to succeed and we are going to fall in here take some time to really understand this and if you have a problem ask me i am going to do the best i can to help you out but make sure you understand this because this is really fundamental in your journey as a software developer not just in c plus plus any career as a software developer you're going to need to understand this okay what we're going to do is comment out what we just did here because we're going to see another way to do this and we don't want to have noise output in our terminal this is going to just make things hard for us to see so what we're going to do we're going to go down in here and put in another piece of code and the result was commented out we don't want to comment that out so we're going to take this out because this is our condition we're going to grab it and put that outside our comment section this is going to do and if we go down again we're going to click on this file icon and uh give ourselves some more breathing room here we don't really need to see that main cpp file anyway so if we come here we can use the else clause to test for the case where our condition fails and if you look here we are essentially saying the same thing we are saying if result equals true we're going to fall in here and else we're going to fall in here so we don't need to do the double if statements that we just did here and this is much more compact and easy to read let's look again at the numbers we have here number one is 65 number 2 is 60. this expression here is going to be false because 65 is not less than 60 we're going to have a false in here and if we come down result is going to be false this is going to fail and we are going to fall down here we're going to say number one or 65 is not less than 60 which is what we have in our values so we're going to build this again and we're going to run rooster that's clear so that we have some breathing room we're going to run rooster and you're going to see that 65 is not less than 60 which is what we expect again if you go up and change these numbers to whatever you want try to change these numbers to different values to see what you get as output if we do this we're going to see that number one is less than 60 so this is going to be true and we're going to have a true in this result variable here if we go down result is true this test condition here is going to succeed and we are going to fall in here let's click in our terminal and hit enter we're going to run rooster we're going to see 55 is less than 60 which is what we have here okay now i hope you have a better idea of how you can use the else close to make your if statements really compact and this is really cool okay another thing i want you to see is that you can not go through a variable like this and use an expression as a condition directly c plus plus allows you to do this so what we're going to do we're going to comment out what we just did let's do that i'm going to use a block comment i hope you know how to use this already and we're going to go down and put in our code it is exactly the same thing but we took the expression and put that in the place of our condition variable here because this is going to evaluate to a boolean anyway so we can use this as a condition and this is something very legal to do in c plus plus so if we run this we're going to see let's see what we have in the numbers i have forgotten by now so 55 number one sixty number two number one is less than number two this is going to be true so the test here is going to succeed and we are going to fall in here that's what we're going to say we're going to weld with gcc as usual and i'm going to clear and run rooster and you're going to see that this is the output we expect if we change up the numbers again let's take this and make it a 75 number one so number one is less than number two is going to evaluate to false the test here is going to fail and we are going to fall in this block here we're going to fall in the else close of our test here we're going to say 75 is not less than 55 is that what we have in there it's a 60 but this block here is going to execute that's the most important thing so we're going to run this and we're going to try and clear and run rooster we're going to get what we expect this is one way we can do this and it is really cool let's comment this out and see another example and if we go down we can see the example we saw in the slides which was using red green and yellow to simulate a traffic light so what we really want to see here is that we can nest stuff but before we do that we need to try and use these conditions because it is just fun so if red is true meaning that the red light is on we're going to stop if yellow is true we're going to slow down if green is true we're going to go and if we look at what we have here red is false green is true false is true and a police stop is true police stop is not really being used so green is true we're going to execute the green statement here and we're going to say go that's what we're going to say let's try and run this we're going to build with gcc bring this up a little bit so that we have some breathing room and we're going to clear and run rooster and you see that it is saying go here you can try and change these boolean variables to different things and see what is printed out here this is a great way to learn but what we really want to try here is nested statements and let's go down and do that okay again we don't want what we have on top here to disturb us so we're going to comment this out and we are going to go down and put in our code for nested if statements so what we're going to say is if the light is green we should go but the police officer has the right to stop us even if the light is green at least these are the laws in the country where i leave so we have a variable called police stop here so if they stop you even if the light is green you're going to stop and we're going to see what we see here so if the light is green we're going to fall in this body again and we're going to run this inside or nested if statement and if the officer has stopped us we're going to stop if they haven't stopped us we're going to go and go on our merry way this is what we want to see here and again i want you to stop for a moment try to run this through your brain and try to come up with the value we're going to see when we run this in visual studio code pause for a minute we're going to run this we're going to build with gcc and we're going to run a program and you see that police officer stops stop and why is that the light is green okay so we we should usually go but the police stop variable here is also true so if we fall in here this is going to execute and it is going to succeed and we are going to say stop and once this statement executes the else statement is not going to execute control is going to fall outside here and we're going to keep doing whatever we have outside this if statement here this is really critical to understand if one block of a statement succeeds all the others are not going to execute and try to really understand this okay we have seen this we can now comment this out because we have something else i want to show you we're going to comment this out and that's that you can combine the if statements here or conditional programming with the logical operators we have learned about earlier in the course and do really powerful stuff for example here we can actually not use this nested statement and do this in one go like this and i think this is much cleaner so we're going to say if the light is green and the police officer hasn't stopped us we're going to go and if they have stopped us we're going to stop and you're going to see that we're going to get exactly the same result that we had before but this is really much more compact so we're going to run and see the result of this and we're going to clear clear and run rooster you're going to see that they're going to stop us and this is really cool you can go up again and change up these variables to really anything you want and see what you get trying to really understand what is going on here it is not difficult and the if statement is really fundamental to any programming you're going to do so this is really all we set up to do in this lecture to learn about conditional programming using the if statement it is a powerful thing and you're going to see it all over your software development career so make sure you really understand this but even if it's not clear now we're going to have a chance to do many examples and you're going to really understand what this is all about we are going to stop here in this lecture the next one we're going to see how we can use the else if close go ahead and finish up here and meet me there in this lecture we're going to learn about the else if close that you can attach to your if statement and that's going to allow you to test for several conditions in your if statement and to really understand this suppose we are building a drawing application and at any given moment the user might be using the pan or the marker or the eraser already whatever tool that it is we provide in our application and if we want to draw we need to know which tool is currently selected by the user and we can do a piece of logic like this we can have the tool stored somewhere in our application and when we get to the moment where we want to do something for example when the user clicks with the mouse and they want to drag and start doing things then we're going to test for the current tool if the tool is the pan we're going to use the settings for the pen if the tool is the marker we're going to use the settings of the marker and we're going to keep doing this and select one tool that is actually selected as the current tool by the user and when you do something like this only one block in this whole chain of statements is going to execute so for example if we have the marker as the current tool the code inside this block is going to execute and say active tool is marker but nothing else in this block is going to execute after we run this statement control is going to jump at the end of this whole chain of if and else if statements here i really hope you understand this and uh this is really powerful stuff for example this is one of the applications i have done in one of my advanced course about qt and c plus qt is a framework that allows you to do graphical user interfaces like this and you can build applications like this so what i did here is i did the exact same thing that we have here i had the chain of if and else if statements and whatever tool was selected as the current tool was used to draw things on the canvas here for example if the current tool was the pen and the pen is this little pen tool here if it is active we're going to draw things like hello world here if we have the star selected we're going to draw stars if we have the rectangle selected we're going to draw rectangles we could do all kinds of crazy things using this piece of logic here but it is powerful stuff and you really have to make sure you understand this so this is how the else if clause can really help you the main thing is to allow you to test for several different conditions you could do this with a lot of if statements but that's not going to be really cool it's going to be ugly and hard to read this is what you should do now that you have an idea about this we're going to head to visual studio code and actually see this in action here we are in our working directory we're going to grab our template project pretty quick and we're going to use the else if project here that's our current project so we're going to do that and open this up in visual studio code as we always do we're going to open this up we're going to open main cpp and we're going to remove what we don't need the first thing i am going to do is to put in a set of variables that are going to store our tools and we can do something like this we have a variable for pen marker eraser rectangle circle and ellipse i have to say that this is not the way to go about this we're going to learn about another way we can do this but that's going to be there in the course when we have enough tools to understand this for now we're just going to be happy with this okay 10 is going to represent the pan 20 is going to represent the marker 30 is going to represent the razor and we can go on until we hit ellipse which is represented by 60. now if we go in main and set up a variable and call it tool for example and initialize it with whatever tool we think that the user is currently using for example we can use circle and initialize this we can set up a chain of s and else if statements to do whatever it is we want to do when this tool here is selected okay we're going to go down and put in our code i don't want to type all this so i'm going to put this up here and if you want you can copy the code from the resource section the code is going to be there you can copy it and paste it in here and do things with that okay we have a chain of if and else if close is in here and the first one is going to test and see if the tool is the pan and notice that we are using double equal signs here this is what you use to test for equality you don't use one equal sign you have to use two equal signs if you use one equal sign that's going to be an assignment and this is going to always evaluate to true so you don't want to do that again to test for equality you're going to use double equal signs in c plus plus so if the current tool is the pen we're going to say active tool span if the current tool is marker we're going to say that so we are basically going to do this for all the tools that our application supports so what do you think we'll see when we run this the active tool is circle so we should print active tool is circle here let's try this out we're going to open the terminal so that we can see the output nicely and we're going to world with gcc and the build is going to go through if we run this we're going to see active tool is circle you can go up here and change the tool to rectangle for example if you build again you're going to see that the correct block of code is going to be executed we're going to clear and run rooster active tool is rectangle if we try to put in something that doesn't exist let's say let's put in bird just to mess with our compiler here we're going to get a compiler error because that type is not known by our code here so we're going to get bird was not declared in this scope we don't know what it is you have to put in whatever thing you have declared beforehand here so if we put in a razor for example we're going to weld with gcc the world is going to go through going to clear run rooster the tool is going to be eraser and this is exactly what we expect again the main message in this lecture is that you can use this else if close if you have several conditions that you want to test for just like we have here and again one of these blocks is going to execute and once we hit the end of this chain of if and else if closes we're going to fall here and code after that is going to execute so let's say moving on so that you can really see this here very clear moving on and if we bolt this we're going to see only one block from this whole chain and once one block executes control is going to go after all this chain and execute whatever is after that so we're going to say moving on after we print that the active tool is eraser here i think we have built this so let's try and run this and you see active tool is razer and moving on so after we print the code from the eraser block we're not going to execute rectangle or a circle or ellipse it's going to jump at the end of this whole block this is really what i want to be super clear and again make sure you understand the syntax here the else if statement is followed by a set of parentheses and inside the parenthesis we have the condition we want to test for the condition has to evaluate to boolean and after that we have the block of code that we want to execute and this is really all you have to do to get this to work to your advantage we are going to stop here in this lecture in the next one we're going to learn about switch go ahead and finish up here and meet me there in this lecture we're going to learn about the switch statement and this is an alternative way to do what we did with these else if clauses in the last lecture basically testing for several different conditions but it's going to be in a much more compact way than what we did in the last lecture we are going to be using the exact same example we did in the last lecture and we're going to look at the switch statement and how it works in c plus plus and this is how it works we have our variable which is going to store the current tool and we're going to say switch inside the parenthesis here we're going to pass the variable that we're going to be basically switching on or testing the conditions on and inside the block we're going to be basically testing for each case so we're going to say if the current tool is the pan we're going to say case pan we're going to put a colon in here and we're going to put the code we want to execute for the case where the current tool is the pan in these curly braces here and one thing you should notice from the start is this break statement here this is a statement that tells c plus plus that after we successfully execute a block of code we're going to jump out of this switch statement if you omit this break statement everything after the case that you just executed is going to execute and that's not probably going to be what you want if this is not making sense yet please bear with me you're going to see it run in visual studio code and you are going to understand so what you're going to do you're going to put all the tools you want to test for in separate cases you see here we have marker and after that you can put a default case that is going to be matched when nothing has been matched in whatever it is you have tested for in this switch block here and when we run this piece of code we're going to get exactly the same results that we had in the last lecture but hopefully you can see that this is much more compact than the series of else if closes that we had in the last lecture again the break statement is really useful if you don't put it in c plus plus is going to be confused and after you hit your successful case everything after that is going to be executed so the break statement is basically telling c plus plus if you hit it after a successful case is jump out of this switch block because you are done you don't have to do anything else in here and another thing you should know is that the condition which is what we have inside this parenthesis here can only be an integer or an enum so basically it can be ant long unsigned or car basically every integral type we have seen before but it can't be another type such as string because that doesn't really make sense so be sure to keep this in mind now that you have an idea about this switch statement let's head to visual video code and actually see this in action here we are in our working folder the current project is going to be switch so we're going to grab our template files and we're going to bring that in here i'm going to go up a little bit and open this up in visual studio code as we always do and we're going to open up our main file we're going to get rid of what we don't need we are going to go outside the main function here and put in our tools just like in the last lecture we are using integers each one is represented by 10 20 30 up to 60 and these are the tools we have and i don't think i explained this but we are flagging them const because we don't want them to be modifiable we have seen about const earlier in the course we are going to jump into main and set up our variable it's going to be ant and we're going to call it tool and we're going to initialize this whatever tool we want let's use eraser why not and then we're going to put in our switch block we're going to say switch and if i type this you see that visual studio code is going to give me intelligence or suggestions on what thing i could do one cool thing that visual studio code does is give you snippets to really help you type things out so we can click on this and it is going to auto complete a starter version of our case and we can type in our expression here which is going to be what we're going to be switching over so we're going to switch over the variable tool here and we can put in our cases so in our case we're going to start with pan which is what we have here so we're going to say case pan we're going to remove all this uh let's remove this and say pen i'm going to put a column and we're going to put a block of code that is going to execute when the current tool is the pen and we're going to put that in here let's bring this to the right a little bit and inside the block we can say sddc out active tool is pan okay so after that we're going to put this break statement if it's not here we're going to have a problem because when the current tool is the pan and this code here executes every case after that is going to execute and that's not what we want we have to remember to put this break statement here okay so we're going to put in all our other cases so we're going to copy this and look at the next one it's going to be marker so we can go on the next line and put in marker we're going to paste this in we're going to say marker and we're going to say active tool is marker and we can keep doing the same thing we're going to go download them and paste this in the next one is going to be eraser so let's do that we're going to say eraser and we're going to say current tool is razer and we're going to go down again i am doing this live because it really is important for you to see me type this otherwise you may be confused because this is a slightly confusing construct we have in c plus plus the next one is rectangle so we're going to use that and we're going to say current tool is rectangle the next one is going to be circle and ellipse so let's do that we're going to put in circle we're going to go down and paste this in and bring this back a little bit and i'm going to say circle and we're going to go down and put the next one in which is going to be ellipse and after that notice that video studio code also inserted a default block and this is a block that is going to execute when none of the cases here is head so this is really important so you can also add a block of code and say no match found okay so we want to see this print out and we're going to try this out in a minute okay after we do this we should get this to work and do whatever we were doing in the last lecture so the current tool is going to be razer the block here is going to be matched so code here is going to execute after that we're going to meet this break and what it's really saying is you have done what you have to do jump out of this switch block here and we're going to go to the end of this switch block and we're going to execute whatever is after that so just like in the last lecture we can say moving on and if we execute the program as we have it now it's going to say active tool is razor and it's going to print moving on let's bring up the terminal so that we can really see this and we're going to build with gcc the world is going to be good and if we run rooster let's clear a bit we're going to see that active tool is a razor and we are moving on this is really cool and it is exactly the same thing we had in the last lecture but now we are using a much cleaner construct which switch provides okay we can try to change the tool and see that this really works we can put in circle why not i'm going to build with gcc the world is going to go through as you see here and if we run rooster we're going to see active tool a circle we are moving on you can try and change this however you want if we put in ellipse and world again we're going to build with gcc and if we run we're going to get active tool is pan and moving on why is that ellipse we are saying active tool is pen we didn't change that so we just code a really bad bag here so let's fix this and we're going to build again i'm glad i found this so we're going to clear and rooster and we're going to see active tool is ellipse and we are moving on okay so let's see what could happen if you forgot your break statement after your case here so the current tool is ellipse and it is the last thing we have in here so it's not a good test case what i am going to do is make the current tool the pen or the marker let's use marker that's going to do and i am going to remove the break statement here okay so and i am going to remove all the bread statements we have until we hit the last default case so we're going to go down and we're even going to remove that for the default case here and if we both we're going to build fine you're going to see that the world was good but if we run this try to guess what we're going to get the current stool is the marker so we should print marker and hit the end of the switch block and do moving on but let's see what we get did we build let's build again just to be sure so we're going to run rooster and notice what happens active tool is marker active thought is eraser active tool is rectangle we basically print every other case after the macro case is head and if we try to change this for example not use marker and use eraser let's change this you're going to see that we're going to match the eraser case and everything after that is also going to execute and it is not what you want so let's clear and run rooster you're going to see eraser rectangle circle ellipse and no match found so you have ready to remember to put in this brick statement i am going to hit ctrl z a couple of times and bring those in and i think this is enough we have to hit again so that marker also has it and if we run now we're going to get what we expect let's try to use an eraser again and if we both again and clear and run rooster we're going to see active tool is razer and we are moving on this is what we expect okay this is really how you work with a switch block you have to pass in your expression or your test case and you catch each case using this case syntax here you say case you pass in whatever it is you want to match and you put a column and you put your code inside this block here and remember after each case you have to add this break otherwise everything else after this is going to execute and it is not going to be what you want before i let you go i want to show you that you can actually group these case statements if this is what is making sense for your application so for example for rectangle circle and ellipse this can be grouped into the group of shapes so we can actually say case rectangle and uh put them on different lines something like this so let's delete this and say case rectangle and we're going to delete the other things and say case circle just like this and we're going to say drawing shape i think this syntax is supported in c plus plus okay so let's try in world and uh we're going to world with gcc and the world is going to be good so if we have a razor which is what we have in here we're going to say drawing shape let's clear and run rooster you're going to see drawing shapes moving on we have grouped different cases and handles them with one block and this may come in handy in some situations one thing you should keep in mind is that there are limitations on what you can use as an expression or a condition in your switch statement and that has to be an integer something like ant car unsigned shorter things like that we have seen all of these things but it can also be an enum but enam is something we haven't learned about yet we will learn about it later when we have enough tools to understand that but know about this limitation so if we can in our project in visual studio code for example and say std string name and say john for example and use this as a switch condition or expression we have to include string for this to work so let's go up and include the string library we're going to do string and if we go down again and try to do switch and use name as an expression and take out everything we have in here and try to compile this you see that the compiler is not happy okay it's going to say expression must have an integral or enum type so this is not even going to compile this is a limitation you have and you have to make sure what you pass as a condition here is an integer something like ant or car or other integral types we have learnt about in this course this lecture we're going to learn about ternary operators and this is an alternative way to do tests with the if statement suppose here we have a variable called max we have two variables called a and b and we want to find the maximum between these two things one easy way we can do this is say if a is greater than b we're going to store the maximum in max else then b is going to be the maximum and we're going to store that in our maximum value and we're going to print this out if we do this we're going to get whatever is the maximum between a and b to be printed out on the console and this is going to work really well but we can also use a ternary expression and the ternary expression looks like this and it starts with a pair of parentheses you're going to put your condition inside the parenthesis you're going to follow that with the question mark you're going to put your option one and after that you're going to put a column and option two and because this is a statement in c plus plus we're going to end this with a semicolon here is an equivalent version of that using f as we have been doing all along so we say if condition results equals option one if condition is false result is going to be equal to option two and this is exactly the same thing we are doing with our ternary expression here if the condition is true we're going to take option one and assign that to result if it's false we're going to take option two and assign that to resort so this is really a syntactic sugar or a shorthand for this thing here and it's going to make your code shorter and you're going to see this all over the place in c plus plus code out there so you really need to be familiar with this okay so here is our example of computing the maximum you see that we have the same variables max a and b but we have a one liner that is going to store the maximum in max and we can print this out and hopefully you can see that this is shorter and better than this okay one other thing you should know is that option one and option two should be of the same type or at least the types must be convertible and if that fails you're going to get a compiler error we can also do ternary initialization here we have a boolean called fast we initialize this to false we can use this to initialize our speed variable here we are basically saying if fast initialize this with 300 if not fast initialize this with 150 and we're going to print the speed here and this is going to work and here you see that i lied a little bit in my explanation to make it really obvious what we had in our ternary expression you don't have to always wrap your condition in parenthesis but it makes the code much cleaner okay now that you have an idea about distance it's time we headed to visual studio code and actually tried this and so for ourselves okay here we are in our working folder the current project is ternary operators we're going to grab our template files and put those where they belong it is in this lecture here on ternary operators and we're going to open this up in visual studio code as we always do we're going to open this and we're going to open main cpp and take out whatever we don't need we are going to go down and put in our test code we basically have a few variables let's kill these spaces so that our code is not cluttered here and we're going to print our maximum and we're going to be using a regular if we have three variables max a and b we are saying if a is greater than b we're going to store the maximum and max otherwise we're going to store b in max and this is going to allow us to print whatever is the maximum between 35 and 20. if we build this let's bring up a terminal and we are going to build with gcc as we always do let's bring this up a little bit and we're going to clear and run rooster we're going to see that the maximum is 35 if we put our maximum in a beam we're going to get the maximum steel so we're going to run this out to build with gcc and we're going to run this clear rooster max is 200 so we're going to get the maximum regardless of where it is stored in a or b this is how our code works but we can use ternary operators to do the same thing so what we're going to do here we're going to comment out our if statement and use a ternary version so what we're going to do we're going to say result equals and we're going to say a greater than b we're going to put a question mark to signal the start of our two options option one is going to be a because if a is greater than b then a is going to be the maximum so we're going to grab a here and if a is not greater than b then b must be the maximum so we're going to put that as a second option here this is what we mean with this statement and we don't have a variable called results that's why we have a problem here we're going to put our result in max and this should solve this problem you see the squiggly lines go away and we can build this and we're going to get exactly the same result so this is our ternary operator if we build this it is going to go through and if we run this we're going to get exactly the same result max is 200 and it is what we expect okay so take some time and really make yourself familiar with this syntax it is really nothing complicated you just have to make sure your brain is aware of something like this and you're going to recognize that everywhere you see it in code out there okay we have mentioned that you can't pass types that are not compatible and the requirement is that a and b be of the same type or at least types you can do conversions between so for example if we try to do something like pass a hello in here we can't really turn a strength into a integer and this is going to give us a compiler error and let's look at the error we see from visual studio code operand types are incompatible and and the const car are not compatible and they have to be compatible if we ignore this and go ahead and build we're going to also see an error from the compiler and it should be the same error so operands to question mark and column have different types and and cost car and they have to be stands you can really compare so for example we can take out this string here and put in a floating point let's put in a 22.5 and say that this is a fluid you're going to see that the problem is going to go away here let's wait for a minute it's not going away we're going to build anyway you see the world is good we're going to clear so that we don't have all this clutter you see problems by the way and if we both again let's try to do that the builder is going to go through and if we run this we're going to get max is 22 because we are comparing a and 22 here and the compiler inserted an implicit conversion from this floating point to integer that's why we are seeing this here so what is the type that's the compiler really used here okay to make it really clear what is going on we're not going to use max here let's say auto max one and see what the compiler is really deducing to so we're going to take this out and by this we're going to see what implicit conversion the compiler really did here because what we had before we were doing an assignment so whatever was on the left was being turned into an integer because we were doing an implicit conversion from an assignment so whatever is on the left is turned into the type of whatever we have on the left of the assignment i hope you know this already but if we do things like this we're going to deduce whatever is on the left and assign that into max one so we're going to do so we're going to see the implicit conversion that the compiler did in here and it is good to be able to see things like that it is a curiosity thing so let's see what happens here we're going to print max one and i'm going to world with gcc this is going to go through and we're going to see what is printed so let's run rooster and you're going to see that the implicit conversion happened from end to flood and we deduce the flood here in max one so this is really interesting one might have thought that we had a conversion from float to end but it was actually the reverse and this is quite interesting this is really all we set out to do in this lecture i hope you found it interesting again make sure you have this syntax here drilled in your brain because you're going to be seeing this a lot in your career as a software developer not only a c plus plus developer actually and it is going to be helpful for you to know this congratulations on hitting the end of this chapter this chapter was all about flow control and we had the chance to learn about many of the tools that c plus plus provides to do conditional programming and we were able to see all kinds of crazy things we had the chance to do things like if red is true we're going to run the code inside if yellow is true we're going to run code inside if green is true we're going to run code inside and you had a chance to play with those things on many occasions we have seen that the tools that c plus plus provides really revolve around these four things here we have if statements we can do else closes on this if statements we can do else if to really do long chains of things where we do different decisions to do things in our applications we also saw that switch is an option if you have several decisions you can make in your application and we saw that a ternary operator can really be used to do two decisions if you have two decisions you're going to be able to reduce the number of lines you used to do these decisions with to one line and we had a chance to really look at that in detail in the lecture on ternary operators this is really all i had to share in this chapter i hope you found it interesting we are going to stop here in this lecture in the next chapter we're going to start and learn about loops go ahead and finish up here and meet me there in this chapter we're going to be learning about loops and this is a construct that is provided by the c plus plus programming language to allow you to do repetitive tasks relatively easily suppose we have a task to print a message 10 times we can go down and do it like this and it is going to work but what if they try and tell you do this 100 times then you're going to start scratching your head you're going to type this 100 times and you're going to run your program and it is going to work then your manager comes up and they say i want you to do this 10 000 times or even 100 000 times and then you're going to start pulling your hair or running around breaking vents because that's impossible to do not to worry here c plus plus provides a way to do this relatively easily and we're going to be learning about ways we can do this in our program in this chapter the constructs we're going to be using are these you see here there is a for loop a range base for a loop a while loop and do a while loop and we can make use of these tools to not go crazy printing a message one million times because that's really bad okay so that's what we are up against in this chapter we're going to be learning about these loop constructs and you're going to find that they are really cool for your application i am excited to be teaching you this so let's get started in the next lecture and learn about the for loop go ahead and finish up here and meet me there in this lecture we're going to learn about four loops for loops are one of the constructs we have in c plus plus to do repetitive tasks they allow us to do something like this much more easily for example we don't have to type stdc out of c plus plus 10 times or 100 times we can do this in less than five lines and you're going to see how cool this is the syntax for for loops looks like this and it may look intimidating for first users but we're going to break this down we have a few parts in here the first part is the iterator which is a variable that we're going to be using to navigate through the loop and that variable is this unsigned int i here we are going to initialize this to zero and the value we initialize this with is going to be our starting value and that's another key point of any loop any loop is going to have a starting point we are also going to have a test which is going to control when our loop ends for example this loop is going to start with i equal to zero it is going to keep doing stuff until i is no longer less than 10. as long as i is less than 10 it's going to keep doing whatever it is we want to do and the first part of the loop is this plus plus i thing here and this is an incrementation part that's how i'm going to call it for simplicity but at least a part that keeps changing our iterator to make our loop move forward after these four points we are going to have a pair of curly braces and inside these curly braces we're going to have the body of our loop and this is the code we want to execute multiple times and inside this loop body we can really do all kinds of crazy things you're going to see how cool this is again we have a few parts with our loop we have our iterator which is this variable i here we're going to be initializing this with a value that denotes where we want our loop to start we then have a test which is going to control when the loop is going to end we're going to have our incrementation part which is going to allow our loop to move forward and it's going to be doing that through incrementation or decrementation but to make explanation easier here we're going to call this part the incrementation part of the loop after that we're going to have the body of the loop and it's going to be within this curly braces and it's going to be whatever it is we want to execute inside this loop okay these are the parts of a loop and you need to keep this in mind okay now that you know the main parts of the loop how does it really work let's go through this and see how it's going to print i love c plus plus 10 times so when this code gets executed we're going to start with i equal to 0 because that's what we are initializing this value with after the incrementation part we're going to run the test and the test is going to say is i is 0 less than 10 and that's going to be true so if the test turns out to be true we're going to jump into the loop body and if it's false we're going to jump to the end of the loop and execute whatever is after our loop this is how it works so if we jump in for the first time when i is 0 we're going to print i love c plus plus for the first time then after the body is executed we're going to execute the incrementation part this is going to make i equal to 1 then we're going to come back to the test we're going to say is 1 less than 10 that's going to be true we're going to come in the body and print i love c plus plus for the second time after that the incrementation part is going to run we're going to have a 2 inside i here 2 is less than 10 this is going to be true so we're going to follow him the body again and print i love c plus plus for the third time we're going to keep doing this until we hit nine for example if we increment and get a nine we're going to form the test and say is nine less than ten that's going to be true so we're going to fall in here and print i love c plus plus again and after that we're going to run the incrementation part again this time i is going to become 10 we're going to do the test 10 is not less than 10 so the test is going to fail and we're going to fall on the outside of the loop here and by that time we'll have printed i love c plus plus 10 times and this is really how a loop works for the first time we're going to run this initializer part here to initialize our iterator which is this value i and then after that we're going to run the test if the test is good if it's successful we're going to fall in the body we're going to execute whatever code is in the body and after that we're going to keep doing the cycle increment test the body increment test body increment test body until we hit a point where the test fails and if the test fails we're going to fall outside this for loop here we're going to execute whatever is after this closing curly brace and this is how a loop works i want you to go through this one more time by printing i love c plus plus and making sure that this is printed 10 times and one thing you should have noticed now is that we use this test to control how many times the loop is going to start for example if we wanted it to run 20 times we would start from zero and and at 20. and you don't always have to start from zero you can really start from anywhere for example you can initialize i with a value like 30 and end when i is equal to 55. let's say that as an example you can really do anything the main important thing is to understand how a loop works one thing i want you to notice is that we are saying unsigned int here to declare our iterator iterators are a common thing in c plus plus code and they're not just used for loops like we are doing it here there is a type we use to do iterators and it makes our code much more readable and that's called size t this event is a representation of some unsigned and type in your c plus plus compiler this is going to be different from compiler to compiler but for example on my system this is eight bytes so it has quite a good range of values you can put in there if we use size t type to declare our i iterator here we're going to say something like this for size t i we're going to put our curly braces we're going to put a semicolon we're going to put our test and we're going to put our incrementation part and i think i really emphasize this but the parts of our loop inside this parenthesis are separated by semicolons like this so we have the iterator declaration we have a semicolon we have the test we have a semicolon and we put our incrementation part and then we go on and put our curlies and inside we can do whatever it is we want to do inside the loop so size t is something you're going to see a lot make sure you know what it means remember that it is a representation of an unsigned and type so it can only represent positive numbers and it is used to represent things like sizes in c plus plus thank the number of students in a classroom for example that can never be negative think about the number of seats in a car that's something that is always going to be positive and it won't make sense for that to be negative so for those kinds of things we can use size t to represent the size of those things and you're going to see how this works in a minute okay now that you have a pretty good idea about how a loop works and the main parts that make it up i want you to know that you can also do all kinds of crazy operations inside the body of your loop for example here we are printing out the current value of our iterator and we're going to print the double that we can do this we can also leave out the curly braces and this is only valid if we have one statement we want to run in the loop so this is going to print this message here five times because this loop is going from zero all the way to four it is going to stop when i stops being less than five and this is going to be five times if you go through this you're going to see that this is going to print five times you should also know that this iterator that we are declaring like this is going to be scoped inside the body of the loop so you can only use it inside these curly braces and if you try to use it outside for example here after the closing curly braces of the body of the loop you're going to get a compiler error again you can only use this variable i which is our iterator inside the curlies that mark the start and the end of our loop body if you want the iterator to be usable outside the scope of the for loop you can do something like this you can declare your iterator outside the loop and you can initialize that with whatever you want and then you can set up your loop like this you can say for j and put and your test and your incrementation part and inside do whatever it is you want to do in the body of the look if we do it like this then j is going to be usable outside of the loop and we're going to see its value if we print it out like this so this is also something you can do you can even go all crazy and leave out the declaration part if you happen to have the iterator declared outside so if you do something like this it is also going to work okay the last thing i want you to see before we head over to visual studio code and actually try this out is that it is a bad thing to hard code your values in for example you see here we are saying j less than 10 this is really bad design because every time you need to change the number of times that your loop is going to run you're going to have to hunt for these loops and change these values and this may be in a lot of places in your code so a good thing to do is to put the value that controls how many times your loop is going to run in a variable like this we're going to store that value in this count variable notice that it is of size t because it is a representation of the size of things something we don't really want to be negative and we're going to use this in our test part of our for loop here instead of saying j less than 10 we're going to say j less than count and if we want to change this we have one point of control to go and change this value here and this is really good design you should do this okay we have talked a lot about loops it is time we headed over to visual studio code and actually tried these things out here we are in visual studio code the current project is for loop here we're going to grab the code from our template project and we're going to put that in place here and we're going to open this in visual studio code so let's do that pretty fast for loop that's the current project we selected and we opened this in visual studio code we're going to open up our main cpp file which is going to show up here and we're going to remove things we don't need here so let's remove that okay the first thing i want you to do is the bad way to do things again if we want to print i love c plus plus 10 times we can do it like this and if we run the program it's going to work but this is really bad design because you would have to copy and paste this for example a thousand times or even a hundred thousand of times and that's really bad so if we bring up our terminal and build with gcc like we always do the build is going to go through you see it's successful we're going to clear and we're going to run rooster and it's going to say lfc plus plus 10 times if you count this it's going to be 10 times because that's what we're doing here but this is really bad design you don't want to do something like this so we're going to comment this out and do this better using a for loop let's say that this is the bad way and we're going to use for loop which is the good way sorry so we're going to start by declaring our iterator so we're going to say for we're going to say size t let's use unsigned end to show you that you can use that and we're going to say i and we're going to initialize this with a zero and this is going to be the declaration of our iterator this is the first part we have in this parenthesis here we're going to put in a semicolon and put in the test we want the loop to run ten times so it's going to run from zero to nine we're going to stop when i stops being less than ten this is one way you can understand this after we have our test and we're going to put the incrementation part which is going to change the iterator to make the loop move forward and we're going to do that by incrementing our iterator after that we're going to put a pair of curly braces not angle brackets and we're going to fall inside the body of the loop this block here delimitated by the curly braces is the body of the loop and inside here we're going to put whatever we want the loop to run so we can go down here and say i love c plus plus 10 times let's say that but before we do that let's say i so that we know the current iteration where we are at and we're going to put a column and say i love c plus plus this is going to be better to follow in the terminal i guess and we're going to put in our new line character move to the new line and we need to put our stream output operator here for this to make sense to the compiler and this is our loop now if we run it we're going to see exactly the same thing like this but we're going to have these iterators prepended to the message so that it becomes easy to follow this and again how this is going to work we are going to run our code to declare and initialize the iterator after that we're going to do the test we are going to start with i zero so zero is less than ten this is going to succeed we are going to fall in the body here and after that we're going to print sddc out i love c plus plus and this is going to be zero we're going to increment i and we're going to do the test again we're going to have to do is one less than 10 the test is going to be successful so we're going to fall in here and we're going to print a one and we're going to run the incrementation part again we're going to run the test and we're going to keep doing this until we hit a point for example when we increment this to be a nine and after that we're going to run the test we're going to say is 9 less than 10 the test is going to be successful so we're going to follow in here and we're going to print nine and after that we're going to increment again we're going to run the incrementation part and we're going to make i 10 if we run the test we're going to say is 10 less than 10 the test here is going to fail we're going to stop running the loop all together and we're going to fall on the outside of the loop and say loop done let's say that so that we can see this on the terminal and this is going to run and print i love c plus plus 10 times and we expect i to go from zero all the way to nine try to run this in your mind take a piece of paper try to print this as you fall into the body of the loop you're going to see that's exactly what is happening here so what we're going to do we're going to boil this again so we're going to run the task to work with gcc the world is good we're going to go down and run this we're going to clear and run rooster and you're going to see that we have zero i love c plus plus all the way to nine i love c plus plus our loop is working exactly the way we want okay so this is the first step we are able to run our code a lot of times without really having to manually do things like this and the benefit of this is that if we want this to run for example 100 times all we have to do is change our test to go until i is equal to 100 and we're going to stop when i stops being less than 100 and if we run this we're going to see the message from 0 all the way to 99 and this is pretty cool the torso is going to be 100 times so let's weld again to show you how cold this is we're going to run this task to weld with gcc again and we're going to clear run rooster prepare to be amazed run and it's going to run all the way to 99 we can change this to even 1 000 or even 10 000 why not we're going to weld again and we're going to be successful and we're going to clear and run rooster and this is going to go all the way to 10 000 times we're going to wait for this to get there now 3 000 4 000 5000 6000 7000 8000 ten thousand it's going to go all the way to nine thousand nine hundred ninety nine and it is going to stop and the total of times the loop run was from zero all the way to this storping number here okay now we can see that our loop is really working fine we don't have to do these hddc out statements a crazy amount of times okay and the benefit again is that regardless of the number of times the code here is going to run our loop is going to stay relatively the same it's just going to be one two three four lines of code and this is really cool okay so i think this now makes it very clear how loops work so we're going to comment this out and show you another thing and we're going to show you that you can use size t to actually denote the type of your iterator and it is easy to do that so we're going to say use size t and for us to be able to see all this here let's hit this file icon here to close this pane and we're going to have a better chance of seeing our code here so what we want to do we're going to declare a group we're going to save for we're not going to say our design and we're going to say size t and this is going to be the type of our iterator we're going to say i we're going to initialize this with a zero and we're going to put a semicolon like we just did we're going to put in the end test we're going to put in the test so we're going to say i less than 10 no problem with us and we are going to increment to put in place our incrementation part we're going to fall in the loop we're going to say stdc out this is the code we want to run so we're going to put in our coverage value for the iterator and we're going to say i love c plus plus again and we're going to put in our new line character and if we build and run this it's going to work and run the same so let's say loop done here so that we know when it's done and we're going to weld and run we're going to world with gcc the world is going to be good and what we're going to do we're going to clear and run rooster and this is going to say i love c plus plus 10 times from 0 to 9. again the message here is that you can use size t to represent the type of your iterators and this is the type that is even used in the c plus plus standard library so this is going to be something people expect from your code if you are representing sizes in code that should be compliant with the c plus plus standard library nothing stops you from using unsigned it like we do here but we're just going to follow the convention from the c plus plus standard library and use this type here to represent all sizes and some of you might be wondering how big is this thing we have the tools to know how big it is so let's do that pretty fast so we're going to go down here and say sddc out and we're going to say size of size t and we can print this out and let's say what we are doing here so that it is easy to use this code as the reference and if we print this out we're going to see the size of this thing on the system where this code here is going to run so we're going to build with gcc again the bolt is going to be good we're going to go here and clear and run rooster you're going to see that it is eight bytes on my system so you're going to have quite a huge range of numbers you can put in here because this is going to only be positive okay if you try to put in a negative number you're going to get an underflow and we have learned about overflow and underfloor in the previous chapter okay so let's comment this out and keep learning more things about for loops okay the next thing i really want you to focus on and learn is the scope of the iterator and we're going to grab our code again here so that we can use this as a learning tool so we're going to go down here and put that in and what we're going to have let's move this a little bit to the right i think we can do something like this i am going to select the entire thing here and hit tab and it is going to be aligned with whatever we have here okay so we have our loop it is going to run 10 times it's going to print i love c plus from 0 to 9 but what is the scope of this i think here and the fact is it is scoped inside this loop here you might have guessed that if you try to use it outside the loop you're going to get a compiler arrow because it is only usable inside the body of the you can also use it inside the control part of the loop here but you can't use it outside the loop so if you try to print i here for example you're going to get a compiler error so let's try and do that and show you how wrong this can be so if we try to print i here let's say i to make it clear what we want to print if we try to do this we're going to get a compiler error because i is not in scope it is only in scope inside these curly braces that show the body part of our loop okay visual studio code is showing the problem here it's saying eyes undefined here so you shouldn't really do this but if we move on and ignore the message from visual studio code and compile we're going to get a compiler error and it's going to say i don't know what you're doing here i was not declared in this scope where you are using it and i think it's even going to give me a line a number it's going to tell me 46 that's where you are doing something wrong and if we go in our code we're going to see that this is the line where we are making the mistake so i is not in scope here we're going to comment this out and say compiler error i is not in scope that's what you're going to get if you try to do this and the main message is that if you declare your loop like this ah is only going to be scoped in the body of the loop and that's the only place where you can mention its name and use it if we comment this out and world again the compiler error is going to go away you're going to see that the world is good let's clear so that our output here is good we're going to build again and show you that the build is good and uh this is really cool both finished successfully okay this is one way we can set up our loop but there is a way we can make our iterator usable outside the body of the loop and we're going to show you how you can do that next so let's comment this out and go down again and we're going to grab the code because we don't want to type all this again and we're going to say iterator declared outside the loop we're going to go down and put in our code and we're going to align this nicely and we're going to take the iterator and declare it on the outside of the loop so we're going to say size t and we're going to say i and initialize this to 0 and we're going to say this very clearly iterator defined outside and we're going to say in our [Music] declaration part of the iterator we're going to just say i and this is going to work pretty fine if we build this you're going to see that the world is good and if we run this it's going to do the same thing it's been doing it's going to print i love c plus plus 10 times and if you want i usable on the outside of the loop now you can use it we can grab the code here that was failing in the last run of our code and put that out here and we're going to align this a little bit and if we compile this the book is going to be good and if we try to run rooster we're going to see i love c plus 10 times we're going to say loop done and we're going to print the value of i and you see that if we do things like this i is going to be usable on the outside of the loop body and sometimes these may come in handy depending on the logic of the application that you are designing just know that this is something you can do in your loops okay you can go even crazier and leave out this iterator declaration part in your loop all together so you can remove that and we're going to show you how you can do that in a minute so let's comment this out i don't want to remove code because i want you to have this as a reference if you need to look at this later so we're going to say leave out and what you can do is grab the code we had before i'm going to grab it and we're going to paste that in here and what we're going to do is take out this eye this can work and you may see code like this out there in the wild so we're going to run this task to build with gcc the world is going to be good and we're going to clear and run rooster and it's going to do exactly the same thing so this is one way you can set up your loops okay the last thing we need to see about loops is that this test here is using a hard coded value and if we needed to change this loop to run for example for a thousand times we would need to manually hunt for this line of code change this and this is really not good design so what is recommended is to store this value in a variable and test against that variable name instead let's comment this out and show you that so we're going to comment this out and go down and copy this entire thing i don't think i want to do my loops like this because this is really unintuitive most times so i am going to declare my iterator inside and i'm going to copy the code that does that here we're going to go down and say don't hardcord values okay we're going to say that it's really bad and we're going to go down and show you a better way to do things and again right now we are hard coding in this value but we can say const size t and we can call this value count for example and we can initialize it with whatever we want in this case we want to control our loop to run 10 times so we're going to put in a 10 and instead of hard coding and this 10 we're going to say count okay and if we build and run it the loop is going to do exactly the same thing it's going to print i love c plus plus 10 times so let's do that and run rooster it's going to do the same thing but now we have one point of control if we want this to run for let's say 100 times we can change the count value to 100 times and if we both again that's how many times the loop is going to run and this is much cleaner so i do recommend you store the values you use in your loops in variables like this and it's going to make your code look better so it's going to run 100 times and this is exactly what we want this is really all we set out to do in this lecture i hope you found it interesting i am sorry that this lecture turned out lengthy because we had a lot of things to talk about but now i hope you really know a lot of things about ways you can use your for loops to make them do things and again many loops that we're going to use in our c plus plus code are going to have five parts we're going to have an iterator which is going to be a variable we use to manipulate how a loop moves forward and does things the starting point is going to be where the loop is going to start we're going to have a test in place which is going to control when the loop ends and we're going to have the end point encoded in that test we're going to have the incrementation part of the loop which is going to control how our loop moves forward and we are going to set up a pair of curly braces that are going to contain the body of our loop in this lecture we're going to learn about while loops and this is another construct we have in the c plus plus programming language to allow us to do repetitive tasks the syntax for a while loop looks like this we have five parts in our loop we have the iterator the starting point the test the incrementation and the loop body it's just going to be set up differently for while loops the iterator is declared on the outside and you see here we have unsigned in i which is exactly the same type of iterator we've used in the last lecture but know that you can also use size t here i am just using unsigned and for this case after you have your iterator declared you're going to fall to this part here and say while open a set of parentheses and inside you're going to put in your test after that you're going to open a pair of curly braces and design them you're going to put the body of your loop and you're going to do whatever it is you want to do in the loop in there and after your code to do whatever it is you want to do is executed you're going to put in your incrementation part after you code and that's what we have here and this is the setup we're going to be using for our while loops again we have the iterator declared on the outside and we can initialize it with whatever it is we want to start with then we're going to put in our test it's going to be within this parenthesis and after that we're going to set up a pair of curly braces and inside them we're going to put whatever code we want to run in the loop and then we're going to follow that with our incrementation part and if we try to run this we're going to run this line we're going to declare the iterator i and it's going to start with zero so the test is going to run and 0 is less than 10 and we're going to succeed and we're going to run the body here we're going to say i love c plus plus and after that we're going to run the incrementation part i is going to become one we're going to run the test again so one is less than 10 we're going to succeed we're going to fall in print i love c plus plus we're going to increment again then we're going to have a 2 in i here we're going to run the test again we're going to say is 2 less than 10 that's going to be true so we're going to be successful we're going to fall in the body of the loop we're going to increment and get a three we're going to test against our three value three is less than ten we're going to succeed we're going to print i love c plus plus we're going to increment again we're going to test again and we're going to say is for less than count or 10 we're going to be successful and we're going to keep running until we increment and get a 9. once we get a 9 we're going to run the test again and we're going to say is 9 less than 10 that's going to be successful so we're going to fall inside print i love c plus plus we're going to increment and get a 10 and if we run the test again i'm going to say is 10 less than 10 that's going to fail and we are going to fall on the outside of the loop and we're going to run whatever code is on the outside here this is really how a while loop works and i hope you can see the similarities between the for loop we have seen it is really the same thing we just have different syntaxes to do these things okay if you look at these things we already have the same pieces of information it is just different syntaxes that c plus plus provides to do the same things and sometimes you're going to find it convenient to use while loops and sometimes you're going to find it convenient to use for loops and you're going to have to decide what works better for whatever it is you are working on for now we're going to head over to visual studio code and try this out okay here we are in visual studio code the current project is while loop we're going to grab our template files and we're going to go in our project and put those in and we're going to open this in visual studio code so let's do that we're going to open the folder and we're going to work on while loop here we are going to open our main cpp file pretty quick and we're going to get rid of this and what we're going to do is remove what we don't need and again we're going to show you the bad way to do things you don't really want to do this manually we want to print i love c plus plus 10 times so we're going to build let's bring up a new terminal so that we can use this when we run thanks and we are going to world with gcc let's do that pretty quick the world is good so we can clear and run rooster and we're going to have i love c plus printed out 10 times so what we can do is comment this out and use a while loop like we want to do in this lecture here and as we have seen with the slides the first thing we need to do is declare our iterator on the outside so we're going to say size ti we're going to initialize this with zero and that's going to be our iterator declaration and we're going to say while i is less than the value where we want to stop and again we don't want to hard code these values and so what we're going to say is really const size t and we're going to call this count and make it uppercase this is a convention we use to declare constants in c plus plus but you don't have to make them uppercase it is just a convention that is going to make your code easier to read for other people so we're going to put a 10 in here because we want our loop to run 10 times and we're going to say y i is less than count and we're going to set up a pair of curly braces and they are going to delimitate the start and the end of the body of a loop here and inside we're going to put in whatever code we want the loop to run multiple times and the code is now very familiar we're going to say sddc out and we're going to print the current iteration and we're going to say i love c plus plus sdd endl for our new line character and after that we will need to put in our own incrementation part if you forget that you're going to get something really bad happen we're really going to look at that later in the chapter but for now remember to put the incrementation part and we're going to say plus plus i this is really easy and we're going to say that this is the test and this is going to do what we want but let's go down here and say that loop is done when it is done so we're going to say loop done and we're going to say stdendya and before we run this we're going to go through it and see how it is going to work so let's click on this file icon here to close this pane so that we have some breathing room i am going to open a notepad file we're going to use to see these things really happen when this loop runs we're going to initialize the iterator here what we're going to do is put in a 0 and we are going to run the test to see if 0 is less than 10 and the test is going to be successful so we are going to fall in here and say 0 i love c plus plus so let's say that here to make this super clear inside the loop we're going to print this message zero i love c plus plus and after we execute this line we're going to increment i is going to become one we're going to go up again and run the test and we're going to say is 1 less than count or is 1 less than 10. the test is going to be successful we're going to run inside the loop and we're going to print 1 i love c plus plus that's what we're going to do here so let's do that and after we do that we're going to run our incrementation part again i is going to become two we are going to run the test we're going to say is 2 less than 10 that's going to be true of course and we're going to fall in the body again and say two i love c plus plus so let's say that we're going to increment and we're going to get a three we're going to run the test again the test is going to be successful because three is less than ten we are going to fall in here and say three i love c plus plus okay so we can really follow this and we are going to increment to get a four and the test is going to say is four less than ten that's going to be true so we're going to succeed and following the body again and we're going to say 4 i love z plus blocks we're going to keep doing this and we're going to increment again we're going to get a 5. the test is going to run again we're going to say is 5 less than 10 that's going to be true of course and we're going to run the code to print five i love c plus plus we're going to increment again to get a six we're going to run the test we're going to say is 6 less than 10 that's going to be true of course again and we're going to print sets i love c plus plus and we're going to increment to get a 7 we're going to run the test our iterator we're going to say is 7 less than count that's going to be true we're going to print the message again and say seven i love c plus plus and we're going to increment again to get an eight we're going to say is eight less than and count that's going to be true we're going to fall in the body and say eight i love c plus plus we're going to increment to get a nine we're going to run the test against our nine of course nine is less than ten so that's going to be true we're going to fall inside and we're going to say nine i love c plus plus and after that we're going to increment again and we're going to get a 10. now if we run the test we're going to say is 10 less than 10 and that's going to be false because 10 is not less than 10 and if the test here fails we're not going to fall in the body of the loop control is going to fall at the end of the loop body and we're going to print loop done that's what we're going to say here we're going to say loop done and our loop will have run 10 times by the time we print this loop down message here if you count here you see that the loop is going to run 10 times and it is going to say i love c plus plus 10 times which is really what we want okay i really had to show you this manually so that you can really see what is happening but now we can weld and run this and let the computer do the heavy duty of running this and showing us all these messages so the build is going to be successful of course we're going to run rooster and it's going to exactly show the same thing we just came up in our trusty notepad file here you see it is the same thing but the beauty is that we don't have to do all these crazy computations again what we can do is we can even change the count to 100 so it is going to run for 100 times that's pretty cool so we can clear and run rooster and is going to do that 100 times and that's pretty fast for computers this is really all we had to share with you in this lecture the main thing is to learn about the syntax of a while loop and again notice that we have these five parts that make up our loop we have an iterator we have the starting point we have a test we have the incrementation part and we have the loop body it is just set up differently with a while loop like we can see here in this lecture we're going to learn about do while loops and this is yet another construct we have in c plus plus to do repetitive tasks let's look at the syntax here we have our declaration of the iterator on the outside and after that you're going to say do you're going to put a pair of curly braces like this you're going to put whatever code it is you want to run inside the loop and then you're going to do your incrementation and after the closing curly brace you're going to say while and you're going to put in your text this is really the structure again notice that we have those five parts and this is the declaration of our iterator and we're going to initialize this in place for example if we want to do that after that we're going to fall in the body of the loop and after that we're going to do the incrementation after we run the code it is we want to run in the body of the loop and then we're going to do the test later and the special thing about a do while loop is that it is going to run first and then you do the test last this is really something you need to be careful about make sure that when it runs it is doing whatever it is you want to do because the test is going to run after your code has actually run and you can tweak this to do whatever it is we want it to do as we are going to see when we hit visual studio code in a minute okay again one thing you need to keep in mind is that the do while loop is going to run the body of the loop and then do the check or the test and that may come in handy in some of the applications you'll be doing within your career as a c plus plus developer okay this is really the syntax we're going to be using we're going to head over to visual studio code and play with us okay here we are in our working folder the current project is going to be do while loop we're going to grab the template files pretty fast and we are going to put them in our current project let's open this up and put them in there and we are going to open this in visual studio code do while loop is our project we're going to open this and the main cpp is opened up so let's remove whatever it is we don't need and we are going to show you the bad way to do things the bad way to do things is typing your code to do things manually if you wanted them to be done multiple times and this is something bad if we bought this and run we're going to go down and run this clear not that's g brush we're going to run rooster and we're going to get i love c plus plus 10 times and you don't want to do something like this what we want in this lecture is to use do while loops to solve this problem we're going to comment this out because it is really bad and we're going to go down and set up our do while loop the first thing we need to do is to set up the count because we want to store this in some variables so we're going to say const and count and we're going to put in our value let's put in a 10 not 190 we're going to say 10 times and we're going to set up our iterator we're going to say size t and we're going to say i and initialize this with our starting point we're going to start from zero and what we're going to do is say do and put a pair of curly braces and inside this pair of curly braces we're going to put whatever it is we want to do multiple times so we want to print i love c plus plus so that's what we're going to say we're going to put in the current iteration and we're going to put a column and say i love c plus plus and then we're going to jump to the new line with stdendl and after that we don't want to forget our incrementation part so we're going to say plus plus i and this is going to be the part that moves our iterator to the next step this is going to be the incrementation and this is going to be the iterator declaration and after the closing brace we need to say our test so we're going to say while and we're going to put in our test here and we want to keep doing this as long as i is less than count of course and we're going to put in our closing semicolon here now this code is going to run 10 times but let's go through how it's going to run so that you can really see everything that is happening here okay so when this code here is run we're going to hit the initialization line here we're going to declare i and we're going to put in a 0. we're going to say do and inside we're going to print whatever we have in i and say i love c plus plus so we're going to say 0 because i is 0 at this point and we're going to say i love c plus plus and after that we're going to increment i is going to become one and we are going to do the test at this time we're going to say is one less than ten that's going to be successful so we're going to run the body again and we're going to say 1 i love c plus let's copy this and print this again so that we don't have to type this all the time we're going to say 1 i love c plus and after that we're going to run the incrementation part again so we're going to get a 2 inside i we're going to test and say is 2 less than 10 that's going to be true so we're going to run the body again and print 2 i love c plus plus and we're going to increment to get a three in there we're going to run the test against our three is three less than ten that's going to be true so we're going to fall in the body again and print three i love c plus plus let's do that we're going to do the incrementation we're going to get a 4 inside this i variable here we're going to run the test we're going to say is 4 less than 10 that's going to be true of course we're going to say four i love c plus plus when we fall in the body again we're going to do the implementation and get a five we're going to do the test it's five less than ten that's going to be true so we're going to fall in the body again and say five i love c plus plus let's say that we're going to increment again we're going to get a six the test is going to run and we're going to say is six less than ten that's going to be true again then the body is going to execute and say 6 i love c plus plus we're going to do the incrementation and get a 7. we're going to run the test against our 7 so we're going to say is 7 less than 10 that's going to be true we are going to fall in the body again and say 7 i love c plus plus we are going to do the incrementation we are going to get 8 we're going to say is 8 less than 10 that's going to be true we are going to fall in the body again and say 8 i love c plus plus we're going to do the incrementation we're going to get a 9 and we're going to run the test we're going to say is 9 less than 10 that's going to be true so we're going to go inside and say 9 i love c plus plus and we're going to do the incrementation again now we're going to get a 10 and the test is going to say is 10 less than 10 and that's going to be false now the test is going to fail and we're going to fall after the while loop here so if we had a statement here that said stdc out loop done we would print that we would go down here and say loop done and this is what we're going to get if we run this program and again i encourage you to try this out in your mind print this in some file you have on your computer or you can even use a regular piece of paper but make sure you understand the flow we go through to get this code to run multiple times okay now that we have done this we can work this and let the computer do the heavy work for us and run this fence multiple times the world is going to be good so what we're going to do we're going to say clear and run rooster and it's going to say exactly what we expect i'll have c plus 10 times and then it's going to say loop done okay one thing i want you to see is that the do while loop runs before it does the test and this may cause crazy things happening in your program if you're really not careful let's say that you wanted this program not to run and initialize this thing with a zero for the count so it's going to run zero times but that's not going to do what you expect with the do while loop because it runs things before it does the test so if you run this program it's not going to just say loop done it's going to say i love c plus plus and have zero value in here and it's going to say loop done but that's not going to be probably what you intended by putting a zero in here you put a zero in here if you want this code to run zero times or the code in the body not to run that's another way to say that but in this case a do while loop is not going to do what you expect because it runs before it does the test here let's build this and show you this so we're going to go through the world is successful and we are going to clear and run rooster you're going to see that it's saying zero i love c plus plus loop done it is running even if we set our count to zero here so be sure the do while loop is doing what you want but if you are careful about this you're going to have no problems and if you want the rope to actually not do anything when you have zero counting here you can use the while loop or the full look like we have seen before i would like to welcome you in this new chapter where we're going to be learning about arrays and arrays are a way to set up collections in our c plus plus program so far we have been working with single variables for example you have a bunch of integer variables you can even have 10 or 20 variables of integer type and that's going to be fine but sometimes you don't want to manage all these variables it is really desirable to group all these variables into one single unity and manage them as one single entity and we can do something like this group them together and give them a name so that we can refer to them or manipulate them under this name here and c plus plus has the array construct to help us do this okay so again if you look in memory how things are laid out you're going to have a bunch of zeros and ones for example here each red defend is a net it is four bytes in size and it can be used to represent a net so if we have a bunch of events like this we might want to group them together and give them a name and manipulate them under this name this is another way to look at what we just talked about here and again arrays are a way to do these kinds of things in c plus plus and we're going to be learning all about it in this chapter we are going to stop here in this lecture and we are going to start and look at how you can declare and initialize an array in the next lecture go ahead and finish up here and meet me there in this lecture we're going to see how we can declare and use arrays in our c plus plus program and again arrays are a facility we have in the c plus plus programming language to group many variables together and manipulate them as a single unit for example here we have a bunch of integers in memory each of these red things is going to be four bytes in size so we can think of it as an integer and we can group them together and give them an a we call them scores here and they are going to be living under that scores entity so if we want to refer to each of these things z plus plus provides an indexing system so we cannot use angle brackets like this and refer to the first of these integers as zero so we can go from zero all the way to nine here but your array can even be bigger than this many times bigger than this and we're going to see examples of how we can do that but this is an example of how you can take many variables group them under one entity give them a name and be able to reference each of these events that are under that entity and now that the idea is pretty clear let's see how we can do this in c plus plus the way to do that is to say and you have to specify the type of your collection you have to specify the name after that and then you're going to put a pair of angle brackets like this and inside those angle brackets you can specify the size you want that collection to have or that array to have and after this statement is run we are basically going to have something like this in memory we're going to have 10 integers so if you count here 0 all the way through 9 that's 10 integers they're going to be grouped together under one logic entity that logic entity is going to be called scores and we can go through that name to manipulate all these things that are in this entity okay after your array is declared and you have space for it in memory basically something like this we need a way to read data from at and we're going to see how we can do that and it is what we have here for example if we want to print the first of these elements we're going to say scores angle brackets and specify the index of the first element in that array or that collection of integers that is called scores okay one thing you should really keep in mind is that arrays are indexed from zero they don't start from one so the first element is going to live at index zero it's not going to live at index one so here scores zero is going to print the first element and scores nine is going to print the last element which happens to be the last in our collection here of integers okay once our array is declared it's not going to have data in it there is going to be data in but it's not going to be data that we put in so we can think of this data basically as garbage another thing i wanted to bring to your attention is that your array has clear boundaries when we declare an array like this notice that we specify 10 as the size of our array it's only going to have 10 elements inside and you can see that we have from index 0 all the way to 9 and inside we have garbage data but we're going to fix that in a minute what i want you to see here is that you can to try and read outside the bounds of your array c plus plus is funny in that it allows you to do that but if you do that you're going to get weird problems for example your program would crash or do something really bad so you shouldn't really do this be aware of the boundaries of your array if you try to read the data from the outside of your balance you may read garbage data you may even cause failure of the operating system because you don't really know whose memory you're reading it's not going to be memory that is allocated to your program so array boundaries are something you need to keep in mind when working with arrays okay we just saw that we could read data from the array using this angle bracket syntax we can even do that through the loops and read all the elements in the array and this is how we could do that and you can see that the loops we learned about in the last chapter are starting to come in handy okay we have seen how we can read data from the array but what if we want to put something back in what if we want to write our own data in an array well the syntax is pretty much what you would expect we also use this angle bracket syntax but we're going to assign data to that element in the array for example if we wanted to assign data to the first element in our array we're going to say scores angle brackets 0 and we're going to assign a 20 in it and we can assign a 21 into the element at index 1 and the 22 in the element at index 2 and if we do that we're going to have something like this in our array so index 0 we have a 20 index 1 we have a 21 index 2 we have a 22 and the rest is still garbage because we didn't put in our data we can also write data in a loop for example here we have our array declared and we're going to loop around putting data at different indexes notice that the index is going to be the current iteration we are at so this is a common way to do things in c plus plus and you need to be familiar with this and after we put in the data we can read that back and make sure that it is the actual data that we put in and we're going to have a chance to play with this in visual studio code in a minute so don't worry if some of these things don't make sense yet but again the main thing here is this angle bracket syntax we can use that to either write data in the element of the array or read data from the element of the array and this is the syntax here scores angle brackets i okay so far our array has been declared and we didn't really put in data and there was junk data inside but there's a way you can declare an array and put data in in one single statement and this is how you would do it here we have another array it's going to be grouping double variables we call the array salaries and we specify that it's going to be of size five and we can put these elements in a comma separated list using braced initialization like this and it is going to work if we try to print it out we're going to see these elements here and you see that we can read this using the same syntax we've learned about salaries we say the array name we do angle brackets and inside we put the index of the element we are reading from and this index is coming from our loop here and it is going to work you're going to get this element printed out this is pretty cool when initializing your array you can leave out elements for example here we have a neat array it is called families it's going to be of size 5 but we are just putting in three elements the rest are going to be initialized to zero when we do things like this and this is something you need to be aware of and we are going to try this out in visual studio code and you're going to see that this is actually true it is possible to also omit the size when you are declaring your array and the compiler is going to deduce the size from the elements that you initialize your array with for example here we have in one two three four five six elements this array is going to have a size of six and if we print elements we're going to get those printed out notice here that we are using a ranged base for loop and hopefully you can see that it is easier to read data from a collection like this using a ranged base for loop because we don't have to set up an iterator initialize it to do the test do the incrementation we can just read the value and use it and be on our merry way this is pretty cool it is possible to also declare your array constant and when you do that you won't be able to modify elements of that array that's what it means if you make it constant you basically want it to stay constant and you don't want to allow modifications of your array and we're going to see this in a minute when we get in visual studio code the last thing i want you to see is that you can actually do operations on the data that you have stored in an array for example here we can sum the elements up and store the result back in our variable sum and this is something you would do and it might come in handy in many times okay one thing i want you to see before we head over to visual studio code and play with us is that arrays store elements of the same type repeat after me arrays store elements of the same type you can't try and store variables of different types in an array i don't even know how you would actually declare that and if you try and store a different type than the type that was declared for the array you're going to get a compiler error so this is something you should keep in mind but it makes sense because when you declare an array you're going to be given a bunch of bytes in memory and those bytes are going to be collections of data that is of the same type for example here we have a bunch of integers again each of these red events is an integer and if you try to put in something that is smaller or bigger than an edge then the compiler is going to be confused that as to how to read these text so for arrays to work the types that you store in the array have to be the same and they need to be consistent this is something you should know and if you try to break this rule you're going to get a compiler error now that you know about arrays we're going to head over to visual studio code and play with them a little bit okay here we are in our working folder the current chapter is raised the current project is declaring and using arrays we're going to grab our template code and we're going to copy and move the files over and we're going to open this thing up in visual studio code pretty quick we're going to open up the main cpp file and remove things we don't need and we're going to see how we can declare an array of ants okay the way you do that you say the name of the arrays and you put these angle brackets and specify the size of your array if you want you can put a space in here it's really not going to make a difference and you're going to put a semicolon at the end the moment this line executes we're going to have the space for 10 integers allocated to us in memory and that space is going to be called scores and we're going to be able to read data from it or even write data back into it for now let's see how we can read the data and it is really simple we're going to use this angle bracket syntax that we just saw in the slides so if we want to read the first element we can say sddc out we want to print it out and we're going to say scores specify the index we want to read from so we say zero and we're going to say std endl to move to the next line if you have this typed out you're going to see that this is going to work and it is going to print whatever is stored in the first element in this array here and again i want you to keep in mind the memory structure of your data when you are working with arrays so what we're going to print right now is whatever junk data is in this first element here okay in here we're going to say that we haven't jumped yet and let's bring up a terminal so that we can play with us and we're going to build this with gcc as we always do the build is going to be good and we're going to clear and run rooster you see that this is the junk value we have in here we don't have any useful data in here it's just junk data because we didn't initialize this array we can also read the element at location two and see what we have in there when you run the program i'm not even sure if it is guaranteed that you're going to get the same data if you run the program multiple times but we don't really care we know that it is junk data so we can't use this in our program what we're going to do is run this and see the value for the element at index one let's build with gcc the world is going to be good we're going to clear and we're going to run rooster and you're going to see that at index 1 we have a zero end but doing it manually like this is really time consuming and it is possible to actually do this in a loop we know the size of this array so it's a 10. and we can loop around 10 times pretend each element at each iteration and the way we do that we're going to read with that loop and we're going to say for okay size t and we're going to start from index to zero this is really important because arrays are indexed from zero they don't start from one again this is something you need to drill in your brain and we're going to put in our test case we're going to say i less than 10 because we want to go from 0 through 9 and we are going to increment our iterator after we do that we're going to have to jump into the body of the loop and we didn't name our iterator that's why we're having this squiggly line here let's do that pretty quick we're going to jump into the body of our loop and we are going to say sddc out and we're going to say scores and we need to be careful about how we format this because i is not something we have we're going to be getting that from our iterator here so we're going to print it like this and this is going to work no big deal and we're going to say squares and we're going to say i that's going to be our index that we want to print the data for and we're going to say stdendl and uh because we don't want this to confuse us we're going to comment that out and we're going to go up and make sure our comment is nice and neat and we're going to try and run this and what do you expect to see well we don't have any real data in here we're going to have some junk data so it could really be anything we get printed here so let's build with gcc and we're going to give ourselves some breathing room we're going to go down and clear and run rooster and you see that um this is really some junk data and if we run it again let's see that we get the same output it is the same data in here if we want to have in some reliable data we need to write in our own data and we're going to see how we can do that we're going to see how we can write data into an array and the syntax is really simple so the way you do that you just say the index for which you want to write data in and you say scores and you specify the index let's say zero and i'm going to put in a 20 for example this is how you do this you say 20 let's say scores we're going to grab the index one the first element and we're going to put in a 21 why not and we're going to say scores we're going to say 2 for index 2 and we're going to put in a 22 why not and after we do this we can print this out or read data with the loop and make sure that the data we did writing is actually n so we're going to copy this and comment this out because we don't want this to confuse us on the console and we're going to do that we're going to come down here and print the data out let's say what we're doing here and if we build this we expect to see that at index 0 1 and 2 we have the values 20 21 and 22. let's build and see we're going to run the test to build with gcc the build is going to go through we're going to clear and run the rooster and look at this score is a 0 20 score is 1 21 score is 2 22 and our data is actually getting gain now we're going to see that we can also write the data using the loop to make it really easy without us manually doing things like this for that we're going to copy what we have here to print data out and we're going to comment what we had out here so let's comment this out and we're going to go down and uh print the data again we're going to put in the code here and before we print the data we're going to put data in with the loop so the loop is basically going to be the same thing we're going to say size ti we're going to initialize this we're going to say i less than 10 because we want to loop 10 times we're going to increment and we're going to fall in the body of the loop what we're going to do is write data at the index of the current iteration i should say so what we're going to do we're going to say scores i and we can really put in anything for now let's say we want to put an i multiplied by 10 so we're going to take the current index multiply it by 10 and we're going to store that in the index at the current iteration this is what we are doing here and if we print this out try to think about what we're going to get here so at index 0 let's put in a 0 to be super clear here that we are starting from index 0. at index 0 we're going to take 0 and multiply that with the 10 so we're going to store 0 at index 0. the loop is going to run again at index 1 we're going to take a 1 multiply with 10 we're going to get it 10 at index 2 we're going to multiply 2 with a 10 and store a 20 at index 2 and we're going to keep going and basically have 0 10 20 all the way to 90 stored in our array here so if we print it out we expect to see 0 10 20 all the way to 90. let's weld and see that that's actually what we have in our array we're going to work successfully let's clear and run rooster and you see that this is exactly the same thing we have in our array how cool is that okay another thing i want you to see is that we can declare and initialize an array in place but before we do that let's say what we were doing here we were trying to write data in a loop and we're going to comment this out because we don't want a lot of confusing output in our console and we're going to go down and put in the data because i don't want to type all these thanks so we can declare and initialize an array in place and the way we do that the array is going to store doubles now we can really declare an array of anything but so far we have seen that we can do that for ant now let's try a little bit and show you that you can do the same thing for double times so we can declare an array of double types the size is going to be five and we're going to initialize that with this data here so at index zero we're going to have a 12.7 index one a 7.5 index 2 a 13.2 index 3 an 8.1 and index 4 and 9.3 and that's what we're going to have if we print this out here we have a simple loop to print it out and it's going to basically say salary 0 12.7 and it's going to go all the way to salary 4 and print a 9.3 and this is something you can do if it makes sense for whatever you are designing with c plus plus we're going to build this with gcc let's clear and run and you see that this is exactly what we expect okay now this is super clear the next thing we're going to see is that if you don't initialize all the elements in the array those you leave out are going to be initialized to zero and we are going to put in a piece of code to play with that so here we have an array it's a net array it's going to be called families and the size is going to be five but we are only initializing three elements and these elements are going to be elements on the front so elements at index 3 and 4 are going to be left out and they are going to be initialized to 0 by the compiler so if we print this out we expect to get 12 7 5 and 0 0 at different indexes in this array here let's build this with gcc we're going to clear because the build is successful and we're going to run this this is exactly what we expect we have a 12 a 7 and 5 and the last two elements are initialized to zero and this may come in handy sometimes okay let's comment this out another thing we want to see is that we can actually omit the size when we are declaring our array and initializing it so here is a simple example we have an inter array called class sizes we don't specify the size but we initialize it and the compiler is going to deduce the size of this array from the elements we put in here so if we put in two elements so it's going to be of size 2 if we put in 10 elements it's going to be of size 10 and this is how this works but if you do something like this and not to initialize the array let's take this out for example you're going to get a compiler error because the compiler now doesn't know which size it's going to give to this array so be sure to either put in the size or initialize your array like this and it is going to work in this case we're going to have six elements and and you see that a range based for loop is going to come in handy here because we don't really know the size of this array if somebody comes and adds another element let's say 23 the size is going to change and if we hard to code the size in our full loop the code is going to break but a range based for loop is going to use whatever size this already has so we're going to try and build this we're going to build with gcc as usual and the world is going to go through if we run rooster we're going to get the elements we have 10 12 15 11 18 17 23 if we add a new element this is going to work you see that we don't need to modify the loop here to put in the new size and this is really cool i like this about range based for loops we're going to build again and we're going to clear run the rooster and this is going to give us exactly the data we have in the array so this is one way you can do things another thing we saw is that you can't modify const arrays so let's do an example here for example we're going to grab this thing and uh put an array declared down here we're going to copy that and use that here and we're going to make this cost let's give it a name let's say birds for example for example we want to keep track of lines of birds or whatever this is just an example to show you that you can't modify this array so if you try to do something like birds and index 2 for example and you want to change this to an 8 the compiler is going to complain you see that visual studio code is complaining already expiration must be a modifiable value so it is saying that the array stores const elements so you can't modify elements of this array okay so this is something you can do if you don't want people to be able to modify data in your array and the last thing i'm going to show you is that you can do operations on the data in an array so let's bring up our scores array again so we're going to go up and copy it because the code is using there so let's put in our own scores array it's going to be pretty fast and we're going to say that it is an array with this angle brackets we're going to initialize this let's put in a bunch of values and what we can do is sum these things up we're going to store our results in the sum variable and we're going to loop around with a ranged based for loop because we don't really know the size of this array it can change any time with however many elements and a range based for loop is really cool in that we don't have to keep track of the size of ourselves so we're going to go in here and get each element and add that to some so for example for the first time sum is going to be 0 because that's what we initialize it with we're going to get into this loop here the first element is going to be 2 we're going to add that to 0 and get 2 we're going to add a 5 we're going to get a 7 we're going to keep adding this element and at the end sum is going to contain the sum of all these elements in the array here and this is one cool way you can sum up the elements in your array so if we run this we're going to get the sun printed out let's do that pretty quick we're going to run this has to build with gcc and we have ourselves a compiler error what is happening here it's going to say redeclaration of scores where did we declare scores before let's go up and make sure it is not declared somewhere and it is declared here so we want to comment this out come back to our code so that we can see it and we're going to build again with gcc and now the world is going through we're going to clear and if we run rooster score sum is 37 and if you sum these things up i'm sure you get 37. this is really all we set out to do in this lecture i hope you have a better idea of how you can declare your arrays put data in read data out of those arrays make them const or even do all kinds of operations on them and this was really what this lecture was all about we are going to stop here in this lecture in the next one we're going to see a cool way we can use to get the size of an array go ahead and finish up here and meet me there in this lecture we're going to see a way we can query for the size of an array at runtime if you remember in the last lecture we had an example like this showing how we can declare an array and initialize it in a single statement like this but the problem we had with this is that if we tried to print the array using a for loop for example we would have to use the size and the size of this array may change if somebody decides to add elements or remove elements then the size is going to be irrelevant c plus plus provides a way we can query for the size of an array at runtime and we're going to get the real size accounting for the current number of elements that we have in the array and for that we use the std size function this is a function that comes with the c plus plus standard library so you're going to have it already if you have a compliant compiler if you see here you see that it was introduced in c plus 17 and it is a cool addition we can use it to make our code easier to work with if you look here we have a simple loop which is going to print data out it is going to loop starting from zero all the way to the size of the array you see that we use this to control when this loop is going to end we then put in our incrementation part and then we jump into the body of the loop and print the current element in the loop and if we do things like this we're going to be able to print all elements in the array regardless of the number we initialize it with okay so this is one way and it was introduced in c plus plus 17. but some of you must be asking how did we do things before z plus plus 17 well we had to kind of do a hack to get something like this we use the size of operator we have seen a lot of times right now and the way you use this if you do size of and passing the array you're going to get the entire size of the array so if we take that and divide that with the size of a single element in the array we're going to get the number of elements of the array and this is really what we are doing here we are setting up a count variable and we are taking the size of the array and dividing that with the size of a single element in the array and we are just grabbing the first element because you can really use any element here and once we have this we can use this count to do a loop like this and it is working pretty well please remember that you don't have to go through this craziness we're doing here you don't even have to use std size because you can use a ranged base for loop and get access to the elements you have in the array and it is going to work pretty well now that you have an idea about this let's head to visual studio code and actually play with us okay so here we are in our working folder we are going to be working on this project size of an array we're going to grab our template project we're going to copy the files and put them in place and we're going to open this up in visual studio code let's do that it's going to open up in visual studio code and we're going to try and clean it up a little bit and we are going to declare an array if we want to print this thing we need to know how many times we're going to loop and print elements in here so one thing you might want to do is to say okay we have 10 elements in here so i'm going to loop for 10 times you might think this is going to work and it is going to work if we bring up a terminal and build this with gcc like we usually do and uh clear and run rooster we are going to get our numbers this is what we have in the array from one to ten but what if somebody comes and changes the elements we have in here let's add an 11 and a 12. and the moment we change this we will need to come and change the size in here and this loop here might be buried somewhere deep in your project and it might not be as easy as it is now to see that we also need to change this this may be a really big source of hard to debug problems so what we really need is a way to get the size of the array at runtime and be able to dynamically query for that size and use it in our program and that's exactly what std size provides so we're going to say end count and we're going to put in a value let's use a braced initializer so we're going to say std size and we're going to pass the array we want to get the size for and if we do this we're going to get this size here and we're going to be looping f
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact