Basics of the C# Language Using Godot 4.2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is Mitch with find Point CGI and  today we're going to talk about the basics of   C with gdau so we're going to talk about what C  is why you want to use it what makes it special   then we're going to jump into how to install C  how to install Visual Studio what you need to   know to make your stuff work and then we're going  to jump into the basics we're going to go through   what inheritance is what type safety is what lists  are what for each Loops are what classes are we're   going to get go through a stepbystep instruction  from the ground up to teach you about CP and then   in the next video right after this I will be  building a budget app to actually um kind of   solidify a lot of these Concepts so that's what  I have in store for you guys today so let's go   ahead and get started okay so the first thing we  should talk about is just Why should you learn   C and the first reason is C object oriented that  basically means that you can model what the real   world is like so think about it kind of like  if you have a person you can actually set up   a person object to Define that person so this  allows you to have a big reusability mindset   when it comes to C it's also extremely modular and  has really great maintainability the code itself   is type safe and what that means is it basically  forces you to Define everything before you start   using it a lot of other languages like python  or GD script you can kind of Define things at   will and this can cause some issues with code  reliability right so sometimes you'll be in   gdau and you'll be running through something and  then suddenly you're like why is this broken and   then you look at it and you go oh it's because  I misspelled something or something like that   that's stuff that doesn't happen in C because  everything has to be defined before beforehand   so it's all so the compiler will actually tell  you what is wrong with your code a lot of times   so it's really useful the other great reason is  it's a super versatile language it supports all   sorts of awesome features and it always evolves  I mean Microsoft is evolving this thing all of   the time and that kind of drops into why we want  to learn C or why you should learn it and that is   it's really versatile you can create desktop  apps you can create web apps you can create   mobile apps you can create video games using C  with gdau and of course unity and you could do   artificial intelligence you could do so much with  this language it's actually kind of crazy and the   other big reason is it is a top language when  it comes to jobs and a lot of times people think   about I want to learn Game Dev so therefore I'm  going to learn GD script or I'm going to learn I   forgot what the language is but it's for game  maker and those languages aren't bad the job   Market's just really small for those languages  the nice thing about C is if you don't make it   in the game industry or if you go outside of the  bubble that is goo or other game engines knowing   c will get you a job somewhere and it's always  good especially when the Market's not very great   to know a language that is really strong and has  lots of job opportunities another good reason is   cuz it's extremely easy to learn it's a little  bit harder than other languages like python or   GD script but it's a super easy tolearn language  and finally it's got a huge Community you have   so many resources and tutorials and things like  that it's just crazy how big the c community is   for software development not for gdau so that's  something to keep in mind the gdau community is   extremely small right now I'm one of the few that  so that's something else to keep in mind so now   the question is okay so you've heard C and you've  heard net and net and C are always kind of said in   the same language right so what isnet well net is  a free and open- Source platform that is supported   by Microsoft so it's owned by Microsoft but it's  open source so you can actually go and contribute   and download the code and use it for whatever  you need but it is maintained by Microsoft and   the question that a lot of people ask me is  why would you ever want to choose Donnet as   your framework and the thing about net is it's a  platform it's not a language I know that people   think C andn Net are the same thing but C is  the language net is the runtime that handles   everything it's the libraries it's the Frameworks  it's things like that so the nice thing about it   is its crossplatform and it allows you to build  stuff for the web for Cloud for mobile for gaming   Internet of Things Ai and desktop now the first  thing that we need to do is we need to actually   download an installnet now I'm not going to go  through the whole process of all the small minute   details here but to use net especially with gdau  you need to download net so click on this right   here we're going to download net 8 net 8 is the  latest version ofet so I'm going to download the   unverified file going to double click on it we'll  go ahead and install it real quick so just click   install say yes to it going and allowing the  installation and then you'll see I'm going to   need to close down the applications that are using  myet version so it's going to take a second but it   should go through and now it's going to ask you  to reboot your computer so go ahead and reboot   your computer and I will be right back all right  and we're back so once you have your reboot done   go ahead and at this point we need to actually  check to see if it's been installed correctly so   let's open up a command command prompt here  and let's bring it over here and type net d-insar really running stuff like that you'll  see that it has our API run times and you'll   see other architectures if there is any now you  can see here if you see right here net SDK 8. do   something right in my case it's 203 but in your  case it could be you know I know 209 or something   right depends on which one you pull down and at  what time you're pulling this tutorial um but   what you want to do is you want to make sure that  you have the correct version ofet installed now   once you know that you're running the correct  version of the net SDK if you scroll all the   way to the top here you can see version 8.0.2 3  that's the version that they're using so that's   just something to keep in mind now once you know  that you have two options here we need to have a   IDE to be able to actually code our net code and  the best IDE for this is visual studio right here   this is Visual Studio it is literally built from  the ground up to B for.net that's what Microsoft   has built it for that's what it's for now if  you don't want to use Visual Studio or if you   are not on Windows because this is a Windows only  application that's where vs code comes in now I   typically code in VSS code for gdau not because  I don't like Visual Studio because I love Visual   Studio it's one of my favorite tools but because  you the audience are not always using a Windows   machine so I use VSS code so be sure to install  whichever one you want and you can go from there   now in my case I did Visual Studio code so if I  type VSS you will see here's Visual Studio code   and it's probably going to open up all sorts of  Windows because I use VSS code for a lot of stuff   this is actually a AAR navigation system that's  a future tutorial that I've been working on now   if you use vs code it will not work by default  with C without installing the proper workloads   so you can see here on the left you have your  Explorer but all the way here on the left you   have a little menu bar right here you're going  to want to click on extensions and type C sharp   like this and you'll want to pull down the C dev  kit which is the Microsoft official C experience   or you'll want to pull down the C base language  support I'm pretty sure the um extension has the   dev kit as well but you're going to want both  both of them are extremely useful and both of   them are awesome to use for your project now one  thing one of the reasons why this is so nice is   because it allows you to run and debug C projects  if you go to your Explorer you can see that there   is a solution Explorer here and this is how we  actually navigate our C projects you can see all   the dependencies the an analyzers Frameworks the  packages stuff like that so it builds in a lot   of that functionality from Visual Studio into vs  code so uh some people say that VSS code might be   a spiritual successor to visual studio eventually  though we'll find out when we find out now once   you have this we are pretty much good to jump  into coding our stuff in inside of gdau and and   learning the basics of C so let's go ahead and  jump into that now for us to be able to code in   C we need to basically go over the basics so one  of the biggest common basic task inside of C that   people do is called hello world and to do that we  need to create a script so in gdau if you've never   used gdau before all we need to do is come over  here click on user interface right click on that   control node and attach a script we're going to  call this app manager and the reason why is cuz   I don't want to create another script later  and have to recreate another script so we can   basically just name it app manager and then we can  use that going forward so we'll click create and   you'll see that vs code has come up now if you're  still relatively new to Gau you need to actually   set it up so that VSS code will come up so if  you go to edit editor editor settings and you   scroll all the way down to the bottom over here  on the left pane click on editor and then right   here change this from disabled to visual studio  code and that will basically solve that problem   then once you open up a script it will show up  right here inside of vs code so now we can close   that we don't really need that and this could be  closed there we go let's clean all that up now   with C in order to basically print hell World out  to the console we can just do that in our ready   now I'll explain life cycle and things like that  later in the tutorial but right specifically here   we'll just type GD do print we'll hit quote hello  world semicolon and we will hit save and I'll go   through what all of this means in a little bit but  just know that this is basically how to do a Hello   World so we'll click on gdo we'll click on play  we'll save this scene and then you'll see that   it's going to pop up with another window here and  it will say hello world right here in the console   and that's basically the basics of C so now you  know C pretty much well C's actually a little bit   more complicated than that but you can see that we  at least got it to do a thing so that's exciting   now let's take a step back and let's talk a tiny  bit about what this all means and I'm not going   to go through inheritance and all of that just yet  but I will say some of the basics right so I don't   want to go through everything we will cover that  later in the tutorial so first things first is   the using so using is a namespace right these are  including namespaces into our classes here or into   this file if that makes sense and a namespace is  um basically a keyword that defines a collection   of code if that makes sense so for instance if you  were to get rid of using gdau you'll notice that   suddenly C does not know what GD is it says the  name GD doesn't exist in the current context and   the that's the reason why using exists it's saying  Hey I want to use the gdau library in this script   or in this bit of code here so you're basically  just saying I want to use this and there we go   right now you might be asking okay why don't we  just include everything right why don't we just   have using everything right well the reason why  is because if we do using system. collections.   generic and we create a dictionary and I'm going  to show you guys this but don't worry it's not a   huge deal here you don't have to follow along  so if I say Dictionary d like that you'll see   using the generic type dictionary require to  arguments right which is fine so we'll [Music] go so see we have a dictionary here  and then if I type dictionary like this which dictionary am I using so if you look  at this there's a dictionary here which is gdau   collections dictionary T value and then there's  a dictionary here which is supposed to be system   collections generic dictionary so that's where  the namespaces kind of come in that's where these   usings come in is you're telling the compiler  this is exactly what I'm looking for and you don't   necessarily have to use this if you don't want  to you can actually come in here and just type   gdau Dot collections. dictionary and that fully  qualifies it right and it works and then you can   also do system. generic collections. generic.  ditionary and that also works so now you'll see   everything's happy but this is an absolute pain  to use if you have to do 50 dictionaries or if   you had to do a couple of strings or a couple of  integers or really anything that has to do with   c sharp you'd have to do these fully qualified  connections so instead they came up with these   usings to basically eliminate the need for  that so you can see if I get rid of system   collections generic like so you'll see a system  collection generic dictionary still works you'll   see that it's not unhappy with us and if we get  rid of this and we just have dictionary you'll   see that it works and that's the purpose of a  using statement and that's the reason why we   don't actually have everything included into your  class and also the other big reason is because if   everything's included into your classes then if  somebody named a class the same as something else   it could cause problems so this is how they've  worked around it basically now here this section   here is a class and a class is basically a basic  concept of object oriented programming right it   revolves around making a real world entity so  think of it kind of like this when you create a   class it is a blueprint or a prototype of what  that object actually is and um the nice thing   about it is a class basically houses everything  about that one object so in this case public   partial class app manager is probably not the best  title for example for for this but if I come down   here and I say public class person I can come  in here and Define what a person is so I could   say something like string name is equal to let's  say empty and then I could say int age is equal   to 20 let's say right and then I can come in here  and say public void get person's name like so and   then I can return name which actually this would  not be a void this would be a string but you can   see this is the definition of what a class is is  it's defining an object in this case it's a person   it could be a animal it could be a gun it could be  a financial transaction right it could be a lot of   things but the idea is that you are encapsulating  the data that is associated with that object that   person so you can see in this case I have  a public class person they have a name they   have an age and I can get their name from them so  the idea behind C is that everything is basically   segmented into these reusable components if that  makes sense now I'm going to burn this and get rid   of it but that's basically the gist of and we'll  talk about how to actually instantiate classes is   how to actually use them and stuff like that in a  little bit now you'll see that a class is kind of   broken up into three three or four parts right so  you have a public which is what's called a access   modifier so you have public private and internal  and I'm going to completely ignore internal uh   very rarely do I see people use it but it is a  thing there's public which means it's accessible   outside of the class Library private it which  means it's not accessible outside of the class   Library generally speaking you're not going to  use this unless you're making class libraries but   if you want people to be able to use it outside  of your project right outside of that specific   project you want to make it public if not make  it private it's always good to have proper Access   Control partial don't worry about this this is a  Gau thing and a generated code thing so basically   because gdau has C++ in the back end uh we have to  have partial to kind of extend classes don't worry   again this is not something that you're going to  see on your day-to-day life if you go and get a   job as a c programmer typically you don't see this  but that is what that is for class which is just   telling us that it's a class you have the name  of your class right here and then you have this   little colon and then control this is because of  inheritance so what this is doing is it's saying   App Manager inherits from control so think of the  world as like a big giant tree if that makes sense   if you come in here and you have something like  a node and then you have something like control   and then you have something like app manager  right so when you would do inheritance what   you're doing is you're basically saying that this  node connects down to this control which connects   to this app manager how inheritance works and  I will explain how it works on a more practical   example later but how inheritance works is if  the node has a function called get name right   then if something inherits from that node it  automatically receives the function get name   like this okay if the control node has something  like Get Wrecked size right then everything that   it inherits gets that but over here on the Node  3D node 3D doesn't get Rec size because it's   not inherited from the control node but it does  receive get name because it inherits from node if   that makes sense so that's the purpose of this is  they receive every public method that this guy has   including variables so if this guy had a variable  and I'll explain that in a little bit as well but   if it had something like public string name right  then that propagates to all of these guys like so   they all have that specific information so it's a  way to help encapsulate and organize your code so   that people aren't having gigantic class files  and things like that a lot of times like in C++   you'll have humongous files that associate with  a bunch of different things in C everything is   very encapsulated it's very reusable if that  makes sense so that's basically the the basics   of inherit and that's what this is doing is it's  just saying that App Manager inherits from control   if we were to get rid of that it would get rid  of these guys right it would say these guys are   no longer there but now this doesn't inherit from  anything so it loses that connection to the gdau   code base and it just kind of becomes its own  class if that makes sense so that's basically   what inheritance is all about now what else are  we looking at here well this here is a function   and a function is a reusable piece of code now  think of it kind of like this if I want my thing   to do something right I can write my code right  here right this area here is for stuff that's on   Startup so like if I were to uh hit the play  button this section here runs first then this   runs every single frame so if I wanted to say  hello world every single frame I could do that   put that here and then it will run every single  frame of the game play so if I come in here and   I hit refresh you'll see this is going to print  out thousands of them really quickly because I   have a pretty fast graphics card so it's going to  run pretty quickly but you can see there you go   now that's great but when you're doing code a lot  of times let's say You're Building A really crazy   AI pattern right we're going to go we're going to  detect the player we're going to hunt the player   down we're going to go kill the player stuff like  that this is perfect for what a process functions   for right it's to to run every single frame so  it could do all these checks and do all these   things but having all of that code built into  this one function is going to be horrible for   maintenance it's going to make things awful and  really hard to use so that's what functions are   for I can come in here and I can say private void  print hello world like this right and then I can   take this guy I can contr X paste it in there and  then I can come in here and say print hello world   like so and now I've abstracted that code you'll  see that now our code still works but it's now   a single line here and a function right here and  you might be thinking okay so you just moved code   right but think of it like this I could have 50  thousand lines of code in this one script and I   could have a function call here do a little bit  of logic and then another function call right so   I can have it all broken up so that there are SE  segmented out pieces of code that are reusable and   that's the purpose of function so functions are  really cool but what does all this mean well much   like classes you have public which is an access  modifier there's public private and internal once   again public is if you want EX it to be accessed  outside of the class private for when you want   it to not be accessed outside of the class then  there's override and override will override the   parent level classes code and what that means  is basically everything that's in here will run   instead of whatever's in the ready in the parent  above it and again this is something that will   come apparent later when we start talking more  about inheritance but just keep that in mind void   means it returns nothing and I know what you  might be asking because a lot of times people   don't understand what this means void means it it  absolutely returns nothing so nothing's going to   happen and it's not going to send anything back to  anyone that calls that function and then there's   ready which is the name of the function and then  there's these parentheses which basically means it   takes in nothing in this case in this case over  here it takes in a double which in this case is   called Delta so what does this mean well if I come  in here and I type double Delta like that and then   I pass in Delta like that it's going to take this  parameter it's going to pass it into here and   then it's going to pass it into here and then it's  going to send it here so I could actually go hello   world space plus Delta do2 string like that and  then if I come in here and I hit play you'll see   that there's going to be an incrementing number  that's associated with that object you can see   right here it's showing that number and you'll  notice that the number is changing slightly right   and the reason why is because what's happening is  the G do engine is sending this in to the engine   and then it's going to this print hello world  it's going to this thing right here and then it's   getting printed right here with the hello world  basically so now that we talked about functions   let's talk about variables so we kind of talked a  little bit about that right here with our double   Delta but basically a variable is a thing that uh  houses data and in a lot of programming languages   like GD script a variable's generic so it can  be technically anything but in C everything is   declared and the reason why everything's declared  is because it makes things a bit more efficient   and a bit easier to work with so since she sharp  is strong type that means that you have to declare   what that data type is so for instance you can  see here with double Delta that's me declaring   it as a double and so that's how we do strong  typed languages we have to tell it what it is   so in order to declare a variable it's actually  really easy you just come in here you type the   type of variable so string which means a word  s is equal to this is a word right and then you   also have int which is a number so 50 and then  you have a float which is a number with decimal   and that's 5.3 to whatever F like that and that's  how you know if that is a float now C has a bunch   of different types of variables that you can have  and variables don't have to be specifically those   types because there is just a absolute boatload of  them but variables can also be classes as well and   I know you're going okay hold on we just talked  about classes right they're they're like objects   right well variables can also be classes so if  you remember our class earlier where I did a class   person right and I had like a string s is equal to  quote right something like that I can come in here   and I can say person p is equal to a new person  right like that and that's me basically creating a   person out of thin air right that's me saying that  that person now exists so that's what variables   are is they're basically points in memory that  we can change and adjust now inside a c this is   considered a scoped variable and a scoped variable  is this function gets done this object these   variables get destroyed right so they're not going  to be able to get used outside of this little   function right here and that's where Returns  come in that's where all those additional things   kind of come in and what I mean by that is if I  wanted to return a variable from this function   right I can come in here and say return and then  I could return s let's say the string right right   but you'll see we have an underlying and that's  because there's a private void print hello world   void means it's returning nothing so C is going  hold on a minute you said this was returning   nothing so why is there a return statement so  that's C kind of error correcting you right so   we can come in here and say string like that and  then we can come up here and say VAR return string   is equal to that and then we can go GD do print  return string like that and now you'll see if I go   into Gau real quick and if we hit play you'll see  once it builds it you'll see Hello World this is a   word right you can see how it's printing that out  and that's because what it's doing is it's coming   in here it's getting this string it's getting it  set right here it's coming through it's creating   this integer it's creating this float which are  not being used that's why it's gray out it's   creating a new person it's printing out hello  world and then it's returning s now that also   works by the way you can set like for instance  person's string right here if this is a public   string you can actually set that so if I hit P.S  you'll see that it doesn't show up right even   though there is a string let's change this to name  let's say right so if I go p.n name it doesn't   work but if I come in here and I go public string  name I can go p. name and it works so that's   basically that access modifier I was telling you  about right so if I go p.n name is equal to Frank   and then I return p and I change my return type  to person I can come up here and change this to   from printing return string to. name and you can  see C knows what everything is is and also one   of the cool things is that you can actually change  this from VAR to person and now you know that this   function returns a person right here and you can  see return string. name so if I save that I can   come in here and actually I got an issue right  here I put a semicolon right there I can refresh   this and you will see that's going to fail because  I missed my semicolon so we'll contrl s try that   again you will see it's going to print out Frank  every single frame also interestingly enough I   could come in here and I could say instead of p.  name is equal to Frank I could change it equal   to S because it is a type string and this is a  type string we can make them equivalent to each   other and that works as well so you can see if  I hit play you will see it says this is a test   word because what we did was we changed this to be  equivalent to S which is this bit of data here so   that's basically what variables are and that's why  they're really cool now variables have different   scope like I said earlier everything inside of  here gets deleted when it's completed like when   this function gets done being ran it gets called  everything inside of it's dead it's gone so what   we have to do is we have to return that data right  so the person made it out of this entire thing   right so number number with decimal and string s  are all gone once it gets out of this scope and   it comes into here and it prints that person  this exists as an object right it exists as a   thing because it made it outside of that scope It  came it got returned so it came out of that scope   if that makes sense but what if I wanted to save  something for longer than that well that's where   class scoped variables come in and if you come up  to the top here you can actually create a class   scoped variable so you could say person person  let's say p is equal to a new person like this now   anytime this gets changed it doesn't matter what  scope you're in it will always be right here so   I could actually come in here get rid of this guy  here and now because it's up here it will exist at   all times as long as that class exists and this is  stuff that will make sense later in the video but   just know that this is how you get stuff to stay  between different things because remember once   process is complete so once this function prints  out the stuff person gets called it gets removed   so if you set a value and you want it to be alive  for longer than the um class function scope then   you need to add it up here and it will last over  multiple runs of this class now something to keep   in mind is this also means that if the class gets  called or gets nulled out then it gets destroyed   so for instance if you come in here and you say  p is equal to null then that's going to destroy   that person it's going to get rid of this object  and it's going to basically remove it from memory   so that's how you can clean it up if you need  to or or if app manager gets destroyed so for   instance you delete this node then this entire  class will get destroyed so everything inside of   it goes away if that makes sense now all of this  is extremely confusing it's a lot easier when you   build an app to actually make it understand hey  guys editor Mitch here so I actually um already   recorded the entire budget app I started going  through the whole thing and it ended up being   like 5 and 1/2 hours long and I didn't even cover  some of the stuff that Mak C good if that makes   sense and you know I I think I got like two hours  in and I didn't even explain what an if statement   is so I figured that it might be good for me to  split the budgeting app from the C Basics and   basically release them out you know one day apart  or a couple days apart so I'm going to go through   some more Basics here so that way you guys can see  what else C has to offer now something something   you'll notice is that it says App Manager 2 and  the reason why is because if you look here I   actually already have the entire budgeting app  coded here because I already went through the   whole tutorial but like I said I figured it would  be better to give you guys a better Foundation   than what I gave you guys in the beginning  so with that being said let's actually talk   about some more foundational aspects of C so I can  better prepare you guys for the coming budgeting   section of that tutorial now the first thing I  wanted to cover is if statements if statements   are really cool basically inside of C we have the  capability to uh do what's called an if statement   and an if statement allows you to say if something  is true then do this now in order to do that we're   going to need to get some user input so in order  to get user input inside of C we can do it one of   two ways we can go through gdau which is what  we're going to do or we could do it through   the console now I'm going to do it through here  because it makes more sense and it's a lot easier   to work with so what we'll do is we'll come here  we'll rightclick add any child node we're going to   add in a button okay and a button inside of gdau  is basically just a button right you can click   it and do things like that you come over here we  can add press me like this and we can kind of pull   this guy over here to the right now if we come up  here we right click add a child node and add in a   line edit like that we could just kind of put this  like this and drag this over here like that now   in order to actually use this this button here it  fires off this thing called a signal and a signal   is an event if that makes sense it's a thing so  a signal is basically saying if this happens do X   right and you can have custom signals you can have  gdau built-in signals you can have all sorts of   different ways to uh interact with objects inside  of gdau and I will cover a lot of that in the budg   shoting portion as well but I'm going to litigate  a bit of it here as well so over here on the right   there's a node section and if you click on that  this is where all of the signals exist inside   of your good do project you can see there's all  sorts of signals here there's button down button   up pressed toggled Focus enter exit right things  like that draw hidden right all that kind of stuff   so what you could do is you could click on this  guy double click click on your control node copy   this guy right here and click connect and what  that's going to do is it's going to tell gdau   that I whenever that button is pressed I want you  to run a function with the name that was below so   if we look at the signal you have this receiver  method how it works is when that button is pressed   it's going to go to the control node it's going to  go into the script and it's going to say where is   underscore oncore button underscore button down  right wherever it finds this it will run that   function function so if we head in here we can  say private void paste like that and then anything   in here will run so if we type GD do print hello  world like that you'll see that if I hit play and   I click on the button well unfortunately I have  that uh that print statement running so I'm going   to comment that out which that's another thing  if you hit slash slash it comments that line out   that means me that that line is no longer going  to run if you comment this guy out that means   that that line is no longer going to actually run  so if we come back here and I hit play you'll see   that nothing is being printed and that's because  these lines have been commented out these two   print so it will no longer print that information  and that's going to give us a perfect opportunity   to click press you will see Hello World and  that's because what happened was we clicked   on this it went to the tree if found the control  node it went into the script and it said where is   this function and then it ran this function so how  does that lend into an if statement and how does   that lend into actually getting user input well  inside of Gau you have the ability to fetch nodes   so if you look at your tree over here on the left  you can see there's your control node and here's   where the script is located right it's located on  this guy you can run what's called a get node like   that and then you can type it by doing this little  greater than symbol and we're trying to get what   the user has right here in this line edit right  so we can do line edit quote line edit because we   need to tell it where we're looking okay and I'll  show you guys what this means so we're fetching   this node back okay so we're fetching this object  so we can do is we can say line edit line edit is   equal to that right and then we could print out  our line edit. text like that and then if we head   over here we refresh this and I type hello world  like that and I hit press me you will see that   it's going to pull the information from here  send it into this guy here and it's going to   run this little guy right here which is to print  it out and that's basically how you can get user   input so how does that lend itself into an if  statement ah now that's where it gets cool so   if we come in here we can type if and then we can  use that if to basically run and segment our code   so what does that mean well if we come up here we  can say string we'll say choice is equal to line   edit. Tex text like that and then we'll go ahead  and print out that text and then we can do if   choice is equal to quote one then GD do print line  edit Choice was one like that and now if I come in   here you can see if I hit one and I hit press me  one choice was one if I do two let's say and I   hit press me you will see it only prints out the  two and the reason why it's printing up the two   is because of this guy right here but this little  bit of code is not being ran and the reason why is   because the choice right that value this little  line edit text so this guy right here this text   is not equal to one now you'll notice that I have  two equals and the reason why is because in inside   of c and most programming languages a single equal  means that I am making this equivalent to this I   am equal equaling this to this but two equals is  a question it's saying is this equal to this and   that's a huge distinction and inside of C you can  also do not equal so you'll see if I do that and   I refresh and then I set this to two and I hit  that that you'll see that it runs Choice was one   why because I put two in and two is not equal to  one and that's basically what an if statement is   now if we change this back to equal equal let's  talk about an extension of the if system to the   Els system we type Els GD do print Choice was not  one like that now we can come in here refresh this   hit two and you'll see Choice was not one and  you'll notice that what it did was we pulled   back our line edit we pulled back the text that  they put in there we printed it out we checked if   the choice was one which it wasn't it was two and  then we said oh well if it's not one else if this   is not a true statement then do this so now you  might be asking okay but what happens if I have   multiple things what if I want to check if it's a  one um and do something or if it's a two do this   or if it's a three do this right well that's where  else if comes in so you can see choice is equal to two Choice was two and I could say that or two and  now you'll see if I come in here here I refresh   this you can hit one choice was one two Choice was  two three Choice was not one or two and that's the   beauty of the if else if else statement and this  is basically the basis of building even the most   complex programs every program pretty much uses  a system like this to run now let's talk about   arrays and lists so arrays and lists are two a  very similar things so an array is called a is   a collection of objects or a collection of data  and basically you can come in here and you could   do something like int and then you can which is a  type number and then you can hit bracket bracket   int array and we're going to make that equal to  a new int bracket bracket and in inside of this   little bracket here is the size of that array  so for instance if it's three and I go bracket   1 comma 2 comma 3 like that now if this is five  then this should be comma 4 comma 5 now these   values don't have to be sequential you could  have something like 2 10 30 right so it doesn't   really necessarily need to be an exact amount but  something that you'll notice is arrays are they   are always the same size so they're always going  to be whatever the number is inside of here and I   can access that information by typing GD do print  and I can come in here and go in Array bracket   bracket and I could choose a number now these  numbers start from zero so if you take a look   there's what's called an index okay and I'm going  to come in here and show you so if you hit 0 1 2 3   4 right so that's how it works so this is a five  array element so we have zero which is our first   index one which is our second index two which  is our third index and the reason why that is   is because back in the day of bcpl language which  is before like C language so this was way back   you know probably like the 70s um they determined  that the pointer value of an address starts with   p+ 0 so that's why zero ended up being the first  number if that makes sense so something just to   keep in mind is arrays and lists and any type of  collection just starts from zero so if I were to   type zero that's equivalent to one if I do three  that's equivalent to 30 right because it's inside   of this array so you'll see if I come in here  and I go into this guy I refresh you'll see in   a second when I run this it will print out 30 see  30 right there and that's because this guy here is   pulling this array value and because we appended  it that means that I could do number five and it   should just work right so if I hit refresh you'll  see it should just print five ah see here we go   we got an error see this is another common issue  you're going to run into index was outside of the   bounds of the array so what does that mean that  means that index five doesn't actually work so   what's up with that right we have an append right  here well that's because unfortunately I kind of   lied to you right you can't really append to an  array like that and that's where lists come in   so if I come in here I'm going to get rid of this  little append here and I'm going to come in here   and I'm going to go list int int list is equal to  a new list int like that so let's talk about what   a list is a list is a flexible array effectively  it's an extended array that allows you to do a   lot of really cool stuff so if you want to add  stuff to it you can type int list do add like   that you could duplicate this and say add five  six and8 and then you could come in here and go   int list bracket bracket zero like that and you'll  see that's going to print that first element the   first one in that int list so if I come into gdau  here and I refresh you'll see hopefully it's going   to print out one like that and the reason why  it prints out one is because that's that first   element in that array if I set this to two you'll  see that it's going to print out the third element   in our array which is going to be a six and you  can see here it is six so that's the power of list   you can also remove stuff from lists int list.  remove and then you can pass in the item that   you want to remove so in this case let's say six  and then if we print out let's see if I'm removing   this one this is one 0 1 2 is going to be six  I'm removing six right so this should print eight now there we go it's printing eight because  lists are mutable they can be changed they   could be adjusted they could be altered and that's  the beauty of lists arrays they have to maintain   that size they cannot be flexible in their size  and you'll also notice that um the only thing you   could do is change them so if I type int array  bracket bracket 2 is equal to 17 or 18 in this   case then that means that this array element here  is going to to become 18 and lists have the exact   same thing so if I go into list bracket bracket 2  is equal to 17 like that you'll see that if I come   in here and I refresh you'll see that if I click  this it's going to say 17 and that's because this   got changed to 17 if that makes sense so that's  the beauty of lists and arrays they're basically   collections of data that you can you that could be  used to iterate on now the nice thing is is arrays   and lists can be of any type so if I for instance  want to come in here I can get rid of this I   could do a list of person like this and then I can  create a new person so I can say person p is equal   to a new person right and this person's name is  quote John like that and then I can create I can   add it to my list so I can say int list. add p and  that's going to add that person to that list and   I could do the same thing and add another person  person person is equal to a new person like that   and I could say person. name is equal to Madison  like that and then I can come in here and go GD   do print int list bracket bracket one like that  and then I can come in here and I can hit go and   click press me and you'll see that we got an error  system out of range index was out of range must   be non- negative or less than size of collection  so if we take a look we did not add that second   person so we can go int list. add person like  that we need to add it to that list and then we   can go ahead and actually do it so if we come in  here hit press me you'll see it says person which   my guess is because I didn't do yep so you'll  see a said person because C only tells you the   type of the object if it's an object so what we  did was we pulled that element out of the list   and printed it but if we want to access the name  we can hit dot name like that and then we can hit refresh and there you go Madison and that's  basically how lists and arrays moreal less work   if we want to remove it from a list we can say int  list. remove and then we can remove an element and   we'll remove P because we have our person right  here so we'll just remove them so you'll see that   if if I change this to zero because that's what  this guy is he's index Zer if I remove JN from   the equation and it's just Madison if I hit  press me you will see it says Madison because   the collection is mutable it can be changed so  therefore when we removed John Madison moved up to   the first person in that array and then that list  and then it printed her out if that makes sense so   now what happens if we want to both people's name  well if we come in here we can copy this right and   change this to one and then get rid of this remove  and that would print both these people's names right John Madison right but what happens  if we have 500 elements in this array and   we want to print all of them that's when it  gets hard right you don't want to duplicate   this 500 times well that's where four  Loops come in so inside a c we have   this thing called a four Loop so you can say  four in I comma semicolon I is less than int   list. count so the size of that list i++ and  we'll need to make sure that we do I is equal   to zero because we need to tell it what it  is like what value it's going to start at   and then then we can say for this bracket  bracket g. print int list bracket bracket iame like that and then we can get rid of these  guys like that and then we can come into C we can   refresh this and you'll see that if I hit play or  I hit press me it'll say John and Madison and the   reason why is because it's going in iterating  through that list and it's saying let's grabb   the next element let's fire it off let's grab  the next element let's fire it off and that's   what a four Loop is for and the nice thing is  that you could start this at any index and you   can iterate it based off of anything so I could  say I is less than one then do this right so now   you'll see it's going to run once because it is  going to be zero and then when it hits one it   will be it'll be equal to one so it'll actually  just run the one time if that makes sense now   what if I want a little bit more of a friendly  way to handle this right instead of that weird   way what we could do is called a 4 each Loop a 4  each is actually much simpler to use you can say   for each person P well I guess I can't use P cuz  I have this new person here so we'll use her in   int list bracket bracket you you'll see that how  this works is what it does it pulls this element   out of this list assigns it to this object here  and then allows us to use that so GD do print per   like that and you'll see if I refresh this well  it's running person twice and the reason why is   because I didn't do per. name like that so we'll  do that and we'll refresh this and you'll see that   it runs John and Madison and that's the beauty of  this system so in order to create kind of like a   really cool system what if instead of doing this  right we just had an array of strings and then   we use that to create a list and then pass that  list this for each well that's absolutely doable   so let's go ahead and get rid of this string  bracket bracket string array is equal to a new   string bracket bracket let's say five names and  and then let's go bracket quote John comma quote   Madison comma quote Mitch comma Victoria comma  and then we get one more so let's go with Alicia   like that then we could come in here and say for  each VAR item in string array person person p is   equal to new person bracket bracket semicolon  p. name is equal to item because remember every   element in this array will become this string  arrays item right and we could come and so we're   basically creating a new person we're setting  their name associated with that item which I   really should just go with name like that just  because it's going to be easier for you guys to   read and then we can come in here and say int  list. add P like that and then you'll see that   after it's done it's going to go through Loop  through it and print it so you'll see refresh   press me John Madison Mitch Victoria and Alicia  and that's the power of lists and for each loops   and those kinds of things it's a way for you to  have a collection of things that you can iterate   through and work with now I think at this point  you guys have a pretty good understanding of C   so I'm going to leave this tutorial here and in  the next one we're going to go ahead and take   all of this and build a budgeting app based off  of these basic concepts it's going to be a long   tutorial but it's going to be a lot of fun so if  you like this video go hit that like button hey   you know if you dislike this video go and hit  that dislike button because I am here to make   content for you guys this video was a viewer  suggested like all of my videos so if you have   any suggestions please leave them in the comments  below or on my GitHub link is in the description   and submit a ticket to me under fineo suggestions  that's where I get all of my suggestions that's   where I find all the future tutorial ideas and  hey if you have any questions or comments about   this please leave them in the comments below or  jump on my Discord link is in the description   I'll be more than happy to help you out with any  issues you might be having but that is all I have   for you guys today so thank you so much again  for watching and I will see you all next time [Music] thanks
Info
Channel: FinePointCGI
Views: 1,458
Rating: undefined out of 5
Keywords: basics of c, basic structure of c program, c programming basics, basics of c programming, language basics, c language basics, basic ideas of c programming, c basics, basic structure of c program-in hindi, basic, function basics, basics in telugu, c c programming basics, basic techno, c language basics in telugu, prerequisites of data structure, c programming basics in hindi, basic c programming, c basic introduction, structure of c, basic program structure
Id: vsMv6dxkduo
Channel Id: undefined
Length: 60min 55sec (3655 seconds)
Published: Mon Mar 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.