Learn C# Beginner FREE Tutorial Course! [2024]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I'm your codm welcome to the free beginner section of my C Mastery course by going through this course you'll be able to make games website apps Robotics and literally anything C is an extremely versatile language starting from the absolute Basics like how code executes line by line and what is a variable then covering the intermediate topics like what are interfaces generics and events and how you can use them to build some really awesome things you will also learn the theory behind how to choose good naming rules clean code guidelines and learn about design patterns and finally the advanced section which will be coming in a future free update we'll cover lots of very Advanced topics now this free YouTube video this one contains all the video lectures from the beginner section in the previous video I spoke about my goals to make the video lectures free over here on YouTube if the premium version course sells 100 copies then one month after this video I will publish intermediate section for free here on YouTube and same thing for the advance section one month after that and yep since the last video there's already been 100 people who picked up the premium version so one month from now stay tuned for the intermediate section or if you're watching this in the future then check the pin comment down below the premium version of the course has the video lectures just like this video plus all of the intermediate lectures and the advanced section coming soon as well as all of these really nice bonuses So speaking of that here are the bonuses for the premium version I came up with some that I think are really awesome and definitely worth it if you can't afford them well at the same time not putting any knowledge behind the pay wall so even people who can't afford it they can still learn from the free video lectures they really just need to do a little bit of extra work themselves the premium version is really just selling convenience making it really easy for you to truly gain the knowledge the the premium version comes with a companion project this is a Unity project that contains a bunch of extra content basically the video lectures while being preas of themselves there's still really only half the course the other half of it is inside of this project it has a bunch of custom editor Windows to guide you through each lecture and each lecture has a section on frequently asked questions these are common questions that have some really detailed answers that provide even more details on what is covered in the lecture then there are some quizzes these are multiple choice questions again own with very detailed answers it's really not just correct or incorrect if you pay close attention to each lecture you should be able to get most of these right but whether you do or don't by reading the extra explanation that will help you truly learn that topic then the interactive exercises I'm really happy with this feature I think this will really help you learn learning by doing is always much better and these exercises encourage you to put what you learned into practice instead of just blindly watching the video lectures I designed a ton of handcraft exercises for all the lectures to help you put into practice and truly learn the contents of each lecture there are all kinds of exercises some are about spotting and fixing errors others are about asking you to Define some function or class or Implement some kind of logic there are some where you just write code and somewhere you write code and then play the code in unity in order to complete the exercise then each exercise also contains a hint just in case you get stuck or a solution in case you get really stuck as well as a video walkthrough of me going through that exercise and completing it while explaining everything in detail so if you're the kind of person who gets stuck in tutorial hell then I truly believe that this will really help you escape it in order to learn you need to actually do things and this exercise in encourage you to do that they encourage you to do it as opposed to just blindly watching the video lectures then the companion project also has a companion window basically this window is also listening to a bunch of errors and if it finds one it will help guide you in the right direction I manually wrote a ton of text for when text lots of errors all of them based on common errors that I see people ask about in comments in my own videos so this should help prevent you from getting stuck in your Learning Journey and allow you to get helped instantly then the course also has an AI to help you answer questions this one was trained on the contents of the course and my own knowledge so it should be very accurate and helpful but more than that it's simply the fact that it is extremely fast you post a question in the course comments and within a few minutes the AI will respond with probably a very good answer although of course I myself will also still be answering all the questions manually the goal with this AI is really speed so you get a response almost instantly and then within the next few hours I will manually answer myself then a simple bonus is how the premium version has the course split into lectures as opposed to Here on YouTube where it won't be one giant video it has to be that way due to how the YouTube algorithm works works this is a small thing but it can be helpful especially if you take your time to slowly go through the course just like you should do remember that the only goal is really that you actually learning yourself it does not matter how quickly you go through it so separate lectures help with that so that you can pause and actually try out your knowledge before going further and you don't have to memorize specific time stamps and over here on YouTube the videos have the normal YouTube ads as usual whereas the premium version has no ads so there's nothing to interrupt you while you're learning another really nice bonus of the premium version is something that I'm going to try out for the first time which is a live study group basically once or twice per week I will go live and post a link to the live stream so people can join in there I will watch the lectures alongs side of you then we're all going to read the frequently asked questions together do all the quizzes and go through all the exercises naturally I won't be reading the chat so I won't be able to answer any specific questions you have I've really never done anything like this so I'm really curious to hear what you all think some people like to learn more as part of a group so hopefully this should be quite fun the first live study group is happening next Saturday March 9th and if you can't live then as always they will be recorded so you can watch the recordings later and final bonus is really just by knowing that by buying the premium version you won't be helping hit the goal to make the free video version available for everyone so you will be helping to make knowledge available to anyone who wants to learn so these are the nice bonuses that I came up with that I think are really nice and really awesome while not taking away anything or putting any knowledge behind the pay wall if you can afford then I highly recommend the premium version these bonuses will help you truly learn and learn much much faster than the 10 years that it took me but if you can't afford it that's okay like I said there's no hidden knowledge hidden behind the paywall you really just need to put more effort in yourself in order to actually apply knowledge that you gain and if you want you can go ahead and pick up the premium version right now there's a link description I added a nice launch discount so you can get it before the first live study group next Saturday so whether you get the premium version or watch your free lectures I really hope you'll learn a lot also just in case you're new to my channel so a bit about me and my credentials or why you shouldn't listen to what I have to teach for me I've been programming for over 25 years and specifically using C for over 10 years I'm a professional indie game developer with several successful games PO on seam covering a wide range of genres my games are usually focused on complex systems since I am primarily a programmer it's what I really love to do and what I love to teach also a couple of years ago I started over here this YouTube channel making free video tutorials to basically share my knowledge to help you learn how to make your own games there's already over 800 free videos over here on this channel if you want to continue learning Beyond this course also let me make one very important note for beginners which is simply take your time remember this is your Learning Journey no one else's the only thing that matters is that you're learning it does not matter how long it takes you so it does not matter if someone learns all of this much faster than you you are really not in competition with anyone but yourself also you are not expected to go through this course in one day one week or even one month basally all the contents out of this course this contains knowledge that took me 10 years to learn so really just take it slow take your time and really just focus on learning and one final request if you find this video helpful if so go ahead and hit the like button and subscribe to the channel it's a tiny thing but it really does help all right so let's get to the first lecture starting from the very beginning first we're going to learn what is C and what have the many use cases for it then on the getting start lecture we're going to install Visual Studio which is where we're going to run our code then we're also going to install unity and learn the unity Basics this is an optional step but it's important since the interactive exercise and quizzes those exist in a Unity project after that we're going to load up that project to see how the interactive exercise work and then is a really useful lecture covering some very common errors you should refer back to this one whenever you encounter some issues after that one we can finally start writing some code beginning with the absolute Basics learning how the code is written how it executes what is a code block and how code is case sensitive then we're going to learn about variables these are containers for data the data needs to have a specific type so then we're going to learn about the various built-in data types things like int B string and so on next we're going to learn about if statements this is a core pillar of any programming these are conditions which affect how your code will execute after that we'll learn a different way of handling conditions using a switch statement next we'll learn about another core pillar functions this is how we can place logic in separate functions and call them when needed after learning about functions which involves different code blocks then we are going to learn about variable scope which is basically from where that variable can be accessed next we'll learn about comments the Syntax for this one is actually really simple but it does bring up a lot of interesting questions regarding writing good clean code then we're going to learn how to store multiple pieces of data in one container using arrays and lists afterwards we're going to learn all about loops this is how we can run certain logic multiple times like for example looping through all members of a list then another crucial pillar of object-oriented programming we're going to learn the basics of classes how to define one and construct it next we will be ready to learn about static what is the difference between making something static and non-static then learn about access modifiers this is another one that is super simple in terms of syntax and extremely important in terms of learning to write good clean code after that comes one of the most important lectures in this whole entire course it's a lecture on naming rules and how you should be very careful and and very deliberate about the names that you use in your code and after that is yet another extremely important lecture this one covering the theory behind why you should write good clean code as well as some nice simple guidelines that you should follow with all that done we will have learned the absolute Basics so we will be ready to inspect a nice beginner project that involves everything we learned in this section all of that makes up the beginner section and afterwards comes the intermediate section we're going to take things up one level and learn some more complex stuff beginning with something very important that I debated whether I should put in the beginner section it's going to be Visual Studio shortcuts I end up putting it here because for beginners technically it's more important to learn how to write the code and it's not as important to be extremely efficient with how fast you use your ID but for intermediates this is a must have so we're going to learn about a ton of shortcuts to help you write code and navigate your environment much better then comes one of the most important lectures in this whole intermediate section it's all about refactoring this is the process of rewriting your code to make sure it is as readable and understandable as possible as an inter it you absolutely need to know that this is a natural part of the code writing process and something you should not skip next is a lecture on enom pretty simple and very useful it's how you can define a specific list of values that you can use in many ways after that we'll learn about properties which are kind of like a mix between variables and functions then we're going to learn about multi-dimensional arrays meaning arrays that can hold multiple dimensions of data after that we're going to learn about nest and Loops which is especially important in order to cycle through multi-dimensional arrays next we're going to learn another method for doing some looping logic we're going to learn about recursion this is something that for people with a math background it might actually be similar to understand than R Loops then we're going to learn about an extremely useful data type the dictionary this is a key value pair that has tons of really awesome use cases after that we're going to learn about some more collections like q and Sack then we're going to learn about the prams keyword and what exactly does it do in a function next we'll learn about optional parameters how to define them and how to call them from a function then another crucial lecture for intermediates this one is on learning the differences between value and reference types this is extremely important if you don't know this difference then you will go crazy at some point in the future wondering why something isn't changing and it's usually going to be because you don't know you're working with a copy and not a reference related to that is the lecture on structs these are similar to classes but importantly they are a value type next we're going to learn some more intermediate use cases for classes we're going to learn about inheritance polymorphism and a bunch more then learn about interfaces this is one of my favorite features of C it allows you to write some really nice modular code related to that we're going to see various ways of testing for types and converting two and from various types also related is the lecture on switch pattern matching this is a really simple way to do a switch not just on value but also on type then we're going to learn all about delegates lambdas and Anonymous functions this is how we can store a function itself inside a field next we're going to learn the differences between lambdas and local functions after that we'll learn about events which is how we can have one class be notified when something happens then the next lecture that one is going to be all about generics this is another super powerful C feature that makes your code work with multiple types next is a simple lecture on conents and read only what they are and what are the differences between those two after that is in lecture on exceptions and how to handle them with a try catch then in lecture on implementing I enumerable so we can add support for something like a 4 in for some custom types next we're going to learn about namespaces what they are and how to make your own then a really important lecture on one of the most important design patterns the single tent pattern and after that is a general overview of a bunch more design patterns and finally after doing all of that we're going to check out the intermediate project that uses all of this to make something really interesting so that makes up the intermediate section and then comes the advanced section for this one like I said in the beginning I'm still actively working on it so this will be coming later as a free update here are the topics that I'm planning to cover all right so that's everything we're going to learn in this complete C course like I've said many times you are not expected to go through all of this in just one day or one week or even one month it took me 10 years to learn all of this so always remember that and take your time and really just focus on actually learning this is your Learning Journey so that's really all that matters take it slow really pay attention do all the content in the companion project and I guarantee that if you do that then by the end you will have gained a massive amount of knowledge hello and welcome I'm your Cod monkey in this lecture we're going to learn what exactly is C and what it's used for okay so C is a program language it was created by Microsoft in the year 2000 it is an objectoriented General multi-purpose language with tons of use cases for me specifically the main way that I use it is for game development with unity but you can also use it to make websites with asp.net or Blazer you can use it to make Native mobile apps with zamarin you can do tons of stuff in the cloud it's especially easy to use with Azure which is Microsoft's cloud service you can develop on Internet of Things devices or you can use it to make some simple desktop apps to help you in your daily life you can make console programs or visual desktop applications so it has tons of use cases it is multiplatform meaning you write C and you can run on Windows Mac Linux and other platforms the language is constantly evolving despite being 24 years old it is still actively being developed with new versions coming out on a regular basis it is also one of the most popular and independ languages so it's a great one to learn personally I also believe that it's a great first language to learn I especially like how it's type save meaning it has very specific types and if you write some code incorrectly you will get a compounder warant instead of having an error suddenly show up while the code is running and if you're not a complete beginner if you already know Java or C++ then chances are you won pick up pretty easily since it is relatively similar to those languages so yeah personally I'm a huge fan of C I've been using it pretty much every single day since I started game development with unity over 10 years ago I've made tons of games with it and I've also used it to make some personal desktop apps and I also use it when I explore the cloud so it is a great very valuable language to learn and if you pay close attention to this course then by the end you will have gained a massively valuable skill hello and welcome I'm your K monkey in this lecture we're going to go over how you can get help when you need to and how to get the most out of this course okay so learning programming can be a little bit tricky at first especially if you are a complete beginner right away let me say that is perfectly normal if you are a beginner and some things seem quite confusing that is perfectly okay that's all part of the regular learning process so there's really no point in getting frustrated just take your time and really just focus on trying to learn in order to help you learn this course has a bunch of things for every lecture the unity project contains a bunch of helpful common frequently asked questions I highly encourage you to always read through those as you go through each lecture and whenever you have any question then I also highly encourage you to go answer all the quizzes for every lecture and do all of the interactive exercises you will learn so much more if you're being active in your learning as opposed to just passively watching a video so definitely make sure you do those after watching every lecture pause go to the N project and do all of those also on that Unity project if you write some code that has some error the companion can provide some extra clarification on what exactly that error means so if you're having some issues and you're writing code in the C console try writing that same code inside Unity to see what the companion says then there is a really important lecture covering a bunch of common errors if you have errors in your code chances are the error has an explainer in this lecture also when you have some issues the first step is to actually identify what exactly is the issue and for that the best way to find it is to add tons of logs in your code at various places to see exactly what the code is doing line by line there are many cases when beginners think the code is doing something but in reality it's doing something completely different adding logs is a super easy way to help you visualize exactly what the code is doing and of course you can simply post questions in the comments of that lecture one bonus this course is the automatic bot response you can ask something and within two to three minutes the bot will reply I train the bot on the contents of the course and my own knowledge so the answer should be pretty good the main benefit of the bot is the near instant reply so you don't have to wait for me to reply and you can continue learning quickly but also after the bot replies within the next 24 hours I will manually answer your question and confirm or provide some extra clarification on whatever the bot said then here's also a quick guide on how to ask a good question for example do not just say it's not working just saying this is really not helpful technically not working can mean a million different things for example do you have a compile error if so what exactly does it say specifically what line of code is throwing that error or if it's not some kind of compile error how exactly is it not working so did you expect one result but got another did the code seem ly not run are you getting some different results from what you see in the video in order for me to be able to help you I need as much information as possible there's really not much I can do to help if the only message I have is it's not working so when posting some question be very very specific about what exactly is the issue so I can do my best to help all right so that's see many ways you can get help throughout this course just keep in mind what I said in the beginning if you are a beginner It's Perfectly Normal that something seem quite tricky to understand that's all part of the regular learning process so don't get stressed about it all that matters is that you learn learn it really does not matter if it's easy or hard if it's fast or slow as long as you're learning then you're doing great remember that I've been programming for over 25 years so if you manage to get through this entire course and fully learn everything in it in just a few months then you're doing so much better than me so really just take your time use all these tools at your disposal to really learn and let's get started in the next lecture hello and welcome I'm your Cod monkey in this lecture we're going to install and set up visual studio which is where we're going to be writing our code okay so let's see where we're going to write our code you can use whatever ID you want ID stands for integrated development environment some common examples for C are visual studio code jet brains Rider or normal Visual Studio you can follow this course using whatever idea you want but if you're a beginner then it's probably best to use the exact same as me here I will be using visual studio 22 the community version this one is free also an important distinction it is Visual Studio not Visual Studio code it's a bit confusing how the names are very similar but those are two completely different programs here I will be using the regular visual studio so like downloaded go to visual studio. microsoft.com then click on downloads and download the free community version then go ahead and open up the executable file after installing right away it opens up the visual studio installer this is the main program that manages all of your installed versions of visual studio and right away we can install Visual Studio Community alongside a bunch of modules like I mentioned in the previous lecture C can be used for a lot of stuff and visual studio you can also develop on other languages so here you can see tons and tons of modules the ones that we're going to need in this course are the net desktop development this will let us make console apps which is the main template that we're going to use to learn C so make sure to include that one and then since the interactive exercises since those are built in unity also make sure to include game development with unity okay so these are the two main ons that we need so let's go ahead and install this will download all the modules and install them okay it's done and launches visual studio right away now it's asking for a sign in this can be useful to transfer your settings across devices so if you want go ahead and sign in or just click on skip for now then for the theme again you can choose whatever you want personally I'm going to go with the light theme which is what I like but you can use whatever you prefer and right away let me answer the question that I inevitably get every time I say that I prefer the light theme when so many people seem to prefer Dark theme for me my eyes simply get really messed up in dark mode if I try focusing on white letters on a black background I get a massive headache really quickly so personally for me I use light mode but this is really just a visual change so choose whatever you prefer okay with that let's start using visual studio and Y right away we can see the project list although it's empty right now we can open a project or create a brand new one so let's do that then here we can choose the templates let's go ahead and make a simple console app which is the most basic thing possible so you can search for console up here and now yep it is this one right here the one that has the C icon in the corner it is not this one with VB that's for Visual Basic and also not this one for F this is for f and it is also not this one that says C but also says NET Framework it's a bit confusing but net and Net Framework those are two completely different things right now in here we want net and not NET Framework so yep it's this template also if you don't see it then go back into the visual studio installer click on modify and install some modules and then make sure net development is checked okay so this is the template that we want the console application so let's use this one okay now let's give it a name you can name it whatever you want here I'm going to go with the basic hello world then go ahead pick a location to spawn your project and leave the solution the same name as default okay so like this let's go on next then over here we can choose the framework so for this let's go with net 8 and now there's a pretty important option here which is whether or not use top level statements basically if you leave this unticked so like this then the project won't be created and look pretty much empty whereas if you take this then the project will be created and look like this with a namespace program and a main function the option for this difference this was added relatively recently so if you watch all their C tutorials they might show this template but for complete beginners it's completely empty option it's actually easier to follow so we're going to use this one later on after we'll learn the absolute Basics we will make another project with the more complex template but really the main takeaway that I want you to learn from here is really just code is code meaning that for everything we're going to learn in the absolute basics part using this simpar template all of that can also be done in this more complex template the difference is simply in how in the basic template you just write code here but in the more complex template you need to write that same code inside the main function if you write code in a different place you can get some strange errors so that is why for beginners the simpler template is much easier so over here to keep things simple let's first use that basic template so make sure to leave this one unticked so example down like this and let's create all right yep so here is our empty template and by default it already has a standard hell World print you don't need to understand this line of code just yet I will explain all of this in detail soon right now we can just run this test so on the top right on middle you can see this nice little play button or alternatively the default shortcut is F5 so go ahead and press either one of those and up here is the window our program ran it did indeed say hello world and everything finished all right awesome okay so we can already start writing some C code and run it now let's change some things in visual Studio there's a really nice extension that I really like which adds a bit more colors to the code which I find that it makes it much easier to read and write so up here on top let's go into extensions and then manage extensions then go ahead and find Vora you can go to the online Tab and search for it now I believe that this one is actually only available on Windows so if you're on Mac you might not be able to find it maybe there is an alternative but I'm not sure either way this is just an optional extension just to add some visual so technically you don't absolutely need it but if you are on Windows and I highly encourage you to install it let's go ahead close visual Studio to let it install okay great so done let's open up the same project again all right so now for some more settings you can go into tools and options and over here let's scroll down to find vs and here are the settings now for the most part I believe the ones that I have here are the defaults you can pause the video and compare my settings with your own I believe I really only changed some colors over here on the rainbow braces right away if we look at our default hello world code it should already have some colors that make it a bit easier to read again this is not a requirement you don't need this extension but I do find it quite useful I find it helps to make the code more readable than having almost everything is gray next for some text formatting rules let's go up here into tools and then go into options then over here let's scroll down and find the text editor and again pause the video if you want I think over here I think I have everything as a default then let's expand upon this one and look specifically at the C options and over here again pretty much the same default I think the only thing different is over here I disabled the automatic brace completion just because personally I don't like it and let's expand upon it see a few more things so these scrome bars then over here the tabs pretty normal they not Advance again I think all of this is pretty much a default but intellisense right like this and then inside the code style in general I think over here I didn't touch any of this so I think the only thing I touched was over here on formatting and the main thing that I changed was over here on the new lines basically the default is to have all of the currently braces all of them on new line and over here I switch this ones in order to make them spawn on the same line which personally I prefer but again this is very much personal preference the code really doesn't care whether the braces are in the same line or new line so you can use whatever form you prefer personally I prefer it like this I've been using this code sound for 25 years now so this what I won't be using in the course but it's just a visual thing so if you really prefer it on new line then go ahead and place it on new line okay so after all that Visual Studio is set up and ready to go now let's continue on to the next lecture we're going to install unity and learn the absolute Basics hello and welcome I'm your Cod monkey in this lecture we're going to learn the basics of unity now like I said this part is technically optional if you're just looking to learn C then technically you don't need to learn Unity but since I won't be using Unity to build the inter active exercises and also using it to Showcase some example code with some fun visuals to help you learn better because of that even if you don't specifically want to learn Unity still make sure to watch this lecture just so you're not confused when any Unity code shows up first we need to get Unity Hub now since we selected unity game development when we installed Visual Studio it already automatically installed Unity Hub in your case if you accidentally did not take that box you can just manually go to unity.com and download it then open up un Hub this is the program that manages all of the various un versions in case you see something that looks like this this is actually the older version of The Hub so like it says here there's a button to update so let's do that and yep here we are in the updated Unity Hub it asks you to sign in so go ahead and do that if you don't have an account you can create one it's free Unity itself has a free personal version so you don't have to pay anything go ahead create an account and log in and right away it selects a version to install here you can install this version right away but we're actually going to install a specific version that might or might not be the same as the pre-selected version depending on when you're watching this so over here either way you can either install or skip this then here we have un Hub open on the left side we have some buttons we can manage our account we can install various un versions or check out the projects so over here on the projects tab we can add a project but before doing that let's go ahead and download the interactive exercises project go on to the course website on this lecture then scroll down and find the downloadable project files yep it's this one so go ahead and download that it's a simple zip file just open it and extract it somewhere and now back here in unity Hub let's go in the project Tab and then up here let's add a project select the one we just downloaded and let's open okay yep so here it is and over here it says the version that this project was built with if you don't have this version you can click this button to download it and yep it will download the correct Unity version now technically I should say you can load the project with a later Unity version doesn't specifically have to be this one but in order to avoid any confusion with menus being in different places in order to avoid any of that I highly encourage you to use the exact same version so there it is everything is installed everything is ready so now we can click to open up this project and yep here is the project open now let's just do a little bit more setup for example let's go up top over here let's go into edit and then let's open up the preferences then over here on this window on the left side let's scroll down into external tools and over here for the external script editor make sure you select your Visual Studio installation or if you prefer you can obviously use a different ID but if you're following everything along with this course make sure you select Visual Studio okay then for the extra options you can leave everything like this and here's a screenshot of my general options I believe all of this is the default so now we can close this window and now if we have correctly set the external editor if we go up here into assets and then click on open C project Yep this one should open your Visual Studio okay great so far so good now just one thing you should already see the code kind of like this meaning with a bunch of colors so for example if I place the mouse over here on top of mono Behavior it should correctly identify this is a class inside Unity engine this is a Unity specific class if you don't see it if you Mouse over this and doesn't recognize this type or if you see this as written in complely Gray if so then perhaps something went wrong during installation so you might need to manually install the visual studio tools for Unity you can just Google Visual Studio tools for Unity you'll be able to find this page and you can can download it but if you follow everything then everything should be working correctly okay great okay so here we have the default Unity layout now first things first let's just familiarize ourselves with the various Unity Windows and create our custom layout you can see there's a lot of tabs everywhere like for example the scene tab the game Tab and so on you can click and drag and move these tabs anywhere you can make them into separate places or you can dock them into various kinds of places so you can basically set up the editor in any way you want but it won't be easier to follow this course if you're following the same layout as me so let's see the layout that I normally use first is over here the inspector on the right side this way we always have a nice view of the select object and whatever scripts it has then for the Hier key personally I prefer that instead of here I click and drag and I put it right here next to this one I like having the Hier key over here on this lower left corner we're going to make sure our scene is always organized so we don't need the Hier key to take up tons of space then with the project files next to it and with the console tab on the next tab next over here in the center here we have the scene View and right next to it the game view this way we can easily view all of our objects the console project everything is nicely accessible so this is the basic layout that normally use now for just a couple more options over here when the project files window is selected on these three dots on the top right corner on these personally I like to select the one column layout it showcases all the project files in a nice compact view then on the console next to it once again on the three dots over here we have some options for example over here on the log entry I like to set it to one this makes the console nice and compact which personally I prefer you can see a bunch more logs without taking up too much space then the time stamp are either enable or disabled doesn't really matter one very important thing are these three buttons over here these are the type of messages that are going to appear on the console so the left one this one is for regular messages we're going to use this quite a lot the second one is for warnings and finally for errors basically if you un toogle any of these then those types of messages will not show up over here on the console so if you ever come across an issue where nothing is showing up on the console make sure these three buttons are tick then over here on the left side for the clear we have some options so on this little arrow I like to set it to clear on play which means the console will be cleared every time we start playing our game then for the collapse this one basically collapses messages that are exactly the same into just one entry personally I do not like this I think this is very confusing I want to be able to see every single message as it comes out even if it's identical to a previous one I've also seen this lead to a lot of confusion for a lot of beginners they think their code is just firing one message when reality it's SE firing hundreds So to avoid any confusion leave collapse un toggled and then for the error pause this will automatically pause the game when some kind of error occurs so for this one you should definitely have this toggled if there is an error you want to know about it you want to know when it happens so you can actually fix it okay so that's it for the console and now on the game view up here and over here on the button that says free aspect let's select full HD 1920 x 1080 this is the standard 16x9 aspect ratio then on that same button you can also enable vsnc this can be helpful because some of these demos are really simple so if you disable it they might run at like a th000 frames per second which is going to put a ton of unneeded stress on your graphics card so just leave it as vsync and then just one thing if you see your game view is looking a little bit too pixelated make sure on this here on this lad you are not fully zoomed in if I fully zoom in I can see this becomes a little bit pixelated this basically just increasing the size of this window so on this this one on the scale make sure it's pushed all the way to the left okay so that's the game view setup then for the scene view for this one pretty much all default should work just fine just one thing to know some demos are in 2D so for that up here on the top right corner there's a button to swap between 2D or 3D this just changes the scene camera and then one important thing is on the top left corner over here there's these two buttons this impacts where the handles are actually going to show up usually you want to keep them just like this meaning this one on pivot and this one on local if you ever see your handles on some weird places always come back to this you almost always want it to be on pivot and local okay so here we have our basic layout it will be easier to follow the course if you're using the exact same layout as me so ideally you should be using this but again all the windows are customizable so if there's something you absolutely prefer differently then go ahead you can drag your windows and place them anywhere once you have layout your happy with you can go on the top right corner and over here where it says default you can click on it and we can basically save a layout so you can go ahead save it to something and then if you ever find any issues you can reload a previous layout so here we're back into default and if I click it yep now back into this one okay so now that the layout is all set up let's go through the basics of the UN editor so first just some basic controls over here with the scene view selected this where we can move around and view the entire game world you can hold the right Mouse button in order to look around then if you're holding the right Mouse button you can press W and in order to move around if you press on shift it increase the speed while holding the mouse button if you use the scroll wheel you can either increase or decrease the speed alternatively you can stop holding the right Mouse button and just press the middle bouse button in order to pan around or hold alt and left click in order to rotate around any kind of object and if you have have some kind of object over here on the hierarchy if you get lost like for example you're moving way far away and you have no idea where the object is you can just select it and then press the F key and it's automatically going to focus that object then here on the hierarchy window this shows all the game objects that exist in this scene a game object is really just a name for an object in unity so each one of these each one is a separate game object to create a game object we can simply click on the plus icon and over here we got a bunch of presets like for example inside the 3D object let's create the 3D cube let's press F in order to zoom in onto it yep there you go there's the basic Cube where an object is selected we can see the various gizmos the various handels that we can move our object so over here by clicking and dragging on these arrows we can move our object in any way then on the left side we see the various tools so the first one is just for panning around the second one is the move tool so we can use it to move our object third one is for rotating in any way then we've got the scale tool we can scale in any direction then the rec tool this one is mostly used for 2D and UI stuff but yeah pretty much also does some scaling and then the multitool which contains pretty much everything and another similar one for editing the bound volume although this one only affects the Coller and not the actual visual mesh so you should probably not be using this one okay so with the game object selected over here on the inspector we can see a bunch of stats for this game object Unity is really based on components so here we can see all the various components attached to this game object each component does all kinds of things for example this Cube game object first of all this one has a transform component all game objects are going to have this component this is basically what gives it a position rotation and scale then specifically for this game object this one as we can see it has a cube visual so that visual is composed of the cube mesh fil component this is what holds the actual mesh that is being rendered and the mesh render component this is what actually renders that mesh and finally by default this one also has a box and letter which is used in physics so in general the main thing to remember is that Unity is made up of scenes then each scene is made up of game objects and each game object is made up of components let's see an example from the interactive exercis they're all split into different scenes they're organized by folders so for example let's go inside the lecture teaching all about if conditions and let's open up this one for the distance the way we open the scene is just with double click and we can see this is a scene because that has a nice Unity logo so let's go in this case I don't want to save the sample scene and Y here's the scene and like I said most of the demos these are actually in 2D so we can click the 2D button in order to visualize it in 2D in order to play the game you simply click on the play button right up here on the middle and you up here the game view now is playing on whatever logic is in this particular scene this one is on the lecture talking about ifs and if I click to move and showcase how doing a simple if in order to test a distance so in this case for example let's inspect the player components so select the player game object on the Hier key and over here on the inspector we can see it as the player script so we can double click in order to open it and yep it opens up visual studio and opens up to the player script so here we can see a bunch of code again don't worry too much about what exactly this is saying what exactly this is doing this course W teach you everything about C so everything that we're seeing here we're going to learn about through the entirety of this course right now the only thing that I want you to learn with regards to Unity and C is that Unity scripts have some default functions they've got an awake function and an update function again I want to cover what exactly are functions in detail in a little bit so just know that the awake or also the start function those only run once whereas the update is going to run on every frame so if our Unity window is running on 60 frames per second then this code will execute 60 times per second then for some Unity specific things in order for script to be attach as a component it needs to extend this mono Behavior class in the unity scripts many times we are going to access the transform component so this refers to the transform component attached to whatever game object this script is attached to and finally debug.log this just a way that we can write any messages to the console so pretty much the same thing as the conso right line that we're going to see in a little bit okay so that's the absolute basics of unity like I said you can use C without Unity among using it to make the interactive exercises and to make the lectures a bit more visual which should help you learn better also by the way after you watch this whole course if you want to learn how to make games in unity then check out my entire course on making the game kitchen chaos it's a beginner course starting from the absolute basis of unity and game development and it takes you step by step until we have a nice complete fun game if you fully understand everything in this course you w be able to easily understand everything in that one okay so in this lecture we installed unity and learned the basics now we are ready to learn how the interactive exercises work which we're going to see in the next lecture hello and welcome I'm your KMI in this lecture we're going to learn how the companion project works okay so the companion project is something that I work really hard on to help you truly learn the contents in this course if you do what I say if you make an effort to go through everything all the content inside of this companion project if you do that then by the end you will have definitely Master C if you're following along you already downloaded in sound the project if not go ahead and download it from the link under this lecture so the companion project contains a ton of stuff first of all everything is over here on the project files all separated by lectures so all the demos that I Showcase in the videos you can come here and inspect them in order to see all the code that makes up those demos but also the main way to interact with this project is through a bunch of custom editor Windows the main one is the main window so it should be open already if you import the project if not you can go up here onto the Code Monkey menu and let's open up the main window and yep it is this window now the first thing this window actually has is an update Checker that runs automatically as I add more exercise or quiz or questions to the project as I do that I will update the project so when that happens you will see a nice popup where you can click which will take you to this very lecture where you can always find the latest project version then over here you've got some nice stats so basically it tracks your completion as you go through all the content in the course it will keep track of all of it the goal is for you to get to 100% completion by the end then we have a button that takes us to the lecture list so over here you can select what lecture you're on like for example let's say we are inspecting the variables lecture and now we can browse the three parts for each lecture there are frequently Asked question questions quizzes and exercises each of these has a separate Unity window by clicking on it it should open that window so for example here is the one for frequently asked questions this one shows some common questions and if you click through it you get a very detailed answer so these provide even more clarification on the topic covered in the lecture next we have the quizzes so these are multiple choice questions you get a question and various options again these are related to the contents of that specific lecture so if you fully understood everything you should be able to get all of these questions correctly but whether you get them right or wrong either way you get an answer with nice explanation with some extra clarification and then the exercise window these are exercises that rather than just answering questions you have to actually write code for example here's a basic one from the variables lecture it asks you to define a certain speed variable so here on the text you need to edit some script declare a variable of type int with the name speed and assign the value five to it right away we can click on start exercise this is basically going to load up all the files for that exercise now this one is pretty simple we just need to follow the instructions so in this case we just need to define a speed variable so we can open up the exercise. Cs file it should open up automatically and over here we see some more instructions in the comments so now here all we have to do is just complete whatever the exercise is and if you get stuck in any exercise all of them they all have hints which should help point you in the right direction then if you get really really stuck you can also just view the solution so this will tell you exactly what you need to do in order to manually complete the exercise or alternative there's a button to apply a solution this one is basally going to overwrite the exercise files with the correct solution then once you complete the exercise you can see a nice completion message and something very important is the button here for the video walk through this one is exactly what it says it's basically me going through the exercise explaining how to complete it and what exactly the exercise is teaching then when you're done you can click the button to complete the exercise this will basically clean up all the exercise file so you can go back and go on to the next one also some exercise have a Unity scene like this one so so you need to complete the exercise you need to write the code and then you need to hit play over here inside of unity in order to test your code and see if you have actually successfully completed the exercise if so then again same thing just stop playing complete the exercise and go on to the next one also related to the exercise window is a separate companion window which you should also open it should be opened by default in this corner if not again go into code monkey and open up the companion window so you have this is a really nice companion window that listens to a bunch of compiler errors and gives you some guidance this one is separate from the exercises so this is constantly reading for errors whether there's an exercise active or not so this can be helpful even if you're trying to write some regular code if you're trying to test out how something in some lecture actually works this provides a bunch of useful guidance for tons and tons of compiler errors so you should keep this window open and visible at all times I've said many times that the way that you truly learn is by writing code not just by blindly watching the videos you need to actually do something you need to apply your knowledge in order to truly learn so I'm really happy with this companion project that I built especially these exercises they really encourage you to write code related to the concept of each lecture and in doing so you will actually sidify the knowledge you gained there are many exercises for every lecture this is something that took a ton of work to make so I really hope you make the effort to go through everything in this companion project to help you truly understand all the contents inside of this course so as you're going through the course first watch and listen to the entire video lecture make sure you're paying attention and real learning pause the video and Rewind if you need to then after watching that lecture go on to the companion to project then over here load up the lecture list and go on to whatever lecture you're currently on then read all of the frequently asked questions and the detailed answers then go through all the quizzes and try to answer them all correctly and then go through all the exercises and really put into practice what you learned in that lecture you can see there's a bunch of content already for all the lectures up until this point so take this time right now to pause the video and go through all of this to ensure you are fully understanding everything up until this point by the end of this course you should have all of your totals set up 100% completion if you do that then you will truly have learned C which will then allow you to build anything you can imagine all right so we're almost ready to start learning C but before we do let's go through a bunch of really common errors in the next lecture hello and welcome I'm your kmy in this lecture we're going to learn about a bunch of common errors that you might encounter so here's a list of the many common errors that we're going to cover in this lecture this lecture is really mainly meant to act as a sort of reference whenever you have an error come back to this one to get some guidance on what exactly it means and how you can fix it I add this lecture right here on the beginning of the course before we get into writing any code just so you are aware of these various errors but if you are a complete beginner then don't worry about fully understanding everything in this lecture right now for most lectures I highly encourage you to stick with it and rewatch a lecture if you don't fully understand the contents but for this one this one is special this one is really intended as a reference if you are a complete beginner then watch this lecture just so you are aware of the various possible errors that you might encounter but don't worry too much about understanding all the the individual details in the code that we're going to see everything mentioned here variables classes type scope and so on these are all things that I will cover in detail in future lectures so just remember that this lecture exists and refer back to it if you encounter any of these errors starting with the most common errors of all some variation of these three usually these mean some kind of symbol is missing or it's in the wrong place one of the most crucial things about any kind of programming is how code has to be written absolutely exact if you miss just one symbol then everything breaks this is really something that a lot of complete beginners have a lot of trouble with for example here this code has an error and thankfully this one is actually a very helpful error it is correctly telling us that a parenthesis is expected here we have an opening but we have no close you always need to match the same number of opening and closing parentheses so the solution here is pretty simple just close it yep then here is another similar error this one is saying there's an ending currently braces expected and again same thing different numbers so we've got one two three openings but only one two closings so we're missing one here and here is another really common error this one is because we have a missing semicolon the semicolon is what actually defines the end of an instruction without it the compiler doesn't know that this instruction has ended in C SHP new lines don't really matter so I can paste another line down here and hope these are still errors having one line of code on each line does not matter what matters is the semicolon so always need to add it to this one and to this one then on these same mistakes if you make something slightly different so if instead of the ending one you get rid of the first one now there's a whole ton of errors in this case they're not quite as helpful so that is why you always got to keep in mind you have to write the code perfectly and here we have tons of Errors so this is quite messy the one that truly matters over here is this one it's at the main function must declare a body so the missing here is once again has to do with the currently braces we've got three closing but only two opening so we're missing an opening on this one and again same thing if for example we forget to add this one then again tons of Errors so again this this one is really tricky to figure out what is going on the issue here is our missing opening parentheses so these are really the most common syntax errors if you see any of these pay very very close attention to how your code is written remember it has to be written perfectly so don't write some messy text write it properly with all the required parentheses all the currently braces and semicolons next really important common error is simply this one the type for namespace could not be found so here I am trying to Define a variable but as we can see we have this are in the code this is yet another way as to how code must be written perfectly it is not just the words that matter code is case sensitive this is something that I see a lot of beginners having trouble with they think for example this and this they think these two are the exact same thing but they are not in terms of English sure they sound the same they look similar but in terms of the code these are two completely separate things the code really doesn't understand meaning doesn't understand English it really just understands syntax so always always remember how code is case sensitive changing something from uppercase and lower case just doing that one tiny change all of that suddenly refers to something completely separate again this is something that I see a lot of beginners struggling with so this is something I will repeat quite a lot throughout the beginner section of this course if you have some error like this one the type or Nam space could not be found first thing you should do is always double check the capitalization double check that you wrote actually perfect so in this case it is lowercase int and yep that works also since we're here one thing that is not an error but rather a warning sometimes you might see these green squigle lenses these are warnings or suggestions for how you can improve your code in this case just says the variable is assigned but it's never used again that's because just just some testing code in general just know warnings are not errors so for example I can compile this code and works perfectly the warning is simply a message that tells you that you're probably doing something that you probably don't mean to now here's a similar error it says the name age does not exist in this context and again same thing capitalization problem age on lower case and age with upper case on the a these are two completely different symbols same thing over here which is saying tap namespace player cannot be found and again some beginners might see but it is the finest right here except no it isn't once again same problem over here player is on lowercase and that one is capitalized so over here it needs to be written exactly perfectly and yep now it works and up here also perfectly and yep now it works so as a beginner the first question you should always ask yourself is did I write the code correctly and specifically in terms of case sensitivity is everything written perfectly exactly as it should be or not that should be your very first question another common error is this one it says use of unassigned local variable this one is pretty self-explanatory basically you need to initialize a variable in order to use it so in this case we really just need to give it some value and Y the error is gone related to variables and data types a common error is some variation of this one cannot implicitly convert one type into another one like it says this has to do with trying to convert one type into another for which there is no implicit conversion this is a common problem over here defining something as a float and then assigning that float onto something that sores an INT inss can really only sore whole numbers so that is why this does not work floats or decimal points but ins cannot if you absolutely need to do this then one thing you can do is simply cast the value so over here put int so you cast like that there's no error anymore however if you do it then this will lose some data for example if over here for number I store 5.6 if I do this and then I C it the value stored over here in h this one is going to be just five if you force the conversion then basically anything after the decimal is going to be ignored or the same error can be due to the data type that you're trying to store the data type this one represents a 6 4bit int whereas the int this one is only 32bit int meaning that along can store everything inside an INT and more you can define an INT and then assign that int onto a long this does work there's no error on this line however there is an error on this line that is because an INT cannot hold all the values of a long so you cannot implicitly convert a long onto an end again if you really really want to do it you can simply cast it onto an end or alternatively trying to do this again same thing cannot convert a string onto an INT if you want to basically parse a string then you can do it like this so X is the convert class and convert it onto an n32 related to data types is doing some math so a common error is doing something like this so basically you got 1 divid by 10 and you expect the result to be 0.1 but nope the result is actually zero that is because when doing math between two inss the result is going to be an INT and in an INT we cannot sort the number 0.1 so again it ignores everything after the decimal point and returns just zero the solution here is to cast one of the numbers included in the calculation onto a float and if another result is correct always keep in mind mind what data type you're using and make sure you're assigning the right data for the right type then another common mistake is when doing some conditions you might write an N like this so one condition and another one but this is not correct with just one % this is actually a completely different operation this is a bitwise and and not a logical and so once again goes back to how you have to write the code perfectly for a logical and you need two erss or for a logical or it's also not just one pipe it's actually two also for equating values it is two equals if you use just one equals that actually is the assignment operator so that is something completely different if you want equality you need two equals then after we learn about functions a common is going to be this one the name age does not exist in the current context now in this case the capitalization is correct these are both written perfectly exactly the problem here is that this one up here this one is a local variable and by being local it means that this variable only exists over here inside the main function and down here inside this other function down here does not exist at all this is a problem to do with variable scope and later on there is a detailed lecture on that specific topic also after we learn about functions and parameters one common error is this kind of error this is not a compile error it's just some Behavior difference here a beginner might think that if you define this then you add this and this one in turn is going to add two you might think that over here is going to add two onto that number but the answer is no when we run this line this one still says 35 so the value inside the age variable that one is still 35 this has to do with difference between working with something as a copy versus a reference there's a dedicated lecture on the topic of value types versus reference types in the intermediate section int is a value type which means that it is passed in as a copy so when we call this function what this one receives is a copy of the original data meaning it's just a copy it has absolutely no connection to the original variable so when we modify this one we're just modifying the copy and not the original value that is why over here when we call this the original value still stays exactly the same so always remember whether you're working with a copy or the original data then when we start using classes this is a common error it says an object references required for the non-static field method or property to be called in the class lecture and in the static lecture I talk a lot about the difference between the class definition and an instance of that class and that is exactly the problem here this function over here this one is not marked as static but we are trying to access it through the class name itself as if it were static like the a says we need an object reference meaning we need to create an object of this type in order to create an instance of that class so we can define a player equals a new player and now using this object this instance of that class now we can call it and yep everything works or alternatively we can keep the same thing we had previously and simply mark this one as static so always be very very careful are you intentionally trying to access something through the class itself or through some instance of that class another common error that we're going to run into once we start learning about classes is this one it says something is inaccessible due to its protection level this has to do with the access modifiers there's also a d lecture on this Topic near the end of the beginner section by default if you omit the access modifier then it's going to default to making it private and private means that only this class over here only this one can access this function so if you want to access it from elsewhere like for example from this function up here if so then we need to mark this as public and yep now it does work then when writing some more intermediate code you might encounter something that does something like this like require some encoding in order to save some bytes onto some text but if you just try using the encoding type if you do then it says like this once again the same thing the typew word namespace could not be found now this one is not a capitalization problem the class is indeed called encoding however this one exists inside the system. text namespace so before we can actually use it we need to be using that namespace so on top of f we need to add using system. text in this case and yep the errors go away and this now works alternatively you can place a mouse over the error then click on this little light bulb icon and this tells you a bunch of things like for example it needs using text and if there you go it does and it works all right so those are a bunch of common errors again like I said if you're a complete beginner and you didn't understand anything in this lecture that's perfectly fine all these Concepts variables functions classes and so on all this will be explained in detail in future lectures just keep in mind that this lecture exists if you ever get suck chances are you're going to encounter one of these errors if so come back here for an explanation on what might be going wrong and how you might fix it so remember this lecture and use it as a reference throughout the entire course now that we've covered this we are ready to begin learning the absolute basis of programming so let's learn that in the next lecture hello and welcome I'm your Cod monkey in this lecture we're going to learn the absolute basics of programming so how the computer executes code how code is Cas sensitive matching parentheses and a bunch more okay so in the getting stting lecture we built this default consol app project here we have a bunch of code again I will explain all this in detail in a little bit this one over here is a comment and here we've got something running a function order to print some text onto the console we can run this code so again we can go up here press F5 and yep it runs the console and right away it does say our message now the first thing to learn is how the code actually runs and that is how the code runs from top to bottom meaning if we have four instruction it is going to first run this line of code then it's going to run this line of code then this one and finally this one it is always going to execute one after the other always from top to bottom now later on when we'll learn more about functions we will see how the code execution jumps a little bit but in the end it's really all just sequentially running from one to the other from top to bottom so up here if I copy this line and I run this function twice and on the second time I say something else again code runs from top to bottom so it just going to first execute this one and print this line onto the console and then it's going to execute this one you know that it is exactly like that also one very important thing is in C the end of an instruction is denoted by a semicolon if we do not have it if I erase this then the code basically assumes that the instruction is not over another thing is how new lines don't really matter to the compiler so I can add a bunch of new lines and then the semicolon and now this is perfectly valid code all same thing when accessing a function so over here I can add a ton of new lines right after the dot I can add a ton of new lines now you can't add it in the middle of something like in the middle of this function name if I split it then it does cause an error but otherwise new lines generally do not matter in terms of running the code all that really matters is the semicolon that is what actually defines the end of an instruction a lot of beginners make the mistake of forgetting about that they usually forget about the semicolon and they get a whole bunch of Errors which by the way if you see a red squiggly line and you just put the mouse over it it won't tell you the error in this case the error is simple and our helper function actually helps us see what the error is so in here there's just an expect semicolon if we add it everything's fine so you find any strange issues always make sure to remember that you need a semicolon in order to end an instruction and also speaking of things that don't matter so new lines generally don't matter and another thing that does not matter is identation so right now I can ident this line just like this and it's going to run and I can have another one ented a bunch more so I can have code written like this and if I run it y everything still runs exactly the same if you've done some Python Programming then in that language initation does matter but over here in C it does not this is perfectly valid code although of course bad initation like this one can make the code hard to read so even though technically it does not matter for the compiler it definitely matters to you in order to make the code easy to read so always make sure you ident your code properly another extremely extremely important thing is how code is case sensitive meaning for example over here we've got an instruction a console. right line if I said right right line within lowercase L nope there is an error there is no such thing called right line within lowercase L there only exists one with an upper CASL so you need to always pay very very close attention to how you write your code this is one of the main things that I see a lot of beginners make their mistake they write code like this and they don't know why it's not working they assume that as long as the words are correct so for example if I write it like this they assume since this is technically the same thing as over here in terms of English they both say the same thing they assume that because that it should work but in reality no this does not work and the answer is always because they did not write the code exactly as it needs to be it needs to be 100% exact including capitalization so it cannot be in lowercase C it has to be an uppercase C only that actually matches the console and right line cannot be written like this the W needs to be uppercase and the L also needs to be uppercase so if you have issues in your code the first thing you should check is see if you are writing the capitalization correctly it is is not just the words that matter it's everything it's the capitalization it's the place where you put the dot the place where you open and close parentheses the semicolon all of that all of that matters so always make sure you write your code very carefully because every single detail matters and of course you can always rely on code autoc completion to help you write usually after writing a DOT you see the auto completion and if you start writing right line over here it shows you exactly as it should be you can just press enter and everything won't be written properly now the next important thing is knowing where to write the code now over here in the new template over here it's easy you can just just write code and all of this will execute without problems but now here on the previous Center template on this one note there's a bunch of currently braces these currently braces form what is known as a code block code is group between blocks for example everything inside of this code block everything here belongs to this namespace then everything inside this code block belongs to this class and everything inside this code block belongs to this function again don't worry what is a namespace class or function right now I will cover all of those in detail later on for now all I want you to learn is what is a code block which is everything inside a currently bracet and that impacts what kind of code you can write within each code blog so for example in this whole template in this one here we need to write the code inside the main function this is where the program actually starts executing like I said the code executes from top to bottom and as soon as we run this program it is going to start right here from inside the main function it starts running from this first line if we run just like this if there go it still works say exactly the same thing however if I take this code and I remove it from the function and let's say I paste it just over here so just inside the class code block if I do then obviously I get a whole bunch of error this because we are writing code inside a class block and inside a class block you cannot execute a normal instruction you can only Define Fields functions and so on now in this case the error isn't very helpful so really just remember where you are writing your code if you're writing a regular instruction that you want to execute then chances are you want to write it inside a function in this case chances are you want to write it inside the main function so always keep this in mind like I said it's very important how you actually write your code but it's also extremely important where you write it if you're writing code inside of this own template then make sure to not write the code out here here or directly inside namespace or directly inside the class if you're writing some kind of regular instruction make sure you write inside main although in the beginning portion of this course we're really just going to be writing our code in the simpler newer template just to avoid any confusion over here we have no currently braces so there's no issues like that we can just write all of our instructions over here sequentially and everything will execute once again top to bottom and finally very important is to always make sure you have matching opening and closing symbols that refers to for example code blocks every opening currently brace needs to have an ending one and same thing over here here for parenthesis every time there's one beginning there has to be a matching any one if I go ahead and I erase one of these like for example over here opening namespace we've got one then inside the class we've got two and inside the main we've got three and over here we have three closing ones but if I get rid of one of these if there you go now we have an error and this one is a nice up error so it tells us that one more is expected so always double check that you have the exact same number of both opening and closing braces and parentheses over here same thing if I raise one of these parentheses y that's also an error because we did not finish the parentheses that are calling the function so always make sure the same number of matching opening and closing if you put the cursor inside of it it should highlight the opening and the closing braces so if I put the cursor here it highlights both those if I put it here it highlights both those and so on and in general whenever you make some sort of error like let's say you forgot to write that you forgot to terminate the instruction you see the red squidling line so always remember you can place your mouse over it and gives you a bit more information sometimes the eror is super helpful like this one but sometimes might be a little bit more confusing so also remember you can refer back to the common errors lection sure to see some common issues or of course remember you can always post questions in the comments and I'll do my best to help okay so here we'll learn how code executes from top to bottom we'll learned how it's extremely important that code is case sensitive and you should always double check that you wrote the code exactly as it should be and we learned what is a code block and where we should write code as well as importance on making sure we always have a matching number of opening and closing symbols now with this basic knowledge we are now ready to learn one of the basic building blocks of programming which are variables so let's learn that in the next lecture hello and welcome I'm your KMI in this lecture we're going to learn all about variables what they are and what we can do with them okay so variables are one of the most important parts about programming in literally any language they are basically containers for data so we can define a variable which is going to be our container and then we can take some data and put it inside that container now variables also have data types which means different containers can hold different things so if a variable is meant to store a whole number I cannot place a text string inside of it there are some containers that can only hold numbers some containers Can Only Hold text others can hold true or false and a bunch more those are the data types which we're going to cover in detail in the next lecture and then we can also have some code I use a variable and basically when the code is executing that line of code in there when it reaches this point it is going to access this variable and grab the value from that variable so basically goes inside that container in order to grab the value that is stored so on the same line of code if we modify the value that is actually stored inside the variable basically we modify the behavior of our code so by writing code that uses variables instead of values directly by doing that we have a lot more control over exactly what our code is doing for example the exact same code to move a player can make it move fast or make it move slow depending on what value we store inside that variable okay so that's the theory now let's see how to make variables in code and the way we Define a variable in C is first we write the data type now there are various data types depending on what type of data we want to store like I said some store strings of text others store whole numbers others decimals and so on I'm going to cover a bunch of data types in detail in the next lecture for example over here let's store the simplest data type just whole numbers and for that we write in for the int data type this is an integer so basically whole numbers and also again like I mentioned in the last lecture it is extremely important that you write the code exact always remember how it's case sensitive so for defining the int data type it is exactly like this so int on lowercase if I make this an uppercase i then the code no longer knows what this refers to if I put the whole thing in caps then nope this is not recognized that as data type so once again always remember always write the code properly and perfectly so in this case on lowercase int so first we Define the data type and then we write the name for the variable the name can be pretty much anything you want there are only a handful of limitation like for example it cannot start with a number so this is not a valid name but for the most part picking a proper name like for example let's say age yep there you go just like this basically over here we are defining a variable called age which is of type int and now after defining the H variable we can do an equal sign and basically we can assign a value to this variable so let's say 35 with this we are assigning the value 35 onto the age variable so basically picking the value 35 and placing it inside the age container also it's very important that the value matches the type expected by the variable so over here I cannot write for example 35.2 this does not work this is an error because again in can only store whole numbers so I cannot store a decimal here so over here let's make sure to write a whole number let's say 35 and also as you write code you might see some symbols appear in kind of a faded gray as well as having the screens quickly lined underneath these are not errors these are pretty much as code suggestions so in this case it is saying that we are assigning this variable but the value is never used again that's because I'm writing the code slowly as I teach what it's doing so basically just don't get concerned with these green squiggle lines these won't go away as we actually use this variable so just remember these are not errors so don't worry about them okay so yep here we have defined the variable H and the variable is basically a container which is currently holding the value 35 if we want to inspect a value inside a variable for example one way we can do it is with the very helpful console that right line that we've seen up here so let's write the exact same thing and once again always make sure to write the code exactly perfectly so you can just do right line age and this basically going to print the age onto the console so if you go ahead and run this and if there it is it printed the value 35 so basically as the code goes to execute this line of code when it reaches this point it then looks inside this variable and pretty much just replace over here with what is the contents inside that variable so yeah great it's this simple here I have a demo in unity with this player character it is being moved using this move speed variable and basically it is currently moving at this speed and now if I take and I modify the value inside this variable there you go now it moves much faster or much slower so basically every time the movement function is running it is always grabbing the value inside this move speed so if we modify this then it modifies the actual Behavior so back in our symol console here here we have set the AG to a value then we can also modify variables so for example let's print the current age so that it should print 35 then let's go inside the age and do something to it for example let's assign it to a brand new value and let's print the age exactly the same and let's see and if there you go now it prints both values both the original value and the updated value now instead of assigning to modify the entire value we can also do some math to it and we can also use the variable in whatever meth we want to do so for example let's say h equals H + 1 so this should increase by one so it should say 36 and if that go 35 and 36 also another way of doing exactly this so incrementing by one is you just do Plus+ so both these lines of code are doing exactly the same thing both are incrementing age by one and yet another method of doing this is plus equals and then one so what we have here is exactly the same thing as we have up here so basically takes the age and equals H + 1 so over here h plus equals this does the exact same thing as up here so all these are really just different ways of writing the exact same operation although on this one on the Plus+ we can only do this once so this one only increments by one whereas these we can modify and put any value and of course here we are doing addition but we can do the same thing with subtraction so minus minus in order to decrement by one so all these also work and then on these two we can also do multiplication or division yep so over here we are taking the age and multiplying it by two and over here we are taking the age and dividing it by two however this short hand this one only works for incrementing and decrementing if we try doing slash slash nope this one becomes a the common doesn't actually make a division so there's lots of math operations you can do with variables and also up here when you define a variable you don't necessarily need to assign a value right away so this over here is perfectly valid code however like the red Squig line says over here use of unassigned local variable meaning we need to actually assign a value before we actually use it so this is valid code but before we use it we need to assign it to something so for example like this and Yep this does work also another thing is how you can Define multiple variables at once so you define the type and then Define the variable and then a comma and then any other variable you want so for example an in for age in for health in for power something like that so we have defined three variables and then we can just assign values to them Yep this works or alternatively we can also just Define and assign multiple of them right here Yep this is also valid code then I should also point out that you can do math directly on the variables like this or you can also do it directly inside a function call so for example over here H+ 2 let's just erase both of these so that one should say first 25 and then 25 + 2 and if you go 25 and 27 but for example let's say you want to print out the age and then the health if you do age plus Health if you do this the that actually prints 125 instead of actually printing out both values both as separate things that's because both of these These are both numeric values so it basically ends up doing math with both these if you want to print out both values then you basically just need to convert these into a string that way they won't do any mathematical operations so the easiest way is up here we just had a quotation marks this basically marks the beginning of a string and then another one to Mark the end of it again remember all have matching ones so inside the string we can add anything like for example a comma and then we add a plus and like this basically an INT Value Plus a string it's going to convert the whole thing into a string then add another int which is also going to be a string so that should now print a string that will both print the age and the health and if there it is just like this okay so here we'll learn how variables are really basically just containers for some data and then you can do some operations to modify that data and speaking of data importantly is how each variable has a specific data type so let's learn about those in the next lecture hello and welcome I'm your Cod monkey so here let's do a walk through of everything in the variables lecture so basically I'm going to manually do everything that you should do after every single lecture so we just saw the variables lecture so let's open up over here the main window let's go inside the lecture list and this case we were following the variables lecture so let's go inside of that one and let's do them one by one so let's begin with the frequently asked questions these usually provide some extra clarifications on the topic on the lecture so in this case starting off with the basics so what exactly is a variable a variable is a container for data so think of it as a box and inside that box you can place some kind of value then you can write code that uses a variable instead of a value and by doing so when the code executes it will go inside that variable grab the current sord value and continue running the logic using that value this also means you can have different Behavior depending on the value stored inside the variable the simplest example is just a player object with some movement code instead of writing the movement speed directly you can define a variable and by modifying the value stored inside that variable you modify how fast or slow the player moves so variables are basically just containing to contain some value next why are data types important so each variable needs to have a certain data type and that data type defines what type of data we can store inside that variable there are data types for storing numbers and if you define a variable of that type then you cannot store a string of text inside that variable you always need to use the correct data type for whatever dat you're trying to store and the next lecture covers data types in detail so yep every variable needs to have a certain data type and based on what type you choose that defines what type of data you can store within that variable next what is a good name for a variable this is a great question so a good name is something that describes exactly what that variable represents ideally just by reading the variable name you should be able to understand what it represents and how it might be used for example a move speed variable this is a good variable name it clearly communicates that this is a variable that is used as some kind of movement speed whereas a bad name would be X just by reading it you have no idea what it represents unless of course you're making some kind of meth equation and X really is the correct name for that element so you should avoid abbreviations and acronyms those might be perfectly clear to one person and very confusing to someone else there is a very very important lecture later on in the course all about naming rules and how to choose good names next question what happens if I write int with capital i instead of int and the answer is you get an error because there is no such type named int with a capital I so always remember that code is Kate sensitive the compiler does not know English so as far as the compiler is concerned int with uppercase and in lowercase these are two completely separate types with no relation between them whenever you get an error the first thing you should should check is if you're writing the code absolutely perfectly pay very close attention to capitalization this something that a a lot of me mistake and Y this is something that I will continue repeating many times throughout this course code is case sensitive I see many beginners make this mistake on the many comments that I receive so I'm repeating it nonstop because I want to make sure that you don't make the same mistake next question what is console. Right line so this is a function that rites to the C console the console this is actually a class and right line is a function within that class but like it says here don't worry about functions in class right now those are all explained in detail in future lectures for now just know that it prints something to the console and the alternative in unity is this it is a simple debug.log basically if you're working in a console app use console. right line and if you're writing some code inside a Unity script for that the related one is debug.log and in doing so it will print some kind of message down here on the console next question what are the various ways to increment a variable and yep all of these lines of code are doing the exact same thing incrementing a variable by one so you can do H equals H+ one that will increment by one you can do AG plus equals 1 that won't do the exact same thing so grab age and increment by one or h++ which again same thing increments age by one all three of these lines of code these all do the exact same thing like it says here these are just short hands the last one is especially useful when working with loops which we're going to learn in a future lecture next and final frequently Asked question what if I Define a variable but don't give it a value if you don't give it a value then the variable is uninitialized and cannot be used until you give it a value so if you do something like this you get an error so defining int age and then trying to do a constant on the right line using AG if you do this you get an error because it says that the variable age is uninitialized although later on in the course we are going to cover classes and class variables and if you don't Define a value for those it won't default to a zero value so the zero value that means something like zero for an N Field means false for a b field or known for a string field again class are covered in detail in a future lecture all right so yep those are all the frequently asked questions as you can see they contain a bit more detail a bit more specifics on what was covered in the lecture going back in the main window we can see we have completed all seven frequently asked questions now let's go through all the quizzes starting off with the first one right here what is the primary purpose of a variable in C so is it to name a function to store data to execute commands or to create new data types hopefully if you fully paid attention to the contents of the lecture you should be able to answer all of these quizzes correctly so in this case the primary purpose if variable in C the primary purpose is to store data and if that is indeed correct here is the answer with some more details variables are containers for data you define the variable and then put inside whatever value you want based on the data type then you can write code that uses that variable in some way and when that code executes it goes inside the variable container grabs the value that is stored inside it and uses that value to continue running the code next question which of the following is a correct way to declare an integer variable so is it integer age equals 35 is it int AG int with uppercase i or all uppercase int again yet another example of how important case sensitivity is for example if you were to incorrectly choose let's say this one with capital I nope this one is not correct the correct one is that all lowercase answer always be very careful with how code is case sensitive int and uppercase int might look similar but they are two completely different things the biggest mistake that beginners make is writing code carelessly and then have no idea why it isn't working so always pay very very close attention to capitalization write the code exactly as it should be next Quiz how can you modify the value of a variable is it by assigning a new value to the variable by using the modify keyword or by redeclaring the variable with a new value so if you redeclare you're going to get an error because you cannot declare two variables of the exact same name and there's no such thing as a modifi keyword so the correct answer is by assigning a new value the answer here you can define a variable and then assign a new value to replace the previous value so Define in h equal 35 and then replace it with 25 like it says here you don't redeclare it if you do you get an error since you cannot Define two variables in the same scope with the exact same name so over here if you do this yep you get an error age is already defined however you can also have multiple variables of the same name as long as they are on completely different scopes for example you can have a local variable named age inside one function and a different local variable on a different function also named age there is a detail lecture on scope later on in the course next Quiz what does the statement h++ do so does it reset the value of H to zero does it divide the value of H by itself does it increase the value of H by one or multiply the value of H by two this one is a short hand for increasing by one so yep it increased the value of H by one like it says here the shorthand for increasing the value sorted in the variable by one this is a really super useful shorthand especially when working with loopes which we're going to cover in a future lecture these are the two main shorthands so h++ and H minus minus although these two do not work you cannot do age multiply multiply this doesn't work or divide divide also doesn't work so this shorthand only works for these two but if you want to do this kind of math so multiplication and division if so you can use these other short hands so plus equal 2us equals 2 time equals divide equals next Quiz question what math operations can you do on a variable so is it only addition is it addition subtraction multiplication and division or is it only assignment of values any up with variables you can do pretty much all kinds of math so the answer is this one you can add plus Subtract with minus multiply and you can divide although of course this assumes that the data type being used in the variable is a number type naturally you cannot multiply a string or divide an object next multiple choice question is it possible to declare multiple variables in one line so yes by separating them with a comma or no each variable must be declared in a separate line and yep if you use a comma you can indeed Define multiple variables in one line so yep you can do this this is perectly valid code so int age comma in health and so on this works although at the same time while this is valid code personally I tend to avoid doing this because I feel like it makes the code a bit more confusing so while you can do this personally I would always Define each variable in a separate line just to keep the code nice and readable next Quiz question how do you print the value of a variable to the console is it with console. right line and the variable name is it console. print or just right line and this is something that you're going to do tons and tons of times so even if you get it wrong right now you will certainly get this right soon enough the answer is this one consant outright line a very common function you use consant outright line whenever you want to print anything onto the C console and by the way if you don't understand this syntax then don't worry this is a function and a static class all of that is explained in detail in future lectures or alternatively when working with unity the similar function that prints with unity console that one is called debug.log and then you print whatever you want okay so final quiz question what does the statement age star equals two do does it divide value of age by two does it in by two or multiplies by two and yep I'm pretty sure that the star is the universal symbol for multiplication so yep this one is the multiplication the srisk means multiplication and this syntax is a shorthand for doing sum function on the left side so these two lines of code are doing the exact same thing so H star equals 2 or H equals H * 2 yep both these are doing the exact same thing all right so that's the questions and we have 8 out of8 done now let's go into the exercise and let's go through all these one by one this one is a really really important part after you go through every lecture definitely make sure you do all of the exercise so let's do them beginning with this one okay so lecture variables exercise titled Define speed variable the goal is to edit the script exercise. CS we need to declare a variable of type int with the name speed and assign the value five to it so pay close attention to where you write the code so here's a quick hint right inside the main function so let's open up exercise. CS okay here is the file and right away there's just a quick explainer about what are these things in green these are commons these are just text the compiler completely ignores this so none of this is read as code comments are going to be covered in detail in a future lecture so the goal is to write some code after this line but again very important like I mentioned in the lecture on programming Basics we need to write it inside the main function so right here the goal is to declare a variable of type int with a name speed and the value five so like we learned in the lecture first we write the type name so in this case we want an INT and again always keep in mind code is case sensitive so it's in all lowercase it is not uppercase if you do it like this we got a red squiggle line int is not being recognized so the type name is int then after the type we write the name so in this case we want it to be named speed and then in order to assign a value we need to use the equal sign and then whatever value so in this case let's assign it to the value five and Yep this is it now since this is the first exercise let me just show a few things so for example if we make a mistake here so if we write int with uppercase again very important before you go back into Unity you have to save so either go up here into file and then save or just press contrl s so as soon as you save Unity should see the changes and it should start to compile or in this case we have a compiler error so it doesn't actually compile and like I mentioned on the lecture on how the exercise work always make sure you have the companion window open that way if you make some kind of mistake that leads to some kind of error here you can see a little bit more text that might help clarify exactly what the issue is so over here the issue is pretty simple it's because we wrote in incorrectly so as always remember how code is case sensitive so over here we need to make sure to type in and again if we also make another mistake St here like we named sped instead of speed right now the code is compelling because we have no syntax errors any you the exercise is still in progress because we didn't actually compl it perfectly and again down here on the companion window we can see some nice helpful message so did you accidentally write something different again the goal with this exercise is to teach you how to write things absolutely perfectly so now let's properly construct our exercise so just like this so Define a variable of type in named speed with the value five and if we save and just let the code compile and if there it is we have the ex SI completed then we can also show the hint like the hint says here remember the Syntax for how variables are declared so first the type and then the name and then optionally assign some value always remember how code is case sensitive so Define variable named exactly speed not uppercase speed or anything else and if we were to get really stuck we can just show the solution and up here we see the goal is to Define a variable of type in named speed with the value five so the solution is exactly this so in speed equals 5 and if we want we could click on apply solution any if that overrides file with one with the solution which is exactly what we wrote so yep just like this so like it says here congrats in this exercise you applied what you learned about how variables are defined how they have a type a name and a value so if that's it now click the complete exercise button order to finish the exercise all right here is the lecture on variables let's see the exercise titled fix error in so like it says here edit the script exercise. Cs and fix all of the compile errors and by the way as soon as you start the exercise or click on this button to open the file and if you do and if you see Unity kind of breaking like this if so then go into your visual studio and over here you might see this window telling you that some file has been changed externally that's because the whole setup for starting the exercises so whenever you see this window just go ahead click on yes to all and yep it should reload everything and yep here is the exercise file again the goal is to fix this error and here's a quick tip place the mouse over the red line in order to see the actual error so if we place the mouse over here directly on top of the int Red Square line if so we can see this here the typ Oro namespace int could not be found and like it says here alternatively we can check the unity console or check the companion window so here in unity if we look at the console yep we see exactly that error tyer name space in could not be found and over here on the companion window over here we can see a nice little hint on what exal the is might be and Y in this case this teaches us exactly what is the solution what is the problem that we have here as always remember how code is case sensitive then we can also show the hint so in this case the hint is pretty simple so remember how code is Kate sensitive and we can also see the show solution so there's an that says int uppercase i does not exist and that is because code is cons sensitive and the proper name is in all lowercase so the compile error here is pretty simple remember how you have to write code perfectly so in this case it is not int uppercase but int all lower case so y that's really it let's go ahead and save the code is compiling and if there it is exercise completed okay so congrats in this exercise you'll learn how to solve the most common error that beginners encounter which is issues with capitalization always remember how how code is case sensitive so in on lowercase and int with an uppercase i those are two completely different things one exists and one does not same thing for all the other types so float versus float bull bull true true and so on so once again another exercise to really make sure you fully understand code is K sensitive the reason why I'm repeating this so often is really because this is the main error that beginners encounter okay so we can now complete the exercise all right so lecture on variables here is the exercise change unit speed so the goal with this one is modify the speed variable to make the player reach the Target in time this exercise has a Unity scene it should be unloaded automatically so click on play in unity to test your code and just a quick tip just in case the scene doesn't load automatically it should do that but in case it doesn't just go into your project files and if you scroll down you go into whatever lecture you're currently on So currently we're on the variables lecture and over here if we go inside the exercise folder we should be able to see all the files for this exercise so over here we have the exercise scene and yep it is this one so like it says here the goal is to modify the speed variable now before we check the code let's try running this code and see what happens so the player is moving some kind of player character it is moving and nope it did not reach the Target in time okay so we need to modify the speed in order to make sure it gets there on time so here is the code and again you can ignore these lines that start with two for slashes these are comments and the code ignores all of these lines comments are covered in detail in a future lecture so here we have a line of code that is setting some kind of player speed variable so all we need to modify is this value and then we can press play in unity to test our code and again something that is going to happen more and more as we see some more complex exercises is some of them have some testing codes so just make sure you don't modify these lines of code otherwise the exercise breaks so in this case all we want to modify is this one and if we get stuck we can always show the hint and like it says here variables are containers for data so when the code runs it gets to the variable and it grabs whatever value stored in that variable and uses that so in this case if we want the player to move faster then we just need to modify the value that is stored inside the player speed variable or as always you can click show solution in order to see it so in this case the player speed variable just needs a value that is big enough in order to get to the Target on time so for example float player speed equals six so let's say let's put it on 3F so let's go ahead save and over here back into Unity let's make sure it compiles and let's hit on play and it is moving forward and nope it still did not appre in time so let's increase by a little bit so instead of three let's go with six okay let's run the cone and it is moving forward really fast and Y there you go success we didn't get in time all right awesome so in this exercise you'll learn how variables are used in the actual code meaning how their value actually affects how the code behaves so over here by just modifying the value that we stored inside a variable we managed to see how that actually impacted the behavior for the code which in turn allowed us to achieve our results okay so let's go ahead stop playing and let's complete our exercise all right let's see the lecture on variables with the exercise titled fix the error uninitialized so like it says here fix the error in the console a variable is being used while unassigned so here is our exercise Phile and as usual let's place the mouse over the red line order to see the error and our error here thankfully this one is predy descriptive so it says use of unassigned local variable AG this is teaching us that before we can use a variable we need to actually assign some kind of value to it so this exercise is pretty simple and the way that we assign a value like we saw in the lecture is by using the equal sign and then using some kind of value but again remember how the values also dependent on the type that we're using so since this is an INT variable We cannot put some kind of string of text this does not work we also can output some kind of decimal value nope here it has to be an integer so let's put just a whole number and Y just like this and we no longer have any errors the variable is correctly being assigned with some kind of value so then we can indeed use it so let's save our code and c and let it compile and if there it is our exercise has been completed so good job here you learn another common error using variables before assigning a value to them so always always make sure a variable has a value and the best way to avoid this error is to always give it a default value whenever you define a variable so yep always keep that in mind remember how before you can use a variable it needs to have some kind of value okay let's complete the exercise all right so here is the lecture on variables and let's do the exercise title double variable so the goal with this one is double the value inside the age variable do not modify the code defining the variable or calling the function result so we should really just double the value inside the age variable so we should only double the value inside the age variable and then we can play the scene in unity in order to test our code all right so here's the script and again we've got some comments that again are ignored by the compiler these are just text just to guide us then over here first thing we have is an INT variable named H and it is being initialized with the value 20 and then like it says here modify this line down here set it to double the value stored inside of H and as a quick hint remember how you can use math with variables now one obvious thing is you can just do math within your own head so you can just set it to 40 which is indeed the of 20 and if we hit play unity and technically we got a success we did get double however while the result is accurate there doesn't seem to be any multiplication so the goal with this exercise is to teach you how you can use mathematical operations within your variables so setting it to a fixed value that doesn't make much sense so let's complete this exercise properly let's set it to double by using actual math by actually multiplying by two so over here instead of the Direct Value we can use math so we can take our age variable so that is the one that we defined up here so it is currently with the value 20 we can take this and then we can do multiplications using an asterisk and then we can multiply it by something so in order to double it pretty simple just multiply it by two so y this is going to assign the age double variable it is going to be assigned to the value of AG multipli by two and the value of age contains the value 20 so in this case 20 * 2 and H double won't be set to 40 okay so that's it always remember to save your file just let Unity compile the changes and let's set on play and if there it is we have our success and our exercise has been completed so good job in this exercise you'll learn how you can do math with variables Yep this is definitely something that is very valuable always remember how you can take your variables and do math with them now we can complete the exercise and yep we have five out of five exercises completed so everything is Fally completed there it is the variable's lecture all fully on green so what we did here basically what I want you to do after every single lecture so watch a video lecture then come here and do all of that go on to the main window select the lecture that you want go through all the frequently asked questions all the quizzes do all the exercises and make sure that everything is nice and green if you truly want to learn C then this is really what you need to do hello and welcome I'm your Cod monkey in this lecture we're going to learn all about the various built-in data types that exist okay so in the previous lecture we learned about variables which are essentially just data containers and those containers they need to have a specific type there's a bunch of built-in types we can use for example some containers can only store whole numbers other containers can store strings of text then other ones can store the value either true or false and some can sore decimal numbers so there's a bunch of interesting built-in typs we can use here is the same demo that we saw in the previous lecture with the Sprite just bouncing up and down and this one has a parameter for a movement speed this one is defined as an INT variable so that means that over here we can only write whole numbers if I try writing some text nope that does not work if I try adding a decimal point nope I can't put it over here since this type is of type int it can only hold whole numbers here is the code that we wrote in the previous lecture so I've got an in for an age and over here if I try assigning a decimal number like let's say 35.5 and no this one has an error like I said ins can only store whole numbers if you want to store a decimal value then one of the types we can use is a double so we literally just swap in for double and now this variable this one is now of type double and with this yep we have no errors so we can indeed write a value with a decimal point and everything works now there are actually three types that we can use for decimals we can use float double or decimal there's a nice sa directly over here on the C docs that shows the maximum numbers and precision which is what affects all those types as you can see they've got different sizes and different Precision the smallest one is a float which is stored in just four bytes then a double is stored in eight bytes and a decimal in 16 bytes and over here we can see the Precision so basically if you're dealing with something where Precision is Paramount like for example when dealing with money for that you really want to use the decimal type but for all other use case you can either use a double or a float in regular C applications I believe doubles are actually used most often since in order to write a double you really just put a decimal point and Yep this one is a double whereas the other type the flow type for this one if you just find something with a decimal nope it does not work this one shows an error and the error is because if we just put a dot then this one simply becomes a double in order to make it an act float we need to append the F onto it so like I said in regular C applications I believe doubles are mostly used since you don't even need to append anything you just write the number whereas for floats you need to open F and in unity game development which is the thing that I do most most of logic is usually done with floats so personally that's the decimal type that I normally tend to use now for Unity beginners one of the things that trips them up is forgetting to place the F at the end end like we saw if we don't do it yep we get an error because this is a double and double is not automatically convertible into a float so if you ever see an error like this always remember that if you're defining a float make sure to add F at the end okay so these are the main types for holding decimals and then there's also a bunch more types for holding whole numbers Beyond int again the documentation has a really nice table over here so int is the one that you're probably going to use most often this is a s 32bit integer you can store values between minus 2.1 billion to plus 2.1 billion but there are use cases where you don't need that much Precision there are some cases where you are memory limited so you want to use as fewer bits as possible so in those case you've got all of these values and on the other hand if you want to store a real large number like for example the age of the universe for that you would normally use either long or you long but for the most part you will really just be using inss so don't worry about the rest just know that those also exist and also since we're talking about maximum and minimum let me just show you what happens if you go above the maximum so for example here we've got age defined as an INT now here we can give it the maximum value for an INT for that we can access the int class and pass in a DOT this one lets us access a bunch of constants or functions inside the in class again I will cover class in detail in a future lecture for now over here we can just check the max value and this one is basically going to represent the largest possible value that we can sort inside an in which like we saw a while ago the maximum is 2.1 billion so let's do a console di right line on this one and see what the maximum is and then let's do something interesting so let's take the age and incremented by just one and let's print again and see what this one prints and Y that's a result so basically first we've got the maximum so 214 Yep this is the maximum value for n and then if we have that value and we increment it by just one then we get what is called an overflow and essentially it really just Loops back the value to the absolute minimum so if you go past the maximum you really just end up on the minimum when working with data types always make sure you define some type that is enough to store whatever value you need to store okay so here we have instant floats for most of our number needs then we can also do operations on them so on a float you can do a float time two something like this you can do division you can do addition you can do subtraction so all the usual things now there are some quirks if you do some mathematical operation using different types like for example floats and ins I won't cover type conversions in a little bit but in terms of numbers usually you're going to work with mostly with ins and floats and then another very important data type are strings these are strings of text so you define a string by typing in string so just like the same as any other variable then let's call this one name and let's assign a string value onto it now when making a string you do it using double quotation marks and remember to add both the beginning and the end quotation mark and as usual every instruction needs to end with a semicolon so over here do not make the mistake of putting the semicolon inside the quotation marks this one is an error because over here we just have a string with a semicolon instead of heading a semicolon to end instruction so don't make that mistake and also it's very important how these are double quotation marks these are not the single quotation marks these are completely different things those are used for another type that I'm going to cover in a little bit so it's double quotation marks we have the start we have the end and then inside we can write whatever Tex we want so for example writing Code Monkey and then we can do a console down right line and let's print out our name variable and if there it is it prints exactly as intended as for making some operations with strings the most basic one is just a pinning or concatenating a string for that you just take a string and then do plus and then another string like for example my name is and then plus and then the name this is basically going to add this ring onto the end of this ring if we run this and if there you go my name is Code Monkey okay great now also important over here are the spaces inside the quotation marks these spaces do matter if we don't put a space here give there you go it says is and then code monkey with no space between them and on the other hand if we add a ton of spaces y there you go now it adds a ton of spaces so basically just be aware that spaces do matter these are not just empty and then when concatenating strings you can also use it to mix multiple types so for example our age up here so we can do my name is then plus and add the name onto it then we can add some more again let's start by adding a space and I'm another space and then let's print the H was years old okay let's see this any if there's our final string putting together all the values from all of our VAR so this is one way to do it you basically just make one string then plus add another string and so on this is one way to do it or alternatively you can use a string interpolation method to use that one you basically just put everything inside one string so let's get rid of all the pluses and all the things creating brand new strings so let's write it just like this except of course just like this this just going to print my name is name and I'm a years own so this is not what we want for string interpolation we have an entire string and then before it we add a dollar sign and now inside whenever we have something that is a variable we can add some currently braces open and close them again don't forget to open and close let's add on both these variables and now if you print if there it is now it says the same thing so these you are basally doing the exact same thing they achieve the exact same output you can add a construct a long string by building different pieces and adding them onto one another or you can just make an entire one and use string interpolation in order to Define what is a value and what is a string also here's yet another more advanced zp on strings let's say for example you want to store a string that contains quotation marks so let's do a right line and let's print my name is Code Monkey but now let's say that I want to print some quotation marks directly in there now if we do it like this obviously there's an error basically over here we are starting and finishing one string then over here starting and finishing another one and over here we've got some symbols that c does not understand so this does not work basically we do not want this quotation mark to end this first quotation mark instead we want this one to be seen as just a character so if you need to put quotation marks directly inside a string for that you can just go before it and add a backslash and this is basically going to escape this character which means it is going to consider it as text instead inad of interpreting as a string quotation mark so we can just add that one to that one and to that one and if there you go Exel like this and if there's a text in the string does print with all the quotation marks okay great so another thing that I mentioned is how it's important that you use double quotation marks the reason for that is because there is another type that is specific to single quotation marks and that type is sh that stands for some kind of character and for that you use the single quotation marks and inside you can place a single character like for example the letter A again this type Only Stores a character so if I try putting a bunch more stuff nope does not work this one only represents one character so that's the only thing that I can sore inside it now another very important type is bullan or bull so for that you write bull then give it some name like let's say is player and then for this one there are only two values we can sort inside a bow it can be either true or for example let's say is enemy and can be false so the bowling type can only have these two values nothing else and again it's extremely important that you write the code exactly so it's true like this all in lower case it is not capital T true nope this does not exist it is not everything in true nope does not exist so it's all lowercase now this type bullan this one is extremely important in if statements and conditions which we're going to see in detail in the next lecture and there is one more special sort of type that you should know although it's not really a type and that is how simply instead of defining a specific type like ball Shar string and so on instead of that you can just write VAR then give it some kind of name my variable and then give it some kind of value and basically with this the compiler will essentially look at the value that is assigned and infer some type of type for this variable so for example for this one since we assigned the value 10 this one correctly Define this variable as an INT whereas if I put 10.2 for this one now yep now it correctly identifies it as a double if I put an F yep now it correctly identifies as a float if I put false and Yep this one is now a bull and if I put false in quotation marks and Yep this one is now a string so rather than explicitly defining the type if you want you can just Define it as VAR and let the compiler infer the type for you some people like this because it tends to make the code a little bit more compact especially when we're working with complex names when we're working with types like list and so on however personally I am not a fan of VAR personally I like being as explicit as possible so that I know exactly what type I'm working with so personally I never use V I always very explicitly write the type that I'm using but there is nothing wrong with VAR it's really just personal preference so if you like using it then yep by all means use it here is a demo that I built included in the interactive exercises so we've got a bunch of valys up here and a bunch of containers down here of various types now this valy the value 56 this when is selected and we need to place it in the correct container now if we try putting this in a bullan nope that does not work because this is not a bullion this one is a whole number so for whole numbers we can place them inside the int container then over here we've got a string so this one cannot be a flow it cannot be a bow nope this one is a string then we've got false this one is a bulling value seven this a whole numbers let's put it inside the int Iron Man this is another string let's put it on string Black Widow same thing it's a string 35 this one is a whole number so goes into int 33.3 F this one is a float so go inside the floats true this is a bullion so go in there this one is a float and this one is also a float all right yep all done go ahead and give it a try yourself and see if you get a all right now I just see one more thing on types which is type conversion and we actually already saw that if we do a console. right line and on this one let's print out the age if we do this and run y there you go we did print out the age so the code is automatically converting our int age into a string that is printed onto the console basically all types can be automatically converted into a string then you can also mix types together for example let's say number five plus and then a string say number and Y this code Works however like I mentioned in the programming Basics lecture code runs from top to bottom but also left to right meaning over here let's see what happens if we place first the string number then let's add the number five and then let's add the number six so let's see what this evaluates to and if there it is a string number 56 okay great however now let's reverse the order so let's put 5 + 6 and then the string number now let's see what this one evaluates to and if there it is now it says 11 number so this is the difference between how the code runs from left to right basically over here the code is going to see okay this is a string and this is an INT and the result form adding a string onto an end the result from that is going to be a string so this one is going to concatenate into the string number five and then it's going to have another string against another end so it's going to add the six onto it however on this one first left to right it is going to check an INT plus another int that is a simple mathematical operation so it is going to add these two numbers together and then the result is an INT which then adds onto a string so it is going to do string concatenation so the second one ends up with 11 number whereas the first moon is number 56 so remember code executes from top to bottom and left to right always keep that in mind now in terms of of converting to a string anything can be converted onto a string however for other ones some types can be converted into others automatically some have to be done explicitly and some can't be done at all when it comes to numeric types these can be automatically converted if the target type can hold that data for example over here let's Define along and over here the value that we put doesn't really matter so it doesn't have to be a massive value and then if we Define an INT and we try assigning this int onto the one that we had inside long and nope this does not work if we look at it we see the error which is you cannot impl ently convert the long onto an INT the reason for that once again goes back into this chart the long type this one can store a lot more values than the int can store so because of that the compiler does not let us automatically convert this because the conversion might imply some loss of data and the compiler protects you from that however we can do the opposite since over here the long this one can sore these many values whereas the in can sore this many so basically a long can sort everything inside an INT and a bunch more so because that we can do the opposite so we can Define age Universe inside an INT and then for the long we can put it age Universe in and Yep this one does work again that is because long can store more values than just the int so we have a guarantee that whatever we Define inside the end can be stored inside a long and then same thing for doubles and floats doubles have more Precision so we can store a float inside a double but we cannot do the opposite and also like we saw we cannot have an INT and assign it some kind of decimal value this does not work however like the error says here these are implicit conversions however we can be explicit and it will force a conversion even though it might end up with lot of data and the way we do that is we just cast it onto a type so we open parentheses we put the target type and then close it so this is a cast I'm actually going to cover casting in more detail later on this course if we do it nope we no longer have an error however like I said you might end up with a loss of data for example over here we are forcing the 35.5 to be placed inside an INT so let's see what this actually returns so let's do a console. right line let's see what this prints and if there go it prints 35 since ins can only store whole numbers basically when it gets to decimal point it just ignores everything out of it importantly it does not round the value so even if I put this 35.9 nope it still says just 35 so basically when we are forcing the conversion onto an INT it is basically going to ignore everything after the decimal point now on this topic one of the most common mistakes that beginners make is actually when dividing inss so for example let's say the player has some kind of health and it's stored in an in let's say they currently have one Health and then let's say they've got a health max and the health max is 10 and then you're trying to normalize this value so you do Health divide by health max so now what do you think this will actually output in theory the mathematical answer would be 0.1 so we've got 1 divid by 10 that should be 0.1 but nope here the result is zero again it's the exact same reason it's because of type limitations we are dividing an INT by another int so the result of that cation will also be an INT and inside an end we cannot store the value 0.1 so it does the same thing that we saw a while ago it ignores the decimals and just sores the whole number which in this case just ends up storing zero now in solution this problem is really simple we just need to convert one of these values into a float so we can just cast it onto a float so now here we have some math doing between a float and int so the final result will be a float and if we test yep now it does return the correct result 0.1 so if you ever have any strange results when doing calculations always double check that you're using the correct type so you're not losing any data and like I said there are some types you can convert even with a cast for example you cannot forcefully cast a false onto an INT this does not work and same thing for a string onto a number nope this does not work if you do want to convert a string onto a number for that you have two methods one method is using a class called called convert and then inside it converts a whole bunch of functions in order to convert from all kinds of types like for example convert to n32 which is going to be the regular int the N64 this one is going to be the long and the n16 this one is the short if you're wondering what those numbers are it's over here it's the size of the actual value so the int that is a 32bit int whereas the long is a 64-bit and so on and on the right side we can actually see the full name for the types what we see on the left typ is usually what we use but these are really just aliases the actual types are the ones over here so an INT is really a system. in 32 the long is R system. N64 and so on so anyways yep here we have a whole bunch of functions and again don't worry about what our functions for now I will cover them in detail in future lectures but if we were to use this function then we can convert the value 56 into the health and Yep this does work although of course this only works if the ring actually has number if you write something you actually going to get an error all right so that's a lot of info on variables data types and type conversion this is one of the most important part of the basics of programming so definitely make sure you fully understand this topic before going further if you need go ahead and rewatch this lecture to really solidify this knowledge it is really important that you fully understand variables and types before going any further next we're going to learn another extremely important part of programming if statements and conditions so let's learn that in the next lecture hello and welcome I'm your codm in this lecture we're going to learn about IFL statements and conditions we're going to learn how conditions are evaluated how to do mathematical tests and logical operations so let's learn about ifs and conditions this has to do with one type that we already saw in the previous lecture bullion this is a type that can only be either true or false those are the only two possible values basically we write a condition and then the code evaluates that condition to either true or false now the simplest condition is just equality so for example is one equal to one the answer is yep so when the code evaluates this one it evaluates it to true and if we check is one equals to two that is obviously false one is not equal to two so this one evaluates a false then we can use these conditions inside an if the if has a condition and then a code block and if the condition is true then it is going to execute this code block and if it is false then it does not execute this codee block and again going back to what I mentioned in the programming Basics lecture the code executes from top to bottom so first it executes this if then checks the condition and the condition is true so it goes inside and executes the code inside then goes back outside then executes this if checks the condition this one is false so it skips that one and continues running afterwards so the code is still executing from top to bottom as usual but by using ifs we can control what code actually runs when here is a visual demo in the interactive exercises we've got a simple if condition test in if a distance is under a certain amount if so then we're going to set this target color to green and if not we're going to set it to White so I can click to move the player and as the distance goes under that amount yep that one turns green and if goes above then that one turns white so depending on whether this condition is true or not it is running either this line or this line of code here in the code let's see our syntax and the syntax for an if is really simple we just write if and again remember how code is case sensitive so it is not if all uppercase it is not uppercase and lower case it is neither of these instead it is if on lowercase again very important if you write it properly it should change color then we write the condition inside parentheses remember to have a matching closing parenthesis for every opening the condition can be all kinds of things they will eventually be evaluated into a bullan so either true or false so over here we can just write through for testing so we have if then the condition and then we just do a code block and this is the code that will be executed if the condition is true so for example over here just say console. right line let's say inside if and if we run this and if there it is we are inside the if so we did run that code whereas now if instead of true if we change this into false and run it and if now that one does not run because that one is false so the code goes to execute this line then it says this condition is false so it is going to skip this one and continue afterwards now it obviously doesn't make much sense manually write either true or false over here on the condition usually you want to test some kind of thing some kind of proper condition and the most basic thing is simply mathematical operations so if we test 1 equals 1 if we test this if that is inside goes inside the if alternatively instead of just equality we can simply do some meth medical operations like for example let's see if we are testing if two is bigger than one so pretty simple and the result of this test is true since two is indeed bigger than one so if we run this yep there's our message it works basically what the code is doing is when it gets over here to the if it gets to this condition then it evaluates this condition and again it is going to evaluate in whether true or false and then if it is true goes inside it if it is false skips it there's a bunch of mathematical operations you can do you can do bigger than he can do less than he can do bigger or equal less than or equal so all the standard mathematical operations and of course over here I'm using values directly but you could replace this with a variable so like n for the age 35 then let's say if age is under or equal to 40 and yep obviously this works so you have all kinds of operations and then you also have the equals now here's another extremely extremely important thing when you want to test for equality you do a double equals not just one it's a double equal sign the single equals this one is meant for assignment like for example we Define a variable then we do a single equals and we are assigning the value 35 onto our variable but when we want to test for equality we do a double equals again this is very important this is a mistake that a lot of beginners do they do something like a single equals and they have no idea why there's an error over here the error isn't very helpful it says in left hand side of the assignment must be a variable so if you're a beginner this makes no sense but that's because the single equals this one stands for assignment and double equals this one is for equality and if you try putting that inside a right line so let you Conant that right line if we do age double equals 35 if we do this if there I go that does return true however if now we put just a single equals and if now instead of either true or false it just says 35 that is because the single equals this one is assigning the value over there and it's not actually doing any comparison so again don't make this mistake single equals is for assignment and double equals is for equality so related to equals is the not equal sign and for this one we do an exclamation point and then an equals so the exclamation point means not so not equals so in this case this one will not run because one is equals to one so one not equals one is false whereas if we put a two one does not equal two yep that's true so yep it does run and of course over here we can simply use bulling variable so bull is player let's define this as true and then over here just if is player if so then Yep this works so over here we have the code block that is going to execute if the if condition is true and then we have another keyword that we can add after the if we can add the keyword else and then another code block and now this code block will execute if the first if is not true so let's do another right line inside else and over here we Define players true then we test if is player so it is going to be inside this one so it is not going to run this one if we test if there you go it just says inside the if but then if I swap player to false so now it is not going to run the if and instead it's going to run the else if that it is just like that so for example instead of having two ifs like if is player and if not is player which by the way the not equals that one can also be applied like this if you put the exclamation point that is basically going to negate whatever comes afterwards so over here we have not is player so we are testing if it is player or not is player which is really the same thing so if you want to test this kind of thing then using an else makes it much simpler that's exactly what the code over here in the demo is doing it is testing if distance is under four and if so then it's going to set the target color to Green if not it is going to set it to White if I had not written this else statement then as soon as became green even if we stood outside of it it would not change it back whereas like this if it's inside turns green if not else it turns white and now here we are testing a single condition but we can also combine multiple conditions let's say for example we want to test if two is bigger than one and if three is bigger than one the way we write a logical land is just like this with two ense again it's very very important just like the double equals this is a double ense if you put just one this is a completely different operation this is a bitwise end which is something that I covered in the advanced portion of this course but over here for a logical end we do not want just one so we want two of them again don't make this mistake so let's see what this outputs and if there it is it runs the code inside the if so basically here we have two conditions and when the code gets to this point it is basically going to evaluate each condition from from left to right so first evaluates this one is two bigger than one the answer is yes so this one evaluates true and is three bigger than one the answer is also yes so this one also evaluates the true and then we've got an end operation between a true and a true and this end operation the output of this one will only be true if both conditions are true which is the case over here so that's why this one runs inside the F if we change one of these so let's say true and false if so then the output for this one will be false so it is not going to run this one instead it's going to run the else in order to know what combinations make true or false you can look at what is called a truth table so over here we've got the values A and B and then we've got the result of doing an and operation or an or operation between both those A and B can be either true or false which by the way sometimes true or false is represented as simply ones and zeros so 0 equals false and one equals true really just different representations of the same thing so over here we have the end operator and like I said the end this one is only true if both conditions are true so if a is false and B is false both are are false so the end output is going to be false if a is false but B is true again we still don't have the condition that both them are true so the output is still false same thing over here if a is true B is false the output is still false it is only if we have both A and B both of them are true if so then the output of the end is also true so the result of an and is only true if both components are true then the other operation that we can see here is the or operator this one is going to be true if either A or B is true so if both them are false if so then the or is going to be false but if just one of them is true then the output of the or is going to be true or if both of them are true if so then the output is also going to be true over here in the code the way we write an or operator is denoted by two pipes again pay close attention to the fact that it is two if you write just one this is a completely different operation this is another bitwise operation which again I cover in detail in the advanced section so also pay close attention here it is two pipes so this makes an or operation like I said this one is going to run our code block if either of these are true so if this one is true then Yep this one is going to run and yep it does run the r only returns false if both of them are false and if we set this yep we run the code inside the else there is an interactive truth table in the exercises here up top we have the inputs and down here we have the outputs so for the end the end is only true if both are true so if that one is true and false the output is false false true that one and if both are true that one is true the or is only true if either of them are true so if either of these are true the output is going to be true and the not simply inverse it so if it's false become true and if it's true becomes false now these conditions doing an or or an and doing this always works just between two elements but you can also have multiple conditions not just two you can have as many conditions as you want so here in true or false you can have as many as you want however it's also important to know they are evaluated from left to right meaning that whenever doing some kind of or or and operation it is always done just between two conditions so when the code gets here first it evaluates this one and gets a result from this evaluation then after having that result then it does the result of this one alongside the result of that one and finally from this one along with the result of all these so it does all those evaluations and importantly always from left to right so for example let's write if 1al 2 or 1 equal 1 and 5 is bigger than one so if we have these conditions if you want pause the video and try to figure out if the code is going to run or not like I said left or right so first it evaluates this one does one equal two the answer is no so this one evaluates into false then it is going to evaluate this one does one equal one the answer is true yep then it's going to do an or operation between false or true and again the or is true if just either of them is true so in this case this one outputs is true then it evaluates this one is five bigger than one that is true and then we have a true and true this one returns true and yep we do run this code then if you want you can also have more control over the order of testing simply by using parentheses just like you do in normal math so for example let's write some code like this so for example we have false or true this one is going to evaluate in true then it's going to do true and false that is going to evaluate to false and then then false or true that is going to evaluate in true so it is going to run this one so over here we can add some parentheses so we want to do this condition first and then this condition first and this way first it is going to evaluate theu then evaluate theu and only then evaluate the whole thing another important thing is also how conditions can short circuit meaning let's say we have an or and with an or it is going to return true if either of them is true so if we have true or false or actually in this case let's put some proper conditions so one bigger than two that is going to be false and two bigger than one so if we have this basically over here two bigger than one this going to evaluate to True one bigger than two this going to evaluate to false now like I said the or this one is going to be true when either of them is true so when the compiler starts to evaluate these conditions it starts to evaluate the first one again left to right and evaluates this one into true and since that is the case the compiler is smart enough to know not to waste any power on evaluating this second condition because the first one is true then it's already going to run inside the if so after here we can have a bunch more Ores and it does not matter the compiler would only test this one since this one is true it just ignores everything after this however on the other hand if we have an end if so then it's going to valid this one this one is true but then the end is only true if both of them are true so it cannot stop executing here it needs to execute this one in order to see if it's true or false in order to know does it run inside if or not so in this case the short circuit for the end would be if this one is false if this one is false right here then doesn't even bother evaluating this condition because if the first one is false then the end is always going to return false this is the kind of thing that becomes really important in some performance intensive applications especially when you're running some expensive function to test something for example if you have true or and then over here you've got some kind of function that costs quite a lot in terms of performance if so then the compiler is going to be smart enough and it's not even going to waste any time running this condition because the first one is already true now over here let me also make one quick note ifs technically do not require a code block so if you just have an F without an else if so technically you can just do this so you can raise the code block and just write the condition afterwards if you have just a single instruction inside the if then this is perfectly valid code so let's put this one true so it goes inside and if there you go it does run the code inside the if however I highly highly advise you to always write a code block the reason is simply because it is very easy to write an if like this and then later on you want to write another instruction so let's say inside if2 so you add this but you forget the righty code lock and you assume this one is still going to run inside the if and in this case if we try running this we do see if and then the F2 but if we change this into false ideally we would not want either of these to run but yep the second one does run that is because if you don't include a code block then the only thing that is considered to be inside the if is just the next line immediately afterwards remember that in C identation does not matter so the fact that this one has as many spaces that does not matter at all the only thing that matters are code blocks and semicolons if you don't include a code block then basically just executes a code inside the if which is going to be until the first semicolon till the first instruction so in order to avoid this mistake my advice is to always write code blocks in all your ifs so always put it like this even if you have just one instruction inside it and speaking of ination like I said it does not matter so you can event the code like this or do anything crazy anything you want as far as the compiler is concerned all this is valid code inside this if but for your own personal sanity definitely make sure to take some time to ENT things properly now here we have our ifs and we saw the else as well something we can do is simply chain ifs so let's do an if true and then else and after the else we can write if immediately afterwards so for example here's some code we Define some kind of age then we do the first if testing if age is under 20 in this case it is not so we're just going to run the else related to this if and then inside that else we've got another if so if under 30 meaning the code inside here is going to run if the age is not inside this if so if it's above or equal to 20 and under 30 and if not then it's going to go into the else connected to this if and finally run this line of code and if we test Y correct ag35 is indeed above 30 so you can chain a bunch of L FS together or you can simply just do a bunch of ifs or if we want we can also chain a bunch of them just like this so basically just having some ifs one after the other Yep this is also perfectly valid code all right so here we learned about ifs and the last thing that we did was chain a bunch of ifs together this code works correctly we have all of our ifs one after the other but with this many ifs it becomes really hard to understand what the code is doing so to solve this particular problem we have another way to run conditional logic which is by using a switch so that's what we're going to cover in the next lecture hello and welcome I'm your Cod Mony in this lecture we're going to learn about the switch this lets us very easily test comparisons against multiple possible cases okay so here we have the code that we wrote in the previous lecture we have a bunch of ifs chained together technically this works but it becomes a bit hard to read to do this kind of logic there is a much better way when we want to test multiple different cases and for that we want a switch to do that first we write switch and then we write the value or variable that we want to use in our comparison so in this case let's use the same name variable then we open and close our code block and now inside here we write case and then afterwards what we want to compare against so in this case let's compare against the name Code Monkey then we do a colon and down here we write whatever code we want to execute if the switch variable matches this value so let's do the exact same thing and finally we end the case by using a break yep just like this this is a valid switch case let's just add the other two yep just like this so basically what we have down here is exactly the same thing as we have up here except instead of having a ton of separate ifs we just have a switch we just write our name variable once and then individually each value we want to compare against and if the value in here it exactly matches the one here then it's going to run this code if it perfectly matches this one it's going to run this one and so on basically when the code gets to this line it is going to grab whatever value is stored inside of name then it's going to compare against each one of those cases and when it finds a match like this one then the code goes inside and runs this line of code then when it reaches the break this one tells it to break out of the switch so it skips all of these and jumps and continues executing down here basically the switch is really great when you want to compare one value against many different values it is much simpler to use a switch as opposed to a ton of if eles then the switch also has a special case called default and basically the code here is going to run if the value that we choose up here doesn't not match any of the values inside any of the cases if it does not match any of them it is going to run the default so here is the interactive demo I've got a bunch of buttons up here that I can use to modify the string name and Y we can see which switch is going to run so if the name is code Mony then it's going to run this case and it's going to change the outline color into white if I click on irman then the name is now irman so it's going to run this one and put the outline in white and put the outline in yellow if I choose black wiow it's going to run this one and if I choose Spider-Man which does not match any of these three if so then it's going to run default and run like this now if you forget to write a break so if I remove this technically this is still valid code as long as there's it this one causes an error it tells you that control cannot fall through from one case label onto another one however if we raise this line of code yep now this one is perfectly valid so if up here I change the name into Iron Man let's see what this code runs and if it says hello black widow basically what this is doing is if the value up here matches either this case or this case if it matches either of those then it's going to run this line of code so you can have multiple cases run the same line of code although normally you usually want the code to execute only the code inside that case you want each value to execute a different thing so in most cases you want to use a break at the end of each one that way it is very clear what code runs on what case also if you don't assign a default so if I raas this and I put let's say Spider-Man if I put it twitch does not match any of these cases then y it simply does nothing so if it does not match any of the cases then nothing inside the switch runs and just continues running afterwards and over here I'm using a string but you can use literally almost anything else like an in or a sh so we can do int age and then do a switch do a switch on the age and inside do the same thing so K 10 and over here we've got h10 we can do a default we can do all kinds of things and yep all of this works okay so here we learned about the switch which is extremely useful next let's finally learn one extremely powerful feature in any programming functions so let's learn about those in the next lecture hello and welcome I'm your codm in this lecture we're going to learn all about functions this is an extremely crucial thing so make sure you fully understand everything in this lecture so functions are another core Concept in programming a function is basically putting some code inside a box so let's say over here we've got a bunch of code this is all in just one main function this is our entire program but instead we can build like this we basically put some code in a separate box so this is going to be our function and then we can easily call that function meaning run the code inside that box by just calling one line of code one big benefit of this is simply organization our code becomes much easier to understand if we have some clear functions with some clear names that do one very specific thing that is much better for organization as opposed to having a giant wall of text for your entire program another big benefit is how it cuts down on copy pasted code without functions we would need to write the function code every time we wanted that behavior so for example let's say here we got some code that we are copy pasting and repeating three times right away it's bad that we are copy pasting the same code multiple times but even worse than that is imagine if we wanted to modify some behavior on on this code that we are repeating multiple times in order to modify it we need to apply those modifications to every single version every single place where we are calling essentially the same code whereas if we're using a function it doesn't matter how many times we call it if we need to modify anything in this logic we just modify it in one place and automatically updates in every other place also just one quick note like I've said several times code runs from top to bottom and that is still true when it comes to functions except the code jumps around a little bit so it starts executing up here executes all these and gets to the function and then it jumps inside the function then it runs the code top to bottom from that function and when it reaches the end basically goes back and continues executing from there so the execution jumps around a bit but it's still pretty much the same in terms of working from top to bottom and now before we see the Syntax for functions we should look into the other more advanced template so far we've been using this really basic template where we just write our code here this is really great for the basics but by now we already have enough knowledge to be able to use the more thorough template so go ahead and open up visual studio then let's go to create a brand new project once again let's pick a console app you should be able to see it on the recent project templates or again search for it and again remember it's this one with the c icon it is not the one with vision basic or F and it is not the one with NET Framework it is the one that just says console app in C so let's pick this one then let's give it some name I'm going to go with Code Monkey C course place it anywhere and leave it the same solution name let's go next then over here yep let's use net8 and importantly is over here let's make sure to take this box we do not want to use the top level statements which is the simplified template we don't want to use that we want to use the standard code so that's it just like this let's head on Create and up here is the brand new template for now you really don't need to worry about the specifics of this so don't worry about what is a namespace what is a class what is static and so on for now you can ignore all that just know that the only difference in this template is all the code that we've been writing right now we would simply write it directly in here inside the main function if we run this we should see the exact same hello world if there it is Hello World exactly the same as previously the one very important thing is here being very careful over where you writ your code meaning it needs to be over here inside the main function inside this code block it cannot be for example over here just inside the class code block this does not work this causes an error and it also cannot be outside here inside namespace or completely outside of anything so all the syntaxes all still exactly the same as we've been seeing so far just make sure to write it over here directly inside the main function and now before we see the syntax ins set a function those are usually referring to the exact same thing technically in C the correct term is Method but both terms are really used interchangeably sometimes I call them functions sometimes I call them methods just know that in most cases when someone says either term they're really referring to the same thing now in order to define a function We Begin writing let's say outside the main function again it's very important that you write the code in the correct place we want to start writing over here which is after closing the main code block but still inside the class code block so inside this one but outside the main So Right Here We Begin by defining the data type that we're going to return from that function if we don't want to return any data we can use a special type called void void means nothing so pretty much exactly the same thing as our main function here which also does not return anything so we have the return type and then we give the function some kind of name I'm actually going to talk about the importance of good names later on in the course in a dedicated lecture it is an extremely important thing you should always always give it a proper name name and not something that has no meaning like for example just the letter X if you do that then you have no idea what that is actually doing so over here let's give it a proper name let's say we want to make a function that just prints hello to the console so let's give it a proper name and just call it say hello so that's the name of our function and then we open our parentheses and inside we add any parameters that we want our function to have although optionally we can also have it with no parameters so let's just close the parenthesis again always keep in mind matching opening and closing so we Define this and then we just write our code block so just currently braces open and close and yep over here we have the function code now here we can write whatever code we want our function to do so for example let's just do a simple console. right line and let's just print out hello all right yep that's it pretty simple here we have defined a function also just one more thing in order for our demo to work we actually need to mark this function as static so over here we need to write static just like we have on the main now like I said a while ago for now don't worry about understanding static I will cover it in detail later on the course for now just know that in order for our code to work we need both the main function and the say hello function we need both of these to be marked as static okay so our function is ready however if we try running this and if we still just see Hello World we do not see our second function hello that's because over here we are defining the function but we didn't actually run it remember how code executes it executes from top to bottom and the entry point is over here inside the main function so right now all our program is doing is it starts executing over here then runs this one prints hello world onto the console and it reaches the end the main function and it Clos our program so just defining a function doesn't actually do anything we need to actually call it or run that function and in order to do that we just need to write our function name so let's say say hello and then the parentheses for any parameters which in this one doesn't have any so let's just open and close and again terminate our instruction with a semicolon also again very important remember how code is case sensitive so you have to write the name of the function exactly if I replace this age with a lowercase age nope does not work at Reddit squiggling line needs to be perfect if I replace the S with s nope does not work so always remember code is K sensitive so just with this if we test and yep we do see Hello World and then hello so basically like I showed in the diagram the code is running from top to bottom so starts executing here then goes to execute this one which in turn cause the execution to jump onto this one and when this function terminates then it goes back into the original execution which in turn simply finishes the program another extremely useful thing about functions is simply how you can call them as many times as you want so for example let's make a bunch more instructions over here let's print out hello vertically okay just like this now let's say we want to print this out three times so we could either copy paste the code literally three times but obviously this looks quite messy or using functions we can simply call the function three times and if we test and if there is it did run our function did run our print three times and also like I mentioned diagram if we want to modify any Behavior inside this function we just need to modify it over here just once in the function and basically automatically updates whatever that function is called as opposed to if we were copy pasting we would need to make that modification multiple times so just with this simple example we can already see a ton of benefits from functions they help us organize our code by splitting our code into functions that each do a specific thing and also helps us avoid tons of copy pasted code by putting any code we want inside a function and simply calling it now that we've seen the super basic function next thing are parameters and the way you add parameters is inside the parenthesis parameters are essentially just variables so we start by writing the data type for the parameter let's say we want one of type string so we just write string then we give it some kind of name so let's say a player name again make sure you choose good names that clearly represent what this parameter should be what it should represent and obviously inside this function we can use our player name in any way you want so for example let's say hello and then print out the player name all right yep just like this and now up here we can already see a bunch of errors and the error is saying there is no argument given which also by the way you might hear the name argument instead of parameter these are again these are again sort of interchangeable names just like method and function now technically the definition is that over here when defining the function this one is technically called a parameter whereas when you use it to call something that one is technically called an argument so that's what's technically correct but in reality most people use both terms interchangeably so parameter or argument really refers to the same thing and out here we can see the error and the error says that there is no argument given that requires that corresponds to the required parameter for our player name so inside the parameters we need to pass in a string for our player name we can either pass in a variable so Define a string variable and pass it in or just write over here a string directly so let's say code monkey and Y just like this and if we test and Y it does say hello Code Monkey which of course means that you can call the function multiple times with multiple different parameters and yep it runs the logic with multiple parameters and speaking of multiple we can also have more than one over here we've already seen zero parameters and one parameter but we can have as many as we want you really just add a comma and then again any type of name let's say int age like hello player name you are age years old then over here on say hello just pass in another parameter just like this and if there it is hello Code Monkey you are 35 years old the next very important thing about functions are return types right now we Define this function as returning void and void means nothing so it means that this function doesn't return anything but now let's make a function that does return something let's make a function to Simply add two numbers together to get the result let's play Just around with simple whole numbers so let's put int we're going to return an INT let's call it add numbers then inside parentheses let's add two numbers so let's define int X int y okay like this and right away Visual Studio is showing us an error and it says not on code pass return a value meaning if we Define some kind of return type other than void we have to return something and the way we do that is simply with the keyword return and then whatever value we want to return so in this case adding two numbers let's just do X Plus Y and Y there it is super simple again in order to call this we need to add in static just make sure that it works and out here now we can call add numbers let's add the number one and two and let's see let's actually put this inside a constant out right line just to see the result okay let's test and if there it is the result is indeed three so 1 plus 2 equals 3 another thing you can do is Def find multiple functions with the exact same name they just need to have different parameters either a different number of parameters or different types so here we cannot Define another function named exactly add numbers with also two in parameters we cannot do this but if we swap one of these into B then all of a sudden this function definition is is indeed valid or alternatively if we have int but then we have a final one if we do then Yep this one is also valid and then up here when calling add numbers as soon as you open the parenthesis you see this one and you see one of two so this is basically showing all the various versions of this function so the first version takes an X and A Y both inss and the second version takes x y and Zed and for the types we can just look inside the conso down right line so add the parenthesis and over here we see this one has 18 versions so this one takes no parameters this one takes a bullan a character a character way double float and so on and then of course you can also chain functions together so basically the return value this means that whenever this function runs it is going to run this logic and return some kind of thing so just like we saw on the ifs and the conditions basally the code gets it here evaluates this function gets some kind of returned value so for example up here instead of passing in number 35 we can call add numbers and pass in something as long as the return type on this function matches the parameter type as long as it's like that then Yep this works if there it is that's correct and of course we can simply put the result of a function into inside some kind of varable so in number we can just add numbers and now if we do a conso now right line on our number if we do this if there it is it does print the same thing now one very useful type to return from a function is bullion so for example let's make another function let's make it return Bull and let's say for example just is positive we want to return if a certain number is positive let's receive an INT X and we can do if and compare X to two so if X is bigger than or equal to zero I think Z equals as a positive number I'm not sure but I think so so if so then this return true and if not then return false and if there it is and again just in here we need to make sure to add static in order to make this work so now up here let's do a console do right line and let's pass in is positive let's say the number five then let's add a semicolon just separated and let's say is positive and let's say minus Z okay let's see what this does and yep it says true and false so yep that is correct five is indeed positive minus 2 is indeed not positive also by the way here instead of doing these returns instead of doing these ifs instead we can really just return the condition itself so we can just do return and just return the condition so just like this and yep everything still works exactly the same when the code gets it here it is going to evaluate this condition which is going to return true or false and then Returns the whole thing and then prints it out now back over here on the void return type on this one you don't have to call return over here we have no errors however if you want you can also call return and for this one since this returns nothing it returns no type instead of calling return then passing in some value we just call return Then semicolon that's it basically what this will do is it will stop the execution of the function directly at this point so visual studio actually helps us is you can see this line is in a dark ray and that is because like it says here unreachable code detected since we are always calling return over here then this line of code will never ever run speaking of functions and parameters this also a great type to see how things work with regards to value types versus reference types this is something that I cover in detail later on in the intermediate section but I want to make a quick mention up here for example let's keep just he say h hell let's actually get rid of all these okay so let's say I Define an N for the age and let's say I put it at something and then I call say hello and then let's put the exact same thing so that and passing the H and now over here inside the say hello let's do h++ so basically we are incrementing the value of H so when this line of code runs instead of printing 35 it should print 36 but now the important thing is now out here let's say we print out the age again so right line on the H so let's print this one out and now let's see what this one says and if there it is so inside the function it says you are 36 years old but outside the function it says 35 basically what is happening is when we call a function over here when we pass in a parameter when we do that we are actually passing in a copy of the value stored inside this variable so when we increment a variable in here we are really just incrementing the copy and we are not actually modifying the original value that is why when we run here we see 36 because we are working with this copy that we did modify but when we exit the function and we run this line of code when we do that we are using the original piece of data which has not been modified again this has to do with the fact that some types like int those aren't passed in as copies meaning that the function receives a copy of the original value and if you modify that inside the function you are modifying the copy and not the original value and then there are other types which are reference types which is going to have some different Behavior those are passed in as reference setad of a copy so for those if you modify them inside the function then you will also modify them outside the function now if you're a bit confused don't worry like I said I talk about in detail about value types versus reference types I talk about what they are and what that actually means later on in the intermediate section right now I really just want you to be aware of that difference when working for example with primitive types like over here the int when you pass it in always remember you are working with a copy so just be aware that if you modify something inside a function it will not necessarily modify it outside the function all right so here we'll learn another really crucial thing about programming functions they are extremely powerful and you will be writing tons of functions with all sort of return types and all sorts of number of parameters so definitely do make sure you fully understand functions before going any further watch this lecture a second time if you need to they are really that crucial and now that we have introduced functions which allows us to have multiple separated code blocks because of that you should really learn the basics of variable scope so let's learn that in the next lecture hello and welcome I'm your Cod monkey in this lecture we're going to learn about scope which covers the lifetime of variables and from where they are accessible okay so scope is very important it's another one of those things that trips up beginners it comes back again to how code is organized in different code blocks and variables defined within a code block can only be accessed by that code block and any child code locks so for example if I Define a variable up here I can indeed access it in here in here in here and here yep it is accessible everywhere however if I Define a variable in here it is accessible from Within These child code blocks but it is not accessible from a completely separate code block and it is not accessible from a code marck that is above it in the previous lecture we covered function so here there is a function and now up here in the main let's define some kind of variable let's say bull is player let's define this variable over here and now if down here on the function if I I try accessing this and modifying it nope this does not work we have an error the name is player does not exist in the current context context is really just another word for scope basically when we Define a variable up here inside the main function code block inside of here we are defining a local variable meaning this variable is player this variable only exists inside this function within this code block as soon as we exit this code block for something else like for example this other function down here that variable literally does not exist this local variable only exists inside this code block and it only also o exists after it is defined again code runs from top to bottom so afterwards we can set is player into true but before we Define it we cannot do this nope this one does not work we cannot use the local variable before it is declared so this is yet another mistake that I see a lot of beginners having trouble with they Define some kind of variable and they have no idea why they cannot use it in a complete separate code block so always remember where you are writing code in which code block and remember that local variables defined within a code block they are not accessible from a completely separate code block also if you define a parameter variable like these two that we have here parameters are also local variables and they also only exist inside this code block so for example if out here after calling say hello if I try to access the player name nope that does not exist because again player name is essentially a local variable which only exists within this code block now these questions of scope apply to complely separate code blocks like these but it also applies to code blocks within code blocks like for example over here if I Define an if if something and then I make a code block within this code block which is essentially a nested code block inside the main within this this one I can indeed access player and do something to it so again back to diagram if we Define something over here we can access it from any nested code block however if we do the opposite so if instead of defining it in there let's say we Define inside that code block and then over here trying to modify it and nope this player does not exist over here it does not exist because the code block where we are writing player the code block inside main that one is a parent code block of the if code block which is where the variable is actually defined so as you go deeper inside code blocks you can access variables that were defined outside of it above it but when outside you cannot access things that were defined inside now if you have a variable that you'd like to access and modify between multiple functions like something that I want to access both inside the main and inside the say hello function for that you can simply make it a class variable I'm going to cover classes and also the static keyword in detail in future lecture so don't worry too much about what they are for now just again remember it's all about code blocks so here we've got the program class and it exists inside this code block and inside this class we have our two functions so just out here we can Define some kind of variable so let's say bull is player and again to make this work we need to mark it as static so with this one defined we can then access it over here inside the main and also over here inside the hello because again these two code blocks are basically nested code blocks of the class code block but on the other hand if we Define a complet separate class then over here on this class which is again another completely separate code block from the one up here if over here we make some kind of function then inside this function code if we try accessing is player nope does not exist because this player does not exist inside this class we Define up here so it only exists inside this program class and any nested functions so in general if you ever find some kind of issues if you ever see this error message saying that some kind of variable does not exist in the current context if so then always double check your code to make sure that the variable that you are trying to access you can actually access it from wherever you are trying to access if you do want to pass in some data some kind of local variable to within a function if you want to do that then you can literally do exactly what we've been doing so you define a function with some kind of parameter you def find the local variable over here and then simply put it in there and then again remember the thing that I mentioned in the previous lecture about how parameters these are really copies so if we do h++ in here it is actually not going to modify this one so if you did want to modify that one you would simply return some kind of thing so return the age and up here simply put age equals that and now if we do a console out right line on our Ag and if this one does print 36 so we pass in the local variable to inside the function then the function we do something we return it and with that return value we put it back on the local variable all right so that's scope this is another cruci thing that I see a lot of beginners struggleing with so definitely make sure you fully understand this anytime you see that error again make sure to come back to this lecture in order to fully understand why it's happening next we're going to learn about something very simple but also very important comments so let's learn about those in the next lecture hello and welcome I'm your Cod monkey in this lecture we're going to learn all about comments these are basically just some text annotation that you can add to your code to provide some extra clarification on anything you need okay so comments are really super simple this is how you can pretty much write any text you want directly inside your code however that text is not interpreted as text meaning comments are just for you the programmer the compiler completely ignores them so as the code is executing line by line it is executing it gets to the comment and skips it and continues executing so as far as the compiler is concerned the comment literally does not exist in terms of syntax they are really simple you just write two forward slashes and then write whatever code you want like I said this is not interpreted as any instruction or any code or anything the compiler completely ignores all of this so I could write some kind of valid code but nope this is not registered as code as far as the compiler is concerned this line over here does not exist so you can make single line comments with two forward slashes or you can make multi-line comments with a forward slash and any Star and then you end it with another star and another forward slash so with this anything you put anywhere inside of this all of this is considered a comment all of this is ignored by the compiler also comments do not have to be just in their online you can write some regular code and then afterwards write some kind of comment so Yep this is valid code and alternatively for this multi-line comment you don't technically need to do it in multi-line so you can just you open up that one and close it and write anything right inside there then also this comment right after the forward slashes anything afterwards is considered a comment meaning that you can't actually make a comment before so this do not work the compiler is going to ignore all of this it is not going to execute this instruction so technically if you absolutely need to do this you would simply do the for/ star then close it and yep now this would indeed work this is a common order by the compiler this is a normal instruction which would run another comment although you probably really don't want to do this comments right before any kind of instruction that just makes the code really hard to read so whilst technically you can do this you probably really shouldn't with this we can see how in technical terms comments are really super simple to understand there's a super simple syntax and then you can write whatever text you want now the more important thing when it comes to comments is knowing what to comment something that we understand to do is pretty much just explain what the code is doing so here's an example of some really bad comments these are bad because they are completely useless they are really just repeating what the code is doing so by looking at the code we can see we've got a variable with the name a being defined with type int so this comment is completely useless all of this just adds just some needless noise and comments like this also have the problem that you might simply forget to updated for example if I update the codes over here instead of incrementing by two increment by one and all of a sudden now this comment is completely wrong so in general the general rule is your comments should focus on the why and not the how meaning instead of saying what the code is doing line by line instead of that you should write why you wrote that code what problem is trying to solve although at the same time comments are actually something you should aim to avoid what I mean by that is your code should have good names for your variables your functions and classes all that should have great names so that just by reading their name you should be able to understand what it's actually doing that means that your code should ideally be self-documented in many cases if you find that your code has a whole bunch of comments chances are that's a sign that you should refactor your code for example over here if we were to rename name this to use some terrible names so I named the function just to say let's rename this parameter to just p and this parameter to just H beginners might do this because for some reason beginners like to make their parameters as small as possible and then they might say prints out a hello to a player and then need to clarify P this one means the player name and how a this one means the age so this is an example of comments that are technically not as useless as the ones up here but at the same time these are very clearly telling you that instead of having these comments you should refactor this code in order to make the function in order to make what this is doing much much more understandable you can just do a direct comparison between this and what we had previously and obviously this version with no comments this one is much easier to read much easier to understand that is why being able to come up with good names when writing good C though that is why that is so important that's a very important topic and something that I'm going to cover in detail in a future lecture so for now when it comes to comments I just want you to learn what comments are how you can write them and also to encourage you to avoid writing any useless comments all right so those are comments and with that by now we have already learned quite a lot about the absolute basics of programming we are now ready to take it one step further and learn about some more complex types we're going to learn about arrays and lists so let's do that in the next lecture hello and welcome I'm your codm in this lecture we're going to learn about arrays and lists this is how you can store multiple pieces of data together okay so previously we covered the basic data types for example you've got int B string and so on the variables are essentially just containers for data and for example if you make an in variable and then you can store some kind of int or whole number value like for example the number 56 I can take it and I can indeed put it inside an INT variable however when I want to store more than one number so now let's say I want to store this one nope that does not work I can only store one value inside a single int variable so when you want to store multiple values that is where arrays come in you define an array of a certain type like for example an interray you give it some sort of size which is going to be fixed you define that size when you create the array and that basically creates multiple boxes multiple containers on which you can put any kind of data you want of that type so for example now on index zero I can place this value and on the next one I can place this one then I can place this one and so on Sample like this I can place multiple pieces of data on a single variable one important thing about arrays are the indexes note how the array has a length of five meaning it has five elements but for these five elements the first one is over here on index zero then the second one is on index one and so on so the positions in the array are always index space and they only start at zero using the index we can individually either set or read values directly from the array okay that's e theory now let's see the syntax the way that we Define an array is actually really simple we Define the data typ as usual so for example let's say an INT array where we want to store ins so we Define data type and then open and close the square braces important it is this one it is not parentheses is not curring braces Noe it is the square ones so you do this and yep that's it this variable is now defined as an interray let's give it some kind of name like numbers array so Yep this is our interray and afterwards we can initialize the array and for this there are actually several ways to do it if we want to initialize it with some elements the simplest way we can do it is just open currently braces and inside put any kind of elements like let's say a whole bunch of numbers so if there you go like this we are initializing the array alternatively you can be a little bit more explicit with the tab creation you can do new int array and then pass in all of the elements personally for me this is my preferred method like I said I like my code to be as explicit as possible so personally I do like to be very explicit with how I Define the array the exact type that I'm using or alternatively you can simply create an array without any elements and just give it a certain size like this there's an error you have to give it a size so for example let's say with five and there you go this creates an array the numbers array this is an array with five elements and like this if you don't initialize to anything then pretty much all the elements will be initialized to zero so speaking of the size this actually brings up a very important topic about AR Rays there size is fixed and after being created the size cannot be changed over here we are creating an array with size of five and that's it that cannot change if you want to check the side of the array we can see that through the length property and the way we do that is first we access our variable so that see numbers array and then we write a DOT and this dot basically means that we want to access something inside this variable inside this variable type and in this case this variable is an array so by tying dot we are accessing all the functions and properties that exist in the array type so it's kind of similar to how previously we saw console. right line basically right Line This is a function that exists inside the console class that's what the dot means it means we are accessing something on the symbol previously defined okay so like this we are accessing something on the array and if we want to get the size the array for that we can access the property for the link so if we just do a console right line on this one just to see what this says let's see and if there it is it says five which is exactly the size that we Define for our array if we modify this to three and run and yep now it says three so the length this is going to return the size of the array then another thing you are going to do quite a lot with arrays simply accessing them to read an element on a certain index and the way we do that is we simply write the array so numbers array and then we write the square brackets and inside it we put some index like for example let's say index zero again remember how in C indexes start with zero so the one on index zero is going to be the first element the one on index one is the second element and so on so if first let's initialize the array so let's initialize it with something let's put a whole bunch of numbers okay so we initialize with a bunch of numbers and now let's print out the one on index zero so let's do a console d right line on that and let's just get rid of this previous log okay let's see and if there you go it does print out the number 56 which is indeed the very first element on this array however also something very important is that we actually initialize array first and like I said we can initialize like this with some values or we can simply omit this and simply initialize with a certain size if we run this code yep we return zero basically the array initializes everything to zero however if we don't do this if we just find the array then nope here we have a compiler error so we need to actually initialize the array before we can use it and like I said the arrays have a fixed size so when accessing an index that also becomes important for example if we try to access after the size so for example over here we have three elements if we try and access the one on index five which does not exist if we do this then yep we get an index out of range exception that is because the index index five that does not exist that is outside the boun Z array same thing if we try to go the other way so if we try to go minus three if we do this then yep same thing same error now if we do want to change the size the array like for example if we want to add another element to the array like I said that literally cannot be done the size cannot change so the only thing we can do is simply make yet another array add it with one more element of space and then copy all these elements on the new array and then add the new element so you'd have to make all that logic manually if you wanted to add an element to this array or alternatively for removing an element once again same thing you cannot change the size so You' create a new array with one less length and then copy the remaining elements onto that array you can manually write that logic with the help of Loops which we're going to learn about in the next lecture or in order to add or remove elements to an array you can simply use a different collection type namely the list the list is actually built on top of the arrays and basically has a bunch of helper functions to make it super easy to add or move elements or do a bunch more things and to use a list first we write list and then we write the angle bracket so this is a less than sign then we write the data type that we want to store in the list like for example an INT and then the closed angle brackets so the greater than S again don't confuse the symbols make sure you are using these symbols like for example there is a double character it is not this one it is a single one the same one you use for mathematical comparisons now the technical term for what we have written here inside the brackets the technical term for this is a generic which we're going to cover later on in detail in the intermediate section for now just make sure you write this syntax exactly so with this we have defined the list of ins so let's call it int list or actually to keep it the same name let's call it numbers list okay and then to create our list we have pretty much the same options we have for the array we can do the full type declaration so over here put any kind of values Yep this is our list however on this one we cannot initialize it just like this this specific initializer only works for AR Race So for list we cannot use this one but we also have the third option of Simply initializing a list just like this and here note how I did not put any number inside the parenthesis whereas over here when defining a new interray we have to give it a size but for a list like I said lists don't have a fixed size they can grow in shrink so when creating a brand new one you don't have to give it a size although technically if we actually inspect this Constructor we can see there is a version that does take a capacity now this is a more performance optimization thing if you know you're going to need a certain number of elements you can preset that capacity but in most cases and especially right now on this beginner section you can just ignore it just do it like this and to create a brand new empty list so we have created on list and now we can do let's say the same thing that we did on the numbers array let's first print out how many elements are on this list it's pretty much the same thing we just access the same number of list except right now on a list this one is actually not called length if we find links we can't find anything so it doesn't have that name in length it is instead called count for counting the number of elements in the list but in terms of logic it's really the same thing so it Returns the amount of elements within that list so let's do a right line on this one and since we just created this one should return zero and if there it is this one does have zero okay great then like I said one of the big benefits of lists as opposed to arrays is we can very easily add or remove elements the way we do that is we just access the list and then use the add function and this one takes an item that we want to add so let's add the number 12 and if we do this and afterwards let's print out the right line on the numbers list. count let's see this and yep that one does indeed have one element and if we access the list on the Zero index in order to see our first element and if we do see our list contains the number 12 okay great so just like this we can already see just how much better it is to work with list as opposed to Ray with this there's no need to manually create a new array with the size plus one we don't need to do that and copy all data basically all of that is handled automatically by the list we just call add or alternatively we go inside the numbers list and we call remove either one of these and we'll either add or remove an element over here on the remove the way this one works is you give it some kind of item so in this case the only element inside is the number 12 so if we call 12 this is going to remove the number 12 or alternatively there's another one there's the remove at which then takes an index so if we do this and pass in the index zero it is going to remove the element on index zero and related to the ad there's also another one which is the insert basically the ad that one is always going to add the new element to the end of that list whereas with insert over here you can pass in an index where you want to insert that new element another Super useful function in our list is for finding elements let's add some more elements just a test so let's add 12 0 and let's say 89 so we did this and now let's say we want to find if the number 89 exists inside this list if so we can go through the list and we can call the contains function and like name implies this one tests if it contains a certain element so if we test for 89 and c and it returns true because does have that element whereas if we put some different number and nope does not have that element another similar way to find elements within a list is to use the index of this basically going to return the index of an item if it finds it so for example the index of 89 this one is the third element on the list so it should return two and if there it is does have a two or if we include something that is not found so if you include something does not exist if so then yep when it doesn't find anything it Returns the index minus one since minus one is not a valid index minus one usually means not found and by the way actually over here on the array you can also do something similar to this you can do an index of however it's a bit different so it is not like this. index of this does not exist there's no function called index of within the array however it actually exists as a static method again don't worry what static means right now we'll explain in detail in a later lecture but basically we just need to access through the system. array we access through that and we can do index of and this one takes our array so the numbers array and then whatever we're trying trying to find let's say we want to find the element 23 and if we do a console. log on this do a right line let's see and if that one does find it on index one and yep number one to three is over here indeed on index one so as you can see arrays and lists are really excellent really very useful tools for handling multiple pieces of data the technical term for these are collections an array is a collection and a list is also a collection there's a bunch more collection types for more advanced use cases you have stacks dictionaries hash hats and a bunch more I cover all those in detail in the lecture in the intermediate section section for now just knowing about arrays and lists it is going to be more than enough whenever you need to store multiple pieces of data you can use either one of these in most case you probably want to use a list just because they are so easy to use and when you need absolute maximum performance for that arrays can be quite useful all right so here we learn about something very useful arrays and lists you won't be using these a lot in your programming career and now in the next lecture we're going to learn about one of the core tools of just about any programming which are Loops so let's learn that in the next lecture hello and welcome I'm your cod mck in this lecture we're going to learn learn one of the more fascinating and Powerful features of any programming Loops this is how we can run certain logic multiple times okay so let's learn everything about loops this is another really crucial core part of pretty much any programming there are so many scenarios where Loops are used it can be a little bit tricky at first for beginners especially since the code jumps a little bit so we do pay close attention normally the code runs from top to bottom and when it comes to Loops that is still true although it also jumps and runs the same instructions multiple times for example if this Loop over here was defined to run three times if so then it was run this line then this line then this line and then instead of exiting and continuing down it would jump back to the beginning and run this line again this one again jump back run this one this one and then finally exit the loop we're going to see how this is very useful especially when combined with the arrays that we learned in the previous lecture it's how you don't even have to know the size of the array in advance you don't need to copy paste a whole bunch of code to it rate through each element you can just write a loop that will jump through all the elements of the array and then you can do whatever logic you want on each element we're going to learn about these four Loop types so we've got wild do while four and a four each let's begin by learning the most basic Loop possible which is a simple while loop this one simply has a condition and it will basically execute the code inside the currently braces while that condition is true as soon as the condition becomes false the code stops executing and jumps after to write a one loop it's really simple we just write the keyword while then we put some kind of condition inside parentheses like for example let's just put true and then we put a code block with currently braces all right so that's it basically now with this we are going to run the code inside this code block for it as long as this condition is true now what I have written here is actually something that usually you want to avoid for the condition I just wrote through and like I said this code block will run for as long as the condition is true which right now is forever since true this will never change by itself so what we have here is an endless loop if we try putting in log here console. right line and we try to see what this does and if there it is the log is simply printing towards Infinity forever and ever until the end of time so usually this is something you don't want to do and by the way quick tip over here if you want to cancel this you can either click on the button over there or you can press contrl C and that will automatically stop the code from executing okay so here we made our while with an infinite Loop now sometimes this is actually desirable like for example in a game engine you do want your update code to run every single frame until the player quits the game but right now we want the loop that actually ends so for that we just need some kind of condition that eventually becomes false and the most basic one is just having some kind of counter so before let's just Define a simple in I by the way usually when working with loops the variable I is commonly used it stands for the iterator variable and usually we want to initialize this one at zero and not at one techically you don't have to but usually you want to start things with zero that comes back to what we covered with arrays on the previous lecture how array indexes always start at zero so for iterator variables usually you do the same thing now over here one very important thing is make sure you are defining the variable before the loop meaning don't place this code inside of here if you do then you are basically always defining the variable with the same value so every time the loop runs it is always going to have the same value it would never increment and the loop would never end so make sure you define this one outside of the loop so right before now over here instead of true let's cycle while I is under three and in order to make sure that this condition actually ends that it eventually becomes false because right now it is still infinite in order to do that at the end of our one Loop let's simply increase our iterator by one so just I ++ okay so that's it pretty simple let's see what this is doing over here let's just print out Loop and then let's print out the I okay so let's see and if there is Loop 0 1 and two so y that is correct the code inside this code block is running while this is true and when we first run the iter rate is going to be zero so is zero and three the answer is true so the first time it is going to execute this one and then this one which is going to increment the I into one then it goes back loops and test the condition once again so now is one under three it's still yes so once again runs increases it next Loop is two under three still yes so it still prints for the third time but now it increase this one increase the I to three so the next time goes is three lower than three the answer is no 3 equals 3 so at that point it drops out of the loop and continues executing down here also by the way what we just did here is actually a great tip for when you're confused about loops just go through them in your head line by line to see exactly what things are doing and of course always write some logs in order to remember what you're doing with your iterator variable like for example here if you made the simple mistake of doing iusus instead of i++ if you did that yep there you go the loop never ends it's an endless loop again or alternatively if you did increase but for some reason you forgot and you increased it just after the loop and you once again still an endless loop it's constantly at zero so always pay very close to your conditions and how exactly you are eventually exiting that Loop also let me mention another common mistake that beginners or really every programmer makes which is off by one errors meaning they think they're running a certain amount of times but it's actually running that many times plus one so usually the mistake is instead of doing for example lower than lower than or equals so let's say you wanted to run this three times but if you put less than equals if so then yep the loop actually runs four times so always double check that your condition is correct exactly as you intended so that the Loop runs as many times as you actually think it does now one really important thing about the one Loop is how it tests condition before going inside the loop so for example for the iterator variable If instead of initializing on zero let's initialize it on five which is immediately going to have false on this one so let's see if our code prints anything to the console and Noe the answer is it does not if this condition is false right away then the code inside the loop never runs not even once so related to that let's see the other similar Loop the do while this one is very similar to the while loop the only difference is really when the condition is is testing so for this one first we write the keyword do and then we open and close our C braces so we have our code block and then we have the while and then parenthesis our condition so let's put the same condition I under three so yep just like this and let's do a console. right line do one Loop and let's print out the iterated variable and inside the loop again always remember to increase your iterated variable okay so here we can directly compare is this one going to run the one inside well which I already saw is not going to run and then let's see if this one does run let's test and y there's the result so the regular well Loop this one did not run not even once but the do well even though this condition is always false even on the first iteration it is already going to be false despite that it is still going to run the code block inside the loop at least once so that's the difference between these two Loop types this one tests in the very beginning so it might not even run once whereas this one tests the condition at the end so it is guaranteed that the code inside of here is always going to run at least once and if we put the I back into zero and then remember to reset it on this one and if there it is both them be similarly both them run three times okay so while and do while these are the most basic Loops you just have a condition and then you do something while that condition is true but another one that you're probably going to use most of the time is the for Loop this one can sometimes look complex with beginners so definitely pay close attention this one is split into three parts the first part is the initializer so here usually we initialize some kind of iterator variable then we have the condition so same thing it is going to continue running while this one is true and finally we have the increment so usually over here you increase the iterate variable by one so let's see the syntax let's just comment this out and let's go up here and write our four so first We Begin by writing the keyword for and then let's open and close the parenthesis and now in here the for Loop has three components and they are split by semicolon so the first component is going to be where we're going to initialize our iterator variable like I said the very common thing is usually just Define an INT name it I and initialize it on zero okay so then we do a semicolon and then we can write the second component the second one is going to be the condition so let's put the same thing we had in ouri so I under three and yet again another semicolon and now we have the third component this is going to be the increment so here usually you want to increase the iterator variable by one so i++ yep so just like this we have our three components then we just have our regular code block now over here we can do whatever we want like the right line for the for Loop and print out the it variable all right so let's test and Y once again the same result it runs the code inside the loop three times now let's go through this piece by piece to see exactly how the code is executing so basically when the code gets to this line over here when it does the first thing it's going to run is this part over here which is going to initialize the iterated variable right after that it is going to test for the condition so very much like the one Loop meaning if it is false it is going to skip it entirely but if it is true then the next thing it goes is it goes inside the for Loop and runs all of the code inside the code block and then after running the entire code block after that it runs the third component which is going to increment the iterator variable and after doing that then it it jumps back and tests for the condition once again then again keeps doing the same thing if it's true continue running this one and then jump back to the iterator test again and so on until finally it becomes false and then it jums outside basically the one great benefit for the for loop as opposed to something like a while or a do while the one big benefit is how it makes it much easier to handle the iterator variable although I should also note that while this does help handle the iterator variable a bit more easily the same issues that we had with infin loops those can also occur here so for example if for the itat instead of doing app Plus you do IUS minus then Yep this one now becomes an infinite Loop or if you put i++ but then over here for some reason you put some condition that is always true then once again this is also going to be an infinite Loop so really the potential for an infinite Loop that is always there regardless of what Loop type you're using but generally since the four Loops since that one at least force you to use an iterator variable and increment it for that it should help avoid those unwanted infinite loops and also remember what we covered on the electure on scope the iterator variable over here the in I this one is being defined in this special place inside the for Loop and doing it like that makes it a local variable specifically local to this for Loop so we can access it in here so here we are printing it but if we try accessing out here then nope this does not happen this variable only exists inside the code block inside the for Loop so after ending the loop this variable no longer exists whereas for example over here on the while on this one we Define the in iterator outside of the while so after the while we can still do anything we want with that variable this is actually usually something you don't want to do so that's another reason why the for Loop is one of the most useful ones now now the for Loop can be used for running some code a certain number of times or something very useful is when used with what we covered in the last lecture arrays or list so here let's define a string array for the name array and let's just create the new string array all right so here we have a nice array of various strings and now if we want to cycle through this array then a for Loop is one of the great ways to do it we use usual iterator variable we increment by one and then over here for the condition we want to iterate through the array for as long as we have elements so for that we can check if I is under and then go inside the name array and access the arrays like this property is going to contain the number of elements inside the array and once again don't make the mistake of the one off by one errors so over here make sure it's always less than the length and not less than equals because this length for example this one is going to return four we have four elements but again remember how the indexes start at zero so this is index 0 1 2 and three so there is nothing on index 4 and if you put it less than and equals then the final one is going to be on I of4 and on I of4 would give an array because nothing exists on index four so always pay attention to that very common error and now with this we can just print out just this name so let's just go inside the name array and access on the I index yep like this let's test and if there it is we are printing all the elements in our array all right awesome also one quick note to cycle through a list is pretty much the same thing so here if we make this a list of string instead of an array so a list of string let's do it like this and initialize our list and the only difference is like we saw in the previous one instead of the list calling length it is called count so y just like this and yep the code is running the same way okay so this is great this is how you can cycle through all the elements in an array or a list however there's actually an even simpler method specifically designed for lists and arrays instead of a four in handling the iterator variable ourselves instead of that we have the 4 in and for this one we Define an iterator which in this one instead of being an index it is actually going to be an iterator of the type that contains our collection elements we have an iterator in some kind of collection so basically this one helps us automatically handle all that Cy logic without keeping need of some kind of index doing a for each is really simple we just write for each and then over here we write the name for our iterator variable however this time again it is not an index so we're not going to use an INT we're going to use whatever type we're cycling so in this case we are cycling through an array of strings so for each string then give it some kind of name so in this case let's call it name then the keyword in and then the name array and yep now here we can just do console the right line and just print out this name variable let's see what it does and you basically the same thing so the four and the 4 in work the same way basically over here this is going to loop as many times as we have elements in our array and every time it Loops the 4 each itself is going to handle updating this name variable over here so when this runs the very first time it is going to assign Code Monkey to that name then the second time it runs it assigns Airmen and so on so it basically handles updating everything updating the iterator variable without you needing to keep track of indexes or anything like that now technically I should also say that the 4 in this one works with anything that is enumerable like any collection it works with arrays it works with list and a bunch more I cover more collection types and how to make your own custom types inumerable in a lecture in the intermediate section here is the demo for this lecture this is kind of like a mini RTS game so I've got a list of all my units and over here are the various units in the world here in the code the units are simply a list of units and then I have the simple code that just test when the player presses the left Mouse button when it does then does a 4 each through the entire unit list and tells each unit to go to that Target position so with all the units inside the list if I click if there you go they all move straight towards the Target and now by using the exact same code if I modify the list and remove all kinds of units so let's remove a bunch of them and leave just one unit if I do that and now I click and if there you go the same logic is now moving just one unit so this is a great practical example of how useful Loops are and specifically the for each Loop you can write some kind of logic that applies to some kind of list and that logic will work regardless of how many elements are on that list also a note it is perfectly fine to do a four each or even a four on a list or array with no elements so if we raises so that it's pretty much just an empty array and if there you go everything still runs there's no errors basically over here if the length is zero then zero under zero that is already fall so it's going to skip it and over here on the 4 in if the array is empty then it's also going to skip so these are the main four Loops while is the simplest one but personally I never really use it and same thing for du I very very rarely use these two then down here the four this one is extremely useful in tons of scenarios and for each this one is perfect for doing any kind of logic on list or array all right so that's Loops it's another really important core component of programming so definitely make sure you fully understood everything in this lecture before moving on next we're going to learn one of the core pillars of objectoriented programming which are classes so let's learn that in the next lecture hello and welcome I'm your Cod Mony in this lecture we're going to learn about classes what they are how we Define them how we create objects of a type of class and what we can do with them all right so let's learn about classes this is one of the Core Concepts about C and other objectoriented languages a class is really just a type for example the list that we saw a while ago the inist type is a class and an object is simply an instance of that type so in any of those objectoriented languages you define an object type meaning some kind of class and then create individual instances or objects of that type for example we can define a class named player and inside of it we can define a field for a string name and an in health and then we can create two instances of that class and on this one let's say we set the name equals Code Monkey on this one name equals Iron Man so importantly note how each instance of this type each instance is completely separate the class defines the structure of the type but then the instances that are created from that type each of those has their own memory with whatever that is inside those fields so the class definition is kind of like a blueprint and using that blueprint to then create different objects so here we are in our code let's begin by defining our class and again it's very important where you write the code for example you cannot start writing a class directly here inside a function this throws an error because we cannot define a class directly inside a function so make sure you are writing outside the function and now we can either write inside the program class or we can make it separately either one works in this case let's make it inside the program code block so write in here if you write inside the program class code block if so then we're basically making a n class which in this case is perfectly fine so let's do this we start writing the keyword class and then the name of our class let's say we're defining a player and then open and close our currently braces all right so this is it just with this we have already defined our class which like I said a class is really just a type so it's similar to like an ENT or something so up here usually we've seen Define for example an in for the age and over here we can Define our custom player type player and if there it is just like this this works perfectly note how the color over here is different that is because this one is correctly identifying the player type and again if you're seeing compile errors once again the unlikely culprit is due to being case sensitive so here if instead of player I just write player this is an error there is nothing named player with p on lowercase so always remember that code is case sensitive and of course just to clarify the name over here can be whatever you want doesn't have to be named player the name of the class can be whatever you want and the name of the variable that you create from that class that can also be whatever you want so with this we have defined a variable of our custom type however we just defined it we didn't actually initialize it just yet if we try doing a console. right line then here nope we have an error we cannot do this so it's the same thing as if we try printing the age we also cannot do it we need to First initialize it now in order to initialize it usually for something like an in for this you can usually just sign a value and Yep this works but our custom type isn't a number like I said it's some kind of object so to initialize we need to create our object and the way we do that is with a new keyword so pretty much Exel like we saw with the list so we just do new and then our type so in this case it's called player and then just add the parenthesis so open and close and yep just like this and if we do a log on our player variable let's see and yep it does say it does print a log and it pretty much prints the type and the type is inside Code Monkey C course. program inside the program class and then it's finally called player so y we have indeed created a new player object and we assigned it to this player field so here we have created an instance of our type and now you might be hearing me say that word instance without fully understanding what I mean by that so I mean goes back to the idea of the class definition being some kind of blueprint and then when we do new player we are essentially creating a new object of our type so we're using that blueprint to create an instance of that type meaning that we can create as many instances as we want like for example let's rename this into the Code Monkey player and then let's make another one another one of type player let's call it Iron Man player and let's also do new player and Y just like this we have two completely separate objects that both share the same class so both were made from the same blueprint but each of these two objects is are complete separate object if you want you can actually verify this so let's print out the code monkey player and then let's print out the get hash code this is a default built-in C function that every object has by default it basically returns a hash code for that object so we can print this one out and then print out the one for the Iron Man player so Iron Man player. getet Has code we can print this and Y we can see our two objects are indeed two different objects since they have different hashes so this part is really important for you to understand the type definition itself is kind of like a blueprint and then we use that blueprint to create instances of that type each instance is different and each instance has their own memory and their own Fields their own values and so on okay so now that we've seen the absolute Basics defining a class and creating an instance of it let's see what else we can do one of the main things is actually what we're already doing over here when creating a brand new player this kind of looks like we're calling a function and it kind of is now technically this isn't a function this is what is called a Constructor which is basically what constructs an object of AR type by default any class that you define it will automatically have a Constructor just like this so one with zero parameters but if you want you can also explicitly Define it you just write the exact same name as a class so in this case player and then open the parenthesis and then we can add whatever parameters we want or none so let's just close the parenthesis and then just write a code block all right so Yep this is a Constructor and over here you can write whatever logic you want now again it's very very important that the name perfectly matches the class name if it does not match there won't be an error if it does not match and this assumes we're just defining a regular function and requires a return type in order to define a Constructor which again is a kind of like a special function for that one it has to be named exactly the same name as the class okay so here we have our Constructor and here we can pretty much build whatever logic we want that is going to be run whenever the player is created up here for example we can add a console. right line and over here let's just say creating player okay great like this except we are actually seeing some errors over here it says that player is inaccessible due to it protection level now this has to do with access modifiers which is a topic that I'm going to cover in a few lectures for now let's not worry too much about it just know that over here on the player Constructor over here before we need to add the keyword public so it has to be like this public player and then the parenthesis so make sure you write that and there are no errors now and if we test and if there it is we can see we are indeed creating two players all right awesome now another thing we can do is Define functions inside of our class and we Define them just like we already saw the only difference is right now we actually don't need to mark it as static I won't cover what static means in detail in the next lecture but for now just know that if you're writing a function directly inside the program that you call it from Main for that you need to mark it as static because main itself is marked as static but down here when working with our class over here we don't need to mark it as static so for example let's define some kind of function let's return nothing so void and let's call it say hello and over here pretty simple we just do a console d right line and just print out hello okay great so with this our custom type now has a function as to how we're going to call this well up here we have an instance of our player class so we created our object we can simply access that object and then press on dot which again the dot means that we are accessing something inside the previous Sy so we're accessing something inside the Code Monkey player which is of the player type and now we can call our function except actually right now we can't again it's the same thing that I mentioned a while ago it has to do with the access modifiers that I'm going to cover in detail in a few lectures so again for now in order to make this testing work let's just make sure to mark this one as public so public void say hello and now if we go up here and press on thatt yep we do see our say hello function and now we can call it just like we call any other function yep just like this now importantly is we are going through an instance of our object so we're not going through the object class itself rather an instance of it if instead you do write the class name so you do player. say hello then know this is an error this does not work to call this function we want to call it through the actual object that is created of the player type and now at this point perhaps you might be noticing that for example over here the console not right Line This one is different for this one we are not actually creating a console object we simply access the function the right line function directly through the console class and again that has to do with static which I'm going to cover in detail later on so for now just make sure that the say hello function this one is is not marked the static and up here make sure we are calling say hello on the created Code Monkey player okay so with this if we test and if there it is creating the player and then calls hello okay great here we can see we are running a custom function inside of our custom type so since we can Define functions another thing we can do is also Define some class variables and for that we write them inside the class block so not inside a function we don't want to create a local variable we want to define a class variable so let's go up here just before the Constructor let's Define a variable like for example string name and if we want we can even give it a default value so let's say Code Monkey okay great so here we have defined a name variable and now if you remember from the scope leure from that one basically anything that we Define in a code block like a variable over here defined inside the class code block that means we can access this variable inside any of these nested code blocks so for example over here inside the hello function over here let's print out so let's use the string interpolation method and let's print out the player name okay so like this let's see and yep it does say hello Code Monkey okay great so far so good now by the way I'm calling this a class variable or member variables but sometimes I use the term Fields again it's another example of interchangeable names technically the more correct name for this is actually a field the word variable is more used for when it's a local variable defined inside a function but those terms are pretty interchangeable so if you show someone this code and you call this a variable they will understand what you mean okay so here we have defined a class variable or a field and like we saw up here we can access our object and call this function and we can access this field in the same way however again the same thing in order for that to work we need to mark this field also has public so we can access it from up here so over here we can go through the Code Monkey player and access the name and I can set it to something I can read it or modify it so for example for this one let's set it to code monkey and then for this other player down here for the Iron Man player let's set it to Iron Man and then let's call the say hello function on this one okay so let's test and see what this does and if there it is creating player then says hello Code Monkey creating the second player and says hello Iron Man so here we are creating objects modifying values inside of it and also calling some functions with this we can also go back and see exactly what I covered in the diagram in the beginning on how the objects that we create these are essentially objects that are separate from one another they are created from the same blueprint class but each of them are different each of them can have different values that is why over here we are creating two instances of our player type and then on one of them we are setting the name to Code Monkey the other one to Iron Man and that works because they are completely separate objects each with their own data so when over here we are modifying the name inside the Ironman player we are only modifying the name inside this specific object meaning it does not modify the name on The Code Monkey and does not modify the name on any other player object we have when we modify this we are only modifying this data on this object this is another really crucial thing to understand how each object each instance of a class each of those has its own data stored in memory Okay so we've already learned quite a lot about functions fields and Constructors another thing we can do over here on the Constructor is we can Define some parameters so pretty much like we would for a function for example let's say we receive a parameter for the string name then over here let's print out the name so creating player and then pass in that name okay like this and now up here when constructing our players for this one let's pass in Code Monkey and for this other one let's pass in Iron Man and for now let's just get rid of this other code so let's just leave those two so just doing this just constructing a Code Monkey player and an Iron Man player and yep it works creating each of them okay good so now here's an interesting scenario let's say we want to define the name over here on the Constructor and then we want to update this field and then we want to use that field down here right now that is actually not going to work so let's put the default name to something else let's put it just a dash just to make sure that it isn't updating on the first one let's test let's just go here Code Monkey player and let's call say hello and same thing on the iron man. say hello and yep look at that so it does create the player and does receive the Code Monkey string but then when saying hello it just has the default Dash that is because even though we called all of these we call them all name even though these are actually two separate symbols when we receive the name over here as a parameter in the Constructor this parameter over here is just a local variable by itself this parameter has no connection to this variable even though the name is exactly the same that still does not mean that they have any connection we need to explicitly Define that connection also by the way here's a quick tip on visual studio if you just place the cursor on top of a symbol it automatically highlights all instances of that symbol so note how over here it is highlighting this one and this one and does not highlight this one and this one that is a clue that these are two completely separate symbols even though they do have the same name if we click on this one yep it does highlight this one and this one but not these two so these are completely disconnected piece of data in order to connect them basically we just need to assign this variable to the one that we receed here so over here you might think to write name equals name with the assumption that the first name is going to refer to this one and the second one is going to return to this one but nope this doesn't really work we have a green squiggling line if and if we put it inside assignment made to the same variable so here once again we can use the cursor trick in order to see yep here we are referencing the same symbol we have no reference to this one itself so one option that we have here is choose a different name sometimes people like to solve this problem by adding an underscore n their parameters so you could add underscore here then one here and one here and doing so yep now this works so now these three instances are instances of this symbol but if we select that one yep now that one is that one and if we run this code and yep now it does work creates the player and says hello with the correct name so this works this is one option you can simply Define your parameter names to have different names from your class variables or another alternative method the one that I actually prefer is to write the exact same name so over here let's name this one exactly the same name name and also this one also name but here we have the problem we're always accessing the same thing so the way we change that the way we access this field specifically the way we do that is with a keyword this this is a special keyword that like name implies refers to this object it refers to the object itself where the code is running on so by using this we can essentially reference this object and doing that we can reference this class variable again we can put the cursor over in order to highlight and Y now we do see these three are referencing this symbol and these three are referencing this symbol and if we run this any of now this word creates both players and correctly assigns their name so once again we have two separate instances of our player class each with their own data and each with their own name we are setting that data through the Constructor itself and then we are using that data in some kind of function okay great so here we learn about classes this is one of the core building blocks of any object-oriented language like C this is yet another topic that you must make sure you fully understand rewatch this lecture if you need to classes are one of those things you are going to use a ton through your programming career so definitely make sure you fully understand it now in this lecture I briefly mention two things access modify and static so let's finally learn about those beginning with static in the next lecture hello and welcome I'm your kmy in this lecture we're finally going to learn what is the static keyword and what does it do okay so since the very beginning we've been working with this default code in the beginning I told you to just ignore the static over here but now since we have already learned what are classes now we can finally start to learn what is static in the previous video we wrote this code and I told you to make sure not to write static inside any of this either the field or the function what it means is when you you define something as static when you do that you are making that belong to the class itself as opposed to any instance of that class so if you make something static like a static score field if you do that then now that symbol that score that only exists on the class itself and does not exist as a separate copy for each individual object you create of that type so here when we made our custom class we wanted this function and this field we wanted that to belong to each instance of that class we wanted that as opposed to belonging to the class itself so that is why we made these intentionally non static and by doing that we made it so that up here in order to call this function we need to call the function on some kind of instance of our type so that is why we need to create a Code Monkey player in order to call the say hello and if we try doing player. say hello nope this does not work this is an error because we marked this as non-static means that we need an instance of our type in order to call this function and it's the same thing for the console. right line that we've been using for this one we can inspect this function so we can right click in here and then let's go into this one go to definition and if you do that yep it opens up this window which basically shows the code inside the right line function and over here we can see this function is indeed marked as static that is why when we use it we use it through the console class itself meaning we never do a new console and then using that console we call right line we don't do that since that one is Mark as static we access the console class itself and then call the function directly on the class itself so static means this belongs to the class itself and not any instance of that class as to when would you use this one example is over here on the console it's super easy to just write console. right line write something to the console is much easier as opposed to having to create a new console object having to keep track of that object in order to call the function we just access the function directly makes it super easy to use and then it can make sense if you have some kind of behavior or some kind of data that you want to belong to the class itself and not all the instances of that class like for example if you have some kind of score that is meant to be shared between all the other objects if so then it might make sense to Define that in class that way we have just one score field that is shared between all the various players in order to make something static it's really simple we just add the static keyword like for example over here on the say hello function let's mark this as static however in doing so we can already see two errors there's one over here on the name and one up here on our players so let's actually for now just comment out this code and let's just do player. say hello yep now this one does work because that function is now marked as static but now the are down here on the name like it says here an object's reference is required for a non-static field object reference is really just another word for object instance this basically means that we cannot access non-static Fields whilst inside a static function that is because again static means that it belongs to the class itself and not any instance so since this function does not belong to any instance naturally we cannot access any instance variables that is also why quite a while ago on the variables lecture when we were creating some class variables up here I said that if you want to create a variable that you wanted to access inside main you need to mark it as static if we just make an in age over here and over here try to access age equals something and over here try to access age equals something nope this does not work we cannot access it in order to make it work this needs to be static because main is static so whil inside a static function we can only access static fields in order to make our code would work here we can really just mark this one as static with the obvious side effect which is like I said this field now belongs to the class itself so with this we could not have multiple players each with different names by doing this there's pretty much just one name field regardless of how many player instances we have and by adding static over here we created yet another error right here and once again it has to do with this keyword which like I said this one refers to the object that this code is running on but once again this one is a static field meaning it does not belong to any instance so we cannot access it through a this which refers to a specific instance and if we remove this then we have the exact same problem we had which is all of these are referring to the same symbol so we want to access this one and as how we access a static field if we can't do it through this the answer is we access it through the class name so over here player. name and once again put the cursor over in order to highlight and yep now it is correctly referencing these two okay so we have no more errors now let's see what this is doing so first let's call player. hello and then let's create a new code monkey player and let's print player. say hello again and then let's create an Iron Man player and now let's print say hello again now if you want a quick challenge try to pause the video right now and try to guess what all of this is going to print on the console so which name is going to be printed over here on this function call which one on this one and which one on this one so if you want pause the video right now and try to give that a go try to get a guess over here let's run the code and see what this says and yep it says hello nothing then hello code monkey and finally hello Iron Man here we can clearly see how static data belongs to the class itself and not any instance of that class basically the first time we say hello the name is going to default to a dash so that is why print stash then we create code monkey and when creating that object that individual instance of that object is going to access the global name field that is inside the player class so the static field so we're going to update that one from Dash into code monkey that is why it prints that and finally create an Iron Man and again same thing that is going to modify and update and override the name so the name no longer says Code Monkey by the end it only says Iron Man the important takeaway here is that even though we are creating two instances of that object since over here we are accessing a static field remember the static field belongs to the class itself so we have just one of these even though we have two instances of our player now it might seem like I'm repeating myself a little bit too much in this lecture but this really is a crucial thing to understand the difference between making a field static or non-static the difference between how many instances of a field you have this is really crucial to fully understand so you don't get confused if it's static you have just one field that is shared between all the classes and if it's non-static then each instance of that type will have its own field like back here in the diagram we Define a static in score and this one is basically shared between all the other objects but then we have a non-static string name and inal and each object has their own copy has their own data within that field now we already saw how we cannot access a non-static field from a static function but the opposite is valid so here on say hello if I remove the static to make this a non-static function and if there is no eror down here while on a non-static function we can indeed access static Fields although again remember how a static field This one belongs to the class itself and there's really only one field that is shared between all instances of that class so for example up here let's create a new Iron Man Code Monkey so let's actually Al uncommon this code up here for example let's create a new code monkey player then let's create an Iron Man player and then let's print hello on the Code Monkey player itself so once again if you want a mini challenge try pausing the video right now and try to guess what this one will say and if we test and yep look at that it says hello Iron Man even though we are calling the say hello on the code Mony player object and the reason is once again because this field is static so when we create a new Iron Man that One is updating that static field to just say Iron Man so down here when we access static field we are accessing the only field that exists within the entire class now let's just learn one more tiny thing about static which is how you can mark the class itself as static so here you can write static class and basically by doing this the compiler forces you to make everything inside that class as static so we cannot have a regular instance of our player Constructor we cannot have this and we cannot have a non-static function it needs to be marked as static so just like this so this can be helpful for example when you want to make sure that everything is static if there are some kind of class like for example the console here if we once again right click and go to the definition we can see yep the console is smart as static you are never ever intended to create a new object of type console that is never supposed to happen everything inside this console class all of this should be static all the functions all the fields all of this should be static and use as static so in order to enforce that you can just add the static keyword and basically the compiler won't force you to make everything within that class marked as static so when you want that add static but when you want to mix between static and nonstatic then just don't add it and simply add static to the things that you do want to make static all right so that's static now if you're a beginner and you're slightly confused go ahead and watch this lecture again learning the difference between static and non-static is really important you need to fully understand the difference between what belongs to an object and what belongs to a class itself next let's learn about access modifiers this is something that is extremely important in order to write good clean code so let's learn that in the next lecture hello and welcome I'm your Cod monkey in this lecture we're going to learn all about access modifiers specifically private and public learn what they do and just how important they are okay so access modifiers like name implies it controls what kind of access we have with something we've already seen them a little bit a while ago when we defined these functions inside our class I told you that you need to make this public this is the access modifier in general you have two access modifiers you have public and private now technically there are more there's also protected and there's also internal and a bunch more so technically there are more but most case you will only be using either private or public now here I told you to make this public because if we don't Define the access modifier if we just do this just like this then up here we can see our function has an error it is saying the say hello function is inex accessible due to its protection level that is because if we don't specifically say public if we omit the access modifier then this one basically defaults to private so if we write nothing is the same thing as if we explicitly write private and like the eror says we cannot call the function because we have no access it is private when we make something private it is only accessible to that class and nothing else so up here we cannot call the sayell function but for example over here inside any code inside of the player class over here we can call say hello and Yep this one works perfectly so by making something private it means it can only be accessed by that class where it's been defined and what I'm saying here applies to functions but also to fields for example if this one is public then up here on the main we can access Iron Man player. name and we can set it to something so this works this is valid code however again if we omit this which again is going to default into private if so then yep now this one is inaccessible if we do want this field or this function to be accessible from outside then we do need to mark them as public so public string and over here public void now if you a beginner you might be thinking this is just adding needless confusion to the code you might be thinking why wouldn't you just make everything public so you can access everything from everywhere and that is one of those questions that is perfectly reasonable for a beginner to ask and as you grow your skills you will actually see just how valuable this is the next two lectures are going to be on the theory behind how and why to write good clean code and one of the main clean code principles is simply to limit access the reason why public and private exists the reason for this is all about helping you manage complexity and the best way to manage complexity is to limit accessibility so access modifiers are really extremely important like I said I'm going to cover this topic in much more detail in the next few lectures for now just know these are extremely important and as a general rule you should always default to making everything private and only make it public when you have a very good reason to do it so by default we should Define all of these as private so this one is private this one make it private and the public string make it private yep so by default we should start writing our code like this and then for each piece of code we write we should be able to ask ourselves okay does this need to be public and if so like for example over here we do intentionally want to create some player objects so we do want this one to be public then for the function we also want this code up here to be able to call the function so this one should be public however for the name field this one is meant to be an internal piece of data that is only used by this class so this one should be marked as private and out here we should not have access to that field so as general rule just follow that basic guideline default to making everything private and only make it public if you absolutely need to if you just follow that one very simple rule you will write much better much more high quality code and like we saw technically we don't need to write the access modifier if you want it private you can just submit it and this is going to default to private however personally I like being as explicit as possible so even though technically you don't have to write it personally I always write it every time I want to be very very clear that I want this to be private I don't want to have to remember where are the defaults because actually defaults default to different things depending on what code you're working on there is a great guide on the C docs that goes into this topic in a lot more detail so you can see all the various access modifiers and from where you can call some kind of thing or access some kind of variable and then down here if we scroll down we can see yep the default access summary table and you can see how for some things if we omit it it defaults to private but for other things it defaults to public and for others defaults to internal I'm going to cover all this in detail in future lectures interface strs inom and so on since the default actually defaults to various different things depending on what type we're working with that is yet another reason why I really believe you should be as extremely explicit as possible so over here do not just submit this let's write properly private and now that I've taught you what are access modifiers I can finally explicitly Define them over here on this code honestly this is something that has been driving me crazy throughout this whole course I didn't want to use them until I explained them I really don't like how this default code is set up but but now that you know what they are now I can finally refactor this code over here for the player class this one is only going to be used inside the program class so let's make it as private Again by defining this one as private means that we can use it inside of anything within this program class code block then for main let's also default this to private this our main entry point for our code so we don't want any other code calling this function and then for our program class instead of making internal let's make it public public because in this case this one is a global class inside this namespace and if we try making this as private then no this actually throws error basically elements defined in a name space itself these need to be either internal or public so let's put it as public okay so now the code is much more explicit everything is much more clear we can know for certain that the main is only going to be the entry point and it's never going to be called from anywhere else we can see that this player class is only going to be used inside the program class and within the player class we can see that the Constructor is meant to be accessible from outside this function also accessible outside but this name field this one is meant to be only used by this class okay so with that we have learned about the basic access modifiers and this pretty much concludes the absolute Basics so now like I said in the next few lectures let's learn a little bit more on the theory behind how to write good clean code starting with one of the most important guidelines for that which is naming rules so let's learn that in the next lecture hello and welcome I'm your Cod Maki in this lecture we're going to learn something extremely important let's learn about naming rules this is a very very important lecture that you should definitely refer back to whenever you have any questions following good naming rules is something that will make you a 10 times better program so first what exactly are naming rules these are really just rules you define and then follow while writing your code it is extremely extremely important that you are consistent in applying your rules you don't want one class in your project to be written a certain way and a different class using a completely different naming scheme that would create a ton of confusion make it really hard to read the code so naming rules and writing code correctly is very very important one mistake that I see a lot of beginners do is they send me some code and ask why it's not working then I take a quick look and see all the code written in a very messy way some functions are in lower case others using snake case some fields are in Pascal case some in camel case always remember remember code is case sensitive so it is really extremely important that you write the code correctly if you change a character from lowercase to uppercase then you are suddenly referring to something completely different if you are a beginner always keep that in mind code is case sensitive so don't just write code semi- randomly write it properly as if you were doing some kind of English test now with that said when it comes to naming rules you can use whatever rules you want different people like different rules for example some people like to use a prefix and variables they like to add an underscore or M underscore or other people like to use a postfix some people like capitals for Capal case for properties St case for fields which by the way here are the definitions for those terms Pascal case this one starts with a capital letter and then it is lower case and then a capital for every word afterwards then chemel case this one also has a capital for every second word but the first one is lowercase and finally snake case for this one you just use an underscore to separate the various words these are usually the various ways you have of naming some kind of symbol and like I said the specific rules you follow don't necessarily matter every rule can be valid the most important thing is just that you are consistent and always always following through your rules so Define some kind of code style and always stick with it for me here are the rules that I follow and the code style that I use for function names I like to use Pascal case this is also the C standard so always capitalize the first letter of every word then for the function parameters for that I use chamel case so it starts lower case and then upper case after every word then for properties this is something that I'm coming to cover in detail in the intermediate section for these I use Pascal case for Conant again another thing I cover in the intermediate for this one normally use uppercase snake case so everything is uper case and use a little underscore to separate the words events are in Pascal Cas and then films these are in cam then another one of my personal rules is something that I mentioned in the beginning lectures which is a position for the opening of the curly braces now personally I do like opening it on the same line but some people prefer to place the opening on new line I know some people have some really strong opinions about this but really this is just the visual style that I personally prefer it's probably due to the fact that when I started learning programming when I was 10 years old I was writing some Mir scripts which you use this style so I'm very used to it and I like it but it's really just a visual thing if you like the currently brace on new line then by all means go ahead and use it and for some more very useful general rules first is spend time deciding on a proper name don't just use the first name that comes to your head actually think what would be the proper name for this symbol also do not use single letter names a lot of beginners like use this just because for some reason they want to be as compact as possible but remember the goal with code is not really to be compact it's to be understandable and single letter names usually are terrible the only exception for that would be it readers on a for Loop for those it's perfectly fine to use just an i or a j but for Prem much anything else use a proper name and not a single letter related to that also don't use acronyms or abbreviations those might seem really clear to you right now when you're writing the code but in a few weeks when you yourself get back to the code you might not remember what those mean again don't be afraid of being verbose if the most accurate name you can come up with is somehow 20 characters long then really that's a name remember you don't get bonus points for writing extremely compact code so always always value making your code readable and understandable even if it requires functions and variables with very long names and the final extremely extremely important rule is simply don't be afraid to rename things if you come up with a name and later on you come up with a much better name then just take a second to rename it with Visual Studio shortcut it's insanely easy you just place the cursor over something like for example the player class let's say instead of this being used just for Players let's say it was also used for enemies so I want to really name this unit the shortcut is Control Plus RR and it shows up this window and now we can easily rename it and yep look at that it renames every instance of this type so renaming things is really super easy so definitely make sure to do it if you come up with a better name come back and rename that symbol always remember code is not fixed it is meant to adapt and grow as you work on it and part of working on it is refactoring and renaming things so don't be afraid to do that okay so these are my general rules and the code style that I personally follow like I said you don't have to follow my rules figure out what works for you and follow your own rules but naturally here in this course I won't be using my code style so if when following this course you're not sure how to write some type of code come back to this lecture and watch it again like I've mentioned many times the code that I teach you how to write in my courses that is on the same level of quality as my Professional Steam games so as such this lecture this topic on Main rules this is extremely important if you just follow these rules or whatever rules you can come up with if you just do that you will save yourself a ton of headaches hello and welcome I'm your codm in this lecture we're going to learn more on the theory on how to write good clean code so first of all what exactly is clean code now there are many definitions for this for me it simply means writing code that is easy to understand and easy to modify with bat code you need to look at it for a real long time to try and work out what on Earth that code is doing whereas with clean code the intent of the code should be immediately obvious everything is very clear and straightforward and requires no effort to understand what exactly it's doing that's the goal the most important thing is simply following a good set of naming rules like I covered in the previous lecture always always choose good descriptive names for all your variables all your functions classes and really anything if you just do that then your code won't be infinitely higher quality than someone who does not follow any naming roles beyond that here's some more General guidelines starting off with a simple one try to avoid magic numbers these are numbers that are used directly in the code with no context for what exactly they mean like for example in this code this one has a bunch of numbers and just by looking at it you can't really tell what those numbers are supposed to mean after studying the code for a little bit you might be able to tell that for example over here this is supposed to be the Super bonus damage amount and for example over here if you see an if testing if the distance is under five and if so call the attack then probably this means this is the attack distance and over here test tesing if the distance is above 10 then go to jump so this should be some kind of jump distance so it requires spending some time in order to understand what the code is doing so by using magic numbers it Opus skates what the code is actually doing so instead of this we can improve this code by quite a bit by simply defining proper named local variables so over here just Define int for the super damage amount multiplier just set it to two and over here instead of that just use this one and now it is very clear what exactly this line of code is doing same thing over here let's def find n for the attack distance and over here n for let's say the jump distance and Y so now the code is much more clear much more understandable you can literally just read this and it makes perfect sense as if it's in proper English so if the distance is under the attack distance you attack if the distance is above the jump distance you jump super simple so with this there's no need to waste any brain power trying to understand the code all of it is immediately understandable another simple one is how you should never ever use strings as identifiers strings are terrible as identifiers because they make it extremely easy to make all kinds of mistakes for example look at this code over here I'm using a string identifier in order to find the name of a chall object and here in unity you can see that this code actually throws an error it throws a non reference exception here the code is doing a transform. fine to get the chn Sprite then goes gets the Sprite run and changes the color so quick question can you spot the air instantly and I'll give you a hint the a is actually not in here the is actually over here in the editor so again Can you spot it and the answer is no I know for certain that you cannot spot it because the air is literally invisible here in the code I'm looking for a child that is named exactly Sprite and here in the editor doesn't look correct over here looking in the hierarchy it does look like it has a child name Sprite however if I go on the inspector and I click over here on the name y look at that that's the issue it turns out that this child name this one has an invisible space at the end so the code is indeed perfectly correct there are no compiler errors here because everything is correct but it is looking for a child with this exact same name just Sprite and since the actual object has an invisible space at the end it does not 100% match the Sprite name identifier this exact mistake is something that has happened to me several times and drove me insane every time it happened or here is another example look at all of these strengths so can you quickly add a glance on the difference between all of these it probably takes you a little bit because visually they all look quite similar but identifiers are case sensitive so looking quite similar does not matter if it is now 100% exact it will not work so if you had an object with any of these names and you accidentally mistyped or wrote any of these other names if you did that you would get an error and you would not know what exactly is going wrong whereas if you don't use strings like for example over here I've got a variable named player 10 I construct a brand new player and I call some kind of function on the player if I use this instead of using a string if so if over here I mistype like for example I change this one into a lowercase L which looks quite similar but right away the compiler tells me there's an error so with this I can fix the eror without ever needing to run the code strings should pretty much only ever be used for text so you use them to store the player name or the name of some kind of object but never ever use a string to identify an object if you need to identify an object use a direct reference or some kind of strip long object or an enum or a tack component literally anything except a string name now as always with every rule there are exceptions sometimes especially when working with things like web development that usually involves a lot of Json and some simple HTTP text requests sometimes times you really have no choice and have to use some kind of string identifier in those case you should still avoid strings whenever possible if you need a string to Define some type of data consider defining a consant and never use the string itself only use the Conant if you do that and you mistype the Conant name the compiler will tell you but basically try to avoid using strings that identifiers at all costs then one extremely important principle in writing good clean code is the concept of managing complexity that's really the main difficulty as you write bigger and more complex projects the difficulty is in successfully avoiding becoming overwhelmed by managing that complexity as the project grows and for that your best tool is called information hiding meaning for any code that you write like a class you should only expose as little as possible in order for the class to be usable for example specifically in unity a lot of unity tutorials tget that beginners teach you to make every single fi public but this is absolutely a terrible Code Practice I made the entire video on this subject by making it public it makes this field easily accessible in the unity editor which is usually the goal however it has a side effect because it makes it also accessible from any other script now for a beginner this might sound like a good thing you can easily access anything from anywhere but that means that every single class in your entire code base which could be a thousand classes long every single one of those could potentially access and modify that field so as you write your code you have to constantly keep in mind that scenario so that's a lot of Wast of brain power so like I said the simple but extremely important guideline to follow is simply only exposed as as much as you need to and no more so do not default to making everything public all the time instead default everything to private and only use public when absolutely necessary if a field is only ever used in that class then there's no reason to make it public it should always be private that way when working inside of this class you don't have to keep in mind the entire code base you can just focus on this one single class since you know for certain no other class can ever touch any of these fields you could have a project with a million classes but but if this field is marked as private then it does not matter nothing else can touch this field other than this class same thing for functions if you have a function that is only ever used by a single class then keep that function private do not make it public if you do then some other class in your thousands of classes some other one could call that function which could cause some unintended results and if there is some kind of field that you need to access instead of making it public consider instead keeping it private and simply exposing some functions to get it and set it that way you have a lot more control control over how the underlying field is modified and you can even add some logs over here if you need to alternatively properties work as well you can make it a property and add some code in the getter or Setter I cover properties in the lecture in the intermediate section so in general whenever you write a field or function or anything always default to making it private always switch to public if you absolutely need to just following this one simple guideline will help you limit how much access different pieces of your codebase have towards other parts of your codebase which in turn helps you minimize the total number of connections which in turn helps you manage complexity even as the project grows one great principle is the single responsibility principle this means that each class or functions should do one thing and one thing only so if you have a game and you have some kind of player class you probably should not keep any kind of enemy logic directly inside that class that should probably exist in a separate enemy class same thing for functions each function should do one thing and one thing only don't write a thousand line functions that do 10 different things at once one good heuristic for seeing if a function is too big is simply if the best name that you can come up with contains the word and if so that probably means that function is doing more than one thing and would probably be best split into separate functions another related one is to keep your functions short so this kind of follows the same as the previous principle if your functions only do one thing they will probably be quite short if they are big then chances are they are doing more than one thing but this is also a very adaptable guideline that you should adapt to the situation for example in my game dinky Gardians I have quite a few relatively large functions breaking those apart into smaller functions would actually make the code hard to understand instead of easier so all of these guidelines do depend on the specific use cases but on average your function should be relatively short if your program is mainly composed of five underline functions chances are there's some room for improvement remember that refactoring is your friend and a Perfectly Natural part of the process another good one is group logic together meaning for example let's say I have some stats in my player class I've got these in my player class and let's say I've got some sort of UI class that I want to display these since these three separate Fields these are all really part of the same thing these are all player stats instead of working with three separate Fields instead of having to for example call some function on the UI that takes in three separate parameters instead of that consider making a custom type just to hold these stats so something like player stats that inside contains all those and then you would simply have a field just one field of typ player sets that contains all of the player sets that way thata that belongs together is actually together this simplifies things and makes it quite easier to understand in terms of comments the guidelines are the ones that already mentioned in the comments lecture in general you should avoid using comments if you need a comment before you actually write that comment see if you couldn't just rename some variables to make the code intent a bit more clear in most cases well-written code is going to be self-documenting but at the same time comments can be useful if your code is already extremely clear but you'd still like to provide some extra information on something like for example why you're doing doing that code or how that function should be used another good one is simply called dry or don't repeat yourself basically if there's some code that you need to run three times you probably should have a dedicated function instead of copy pasting that code three times although remember these are just guidelines meant to help you so don't look at these as some unbreakable laws in general code should be unique although sometimes you might need to copy paste twice in some scenarios that's fine but if you find that you need the exact same code copy pasted three or four times chances are that's a signal for some refactoring and speaking of refactoring this is an extremely important process that you should be doing at all times refactoring is the act of rewriting or reorganizing code to keep the code quality as high as possible this is a very very important topic and there is a dedicated lecture on refactoring in the beginning of the intermediate section so these are just a handful of nice guidelines to keep in mind to help you write good clean code that will help you a lot as your projects grow in scale and complexity although also do keep in mind these are just guidelines they are not holy rules that must never be broken the ultimate goal of all of these guidelines is to help you achieve your goals always remember that it is always up to you as a programmer to figure out when to follow the guidelines and when it's okay to break them there are some people that follow some specific code rules so rigidly that it actually makes the code worse so don't be like that follow these guidelines which will help you in most scenarios but also always be willing to adapt to the specific code you're writing now on this topic personally I'm a huge believer in writing good clean code if you're a regular on my YouTube channel certainly you've heard me talk quite a lot about it the reason why I love it so much is because I spent a lot of time writing a ton of code where I did not care about code quality I've been writing code since I was 10 years old and I really only started being very careful with the code that I write since I was some like 25 so I've already suffered quite a lot because of code that I would write with some nonsense variable names or some obscure acronyms and then later on when getting back to that code I would have no idea what I wrote or how I could expand upon it because that I really hope to help you avoid the suffering that I went through if you are a beginner I hope you can start off with on the right foot by focusing on writing good clean code right from the start if you do that it will help you learn faster and grow your skills by a massive amount now if you're looking for some more info on this topic there are tons of sources to learn from the one that I highly High recommend is the book code complete reading this book alone massively improved my code skills by 100 fold it really is that good it's a pretty massive book it's over 800 pages long and all really technical so it takes quite a while to get through it but if you do read through it and you fully understand everything if so you will come out the other side an excellent programmer capable of building any program or any game you can think of hello and welcome I'm your Cod monkey in this lecture we're going to see a nice beginner project that incorporates everything we've learned so far okay so let's inspect this project to see how everything we've learned so far is being used it's in the unity project so just go inside lectures then inside the beginner project and load up this scene then let's go ahead head on play and yep so here is the mini game running there is a player and I can move it by pressing either the arrow keys or W ASD every time I press I move a certain amount we can see the player is currently holding zero coins so I can move around and as I approach and touch one of these coins yep it picks it up so I can pick up a whole bunch of them yep that works then there are two buildings I can approach to interact with them and when I'm close enough I can press space and this building this gives me one coin and if I go onto the other building so I go straight to the right and once again I interact and yep now this one gives me three coins all right awesome so this is a super simple demo and importantly it's using everything that we've learned so far so let's inspect how this is working here in the project files we can see how this demo is really pretty simple we just have two files for our two classes and the coin file this one is super small let's go ahead and open it this one is mostly meant to just tag an object as of being of the coin type by the way right now you can ignore this syntax where it has a colon that says mono Behavior this is a Unity specific thing and this column here has to do with class inheritance I'm going to cover that in detail in a lecture in the intermediate section but for now don't worry about this here coin this one is really just a simple class so just like we covered and inside we can see the use of a nice function it is called destroy self returns nothing and has no parameters and in turn really this one just destroys this game object if we look in the editor over here in the hierarchy we can see a whole bunch of coins so all the coin spread out throughout the world each coin is really just a game object and in turn it has a spread render in order to show the coin Visual and then just has a coin script that we attached so on this function all we're doing is just destroying that game object and also note how the function is marked as public this is important because we're not going to call this function from inside the coin class but rather from the player class so let's look at that one here is the player class so most Logic for this little mini games over here inside this class so first of all the player itself is yet another class then we can see up top we have some nice class fields we have a field for a text mesh Pro object looking in the inspector we can select the player and we can look in the inspector looking in the editor we can select the player game object and look in the inspector and again this one also has a Sprite render for the visual and then we have our player script here we have the reference to the coin text mesh object and that's really just a child object of the player this is really just what shows the text then we can also see that we have a float for the move spein and the movement logic that one is handled over here inside the update function now this update this is a special Unity thing that runs automatically on every single frame so don't worry about the fact that technically we're not calling this function from anywhere as you can see this is actually called automatically by Unity on their backend so we have the update function and then over here we have some examples of some simple ifs so we're doing an if and then for the condition here we have two conditions and first we are testing for the input get key down for the double key this is a function from Unity that checks if the player is pressing some key so in this case pressing the double key and this function we can expect and see this one is going to return a bullion so when the player presses the double key this is going to return true and then here we also see an example of a logical or so we are testing if the player is pressing the double key or if they are pressing the up Arrow this way the game is playable with either W ASD or with the arrow keys so it evaluates these two conditions and when the player presses either W or the up Arrow when so this is going to return true so we're going to run the code inside if and over here this logic simply takes the transform this refers to the transform that this script is attached to so in this case the player transform the transform field itself this is another Unity specific thing so don't worry about the fact there's no reference of it up here that is actually inside the monob behavior based class which again I'm going to cover inheritance in detail in a future lecture so here we access the transform and we're going to modify the position we do plus equals meaning we're going to add onto that position and then we are creating a vector three which is a type inside the position so this really just a strug that contains an x y and Zed again it's another Unity type so we take that and we're going to increase it by zero on the X zero on the Zed but plus one on the Y meaning we're going to move our player upwards and for the movement amount we're going to multiply it by move speed which is our variable that we have exposed in the editor that way we can easily modify how fast we move so that's logic just a basic if some conditions and doing some basic movement applying that to all of the keys ws and D so moving up down left and right and here in the game we can test it so like this if I move I moving by this amount and if over there in the move speed I modify this let's say put it on five and if now it moves quite a lot more Okay so we've already seen classes functions variables and if statements In Action Now up here we can also see two lists there is a list of type coin for our coin list and a list of type transform for the building transform list here in the editor again selecting the player and we can see both of our lists and these are referencing the objects that are assigned in the hierarchy so basally what I did is this list started off as empty and I just and I just dragged and dropped each element individually so I dragged all the references to all the coins as well as all these buildings so back in the code here we have our lists and then over here on the update we are calling this function handle coins and the function does exactly what it says it handles all the coin logic it is being called up here on update meaning that this function is going to run on every single frame and inside here we have an example of Loops we are cycling through all the coins in the coin list using a 4 in and then first we do a test to check if the coin is not null and then first we do a test to check if the coin is not null meaning that the coin has not been destroyed just yet so here we see an example of a useful comment it is easier to understand this code by reading the comment rather than the code the comment provides some extra clarification on what exactly this code is doing since checking for n that could technically mean a bunch of things so in this case we are checking if it hasn't been destroyed we do that and then we're going to test the distance so we're going to use the static function inside the Vector three this tests the distance between two points so we're testing distance between transform. position meaning the position of the actual player and then the coin. transform. position meaning the position of the coin if that is under a certain amount so again over here we're keeping our code clean and using a proper local variable in order to make this much easier to understand so it test it and if it is under this amount if so then we're going to pick up the coin so here we add a coin to the player and then we tell the coin to destroy itself so again this is why it's important that the destroy itself is marked as public it's so that the player class can call it if this function was marked as private then the player would not be able to call it so we have this and on the add coin function it's all super simple it just increments a coin amount so this is just a simple INF field that we have up here so just a coin amount we have that and then we assign the coin amount to the text object in order to update the text however also note how coin amount is an INT whereas the text field this one expects a string so that is why we need to convert our in into a string which we can do by calling two string so here in the game I can move around and as I approach a coin yep I can pick it up any up it works it increments the text and everything works okay great then here on the update another function we have is over here the interact action for this we're doing the same thing that we were doing up here so we're testing if the player is pressing down the space bar and if so we're going to run this code so we're going to run our hand on building interaction function and this function like it says is going to handle the building interaction over here we're cycling through all the buildings now here I went with a for Loop instead of a 4 each normally I would also use a 4 each over here but I just want to use different Loops in this demo so just do a simple for cycling through the entire list so we cyle through it and we access the list on that index in order to get the current building transform then does pretty much the same distance logic that we saw so it has the current player position against the building position if it's under a certain interact distance and the player is close enough to interact with this building and then over here we have an example of a switch so this one does a switch on the building transform. name here in the editor in the hierarchy we can see our two buildings and we can see they have different names directly on the game object so we're doing a switch on that and if it is named this building coin one if so then we're going to add just one coin case it is named building coin 3 if so then we're going to add three coins and then on default if it does not match any of these two names if so that shouldn't happen so over here I just fire the unlog error so over here if I move towards this building and I'm close enough and I press on space and if there you go it does add one coin and if I go here and I press on space and if this one adds three coins okay so that's really it for this simple demo here we can see examples of variables being used we can see how we're using various data types we have both simple types like ins and floats as well as more complex types like list then we see some simple if statements these are going to make sure that we only run the move logic if the player is actually pressing a button then on the buildings we can see a switch in action doing different types of logic depending on the name of the building we can also see how we define various functions we can see how those functions have different access modifiers so all of these that are only used by the player class these are all marked as private whereas this one on the coin this one is marked as public so player can call it then we also see how we're using multiple different classes and then we also see how we're using different types of Loops A4 each and A4 and very important note how throughout this whole script there are only a handful of comments very few there are really no useless comments instead all the code was written carefully with good names every function variable has a proper name that makes it very clear what exactly it represents so this way all the code is self-documenting there's no need for comments you can just read the code directly itself and easily understand what exactly it's doing so here in this nice Syble project we have indeed Incorporated everything we have learned so far now here let me also make a quick note just in case you're a more experienced user and not a beginner I made this demo intentionally as simple as possible normally for building a demo like this properly I would use multiple classes to separate all kinds of logic instead of putting it pretty much all in just one player class I would use events and various other things to massively increase the code quality and readability I would make these fields serialized private I would make these serialized private Fields instead of making them public I would remove coins from the list when they were destroyed or I would simply use continue to skip them I would perhaps use collisions instead of distance checks so there are plenty of ways to improve upon this code but on this demo I wanted to all use as much as was taught by this point so that is why I simplified it so much if you are an intermediate user and you want a simple challenge then perhaps try refactoring this demo to make it work with some good clean code and if you are a complete beginner and you have no idea what I'm talking about and don't worry about it just keep watching the course and after you get to the end of the intermediate section maybe come back here and do that mini challenge all right so that's our beginner project hopefully you fully understood everything in this beginner section and had no problem understanding everything in how this project works if there is something you found confusing maybe go back and rewatch that lecture or just post a question in the comments I highly encourage you to make sure you fully understand absolutely everything in this beginner section before continuing on to the intermediate section hello and welcome I'm your Cod Maki and congrats on completing the beginner section in this section we'll learn a lot of stuff starting with installing and setting up visual studio and unity we also learned the absolute basics of unity which isn't necessarily needed if all you want is just learn C but I really hope you've been following the course using the companion project I really hope you've been doing all the interactive exercises answering all the quiz and reading all the frequently asked questions you really learn so much more by doing rather than just watching then we learned about various common errors what they mean and how to solve them like I said this one is a great lecture that works as a general reference so even in the intermediate and Advance section still come back to this one if you get stuck on some kind of error then we start learning from the basis of programming learning how code executes line by line what is a code block and just how important it is that code is case sensitive then we start writing some code and learn about variables how they store some data and what are those data types then was about if statements which help us do some conditional logic related to that we'll learned about the switch statement very useful to have tons of conditions after that we learned about functions one of the crucial components of any kind of programming then we learned about scope this one of those things that's a little bit tricky for beginners so I truly hope you understood this one after that was a lecture on comment what they do and importantly whether you should and should not use them next was the lecture on arrays and lists this is a really important data type for storing multiple piece of data then was another crucial thing in all of programming we learn about loops this is how we can execute logic multiple times and then since C is an objectoriented language we finally learned about objects which we build using classes then we learned about the static keyword and what it means also what are the access modifiers public and private after that we had one of the most important lectures of all it's all about what makes a good name and how to define some good naming rules and then yet another really important one about how to write good clean code by following some simple guidelines and finally in the end we put it all together in a nice beginner project involving every single element that we learned about so congrats again on completing the beginner section I really hope you did all the exercise and all the questions I hope you learned a ton and I'll see you on the intermediate section hello and welcome I'm your kmy all right so congrats on watching this whole video I hope you really took your time and focus on learning all the contents over here for this beginner section if so then hopefully you are much more skilled now than when you started if you're watching this in the future then check the pen comment down below to see whether the intermediate section video is already available like I said my goal is to make that video section free over here on YouTube if the premium core sells over 100 copies and I'm happy to report it thankfully there's already been over 100 people picking it up so one month after the launch of this video that video should be out or if you want you can just go ahead and pick up the premium version right now and continue on to the intermediate section right away along with all of the awesome bonuses that the premium version also has if you haven't been forcing yourself in order to put what you learn into practice throughout this video then now would be a great time to pick up the premium version and go through all the content in the companion project especially doing all the interactive exercises you only truly learn by doing so that is why I designed those exercises to put your skills into practice in order for you to truly learn and the first session of the weekly live study group that one is happening this Saturday March 9th so if you can't afford then check out the premium version with a link in the description if not then stay tuned for the free video covering the intermediate section one month from now also if you haven't already go ahead and like this video and hit the Subscribe button it's a tiny thing but it really does help all right so thanks for watching and I'll see you next time
Info
Channel: Code Monkey
Views: 91,586
Rating: undefined out of 5
Keywords: code monkey, c#, c# course, csharp, c sharp in 100 seconds, learn c sharp for beginners, c sharp beginner, c# beginner, learn C#, c# intermediate, unity game development, learn programming, learn to code, learn C Sharp, c# tutorial, c# beginner to advanced, c# learn, c# free course
Id: pReR6Z9rK-o
Channel Id: undefined
Length: 236min 50sec (14210 seconds)
Published: Tue Mar 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.