Learn C# Programming – Full Course with Mini-Projects — Transcript
Full transcript
- 0:00this comprehensive C course will take
- 0:02you from the basics of C programming to
- 0:04advanced concepts all while integrating
- 0:07exciting mini projects to solidify your
- 0:09learning this course offers a structured
- 0:12path through essential topics such as
- 0:14syntax object oriented programming data
- 0:16structures and more ABA teaches this
- 0:19course and he will help you understand C
- 0:21both theoretically and also gain
- 0:24practical experience in applying your
- 0:25knowledge to real world scenarios hey
- 0:28there free ABA here join me in Walking
- 0:31you through everything you need to get a
- 0:32setup with cop let's have a quick walk
- 0:34through the course contents to let you
- 0:35know what to install for you we'll start
- 0:37with installing the visual studio
- 0:39program required to develop C
- 0:40applications you'll learn about data
- 0:42types including integers strings and
- 0:44many more and how to manipulate them
- 0:46inside your code next we'll dive into
- 0:48control flow you'll discover how to use
- 0:50conditional statements and Loops to
- 0:51control the flow of your program's
- 0:53execution strings are essential in many
- 0:55applications you'll Master powerful
- 0:57string functions in C allowing you to
- 0:59manipulate and transform Text
- 1:01effortlessly data structures are crucial
- 1:03for organizing and managing data from
- 1:05arrays to collections you'll explore
- 1:07various data structures in C and learn
- 1:09how to use them effectively functions
- 1:11are the backbone of any program you'll
- 1:13uncover the power of functions in C
- 1:15including how to create and call them to
- 1:17organize your code no program is immune
- 1:19to errors you'll learn how to handle
- 1:21exceptions gracefully in C ensuring that
- 1:23your applications remain robust and
- 1:25reliable debugging is a crucial skill
- 1:27for any developer you'll discover how to
- 1:29use visual studios's powerful debugging
- 1:31tools to identify and fix bugs in your C
- 1:34code efficiently finally we'll delve
- 1:36into object-oriented programming you'll
- 1:38learn the principles of object-oriented
- 1:40programming how to use classes objects
- 1:42inheritance and polymorphism to create
- 1:44modular and maintainable code by the end
- 1:46of this course you'll have a solid
- 1:48understanding of C programming and be
- 1:50well equipped to tackle a wide range of
- 1:52software development challenges thanks
- 1:54for tuning in let's get
- 1:56started so in order to start developing
- 1:58with C we need to install the latest
- 2:00version of visual studio in this case
- 2:02it's currently 2022 so let's head over
- 2:04Google and type in Visual Studio 2022
- 2:07download we will be presented with two
- 2:09links we have this one and this one we
- 2:11want to use a second one which says
- 2:12download visual studio tools so if you
- 2:14press on that and now will present it
- 2:16with the download area we have three
- 2:18additions Community professional
- 2:20Enterprise Community is free for
- 2:22students open source contributors and
- 2:24individuals which is exactly what we
- 2:25need for the content of this course we
- 2:27have the professional Enterprise Edition
- 2:29which paed for and only used by bigger
- 2:31companies or smaller teams so let's go
- 2:33ahead and press free download the
- 2:35download will start straight
- 2:37away after the download is complete
- 2:39let's start the installer so double
- 2:41click on the executable and press yes to
- 2:43allow it for admin permissions it'll pop
- 2:45up like this Visual Studio installer
- 2:47before you get started we need to set up
- 2:48a few things you can configure your
- 2:50installation you can read the privacy
- 2:52policy in the software terms if you'd
- 2:53like or you can just press continue so
- 2:55it's getting the visual studio installed
- 2:58already so so when you get to this
- 3:00section everything here is all the
- 3:01different packages and tools you can
- 3:03install within Visual Studio 2022 the
- 3:06main one that we're concerned within the
- 3:07content to this CA is the neck desktop
- 3:09environment so you can go ahead and
- 3:11press this one it will then tell you in
- 3:13the bottom right hand corner the total
- 3:15space required is 6.52 GB please make
- 3:18sure you have enough storage space
- 3:20available for this and then you can go
- 3:21ahead and press install if you want to
- 3:23check out other packages you can have a
- 3:24scroll through and read any other
- 3:26packages you may need and may want to
- 3:28install like I said the the only one
- 3:30required for this course is the net
- 3:31desktop
- 3:32environment you can also deal with noj
- 3:34development python development web
- 3:36development if you need to deal with
- 3:38individual components like specific
- 3:40versions you can access it here if you
- 3:42need some more languages installed you
- 3:44can access it here this should
- 3:45automatically select your default
- 3:47language for you that's based on your
- 3:48systems language and if you want it to
- 3:51you can also change the installation
- 3:52location in case you want to install it
- 3:54on a different drive or you just want it
- 3:56in a different folder so now that we
- 3:58selected the neck desktop environment
- 4:00we can go ahead and press the
- 4:01installation button we do have two
- 4:03options down here we have it install
- 4:05while downloading and a download all
- 4:06then install if you have a reasonably
- 4:09fast computer you can stick with the
- 4:10install while downloading and it will do
- 4:12the installation and the downloading at
- 4:14the same time if you have a slow
- 4:16computer it's preferred that you use
- 4:17this option to download it first and
- 4:19then install it just means that it won't
- 4:21slow down your computer too much so you
- 4:23can go for this one and press install
- 4:26now the installation will begin so now
- 4:28that Visual Studio is finished finish
- 4:29installing it will say done installing
- 4:31Visual Studio has been successfully
- 4:33installed we recommend rebooting soon to
- 4:35clean up any remaining files so this
- 4:37just means restart on your computer just
- 4:38so it has a chance to clean up any files
- 4:40that were kept during the installation
- 4:41process so we can just press okay if you
- 4:45have any other the versions of Visual
- 4:46Studio installed they'll also show up
- 4:48down here and now that Visual Studio
- 4:50Community 2022 is installed we can just
- 4:52press
- 4:55launch so now it'll prompt you to sign
- 4:57into Visual Studio we should definitely
- 4:59sign in but just for now I'm going to
- 5:00skip but feel free to create an account
- 5:02or sign in if you already have one
- 5:04existing we can skip this and then we
- 5:06get to choose your theme I personally
- 5:08prefer the Dark theme but you can go
- 5:09with blue blue extra contrast or the
- 5:11light mode so let's select dark and hit
- 5:13start Visual Studio it will do a first
- 5:16time
- 5:17setup and it's already done and we're
- 5:19already inside Visual Studio 2022 so
- 5:22that's it for the visual studio
- 5:25installation so now that we got Visual
- 5:27Studio 2022 installed let's see how to
- 5:30create your first project so once you've
- 5:31opened Visual Studio 2022 you'll see
- 5:34this window popping up you can clone a
- 5:36repository from things like GitHub in as
- 5:37you devops or open a project that you
- 5:39already have or open a folder which may
- 5:42have projects inside or you can create
- 5:43one from scratch if you don't want to do
- 5:46any of these options you can just open
- 5:47the visual studio program just on its
- 5:49own by clicking continue without code so
- 5:51let's go down the create a project path
- 5:53so click on create a new project and
- 5:55we'll get a list of all these different
- 5:56projects inside our system to be able to
- 5:58use the ones we're going to be be
- 5:59focusing on on this course is going to
- 6:01be C in the languages dropdown box and
- 6:04the platform is going to be windows and
- 6:06the project type is going to be console
- 6:08and they have one that uses net core
- 6:10which is for Linux Mac and windows and
- 6:12one that uses the Net Framework which is
- 6:14Windows so we want to go for this one so
- 6:16press on this one and then press
- 6:18next here we have our project name our
- 6:21location and our console app so we can
- 6:24start with the project name and say my
- 6:26first
- 6:27project as you can see it'll start
- 6:29copying over to the solution name as
- 6:31well and it tells you where the project
- 6:32will be created into so I'll be creating
- 6:34a folder called my first project which
- 6:37is the solution name plus my first
- 6:39project which is the project name inside
- 6:40that folder if we change the solution
- 6:42name you'll see update you can see here
- 6:45now we have multiple T's because we
- 6:46appended the solution name the solution
- 6:49name is almost like a folder that holds
- 6:51lots of projects inside so to begin with
- 6:53we'll have one solution that has one
- 6:55project inside and then later on in the
- 6:57course if you feel like you want to add
- 6:59more projects to it you can just add
- 7:00more to this solution so let's get
- 7:02started and press
- 7:08create so now we've created our very
- 7:10first project and just to explain what's
- 7:12here we have a static main void which is
- 7:15the main entry point to our program if
- 7:17you do a lowercase M then the system is
- 7:19not going to recognize this main
- 7:21function because the down NET Framework
- 7:23requires this function to be found in
- 7:24order to run a c console app and you can
- 7:27see here in the errors the program
- 7:29program does not contain a static main
- 7:31function and you can see it's capital M
- 7:33and C programming language is K
- 7:35sensitive so make sure to include it so
- 7:37now you understand how to use Visual
- 7:38Studio 2022 to create a project and also
- 7:41know where the main entry point is in C
- 7:43let's get started with a Hello World
- 7:45example to kick off this
- 7:48course so as we know our main entry
- 7:50point to our application is the static
- 7:52void main if we go inside here and press
- 7:54enter then we can actually start writing
- 7:56some code inside here just a quick note
- 7:59before we
- 8:00continue static void main is inside the
- 8:03class program and the class program is
- 8:06inside the namespace hello world and the
- 8:08reason why you can tell this is because
- 8:10of the curly braces the curly braces of
- 8:12hello world begins here and ends here
- 8:15and Within These curly braces we have a
- 8:17class program and also within the class
- 8:19program we have the styo
- 8:22men so just between these curly braces
- 8:25we want to type console. right line or
- 8:29open the brackets and we can type in
- 8:31some speech marks and we can simply Type
- 8:33Hello World there you go so as you can
- 8:36see now system has actually been
- 8:39highlighted so if we just delete this
- 8:42line you can see now system has been gr
- 8:45out and the reason why is it says it's
- 8:47unnecessary because it hasn't been
- 8:50used so if you undo it then this comes
- 8:55up because I showed you before that
- 8:57console right line is actually within
- 8:59the system Library what happens if we
- 9:01delete
- 9:02it if we delete it then it doesn't know
- 9:05where console exists from and because
- 9:07that just said system we can actually
- 9:09just use system. console. right line and
- 9:12then open the brackets and type in hello
- 9:14world and now this works again and the
- 9:17reason why this works it's because we
- 9:19don't have to Define that we're using
- 9:21system we could just use it straight in
- 9:24here if you have multiple lines like
- 9:27this then you don't want to be using
- 9:29system right at the start ideally you
- 9:31want to have the reference to it at the
- 9:32top of the screen and then use it
- 9:35throughout so now that we've included
- 9:37back in the project you can see that now
- 9:39these have turned gray and it says that
- 9:41it can be simplified if we press the
- 9:42little light bulb and we can say
- 9:44simplify member and it doesn't really
- 9:47matter because we can just delete the
- 9:48rest now so now that we've WR hello
- 9:51world let's press F5 which is a shortcut
- 9:53to the start
- 9:55button as you can see well you couldn't
- 9:58really see because the program closed
- 9:59too quickly and the reason for that is
- 10:02printed hello world to the console and
- 10:04then it didn't do anything else cuz
- 10:05there's nothing else for it to do so the
- 10:07console simply
- 10:08closed we can use another command called
- 10:12console read Line open the brackets and
- 10:14close the brackets and have a semicolon
- 10:16at the end all this does is it waits
- 10:18here until the user inputs something
- 10:21reads the next line of characters from
- 10:22the standard input
- 10:25stream so although the program will
- 10:27actually be reading stuff we just want
- 10:29this line to stop the application from
- 10:31closing so now if you press F5 and run
- 10:33the program you can see it actually says
- 10:35hello world and it's just waiting here
- 10:38flashing and this flashing means it just
- 10:41started this console read line so it
- 10:42doesn't matter what we type into here it
- 10:44just WS for us to press enter and it
- 10:46soon as we press enter this line
- 10:48executes and the program finishes
- 10:50congratulations you just wrote your
- 10:52first C program welcome to the amazing
- 10:55journey of
- 10:56programming so let's look at how to make
- 10:58VAR Ables in our C code let's start
- 11:01every code with a console read line and
- 11:04then we can hit F5 just to make sure our
- 11:06console doesn't
- 11:07close so let's begin with an integer so
- 11:11integers are whole numbers so we can
- 11:13just say int age equals
- 11:1523 so what we've actually done here is
- 11:18we've said we want an INT we want to
- 11:20call it age already made it equal to 23
- 11:25this is called initializing and
- 11:27declaring in the same line
- 11:30so what you could do alternatively is
- 11:31you can end the semicolon here and it
- 11:33just says int Edge and then on the next
- 11:36line you can write ede equals 23 so this
- 11:38is declaring it in the first line and
- 11:40then give it an initial value in the
- 11:42second line if they're right underneath
- 11:44each other like this case then you may
- 11:46as well just make it together because
- 11:47then you have less lines of code the
- 11:49reason why it says it's a green under
- 11:50line it's because it's the unnecessary
- 11:52assignment because we're not actually
- 11:54using ede so let's just point age to the
- 11:56console if we make some new lines and we
- 11:58can type in in console. right line and
- 12:00then press tab to finish it we can write
- 12:03Edge and if we run our code using F5 we
- 12:06can see that 23 gets print out to the
- 12:09screen so now that we've worked on the
- 12:11Edge Let's explore all data types let's
- 12:14define a long variable so let's start
- 12:16with the type long followed by space and
- 12:18then the variable name that we want and
- 12:20let's add equals and type in a very big
- 12:23number so now if we hover over it it
- 12:25says it's a system int32 and int32 is
- 12:29actually an integer but we're telling it
- 12:31it's a long which makes no sense we have
- 12:34an in 64 here but when we hover over the
- 12:36number it comes up as n32 and the reason
- 12:40for that is when C sees that you just
- 12:42have a number typed in like this you
- 12:44will always assume that it is an integer
- 12:47to tell the compiler that you indeed
- 12:49want this to be processed as a long
- 12:50number then you can put a capital l
- 12:52right at the end now if we hover over it
- 12:54it says in
- 12:5664 and this says in 64 so now we've got
- 12:59it working and we've got the long there
- 13:01so now we can take another console right
- 13:03line and just print this out to the
- 13:04screen and just a helpful tip if you
- 13:06type in CW and press tab twice then you
- 13:09get the console right line it's very
- 13:11easy to write your code and then you can
- 13:13type in big followed by a tab and it'll
- 13:15come up straight away so as you can see
- 13:17they're both being printed to the
- 13:18console
- 13:19now so now that we've made positive
- 13:21numbers we can also make these negative
- 13:24by simply just putting a negative sign
- 13:26behind them there we go and just to to
- 13:29show you how big these numbers are what
- 13:31we can do we can type in int. max value
- 13:34and then make another one using contr D
- 13:36and type in int. Min
- 13:38value so you can see integers can be
- 13:41positive 2.1 billion or netive - 2.1
- 13:46billion and if we do the same for
- 13:52long you can see that long is a very
- 13:55very big number because instead of being
- 13:56an INT 32 it's an in 64 which which
- 13:59takes up a lot more memory so now we've
- 14:02covered whole numbers let's have a look
- 14:04at decimals so we can have a double
- 14:07negative for example and we can say it's
- 14:09-
- 14:1055.2 and in this case when you just have
- 14:13a number typed in like this and it's got
- 14:15a decimal point in it it will always
- 14:17register it as a double but just to make
- 14:20sure just as we're doing with the long
- 14:22you can actually just put a capital D at
- 14:24the
- 14:25end now if we print this out to the
- 14:27screen and also print out how big these
- 14:30numbers can
- 14:31be max value and Min value and print
- 14:36this out so we get our minus 55.2 and
- 14:39you can see that these numbers can be
- 14:40very big e+ 308 means it's this number
- 14:45times by 10 the^ of 308 which is a very
- 14:48big number and we also have that as a
- 14:51negative so we have float let's call
- 14:53this precision and make equal
- 14:575.1 and then put a semic call the end
- 15:00now we've got a problem here this is
- 15:01trying to register as a double but we
- 15:03don't want to register a double we want
- 15:05to register as a float and this is the
- 15:06same issue we have the long so when you
- 15:09type any number that has a decimal point
- 15:11in it the C compiler will always
- 15:13register as a double the same way when
- 15:15you type in a whole number it'll always
- 15:17register as an INT so in this case we
- 15:19need to put a capital f at the end and
- 15:22it'll process this as you
- 15:24float so now we can print this
- 15:27out and then let's just print out the
- 15:29max values and the Min values just to
- 15:33show you what that looks
- 15:34like so as you can see here we have our
- 15:36number printed out and again we have
- 15:38quite a big number 3.4 * by 10 to the^
- 15:42of 38 so again it's a big number and we
- 15:44have that positive and negative so quite
- 15:46a big range of
- 15:48numbers and the last one we're going to
- 15:50cover is decimal for things like money
- 15:53and currency
- 15:5514.99 and again in this case it's trying
- 15:58to assign a double into a decimal so in
- 16:01order to denote what a decimal is you
- 16:03can put a capital M right at the
- 16:05end and for one last time let's print
- 16:08out the
- 16:09money and the decimal. max value contrl
- 16:13D and then we'll do Min
- 16:17value and you can see here that this is
- 16:19a really big number as well and it's
- 16:21even bigger than the
- 16:23long so we have our number printed and
- 16:25the Max and the minimum values so now
- 16:27that we've understood that how to
- 16:29declare a variable I have an extra
- 16:31little tip for you so if we had three
- 16:33variables int X an INT Y and an INT
- 16:37Z and let's say you're always going to
- 16:39declare them together like this you can
- 16:41actually miss out and do X comma y comma
- 16:44Z and that will do exactly the same
- 16:47thing there you go and that's really
- 16:50neat and it's a lot better let's say for
- 16:52example you had these variables but you
- 16:55actually had them set to something like
- 16:56equals 10 equals 20 equal 30 this is
- 17:01still practical but it might look a
- 17:03little
- 17:04uglier so you can actually type them in
- 17:06straight into here and if you comment
- 17:09these out again then it still works you
- 17:12can even put these onto new lines like
- 17:13this just so it looks a bit neater just
- 17:16prevents you from typing the additional
- 17:18int each time if they're all going to be
- 17:20the same variable type so that's it for
- 17:22the numbers inside our code so let's
- 17:24just have a quick recap so in order to
- 17:27Define a variable you want to use the
- 17:29data type in this case an integer
- 17:31followed by the variable name and then
- 17:33you can either add a semicolon at that
- 17:35point or you can add equals to give it a
- 17:37value this is just called declaring a
- 17:40value and this is called initializing a
- 17:42value if you want to decide you want to
- 17:44change the variable further down your
- 17:47program for example age equals 50 here
- 17:50then this is fine and this is now called
- 17:52an
- 17:53assignment it's called initialization
- 17:56because as soon as you make it you give
- 17:58it a value and then in later on in the
- 18:00code if you give it another value this
- 18:02is called simply assigning a value this
- 18:05is just called assigning it a
- 18:08value so now that we can store numbers
- 18:10in C let's see how we can store
- 18:13characters and letters so as you know
- 18:15from the introduction we have a string
- 18:16variable so let's call that name and
- 18:19make it equal to
- 18:20Aba please note that when you're
- 18:23defining a string and giving it a value
- 18:25that this value needs to be in speech
- 18:28marks if you put it in quotation marks
- 18:30it will not accept this because that's
- 18:32the notation to declare a Char which
- 18:35we'll get on to next so on the next line
- 18:37we can do a Char letter equals and we
- 18:40can't have something like this because
- 18:42the Char will only accept One Singular
- 18:44character so we have to wrap it around
- 18:46the quotation mark once you've wrapped
- 18:49it around the quotation mark then it
- 18:50will accept it so let's print these two
- 18:52to the screen so CW tab tab and we can
- 18:56just print these to the screen and if we
- 18:58just run the code you can see that we've
- 19:00got the values entered in
- 19:03here we can start to write
- 19:05stuff your name
- 19:08is and then we can output this and we
- 19:11can also put it on one
- 19:13line if we do a console right and a
- 19:16console right then maybe we can take the
- 19:18callon off and just have it space and we
- 19:20can say your name is ABBA and we' see
- 19:22that additional a because that's the
- 19:24right line for the letter so we can
- 19:26space it out with an empty console right
- 19:28line and and they'll add a gap between
- 19:29them both there we
- 19:32go so now we know how to declare a
- 19:34string variable and give it a value and
- 19:36we can declare a Char variable and also
- 19:38give it a value just a quick recap when
- 19:41you're declaring and initializing string
- 19:43variables you need to put the value in
- 19:45speech marks but when you're declaring
- 19:47and initializing a Char you don't have
- 19:49to put it in speech marks you put them
- 19:50in apostrophes whenever you see
- 19:52apostrophes that that can only ever
- 19:53refer to One Singular character when you
- 19:55see a speech mark it could be one or
- 19:57more characters
- 19:59also things can be empty this just means
- 20:02the string is empty this is also a valid
- 20:04character it just means that we're
- 20:05storing a string but it has no value
- 20:08with a Char that's not exactly the same
- 20:10you can't store an empty character
- 20:11literal it has to be given something
- 20:14Charles get defaulted at back sl0 which
- 20:17is just a default character but don't
- 20:19worry about that too much now we'll go
- 20:20into Escape characters which is the back
- 20:22SL layer on in the course for now just
- 20:24note that whenever you declare a string
- 20:26variable we need to put it in the speech
- 20:27marks and when we declaring a Char
- 20:29variable we need the quotation
- 20:32mark So now that we know how to define
- 20:34numbers in C and to also Define strings
- 20:37and characters then let's look at how to
- 20:39convert between them both so let's take
- 20:41some similar code from our first video
- 20:43and we can Define it age the number the
- 20:45negative the Precision and the money and
- 20:47we give it the same values from the
- 20:49first video we simply just print them
- 20:51out to the console here and if we just
- 20:53run we can see them all but this doesn't
- 20:55help us cuz we're just statically typing
- 20:57in the numbers what if these these will
- 20:58actually process a string and we want to
- 21:00convert them into an INT so just above
- 21:03each of the variables we're going to
- 21:04make another variable so we can say a
- 21:06string text age equals
- 21:08-23 and then for the age what we're
- 21:11going to use is this function called
- 21:12convert to int 32 and we can put in the
- 21:16string inside the brackets and what this
- 21:18is now saying is we're taking our string
- 21:21text Edge which is equal to -23 and
- 21:24we're passing it into this function
- 21:25convert to in32 and the reason why it's
- 21:28in 32 is because an INT is by definition
- 21:31an in32 which is a 32bit signed integer
- 21:35so what we want to do here is convert
- 21:37for our text age into int32 and store it
- 21:41back into our int and hopefully we
- 21:43should see the same value when we hit
- 21:46run 23 so now that's actually went from
- 21:49a string variable and then went into our
- 21:52integer and we've printed out as an
- 21:54integer and it's no longer a string
- 21:56variable now we can do the same for the
- 21:58other ones so we can have a string text
- 22:01big number and make it equal to this
- 22:04value just in the quotation
- 22:07marks now we're typing this value in the
- 22:09quotation marks we don't need to put the
- 22:11L at the end the L is just to tell the
- 22:13compiler that we want to use in 64 if we
- 22:15take the L away then it's going to think
- 22:17it wants in 32 this is not re applicable
- 22:20when it comes to text because we're
- 22:21going to explicitly stay on the next
- 22:23line we want to convert to N64 so the
- 22:26compiler already knows that it's in 64
- 22:28for and we don't need to put an L at the
- 22:30end now let's make sure this works and
- 22:32there we go so what we should end up
- 22:34with is the console should look exactly
- 22:36the same as how we had it at the start
- 22:38of the video but actually what we're
- 22:39doing is converting it from a string
- 22:41into the applicable value string text
- 22:46negative equal -
- 22:4955.2 and then in here we can do convert
- 22:53to double add the text negative inside
- 22:56here and let's just run it to check
- 22:59and we still have the same value so it's
- 23:01going well have a string text
- 23:07Precision make it equal to
- 23:10this and then instead of this we can
- 23:13have a convert two and in this case it's
- 23:16actually going to be a single and the
- 23:18reason why it's a single when you hover
- 23:20of a float the definition for a float is
- 23:22indeed a single so we can add that in
- 23:25text precision and we're just going to
- 23:27do the last one and just check it out Al
- 23:28together text money equals
- 23:3314.99 and then instead of this we can
- 23:35have a
- 23:36convert and let's have a look at this a
- 23:38decimal is indeed a decimal so we can
- 23:40say two decimal and then add the text
- 23:43money
- 23:43in perfect now if you run the line of
- 23:46code we can see that all of these values
- 23:48are exactly as we had them now they're
- 23:50being represented by their official
- 23:52descriptions Based On A String
- 23:54conversion as I mentioned before in the
- 23:56error video the problem you'll have with
- 23:58this is if this value actually has a
- 24:00character inside you will get a runtime
- 24:03error this can be fixed in the future
- 24:05when we look at how to resolve these
- 24:07errors and we can have exception
- 24:09handling and use functions like try pass
- 24:12so we can try and avoid these exceptions
- 24:14occurring because when these exceptions
- 24:16occur the rest of our code does not run
- 24:18and it becomes fatal at this
- 24:20point but for now don't worry about this
- 24:22too much just get used to the notation
- 24:25and how these things
- 24:27work so this is video we're going to
- 24:29focus on the Boolean data type so we can
- 24:31make a Boolean data type by saying Bo
- 24:34value equals
- 24:36true this Boolean value is literally
- 24:38just a true or false value we can store
- 24:41anything in here and maybe something
- 24:42like bull is M and we can just say it
- 24:45true and later on our code boings are
- 24:47used to make decisions we'll cover this
- 24:50in the next section of this course but
- 24:52boings are mainly used for making
- 24:54decisions if one thing is equal to
- 24:56another thing then we can take another
- 24:58path or if something is true or
- 25:00something is false then we can do
- 25:01another path for example if you make an
- 25:03output to the user you could check if
- 25:05this value is true and if the value is
- 25:07true then you can address the user as a
- 25:09male and if the value is false then you
- 25:11can address the user as a female these
- 25:13are the kind of things that booleans can
- 25:15help us with and just like any variable
- 25:17if you want to assign it later on you
- 25:18can just assign it again and if we print
- 25:21these out to the
- 25:24screen just like that you'll just see
- 25:26the value is true and the value is false
- 25:28appear on the screen as of right now in
- 25:30the course there's not a whole lot to do
- 25:32with boing just understand that they can
- 25:33hold a true or false
- 25:36value so now that we know how to define
- 25:38different variables and how to convert
- 25:40between them let's see how we can
- 25:42perform operations on different data
- 25:44types let's say we could have an INT age
- 25:47equals 23 and then what we can do to
- 25:49that age is we can say Age ++ and what
- 25:53this age will do is it will actually
- 25:54just increment age by one and if we do
- 25:57age minus minus it will decrement it by
- 25:59one so if we just print it out after and
- 26:01after this as well what we'll notice is
- 26:03there'll be no change because it'll go
- 26:05forward one and then back one there we
- 26:07go went from 23 plus plus to 24 and then
- 26:10minus minus to
- 26:1223 that's one of the important operators
- 26:15that you'll see a lot especially during
- 26:17the next section of this course so
- 26:19another way to be able to add one to a
- 26:21variable or anything to a variable we
- 26:23can replace this with age equals age + 1
- 26:28and what we're seeing here we want age
- 26:30to have the value of itself added to one
- 26:34which will give us the same result as
- 26:35doing h++ another way of writing this is
- 26:38a quick shorthand is you can say AG plus
- 26:41equals 1 these three things are doing
- 26:44exactly the same thing when you do an
- 26:47age Plus+ you can only increment by one
- 26:49and only one these two options give you
- 26:52the flexibility of having something like
- 26:5410 these two are doing exactly the same
- 26:58a AG equal AG + 10 and this is going to
- 27:00read exactly the same thing it's is when
- 27:02you use plus equals it will take the
- 27:04value of the current variable and add it
- 27:06to 10 which exactly what this line does
- 27:09to make it a lot shorter you can use age
- 27:11plus equals and then just put the value
- 27:13at the end just in case you forget to
- 27:15mention age twice so now if we just get
- 27:17rid of these two lines of code and we
- 27:19can just have a look and see if this
- 27:20runs so now the value should be 33
- 27:24perfect and now as we said we're going
- 27:26to cover plusus time and divide so just
- 27:30as you did with plus equals you can
- 27:31actually do a minus equals as well which
- 27:34will give us 13 and you can do a times
- 27:36equals which should give us
- 27:39230 now if we try to do divide equals
- 27:42we're going to run into an
- 27:43issue it says
- 27:462 23 / 10 is
- 27:502.3 and when you take 2.3 and make it
- 27:53into an integer it's just going to
- 27:55truncate the3 and leave you with two
- 27:58with integers if you add two integers or
- 28:00take away two integers or times them
- 28:02you'll always end up with an integer if
- 28:05you divide two integers you don't know
- 28:07what you'll end with for example if you
- 28:09make this into 20 then you'll always end
- 28:11up with two but that's not always the
- 28:13case as you just saw 23 / 10 is 2.3 so
- 28:17if you're ever going to do any division
- 28:19it's best to actually make this a double
- 28:22cuz now that we do the division we
- 28:23actually get 2.3 which is technically
- 28:25the right value so you can add minus
- 28:28times and divide any variables that you
- 28:30want these are just demonstrated in int
- 28:32and double but you can do this the Flor
- 28:34the decimal Etc so let's have a look and
- 28:36see what the plus and the minus
- 28:37operators do when you've got a string
- 28:39variable so we can have a string name
- 28:41equals
- 28:43abber and then we can say the name plus
- 28:48equals is
- 28:51programming and then we can just print
- 28:53out name to the screen and see what
- 28:54happens there you go you've actually
- 28:56just joined two strings together they're
- 28:58completely separate from each other and
- 28:59we've made them string
- 29:03together so if we try to do a minus
- 29:05equals you can't really remove from a
- 29:07string because a string is just text
- 29:09it'll be very difficult for the compiler
- 29:11and for the C language to be able to
- 29:13determine what's currently in the string
- 29:15and whether you can remove it or not
- 29:17this is not something that's built into
- 29:18the language this is not something
- 29:20that's built into the language so let's
- 29:22remove that and we can have the same
- 29:24thing with the
- 29:26CH now the problem with the ch
- 29:28is if we add a to B then let's see what
- 29:31happens to the
- 29:36output what it will try and do is
- 29:38actually get the uni code values for
- 29:40both of these items and try and add them
- 29:43together as integer
- 29:45variables so when you add a and b it
- 29:48will take the unic code value and you
- 29:49will actually get a capital A with a
- 29:51squiggle at the top and if we open
- 29:53character map you can see that this
- 29:54squiggle at the top is right here a
- 29:57which is unic cord 61 and 61 actually
- 30:00means 97 because it's 6 * by 16 + the 1
- 30:05and b means it's 6 * 16 which is 96 +
- 30:08the 2 so you have 97 and 98 and if you
- 30:12add them two values together you'll get
- 30:14the hex code which is C3 just for this
- 30:17character so what you're doing is you're
- 30:18kind of adding them both together and
- 30:20you're producing weird results so unless
- 30:23you really want to do this then you
- 30:24should be worryed about adding two
- 30:26characters together one more thing to
- 30:28note about when you're doing Plus+ let's
- 30:31bring another int variable and we just
- 30:33say int I equals z then we can do I ++
- 30:37and then if we print out I the value
- 30:39should be one which is fine what if we
- 30:42did
- 30:44i++ but we did Plus+ I so the value is
- 30:47still going to be one but what happens
- 30:49if we put this I ++ straight into these
- 30:52brackets let's try and run it and you'll
- 30:55still get the value of zero that's
- 30:57strange because we've said I ++ and what
- 31:00happens when you do Plus+ at the front
- 31:03it will actually execute this and return
- 31:05the value of I and then do the Plus+ so
- 31:08if you actually print the value of I
- 31:10after you'll realize that now it becomes
- 31:12one after if you ever had a situation
- 31:15where you need to increment first and
- 31:17then use the value you can use plus plus
- 31:20I now if you run it you should see the
- 31:22value is one twice the difference is
- 31:25that as the plus plus is before the
- 31:27variable name it tells the compiler
- 31:29please do this action first and then
- 31:31return back the I if you don't want that
- 31:33behavior then use I
- 31:37++ finding the remainder between the
- 31:39division of two integer variables can be
- 31:41very handy when you're determining
- 31:43whether a number is even or odd let's
- 31:45look at an example let's have it in
- 31:48first Nome design the value of 10 and in
- 31:51second Nome and give it the value of
- 31:52three now if we just printed the screen
- 31:55first num divided by second n what do
- 31:57you think you'll get so the value of
- 31:59this in the calculator will be 3.3
- 32:01reoccurring so we'd actually just get
- 32:03three because it's truncating it because
- 32:05of the
- 32:08integer so now what happens if we need
- 32:10the remainder between both of these you
- 32:12could actually work this out by seeing
- 32:14how many times three would be divided by
- 32:1610 as close as possible and you see the
- 32:19value is three and then 3 * by 3 is 9
- 32:23which will give you one left over for
- 32:24the 10 but that's a bit too long in our
- 32:27code so we have something called a
- 32:28modulus operator so we can actually
- 32:31change this divide for a percentage sign
- 32:34and what this will output is it will
- 32:35only give us the remainder so let's say
- 32:3810 / 3 is
- 32:423.3 as we've just said but the remainder
- 32:45is different remainder is exactly how
- 32:46you learned it at
- 32:48school 10 / 3 is actually 3 remainder
- 32:52one because 3 * 3 is 9 and then you have
- 32:56one left over to get 10 so that'll be 3
- 32:59* 3 which is equal to 9 and then one
- 33:02left over to get to 10 so now let's run
- 33:06our code and see if we do actually get
- 33:07one there we go so the best way to
- 33:11determine if a value is actually odd or
- 33:13even is to divide it by two so we can
- 33:17say 10 modulus 2 and if the value is
- 33:21zero then that means the value for first
- 33:23nor is actually even if the value is one
- 33:27then it's OD let's see how this works
- 33:29out if we had 10 modulus 2 then the
- 33:32value is zero because 10 can be
- 33:34divisible by two perfectly let's say we
- 33:37had 11 modulus 2 the closest multiple to
- 33:4111 is 10 which is 5 and then we have one
- 33:44left over so you just have the remainder
- 33:47is equal to one and if we just keep
- 33:50going up in the chain we'll realize that
- 33:52it's the same pattern every single time
- 33:54you go up a value the value just toggles
- 33:59so when we print this out to the console
- 34:01and the value is actually zero that
- 34:04means the number is even and if we make
- 34:06the number odd on purpose and then rerun
- 34:08the code you can see that it's a number
- 34:10one now which means this value is now
- 34:12odd let's look at a couple examples and
- 34:14see if you can figure out the answer if
- 34:17we do a th000 modulus 90 what do you
- 34:20think the value will be and let's write
- 34:22a couple more if we do 100 modulus 90
- 34:26and maybe something like 71 1 modulus 10
- 34:29so have a think about this in your head
- 34:31and see if you can figure out the
- 34:32answers pause the video and see if you
- 34:34are
- 34:35correct let's take a look at the
- 34:38answers so we can see we have 10 10 and
- 34:411 so the closest multiple to 1,000 from
- 34:4490 is
- 34:45990 so we have 10 left over to get to
- 34:491,000 and the same goes here the closest
- 34:51multiple of 100 to 90 is just indeed 90
- 34:55so we only have 10 to get from 90 to 100
- 34:58and in this case the closest multiple of
- 35:0071 to 10 is 70 and we have one to get up
- 35:04to 71 therefore these two answers are
- 35:06indeed
- 35:08correct so now that we know how to
- 35:10really Define a variable by specifying
- 35:12their type follow by the name and giving
- 35:14it a value let's see how we can do a
- 35:16very easy way of defining a variable so
- 35:19normally we would type in Edge what we
- 35:21can use this VAR keyword what this means
- 35:24is the compiler will figure out what
- 35:26type it needs to be based on the value
- 35:27we initialize it with so if we just make
- 35:30a variable called age then it's not
- 35:32going to like it because you need to
- 35:33give it a value this doesn't give the
- 35:35compiler enough information to figure
- 35:37out what this variable is going to be
- 35:39just because it says age this is just
- 35:41text to the compiler this does not mean
- 35:43anything so if you make it equal to 23
- 35:46then the compiler can see that this is
- 35:48indeed in 32 because it knows that so
- 35:51now if you hover over a VAR it says in
- 35:5432 now let's try and do this for the big
- 35:57number
- 35:59our big number equals
- 36:0290,000 and what will happen now is
- 36:05because it's Define as in32 it's going
- 36:07to say this is in32 as well and this is
- 36:10the same issue we had we'll move first
- 36:11defining the long variable in the first
- 36:13video we need to explicitly give it an L
- 36:16just so this knows that it's N64 and now
- 36:19the VAR has updated to an
- 36:21N64 instead of rewriting each one we can
- 36:24just change each of v and inspect what
- 36:26happens double
- 36:28double and now we have a float single
- 36:32single and finally the decimal we have a
- 36:35decimal and we have a decimal and the
- 36:37same thing goes for a string we can say
- 36:39VAR name equals ABA and we can have a
- 36:42VAR letter equals a there we go and
- 36:46they'll both register as there pickable
- 36:47data types as a string and as a Char and
- 36:52if we just print this out to the screen
- 36:53to make sure everything's working fine
- 36:56and run the code you can see that
- 36:57everything is exactly how we'd expect so
- 37:00just a quick recap you don't have to do
- 37:02this at the end of the day this will be
- 37:03exactly the same as writing this because
- 37:06when our code gets compiled down it
- 37:07doesn't matter what these things are cuz
- 37:09the compiler will deal with it however
- 37:11way it wants to this is just for us to
- 37:13read it in some cases if the value is
- 37:15very obvious like as we know an age
- 37:18cannot really be a point 23.1 years old
- 37:22no one says that in real life when you
- 37:23get asked your age so we can just assume
- 37:25that this value is going to be in an
- 37:27integer and that's fine because that's
- 37:29an easy number that's an easy example
- 37:32but if we say we want a negative or a
- 37:34big number then it doesn't really tell
- 37:36us what we're meant to be like this big
- 37:38number could be somewhere up to 2.1
- 37:40billion which is what an integer
- 37:42supports but in this case if it's going
- 37:44to be a big number and we specified the
- 37:46L then we're going to need a really big
- 37:48number bigger than 2.1 billion so in my
- 37:51honest opinion it' be very beneficial to
- 37:53only use VAR if it's very obvious what
- 37:56the variable is going to be and want
- 37:58it's being assigned to using VAR too
- 38:00much might hurt the readability of your
- 38:02code in the future because you have to
- 38:04inspect each variable individually to
- 38:06read what the type is going to be if
- 38:08you're scrolling through your code and
- 38:09you can see that this is along very
- 38:11quickly and you don't have to hover over
- 38:13it then it can improve your read time
- 38:15this is really important as readability
- 38:17of code will make more sense when it
- 38:18comes to Big projects when you have 20
- 38:2030 lines just like this project then it
- 38:22makes almost no difference cuz it only
- 38:24takes a couple of seconds when you have
- 38:26big functions big software when you work
- 38:29at a job then it's going to be very
- 38:31difficult to be able to find your way
- 38:32around and to understand what's going on
- 38:34especially if you're new to the code
- 38:37something very simple like this where
- 38:38you know you're defining a name it's
- 38:40very obvious and VAR will be accepted
- 38:43fine because you can tell that this is
- 38:44always going to be a string there's no
- 38:46other data type in C get specified by a
- 38:49speech mark whereas when you're defining
- 38:51numbers it can be very very hard to
- 38:53understand what different data types
- 38:55that people are using throughout the
- 38:57code especially when these numbers are
- 38:59not of statically typed in they might be
- 39:01inputs from the console from the user
- 39:03from a different function from anywhere
- 39:05so having good variable names along with
- 39:08their explicit types will be very handy
- 39:10to understand code in the
- 39:13future so now that you've understood how
- 39:15to make a variable let's look at a
- 39:17concept called constants so we defined a
- 39:19variable called vat and we set it to 20
- 39:22just to represent the 20% V in the UK
- 39:25now later on in the program we can
- 39:27actually change it to anything we want
- 39:29and that's because it's a variable
- 39:30variables can change in certain
- 39:32circumstances like vat you might not
- 39:34want to change it and you want to
- 39:35disable the ability to change it to the
- 39:37code if it's done accidentally let's say
- 39:39for example you're making a banking
- 39:41application and you wanted to fix the V
- 39:43and you don't want anyone else to change
- 39:45it you can add this keyword called const
- 39:48right before the Declaration and now
- 39:49when you go to reassign it you'll get an
- 39:51error message and it says at the left
- 39:53hand side of the assignment so anything
- 39:55before the equal sign has to be a very
- 39:57Able Property or indexer and in this
- 39:59case if you hover over vat it states
- 40:02that it's a local constant local because
- 40:04there's no access modifier before it and
- 40:06constant because we specified the con
- 40:08keyword so now if we try and run the
- 40:10code we'll actually get a compile error
- 40:12because the vat cannot be changed so if
- 40:15we remove that and then we just print it
- 40:17out to the console using CW tab tab and
- 40:19then we can put vat in the brackets and
- 40:21press F5 and now we can see our 20 being
- 40:24printed
- 40:27so let's try and calculate some values
- 40:29using this code if you have an INT
- 40:32balance make it equal to 1,000 and let's
- 40:35say we wanted to calculate the vat based
- 40:37off this then you'd have to do a little
- 40:39bit of a complicated calculation you
- 40:41have to take the balance and because the
- 40:42vat is a percentage we need to convert
- 40:44it from decimal to percentage so if we
- 40:47do times and then open the parentheses
- 40:49we can insert
- 40:51vat divided by 100 and just to make sure
- 40:55that this gets process as a double we
- 40:57want write 100 D cuz right now it's in
- 40:5932 and if we leave this as an integer
- 41:01division then we'll lose any decimal
- 41:03places and decimal places are very
- 41:05important when it comes to percentages
- 41:07so we put a 100 D at the end now you can
- 41:09see it says double so it'll actually
- 41:11calculate as if it was a decimal number
- 41:13so now if we print that to the screen so
- 41:15we can see here that 20% of the balance
- 41:18is indeed 200 instead of having to do
- 41:21this calculation each time what we can
- 41:23actually do is make another const this
- 41:26time making a double because we're not
- 41:27sure what the output of this division
- 41:29will be and we don't want to truncate it
- 41:31so we want it as a double and we can say
- 41:33percent vat and what we can do here when
- 41:36we assign the value just because it's a
- 41:38constant it means that we can't change
- 41:40the value but that also means we can
- 41:42read it as many times as we want so we
- 41:44can say the percentage V is actually the
- 41:47current V that we've got specified here
- 41:50divided by
- 41:52100d this will give us the vat from a
- 41:55number form into a decimal form form so
- 41:58now when we do this calculation if we
- 42:00duplicate this line using contrl D we
- 42:02can remove these entire brackets and
- 42:03literally replace it with percentage vat
- 42:06and you should see we get the same
- 42:07values
- 42:09perfect this can be very helpful in the
- 42:11future if you have things like version
- 42:13numbers in your code you can have
- 42:15version 1.0 and then if you ever specify
- 42:18this version later on in your code you
- 42:20can just change it once up and it will
- 42:22change the rest this can be very helpful
- 42:25you'll avoid a lot of typos and you
- 42:26won't have to to redeclare this value
- 42:28several times if you had three different
- 42:30occasions where you displaying the
- 42:32version number in your code then it's
- 42:33going to be problematic once you update
- 42:35the version number and you have to
- 42:36change every single occurrence and you
- 42:38might miss them so inserting it once and
- 42:40only having to change it once is the
- 42:42preferred option just to note with
- 42:44constants when you compile your code all
- 42:46the constants will actually do is do a
- 42:49find and replace so it will take this
- 42:50vat and find all references of v and
- 42:54just drop the 20 in
- 42:56there so this is more of a feature for
- 42:58maintaining your code from a user and
- 43:00programmer's perspective rather than
- 43:02anything to do with the compiler because
- 43:04when it compiles it just removes all the
- 43:06constant anywhere and then you're left
- 43:07over with the literal values that we've
- 43:09gave it nonetheless this is still
- 43:11something very handy to learn and to use
- 43:13throughout your
- 43:15code first you want to Define a variable
- 43:18to hold your name so we can just say
- 43:20string name equals abber perfect so
- 43:23that's the first one achieved says
- 43:25Define a variable to hold your phone
- 43:27number number so at first this could be
- 43:28quite easy we could say int number
- 43:31equals and then we can start to type our
- 43:33number in here so now that's our number
- 43:36completed let's move on to holding your
- 43:38age and we can just have an INT age
- 43:40equals
- 43:4123 print variables line by line to the
- 43:44screen so we can use CW tab tab and type
- 43:46in name and we can use contrl D to copy
- 43:49and paste the line then we can do number
- 43:51crl D again and then we can use ede now
- 43:54if we run the code let's see what
- 43:56happens
- 43:58here we've actually hit a slight problem
- 44:00it says 1 2 3 4 for the number but I'm
- 44:03pretty sure that I started it off with
- 44:05zero this is a problem with storing your
- 44:07number as an integer as a number zeros
- 44:10at the front don't really matter if we
- 44:12had a 100 zeros here then it's not going
- 44:15to change the actual value the only time
- 44:17zeros matter is if it's throughout the
- 44:19number or towards the end that actually
- 44:21changes the number but if we have lots
- 44:23of zeros at the start then it doesn't
- 44:25change anything so what you actually
- 44:27want to do in order to store a number is
- 44:29you want to store it as a
- 44:31string therefore when you run the code
- 44:33it will actually rain the zero that we
- 44:35typed in so now that we've accomplished
- 44:37all of these let's see how we can Define
- 44:39the variables using the VAR keyword so
- 44:41if we hover over these you can see that
- 44:43it's picking up that it's a string and a
- 44:45string here and finally this is an
- 44:48integer so what we can do very simply is
- 44:51just change these to VAR and then let's
- 44:53hover over the variables and just see if
- 44:55the tool tip reads the correct data type
- 44:57string string and integer and if you run
- 45:01the code you should see no
- 45:05difference so in this exercise I want
- 45:07you to create and initialize two integer
- 45:10variables make a variable to work out
- 45:12the remainder output the remainder to
- 45:14the screen and then change the first
- 45:16integer variable to another number
- 45:18recalculate the remainder and output the
- 45:20new remainder to the screen so let's
- 45:22take this step by step so first we need
- 45:25to create an initialize two ins let's
- 45:27say int num 1 equals to 10 int num 2
- 45:31equals to two make a variable to work
- 45:34out the remainder so we can have an INT
- 45:36remainder equals to and if you remember
- 45:39how to do the remainder it uses the
- 45:41modulus symbol which is the percentage
- 45:43sign so we can say num one percentage
- 45:46num
- 45:48two now it says output remainder to the
- 45:50screen so we can use CW tubtub add
- 45:53remainder in and hit run and just for
- 45:56now we can see that the remainder is
- 45:58indeed zero if the remainder is zero
- 46:01then that means 10 is even because 10
- 46:03modulus 2 gives a zero and if it's zero
- 46:06then that means it's even if the
- 46:08remainder was one then it would mean
- 46:11it's odd so if we try 11 remainer two we
- 46:13get one so 11 is indeed an odd number so
- 46:17let's change this back to 10 and then it
- 46:19says change the first into variable to
- 46:21another number so just after here we
- 46:23could say num one equals 11 and then
- 46:27recalculate the remainder so we
- 46:29essentially just need to do this section
- 46:31again and we don't need to write in
- 46:33before these two variables cuz int is
- 46:35only use when you're declaring it this
- 46:37is reassigning the
- 46:39variable so we'll reassign variable num
- 46:42one to 11 and then we reassign the
- 46:45remainder to the calculation again and
- 46:47now we can use the exact same line of
- 46:48code above to print out the remainder to
- 46:51the screen now you can see that it's 0er
- 46:53and one it's zero because 10 is even and
- 46:56it's one because 11 is
- 46:59odd so far throughout the course we've
- 47:02been using console right line to Output
- 47:04stuff to the screen for example I can
- 47:06output hello my name is Aba and we can
- 47:08see it in the console the issue is so
- 47:11far we haven't really brought any code
- 47:13to be able to read in values from the
- 47:14console so let's explore that option
- 47:17right now so in the console Library we
- 47:19have a console right line but what you
- 47:21don't realize is we can also use this
- 47:23console right line to ask the user a
- 47:25question and to let the user type
- 47:27something in so let's use CW tab tab and
- 47:31say enter your
- 47:33name with a callon and then end the
- 47:36quotation
- 47:37marks and then in order to return a
- 47:40value from the console into your program
- 47:42we need to use console.readline and if
- 47:45you hover over readline let's have a
- 47:47read at description reads the next line
- 47:49of characters from the standard input
- 47:51stream perfect this is exactly what we
- 47:53want to do right now it's use at the end
- 47:56of our code code just to stop the
- 47:58console from closing so we can actually
- 47:59see the output so if we just run this
- 48:02right now we actually get the
- 48:03opportunity to type but when we press
- 48:05enter our program closes this is because
- 48:07in this case we're simply using readline
- 48:10just to wait until the user has pressed
- 48:12enter so so our program doesn't close
- 48:15but what you don't actually see is this
- 48:17is returning as a string which means
- 48:19that anything that we type into here and
- 48:21when we press enter it actually gets a
- 48:23return back to the code but we're not
- 48:25doing anything with it because we don't
- 48:27care at this point so now let's actually
- 48:30do something that will make us care
- 48:32about the user's input so we can make a
- 48:34new string variable and assign it to
- 48:36console readline and it should give us
- 48:38the ability to store Whatever Gets
- 48:40entered inside the console so we can
- 48:43just do string name and we can end the
- 48:45semicolon here and if you wanted to on
- 48:47the next line you could do name equals
- 48:49console do
- 48:52readline or if you wanted to have it all
- 48:54in one line then you can get rid of the
- 48:55semicolon and bring it up like this this
- 48:58is the preferred method as both lines
- 49:00were on top of each other and it wasn't
- 49:01really necessary to split them here
- 49:03we're doing a declaration of string name
- 49:06and an initialization and given it the
- 49:08value of console. readline and this is
- 49:10whatever value the user is going to type
- 49:12in let's run our code and see what
- 49:15happens enter your name abber now when I
- 49:18press enter it successfully started ABA
- 49:21inside this string name variable and now
- 49:23it's waiting at this line the console
- 49:25read line just just right here so when I
- 49:28press enter the program closes so now
- 49:31that we've made a variable and not done
- 49:32anything with it let's print out to the
- 49:34screen to make sure that something's
- 49:35actually worked so if we use CW tab tab
- 49:38and we type in name in the brackets and
- 49:40we can run this code again and let's see
- 49:42what happens as soon as I type in abber
- 49:44and press enter then this second abber
- 49:47is actually the variable printing out to
- 49:48the screen so here it says enter your
- 49:51name with a colon and normally when you
- 49:53see the colon it sometimes lets you type
- 49:55on the same line so if you'd prefer the
- 49:57user to type on the same line then we
- 49:59actually have another function for that
- 50:01instead of using right line we can just
- 50:04use right and the difference between
- 50:06right line and right is the right line
- 50:09will actually have a Terminator at the
- 50:11end of the line to tell the console that
- 50:13he wants to push everything after it
- 50:15onto the next line whereas console.
- 50:17right will just print it out to the
- 50:19screen and not worry about any new lines
- 50:22let's try run it now and now we can type
- 50:25in abber as you can see it's a little
- 50:27close to the colon so we can fix that in
- 50:29a second so you press enter you can see
- 50:31now that comes up on the second line
- 50:34so we just put a space after the callon
- 50:36just to give it a bit of room now if you
- 50:38type in Abba you can see that there's a
- 50:40bit of a gap and it's quite nicer to
- 50:42look at now if we press enter you can
- 50:44see that AB has been stored
- 50:47successfully so what we can do now is
- 50:49maybe ask for your age so we can have a
- 50:52console.
- 50:53right enter your age call on space and
- 50:58then do a string age equals console.
- 51:00readline and then we can have a age
- 51:03output as well and let's see how that
- 51:05works so we can say abber then say 23
- 51:08and press enter and now we have abber
- 51:10stored within the name variable and have
- 51:1323 stored within the age variable so
- 51:16let's try and output a string that's a
- 51:17bit more meaningful to the user let's
- 51:19clean this console up and see what we
- 51:21can do so we don't need to Output their
- 51:24result back to the console because they
- 51:25literally just type that in and we don't
- 51:28want to have this line because we just
- 51:29use that as an example so if we run the
- 51:31code right now it'll look better but it
- 51:34does look a lot more structured enter
- 51:36your name and then enter your age so now
- 51:39when you press enter the program will
- 51:40just close so let's try and output these
- 51:42two variables in a formatted fashion so
- 51:45as you now know we can use console. WR
- 51:47to print a lot of stuff on one line so
- 51:50we can use console. right and we can say
- 51:53your name is and then add a space and
- 51:56finish that line there and then
- 51:59duplicate it down we can write name
- 52:02duplicate that down and say space and
- 52:05your age is duplicate that one down and
- 52:09then we can type in age and let's see
- 52:11what happens now abber
- 52:1423 there we go so we actually have the
- 52:17name output and the age output all on
- 52:20one line if we all change these to right
- 52:23lines then they're going to be on
- 52:24separate lines and it's not going to
- 52:25look very nice nice so I'm just going to
- 52:27introduce you to a notation called
- 52:29string concatenation this is the notion
- 52:32of being able to concatenate two
- 52:33different strings which just means to
- 52:35add one string onto another as you can
- 52:38see this is taken up four lines of code
- 52:40and it's getting a bit ugly the better
- 52:42and easier way to do this is by
- 52:44containing it all within one line so we
- 52:47can have a console right line and we can
- 52:49take the first section your name is keep
- 52:52the space at the end we can end the
- 52:53speech marks and put a plus and then we
- 52:56can send name so now we've just added
- 52:58these two together if we just comment
- 53:00out the rest of them and run the code
- 53:02and you can see that they should be on
- 53:04one line there you go so we can say your
- 53:07name is Aba and let's just keep changing
- 53:09the plus symbol so we can say and your
- 53:13age is another space and the speech
- 53:16marks and plus the age now we've got all
- 53:19of this contained within one line and it
- 53:21looks a lot better enter your name Abba
- 53:25enter your age 23 and now you can see
- 53:27exactly the same output but reduced to
- 53:29one
- 53:32line as you can see these three lines
- 53:34are all stuck to each other so if you
- 53:36wanted to add a blank line between these
- 53:38two you can just put an MC console right
- 53:41line just in here and what this will do
- 53:43is simply just print the right line
- 53:45character to the screen which will force
- 53:47the console to make it on the next line
- 53:50just like so here's the console right
- 53:52line from the empty line that we just
- 53:53wrote and this is the console right line
- 53:55to Output the Val values so now that
- 53:58you've understood how to read values in
- 53:59from the console let's try and convert
- 54:01some values as I showed you how to do
- 54:04earlier on in the cuse you can actually
- 54:05convert these strings into integers
- 54:07ideally you want to store variables in
- 54:09their data types as we covered before
- 54:11ages should not be stored as strings
- 54:14they should be stored as integers but if
- 54:16you try and change this to an INT then
- 54:17you're going to have an issue console
- 54:19read line will return back a string so
- 54:22therefore it can't implicitly convert
- 54:25from a string to an integer so we need
- 54:27to do this in two steps we can rename
- 54:30this to age input and then on the line
- 54:33underneath it we can say int age equals
- 54:37convert to int 32 and then add age input
- 54:41in these
- 54:42brackets there we go and now when we
- 54:44actually print the line of code nothing
- 54:47should actually
- 54:49change your name is abber and your age
- 54:51is 23 but now what's actually changed is
- 54:54we're storing the variables in the
- 54:56correct format this is the ideal method
- 54:58because in the next section when we
- 54:59cover if statements you'll realize why
- 55:01this is very important if you wanted to
- 55:04minimize the amount of steps you need to
- 55:06convert this then you can actually move
- 55:08the console read line and get rid of the
- 55:10string input variable and we can just
- 55:12paste it straight into the bracket of
- 55:14the converter in 32 and you can get rid
- 55:17of the string input variable and
- 55:19everything will work the same h 23 and
- 55:22now it's working exactly the same if
- 55:24this is too many steps for you or if
- 55:26this looks too complicated then just
- 55:28please revert it back just to keep it
- 55:29the same way for the time
- 55:32being so now we've learned how to read
- 55:35in values from the console and output
- 55:36them back to the screen now let's see
- 55:38how we can use if statements to divert
- 55:40our code into multiple paths so in the
- 55:43previous section I told you that it's
- 55:45very crucial for our code to have this
- 55:47int age and the reason why is age is
- 55:50meant to be stored as a number and not
- 55:52as a string so we read it in first as a
- 55:54string and then convert it to an integer
- 55:56using this function and store it into
- 55:58our age variable this is the reason why
- 56:01so our condition goes as follows we
- 56:04would write the word if and between the
- 56:06brackets this needs to be evaluated to
- 56:08either a true or false value and then we
- 56:10can have the curly braces to tell us
- 56:12what to do if this becomes true so in
- 56:15this case you want to just check if our
- 56:16age input is greater than or equal to 18
- 56:19let's say this was an entrance to a
- 56:21place that only allowed overes to enter
- 56:23so we can do age input which refer es
- 56:26the age input variable and we can use
- 56:29the operators equals greater than
- 56:32greater than or equal to less than or
- 56:34less than or equal to or not equal to so
- 56:36these are all the different options that
- 56:38we have we can say if the age input is
- 56:40equal to
- 56:4218 perfect so now that we've read in the
- 56:45age input we can check if it's 18 let's
- 56:48just process a message in here says you
- 56:50are 18 and let's have a look and see if
- 56:53this works
- 56:5618 now I press enter it says you are
- 57:0018 but this is the problem with storing
- 57:02everything as a string and then trying
- 57:04to use it as if it was a number if I
- 57:07type in 18 followed by a space as you
- 57:10can see there's a blank space here now
- 57:11and press enter you can see that it
- 57:13doesn't come up and the reason why is
- 57:15it's processing it like this with a
- 57:17space at the end in order to avoid
- 57:19situations like this this is why we
- 57:21would convert it to a number and the
- 57:24reason why we're using equals to here is
- 57:26we can't use greater than or equal to
- 57:28because strings can't be greater than or
- 57:29equal to another string they can only
- 57:32ever be equal to or not equal to string
- 57:35is just text so if something equals to
- 57:37something then it's fine the only way
- 57:40that we can use these different
- 57:41operators is if we actually interact
- 57:43with our integer variable now let's see
- 57:45how we can use this so we can say age
- 57:48equals equals 18 and now you can see we
- 57:50actually reference it as a number now
- 57:52let's run the code if I do 18 Follow by
- 57:55space it still works and the reason why
- 57:59is it actually dismisses that space when
- 58:01it's converting into the integer so now
- 58:03a just started as an integer and we can
- 58:05actually manipulate this a bit better so
- 58:07instead of saying you are 18 we can say
- 58:09you are 18 or older and now instead of
- 58:12doing equal to we can have greater than
- 58:15if we have greater than then we're
- 58:16actually dismissing the number 18
- 58:18because if the age is greater than 18 so
- 58:21this will read like this and 18 is not
- 58:23greater than 18 so this will return back
- 58:26false what we actually want to use is
- 58:28greater than or equal to if the age that
- 58:31we've inputed is greater than or equal
- 58:33to 18 then you are 18 hence the equal to
- 58:37are greater than if you're older so now
- 58:40let's try and run this AB 23 perfect you
- 58:44are 18 or older let's change so let's
- 58:47change some other if statements to add
- 58:49some additional conditions so we can say
- 58:51else if and the only reason why it would
- 58:54come into this bracket if this condition
- 58:57returns false it will move down the next
- 59:00condition in the else chain if this was
- 59:02wrote as a separate if statement then
- 59:04this would run completely independently
- 59:06from this one if you're going to have
- 59:08chained if statements you need to make
- 59:10sure that you chain them correctly in
- 59:12this case I want to check if it ages
- 59:15between 18 and 25 and then move on to a
- 59:18different age bracket and these two
- 59:20things are grouped together so we should
- 59:22have this as an else if because with an
- 59:24edge bracket you can only be of one age
- 59:26bracket and not multiple so we can say
- 59:29if age is greater than or equal to 25
- 59:32and we can copy this and we can say you
- 59:34are 25 or older now let's see what
- 59:36happens enter your name abber and let's
- 59:39just say that I am
- 59:4035 it says you're 18 or older but I told
- 59:44it the program I was 35 now the problem
- 59:46we're facing is any value that's over 18
- 59:49will always enter inside here and it
- 59:51will skip over this one completely this
- 59:53is not the behavior that we want so so
- 59:56what we can do in our if statements is
- 59:57we can use the double pipe which
- 59:59represents or or the double Ampersand
- 1:00:01which represents and and we can change
- 1:00:04some conditions in here to make sure
- 1:00:06that you implement some sort of range so
- 1:00:09we can actually have a range between 18
- 1:00:11and 25 so we can say if the age is
- 1:00:14greater than or equal to 18 and the age
- 1:00:18is less than or equal to 25 and we can
- 1:00:21tweak this message you are between 18
- 1:00:25and
- 1:00:2725 and let's just run that for now and
- 1:00:30see what happens abber 25 now it says 18
- 1:00:34and
- 1:00:3525 so now that we've got between 18 and
- 1:00:3825 here we can tweak this to be greater
- 1:00:41than equal to
- 1:00:4426 if you're 25 will enter this
- 1:00:47condition and if you're 26 or older into
- 1:00:50this condition so let's test that now if
- 1:00:52we enter
- 1:00:5335 now it says your 26 older that's
- 1:00:57perfect because now that we've
- 1:00:58implemented range checking it gives us
- 1:01:00the ability to differentiate between
- 1:01:02different age groups more precisely
- 1:01:04let's Show an example of how to use the
- 1:01:06R so if we just have a block comment
- 1:01:09around all of this we can write an if
- 1:01:10statement in here and we can say if the
- 1:01:13age is smaller than zero or the age is
- 1:01:16bigger than 150 then we can output to
- 1:01:19the screen in valid Age and what this is
- 1:01:23saying is if we're less than zero and
- 1:01:26notice that this is less than zero so
- 1:01:28this value will actually start at minus
- 1:01:30one and this value start at 150 is minus
- 1:01:34one or less or over 150 then this will
- 1:01:38happen notice why it's an or one of
- 1:01:41these need to be true in order for this
- 1:01:43to be true when you've got an and both
- 1:01:46sides need to be true in order for this
- 1:01:49condition to invalu it is true so let's
- 1:01:51try our code we can say Abu and we put
- 1:01:54minus one and now it says invalid age
- 1:01:57which is perfect because this is our
- 1:01:58range checking and we can try again with
- 1:02:02151 as it's just outside the range it
- 1:02:05will say invalid age and if we have an
- 1:02:08else on here we can print valid
- 1:02:12age notice there is no condition in here
- 1:02:15if you have an open else like this it
- 1:02:17means If This Were to return false it
- 1:02:20will always do this regardless of any
- 1:02:22condition in this case it's perfectly
- 1:02:25reasonable to do this because we know
- 1:02:27that any values that are underneath zero
- 1:02:30or over 150 is invalid and anything
- 1:02:33between 0 and 150 is indeed valid what
- 1:02:37you can do instead is actually move
- 1:02:40these if statements inside here so
- 1:02:42instead of just printing out valid age
- 1:02:45we can actually check the age as
- 1:02:47well so let's run this now and we can
- 1:02:50say Aba minus one and it will only print
- 1:02:52out invalid age and then move on because
- 1:02:55once it reads the this line of code it
- 1:02:56will actually skip over the L's and
- 1:02:59finish up down here and now if you enter
- 1:03:01a real
- 1:03:03value you are between 18 and 25 so let's
- 1:03:07follow the path it got to here and
- 1:03:09realize that the range check was
- 1:03:10actually correct so jumped out of this
- 1:03:12curly brace and went to the else because
- 1:03:14this had returned false we jump into
- 1:03:16this else and now we start checking if
- 1:03:19ages between 18 and 25 which it was
- 1:03:22because our age variable is 23 then it
- 1:03:24outputed this to the screen
- 1:03:28now if we do the same thing but have 35
- 1:03:30instead then it follows the exact same
- 1:03:32path except towards here this will
- 1:03:35return back false and then it will check
- 1:03:36the next condition if age is over 26
- 1:03:40then we'll return
- 1:03:42this so now let's just make a quick
- 1:03:44multiplication program using if
- 1:03:46statement to see if you can get the
- 1:03:47answer correct so let's just comment
- 1:03:49everything
- 1:03:51out and we'll start the next section
- 1:03:53underneath
- 1:03:56so let's make a console. write and ask
- 1:03:58the user to enter the first
- 1:04:01number and then we can say int number a
- 1:04:05equals convert to in32 and the value
- 1:04:09that we want to convert is a console.
- 1:04:11readline which is whatever the user
- 1:04:13entered from the console again if you're
- 1:04:16unfamiliar with this format you can just
- 1:04:18type it in manually number a input
- 1:04:21equals console read line and then we can
- 1:04:23put the number input in here now if you
- 1:04:25use shift and highlight these lines and
- 1:04:26press contrl D we can duplicate this
- 1:04:28entire section and we can say enter the
- 1:04:31second number change a to B change a to
- 1:04:35B and then finally change a to B perfect
- 1:04:38so now we've got enter the first number
- 1:04:40and enter the second number and we can
- 1:04:42say our
- 1:04:44answer equal to number a multi by number
- 1:04:49B and then we can have a console. WR
- 1:04:54value of we can add to it number A plus
- 1:04:58an X Plus number B and then finally plus
- 1:05:03a callon for the user to type in and
- 1:05:05then we can say string answer input
- 1:05:07equals console. readline and then
- 1:05:11int actual answer equals convert to in32
- 1:05:17and then we can have the input in
- 1:05:19here so now that we've got one number
- 1:05:21from the console the second number from
- 1:05:23the console we're just making a variable
- 1:05:25that will multiply them together and
- 1:05:27store inside answer and then we're just
- 1:05:29going to ask the user what's the value
- 1:05:31of number a times my number B and they
- 1:05:34can input that we store it as a string
- 1:05:36and then convert it to an integer and
- 1:05:38store the answer into actual answer so
- 1:05:41now just after the section we can write
- 1:05:43an if statement that says if the answer
- 1:05:45equals to the actual answer then we can
- 1:05:48say Well
- 1:05:50done and else and the reason why we
- 1:05:53don't need to condition this else
- 1:05:55because with equals to the only opposite
- 1:05:57thing you can have is if it's not equal
- 1:05:59to so if you really wanted to you could
- 1:06:01also write if answer is not equal to
- 1:06:04actual answer then in here you can say
- 1:06:08close but it was wrong you don't
- 1:06:11actually need this this is just for
- 1:06:13learning purposes because this will
- 1:06:15return back false if they're not equal
- 1:06:16to each other so this statement is just
- 1:06:18a given if they're not equal to each
- 1:06:20other then we can just assume that
- 1:06:22they're not equal to each other so we
- 1:06:24don't need this additional statement if
- 1:06:26this condition evaluates to being false
- 1:06:29then that means they're not equal to
- 1:06:30each other we don't need this additional
- 1:06:32check to check if they're not equal to
- 1:06:34each other because that's a given if
- 1:06:36this returns back false so we can just
- 1:06:38leave this as an else let's try run the
- 1:06:42code 10 10 value of 10 * 10 100 now it
- 1:06:47says well done and let's just try that
- 1:06:49again and we can enter a different value
- 1:06:5190 Clause but it was wrong so let's just
- 1:06:55have have a quick
- 1:06:56recap so in the previous section we
- 1:06:59talked about how to input from the
- 1:07:00console and we generated this line of
- 1:07:02code which will output your name and
- 1:07:04your age in one line to the console and
- 1:07:07then we looked at how we can use these
- 1:07:08different operators in our if statements
- 1:07:10and we did some range check in to check
- 1:07:12if the age was between 18 and 25 and it
- 1:07:15would output something to the screen and
- 1:07:18if it was over 26 you'd get a different
- 1:07:20output to the screen and then we took
- 1:07:22that a little bit further and started
- 1:07:24using the r operator
- 1:07:26if the age is smaller than zero or it's
- 1:07:28greater than 150 then it must be invalid
- 1:07:31and if this condition returns false it
- 1:07:34must be between 0 and 150 so we can come
- 1:07:37into this L's bracket and inside here we
- 1:07:39can do our range checkin from the
- 1:07:41previous bit of code we can check the
- 1:07:43ranges between 18 and 25 and print a
- 1:07:46value or if it's over 26 we can print
- 1:07:48another value and you can keep changing
- 1:07:50these if you want you could have 26 to
- 1:07:5240 and 40 to 60 Etc Etc that's up to
- 1:07:57you so this is just a quick little
- 1:07:59program to simulate multiplication
- 1:08:02program we can enter the first number
- 1:08:04using console. WR and then we start it
- 1:08:06as a string and we put that string into
- 1:08:08the converter in32 function to give us
- 1:08:11back an integer variable and we do
- 1:08:14exactly the same thing for the second
- 1:08:15number and then we make a variable to
- 1:08:18store both numbers multiplied by each
- 1:08:20other we ask the user what is the value
- 1:08:22of number a Times by number B and we
- 1:08:25give them an opportunity to type in a
- 1:08:26value we read that value in convert it
- 1:08:29to an integer store it as an into actual
- 1:08:32answer and then in our if statement we
- 1:08:34say if the answer is equal to the actual
- 1:08:36answer then we print well done to the
- 1:08:40screen double equals are used for
- 1:08:42equality and single equals are used for
- 1:08:44assignment don't get them mixed up we
- 1:08:47don't need an else condition in here
- 1:08:49because if this returns true then this
- 1:08:51will execute if this is indeed false
- 1:08:54that they're not equal to each other
- 1:08:55then we always want to do this section
- 1:08:57and we don't need to put an additional
- 1:08:59if statement inside here if you wanted
- 1:09:01to but it's not necessary you can have
- 1:09:03lse if answer is not equal to actual
- 1:09:07answer but this is just extra code and
- 1:09:09it hurts the readability and it should
- 1:09:12be
- 1:09:14avoided if statements are one of the
- 1:09:16methods we can decide different paths in
- 1:09:18our code the next method we can use is
- 1:09:20called switch statements so let's write
- 1:09:22a little program so we can demonstrate
- 1:09:23this so we can just have a console right
- 1:09:26enter date of the week and we can have
- 1:09:29in date equals convert to in32 and we
- 1:09:32can place our console. readline inside
- 1:09:35the brackets so we take whatever the
- 1:09:37user typed into the console and then
- 1:09:39place it inside here and inside here
- 1:09:41we're going to convert whatever they've
- 1:09:42typed in to an integer 32 and star it
- 1:09:45into our D variable so if we just print
- 1:09:47this out to the screen to make sure it's
- 1:09:48working
- 1:09:49fine if we just type in seven we can see
- 1:09:52that seven is returned so it's all
- 1:09:53working so normally what we do is type
- 1:09:56if D equals equals 1 and then we can add
- 1:09:59the Cy braces and use CW tab tab and we
- 1:10:02can print out Munday now if you run the
- 1:10:04code and try use number one then Monday
- 1:10:07works and that's fine and then let's do
- 1:10:09another LS if and we can say day equals
- 1:10:11equals 2 and then we can print
- 1:10:14Tuesday perfect so if we try that
- 1:10:16now there we go so now we have the
- 1:10:18ability to print out Monday and Tuesday
- 1:10:20to the console depending what day the
- 1:10:22week the user enters but since we're
- 1:10:25going to have have seven different paths
- 1:10:26because we got seven days of the week
- 1:10:28then sometimes it might be better and
- 1:10:29cleaner to use a switch statement so
- 1:10:32let's have a look at the syntax so we
- 1:10:33can type in switch and then inside the
- 1:10:35brackets it's what variable we want to
- 1:10:37have a switch on so in this case we want
- 1:10:39to be using the day variable so we can
- 1:10:41add D in the brackets this is not a
- 1:10:43condition in the brackets this is just
- 1:10:44telling us what variable we're going to
- 1:10:46be using for the switch statement so
- 1:10:48let's open the curly braces after and
- 1:10:50inside here instead of having paths like
- 1:10:52this we're going to use cases so this
- 1:10:55can be translated to in the case of
- 1:10:57number one we can do this in the case of
- 1:11:00number two we can do this so let's try
- 1:11:03and see how it works we can type in case
- 1:11:05followed by number one and then add a
- 1:11:07callon and then we can type out our
- 1:11:09Monday in here and then we need to
- 1:11:12follow each case off by finishing the
- 1:11:14section off with a break this break's
- 1:11:16very essential and you'll get a syntax
- 1:11:18error if you don't put it in so now if
- 1:11:20we do case two and we do exactly the
- 1:11:22same thing there we go and if I dis miss
- 1:11:25the break then you'll realize that it
- 1:11:27says it cannot fall out so we have to
- 1:11:29use a break just the compiler knows that
- 1:11:31we want to break here the reason why
- 1:11:33this break is placed inside here is
- 1:11:35because what we can do is we can say
- 1:11:38case two and then add another one in
- 1:11:41here called case 3 and what this now
- 1:11:43reads is if we enter two it says choose
- 1:11:46there and if we enter three It also says
- 1:11:49choose it so what you can do is you can
- 1:11:52actually chain so this would be
- 1:11:53equivalent to writing if D equals 2 or D
- 1:11:57equals equals 3 now this is exact
- 1:12:00translation these two are exactly the
- 1:12:02same as of right now we have D equal
- 1:12:04equal 1 which is our K1 and then equals
- 1:12:072 Ral 3 is right here if we go into the
- 1:12:11case 2 path it will fall in the case 3
- 1:12:13so we're saying for this break is we
- 1:12:15want to end this section here so now if
- 1:12:17we just revert this back and continue
- 1:12:19the
- 1:12:20chain I'll just skip this part just so
- 1:12:22it's a little bit quicker
- 1:12:26there we go so now that we've got all
- 1:12:27seven days of the week let's just see if
- 1:12:29it works let's try six and we have
- 1:12:31Saturday so now we're kind of missing
- 1:12:33something from here the only thing we're
- 1:12:35missing is having our external lse just
- 1:12:38in case the user types something invalid
- 1:12:40so normally we would write something
- 1:12:41like this enter value between 1 and 7
- 1:12:46just to make sure that the user knows
- 1:12:47what they're
- 1:12:48doing now we don't seem to have anything
- 1:12:51inside here that gives us the
- 1:12:52opportunity to invalid so we have this
- 1:12:55keyword called default and it works
- 1:12:57exactly the same as the case except what
- 1:12:59this will do is this is basically the
- 1:13:01word that replaces else so we can
- 1:13:03literally take this line of code and
- 1:13:05place it again over here and add
- 1:13:07ourselves a break this is exactly the
- 1:13:09same as doing our L statement inside
- 1:13:11here if none of these cases match this
- 1:13:14variable then it'll go into the default
- 1:13:16and Contin as normal so if we type in
- 1:13:18zero we should see the message twice
- 1:13:20because it's executing for the if
- 1:13:21statement and executing down here and
- 1:13:24let's do eight and you see the message
- 1:13:26again so now that the case statement is
- 1:13:28fully working let's just comment out the
- 1:13:29if statement and make sure that it still
- 1:13:31works one Monday let's just try another
- 1:13:34one and we can do seven for Sunday so
- 1:13:38just a quick recap case statements are
- 1:13:39used by using the keyword switch
- 1:13:41followed by the variable in question and
- 1:13:43then if you want to check if this
- 1:13:44variable number is equal to something
- 1:13:46you put that something straight after
- 1:13:48the word case followed by a colon and
- 1:13:50then you can insert multiple lines of
- 1:13:52Corde inside here and make sure you end
- 1:13:54that line with a break if you want to
- 1:13:56chain multiple like Dayal 2 or Dayal 3
- 1:14:00then you can add the case above right
- 1:14:02here so the case of zero or the case of
- 1:14:05two it will do this little section so
- 1:14:08it'll print out Tuesday if the user
- 1:14:09enters zero or the user enters two of
- 1:14:12course in this case we don't actually
- 1:14:13want that after you filled out all of
- 1:14:16the code make sure you add something
- 1:14:18called the default the default is
- 1:14:19equivalent to an lse keyword in our if
- 1:14:21statement just to catch all the values
- 1:14:23in case the user type something
- 1:14:25incorrect and you want to let them know
- 1:14:27what they should be typing
- 1:14:30instead so now that we explored
- 1:14:32different paths in our code using IF
- 1:14:33statements and switch statements let's
- 1:14:35have a look at how we can iterate
- 1:14:36through our code an iteration in code is
- 1:14:39just the ability to be able to Loop and
- 1:14:41do a certain task so let's say we wanted
- 1:14:43to print high to the console several
- 1:14:45times then we just have to copy and
- 1:14:47paste it down and this will be five
- 1:14:49separate lines that do exactly the same
- 1:14:51thing this will essentially achieve what
- 1:14:54we want the problem is you just have
- 1:14:56exactly the same code copy and pasted so
- 1:14:59what we can do if we just block comment
- 1:15:01this line for the time being we can
- 1:15:03write something called a for Loop a for
- 1:15:05Loop gives us the ability to Loop a
- 1:15:07certain amount of times depending on the
- 1:15:08parameters that we give it so this is
- 1:15:10the syntax you can type for open the
- 1:15:13bracket and we have to use a variable so
- 1:15:16we can declare a new variable straight
- 1:15:17into these brackets we can say inti
- 1:15:19equals z and then have a semicolon and
- 1:15:22there's three different sections to this
- 1:15:24the first one is the variable decoration
- 1:15:26and initialization and the second one is
- 1:15:28the condition so in this case we want to
- 1:15:30do this task five times so you want to
- 1:15:33say I is less than five then we end that
- 1:15:35semicolon there and now for the third
- 1:15:37task every time this for Loop goes
- 1:15:39around we want to increase I by one
- 1:15:42because we want to go in steps of one so
- 1:15:44we can do
- 1:15:45i++ alternativ if you don't like using
- 1:15:48Plus+ you can say I = I + 1 or to make
- 1:15:52that quicker you could say I + equal 1 I
- 1:15:55personally just prefer using Plus+ and
- 1:15:57you'll see that everywhere else there is
- 1:15:59certain cases where you would actually
- 1:16:01want to change this to a different value
- 1:16:03like I +al 2 and we'll show you that in
- 1:16:05a second so this for Loop now says we
- 1:16:08want to go from I equals 0 and once this
- 1:16:11condition returns back false when I is
- 1:16:13equal to 5 or greater then we want to
- 1:16:15stop this for Loop every time this for
- 1:16:17Loop goes around so in hits this curly
- 1:16:19brace and comes back around we want to
- 1:16:21do this action and in this case it's I
- 1:16:24++ so if we just use console right line
- 1:16:26and print out the value I we can see
- 1:16:28what happens when the follow
- 1:16:31runs you can see that the value of I is
- 1:16:34increasing one each time and after the
- 1:16:36fifth time is increased the value is
- 1:16:39actually five so now this statement is
- 1:16:41false because the value of I is equal to
- 1:16:44five so therefore this is now false and
- 1:16:46the for Loop is ended so now that we've
- 1:16:49verified that you can repeat it five
- 1:16:51times using this code we no longer want
- 1:16:53to print I we want to print our high so
- 1:16:55let's paste that inside the bracket and
- 1:16:57run the code now you can see that these
- 1:17:00two values will be exactly the same as
- 1:17:02printing it out to the console just like
- 1:17:03this but now we have a lot less code so
- 1:17:07let's have a look at another example we
- 1:17:08can say 4 in I equal 0 and we can say I
- 1:17:13is less than or equal to 10 and then we
- 1:17:16can do I + = 2 and then if we just print
- 1:17:19I to the console so what this is now
- 1:17:22saying is I want to go from zero all way
- 1:17:25till 10 and I want to go up two times
- 1:17:28each time so what this will be achieving
- 1:17:30is printing out all of the even numbers
- 1:17:32between 0 and
- 1:17:3310 let's try and run
- 1:17:36it now we can see we have all the even
- 1:17:38numbers printed out between 0 and 10
- 1:17:41because we're going up in
- 1:17:42twos if we start this at one for example
- 1:17:45and still keep every parameter exactly
- 1:17:47the same then we'll be getting all of
- 1:17:49the even numbers between 1 and 10 11 is
- 1:17:52missing because our condition stops at
- 1:17:54less than or equ equal to 10 what you
- 1:17:56can also do with for Loops is you can
- 1:17:57let the user determine what this value
- 1:18:00is going to be let's test that so if we
- 1:18:03block comment this section out what we
- 1:18:05can do is we can appen this little
- 1:18:06section so we can have a console. right
- 1:18:09how many times do you want to say hi can
- 1:18:13have a callon followed by speech marks
- 1:18:15and then we can say in Loop counter
- 1:18:18equals convert to in32 and we can have
- 1:18:21console read line inside the
- 1:18:23brackets how many times you want to say
- 1:18:25hi the user types in the value you take
- 1:18:28the console read line as a string
- 1:18:30convert it to an integer and start
- 1:18:31inside loop
- 1:18:33counter so now what needs to change
- 1:18:35inside here the five is what determines
- 1:18:38how many times that we're going to Loop
- 1:18:40so in this case we actually want to just
- 1:18:41Loop by whatever the user types in the
- 1:18:43counter let's just type five and make
- 1:18:46sure it works and if we just type one it
- 1:18:50works if we type zero however it will
- 1:18:54just skip over the for Loop and the
- 1:18:56reason for that is if you're going from
- 1:18:580o to I is less than zero then this is
- 1:19:00immediately going to be false so then
- 1:19:03the for Loop is just going to get
- 1:19:04skipped immediately there is nothing to
- 1:19:06do if the user is typed in zero which is
- 1:19:08perfectly reasonable and you can type in
- 1:19:10a really big number like a 100 and it
- 1:19:12will output them all to the
- 1:19:15screen if the user types in zero then
- 1:19:18nothing's going to come up so you always
- 1:19:19want to be able to let the user know if
- 1:19:21there's something wrong so we can say if
- 1:19:23the loop counter equals equals to zero
- 1:19:26or maybe we can change this to less than
- 1:19:28or equal to because it could type in
- 1:19:30minus numbers as well we could just say
- 1:19:32a conso right line using CW tab tab
- 1:19:35sorry please enter a value above zero
- 1:19:40and then if this condition has returned
- 1:19:42back false then inside our else
- 1:19:44statement we don't need to give it an
- 1:19:46else if because if it's less than or
- 1:19:48equal to zero then we want to process
- 1:19:50the other values because it has to be
- 1:19:53greater or equal to one
- 1:19:56so when this is false this section has
- 1:19:59to be greater than or equal to one so we
- 1:20:01can just place our fall Loop inside here
- 1:20:03and that's fine and let's try to run the
- 1:20:05code now and if we type in zero please
- 1:20:08enter a value of zero and if we just try
- 1:20:10any negative values we'll get the same
- 1:20:12message if you want to print additional
- 1:20:14code in here you can literally do
- 1:20:15anything you want you can even ask the
- 1:20:17user what they want to print up here and
- 1:20:19then you can replace this with a message
- 1:20:22for example we could have a string
- 1:20:23message
- 1:20:25and we can just change this to hello
- 1:20:26world and this could be something that
- 1:20:28you reading from the console so we can
- 1:20:30say message and we can put that in here
- 1:20:32and if we print that there we
- 1:20:35go so we can just say console.
- 1:20:39right what do you want to repeat and
- 1:20:43then we can read this
- 1:20:45in and actually it might be easier if we
- 1:20:48change this Loop counter to Second so if
- 1:20:51you highlight the section hold alt and
- 1:20:53press down arrow twice we can move it
- 1:20:55down and say and how many times do you
- 1:20:59want to repeat it there we go so now
- 1:21:03what we can do is we can use this
- 1:21:05message inside here and then Loop
- 1:21:07depending on how many times the user
- 1:21:09typed in whatever so we can type in
- 1:21:11hello world get this exact value and we
- 1:21:13can say 10 times there we go so now this
- 1:21:16entire program is fully customized by
- 1:21:18the users
- 1:21:20input so let's just have a quick recap
- 1:21:23so we started off this code I print in
- 1:21:25the console right line five times to the
- 1:21:26screen we wrote Our simple far Loop that
- 1:21:29went from in I equals 0 to I is less
- 1:21:32than five and we did I ++ because we
- 1:21:34want to count up in ones you can place
- 1:21:37the high inside here and it'll print it
- 1:21:39five times for us so now we've made our
- 1:21:41code from five lines down to just three
- 1:21:43lines for the follow and then we allowed
- 1:21:46the user to type in how many times that
- 1:21:48they want to repeat it and we change the
- 1:21:50condition from I is less than 5 to I is
- 1:21:52less than whatever the user has typed in
- 1:21:55installed within the loop counter and
- 1:21:57then we hit a problem where if we
- 1:21:59entered zero anything less than zero the
- 1:22:01for Loop would just not run and it might
- 1:22:03look like some things went wrong so we
- 1:22:05added a little if statement in here to
- 1:22:07make sure that the user has entered a
- 1:22:09value above zero and if they haven't we
- 1:22:11can let them know we don't need another
- 1:22:13if statement in here because if this has
- 1:22:15returned back false we can assume that
- 1:22:17the value is something that we can work
- 1:22:19with we later tailored the program so we
- 1:22:22can change this message that was fixed
- 1:22:24to I to something that the user can type
- 1:22:26in
- 1:22:28manually so now that we know how to use
- 1:22:31for Loops let's have a look at while
- 1:22:32Loops for loops and while Loops still
- 1:22:35iterate through code but they're used in
- 1:22:37slightly different contexts for Loops
- 1:22:39are used mainly when you know how many
- 1:22:41times you can iterate through as in this
- 1:22:43example we're going to print to the
- 1:22:45console exactly 10 times but in a while
- 1:22:47loop you might print until a condition
- 1:22:50is true for example if you asking the
- 1:22:52user to type a number and they kept
- 1:22:54typing in something else you don't know
- 1:22:56how many times it's going to take the
- 1:22:58user to type in correctly so in that
- 1:23:01case you would use a y Loop if you
- 1:23:03executing a certain amount of code and
- 1:23:05you're only doing it a fixed amount of
- 1:23:07times then it's always best to use a for
- 1:23:09Loop so first let's try and convert this
- 1:23:11simple for Loop that just prints I to
- 1:23:13the console 10
- 1:23:15times just like so from zero all the way
- 1:23:18till 9 so let's comment out this for
- 1:23:21Loop and have a look at this syntax for
- 1:23:23the while loop so type the world while
- 1:23:26and then in here we have our condition
- 1:23:28and then we can open our curly braces
- 1:23:30and while this condition returns back
- 1:23:32true it will always go inside the curly
- 1:23:34braces and when this condition returns
- 1:23:36back false then we'll exit the while
- 1:23:39loop so let's try and mimic exactly what
- 1:23:42we're doing inside here there's three
- 1:23:44sections to the for Loop we Define the
- 1:23:47variable that we're going to use for the
- 1:23:48for Loop counter and we give it an
- 1:23:50initial value we give it the stopping
- 1:23:52condition and we give it how many many
- 1:23:54times the value needs to go up every
- 1:23:56time the for Loop iterates in the while
- 1:23:58loop situation we only have the
- 1:24:00condition available so let's take our
- 1:24:02condition and paste it straight into
- 1:24:04here and now we need to store our
- 1:24:06variable so just because it's a y Loop
- 1:24:09and we can't store it inside here we
- 1:24:10have to do it outside the while loop so
- 1:24:12let's make it in I and we'll make it
- 1:24:14equal to zero so we've got our variable
- 1:24:17we've got our condition now we need to
- 1:24:19set
- 1:24:20i++ so inside the Y Loop we can say i++
- 1:24:24and then now we've achieved this entire
- 1:24:26line and successfully converted it let's
- 1:24:28try and put the console right line in
- 1:24:30and see what
- 1:24:32happens and let's run the
- 1:24:34code so it's going from 1 all the way
- 1:24:37till 10 and the reason why it's going
- 1:24:39from 1 till 10 not from 0 to 10 is
- 1:24:42because the i++ is at the start let's
- 1:24:44move it down online using alt and down
- 1:24:46arrow key to move it down and let's run
- 1:24:49it again and you can see it goes from
- 1:24:50zero till 9 and if we just block comment
- 1:24:53this out
- 1:24:55and undo the for
- 1:24:57Loop you can see that they're doing
- 1:24:59exactly the same thing so let's bring
- 1:25:01the Y loop
- 1:25:04back and as you can see what we have is
- 1:25:07we've defined our I variable made it
- 1:25:09equals to zero while the I variable is
- 1:25:12less than the value of 10 we print the I
- 1:25:15to the console and increment I by one
- 1:25:17and we keep looping this until this
- 1:25:20statement returns back false and then we
- 1:25:22exit the Y Loop and jump underneath the
- 1:25:24the curly braces and hit the console
- 1:25:26read line and this is the output that
- 1:25:28we've got so now I've just copied the
- 1:25:30code from the if statement video and
- 1:25:31let's make this code a little bit better
- 1:25:33so let's comment out our while loop and
- 1:25:35our for Loop that was just used for
- 1:25:37explanation purposes and this is the
- 1:25:39code that you've saw earlier on in the C
- 1:25:41we read in the first number Star as a
- 1:25:43string use that string to convert to an
- 1:25:46integer star number a and Then star
- 1:25:48number B and these two blocks are
- 1:25:50exactly the same except you have enter
- 1:25:52first number and enter second number
- 1:25:54and we store number a and store number B
- 1:25:57then we store the value of them both
- 1:25:59multiplied inside our int answer
- 1:26:02variable then we ask the user what's the
- 1:26:04value of both answers times together
- 1:26:07give them a chance to input their value
- 1:26:09convert it to an integer and then we
- 1:26:11check the value and print well done of
- 1:26:13Cl but it was wrong now if we run this
- 1:26:15code again and we type in 10 and 10 and
- 1:26:19we know the value is 100 but let's just
- 1:26:21type in 90 now this is kind of a
- 1:26:23problematic user case we've asked the
- 1:26:26user to type in a value and they've got
- 1:26:28it wrong and then when they press enter
- 1:26:30the program just closes there's no
- 1:26:32opportunity to Loop or try this again so
- 1:26:35what we can do is add a y Loop in here
- 1:26:37and we can say while the answer is
- 1:26:39incorrect we can keep asking the user to
- 1:26:41keep on guessing so instead of reading
- 1:26:44the input here let's turn this little
- 1:26:45section into a question what's the value
- 1:26:50of number a * number B and instead of
- 1:26:52allowing an input here let's just put a
- 1:26:54question mark and then let's add a
- 1:26:57console right line just to give a little
- 1:26:58bit of a gap and then in order to do our
- 1:27:01while loop we actually want to create a
- 1:27:03boan variable just to track if the
- 1:27:05answer has been correct or
- 1:27:07not this is the section that we want to
- 1:27:10loop around in the while loop we want to
- 1:27:12read in the user's answer check it
- 1:27:14output a message and then give them the
- 1:27:16opportunity to read in another value so
- 1:27:18they can guess again while the answer is
- 1:27:22not equal to the actual answer
- 1:27:24then we can loop around our code so we
- 1:27:26have to modify a few things here so we
- 1:27:28can't just take this code and place it
- 1:27:30inside the problem with doing this is
- 1:27:33the variables that we want to check
- 1:27:35answer is outside of this y Loop but
- 1:27:37actual answer is inside and the reason
- 1:27:40why there's a red line here says it does
- 1:27:42not exist in the current context because
- 1:27:44we're trying to check a variable that
- 1:27:45hasn't been defined yet so what we can
- 1:27:47do is take the Declaration for the
- 1:27:49actual answer and place it outside here
- 1:27:51and make it equal to zero and instead of
- 1:27:54redeclaring it inside the while loop we
- 1:27:55can take this in and get rid of it so we
- 1:27:58set the actual answers to zero set the
- 1:28:00answer to the multiplication while the
- 1:28:02answer is not equal to the actual answer
- 1:28:05then we want to loop around so we don't
- 1:28:07actually need another check in here this
- 1:28:10while loop will keep on repeating if the
- 1:28:11answer is incorrect so we don't need a
- 1:28:14path for correct and incorrect if this
- 1:28:16while loop still continues then the
- 1:28:18answer will always be incorrect so what
- 1:28:20we can do to double check is we can say
- 1:28:23if the answer answer does not equal the
- 1:28:25actual answer then we can place this
- 1:28:27message inside here and get rid of this
- 1:28:31branch and then after the while loop has
- 1:28:34continued we can say Well done so let's
- 1:28:37just read through the code while the
- 1:28:39answer is not equal to the actual answer
- 1:28:42and when this while loop first enters
- 1:28:43it's going to be true because you're
- 1:28:45going to have 10 * 10 which is 100 and
- 1:28:48the value of answer is zero so this will
- 1:28:50return back true because these two
- 1:28:52values are not equal to each other then
- 1:28:54we're going to ask the user to enter the
- 1:28:55value then we're going to read into the
- 1:28:57actual answer check if they're not equal
- 1:28:59to each other and then we output a
- 1:29:01message because we're asking a question
- 1:29:03here we want to ask another one using a
- 1:29:05console right Line enter your answer
- 1:29:10callon here we go so let's have a look
- 1:29:12and see how this
- 1:29:14works 100 100 what's the value 100 * 100
- 1:29:19and let's just get it wrong on purpose
- 1:29:2190 CLA but is wrong and now we get to
- 1:29:23answer again
- 1:29:24and we can keep answering this as many
- 1:29:26times as we want until we reach the
- 1:29:28correct answer which in this case is
- 1:29:3110,000 and we've got the answer well
- 1:29:33done so to make the console look a
- 1:29:36little bit better cuz this is all in one
- 1:29:37block we can separate these with console
- 1:29:39right lines maybe after this line we can
- 1:29:42print out a new line and then after each
- 1:29:44guess we can print out another new line
- 1:29:46so after each guess we'll be down here
- 1:29:48we can just print out a blank console
- 1:29:49right line and just after you ask them
- 1:29:52to enter the second number we can print
- 1:29:54another one here and let's just see how
- 1:29:56that looks 10 10 now it looks a lot
- 1:29:58better because you ask the user to input
- 1:30:00the numbers then you ask them the
- 1:30:02question and then you get the answer and
- 1:30:04then it keeps looping around like
- 1:30:06this so there's a little gap between
- 1:30:09each one so you can efficiently read it
- 1:30:10instead of it all being closed off
- 1:30:12together now if you answer it correctly
- 1:30:14100 now it says well done and when we
- 1:30:17press enter again the program now closes
- 1:30:19so that was using a while loop and now
- 1:30:21we have something else called a do while
- 1:30:24and a do while is very close to a while
- 1:30:27except that the while will only happen
- 1:30:29if this condition returns back true and
- 1:30:31a do while will always happen once and
- 1:30:34it'll check the condition at the end so
- 1:30:36let's have a look and see how this works
- 1:30:38so we write the word do and then we have
- 1:30:40the curly braces and then after it we
- 1:30:42have our while condition and let's just
- 1:30:44copy the condition from the old while
- 1:30:46loop and we can say answer is not equal
- 1:30:49to actual answer so as you can see with
- 1:30:51a while loop it will always check the
- 1:30:53condition first first and then it opens
- 1:30:55the curly braces in the do there is no
- 1:30:58condition it will always do everything
- 1:31:00inside the curly braces once and then it
- 1:31:02will check this while condition if this
- 1:31:05condition is still true then it'll loop
- 1:31:07around and check it every time but it
- 1:31:09will only check it at the end while
- 1:31:11Loops will check the condition as the
- 1:31:13first thing it does and then check it
- 1:31:15every iteration so let's move our cord
- 1:31:18down into here and let's just comment
- 1:31:20out our while loop just to see how this
- 1:31:22works and if we run it again to make
- 1:31:25sure it's working fine 10 10 90 90 50
- 1:31:30and then finally 100 and it says well
- 1:31:32done so you could be thinking what are
- 1:31:34the differences between a while loop and
- 1:31:36a do while in certain cases you want the
- 1:31:38while loop to make sure that this
- 1:31:40condition is true before even continuing
- 1:31:42in this specific case we always want the
- 1:31:45user to answer the question it doesn't
- 1:31:48matter if it executes once because
- 1:31:50actually that's the behavior that we
- 1:31:51want we've asked the user a question
- 1:31:54right here so we want to always be
- 1:31:56looping at least one time the while loop
- 1:31:59user case doesn't quite apply here
- 1:32:01because we don't necessarily have to
- 1:32:03check if they've answered correctly if
- 1:32:05they haven't even inputed a value yet we
- 1:32:08always want to be able to give them the
- 1:32:09option to do it once and then we check
- 1:32:12their answer at the end they could get
- 1:32:13it on the first answer 10 10 100 and
- 1:32:17it'll still be fine the same is true
- 1:32:19with a while loop but it'll be easier
- 1:32:22and it makes more sense to read your
- 1:32:24code when you have a while loop you can
- 1:32:26see that this bit of code depends on a
- 1:32:28condition whereas in a do while the
- 1:32:31first iteration does not depend on
- 1:32:33anything but the second and third and so
- 1:32:35forth depend on the condition so
- 1:32:38depending on your user case and what
- 1:32:40situation you want to use it you should
- 1:32:42either pick a while loop which will only
- 1:32:44occur through a condition or pick a do
- 1:32:47while that will occur once or multiple
- 1:32:49times after the first condition has
- 1:32:51passed
- 1:32:54so when you have if statements that are
- 1:32:56pretty basic you can actually make them
- 1:32:58quite easy to read through the
- 1:33:00conditional operator also known as the
- 1:33:02tary operator so let's see how this
- 1:33:04works let's say we have an INT age and
- 1:33:06we set it to a minus value and we know
- 1:33:09that this value can never be someone's
- 1:33:11age because it's negative but let's say
- 1:33:13that we have a situation where you ask
- 1:33:15the user for their age and they've
- 1:33:17accidentally typed in -10 and we know
- 1:33:19that that value is not actually true so
- 1:33:21we could write something like this age
- 1:33:24is greater than or equal to zero then we
- 1:33:26can print out
- 1:33:27valid and then in the else we can take
- 1:33:31this console right line and we can print
- 1:33:33out invalid so that means if we run our
- 1:33:35code now you can see that it says
- 1:33:38invalid and if we change it to positive
- 1:33:4010 now it says valid so this seems a
- 1:33:43little bit long we are just checking one
- 1:33:46condition printing a value and printing
- 1:33:49another value if that condition returns
- 1:33:51back false so our conditional operator
- 1:33:54works like this we have a condition
- 1:33:56followed by question mark then the value
- 1:33:59if it returns true then the value if it
- 1:34:01returns false let's see how you can
- 1:34:03rewrite this if statement into one
- 1:34:05simple line so let's take this in
- 1:34:09steps these two values are both strings
- 1:34:12that we've typed in so let's make a
- 1:34:14string and we can call it result and
- 1:34:16let's make it equal to our Turner
- 1:34:19operator so the first step is to get the
- 1:34:21condition and the condition in this case
- 1:34:23is if the age is greater than equal to
- 1:34:25zero we can follow that by a question
- 1:34:28mark and then if this returns back true
- 1:34:31we want the string to say valid and we
- 1:34:33can have a call on and if it returns
- 1:34:36back false then we want the string to
- 1:34:38say invalid and we can add a semicolon
- 1:34:41and then straight after it we can just
- 1:34:42have a console right line that just says
- 1:34:45result as you can see now that we get it
- 1:34:47twice we can verify one comes from the
- 1:34:49if statement and one comes from the turn
- 1:34:52operator and now if we make this say
- 1:34:53positive 10 then you can see it both
- 1:34:55says valid so if we comment out the if
- 1:34:58statement and just see this running on
- 1:35:00its own just to double check there we go
- 1:35:03and because this is just returning back
- 1:35:04a string we can actually just take all
- 1:35:07of this code in the conditional operator
- 1:35:09and paste it straight into the console
- 1:35:11right line so you run the line of code
- 1:35:12now we get exactly the same thing if
- 1:35:15this hurts the readability of your code
- 1:35:18then it's best to just store it and then
- 1:35:19use it after if you can read this code
- 1:35:21and it's better for you in the long run
- 1:35:24later on you might want to dismiss this
- 1:35:26variable and do it straight inside the
- 1:35:28brackets just to save a variable and
- 1:35:30then print itto the screen straight away
- 1:35:33it'll be better for memory if you just
- 1:35:34print it out straight away but if you're
- 1:35:36not familiar with this notation yet and
- 1:35:38you want to get used to it then that's
- 1:35:39fine as well so a quick recap we had an
- 1:35:42if statement with a condition and if
- 1:35:44this returned back true we printed valid
- 1:35:47and if it return back false we' print
- 1:35:48invalid and this is pretty simple the
- 1:35:51only difference between these two paths
- 1:35:53is this string literal that we've typed
- 1:35:55into here so we can pull the string
- 1:35:57values out and place them in our
- 1:35:59conditional operator we can say string
- 1:36:01result is equal to if the age is greater
- 1:36:04than or equal to zero then we store
- 1:36:07valid inside result and if it's not
- 1:36:09greater than equal to zero then we can
- 1:36:11St invalid straight in our string after
- 1:36:14we've stored this value we can print
- 1:36:16results straight to the screen which
- 1:36:18will either give us valid or
- 1:36:22invalid when when you're dividing two
- 1:36:24numbers you might get an ugly output
- 1:36:26just like
- 1:36:27this the user sometimes doesn't want to
- 1:36:29see such long decimal places and maybe
- 1:36:32you just want to prevent it to
- 1:36:3481.3 in order to do this we need to use
- 1:36:37a format pattern so the language
- 1:36:39understands what we're trying to achieve
- 1:36:40so if we make another console right line
- 1:36:42what we can do is use string. format and
- 1:36:45inside the brackets we can give it a
- 1:36:47format and then the object that we want
- 1:36:48to format so in this case the object in
- 1:36:51the second parameter is our value so
- 1:36:53let's open some speech marks go to the
- 1:36:56next parameter using the comma and just
- 1:36:58type in our value and then inside the
- 1:37:01speech marks we want to type in the
- 1:37:02pattern if you type in curly braces as
- 1:37:04zero then zero represents the first
- 1:37:07valid you in this list we can do a space
- 1:37:10followed by curly braces one and then
- 1:37:12add another comma and we can add a
- 1:37:14thousand and what this means is value
- 1:37:17will get place where zero is and 1,000
- 1:37:19will get Place one is so let's run this
- 1:37:23and you see the exact same thing's
- 1:37:25happened plus dis place and now we have
- 1:37:27the Thousand which follows this pattern
- 1:37:29exactly if this is confusing don't worry
- 1:37:32too much about it I have the next
- 1:37:33section all about string functions so
- 1:37:36let's get rid of the one followed by the
- 1:37:38thousand and run it again and we should
- 1:37:40see the output being exactly the same
- 1:37:42there we go so now let's see how we can
- 1:37:45make this better in terms of decimal
- 1:37:47places so just after the zero and the
- 1:37:49zero represents the first value we can
- 1:37:52add a call on and this column will tell
- 1:37:54us what format we want the value to be
- 1:37:56displayed as we can type in
- 1:37:590. and what this means is all the
- 1:38:01numbers before the decimal place will
- 1:38:03get displayed and only two numbers after
- 1:38:06the decimal place will get displayed so
- 1:38:08let's try run this now and you can see
- 1:38:09that the code output is a lot cleaner
- 1:38:12one thing to note though by default this
- 1:38:13will round it up 0.37 becomes
- 1:38:170.4 not to worry though cuz this is what
- 1:38:20the user will see on the screen and it
- 1:38:21won't affect the actual value of the
- 1:38:23variable if we print out value straight
- 1:38:26after you'll see that the difference
- 1:38:28hasn't been changed it's still 0.3 in
- 1:38:31the original variable so that's one
- 1:38:33method of doing it what we can do now is
- 1:38:35just use controll D and copy it down and
- 1:38:38we can use different formats to show you
- 1:38:39what it looks like if we remove the 0
- 1:38:43and then one of the zeros and leave it
- 1:38:45like this and it gains one decimal place
- 1:38:47each time you'll see what the output
- 1:38:49looks like we just have the 81 which
- 1:38:53essentially makes it into an integer
- 1:38:55because it's a whole number then we have
- 1:38:5781.0 and the reason why it's 0 is cuz
- 1:39:000.3 will round down as it's under
- 1:39:030.5 the 03 is going to round down as
- 1:39:06it's under 05 so it'll round down to
- 1:39:10zero and then finally we have the
- 1:39:1381.4 and just like before this will
- 1:39:15round down
- 1:39:16to4 and just like before this will round
- 1:39:19down to
- 1:39:200.4 this will round down to 0 four let's
- 1:39:24say you wanted to get rid of trailing
- 1:39:26zeros because you don't want them then
- 1:39:28you can actually change this zero to a
- 1:39:30hashtag once you run the hashtag then it
- 1:39:32will trim off any trailing zeros so what
- 1:39:35this means is it will try and put it in
- 1:39:37this format but if this format happens
- 1:39:39to be a zero then it will just keep it
- 1:39:41like this and if you just have 81 dot
- 1:39:44then it'll also get rid of the dot for
- 1:39:45you so then you just left with
- 1:39:4781 it's really up to you as the
- 1:39:50programmer what you want to display
- 1:39:51depending on what context I'm just given
- 1:39:53you the options that you have available
- 1:39:56so let's say we want to work with money
- 1:39:57now and we have a 10d / by a 3D and what
- 1:40:02that value would be is somewhere between
- 1:40:043333 and this will be reoccurring cuz
- 1:40:06it's exactly 1/3 the reason why I've got
- 1:40:09D's in here is because these are
- 1:40:11registering as doubles if you type the
- 1:40:13value in normally it'll get registers at
- 1:40:15in 32 and the problem with in32 is when
- 1:40:18you do this division it will process the
- 1:40:21division as an integer and then store it
- 1:40:23as a a double the issue with that is in
- 1:40:25this case and in this case both values
- 1:40:28will get truncated let me demonstrate if
- 1:40:30I remove the D from both sides and down
- 1:40:33in here it will'll see what the
- 1:40:34difference is and let's print out money
- 1:40:37and let's see what's happened in the
- 1:40:39first case this isn't actually an issue
- 1:40:41because we have a decimal place here
- 1:40:43which means this will be a double so an
- 1:40:45integer divid by double will give us a
- 1:40:47double but we just place the D here just
- 1:40:49so we don't forget and it makes the
- 1:40:51compiler's job a lot easier cuz it
- 1:40:53doesn't have to convert it from an
- 1:40:54integer to a double and in this case we
- 1:40:57have an INT that's divided by an INT so
- 1:41:00we'll get 3.3 reoccurring but because
- 1:41:02it's processing as an integer then we
- 1:41:04just get three being displayed and this
- 1:41:07is not the intended result so let's just
- 1:41:09replace these back to D's and we'll see
- 1:41:10what we get now which is the 3.3 perfect
- 1:41:14so let's say that we want to display
- 1:41:16this as actual currency because you
- 1:41:17never see it displayed like this it's
- 1:41:20always to two decimal places just as
- 1:41:22follows so we can follow the same
- 1:41:24approach as
- 1:41:25above we can do string.
- 1:41:28format 0 callon 0.0 and the curly brace
- 1:41:32and then common money cuz that's the
- 1:41:34variable in reference now if we run that
- 1:41:36let's see what happens
- 1:41:383.33 that's perfect and now what we can
- 1:41:41do in here because this is the pattern
- 1:41:43we can actually type in anything before
- 1:41:45after this and it won't affect the
- 1:41:47pattern just like so we've added some
- 1:41:49Extra Spaces in here so if we get rid of
- 1:41:51these spaces we can actually just play
- 1:41:53is a pound sign in front of it and then
- 1:41:56let's see what happens when we run it
- 1:41:58perfect so now we actually have some
- 1:42:00money and we can say in here £10
- 1:42:05/3 equals this and now our output looks
- 1:42:09a little bit better because we have some
- 1:42:11formatting now the issue is when we
- 1:42:13start getting into negatives let's see
- 1:42:15what happens so we can say -10 IDE by 3
- 1:42:20equals something and when we run we'll
- 1:42:23see that the format is actually
- 1:42:25incorrect now you never see the negative
- 1:42:28symbol after the pound symbol it's
- 1:42:30always this way around in this case what
- 1:42:33we're doing is we're telling the
- 1:42:35compiler hey we only want to see two
- 1:42:37decimal places but always keep in this
- 1:42:40format the pound sign followed by the
- 1:42:42value and the value of money is -3 so
- 1:42:46it'll Place -3 inside here and then add
- 1:42:48the pound sign before it but we don't
- 1:42:50exactly want that so we can add some
- 1:42:53something called a culture info but we
- 1:42:55can also give it some other formatting
- 1:42:57some other way so let's explore some
- 1:42:59other
- 1:43:00options if you use money to string then
- 1:43:03what we're actually doing here is using
- 1:43:05the double variable and converting it to
- 1:43:07a string I me get passed back a string
- 1:43:10representative of this value and that's
- 1:43:13fine and when we run the code you'll see
- 1:43:15no difference between these two at this
- 1:43:17point they're both being converted to
- 1:43:19Strings but what we can do in this case
- 1:43:22is when we open these brackets it tells
- 1:43:23us there one of four what does this mean
- 1:43:26exactly so in our two- string function
- 1:43:28we have four ways that we can call this
- 1:43:30function the first way you give nothing
- 1:43:32in the
- 1:43:35brackets and if we press the down arrow
- 1:43:38the second way is to give it a format
- 1:43:40provider the Third Way is just a string
- 1:43:42format and the fourth way is both a
- 1:43:45format and a provider so we'll use all
- 1:43:47of these in a second but let's explore
- 1:43:49the easiest one if you add a string in
- 1:43:51there which gives us the third overload
- 1:43:53string format and we can just type in a
- 1:43:55Capital C let's just see what
- 1:43:58happens perfect so now we have the best
- 1:44:01format display to us and the reason why
- 1:44:03this works is the C program will
- 1:44:06recognize that the format of C is
- 1:44:08telling us that we want to display
- 1:44:10currency so then it's going to your
- 1:44:12local machine and figuring out where you
- 1:44:14are based on your date and time just
- 1:44:16down here when you have adjust dat and
- 1:44:18time it will figure it out based on this
- 1:44:21we have UTC time and we have the date
- 1:44:23and time listed up here based on that
- 1:44:26information it can figure out that we
- 1:44:27are indeed in the UK and we want to
- 1:44:29display a pound sign and because the
- 1:44:31program knows that it's - 3.33 it'll put
- 1:44:34the pound sign in the right location so
- 1:44:37now that we've explored C let's have a
- 1:44:39look at the other ones that are
- 1:44:40available so if we use contrl D and copy
- 1:44:42this down three times we can use C 0 C1
- 1:44:46and C2 and now if you run this again
- 1:44:48you'll see what the output gets you c
- 1:44:51will get you currency in it most native
- 1:44:53form which is what we're used to using
- 1:44:55in the UK c0 will only return you back
- 1:44:58with the whole number C1 will give you
- 1:45:01one decimal place and C2 mimic C on its
- 1:45:04own as they're displaying the same
- 1:45:06values now this is where it gets
- 1:45:08interesting if you want to display
- 1:45:10different currencies then you're going
- 1:45:11to have to use this thing called a
- 1:45:13culture info which will be able to
- 1:45:15retrieve back different currency formats
- 1:45:17based on a certain country code that you
- 1:45:19give the
- 1:45:20function so let's have a look and see
- 1:45:22how that works if we scroll to the top
- 1:45:24we can add a new library called system.
- 1:45:27globalization inside globalization we
- 1:45:29have a culture info so let's see how we
- 1:45:32can tailor these to make it look a lot
- 1:45:34better if you're worried about different
- 1:45:36currencies so we us CW tab tab and we do
- 1:45:40the same thing money. two string we add
- 1:45:42our C and this is where our format
- 1:45:44provider fits in so we can start type in
- 1:45:47culture info it should come up like this
- 1:45:50press enter and inside this is a class
- 1:45:53that's why it went green if we press dot
- 1:45:56then we get the current culture the
- 1:45:58current culture will decide based on the
- 1:46:00machine that the program is running on
- 1:46:02if this program is running on a cloud
- 1:46:04computer and the cloud computer is
- 1:46:06stored in Europe then it might display
- 1:46:08euros and if it's stored in America then
- 1:46:10it'll display dollars so you want to
- 1:46:12avoid using current culture if you're
- 1:46:14going to scale your program worldwide in
- 1:46:17the case of this example I'm going to
- 1:46:19leave it because as you can see when I
- 1:46:21run it it will display the same thing as
- 1:46:22as the other ones because this one will
- 1:46:25also recognize that my current culture
- 1:46:27is UK and I'm also telling it to use the
- 1:46:30current culture so in both of these
- 1:46:31cases it's figured out that my current
- 1:46:33culture is indeed the UK and it's
- 1:46:36started using the pound sign but let's
- 1:46:38say we want to give it a different
- 1:46:39culture then what we can do if we
- 1:46:41duplicate this line using contr D and
- 1:46:44remove this section just after the dot
- 1:46:47we can actually say create specific
- 1:46:49culture and inside the parentheses we
- 1:46:52can actually add a string and it says
- 1:46:54here a predefined culture name or the
- 1:46:56name of existing culture info object the
- 1:46:58name is not case sensitive so in the
- 1:47:01case of the UK you have English GB and
- 1:47:05now we need another bracket at the end
- 1:47:06and then if we use control D to copy
- 1:47:08this down we can change GB to us let's
- 1:47:12run this again and see what
- 1:47:16happens these two don't actually output
- 1:47:18anything different because we're just
- 1:47:20doing the normal culture and then we're
- 1:47:22telling it to find the GB culture which
- 1:47:24is exactly the same thing for me so we
- 1:47:26get these two values exactly the same
- 1:47:28and apparently it's picking up that in
- 1:47:30the US you have brackets around your
- 1:47:32negative values which I can't personally
- 1:47:34validate but this is how it's showing up
- 1:47:37so let's try some other cultures and see
- 1:47:39what happens so you can try Au for
- 1:47:43Australian then you can see that they
- 1:47:45actually use a negative followed by the
- 1:47:47pound sign and then the value so even
- 1:47:50though they're both in dollars one's US
- 1:47:52Dollars and one is Australian dollars
- 1:47:54and apparently the US uses brackets for
- 1:47:56the negative values but Australians use
- 1:47:58the negative value like we do so let's
- 1:48:01just have a quick recap so we can
- 1:48:03display the value just like this but
- 1:48:05we'll have a problem with multiple
- 1:48:06decimal places we can use these format
- 1:48:09options to give us zero decimal places
- 1:48:12one or two decimal places and then when
- 1:48:14we use this format the problem with it
- 1:48:17was when you display negative numbers
- 1:48:19you get a nasty format where the
- 1:48:21negative value actually comes after the
- 1:48:23pound sign which is not correct and you
- 1:48:25don't want to Output that to your users
- 1:48:27instead you should use the C format to
- 1:48:29tell the compiler that you want to
- 1:48:31process this as a currency currency
- 1:48:33processing is better because the
- 1:48:35software will understand that the value
- 1:48:37you're trying to represent depends on a
- 1:48:38certain strict format just as follows
- 1:48:41you don't want the negative to be here
- 1:48:43instead of before it because that
- 1:48:45doesn't look very nice to the end user
- 1:48:47alternatively you could use this C info
- 1:48:50if you're going to scale your program
- 1:48:52scale your program means that it's going
- 1:48:54to live on servers and this culture info
- 1:48:57especially the current culture depends
- 1:48:59on where the program is being run on
- 1:49:01because it depends on the system time
- 1:49:03different servers will have different
- 1:49:05dat times set because they'll be in
- 1:49:07different countries so you want to be
- 1:49:09careful and if you want to display in a
- 1:49:10certain currency you can create a
- 1:49:12culture specification and pass the
- 1:49:15country code inside the brackets and
- 1:49:17then you'll get the correct output and
- 1:49:19it doesn't matter where your program
- 1:49:20lives if you always want to gener
- 1:49:22generate in pound symbols then you
- 1:49:24always use create specific culture and
- 1:49:27always specify
- 1:49:29NGB upon doing so you always get the
- 1:49:31pound sign and it doesn't matter where
- 1:49:33your program
- 1:49:36lives so throughout this course you've
- 1:49:38seen this notation a lot print out value
- 1:49:41using console right to give the user an
- 1:49:43opportunity to write on the same line
- 1:49:45reading their value as a string and
- 1:49:47store it in a variable and then use that
- 1:49:49variable to convert it to an integer 32
- 1:49:52and we're passing the string input
- 1:49:54inside here and then we convert it to an
- 1:49:56integer if we run the code you can see
- 1:49:58that this actually works perfectly
- 1:50:00assuming you actually enter a real
- 1:50:02number like 10 we press enter and it
- 1:50:05works and it outputs back to the screen
- 1:50:07using this console right line and if you
- 1:50:09press this restart button we can start
- 1:50:11the program again and let's say we
- 1:50:12accidentally type a character like L and
- 1:50:15we press enter and we've get a format
- 1:50:17exception now this is problematic
- 1:50:20because our program will now close if we
- 1:50:22press continue it doesn't even hit this
- 1:50:24line or this as soon as it hits the
- 1:50:26exception here our program just closes
- 1:50:29if we use debug start with debugging and
- 1:50:31now we're actually running the program
- 1:50:33outside of visual studio so we won't get
- 1:50:35that pop up when the error happens let's
- 1:50:37see what happens if we type this in now
- 1:50:39we can say 10p and press enter and now
- 1:50:42we get that the program has stopped
- 1:50:43working you've probably seen this pop up
- 1:50:45a lot throughout Windows in different
- 1:50:47versions of Windows because when a
- 1:50:49program has a fatal error or an
- 1:50:50exception you see there's pop up appear
- 1:50:53here and the only thing you can do is
- 1:50:54either debug or close it normally the
- 1:50:57debug option isn't available but because
- 1:50:59this is a c program and it's made
- 1:51:01through Visual Studio and the visual
- 1:51:02studio is being installed on the system
- 1:51:04then it gives us the option but really
- 1:51:07all we can do here is just terminate the
- 1:51:09program it doesn't matter what happened
- 1:51:11because we can't recover this if we
- 1:51:13press close then it's literally just
- 1:51:15going to close down it's going to tell
- 1:51:16us the exception and then press any key
- 1:51:18to continue and as soon as you press any
- 1:51:20key then the program just closes so this
- 1:51:23is very fatal for our code since if we
- 1:51:25had some precious data stored in here or
- 1:51:27anywhere else in the program it is now
- 1:51:29lost before we can do anything this is
- 1:51:32problematic and we want to avoid this
- 1:51:34the best way to do this is to use
- 1:51:36something called try pass right here the
- 1:51:39program is assuming that your string
- 1:51:41input is a perfect input and then you're
- 1:51:43immediately converting it to an integer
- 1:51:46now like I said what if this had 10 H in
- 1:51:49it the program is going to cause an
- 1:51:51exception and then the program is going
- 1:51:52to close so let's see what we can do
- 1:51:54with this trass function so the notation
- 1:51:57is int.
- 1:51:59trass the first parameter is a string s
- 1:52:02string s is a string containing a number
- 1:52:05to convert so what this trip pass
- 1:52:07function will do as it says here
- 1:52:09converts the string representation of a
- 1:52:11number to its 32 bits signed integer
- 1:52:14equivalent a return value indicates
- 1:52:16whether the conversion succeeded okay
- 1:52:18perfect so we'll get into that in a
- 1:52:20second so our string s is a string
- 1:52:22containing our number to convert and in
- 1:52:25this case we want to be using the num
- 1:52:26input so we copy and paste that down and
- 1:52:29then we add a comma cuz we have more
- 1:52:31than one parameter now we have an out in
- 1:52:35result when this method returns result
- 1:52:38which is our integer contains the 32bit
- 1:52:41sign integer equivalent of the number
- 1:52:43contained in S and S in this case is our
- 1:52:45first parameter which is num input if
- 1:52:48the conversion succeeded then we get the
- 1:52:51value inside our result
- 1:52:52or zero if the conversion failed the
- 1:52:55conversion fails if the parameter s is
- 1:52:57null or string empty is not of the
- 1:53:00correct format or it represents a number
- 1:53:02that's less or more than the maximum
- 1:53:04value that the integer can store this
- 1:53:07paramet is past uninitialize any value
- 1:53:09originally supplies in result will be
- 1:53:11overwritten let's dive in to see what
- 1:53:13this means because it's an out in result
- 1:53:17what this means is we can actually
- 1:53:18process a variable inside here like intn
- 1:53:21so we can say out num and what this will
- 1:53:24do is when this line executes it will
- 1:53:26try and convert num input and place the
- 1:53:28value inside num so we don't need to run
- 1:53:31this convert anymore we can just specify
- 1:53:33that num is equal to zero so we've read
- 1:53:36in the string that contains our 10 H and
- 1:53:39we have our Nome that contains zero
- 1:53:42let's just remove this console right
- 1:53:44line so now we have our variable set up
- 1:53:47let's see what happens if we do a
- 1:53:49console right line and just print num
- 1:53:51after this let's see what happens to the
- 1:53:53code so if we type in 10 H and press
- 1:53:56enter the value of num is zero but what
- 1:53:59you notice now the program hasn't closed
- 1:54:01because it's tried to pass it instead of
- 1:54:05just passing it automatically and then
- 1:54:06cause an exception so the reason why Try
- 1:54:10pass is called try pass is first it
- 1:54:12tries and then if it succeeds this will
- 1:54:15contain the value and if it doesn't
- 1:54:17succeed this will contain zero to know
- 1:54:19if the number has actually been
- 1:54:21converted we actually have to use the
- 1:54:23bull return value so what this means is
- 1:54:26because it returns us back a bull we can
- 1:54:28store it inside a bull and we can give
- 1:54:30it the name of success bu success equals
- 1:54:33in. trass we try and pass the num input
- 1:54:36and we place the value inside num the
- 1:54:39reason why having this bu success is
- 1:54:41very very essential let's say for
- 1:54:43example the user actually enters
- 1:54:46zero so technically as of this point num
- 1:54:50zero is actually correct because we've '
- 1:54:52got zero and we initialize it to zero
- 1:54:54but of course after the trip pass runs
- 1:54:57this is still going to be zero now we
- 1:54:59don't know if the value zero is because
- 1:55:02this has succeeded or the value is zero
- 1:55:05because we initialized it to zero let me
- 1:55:07demonstrate if I type in a zero here is
- 1:55:11this zero being converted because this
- 1:55:13was successful or is it presenting me
- 1:55:16zero because this has been initialized
- 1:55:18to zero and the only way we can know is
- 1:55:21because our tripod pass will return us
- 1:55:23back with true if s was converted
- 1:55:26successfully otherwise false we can
- 1:55:29actually write an if statement in here
- 1:55:31if success open the curly braces and we
- 1:55:34can start to type something in here so
- 1:55:36we can take our console right line and
- 1:55:38place it up here using alt and up Arrow
- 1:55:41key and then in the lse we can say
- 1:55:44failed to
- 1:55:47convert so now we have a mini setup here
- 1:55:51if it's been success uccessful we print
- 1:55:53out the number if it hasn't been
- 1:55:55successful then it'll go into this path
- 1:55:57and it'll print fail to convert let's
- 1:55:59have a look and see how this works now
- 1:56:01if I press zero and I see Zero that
- 1:56:04means it's followed down the success
- 1:56:06path and this zero actually means it's
- 1:56:09converted my input instead of just the
- 1:56:11value that it's being initialized by if
- 1:56:13you run the code again and type in 10 H
- 1:56:17then we actually see it says fail to
- 1:56:19convert and why that happened is we have
- 1:56:2110 H appear and then this will return
- 1:56:24back false when it tries to convert num
- 1:56:27input and then after it hits this line
- 1:56:29since success will be false it will
- 1:56:32Dodge this path going into this path and
- 1:56:34output fail to convert what you can do
- 1:56:37if you really wanted to is you could
- 1:56:39wrap this around in a while loop so
- 1:56:41let's see what we can do so we can
- 1:56:43specify a success and make that equal to
- 1:56:47true and then we can add a while loop
- 1:56:49with success in there and then open the
- 1:56:51curly braces and put all of this code
- 1:56:53inside here now we're going to have some
- 1:56:55errors because we got success twice but
- 1:56:58what we can do which will be a lot
- 1:57:00easier is we can place this straight
- 1:57:03inside our bracket and the reason why is
- 1:57:06because this returns us back at booing
- 1:57:08so we don't need to store it and then
- 1:57:10use it another thing we can do is
- 1:57:12instead of having Inn and then num right
- 1:57:15here you can actually make this a
- 1:57:17declaration as well so if we just remove
- 1:57:20this value we can write out
- 1:57:22in n and this will do exactly the same
- 1:57:25thing it just uses less variables while
- 1:57:28our success is true we come into here
- 1:57:31and run this code if we failed to
- 1:57:33convert then we still want it to be true
- 1:57:35so we can loop around if we've converted
- 1:57:38it and the value is successful then we
- 1:57:40can actually make our success at this
- 1:57:43point false cuz that will stop the W
- 1:57:46loop from going around let's run the
- 1:57:48code we can type in 10 H 10 a 10 a etc
- 1:57:53etc and as soon as we type in a real
- 1:57:55number then it will print it out and the
- 1:57:57while loop will cancel and now right
- 1:57:59here we're start on the console readline
- 1:58:02now if we press enter the program will
- 1:58:03close so let's have a
- 1:58:06recap we use inter. trass so we can try
- 1:58:10and pass a value before the program
- 1:58:12assumes that the value is in the correct
- 1:58:13format we give it a string variable and
- 1:58:16it returns us back with a fresh new
- 1:58:18integer called num or whatever you want
- 1:58:20to call it inside here we we can place
- 1:58:22this straight inside the condition of an
- 1:58:24if statement because this function
- 1:58:26returns us back with a true or false
- 1:58:27value otherwise known as a bull if this
- 1:58:30has been successful we stop the while
- 1:58:32loop and print the value out to the
- 1:58:34screen and if it hasn't been successful
- 1:58:37then we print fail to convert and
- 1:58:39because our success is still true it
- 1:58:41will Lo back around to the start of the
- 1:58:43while loop and ask the user to input
- 1:58:46another value to try and convert this
- 1:58:49while loop will only exit if it's indeed
- 1:58:51been successful ful and the only way it
- 1:58:53can exit is by having the success being
- 1:58:56equal to false and the only way that can
- 1:58:58happen is by having a successful
- 1:59:01conversion the use of trass in every
- 1:59:04program is very essential because if you
- 1:59:06have a faat exception then your program
- 1:59:08will close and you will lose all the
- 1:59:10data the problem with that is you might
- 1:59:12lose anything that you haven't processed
- 1:59:14yet at that given point in your program
- 1:59:16so you want to try and use triy pass
- 1:59:18because you'll avoid any potential
- 1:59:20exceptions and you can let the user know
- 1:59:22if the conversion has been successful or
- 1:59:24not and write yourself the same while
- 1:59:26loop just like this so you can tell the
- 1:59:28user hey you inputed it incorrectly but
- 1:59:30this is how to do it right and make sure
- 1:59:33that your program doesn't execute
- 1:59:35incorrectly and make sure your program
- 1:59:37doesn't just close unexpectedly and then
- 1:59:39an error will be
- 1:59:42caused in this exercise I want you to
- 1:59:44print out some sort of times table and
- 1:59:47what I mean by this is you want to ask
- 1:59:48the user to input a number for the table
- 1:59:51and then write WR a for Loop to print
- 1:59:53out the x times table X is depend on the
- 1:59:56value that they entered if they enter 10
- 1:59:58in here then this will be the 10 times
- 2:00:00table let's get started so we can have
- 2:00:04our normal console.
- 2:00:06write enter a
- 2:00:08number and then use our int number
- 2:00:11equals to convert to int32 and we can
- 2:00:15say console. read line because we want
- 2:00:16to read whatever the user types in then
- 2:00:19we can have a four in I equals z i is is
- 2:00:22less than 10 we're only going to print
- 2:00:24up to the 10 * table so 1 * Something 2
- 2:00:28* something all the way up to 10 times
- 2:00:30the value that they
- 2:00:32entered immediately we acknowledge an
- 2:00:34error here when you do your 10 times
- 2:00:36table for example you do 1 * 10 2 * 10
- 2:00:40and so on and so forth the issue here is
- 2:00:43we're going from 0 to 10 but because
- 2:00:46it's less than 10 we're actually going
- 2:00:47from 0 to 9 so let's change this to 1
- 2:00:50and then less than or equal to 10 10 in
- 2:00:52both cases we were actually looping the
- 2:00:55same amount of times the issue is this
- 2:00:57was starting on one and the other one
- 2:01:00was starting on zero and the first one
- 2:01:02was finishing on nine and this one's
- 2:01:04finishing on 10 and we want them to go
- 2:01:06from 1 till 10 instead of from 0 till 9
- 2:01:10even though that's the same amount of
- 2:01:11iterations it gives us different
- 2:01:14values so in here we can just have a
- 2:01:16console right line and this is how it's
- 2:01:18going to work we're going to have our
- 2:01:20first number times about our second
- 2:01:22number and that's going to be equal to
- 2:01:24something so our first number is our
- 2:01:27value of I cuz that will say 1 *
- 2:01:29Something 2 * something and so forth our
- 2:01:33second number is whatever the user has
- 2:01:35typed into the calculation which will be
- 2:01:38number and our third number is going to
- 2:01:40be the actual result of the calculation
- 2:01:43which are these two times together so we
- 2:01:45can have I star number and that should
- 2:01:48be it let's run it and give it a shot
- 2:01:52we can do our 10 times table and if we
- 2:01:55press enter you can see that it's fully
- 2:01:57working this will be our I variable
- 2:01:59going down the side the second number is
- 2:02:01what the user typed in and the third
- 2:02:04number is this multiplied by this gives
- 2:02:06you the output let's run it again with a
- 2:02:09bigger number and it seems to handle
- 2:02:12okay and because this is an integer
- 2:02:14you'll expect to work okay up until
- 2:02:16around 2.1 billion which is a big number
- 2:02:19for some time tables anyway so just just
- 2:02:22a quick recap we looped through from 1
- 2:02:24till 10 instead of from 0 till 9 because
- 2:02:27we made sure that when we do our
- 2:02:29multiplications we don't want to start
- 2:02:31the value at 0er * something we want to
- 2:02:33start the first one at 1 * something and
- 2:02:35we don't want to end at 9 we want to end
- 2:02:37at 10 so using 1 and less than or equal
- 2:02:40to 10 will get us the values that go
- 2:02:42from 1 till 10 and that will make our
- 2:02:45output look a lot more concise we're
- 2:02:47using conditional format in here adding
- 2:02:49the zero and curly braces and then the
- 2:02:51one and then the two and in order to
- 2:02:54insert the values in it's a comma
- 2:02:55delimited list after the speech marks
- 2:02:58the first one goes in the right order
- 2:03:00zero is I one is number two is I times
- 2:03:04the number running that code we'll get
- 2:03:07this output there you
- 2:03:11go so in this exercise I want you to
- 2:03:13have a look and see if you can make the
- 2:03:15Fizz bus game fiz buuz game is one of
- 2:03:18the most popular interview questions
- 2:03:19you'll come across
- 2:03:21and the way to do this is to create a
- 2:03:23for loop from one to something in this
- 2:03:25case I'm just going to use 15 and inside
- 2:03:27the for Loop you want to decide if the
- 2:03:29numbers divisible by three and five then
- 2:03:31you want to print Fizz buzz if it's
- 2:03:33divisible by three then it's Fizz five
- 2:03:36it's buzz and if it doesn't fall into
- 2:03:38any of them categories then you want to
- 2:03:40simply just print the number out to the
- 2:03:41screen let's get started so we can have
- 2:03:44a four and because we know how many we
- 2:03:46want to do we want to do in I less than
- 2:03:5015 i++
- 2:03:53and then inside here we need to use the
- 2:03:55modulus operator which is the percentage
- 2:03:57sign so if you remember from the
- 2:03:59tutorial in order to figure out if a
- 2:04:01number is odd or even you could do the
- 2:04:03number modulus 2 and if that value is
- 2:04:06one then it must mean it's odd and if
- 2:04:09that value is zero it must mean it's
- 2:04:11even but what we can also differentiate
- 2:04:14from here is if 10 modulus 2 equals 0
- 2:04:17then that means that 10 is divisible by
- 2:04:202 but also it is even and if it's even
- 2:04:24that means it's divisible by two if it's
- 2:04:26not divisible by two that means it's not
- 2:04:28even and therefore it's odd so in this
- 2:04:32case if we try to divide three and five
- 2:04:34we can have the same calculation but we
- 2:04:37can work out differently so 10 modulus 3
- 2:04:40actually gives us a value of one because
- 2:04:42it's three remainder one so if this
- 2:04:44value is anything other than zero then
- 2:04:47we know that these two were not
- 2:04:48divisible by each other 9 percentage 3
- 2:04:51equal 0 which means it's divisible by it
- 2:04:54so in our if statement we can say if I
- 2:04:57which is the number that we're currently
- 2:04:58up to
- 2:05:00percentage 3 equals equals to zero and
- 2:05:04because we want to check for five as
- 2:05:06well we want to say and I percentage 5
- 2:05:09equal equals to Zer and that means
- 2:05:11they're both divisible by each other so
- 2:05:14we can do a console right line and we
- 2:05:15can say fiz buzz and then in our lse if
- 2:05:19we're going to check if it's three and
- 2:05:21we can repurpose this exact condition
- 2:05:23because this just checks if it's
- 2:05:25divisible by three and then we can print
- 2:05:28Fizz and then if we copy this exact
- 2:05:30structure down and change this to a five
- 2:05:32to check if it's divisible by five then
- 2:05:34we can print out buzz and in the worst
- 2:05:37case scenario where none of them get
- 2:05:39applied then we can simply print I to
- 2:05:41the screen and actually there's no point
- 2:05:44in starting at zero because normally
- 2:05:46these start at one so we'll just do one
- 2:05:48and we can make it 15 by making it less
- 2:05:50than or equal to so instead of before it
- 2:05:53was actually going from 0 to 14 now it's
- 2:05:55going from 1 to 15 and they will tell
- 2:05:58you this in the interview they might say
- 2:06:00counting from 1 to 100 print out all of
- 2:06:03the Fizz buzzers so let's run the code
- 2:06:05and see what's going
- 2:06:07on one two fizz fizz is actually three
- 2:06:11so three is divisible by three then you
- 2:06:13have four and then five is divisible by
- 2:06:16five so you get buzz and six goes into
- 2:06:19three so you get Fizz nine you get Fizz
- 2:06:22because it's divisible by three then 10
- 2:06:24/ 5 gives you buzz 11 12 goes into three
- 2:06:28so you've got Fizz and finally when you
- 2:06:30hit 15 it goes into three and goes into
- 2:06:34five so we get Fizz buzz and the way to
- 2:06:36increase this to 100 is by simply
- 2:06:38changing this to 100 if you need to and
- 2:06:41you can scroll through the entire input
- 2:06:42and you can see all the history if you
- 2:06:44wanted to make this slightly efficient
- 2:06:46what you could actually do is store the
- 2:06:48Boolean values for these calculations
- 2:06:50before you can store the values for
- 2:06:52these calculations before so we can say
- 2:06:56bu 3 div equals false and bull 5 div
- 2:07:01equals false and what we can do at the
- 2:07:04start of each of the for Loop is we can
- 2:07:05say three div equals this value and five
- 2:07:09div also equals this
- 2:07:12value and then every time we find a
- 2:07:15calculation for three or calculation for
- 2:07:18five we can replace it with either three
- 2:07:20div or five div so let's change this one
- 2:07:23here and this one here and our five here
- 2:07:28and here and because these are values
- 2:07:31now we want to actually store them as
- 2:07:33true or falses so we don't need to check
- 2:07:35if they're equal to zero so we can just
- 2:07:37remove them so we can say if three div
- 2:07:40and five div are both true and they'll
- 2:07:42only be true if these two are both zero
- 2:07:45now if we run the code and just make it
- 2:07:4715 again just we have a shorter output
- 2:07:50you can see that our code is exact the
- 2:07:52same the reason why this is more
- 2:07:54efficient is we're only doing this
- 2:07:55calculation twice whereas before we were
- 2:07:58doing it twice for three and twice for
- 2:08:00five now we're doing it once storing it
- 2:08:04and then making use of it this is a lot
- 2:08:06more memory efficient than having to
- 2:08:07calculate it every single time it
- 2:08:09computes a lot faster and it's better
- 2:08:11for readability if the rules of the game
- 2:08:14happen to change and this was divisible
- 2:08:15by four or six then you could just
- 2:08:18change it very easily whereas in the
- 2:08:20previous example you would have to
- 2:08:22change in every single
- 2:08:24case in C when you're constructing
- 2:08:27strings that have backs slashes you're
- 2:08:30going to need to use double backslash
- 2:08:32and the reason for this is the first
- 2:08:34backslash is an indication that it's
- 2:08:36actually going to be an escape character
- 2:08:38you're going to put an escape character
- 2:08:40in and then put the character that you
- 2:08:42want to display and the reason for that
- 2:08:44is they've chose backslash as the
- 2:08:46designated character so you can use
- 2:08:49backslash and then a backs slash to to
- 2:08:51show one backslash only and what this
- 2:08:54means is it will use the first backslash
- 2:08:56as a reference so it knows that it's
- 2:08:59going to process the next character as
- 2:09:01an escape character and what these
- 2:09:03Escape characters are you can have
- 2:09:05things like T for tab and for new line
- 2:09:09and you can even use speech marks
- 2:09:12because if you outputting some dialogue
- 2:09:14and you wanted to say something like
- 2:09:16this he
- 2:09:18said and then you added your own quote
- 2:09:21in there if you want to put something in
- 2:09:24quotes then you can't because the speech
- 2:09:26mark dictates that this is the end of
- 2:09:28the string literal that's why it's cting
- 2:09:31it off like this and then now it's not
- 2:09:32recognizing that something is just WR
- 2:09:34here and what it's actually doing is
- 2:09:37you've got two strings specified one
- 2:09:39that says he said space and one that's
- 2:09:42just empty and then you just have this
- 2:09:44something that's just in the middle of
- 2:09:46course we actually want to insert the
- 2:09:48speech marks within another pair of
- 2:09:50speech marks marks this is where the
- 2:09:52backslash comes in so you can insert a
- 2:09:54backslash before here and this is now
- 2:09:57acceptable and the same thing with the
- 2:09:59normal backslash because the backslash
- 2:10:02character is also going to be the Escape
- 2:10:04character then you need a double
- 2:10:06backslash so then that equals to a
- 2:10:08single backslash when you print it out
- 2:10:11let's Show an example so we have C colon
- 2:10:14sl/ slash which will return us back a
- 2:10:16single slash but visually right now it's
- 2:10:19a double slash but when it gets
- 2:10:21processed or outputed to the screen
- 2:10:23you'll see that it's a single slash so
- 2:10:25this common example is when you're
- 2:10:27constructing a path together and you
- 2:10:29want to use backslashes to denote the
- 2:10:31different directories so let's output
- 2:10:34this to the screen and see what
- 2:10:36happens and as you can see we have a
- 2:10:38single backslash because of the Escape
- 2:10:40character we don't see both of them and
- 2:10:43if we print the speech out to the screen
- 2:10:45just to show you that as well then we
- 2:10:47can see that there's only the speech
- 2:10:48marks around the something and we don't
- 2:10:50have the backslash you can tell when an
- 2:10:53escape character is being used because
- 2:10:55it goes a slight hint of yellow instead
- 2:10:58of the normal color for a string so now
- 2:11:00that we've worked out that this is how
- 2:11:02this works let's see a way that we can
- 2:11:05undo this to make it easier for us so
- 2:11:08you've used the dollar sign for string
- 2:11:09interpolation and you've used the plus
- 2:11:12just to concatenate two variables
- 2:11:14together and just as a quick example it
- 2:11:16looked something like this your name is
- 2:11:19and then the name variable and using the
- 2:11:22plus you would say your name is followed
- 2:11:25by a space and the quotation marks and
- 2:11:27do plus name that's fine and now we have
- 2:11:30another one so similar to how we place a
- 2:11:33dollar sign before the speech marks we
- 2:11:35have a verbum identifier and what this
- 2:11:38means because it says verbatim it just
- 2:11:40means whatever you see is whatever
- 2:11:42happens so let's try and Define this
- 2:11:44path a slightly different way but still
- 2:11:47containing the exact same information
- 2:11:49that you see when you print out to the
- 2:11:50screen
- 2:11:52so let's have a look we can reassign the
- 2:11:55path value and just before the speech
- 2:11:57marks we can put it at symbol and this
- 2:12:00at symbol means it will actually negate
- 2:12:03all of the Escape characters it will
- 2:12:05process a single backlash as a single
- 2:12:08backslash let's take this line of code
- 2:12:11and place inside here now what you see
- 2:12:14is that tint of yellow is no longer
- 2:12:16there so if we print this past to the
- 2:12:18screen let's see what
- 2:12:20happens we actually get the double
- 2:12:22backslash because it's ignoring all the
- 2:12:24Escape characters which means if we take
- 2:12:26them all out this actually looks exactly
- 2:12:29how we want and you don't have to worry
- 2:12:31about the Escape
- 2:12:33characters there you
- 2:12:37go these two are identical even though
- 2:12:40one is being displayed using Escape
- 2:12:41characters and one's using the verbatim
- 2:12:44identifier please note that if you're
- 2:12:46going to use the back sln for example
- 2:12:49and I'll just demonstrate at the end of
- 2:12:50this new line test and if we run that
- 2:12:53code again you'll see that it's printed
- 2:12:55the path on one line the back sln has
- 2:12:58made it go on to the next line and now
- 2:13:00it's printing new line test on a
- 2:13:02dedicated line that's fine the problem
- 2:13:06is if you use a back sln or a back SLT
- 2:13:09or any of these Escape characters during
- 2:13:12the at symbol then it's going to process
- 2:13:14it as literal characters which means you
- 2:13:16can't mix a back sln with an at symbol
- 2:13:20AS this is just taking the text verbatim
- 2:13:23so if we try and run the code now you'll
- 2:13:24see the back sln will actually appear in
- 2:13:27the code if you wanted to do this then
- 2:13:30maybe you could remove the back sln and
- 2:13:32add a plus at the end and then make
- 2:13:34another string that just has a back
- 2:13:36slash n and then this string will not be
- 2:13:39processed as a Verbatim string because
- 2:13:41we don't have the at symbol right before
- 2:13:44it so then we can have the exact same
- 2:13:46thing here just to show you
- 2:13:49this so now the these two are identical
- 2:13:52again just as shown here and as shown
- 2:13:56here the difference being that we can't
- 2:13:59specify a back sln in the same string we
- 2:14:01actually have to concatenate them
- 2:14:03together because of the verbatim
- 2:14:05identifier so let's have a look at
- 2:14:07speech
- 2:14:09marks so if we have a string name and we
- 2:14:12can make this equal to hello and then
- 2:14:15someone and we said before that we have
- 2:14:18to use the backslash so we can activate
- 2:14:20the Escape characters and that will work
- 2:14:22and if we just print that to the screen
- 2:14:24to make sure then we get hello someone
- 2:14:26in the speech marks if we add the verbum
- 2:14:29identify at the start then we see we've
- 2:14:31got an error it's trying to process it
- 2:14:35differently this is a little Quirk with
- 2:14:37the verbatim identifier if you want to
- 2:14:39insert a speech marks you actually want
- 2:14:41a double speech mark on either side the
- 2:14:44double speech marks will get processed
- 2:14:46as a single speech mark because it's
- 2:14:48within a speech mark the verb identifier
- 2:14:51needs to know that you're specifying a
- 2:14:53speech mark and this acts as the same
- 2:14:56reason why you have a backslash here
- 2:14:58it's slightly quirky doing it this way
- 2:15:00but it still works as per you
- 2:15:02expect and when you're specifying
- 2:15:05something like name and we can say hello
- 2:15:08someone so let's print name to the
- 2:15:09screen and make sure it works in this
- 2:15:12case we don't actually need to specify
- 2:15:13any sort of Escape character because the
- 2:15:16quotation mark is a separate character
- 2:15:18to the speech mark and it doesn't
- 2:15:20interrupt anything by placing a
- 2:15:22quotation mark inside the string just
- 2:15:24represents that as its own character
- 2:15:26let's run that and make sure it works
- 2:15:28there you go so just as a quick recap
- 2:15:32Escape characters are very essential
- 2:15:34because we can use things like back SLT
- 2:15:36to give us a tab character in our
- 2:15:38console output this can be very helpful
- 2:15:40cuz you can almost mimic the use of
- 2:15:42columns inside our console output you'll
- 2:15:45notice that back sln is used quite a lot
- 2:15:48in the console output so you can
- 2:15:49separate lines without having to use
- 2:15:51multiple console right
- 2:15:53lines if you have a single backslash you
- 2:15:56can't have it on its own if the string
- 2:15:58is not a Verbatim string you have to
- 2:16:01pair it with another character otherwise
- 2:16:03you will get a syntax error if you want
- 2:16:05to use the verbatim string then you can
- 2:16:07put an add symbol before the string
- 2:16:09definition and then it will ignore all
- 2:16:11of the Escape characters for example
- 2:16:14using backs sln Within These speech
- 2:16:16marks will mean that the back sln will
- 2:16:18literally be printed to the screen and
- 2:16:20you won't get get the new line
- 2:16:21functionality that backlash M provides
- 2:16:24when you want to print speech marks to
- 2:16:26the screen using the verbatim identifier
- 2:16:28you have to use a double speech marks in
- 2:16:30order for it to show up and as you can
- 2:16:32see it slightly goes yellow and that's
- 2:16:34the similar notation as using Escape
- 2:16:36character when you don't have an a
- 2:16:38Verbatim string if you're using other
- 2:16:40characters that are similar to a speech
- 2:16:42mark like a quotation mark because this
- 2:16:45doesn't interrupt the syntax of the
- 2:16:47language then this is fine because a
- 2:16:49character can just be in inside a string
- 2:16:52and to the compiler this is just a
- 2:16:54character even though they are very
- 2:16:55similar the quotation mark doesn't
- 2:16:58affect the speech
- 2:17:01mark the most common approach for
- 2:17:03outputting values to the console is by
- 2:17:05having this format you type something in
- 2:17:08as a static string and then you add a
- 2:17:10variable to it but when you're doing
- 2:17:12long sentences this format can get messy
- 2:17:15quite quickly so this will just output
- 2:17:17both of these on separate lines the name
- 2:17:20follow follow by the name variable and
- 2:17:22the age followed by the age variable I
- 2:17:25just have a console right line to split
- 2:17:26up these two sections just so you can
- 2:17:28see it visually in the console so if we
- 2:17:30run this code you can see that both the
- 2:17:33outputs are exactly the same and the
- 2:17:36reason why is we can use this back sln
- 2:17:39what this back sln is doing is inserting
- 2:17:41a new line character just where my
- 2:17:43cursor is here which is forcing the age
- 2:17:46to jump onto the next line the reason
- 2:17:48why this is a slightly different shade
- 2:17:50to the age text is because it's an
- 2:17:52Escape character back sln means new line
- 2:17:56we'll explain this in a further video in
- 2:17:58this section so don't worry about that
- 2:18:00just know that it means that it produces
- 2:18:02a new line so as you can see since we're
- 2:18:04adding the name followed by the name and
- 2:18:06then the age followed by the age then
- 2:18:08it's getting a bit long and a little bit
- 2:18:10tedious it's not too bad in this case
- 2:18:13because we're not really printing out a
- 2:18:14sentence we're just going straight to
- 2:18:16another line but let's see what this is
- 2:18:18going to look like if we start dealing
- 2:18:20with more multiple pits of data your
- 2:18:23name is followed by the name and then we
- 2:18:26can add a comma and your age is and then
- 2:18:31add the age and as you can see while
- 2:18:33we're structuring a sentence then it
- 2:18:35gets a little bit long we have to
- 2:18:37remember to put in the spaces otherwise
- 2:18:40our code is going to look funky when we
- 2:18:42print out to the screen and the reason
- 2:18:44for that is there's no spaces between
- 2:18:46the name and the age variables so let's
- 2:18:49put them back in and make sure the
- 2:18:51output looks all
- 2:18:53right now that the output looks like
- 2:18:55this let's explore composite formatting
- 2:18:57and what this means is we can get rid of
- 2:18:59all of these plus symbols and replace it
- 2:19:02with curly braces so let's actually
- 2:19:04output this string again but just output
- 2:19:06it completely static your name is
- 2:19:09abber and your age is
- 2:19:1223 and now if we've run this you'll see
- 2:19:15that both outputs are identical this is
- 2:19:18because I know the values for the string
- 2:19:19variables appear here and I can input it
- 2:19:22straight into here so now that we know
- 2:19:24what our sentence is going to be we
- 2:19:26don't need to add any pluses what we do
- 2:19:28need to do is remove our variables so in
- 2:19:31the case of this sentence our name
- 2:19:33variable is here and our age variable is
- 2:19:35here so we can replace this with this
- 2:19:38curly brace notation we can have curly
- 2:19:41braces and then we want to start this
- 2:19:43value at
- 2:19:44zero throughout the string you want to
- 2:19:47have these values starting from zero and
- 2:19:50going up incrementally from left to
- 2:19:52right so our next variable would be our
- 2:19:5523 and we can store it like this now
- 2:19:59that it's got your name zero and your
- 2:20:01age one let's see what happens when you
- 2:20:03output it then we just get this and this
- 2:20:05is very confusing that's because we're
- 2:20:08not done yet so now that we've got our
- 2:20:10string typed in we can actually place a
- 2:20:13comma in here and now it lets us put any
- 2:20:16object in and any object could be a
- 2:20:18string an in a double or any data type
- 2:20:21so we want to insert the values inside
- 2:20:24here going from left to right so our
- 2:20:26name is first and then we do a comma and
- 2:20:29we type in Age and what this now says is
- 2:20:32the name will get replaced by the curly
- 2:20:34brace zero and the age will get replaced
- 2:20:37by the curly brace one so now if we run
- 2:20:40the code let's see what happens and now
- 2:20:42you'll see that the output is exactly
- 2:20:44the same and now look at the difference
- 2:20:46between these two lines you'll see that
- 2:20:49these multiple amount of Plus es and not
- 2:20:51confusing and let's say you forget this
- 2:20:53one then you're going to get a syntax
- 2:20:55error and it's a bit frustrating to keep
- 2:20:57on typing when you're trying to join
- 2:20:59things together now we can clearly see
- 2:21:01that this Flo is like a sentence and
- 2:21:03this space here looks very unnatural but
- 2:21:06this space here looks like it's part of
- 2:21:08the sentence which makes our code a bit
- 2:21:10more
- 2:21:11complete and the same goes if you want
- 2:21:13to print something like this and let's
- 2:21:15convert this one so if we copy this line
- 2:21:18down into here and we just remove the
- 2:21:20spaces then it can look something like
- 2:21:24this name followed by the name we don't
- 2:21:27actually need this space here because
- 2:21:28we're going to be going into a brake
- 2:21:30line we can replace our name with curly
- 2:21:32braces zero and our age with curly
- 2:21:35braces one and then straight after we
- 2:21:38can type name and age and if we output
- 2:21:41this we'll see it's exactly the same
- 2:21:43there we go so just a quick recap
- 2:21:47printing strings and any other variables
- 2:21:48to the screen has been not normally
- 2:21:50achieved by doing a string followed by a
- 2:21:53plus and then adding another variable to
- 2:21:55it adding more than one can be a little
- 2:21:58bit complicated and hard to track of all
- 2:22:01the spaces that you're going to put in
- 2:22:02and you might accidentally miss one of
- 2:22:04them like this one here or this one here
- 2:22:07are very crucial to the output if you
- 2:22:09miss this one then it's going to say is
- 2:22:11followed by the name all together with
- 2:22:14no space we then introduce composite
- 2:22:16formatting which lets us use this
- 2:22:18notation of curly bra Z
- 2:22:21and curly brace one to give us the
- 2:22:23ability to store some variables outside
- 2:22:25of the string and then insert them in
- 2:22:29dynamically we took both of these
- 2:22:31examples and made them down here using
- 2:22:34composite
- 2:22:36formatting so let's explore a new method
- 2:22:38of concatenation that makes this look a
- 2:22:40lot better the reason why this is an
- 2:22:42issue is you have spaces in here and
- 2:22:45sometimes you might forget these spaces
- 2:22:47and it could mess up your program this
- 2:22:49string here looks very very unnatural
- 2:22:51because you're forcing the space but
- 2:22:53when you run it you'll see that it is
- 2:22:55actually what you expect the spaces are
- 2:22:57complete and it makes perfect sense but
- 2:23:00when you're writing it it kind of looks
- 2:23:02a little bit stupid because you're
- 2:23:04forcing the space and then adding a plus
- 2:23:06and then the name and then another plus
- 2:23:08and then another static string and this
- 2:23:10just get a little bit long to type it's
- 2:23:13fine when you have a smaller sentence
- 2:23:14like this but when the program starts to
- 2:23:16grow then it could be a little bit
- 2:23:18problematic and you'll have a lot of
- 2:23:19syntax ER is when you forget to type in
- 2:23:21all the pluses luckily this can be fixed
- 2:23:24let's duplicate this line down and let's
- 2:23:26see what we can do so we have this thing
- 2:23:29called string interpolation and it uses
- 2:23:31a dollar sign as a reference we can
- 2:23:34place the dollar sign before the
- 2:23:35quotation marks and then what you can do
- 2:23:38is you can actually keep this as one big
- 2:23:40sentence so let's remove all of the
- 2:23:43values and have it like this so now of
- 2:23:46course this actually doesn't print out
- 2:23:47variables because we've just replaced it
- 2:23:49with text so if we just run this then
- 2:23:51it's completely messed up because now
- 2:23:53we're not using our variables and this
- 2:23:56can be easily fixed because we've used
- 2:23:58our dollar sign now we can actually wrap
- 2:24:00our variables in this curly brace and
- 2:24:03now you can see it went white if I
- 2:24:05double click on it you can see that it's
- 2:24:06referencing the
- 2:24:08variable wrapping the variables in the
- 2:24:10curly brace means that they can actually
- 2:24:12be processed variables even though they
- 2:24:14are still within the speech marks please
- 2:24:16do not remove the dollar sign because
- 2:24:18now this will be interpreted as string
- 2:24:20text as you can see here putting a
- 2:24:23dollar sign at the start will make into
- 2:24:25string interpolation and then we get the
- 2:24:27ability to have our brackets this can
- 2:24:30make string concatenation very easy to
- 2:24:32do because you don't have to worry about
- 2:24:34all the pluses and adding them together
- 2:24:37and don't have to worry about the spaces
- 2:24:38as well this looks more like a natural
- 2:24:41sentence and what you can do for
- 2:24:43practice is you can actually just type
- 2:24:44out the sentence your name is Aba your
- 2:24:48age is 23 and then from there you can
- 2:24:51actually just break down the variables
- 2:24:53and know that you'll need one here so
- 2:24:55you can replace that with name and then
- 2:24:57you'll know you'll need one here so you
- 2:24:58can replace that one with age and then
- 2:25:00you can place the dollar sign right at
- 2:25:02the end and then there you go you have
- 2:25:04your solution this method means that
- 2:25:07it's a lot cleaner to read easier to
- 2:25:09maintain and make changes in the future
- 2:25:12and that's the whole point of
- 2:25:13programming if your code base is very
- 2:25:15difficult to go around and understand
- 2:25:17what's going on and potentially make
- 2:25:19changes in the future then you've
- 2:25:20already made a bad step and you don't
- 2:25:22want to get into these nasty habits it's
- 2:25:25fine if you're just going to have
- 2:25:26something like this where it's not
- 2:25:27really complicated at all a string plus
- 2:25:30another string that's quite simple and
- 2:25:32that's fine you don't have to go the
- 2:25:34extra effort to put in the dollar sign
- 2:25:36and wrap it around in the curly braces
- 2:25:38even though I personally still would
- 2:25:40because in my personal opinion this
- 2:25:42looks a lot
- 2:25:44better than having it like that you can
- 2:25:47see that it's a lot neater it takes up
- 2:25:49less code and you can see visually that
- 2:25:51you're using a specific variable and you
- 2:25:54don't have to worry too much about the
- 2:25:55spaces because this looks like a real
- 2:26:00sentence another method of string
- 2:26:02concatenation which is the idea of
- 2:26:04taking strings or objects and add them
- 2:26:06together to form a sentence of sorts we
- 2:26:08can say your name is followed by space
- 2:26:11and then add the name followed by space
- 2:26:14add your ages and then add the actual
- 2:26:16age variable and we can also write the
- 2:26:18exact same line using string
- 2:26:20interpolation or composite formatting
- 2:26:23just like this and like this we can
- 2:26:26specify the dollar symbol and put our
- 2:26:28variable in the bracket which is shown
- 2:26:31here and here and for the composite
- 2:26:33formatting we can take the content where
- 2:26:35our variable would go and replace it
- 2:26:37with incremental numbers 0er and one
- 2:26:40wrapped around in a curly brace and then
- 2:26:42specify in the correct order zero takes
- 2:26:45a value of name and one takes a value of
- 2:26:48age if we run this code you'll see that
- 2:26:50that the outputs are
- 2:26:51identical so let's explore this other
- 2:26:54method we have a string test for example
- 2:26:57and then inside the string Library we
- 2:26:59have this function called concat and it
- 2:27:01can takeing any amount of parameters
- 2:27:03that we want to give it so we could say
- 2:27:06your name is space and the quotation
- 2:27:09marks and then we can add a comma and
- 2:27:12then we can say we want to add the name
- 2:27:14and then another comma and we want to
- 2:27:16add and your
- 2:27:17ages and the speech marks add add a
- 2:27:20comma add the age variable and that's it
- 2:27:23and we could just use CW tab tab and
- 2:27:25type in test and let's run it and see
- 2:27:27what happens and now as you can see we
- 2:27:30get the exact same output and because
- 2:27:32this just returns us back with the
- 2:27:34string we can also just take this entire
- 2:27:36line and paste it straight into the
- 2:27:38console right line you don't necessarily
- 2:27:40have to store it but you can if you want
- 2:27:41to and there you go all methods produce
- 2:27:44the same output another way you can use
- 2:27:46this concap function is by giving it
- 2:27:48array we haven't covered array so far so
- 2:27:51I'm just going to show you this for
- 2:27:52demonstration purposes don't worry about
- 2:27:54understanding it so just a quick
- 2:27:56explanation arrays are just ways of
- 2:27:58storing multiple of the same data type
- 2:28:00so if we have these three names inside
- 2:28:03our names we can actually use the
- 2:28:05console to print them all out so we can
- 2:28:08say string. concat and then we can
- 2:28:10concat the names together which will
- 2:28:12just add all of these three together now
- 2:28:14the problem is when you run it you'll
- 2:28:16see they're stuck together so what you
- 2:28:18could do is just add a space between
- 2:28:20them and as you can see this is getting
- 2:28:22a little bit tedious
- 2:28:23now there's other methods that we can
- 2:28:26use to concat some arrays together and
- 2:28:28I'll show you that in the next section
- 2:28:29but for now don't worry about it this is
- 2:28:31concentrating on the string concat
- 2:28:33function and we'll just demonstrate that
- 2:28:35again so if we run the line of code we
- 2:28:37can see that they're all the same so now
- 2:28:39that you have four different methods of
- 2:28:41printing out exactly the same
- 2:28:42information you might be asking yourself
- 2:28:45what's the best method I personally
- 2:28:47prefer to use the string interpolation
- 2:28:49because with this one you can easily
- 2:28:51miss the spaces and you could produce a
- 2:28:53bad output and it might be stuck
- 2:28:55together it's fine when you can see the
- 2:28:57lines right in front of you and you can
- 2:28:59easily identify the spaces but when
- 2:29:01you're adding about 10 variables
- 2:29:03together then it could be a problem the
- 2:29:05reason why like string interpolation
- 2:29:07using the dollar signs and the variables
- 2:29:09in the curly braces is that you don't
- 2:29:11have to rely on missing out spaces
- 2:29:13because it actually forms a sentence and
- 2:29:15you just replace the values with your
- 2:29:17variable composite format in which is
- 2:29:19this one is very efficient and very nice
- 2:29:21to look at because it allows you to
- 2:29:24specify the sentence and then plug in
- 2:29:26the variables after the only problem
- 2:29:28with this is if you were to change this
- 2:29:29value in the future or to change a
- 2:29:31sentence you have to make sure that the
- 2:29:33numbers add up just like in the first
- 2:29:35example if you only have a short
- 2:29:37sentence then this isn't really an issue
- 2:29:39since you can see it all visually and
- 2:29:41you also got to realize that these need
- 2:29:42to be in the right order because if you
- 2:29:44put Age first then it's going to put age
- 2:29:46in this one and name in the second one
- 2:29:48and that's going to be problematic
- 2:29:50because it now mess up the sentence
- 2:29:52using string interpolation you don't
- 2:29:54actually have to worry about anything
- 2:29:56because if you wanted to change this to
- 2:29:57say your age is then you simply just do
- 2:30:00this and that's very easy to do because
- 2:30:02in this case you'd say your age is and
- 2:30:05then you'd have to change a string and
- 2:30:07then now that this is zero we need to
- 2:30:09change this from being age and this one
- 2:30:11to be name whereas in the first example
- 2:30:14you could just easily change the value
- 2:30:16in the curly braces instead of changing
- 2:30:18this reference and the end reference
- 2:30:20conar is not widely used just because
- 2:30:23you have all of these methods available
- 2:30:25where you can easily add stuff together
- 2:30:27but there could be user cases later on
- 2:30:28down the line where uses just so it's a
- 2:30:31bit more obvious and it helps the
- 2:30:32readability of your code and maintain
- 2:30:35within a big project or just on your
- 2:30:37personal projects when you're looking
- 2:30:38back at your previous
- 2:30:41code when you create an initialized a
- 2:30:44string variable you might want to give
- 2:30:45it the value of an empty quot and this
- 2:30:47will just default the value to
- 2:30:49essentially nothing you're giving it a
- 2:30:51value but at the same time the value
- 2:30:52doesn't actually contain any sort of
- 2:30:54characters if we print this out to the
- 2:30:56screen then you won't really see
- 2:30:57anything this video is going to focus on
- 2:30:59string.empty and string. empty will
- 2:31:02actually just represent these quotation
- 2:31:03marks string.empty will represent these
- 2:31:06speech marks so what we can do is we can
- 2:31:08type in string using a DOT and then we
- 2:31:11can type in empty just like that we've
- 2:31:13initialize our string name to a value of
- 2:31:16empty now this isn't necessarily used
- 2:31:18when you're declaring an initializing it
- 2:31:21because this is almost identical to just
- 2:31:23defining a string on its own main reason
- 2:31:25why this is used let's say we actually
- 2:31:27ask them user to input their name so we
- 2:31:29can say a console. right enter your name
- 2:31:32and then we can take this string
- 2:31:33variable move it down using alt and then
- 2:31:36arrow keys and remove this and let's
- 2:31:39initialize it to a console. readline to
- 2:31:41read in the value from the
- 2:31:43console and now what we can do is using
- 2:31:45our is string interpolation method that
- 2:31:48we just learned we can print to the user
- 2:31:50your name is and then the name in The
- 2:31:51Curly braces now let's just see what
- 2:31:54happens so let's actually type our name
- 2:31:56in abber and it says your name is ABBA
- 2:31:59but what if the user just types nothing
- 2:32:01and just presses enter Then it still
- 2:32:02says your name is blank now let's see
- 2:32:05what we can do here we don't necessarily
- 2:32:06want to say your name is blank we want
- 2:32:08to check what this value is so we can
- 2:32:10say if the name is not equal to empty
- 2:32:13quotation marks then we can output your
- 2:32:15name is and then then else we can say
- 2:32:18name is empty
- 2:32:20and then if we run it now and press
- 2:32:21enter it says name is empty so we can
- 2:32:24use string. empty instead of these
- 2:32:26quotation
- 2:32:28marks this looks a lot better when
- 2:32:30you're trying to compare it your
- 2:32:32name is and then when we press enter
- 2:32:34it says name is empty this just helps to
- 2:32:37keep your code a lot tidier just so you
- 2:32:39don't have to write the two quotes if
- 2:32:41you accidentally put a space in there
- 2:32:43now your if statement is incorrect if
- 2:32:45you always use string. empty then it
- 2:32:47just helps you to make sure that your
- 2:32:49code is going to to be maintainable and
- 2:32:50readable in the future reading string.
- 2:32:53empty gives you a better representation
- 2:32:55of what's going on instead of two open
- 2:32:59quotes if there is a situation where you
- 2:33:02want to check if one string equals
- 2:33:03another string then you can do the
- 2:33:06following if message equals equals
- 2:33:09compare then we can print out same and
- 2:33:12then else we can print out
- 2:33:15different and as we can see here they
- 2:33:17both have a capital H and they both say
- 2:33:19hello so they should be identical let's
- 2:33:22run the code and find out so they say
- 2:33:24same which is exactly what we'd expect
- 2:33:27so we have another method of being able
- 2:33:29to do this so even though we can apply
- 2:33:31equals equals to two strings it's
- 2:33:33normally better practice to use equals
- 2:33:36and then place the other string inside
- 2:33:38the brackets so you're saying if message
- 2:33:40equals compare now if you run the code
- 2:33:43you'll see that it's the same
- 2:33:44output in certain circumstances where
- 2:33:47you want to ask what the user has
- 2:33:48entered enter your name and then we can
- 2:33:52use a string name equals console.
- 2:33:54readline we can say if the name does not
- 2:33:58equal to empty strings then from there
- 2:34:01what we can do is type in your name is
- 2:34:05plus
- 2:34:06name and then we get some output
- 2:34:09your name is but since we're
- 2:34:11checking if it's not equal to that that
- 2:34:13means they've entered something in let's
- 2:34:15see if there's another way that we can
- 2:34:16actually write this so if we just
- 2:34:18duplicate it and comment it out so we
- 2:34:20got it for reference so what we want to
- 2:34:22do is we're checking if name is not
- 2:34:24equal to empty quotes so we can say name
- 2:34:28do equals empty quotes but what this is
- 2:34:31telling us if the name equals empty
- 2:34:33quotes then we should do this so then
- 2:34:35what we can do when we learn about the
- 2:34:37if statements is we can use the not
- 2:34:39operator and we can say if the name not
- 2:34:42equals to empty quotes then we proceed
- 2:34:44with this line else we can just
- 2:34:48say and valid name
- 2:34:51input let's run that and see what
- 2:34:53happens if you type in then it
- 2:34:55passes fine and if we type in nothing
- 2:34:57then we get an invalid name input the
- 2:34:59reason why equals equals and the equals
- 2:35:02function are different is because lower
- 2:35:04level using equals equals will actually
- 2:35:07compare the reference in the memory and
- 2:35:09the reference in the memory is what's
- 2:35:11used up when you actually create these
- 2:35:13variables so what this is doing is it's
- 2:35:16comparing the value that's actually
- 2:35:17stored in memory to the other one that's
- 2:35:19stored in memory now the problem is when
- 2:35:22it comes to something like arrays where
- 2:35:24you actually have the same value but
- 2:35:26they're slightly different let me
- 2:35:27demonstrate so I'm just going to use
- 2:35:29this code because we haven't covered
- 2:35:31arrays yet so I'm just showing you an
- 2:35:33example if we have a chart array that
- 2:35:35literally says hello and then we can use
- 2:35:37this line of code to join them together
- 2:35:40but this is actually an object instead
- 2:35:42of a string but if you see what we can
- 2:35:44do in the if statement we can say if the
- 2:35:46message which is equal to hello we can
- 2:35:49use message. equals and we got the new
- 2:35:51compare and this is actually an object
- 2:35:54so now we're comparing a string with an
- 2:35:56object and generically speaking an
- 2:35:59object can hold any sort of value
- 2:36:01because all of these variables that are
- 2:36:03strings in double Etc they're all deriv
- 2:36:05from something called an object which is
- 2:36:07just a very generic way of storing
- 2:36:09information so since these two data
- 2:36:12types are little bit different they
- 2:36:13still evaluate the same thing what the
- 2:36:16equals function is doing is actually
- 2:36:18checking the literal values of the
- 2:36:20variables instead of checking the values
- 2:36:22that are held inside the memory address
- 2:36:24location and the difference here is even
- 2:36:26though we have a character array that is
- 2:36:28giving us the values hello and our
- 2:36:31string is also given us the value of
- 2:36:33hello if we run this line of code that
- 2:36:35uses do equals then you'll actually see
- 2:36:38that the second same actually represents
- 2:36:40the bottom if statement so what that's
- 2:36:42doing is it's actually giving us back
- 2:36:45the comparison saying that it's true and
- 2:36:47it is indeed true because hello is is
- 2:36:49literally equal to hello but if you were
- 2:36:52to use the double
- 2:36:54equals then what you'll realize when you
- 2:36:56run the code is now it'll say it's
- 2:36:58different because what it's doing now is
- 2:37:01it's taking a string object and
- 2:37:03comparing it to an object and they are
- 2:37:06not different because of their memory
- 2:37:08and that's what the equals equals is
- 2:37:10comparing it's not only comparing the
- 2:37:12value it's comparing the memory address
- 2:37:14location and that's going to be vastly
- 2:37:16confusing because you don't want to hit
- 2:37:18these situations so it's always best to
- 2:37:20use the equals function because the
- 2:37:22equals function is concerned about the
- 2:37:24values rather than where they are in
- 2:37:27your memory I hope that made sense and
- 2:37:29it wasn't just confusing but basically
- 2:37:31when you use equals equals it checks the
- 2:37:34values that are actually wrote in your
- 2:37:35computer's memory instead of the value
- 2:37:38itself it will do both it will check the
- 2:37:40memory and check the value whereas when
- 2:37:43use do equals it is only concerned about
- 2:37:45the values and that's all that matters
- 2:37:48when we're doing an if statement like
- 2:37:49this we're checking if they're both the
- 2:37:51same so it shouldn't really matter how
- 2:37:53and where they are stored the only thing
- 2:37:55that matters is the values contained
- 2:37:57within the variable we're only caring
- 2:37:59about the values and not where they're
- 2:38:03stored so when we have a string variable
- 2:38:05defined in C we have it like this and
- 2:38:08when we print it out to the console we
- 2:38:10get the entire string being
- 2:38:12displayed but what if you want to return
- 2:38:14the first element back and not the rest
- 2:38:16then what are your options so in the
- 2:38:18later video in the of course we covered
- 2:38:20the substring function and technically
- 2:38:22you could go from the first character
- 2:38:24till the second character and you will
- 2:38:26just get C back so that's index zero
- 2:38:28till index one and you'll get the cback
- 2:38:31now the problem with that is that's an
- 2:38:32extra function call of something that
- 2:38:34should be quite simple and it is a
- 2:38:36message is a string and strings tend to
- 2:38:38be an array of characters which is
- 2:38:41similar to this we haven't learned about
- 2:38:43array so far and we'll cover that in the
- 2:38:45next section but what an array
- 2:38:46essentially is is it lets you store
- 2:38:48multiple of that data type so we could
- 2:38:51have a chart array that holds exactly
- 2:38:53the same value and each position will
- 2:38:55hold each one of these characters and
- 2:38:57the reason why I'm mentioning this is
- 2:38:59because with strings you can use the
- 2:39:00square bracket notation so if you add
- 2:39:02some square brackets at the end it says
- 2:39:04here it returns back a Char and it takes
- 2:39:07in an index gets the CH object at the
- 2:39:10specified position in the current string
- 2:39:12object so if we use message zero you can
- 2:39:14see that returns us back with a ch ch
- 2:39:17right here gets the CH object so we can
- 2:39:20call this on a string and it can return
- 2:39:22us back with a character because like I
- 2:39:24said with the array of characters that's
- 2:39:26essentially what a string is each one of
- 2:39:29these is just its own independent
- 2:39:31character and a string lets you pair
- 2:39:33them all together so if we print this
- 2:39:35out then we should get C because it's
- 2:39:37zerob based so if we copy this down and
- 2:39:39use
- 2:39:40one 2 and three then what should we get
- 2:39:44we get
- 2:39:45C then we get actually nothing
- 2:39:48technically because it's a space
- 2:39:50character and then the third character
- 2:39:52which is the fourth in the list because
- 2:39:53it's zero based is going to be our I so
- 2:39:57let's just print this out to the screen
- 2:39:58see what happens there we go so even
- 2:40:00though we don't see it visually there's
- 2:40:02actually a space character right there
- 2:40:04and that's the third character in The
- 2:40:05List which is right here so of course we
- 2:40:08don't tend to know how long the string
- 2:40:10is unless we can see it visually and if
- 2:40:12you were to reference one of these
- 2:40:14values and the string is not actually
- 2:40:16that long then you have a problem let's
- 2:40:18demonstrate if we have the string just
- 2:40:20called C and let's comment out the rest
- 2:40:23then that's only two characters so only
- 2:40:26zero and one will be applicable these
- 2:40:28two will return back something let's
- 2:40:30have a look and see what happens so the
- 2:40:32first character and the second character
- 2:40:34will run fine and let's put a console
- 2:40:36read line in there just to break it up
- 2:40:38so the other code doesn't run yet if we
- 2:40:40run the code we get our C and that's the
- 2:40:43max length of our string so far it's
- 2:40:46zero and one now if we press enter let's
- 2:40:49see what happens we get an index out of
- 2:40:51range exception index was outside the
- 2:40:53bounds of the array and the reason for
- 2:40:55that is we only have two characters
- 2:40:57inside our message and we're trying to
- 2:40:59display the third and the fourth this is
- 2:41:02problematic because we literally don't
- 2:41:04have any of them characters to
- 2:41:06display so what we can do instead is
- 2:41:08actually write ourselves a little Loop
- 2:41:10to go through the entire string and
- 2:41:12return us back all of these character
- 2:41:13positions instead of us just guessing
- 2:41:15how long the string is so if we return
- 2:41:17this string back to normal and then just
- 2:41:19after this little section we can write a
- 2:41:21for Loop in I equals 0 and I is less
- 2:41:25than message and then we have access to
- 2:41:27dot length this gets the number of
- 2:41:29characters in the current string so we
- 2:41:31don't have to worry about the maximum
- 2:41:32length in the square brackets we can
- 2:41:34always rely on dot length because we
- 2:41:37don't have to keep track of it and the
- 2:41:39language can know that for us and give
- 2:41:41us the
- 2:41:42value in this case we're going from zero
- 2:41:44to whatever the length of these
- 2:41:45characters are and then in here we can
- 2:41:48actually just conso right line and we
- 2:41:50can do the same notation we did here
- 2:41:52we're saying we want to print the
- 2:41:53message but not only the message we want
- 2:41:56to print the index and the index is
- 2:41:58going to be I in this case because we
- 2:42:00start at zero since it's zero based and
- 2:42:02then we're going to the last character
- 2:42:05so let's print this and see what
- 2:42:07happens so now we get C Is Awesome from
- 2:42:10here down to the bottom c is awesome
- 2:42:13print on every single line and that's a
- 2:42:15little bit annoying to read so what we
- 2:42:17could do is just change this to a
- 2:42:18console right and maybe we can just
- 2:42:20comment this section out just so we can
- 2:42:22see this
- 2:42:25properly there we go so we get c is
- 2:42:27awesome and because it's a console right
- 2:42:29it's right in them all next to each
- 2:42:31other and they're being printed out one
- 2:42:32by one let's add a cool little effect I
- 2:42:35can demonstrate you for now so inside
- 2:42:38the system. threading Library we have
- 2:42:40this sleep function so what we can do is
- 2:42:42we can add another using command system.
- 2:42:45threading we already have threading do
- 2:42:48tasks in there but the function that we
- 2:42:49need is inside the threading Library so
- 2:42:52there's two ways we can write this we
- 2:42:54can actually write everything by hand
- 2:42:56like this dot sleep or you could just
- 2:42:59remove the system. threading because
- 2:43:01we've added it at the Top If you don't
- 2:43:03want to add it at the top that's not
- 2:43:04really a problem you can just include it
- 2:43:06straight into here adding it at the top
- 2:43:08will include it within the entire
- 2:43:09project but if you're only going to use
- 2:43:11it once or twice then sometimes it's
- 2:43:13better to just include it straight into
- 2:43:14here because it'll make your overall
- 2:43:16program size a lot smaller so let's just
- 2:43:18keep keep it in so we can make our Corde
- 2:43:20a bit more readable so we have thread
- 2:43:23dos sleep and what this will do is it
- 2:43:25will suspend the current thread for a
- 2:43:26specified number of milliseconds there's
- 2:43:29a th000 milliseconds in 1 second so what
- 2:43:32we can do in here and we're trying to
- 2:43:33mimic a typewriter effect so we can say
- 2:43:36something like 200 which will be roughly
- 2:43:38a quarter of a second so if we've run
- 2:43:40the code now let's see what happens so
- 2:43:42we get this cool effect where after it
- 2:43:44prints out each of the characters it
- 2:43:45will print out the next character only
- 2:43:48after it's being asleep for a quarter of
- 2:43:49a second and that looks kind of cool if
- 2:43:52you wanted to speed it up you could just
- 2:43:53decrease this number there you
- 2:43:55go of course if you make it too low then
- 2:43:58it might be too fast for you to actually
- 2:43:59acknowledge what's happening and if you
- 2:44:02also make it too slow then it's just
- 2:44:04going to take too long to turn off and
- 2:44:06at that point you can use contrl C on
- 2:44:08your keyboard or use the stop button at
- 2:44:10the top to terminate the program okay so
- 2:44:12let's have a look at another thing that
- 2:44:14we can do with this so maybe we can make
- 2:44:16this 100 for now in the previous videos
- 2:44:18we've used use a function called
- 2:44:20contains and let's say we're just
- 2:44:22checking if it contains C and we know it
- 2:44:24does because we can see it visually
- 2:44:26let's run the
- 2:44:27code and we see we have true being
- 2:44:30printed and the reason why true is
- 2:44:32actually on the same line as c is
- 2:44:34awesome is because we're using console.
- 2:44:37right here so it's actually not made a
- 2:44:39new line character just after the E so
- 2:44:41when it's printing true it prints it
- 2:44:43straight after that so what we can do to
- 2:44:45fix that is just place a new line
- 2:44:47character in here using console.
- 2:44:48readline and we can just drop this a
- 2:44:50little bit lower to 50 there we go so we
- 2:44:53have true and what we can do with this
- 2:44:56is we can actually make our own little
- 2:44:57version of the contains function let's
- 2:45:00demonstrate so if we make this exact for
- 2:45:02loop again in I equals z i is less than
- 2:45:05the message. length and i++ so first
- 2:45:09let's kind of understand what's going on
- 2:45:10with this function returns a value
- 2:45:12indicated whether a specified subring
- 2:45:14occurs within this string and what that
- 2:45:16means is it will return a Boolean for us
- 2:45:18if C is inside our message then this
- 2:45:21will return back true and we know this
- 2:45:22is true because we just outputed it to
- 2:45:24the screen and we can see this visually
- 2:45:26so what we need to do is we need to Loop
- 2:45:28through our message and try and figure
- 2:45:30out if we can find C inside our message
- 2:45:33and then we can give back a value is
- 2:45:35true so maybe we can do a bu contains
- 2:45:38equal false and the reason why we
- 2:45:40initial it to false and the reason why
- 2:45:41we initialize it to false is because we
- 2:45:43want to set it up telling it that we
- 2:45:45haven't found it and then inside the for
- 2:45:47Loop if we find it then we can set it to
- 2:45:51True let's have a look so after the for
- 2:45:53Loop we can print contains of the screen
- 2:45:55and then inside the for Loop what we can
- 2:45:57do as we've seen here it returns back
- 2:46:00every single character so you'll get
- 2:46:02back C sharp then the space then I then
- 2:46:05s and so on and so forth and what we
- 2:46:08want to do is we want to go through each
- 2:46:09of the characters and then just ask if
- 2:46:12the variable is equal to C if C is equal
- 2:46:15to C then we can assign this to true and
- 2:46:18then go through #is equal to C if space
- 2:46:20is equal to C and so on and so forth so
- 2:46:23we can write a if statement here that
- 2:46:24says message square bracket I is equal
- 2:46:28to equal to apostrophe C and the reason
- 2:46:31why I said apostrophe is because this is
- 2:46:33a character and this also returns us
- 2:46:35back with a character so we need to
- 2:46:37compare them both like that if we use a
- 2:46:39string in here then it might complain
- 2:46:41that we're trying to convert between a
- 2:46:43Char and a string and it won't let you
- 2:46:45do the equals equals operator between
- 2:46:47two different variables maybe what we
- 2:46:49can do instead is to follow the approach
- 2:46:51that we used before and use the equals
- 2:46:53function and place our C inside here so
- 2:46:56if the character that we're currently up
- 2:46:57to inside the for Loop is equal to our C
- 2:47:00which is what we want to search for then
- 2:47:03we can set our contains variable equal
- 2:47:05to true we don't need to write an L
- 2:47:07statement in here to set our contains to
- 2:47:10false because we've already gave a value
- 2:47:12at the start if we didn't have this
- 2:47:14value then it'll be beneficial to do
- 2:47:16this but since we have it then we can
- 2:47:18remove this other path and we don't
- 2:47:20exactly need it so let's have a look and
- 2:47:22see if this
- 2:47:24works perfect it says true maybe we can
- 2:47:27change this character to something like
- 2:47:28a make sure this
- 2:47:30works which it does as well so perfect
- 2:47:33so now we've made our own mini little
- 2:47:35contains function inside here and we can
- 2:47:37also use the C library one but the
- 2:47:40purpose of this exercise was just to
- 2:47:41show you how we can iterate through a
- 2:47:43string variable and then decide what
- 2:47:46each of the characters can be equal to
- 2:47:48and then we can decide if they're equal
- 2:47:49to another one and then let the user
- 2:47:51know that we found a match for their
- 2:47:53search
- 2:47:56term so far in the course we've covered
- 2:47:58the equals function and the contains
- 2:48:01function when you're searching through
- 2:48:02strings or trying to find values let's
- 2:48:04have a look at a method that makes sure
- 2:48:06a string is valid before we can process
- 2:48:08information on it so in this quick
- 2:48:10example we have a name output we're
- 2:48:12reading the value into a string name and
- 2:48:14then we simply output it to the screen
- 2:48:17if we run the code and just press enter
- 2:48:18then it says your name is blank and we
- 2:48:21don't really want that because we want
- 2:48:22to check if your name is equal to
- 2:48:24something before we continue so let's
- 2:48:26look at some methods so we have if the
- 2:48:29name is not equal to nothing and then
- 2:48:31let's just print zero to the screen and
- 2:48:34that's one of the methods and we can say
- 2:48:37if name is not equals to quotation marks
- 2:48:42then let's print out number one so what
- 2:48:44this is doing is checking if the name is
- 2:48:46not equal to empty quotes and then this
- 2:48:49is doing exactly the same thing except
- 2:48:51we're using the equals method and let's
- 2:48:53just run the code and press enter and
- 2:48:56you can see the zero or the one doesn't
- 2:48:58appear because it's detecting that it
- 2:49:00contains nothing so if we type in a real
- 2:49:02value we can see Zero and one coming up
- 2:49:05so these are both valid ways of checking
- 2:49:07if the string is not empty so let's look
- 2:49:10at a third one and this one is actually
- 2:49:12very important for reasons I'll explain
- 2:49:14in a second so inside our string Library
- 2:49:16we have is null or empty so we place our
- 2:49:20name inside here and let's see what this
- 2:49:22says now indicates whether the specified
- 2:49:25string is null or at an empty string so
- 2:49:28this can be very effective so what this
- 2:49:31reads now is if the string is null or
- 2:49:33empty then we print a value but we want
- 2:49:36it to be not no lower empty because it
- 2:49:38wants to have some contents so if we
- 2:49:41type a value in again you'll see now we
- 2:49:43get all three numbers and what them
- 2:49:45three numbers mean is that it's past all
- 2:49:47of these checks and each of the checks
- 2:49:49just ensures that the value has got
- 2:49:51something inside and that's fine but it
- 2:49:54can be slightly problematic for this
- 2:49:56reason using this method and this method
- 2:49:59are fine in some cases except as it says
- 2:50:02here it checks if the string is null and
- 2:50:05null means it doesn't have a value but
- 2:50:07it just has nothing instead of something
- 2:50:10let me demonstrate if we assign the
- 2:50:12value of empty quotation marks this
- 2:50:15actually has a value but the contents is
- 2:50:17empty but it still a string as it comes
- 2:50:20up in here if we assign this the value
- 2:50:22of null then it's just a key word that
- 2:50:24we can use to denote that this literally
- 2:50:26has no contents it's not that it has an
- 2:50:29empty string because an empty string is
- 2:50:31still actually a string whereas a null
- 2:50:34it just means the variable is blank now
- 2:50:36this is where the issue arises if we try
- 2:50:39and call a method on something that's
- 2:50:41null Watch What
- 2:50:43Happens we actually get an exception and
- 2:50:45our program just crushes you can't run
- 2:50:48any any code on something if that
- 2:50:50variable is actually defined as
- 2:50:52null it doesn't mind if you check the
- 2:50:55value of it but if you try and run a
- 2:50:57function then you've got a problem so if
- 2:50:59we comment this line out this ensures
- 2:51:02everything it ensures it's null or it's
- 2:51:05empty and this is the belt and braces
- 2:51:07option because if it's null we don't
- 2:51:09actually get an exception when we try
- 2:51:11and run this code as
- 2:51:14follows we get the zero showing up
- 2:51:16because our variable has something
- 2:51:19that's not speech marks and it's
- 2:51:20actually null so we want to avoid this
- 2:51:23option as well and also avoid this
- 2:51:25option in case our string is actually
- 2:51:27genuinely
- 2:51:28null using is null or empty we check
- 2:51:32both for the value of null which is
- 2:51:34right now and if it's got empty quotes
- 2:51:37EG the value is empty so if we just run
- 2:51:40this again and have these two paths
- 2:51:42commented out we can see that two is not
- 2:51:44appearing if we change this back to our
- 2:51:47console read line and we try and do it
- 2:51:50then we get our number two because it's
- 2:51:52validated that this string variable has
- 2:51:55got something inside it and the variable
- 2:51:57is not actually no this is a very good
- 2:52:00way of being able to make sure that your
- 2:52:02program doesn't get exceptions as with
- 2:52:04calling these equals if you want to call
- 2:52:07equals then call it inside here you can
- 2:52:10check if that variable equals something
- 2:52:13by simply placing that if statement in
- 2:52:15inside here after you validated if the
- 2:52:17name variable able actually has some
- 2:52:20contents if it's null or it's empty
- 2:52:23running equals on it will just break the
- 2:52:25code further and you don't need to check
- 2:52:27if it's equal to Something in here maybe
- 2:52:29you were checking if the value is equal
- 2:52:31to abber or something along them lines
- 2:52:33and now if we run it again we actually
- 2:52:36get the output
- 2:52:37correct it's best to Nest them like this
- 2:52:40because if you run this function on
- 2:52:42something that's null it's actually
- 2:52:44really safe but as you saw previously if
- 2:52:46you run a function on something that's
- 2:52:48null then you're going to get a null
- 2:52:50reference exception and your program
- 2:52:51will close down and break you want to
- 2:52:54avoid exceptions at all cost so nesting
- 2:52:57it inside a is null or Remy check will
- 2:52:59be perfect for your code it improves the
- 2:53:02stab ability of your program to ensure
- 2:53:04that exceptions don't
- 2:53:07happen so in this exercise I want you to
- 2:53:09ask the user to input any message and
- 2:53:11then I want you to print the message in
- 2:53:13Order character by character and then
- 2:53:15also print it in reverse character by
- 2:53:17character let's get started so we could
- 2:53:20just have a console right line but let's
- 2:53:22change it to a right and just say enter
- 2:53:24your message call on and then speech
- 2:53:26marks and then just have a string
- 2:53:28message
- 2:53:30equals console. readline perfect so in
- 2:53:34order to be able to Loop through the
- 2:53:36string message we need to have a follow
- 2:53:38Loop if you remember from our previous
- 2:53:39videos we can have this string variable
- 2:53:42followed by a square bracket and then we
- 2:53:44can have an index inside and that will
- 2:53:46print us back any sort of index based on
- 2:53:48that position so if we just have this is
- 2:53:52a test then it's prob print as the T
- 2:53:55because that's the first index and so on
- 2:53:57and so forth so what we can do with this
- 2:53:59information is take what we can do with
- 2:54:02the string index and then also use the
- 2:54:04property which is do length and then use
- 2:54:07these two values to be able to process
- 2:54:09this is a test so we have the first
- 2:54:12character Square braet 0 and the last
- 2:54:14character is at length 14 so our message
- 2:54:17variable now contains 14 characters
- 2:54:20obviously When I close the program then
- 2:54:21that value is lost and I have to reenter
- 2:54:23it but because of that exact reason we
- 2:54:25would always use do length because we
- 2:54:27never know how long the users input is
- 2:54:29going to be so we can have a for Loop so
- 2:54:31we can say 4 into I equal 0 I is less
- 2:54:35than and we can use the message. length
- 2:54:37here and then we can say I
- 2:54:39++ so we want to start at zero because
- 2:54:42our first index position is indeed zero
- 2:54:45and we want to go from the index zero
- 2:54:47all the way to the length
- 2:54:49now in that example I just printed out
- 2:54:5114 for the length but you got to realize
- 2:54:53is there might be 14 positions but
- 2:54:56there's only 13 indexes so it's not
- 2:54:59actually 13 index it's just up to 13
- 2:55:02when you start at zero going from 0 to
- 2:55:0413 gives you 14 positions so when we're
- 2:55:07saying 0 is less than the message length
- 2:55:10then that'll actually take us from 0 to
- 2:55:1213 instead of 0 to 14 if we made this
- 2:55:16greater than or equal to and change this
- 2:55:18to to one for example then that's going
- 2:55:20to go from 1 to 14 and first we're going
- 2:55:22to miss out the first character and
- 2:55:24we're actually going to go one over the
- 2:55:26message length so you always want to
- 2:55:28start from zero if we're going through
- 2:55:30the entire string so this will go from 0
- 2:55:32to 13 and then here we can just have a
- 2:55:35console right line and just print
- 2:55:37message Square braet I and then just for
- 2:55:39the time being we can comment out these
- 2:55:41lines just so it doesn't saturate the
- 2:55:42output so let's run this code and see
- 2:55:44what
- 2:55:45happens this is a test and then we get
- 2:55:48them all on separate lines maybe we can
- 2:55:51just use a console. right instead so we
- 2:55:53get each character on the same line
- 2:55:55instead of a new line character being
- 2:55:56outputed at the end of each of the lines
- 2:55:59so we can say this as a test again and
- 2:56:01there we go we get this as a test
- 2:56:03outputed as well so now that we've
- 2:56:05printed that in order let's see how we
- 2:56:06can print it in reverse so just a quick
- 2:56:09little tip throughout this course I've
- 2:56:11been quite hard on you I've made you Mar
- 2:56:13the for Loop out exactly the way it
- 2:56:15should be but all done manually this is
- 2:56:18so you can get used to the syntax and
- 2:56:20after a while once you understand
- 2:56:21exactly what to do then what you can do
- 2:56:23is when you type in the fall you can see
- 2:56:25that it says as a force snippet tab
- 2:56:27twice to insert the force snippet and we
- 2:56:30can do that now and it actually gives
- 2:56:32you a little bit of an output you can
- 2:56:34see these sections that have been
- 2:56:35highlighted the reason they're
- 2:56:36highlighted is once you're done with one
- 2:56:38section you can actually press Tab and
- 2:56:40it'll jump to the next one so now my
- 2:56:42cursor is jumped on the length and we
- 2:56:44can type in message. length if I press
- 2:56:47tab again then it'll just keep cycling
- 2:56:49through them until we're done and we can
- 2:56:51press Escape upon pressing escape then
- 2:56:54it gets out of the little message and
- 2:56:55there we go we've now just defined a
- 2:56:58really quick for Loop please only use
- 2:57:00this once you're comfortable writing for
- 2:57:02loops and don't use them immediately
- 2:57:04because you might get stuck on what to
- 2:57:06do and how to do it so it's best to
- 2:57:08always write the for Loop syntax out
- 2:57:10from scratch so you can get used to the
- 2:57:11three different sections so this is
- 2:57:13actually a full loop that goes forwards
- 2:57:15it's going from 0 to 13 but what we
- 2:57:18actually want to do now is go from 13 to
- 2:57:21zero so let's actually change this for
- 2:57:23Loop completely and we can do our little
- 2:57:25shocket again so if we do four and then
- 2:57:28we don't actually want to start on the
- 2:57:29value of zero we want to start on
- 2:57:32message. length and then you want a
- 2:57:34minus one and just as we said here we
- 2:57:37never actually want to get to the length
- 2:57:39because we want to go from 0 to 13 so if
- 2:57:41we want to go backwards we want to go
- 2:57:43from 13 to zero and the only way that we
- 2:57:46can get this 13 is by getting the 14
- 2:57:49value which is message. length and
- 2:57:51simply minusing one so now that we've
- 2:57:53got our first variable which is in I
- 2:57:55equals message length minus one which
- 2:57:57will give us the 13 we want to go from I
- 2:58:00and instead of less than it's going to
- 2:58:03be greater than or equal to and then we
- 2:58:05actually want to change it to a zero
- 2:58:07because we actually want to stop at zero
- 2:58:08in this case and then we don't want to
- 2:58:10use I ++ because if we start at 13 we
- 2:58:14want to go down in one instead of up in
- 2:58:16one so we can simply just just do minus
- 2:58:19minus we can take this exact line and
- 2:58:21print out to the
- 2:58:23screen and if we just print something
- 2:58:25small in between like a hyphen just to
- 2:58:28separate the two outputs and let's see
- 2:58:30what
- 2:58:31happens test there we go so we have test
- 2:58:35and then t s e and t is now in Reverse
- 2:58:39but now you might be wondering if we had
- 2:58:42a shortcut for the four what's this four
- 2:58:44with an additional R cords n it for a
- 2:58:47reverse for Loop so if we tap tab twice
- 2:58:49you can see that we have exactly the
- 2:58:51same format inti equals something minus
- 2:58:54one which we have right here and then we
- 2:58:57have I is greater than or equal to zero
- 2:58:59as is we have here and then we have
- 2:59:01iusus just like here so we can literally
- 2:59:04just change this into message do length
- 2:59:08and then these two now are exactly the
- 2:59:10same so just copying this down we'll get
- 2:59:12us exactly what we need but obviously we
- 2:59:14just wrote this I just wanted to show
- 2:59:16you the shortcut so let's try it with a
- 2:59:18longer string this is a test there you
- 2:59:21go and if you wanted to you could just
- 2:59:23print these on separate lines so you can
- 2:59:24just change this into a right line for
- 2:59:27example and have it like that it doesn't
- 2:59:29really matter the rest is up to you but
- 2:59:31the task is just so you can print it one
- 2:59:33way and then print it the other way I
- 2:59:35realized that what you can also do is
- 2:59:38just simply print a console right line
- 2:59:40and have message in there and just get
- 2:59:42rid of this completely and the output
- 2:59:44will still look exactly the same there
- 2:59:47you go and and the reason why I told you
- 2:59:49to write the for Loop is not because
- 2:59:51it'll be easier if you do this it's more
- 2:59:53for your Educational Learning purposes
- 2:59:55having more practice to how to iterate a
- 2:59:57string variable is very helpful for the
- 3:00:00future and you can understand it fully
- 3:00:02if you just print out to the screen then
- 3:00:04you're not learning anything extra
- 3:00:05you're just applying what you already
- 3:00:07know so let's just have a quick recap we
- 3:00:09ask the user to enter the message we STW
- 3:00:11it into a string variable and then I
- 3:00:13demonstrated that you could use the
- 3:00:14square brackets to Output the first
- 3:00:16index then you can output the length of
- 3:00:18the entire variable using a follow Lo we
- 3:00:20can go from zero to message length which
- 3:00:23will give us from indexes 0 up until 13
- 3:00:26in this case and then we can go through
- 3:00:28i++ increment in one and then we can
- 3:00:31have a console right which will just
- 3:00:32print out message index I for the index
- 3:00:35that we're currently up to and then we
- 3:00:36break it off with a console right line
- 3:00:38just in the middle because these are
- 3:00:40printing on the same line so we need to
- 3:00:42insert a new line character at the end
- 3:00:44after we've done that what we can do now
- 3:00:46is print it in reverse and now we can do
- 3:00:49exactly the same thing except from 0 to
- 3:00:5113 we go from 13 to 0 so we start at the
- 3:00:54message length minus one because the
- 3:00:56length was 14 and now we minus one to
- 3:00:58get to 13 and we're going from I is
- 3:01:00greater than or equal to zero because
- 3:01:02that's the value that we want to stop at
- 3:01:04and then we can do IUS minus if we were
- 3:01:07to take out this equals operator let's
- 3:01:10see what happens test and now we're
- 3:01:12actually missing the last character
- 3:01:14because what we've done is we've
- 3:01:16actually went from 13 to 1 instead of 13
- 3:01:18to zero so let's put that back in and
- 3:01:21make sure it works and then we have t
- 3:01:24set for test and then we're outputting
- 3:01:26the exact same thing in both for Loops
- 3:01:29because we're simply just printing the
- 3:01:30character that we're currently up to and
- 3:01:32because this for Loop starts at zero and
- 3:01:34this for Loop starts at the length minus
- 3:01:36one then they output in the same values
- 3:01:39or what it appears to be the same values
- 3:01:41it's just starting at different
- 3:01:44indexes in this exercise we're going to
- 3:01:47be building some sort of a password
- 3:01:48validor these can be present in
- 3:01:50something like a register form where you
- 3:01:52ask the user to enter their password and
- 3:01:54then enter it again to make sure that
- 3:01:56they've entered it correctly you want to
- 3:01:57ask user to enter their password in
- 3:01:59twice andall them both check if they
- 3:02:01both contain something if they do we can
- 3:02:03print out password match if they don't
- 3:02:05passwords do not match and if they both
- 3:02:08contain nothing then you want to Output
- 3:02:09please enter a password so let's get
- 3:02:12started first we can have a console
- 3:02:14console right we can have enter password
- 3:02:19string password equals console.
- 3:02:23readline and then we can just copy these
- 3:02:25two lines down using contrl D and then
- 3:02:28we can have a password C for password
- 3:02:30confirmation and we can say enter
- 3:02:32password again and let just print these
- 3:02:34out to the screen just to make sure we
- 3:02:35know what's going on just like so so we
- 3:02:38can have abber and then abber again so
- 3:02:40now we have both abber started and
- 3:02:42that's
- 3:02:43fine so let's see what we can do now we
- 3:02:45need to check if they both contain
- 3:02:47something so what are the different ways
- 3:02:49that we can do this so we can have if
- 3:02:51password do equals and then we can make
- 3:02:53it equals to string. empty and because
- 3:02:56we want to check if it's not empty then
- 3:02:58we can simply place the not operator
- 3:03:00right at the start and then we can join
- 3:03:02them with another one not password.
- 3:03:04equals and then exactly the same thing
- 3:03:07string. empty but in this case we don't
- 3:03:09want to use password we want to use
- 3:03:11password see there we go so if we've
- 3:03:14came into here then that means they must
- 3:03:16have entered something so let's just
- 3:03:18quickly do our exit path which is if
- 3:03:20they're empty please enter a password so
- 3:03:23we can say else and we don't need a
- 3:03:25condition here because if this were to
- 3:03:27fail then it has to mean that their
- 3:03:29password's empty so we can have a
- 3:03:31console. right line and we can just do
- 3:03:33please enter a
- 3:03:35password and let's just test if this
- 3:03:37path works first and we just press enter
- 3:03:40twice and we get please enter a password
- 3:03:43and maybe if we type something in for
- 3:03:44both and then we get nothing because
- 3:03:46it's actually came here now perfect so
- 3:03:50if we come through and we can say
- 3:03:51password equals equals password C or we
- 3:03:54could use the equals function and we can
- 3:03:56say password. equals and then password C
- 3:04:00and if they both equal to each other
- 3:04:02then we can print out passwords
- 3:04:05match there we go and if that were to
- 3:04:09fail then we can print out passwords do
- 3:04:12not
- 3:04:14match here we go let's run that let's
- 3:04:16find out Aba and abber and we get
- 3:04:19passwords match and if we try abber and
- 3:04:21abber with two A's and then we get
- 3:04:23passwords do not match so what are some
- 3:04:26other ways that we can do this you can
- 3:04:28structure this completely differently
- 3:04:30right now we're doing both checks in one
- 3:04:32single if statement and we can actually
- 3:04:34split this up into two and then provide
- 3:04:36a different error message so right now
- 3:04:38this is please enter a password you
- 3:04:40might have entered the second one but
- 3:04:42not have ented the first one or vice
- 3:04:44versa but it doesn't matter because you
- 3:04:46have to enter both of them in order to
- 3:04:48pass this validation check so what you
- 3:04:50could do instead is actually put another
- 3:04:52if statement inside and paste this one
- 3:04:55inside here and then just keep these as
- 3:04:58two separate if statements and then you
- 3:05:00can actually provide different error
- 3:05:02messages so let's Nest this in one
- 3:05:05deeper so we can say if the password is
- 3:05:08not equal to empty and if this returns
- 3:05:10back true then this variable has
- 3:05:12something inside it and then once we go
- 3:05:14inside these curly braces we get another
- 3:05:16check and if the password C is not equal
- 3:05:19to nothing then that means if we've came
- 3:05:21in here then we pass both of these
- 3:05:23validation checks and both of them
- 3:05:24contain something and then we can
- 3:05:26proceed to check the variables so what
- 3:05:28you can do in the first L statement is
- 3:05:31you can say please enter a password but
- 3:05:33on the second one so just after this
- 3:05:35curly brace we can have another L
- 3:05:37statement or we can say please enter a
- 3:05:41password
- 3:05:43confirmation so I know that was a bit
- 3:05:45quick so let's just have a quick recap
- 3:05:47so we can have have one if statement
- 3:05:48that processes the first password that
- 3:05:50you type in and if this is empty then it
- 3:05:53says please enter a password and if it's
- 3:05:55not empty then it'll go inside this
- 3:05:56curly brace hit the first line and check
- 3:05:59if the password confirmation is not
- 3:06:01empty if it is empty then it'll say
- 3:06:04please enter a password confirmation if
- 3:06:06both of these return back true then it
- 3:06:08will go inside the success path in which
- 3:06:10it will actually check if password is
- 3:06:12equal to the password confirmation and
- 3:06:14we can say the passwords match or if
- 3:06:16they don't equal to each other we can
- 3:06:18say the passwords do not match let's run
- 3:06:21that and see what we get so if we type
- 3:06:23something in for the second one but not
- 3:06:24the first one so for the first one we
- 3:06:26just press enter and then let's type
- 3:06:28something in for the second one and it
- 3:06:30says please enter a password and then if
- 3:06:32we run that and do the complete opposite
- 3:06:34type in p and then press enter for the
- 3:06:37second one then it says please enter a
- 3:06:38password confirmation so you can see
- 3:06:40immediately what we can do is provide a
- 3:06:42different error message for different
- 3:06:44situations if you include these both in
- 3:06:47one
- 3:06:48then you can only give out more of a
- 3:06:49generic error message this is similar
- 3:06:51for when you go to login and when you
- 3:06:53get your password or email wrong it
- 3:06:55doesn't say which one you've got wrong
- 3:06:57it just says them generically which one
- 3:06:59you've got wrong it says invalid
- 3:07:01credentials or invalid username or email
- 3:07:04it'll say them generically like invalid
- 3:07:06username or password but it won't tell
- 3:07:08you which one it is because hackers can
- 3:07:10exploit this and you don't want to be
- 3:07:11doing this but in the case of a password
- 3:07:13confirmation and this is just for a
- 3:07:15register form then it's not exactly the
- 3:07:17the worst thing to do this because
- 3:07:19sometimes what you'll see is the text
- 3:07:20boxes might go red to indicate that
- 3:07:22they're both not equal to each other and
- 3:07:24this is fine on registration but it's
- 3:07:26not fine on the login what we can also
- 3:07:28do in here once we've checked if they're
- 3:07:30both not equal to something we can
- 3:07:32actually just validate how long the
- 3:07:33string needs to be we can have if
- 3:07:36password. length and let's say you had a
- 3:07:38restriction and it can only be over six
- 3:07:41characters and we can say six and
- 3:07:44password
- 3:07:45c.length is greater than equal to six
- 3:07:48and once it passes all three of these
- 3:07:50checks then we can check if they're
- 3:07:52equal to each other if they got
- 3:07:54something inside and if they've got
- 3:07:56something inside and if they've got both
- 3:07:59the length is greater than or equal to
- 3:08:01six then we can check it so we can have
- 3:08:04something like password and then
- 3:08:06password and then this returns back with
- 3:08:08password matched if we have something
- 3:08:10like abber then it's actually returns
- 3:08:12back nothing because we don't have an
- 3:08:14else for this specific bracket so we can
- 3:08:17say else conso right line please enter
- 3:08:22six or more
- 3:08:25characters so if we type in Alber twice
- 3:08:28then you can see we get this message now
- 3:08:30please enter six or more characters so
- 3:08:32right now we have a validation for the
- 3:08:34first password and the second password
- 3:08:37and for the length and if all these
- 3:08:39checks go by then you can process the
- 3:08:41actual passwords that they've entered
- 3:08:43and check if they're equal to each other
- 3:08:45you can technically do all of these
- 3:08:46checks in one line it'll just be a
- 3:08:49pretty long line but what that means is
- 3:08:51you can't have three separate error
- 3:08:53messages like this if you have it
- 3:08:55process in one line the only error
- 3:08:57message that you can give is very
- 3:08:58generic there's no one generic message
- 3:09:00that can fit for password empty and the
- 3:09:03confirmation password empty and for the
- 3:09:05length so maybe what you could do is
- 3:09:07chain the both of them to be check if
- 3:09:09they're not empty so then you can
- 3:09:11process a message with something like
- 3:09:13please enter a password and then you can
- 3:09:15have another check for the password
- 3:09:16length just to let them know that they
- 3:09:18need to enter six or more characters
- 3:09:20splitting these off into two separate
- 3:09:22checks is going to be very helpful
- 3:09:24because it'll let the user know
- 3:09:25something a bit better than a generic
- 3:09:27error message so in the previous
- 3:09:29examples throughout the course we've
- 3:09:31actually talked about using the to low
- 3:09:32function and I said in majority of cases
- 3:09:35where you want actually compare two
- 3:09:36different things you want to actually
- 3:09:38use the two lower function or the two
- 3:09:40upper function and you can achieve this
- 3:09:42by having two lower just here and then
- 3:09:45also two lower just here
- 3:09:48now the problem with comparing if
- 3:09:49they're both equal to each other but
- 3:09:51checking the lower gue values is this is
- 3:09:53indeed a password if you're checking if
- 3:09:55the user has inputed any capitals
- 3:09:57lowercase symbols Etc then there's no
- 3:10:00point implying a too low function
- 3:10:02because that will just ruin their actual
- 3:10:04password and reduce the security so in
- 3:10:06certain cases you don't always want to
- 3:10:08use two lower and two upper for
- 3:10:10comparison you want to just check the
- 3:10:12value straight up and the same thing is
- 3:10:14when you're doing a login you don't want
- 3:10:16to be checking the values lowcase
- 3:10:18because that's their login these are
- 3:10:20some of the specific situations where
- 3:10:22you don't want to be using the two low
- 3:10:23function if the data that your
- 3:10:25processing is pretty sensitive and will
- 3:10:27be messed up if you use the to function
- 3:10:29that you should always avoid it
- 3:10:31passwords fit this perfectly and
- 3:10:33usernames as well if someone's added
- 3:10:35some capitals inside the username then
- 3:10:37you don't want to ruin that by doing
- 3:10:38lower case or upper
- 3:10:41case if your username is spelled like
- 3:10:44this and you try and log on like this
- 3:10:46then they're not equal to each other
- 3:10:48because this will probably be two
- 3:10:49different accounts you can have an
- 3:10:51account with a capital B and have
- 3:10:53account with a lowercase b so you don't
- 3:10:54want to be lowercasing the input and
- 3:10:56then doing a comparison because what you
- 3:10:58might actually retrieve is a different
- 3:11:00account than you're looking
- 3:11:02for so now that we talked about stuff
- 3:11:04theoretically let's see how we can do
- 3:11:06things practically so as you heard in
- 3:11:08the last video arrays are a way that we
- 3:11:10can store multiple variables of the same
- 3:11:12data type in one little group so if you
- 3:11:15had something like num one equals 5 in
- 3:11:18number 2 = 10 in number 3 = 15 and we
- 3:11:22had a similar case in one of the
- 3:11:23exercises like this where we had to work
- 3:11:26out if a triangle was actually a
- 3:11:27triangle so we asked the user to input
- 3:11:29three different angles and then we
- 3:11:31stalled them all and then we added them
- 3:11:33up after and it looked something like
- 3:11:35this in total equals num 1 plus num 2
- 3:11:39and then finally plus num3 and then we
- 3:11:42had some sort of if statement after to
- 3:11:44check if the
- 3:11:45total was equal to
- 3:11:48180 and then we did something in the
- 3:11:50curly braces just to Output that it's
- 3:11:52valid and then else output it's
- 3:11:54invalid you can see how this gets a
- 3:11:56little tedious we have to create a new
- 3:11:58variable every single time we want some
- 3:12:00more data to be stored and in this case
- 3:12:02we just simply want another number that
- 3:12:04we can store but we have to just keep a
- 3:12:06pending one to it in that case we could
- 3:12:09have angle one angle two and angle three
- 3:12:11but how about if you had something that
- 3:12:13just called angles for now let's just
- 3:12:15talk about how arrays work how we can
- 3:12:17use them and then after I've went
- 3:12:19through this example about arrays and
- 3:12:20taught you what to do with it then I'll
- 3:12:22will go get the code from the exercise
- 3:12:24from the triangle video and actually
- 3:12:26transform it into using an array so you
- 3:12:28can see what the difference is and how
- 3:12:29the new code will look like so let's get
- 3:12:32rid of this little section so now that
- 3:12:34we've got three integer variables let's
- 3:12:36see how we can do this another way so
- 3:12:38the Syntax for arrays you first type in
- 3:12:40the data type that you want just like
- 3:12:43before and then you add on a square
- 3:12:44bracket and a closed square bracket
- 3:12:46there's not nothing that needs to be
- 3:12:47inside as of right now then you press
- 3:12:50space and type in the variable name as
- 3:12:52per always and then we can make that
- 3:12:54equal to new in three so what this side
- 3:12:57of the equal sign means is we want a new
- 3:12:59instance of an integer array and we want
- 3:13:02to have three positions since we have
- 3:13:04three variables here that we want to
- 3:13:05store we need to have three positions to
- 3:13:07store them so this line in total says
- 3:13:10that we want to integer array we want to
- 3:13:12call it numbers and we want to
- 3:13:13instantiate our new integer array with
- 3:13:16three positions instancia is just the
- 3:13:18term that we use when you turn something
- 3:13:21into a new class at this point if we
- 3:13:23don't use new then we're going to get
- 3:13:25compiler error because it's unsure what
- 3:13:28this is trying to tell you it's trying
- 3:13:29to start an integer inside integer array
- 3:13:32and that makes no sense so it won't
- 3:13:34recognize the square brackets in this
- 3:13:36case because it has no idea what it's
- 3:13:38trying to do and this syntax is
- 3:13:39completely incorrect so the compiler
- 3:13:41doesn't know what error message to give
- 3:13:42you so we can have a new int and what we
- 3:13:45can do now is by us using the square
- 3:13:47bracket reference we can actually
- 3:13:49specify these three variables inside our
- 3:13:51numbers array so now we have access to
- 3:13:54numbers 0 = 5 and then if we use
- 3:13:58controll D to copy this down twice we
- 3:14:00change this to one and two and then
- 3:14:03print 10 here and 15 here so now this is
- 3:14:06exactly the same as this we have three
- 3:14:09integer variables defined separately as
- 3:14:115 10 15 and now that we've created an
- 3:14:14array of numbers we can specify all
- 3:14:16three these values inside our numbers
- 3:14:18array so instead of having three
- 3:14:20individual variables now we just have
- 3:14:22one that holds everything like a group
- 3:14:25and just to show you how this works if
- 3:14:26we have a console right line we can say
- 3:14:28num one space num two space num three
- 3:14:33and if we use controll D to duplicate
- 3:14:35that down then what we can have instead
- 3:14:37of these num ones is simply just place
- 3:14:39these values
- 3:14:41inside changes to one and changes to two
- 3:14:44and what you should notice is these two
- 3:14:46lines are going to print the the exact
- 3:14:47same
- 3:14:49values there we go so in this example
- 3:14:52we've took our sloppy code that will
- 3:14:54roll us around appending one to this
- 3:14:56number creating a new array and then
- 3:14:58printing it out so let's say we want to
- 3:15:00actually use these to read in values
- 3:15:02from the console what can we do and
- 3:15:04you'd use these exactly the same as how
- 3:15:06You' use variables up here this is just
- 3:15:09an integer variable at a specific
- 3:15:11position held inside a group so we could
- 3:15:13simply have a console right line delete
- 3:15:16the right and just say enter a
- 3:15:18number and then instead of starring five
- 3:15:21we can say convert to in32 and then we
- 3:15:23can have a console read
- 3:15:26line and that will read in from the
- 3:15:28console convert it to an integer 32 and
- 3:15:31then store it into the first position of
- 3:15:33our numbers array and now if we just
- 3:15:35copy this down
- 3:15:37twice and we can get rid of these two
- 3:15:39lines cuz this is just staring it
- 3:15:41statically and add one and two to here
- 3:15:44so now what we're saying is enter a
- 3:15:46number star into to 0 1 and 2 and let's
- 3:15:49see what happens now so now we get asked
- 3:15:52the numbers 10 20 and 30 and you can see
- 3:15:54that they've been stored here of course
- 3:15:57these haven't changed cuz this is still
- 3:15:58referencing our static variables up here
- 3:16:01so what you can see in this example is
- 3:16:03now that we've made an array we can
- 3:16:05specify each of the values very easily
- 3:16:07inside here and if so we wanted to have
- 3:16:10a number four instead of having to
- 3:16:12duplicate it and rename it and change
- 3:16:14the value for example we can just simply
- 3:16:17turn this into four duplicate this
- 3:16:19little section and then just change this
- 3:16:21to a three one thing to note in C and
- 3:16:25many other languages almost every single
- 3:16:27index will start at zero in the last
- 3:16:30section we covered the string functions
- 3:16:32and they also started at zero when
- 3:16:34you're dealing with strings and their
- 3:16:36positions when you have a for loop with
- 3:16:38the string then you always want to use
- 3:16:40index Z as the first position the same
- 3:16:43is true here we have four positions
- 3:16:46which means it goes from zero all the
- 3:16:48way till three it never goes from 1 to 4
- 3:16:51or from 0 to 4 these are bad values it
- 3:16:54always goes from 0 to
- 3:16:56three you might be thinking this looks
- 3:17:00really good but when we go to print it
- 3:17:02we're going to have to copy and paste
- 3:17:03this little section add another one and
- 3:17:05rename this to three this gets a little
- 3:17:07bit tedious if you just trying to print
- 3:17:09them all to the screen so let's see how
- 3:17:11we can fix that we can use a four Loop
- 3:17:13for this so we can have four inti equals
- 3:17:16z
- 3:17:17I is less than and then we can have
- 3:17:19numbers length and if you notice from
- 3:17:22iterating through a string variable the
- 3:17:24syntax is exactly the same we have a do
- 3:17:27length property on the arrays as well as
- 3:17:29we do with a string
- 3:17:31variable so in order to print out this
- 3:17:33line what we can do is print one of them
- 3:17:36every time we go around the for Loop
- 3:17:38instead of using a console right line we
- 3:17:40can just use a console
- 3:17:42right so we can have console.
- 3:17:45right and then what we can simply do in
- 3:17:47here is we can say numbers square
- 3:17:50bracket I and then we can have a space
- 3:17:53right after it just to M make what
- 3:17:56happens here we have the number printed
- 3:17:57out and then a space after it number
- 3:18:00printed out a space after it and so on
- 3:18:02and so forth so let's see what happens
- 3:18:04now if we run the code 10 20 30 40 there
- 3:18:10you go so you can see that these two
- 3:18:11lines are exactly the same and now what
- 3:18:13happens is let's just comment these two
- 3:18:15lines out and if we were to change this
- 3:18:17to five and we can duplicate the
- 3:18:20section change this to a four and then
- 3:18:23now we have five variables started and
- 3:18:25we don't need to touch this for Loop
- 3:18:26because the numbers do length will get
- 3:18:28updated anyway so we can have 1 2 3 4 5
- 3:18:32and you can see all five variables have
- 3:18:34been printed automatically what also is
- 3:18:36quite tedious here is we're having to
- 3:18:38add another one to this just so we can
- 3:18:40ask the user how many times that they
- 3:18:42want to enter a number again this is
- 3:18:44quite tedious but we also have a fix for
- 3:18:46that
- 3:18:47because we know how many times we want
- 3:18:49to ask the user to enter a number then
- 3:18:51we can use a for Loop to iterate through
- 3:18:53and ask the user as you can see every
- 3:18:56single line here is exactly the same
- 3:18:58except for the index position that we're
- 3:19:00dealing with and the same is true for
- 3:19:02this for Loop this line will always be
- 3:19:05the same as we're printing out here and
- 3:19:07the only thing that changes between this
- 3:19:09section and this section is the fact
- 3:19:11that this says one and this says zero so
- 3:19:14we can translate this into a f Loop to
- 3:19:17make it
- 3:19:18Dynamic and we can do exactly the same
- 3:19:20thing above here so let's actually copy
- 3:19:23and paste this for
- 3:19:25Loop because we want to do exactly the
- 3:19:27same
- 3:19:29approach we want to go through all the
- 3:19:31positions inside our numbers array and
- 3:19:33then we can actually ask the user to
- 3:19:35enter a number in so let's block comment
- 3:19:37out this massive section and then we can
- 3:19:40take one of them and place it inside the
- 3:19:41for Loop so now we're asking the user
- 3:19:44enter a number and then we're storing
- 3:19:46inside index IND Z and of course if we
- 3:19:48keep using index zero then it's just
- 3:19:50going to override the first one and
- 3:19:51we're not going to store any data so we
- 3:19:53can change that to an i let's see what
- 3:19:55happens when we run it now 1 2 3 4 5 and
- 3:19:59then we get the five numbers being
- 3:20:01printed this is awesome because what
- 3:20:03we've got now is five output commands
- 3:20:05coming straight to the console just from
- 3:20:07a for Loop the for Loop is responsible
- 3:20:10for printing them all out to the screen
- 3:20:11reading in the value convert it to an
- 3:20:14integer and then storing it inside our
- 3:20:15array and then we have a separate for
- 3:20:17Loop to print out to the screen what's
- 3:20:19really really powerful about these two
- 3:20:21for Loops that we've built is if we want
- 3:20:23to have 10 numbers now we simply just
- 3:20:25change this to 10 and then just rerun
- 3:20:27the code as you can see we now have 10
- 3:20:30numbers there you go so what we've just
- 3:20:33done if we move this code down a lot now
- 3:20:35just so we can have it close
- 3:20:37by we've made our entire program that
- 3:20:40was actually pretty lengthy into just a
- 3:20:42couple of lines and two for Loops one
- 3:20:45for Loop is responsible for looping x
- 3:20:47amount of times x depends on whatever
- 3:20:49that number is and then we go through
- 3:20:51and ask the user to enter a number read
- 3:20:54in from the console convert it to an
- 3:20:56integer 32 and start it within the array
- 3:20:59after this for Loop has finished running
- 3:21:01then we have all the numers stored
- 3:21:03inside our aray and then we can go
- 3:21:05through and print them all out to the
- 3:21:06console another way we can print this
- 3:21:09out to the screen is by actually using a
- 3:21:11for each so we can say int num and then
- 3:21:14we want to be a number inside our number
- 3:21:17so for each of the integers inside the
- 3:21:20number we want to create a new local
- 3:21:21variable called num and then we can use
- 3:21:23it in here and if we take this line and
- 3:21:25copy it down everything's exactly the
- 3:21:28same except we don't need to specify the
- 3:21:30main array numbers now and we don't need
- 3:21:32to specify the square brackets we can
- 3:21:34just change this completely and replace
- 3:21:36it with n and if we just make this
- 3:21:38number smaller so we don't have to
- 3:21:39Output a lot of
- 3:21:41values 1 2 and three and you can see
- 3:21:44that they're exactly the same there's no
- 3:21:46line between them CU I don't have a
- 3:21:47console right line so let's quickly add
- 3:21:50that
- 3:21:51in and there we go 1 2 3 and you can see
- 3:21:54they're outputting the exact same
- 3:21:59values the reason why I needed a console
- 3:22:01right line is because these fall Loops
- 3:22:02are using console. right the problem
- 3:22:04with console. right is even after
- 3:22:06finishing writing all of the lines it
- 3:22:08doesn't continue to the next line it
- 3:22:11stays there so if I don't break up the
- 3:22:13two outputs with the console right line
- 3:22:15then all six outputs from the console
- 3:22:17right and the other console right will
- 3:22:19all go on one line this is just for
- 3:22:21demonstration purpose anyway either one
- 3:22:23of these Solutions are fine more people
- 3:22:26tend to use this one the problem with
- 3:22:28these two options is you can't always
- 3:22:29use them
- 3:22:31interchangeably this one will always go
- 3:22:33through every single position it will
- 3:22:36always go from zero and it will always
- 3:22:37go to the length the problem with this
- 3:22:40fall Loop is you can't choose how many
- 3:22:42times you want to iterate through before
- 3:22:44you stop there might be a very specific
- 3:22:46case where you have a big array of data
- 3:22:48but you only want to select a specific
- 3:22:50range if you example where you had a
- 3:22:52th000 array positions so from 0 to 999
- 3:22:56and let's say you wanted to request and
- 3:22:58run a for loop from 500 to 600 so you
- 3:23:01would literally type in 500 in here and
- 3:23:04then 600 inside here and this would be
- 3:23:07fine as long as your array has that many
- 3:23:09positions but with the 4 each Loop if
- 3:23:12you run a 4 each loop on a thousand
- 3:23:14length array then it's going to go
- 3:23:15through every sing single position in
- 3:23:17some cases you might not want that and
- 3:23:19you only want to go through specific
- 3:23:21cases so if you know you're going to go
- 3:23:23through every single case then you can
- 3:23:25use the for each but if you know you're
- 3:23:27going to go through a certain amount of
- 3:23:28cases then it's always best to use the
- 3:23:30for Loop okay so we covered quite a lot
- 3:23:32in this video so let's just have a recap
- 3:23:35so initially we started off saying we
- 3:23:37have our gome one gome two and gome
- 3:23:39three then we had our static variables
- 3:23:41stored inside there and then we created
- 3:23:43our integer array which is now down here
- 3:23:46and just just to make it a little bit
- 3:23:47easier to store multiple variables
- 3:23:49because if you wanted five variables
- 3:23:51you'd now have to duplicate that and
- 3:23:52change that to number five and this will
- 3:23:54get very tedious if we had something
- 3:23:56like 10 like before this is very long
- 3:23:59and you don't need be specifying number
- 3:24:0110 in your code because this also hurts
- 3:24:03the readability of it using arrays can
- 3:24:06solve this problem quite a lot you can
- 3:24:08easily specify array using this notation
- 3:24:11int square brackets followed by the name
- 3:24:14and you make that equal to a new int
- 3:24:16three the three can be any number that
- 3:24:19you want as long as it's a whole number
- 3:24:20cuz this tells us how many positions
- 3:24:22that we've got we then outputed lots of
- 3:24:24values to the console and read them into
- 3:24:26our Ray but we had the same problem as
- 3:24:29we did above where we just had to keep
- 3:24:31duplicating the sections so what we did
- 3:24:33to prevent that is by writing a little
- 3:24:35for Loop that went through every single
- 3:24:37position in numbers array outp puted
- 3:24:39something to the screen read in the
- 3:24:41value converted it and stored it into
- 3:24:43the array and then after all that was
- 3:24:45done we can use a for Loop or for each
- 3:24:48Loop and print that back to the screen
- 3:24:50now that we finished the base content of
- 3:24:51this video and because I said at the
- 3:24:53start that I was going to take the
- 3:24:54triangle exercise and convert that into
- 3:24:56an array so let's do that now so now
- 3:24:59I've copy and paste a solution from the
- 3:25:01triangle exercise straight into this
- 3:25:03project I've just block commented
- 3:25:05everything above just so it doesn't
- 3:25:06interfere with our code so let's just
- 3:25:08run this code to familiarizes with
- 3:25:10what's going on so we enter 60 60 and 60
- 3:25:14then it says valid
- 3:25:17and if you run it again and enter
- 3:25:18something else that doesn't equal to 180
- 3:25:20then we get invalid so let's see how we
- 3:25:23can make this code a lot better by the
- 3:25:25use of arrays so as we said before we
- 3:25:27don't want to be using this angle one
- 3:25:29angle two and angle three let's make an
- 3:25:32INT square bracket angles equal new int
- 3:25:36and then in this case we have three
- 3:25:38angles and what you could do as well is
- 3:25:40you could say const int angle count
- 3:25:43equals
- 3:25:443 and then you can simp L use that
- 3:25:46inside here so later on in the code if
- 3:25:49we wanted to specify this to a different
- 3:25:51value we can just change that later
- 3:25:53on so now that we've got three angles we
- 3:25:56can get rid of these three lines and
- 3:25:58write ourselves a little for Loop so I
- 3:26:01goes from zero all the way to the
- 3:26:03angles. length and then every single
- 3:26:06time we go around in the for Loop we
- 3:26:08want to
- 3:26:10say console. right enter angle one and
- 3:26:15then we can say angles square bracket
- 3:26:19and not zero in this case cuz we don't
- 3:26:21be specifying index as it will go from
- 3:26:23zero to one to two we want to be using I
- 3:26:27I because we're inside a for Loop that's
- 3:26:29already going through all the indexes
- 3:26:31for us and make that equal to convert to
- 3:26:34in32 and then a console.
- 3:26:37readline now we've got all the values
- 3:26:39stored inside here let's deal with this
- 3:26:42problem so if we just comment out the
- 3:26:44total for the time being let's run the
- 3:26:46code
- 3:26:51oh we have one more error down here
- 3:26:53sorry let's run the code again and we
- 3:26:55can say enter angle one and we can type
- 3:26:57the values in but as you can see we've
- 3:27:00got angle one for each of the times what
- 3:27:03we can do in here is use our I index to
- 3:27:05print out the value so we can turn this
- 3:27:08into interpolation and then wrap the one
- 3:27:10inside curly braces and we can simply
- 3:27:12change as one to an i and let's see what
- 3:27:15happens now so we have enter angle zero
- 3:27:18and then one and then two and this is
- 3:27:20fine but humans don't like reading zero
- 3:27:23they like reading 1 2 and three so what
- 3:27:25we can simply do is say I + 1 and
- 3:27:28because we're using I and then adding
- 3:27:30one to it it's not going to affect the
- 3:27:32variable inside the for Loop so there's
- 3:27:34no problems by doing this this is simply
- 3:27:36just for the output purposes so now we
- 3:27:39can have one two and three just shown up
- 3:27:41like that perfect so what we can do now
- 3:27:44that we have all the angles stored now
- 3:27:47we need to replace this line by writing
- 3:27:49a for Loop that can add up all the
- 3:27:50values for us so we can have four each
- 3:27:53in angle inside our angles and we can
- 3:27:57just say int angle sum plus equals
- 3:28:03angle the problem with this is we can't
- 3:28:06specify a new variable inside here and
- 3:28:09then use it outside an if statement so
- 3:28:11what do we do in this case you want to
- 3:28:13take angle sum and initialize it out
- 3:28:16outside the for Loop and then give it a
- 3:28:18value of zero and then inside the for
- 3:28:20Loop we can remove the int and we can
- 3:28:22say angle sum plus equals the whatever
- 3:28:25angle that we're currently up to so if
- 3:28:28we had three values that's going to say
- 3:28:300 + 60 and then take that value and add
- 3:28:33another 60 and then take that value and
- 3:28:35add another 60 so it'll keep a running
- 3:28:37total for us and if we just print out
- 3:28:39angles some to the screen let's see what
- 3:28:41happens 10 20 30 and now we have 60 and
- 3:28:45that's the correct calculation if you
- 3:28:47add them three together so now that we
- 3:28:50have a running total for angle sum then
- 3:28:52we're pretty much done if we get rid of
- 3:28:54this line of code which is our previous
- 3:28:56total and we actually bring back our if
- 3:28:59statement there you go there's no error
- 3:29:01now and all this is doing is checking if
- 3:29:03the angle sum is equal to 180 and then
- 3:29:06print valid and invalid so we can have
- 3:29:0860 60 60 and it prints out valid and
- 3:29:12let's just comment out our
- 3:29:13output and then we can have 66 6059 and
- 3:29:17then it prints out in valid which is the
- 3:29:19exact same behavior that we saw at the
- 3:29:20start so let's remove the extra cord in
- 3:29:23here and keep it like this there you go
- 3:29:27so now we've got our solution just like
- 3:29:29this and it's a lot better because if
- 3:29:31triangles were a transform in the future
- 3:29:33and we wanted more than three sides then
- 3:29:35we can easily update this value because
- 3:29:37now that we're using AR raise or we're
- 3:29:38using a for Loop to enter out the values
- 3:29:41what we can do we don't actually need to
- 3:29:43store it what I mean by that is we can
- 3:29:45actually just get rid rid of this array
- 3:29:46completely let me
- 3:29:49demonstrate we can take this angle sum
- 3:29:52and place it underneath here we can keep
- 3:29:54the count so we can use that in the for
- 3:29:56Loop so we're going from 0o to three
- 3:29:58because that's our angle count and then
- 3:30:00we can get rid of all
- 3:30:02this and let's see what we can do now so
- 3:30:06we have an angle count equals 3 so we go
- 3:30:08from 0 to three which you'll just do as
- 3:30:10we said before from 0 to two because
- 3:30:13it's less than the angle count and let's
- 3:30:16just print this to the screen see what
- 3:30:17happens so the output's still exactly
- 3:30:19the same but what's happening is these
- 3:30:21values are not being stored they're just
- 3:30:23being shown to the screen and then not
- 3:30:25done anything there is no equal sign
- 3:30:28here to assign it into anything so what
- 3:30:30we can do is we can say angle sum plus
- 3:30:33equals the value straight away and now
- 3:30:35if we try it 60 60 60 then we get valid
- 3:30:39the main differences between these two
- 3:30:41situations is in the first one we used
- 3:30:44an array to store all the different
- 3:30:46values and then we added the total but
- 3:30:49if we're not going to use these values
- 3:30:51after then what's the point in storing
- 3:30:53them we can just use a for Loop to ask
- 3:30:55the user how many times they want to
- 3:30:57input the values and then just add them
- 3:30:59every single time that they add the
- 3:31:01calculation in and at the end we could
- 3:31:03just simply use a conditional operator
- 3:31:05to be able to print at the screen
- 3:31:06whether it's valid or not but let me
- 3:31:08just undo that solution so I can show
- 3:31:10you the array solution and we can have a
- 3:31:12quick recap that was just an extra
- 3:31:13little tip just in case you wanted to
- 3:31:15know know what different ways you could
- 3:31:17use to make this work there we go so
- 3:31:19let's just have a quick recap here so we
- 3:31:21have a Conant which all we'll do is let
- 3:31:24us specify three inside here and then we
- 3:31:26can use it here this isn't entirely
- 3:31:28necessary but it does clean up your code
- 3:31:30a little bit because it gives this
- 3:31:32number three a bit of a meaning it says
- 3:31:34the amount of angles is our angle count
- 3:31:37having a three left over like this is
- 3:31:39what we call a magic number they're
- 3:31:41quite bad because it hurts the
- 3:31:42readability of your code people want to
- 3:31:44know what the three actually means so if
- 3:31:47you bring in an angle count then you can
- 3:31:49actually read we want a set of angles
- 3:31:51and this is the amount that we need and
- 3:31:53that's denoted by angle count it adds
- 3:31:56text to a variable it gives you a way of
- 3:31:59making sure that the text equivalent of
- 3:32:02a variable is present so someone can
- 3:32:04just read it after we've created our
- 3:32:06integer we can actually make a for Loop
- 3:32:08that goes from zero to the length and
- 3:32:10then we ask the user to enter the angle
- 3:32:12and in the previous example we had enter
- 3:32:15angle one two and three so now we can
- 3:32:17mimic that by using I but because I
- 3:32:20starts at zero we want to use plus one
- 3:32:22every time we loop around the for Loop
- 3:32:24we ask the user to enter the number read
- 3:32:26it in through console read line convert
- 3:32:28it using convert 2 in 32 and then St
- 3:32:31into the array after this for Loop is
- 3:32:33finished we have all three values stored
- 3:32:35in the angles array and then we have an
- 3:32:37In Sum to count up all the values that
- 3:32:39we need and then we use the fall Loop to
- 3:32:42go through every single integer value
- 3:32:44inside and make a running to total and
- 3:32:46store it inside angle sum once angle sum
- 3:32:49has the total count then what we can do
- 3:32:51is use console right line along with a
- 3:32:53conditional operator to print whether
- 3:32:55it's valid or invalid to the
- 3:32:59screen in this video we're going to look
- 3:33:01at how we can use the built-in array
- 3:33:03functions to be able to sort an array so
- 3:33:05if you have an INT array called numbers
- 3:33:08and then using the previous notation
- 3:33:09that we looked at we can actually
- 3:33:11specify any numbers that we want using a
- 3:33:13common delimited list so if we just have
- 3:33:16nine numbers inside here then we've
- 3:33:18actually defined an integer array and
- 3:33:20then the compiler can count the amount
- 3:33:21of items that are inside here and give
- 3:33:23it the length but we don't have to worry
- 3:33:25about that ourselves so if we use a for
- 3:33:27each and we can just print
- 3:33:30Inn inside numbers we can do a console
- 3:33:34right line on the new local variable
- 3:33:36called num if print it out we can see
- 3:33:38all of the values and maybe we can
- 3:33:40actually just use a console
- 3:33:43right followed by a space just to give
- 3:33:46them all on the same line there we go so
- 3:33:49as you saw before if you had a string
- 3:33:51called test and we wanted to do
- 3:33:53something to it for example if we wanted
- 3:33:55to use the replace function if we wanted
- 3:33:57to remove all spaces with an empty
- 3:33:59string then if we check the replace tool
- 3:34:01tie we can see that it returns a new
- 3:34:03string most of the string functions will
- 3:34:05return you back the new string after
- 3:34:07doing something with it but in this case
- 3:34:10if we type in Array followed by do sort
- 3:34:13and we can place our array straight in
- 3:34:15the bracket
- 3:34:16what you realize is if you hover over
- 3:34:18sord it will sort the elements in the
- 3:34:20entire array but it will indeed return
- 3:34:22back a void and vo just means it returns
- 3:34:25back nothing so in this case what it's
- 3:34:27doing is you're actually affecting the
- 3:34:29numbers array straight away and you
- 3:34:32don't have to do numbers equals array do
- 3:34:34sort numbers for example if we were
- 3:34:36doing the test replace we would have to
- 3:34:38do this otherwise this would just be
- 3:34:41returning back a string but we wouldn't
- 3:34:43be using it the same is not true for the
- 3:34:45arrays when you run the array functions
- 3:34:48they tend to return back void and you
- 3:34:50don't have to do anything other than
- 3:34:52give it the array that you wanted to
- 3:34:53sort so now if you run this code you
- 3:34:56should just see the same output since
- 3:34:57they already sorted anyway so let's
- 3:35:00change some of these numbers
- 3:35:02up and just see what happens
- 3:35:05now there we go 02 55 6 7 8 999 and
- 3:35:10that's it sorted just like that there
- 3:35:12isn't a whole lot to this sorting
- 3:35:14function but what you should note is you
- 3:35:16can use this new array syntax to Define
- 3:35:18how many items that you want in there
- 3:35:20instead of having to change the amount
- 3:35:22of positions that you have using the
- 3:35:23square brackets you can just denote any
- 3:35:26number of items that you want inside the
- 3:35:28compiler will count them for you and PR
- 3:35:30the length straight into the bracket for
- 3:35:32you so you don't have to worry about it
- 3:35:33using the array. sort function all you
- 3:35:36have to do is give it the parameter of
- 3:35:38the array that you want to sort and it
- 3:35:40will affect the array straight away and
- 3:35:42you don't have to restore it and then
- 3:35:44we're just using a simple 4 each Loop to
- 3:35:46print them out to the
- 3:35:49screen in this video I want to discuss
- 3:35:51the array reverse function so if you had
- 3:35:53an INT numbers equal new int and if you
- 3:35:57just had lots of numbers in here
- 3:35:59defined just up till five then we can
- 3:36:01add a semicolon and again using this
- 3:36:04array function we can say array. reverse
- 3:36:06and pass our array
- 3:36:09inside and then we can just use a for H
- 3:36:11of our item inside our
- 3:36:14numbers and simply just print them out
- 3:36:16using a console. write and we can use
- 3:36:18string interpolation and just put a
- 3:36:19space at the end there we go and if you
- 3:36:22run the code then you can see our
- 3:36:25numbers have just been reversed there
- 3:36:26isn't a lot to this video I just wanted
- 3:36:28to show you about the different
- 3:36:29functions that you have available for
- 3:36:31arrays these can come in handy when you
- 3:36:33have something like a list returned from
- 3:36:34a database for example and it's ordered
- 3:36:37in a certain way let's say you're
- 3:36:38returning back a list of lots of people
- 3:36:40who are ordered back alphabetically and
- 3:36:42it's going from a to zed and let's say
- 3:36:44you have some sort of user interface
- 3:36:46allows you to flip the order instead of
- 3:36:48having to request the database to get
- 3:36:50the data back in the opposite order you
- 3:36:52can use this array function to just
- 3:36:53reverse the list the same thing goes if
- 3:36:55you're trying to order by lowest price
- 3:36:57or highest price you can use this to
- 3:36:59show the different values in different
- 3:37:00orders that's where this array function
- 3:37:02comes in handy when you have different
- 3:37:04situations like the names or the price
- 3:37:06by ascending or descending where you can
- 3:37:08just flip the data instead of having to
- 3:37:11request it back from the database again
- 3:37:13flipping it locally is a lot easier than
- 3:37:15going back to the database is that uses
- 3:37:17more data and more computational power
- 3:37:19on the back end and then return it to
- 3:37:21the front end it's always best to be
- 3:37:22able to use these functions instead of
- 3:37:24having to use external things to be able
- 3:37:26to return back the same data using it
- 3:37:29locally like this will give us a
- 3:37:30performance Advantage because it's done
- 3:37:32directly on the data instead of having
- 3:37:34to request everything again if you
- 3:37:36wanted to do this manually let's have a
- 3:37:38look and see what your options are you
- 3:37:41can have your integer array just defined
- 3:37:43like this with a dynamic list and then
- 3:37:45we can have a sorted array using the
- 3:37:47same length as the current array what
- 3:37:49you can do instead of using reverse we
- 3:37:52can actually just comment these lines
- 3:37:53out you can bring back a normal for Loop
- 3:37:56but if we use two RS it gives us a
- 3:37:58reverse for Loop so the length is going
- 3:38:00to be our numbers do length and the rest
- 3:38:03of the paramet is exactly the same we
- 3:38:05want to go from the last minus one all
- 3:38:07the way till zero and we want to
- 3:38:09decrement in one and we can say our
- 3:38:11sorted number I is equal to numbers all
- 3:38:15right but all this will do right now is
- 3:38:17just give us back the same value stored
- 3:38:19so what we need to do in a case like
- 3:38:21this is make another variable something
- 3:38:23like X and we can store that into zero
- 3:38:26and what we can say is X is assigned the
- 3:38:28value of whatever this numbers is
- 3:38:30because this is going backwards but we
- 3:38:32want this one to go forwards cuz we want
- 3:38:34it to be reversed and we can say x++ and
- 3:38:37see what happens when we run it
- 3:38:39now so nothing's going to come up
- 3:38:41because we don't have any output so
- 3:38:42let's use a for
- 3:38:44each we can have VAR
- 3:38:47number inside our sorted numbers and we
- 3:38:50can just take this and place it in here
- 3:38:52and just print out the item and we can
- 3:38:54use
- 3:38:56num and there you go it's been flipped
- 3:38:59the exact same value is just the other
- 3:39:01way so this is obviously a little bit
- 3:39:03more complicated because you have to use
- 3:39:05an additional variable use a backwards
- 3:39:07for Loop and then incent your own
- 3:39:09separate variable and as well as
- 3:39:11completely make a whole new array of
- 3:39:13course you should only do this if you
- 3:39:14need it as two separate arrays if not
- 3:39:16you can always just affect the one that
- 3:39:18you're up to right
- 3:39:20now because I said before that arrays
- 3:39:23are actually static data and they're
- 3:39:24going to be fixed size then there's no
- 3:39:27way to remove or add an item but you can
- 3:39:29clear what you have already so let's
- 3:39:31have a look array. clear sets a range of
- 3:39:35elements in an array to the default
- 3:39:37value of each element type so the first
- 3:39:39parameter takes our array so let's use
- 3:39:41numbers and we want to start off the
- 3:39:43index that we want to clear from and
- 3:39:45let's say zero and because we want to
- 3:39:47clear the entire array let's go all the
- 3:39:49way to numbers. length and now let's
- 3:39:52just produce a for each our item inside
- 3:39:55numbers and then just have a console
- 3:39:57right string interpolation which just
- 3:40:00prints out the item followed by space
- 3:40:02and now let's run the code and see what
- 3:40:04happens so we have lots of zeros in the
- 3:40:06console and this is to be expected
- 3:40:08because as we've talked about before the
- 3:40:10default value for an integer is indeed
- 3:40:13zero so all this is doing when it says
- 3:40:15is it's clearing it's just actually
- 3:40:17assigning all of the positions the value
- 3:40:19of zero and we can mimic this quite
- 3:40:21easily through a for Loop so if we
- 3:40:24comment this out and write ourselves as
- 3:40:26a for Loop in I equals z and we want to
- 3:40:28go to numbers. length and then simply
- 3:40:31inside we just want to say numbers
- 3:40:33square braet I equals to 0 what you can
- 3:40:37also write is the word default this will
- 3:40:39pick up the default value for the data
- 3:40:41type that you specified you can type in
- 3:40:43zero or you can type in default it's up
- 3:40:46to you but I think in this case if
- 3:40:48you're trying to reset it back to
- 3:40:49default then you don't have to worry
- 3:40:51about remembering it or any typos you
- 3:40:53can just use the keyword default so if
- 3:40:56we run this code now let's see what
- 3:40:57happens so effectively we've mimicked
- 3:41:00what this function is doing but let's
- 3:41:02have a look and see what other
- 3:41:03parameters we have access to so if we
- 3:41:06comment this out and drop the value then
- 3:41:08you can still see we only have one
- 3:41:10Constructor normally you'd see the open
- 3:41:12down arrows on the left hand side to say
- 3:41:14one of two in this case we only have
- 3:41:16access to one Constructor it will only
- 3:41:18give us a range of elements we want to
- 3:41:21remove but really we just want to clear
- 3:41:23it does say here in the tool tape the
- 3:41:25array whose elements need to be cleared
- 3:41:28and because it is a clear and not a
- 3:41:29remove you got to remember when using
- 3:41:31this function it doesn't actually remove
- 3:41:33any of the positions it just resets the
- 3:41:35values inside the array so what we can
- 3:41:37do in this case is we can say numbers
- 3:41:40comma 5 comma 5 and what that will do is
- 3:41:44the first param arer is where you want
- 3:41:46to start so if you want to start at the
- 3:41:48fifth position and then we want to count
- 3:41:50five forward and because we have 10
- 3:41:53numbers then this should return back
- 3:41:54okay let's comment out this for Loop so
- 3:41:57it doesn't take an effect and let's run
- 3:41:59the code and see what happens now so now
- 3:42:01we took the position five which is
- 3:42:03actually the sixth number because it's
- 3:42:04zero base and said we want to remove
- 3:42:07five forward so it's s and then four
- 3:42:10more forward and there we go we have
- 3:42:12five numbers being removed so instead of
- 3:42:14removing all of the different values we
- 3:42:16can actually clear a certain amount that
- 3:42:18we want and to mimic this in a for Loop
- 3:42:21we can do the following we can start it
- 3:42:23at five and we want to go to 10 and
- 3:42:26let's run the code and you can see the
- 3:42:29output is exactly the same when doing in
- 3:42:32a for Loop you need to actually
- 3:42:33calculate what this is going to be so we
- 3:42:35need to take both the numbers the fifth
- 3:42:37index and the five length which is 5 + 5
- 3:42:40and then we get 10 and we don't make
- 3:42:42this less than or equal to 10 because we
- 3:42:45have 10 numbers so we actually want to
- 3:42:47go from position five to position 9
- 3:42:50which will equate to less than 10 we
- 3:42:52actually want to go from position five
- 3:42:54all the way to position 9 which actually
- 3:42:56covers five numbers 5 6 7 8 9 even
- 3:43:01though it feels like the difference
- 3:43:02between the two numbers is only four
- 3:43:05because 9 - 5 is 4 but we actually have
- 3:43:07five numbers between that 1 2 3 4 5
- 3:43:11there we go so when we print the values
- 3:43:14from 5 to till less than 10 we're going
- 3:43:16from 5 to 9 which is exactly what this
- 3:43:18is doing this is the index that you
- 3:43:21start at and this is how many numbers
- 3:43:23you want to count forward so let's just
- 3:43:25have a quick recap of this function we
- 3:43:27defined our numbers array using array.
- 3:43:30clear it doesn't actually clear the
- 3:43:32values it just gives us access to
- 3:43:34removing them back to their default
- 3:43:36values it doesn't actually clear the
- 3:43:38array it just resets the values back to
- 3:43:40their default type we can have numbers
- 3:43:43parameter which will equal to our array
- 3:43:45and we want to say we want to go from
- 3:43:47index zero all the way to numbers.
- 3:43:50length which will be every single value
- 3:43:52inside the array and all that will do is
- 3:43:54go through the array and set them all to
- 3:43:56their default value which is similar to
- 3:43:59what this for Loop is doing and before
- 3:44:01we had in I equals 0 and I is less than
- 3:44:04numbers. length and that will go through
- 3:44:06every single position you can
- 3:44:07technically use zero instead of default
- 3:44:10but that would mean you have to remember
- 3:44:11the default value of every single data
- 3:44:13type it's easier to just the default
- 3:44:15keyword and it doesn't promote any
- 3:44:17typers using the default keyword is by
- 3:44:20far the easiest and the most safest
- 3:44:22option if you're going to have this
- 3:44:23situation so making every number's index
- 3:44:26equal to default in this case because
- 3:44:28we're using integers it's going to
- 3:44:30assign the value of zero and then after
- 3:44:32this follow loes run with I is zero and
- 3:44:34I is less than numbers. length it's
- 3:44:37going to make all of these positions
- 3:44:38equal to zero which is in essence what
- 3:44:41this function is here for because arrays
- 3:44:43can't be added or changed in terms terms
- 3:44:45of the values that they have after
- 3:44:46they've been created you can only really
- 3:44:49modify what's already there or you can
- 3:44:51reset them all back to zero using this
- 3:44:53notation from zero to numbers length you
- 3:44:56can get rid of all the values and make
- 3:44:58them all zero using 5 to five means it
- 3:45:01will start index 5 which is the sixth
- 3:45:03position and then go five numbers
- 3:45:05forward so it will delete these and
- 3:45:07reset them back to zero you can mimic
- 3:45:09this in a follow Loop just like this
- 3:45:11start in I at five and then go to less
- 3:45:14than 10 and the reason why it's less
- 3:45:16than 10 is 5 + 5 gives you the 10th
- 3:45:18index but because it's I is less than 10
- 3:45:21you actually want to go to the 9th index
- 3:45:24because from 5 to 9 that's actually
- 3:45:26covering five different numbers and then
- 3:45:28we can go through and assign the default
- 3:45:30value and then when we print it out
- 3:45:32you'll see that all of these numbers
- 3:45:34have turned into zeros just like
- 3:45:38so searching through arrays are very
- 3:45:41helpful when you try to find some data
- 3:45:43inside your array and you want to return
- 3:45:45back the position to maybe do something
- 3:45:47else with it so let's have a look at
- 3:45:48this example so we have a numbers array
- 3:45:51and let's say we want to search number
- 3:45:53199 and find the index of it so first
- 3:45:56let's ask the user what the number they
- 3:45:58want to search enter number to search
- 3:46:01then we can have in search is equal to
- 3:46:04convert to in32 and we're converting the
- 3:46:07console.
- 3:46:09readline and then let's have a look at
- 3:46:11the function so inside the array Library
- 3:46:13we have an index of surges for the
- 3:46:16specified object and Returns the index
- 3:46:18of the first occurrence in a
- 3:46:19one-dimensional array this is very
- 3:46:21important as it says the first
- 3:46:23occurrence which means if there was
- 3:46:25another 199 at the end it's only going
- 3:46:28to return you back this one and then
- 3:46:30stop immediately and not give you the
- 3:46:32value of this one so let's have a look
- 3:46:34in the parameters and see what it takes
- 3:46:36it takes our array and then a value so
- 3:46:39our array is called numbers and the
- 3:46:41value you want to search by is our
- 3:46:42variable called search if you hover over
- 3:46:44the index function again it says that it
- 3:46:47returns back at integer zerob based
- 3:46:49index of the first occurrence of the
- 3:46:51value in the entire array if it was
- 3:46:53found otherwise the value is going to be
- 3:46:55minus one so we can say inter position
- 3:46:58is equal to this and because it says if
- 3:47:01it's going to be found then it's going
- 3:47:03to have the value of a zero index and if
- 3:47:05it's not going to be found it's going to
- 3:47:07have the value of minus1 so we can say
- 3:47:09if the position is greater than minus1
- 3:47:12because if it's greater than minus1 that
- 3:47:14means the value is zero onwards we can
- 3:47:18also write this by saying if the
- 3:47:19position is greater than or equal to
- 3:47:21zero it's just up to you they both mean
- 3:47:24the same thing and then we can say in
- 3:47:26here number
- 3:47:29search has been found at position
- 3:47:34position there we go so let's run the
- 3:47:36code and let's do a perfect situation so
- 3:47:39we can say 50 and you can see it says
- 3:47:41number 50 has been found at position
- 3:47:43three and that's the fourth index since
- 3:47:46it's zero based and what you could do if
- 3:47:48you wanted to is actually add one to
- 3:47:50this just so it looks like a bit more of
- 3:47:52a readable number because four is going
- 3:47:54to be the fourth number 1 2 3 4 and
- 3:47:59let's try find a number that hasn't came
- 3:48:01up like 60 it's going to return us back
- 3:48:03with nothing this is really bad because
- 3:48:05it looks like the search has failed
- 3:48:07which it has but you need to Output a
- 3:48:09message to let the user know so have
- 3:48:11cons right line and say number search
- 3:48:15has not been
- 3:48:16found let's type that again 60 number 60
- 3:48:20has not been found so let's see what we
- 3:48:22can do to be able to write this function
- 3:48:24ourselves if we just block comment this
- 3:48:26for the time being we can actually use
- 3:48:29in
- 3:48:31position equals to minus1 and the reason
- 3:48:34why I make equal to minus1 because when
- 3:48:36we read the tool tip with this index
- 3:48:38function it said if it's not going to be
- 3:48:39found it's set to minus one so why don't
- 3:48:41we set it to minus one first and then we
- 3:48:43can try and find it so inti equals z and
- 3:48:46we want to go to numbers. length and
- 3:48:50inside here we want to scan each of the
- 3:48:52positions so we can say numbers square
- 3:48:54braet I and if that is equal to our
- 3:48:57search then we can set the position
- 3:48:59equal to I if the number we're up to is
- 3:49:02equal to what the user has typed in then
- 3:49:04we want to set our position to the index
- 3:49:06we're currently up to and we don't need
- 3:49:08to have an else because we've already
- 3:49:09set it to minus one and after the for
- 3:49:12Loop we can actually take this if
- 3:49:14statement and put it back down here
- 3:49:16because the code's going to be the same
- 3:49:18if the position is greater than minus1
- 3:49:20which in this case is our default value
- 3:49:23so in essence if this value has changed
- 3:49:25then this should always pass EG we've
- 3:49:28found the number so let's run the code
- 3:49:31and let's type a number in 30 now it's
- 3:49:34found at position five now if we try 60
- 3:49:37again number 60 has not been found there
- 3:49:41we go so this function is exactly doing
- 3:49:44some something similar to this it's
- 3:49:46going through every single position
- 3:49:48finding the variable and if they match
- 3:49:50assigning the index and then after just
- 3:49:53returning back to position back to the
- 3:49:55console so let's comment our example and
- 3:49:57let's see what other variants of the
- 3:49:59function we can use and let's comment
- 3:50:00back the first
- 3:50:02one so using this line we can duplicate
- 3:50:05it and just comment it so we have some
- 3:50:07other parameters inside here if we add a
- 3:50:10comma we can actually give it a start
- 3:50:12Index this is the starting index of the
- 3:50:14search and let's say you only want to
- 3:50:17search from this second number onwards
- 3:50:19then you can just type in the number two
- 3:50:21so now if you've run the code and type
- 3:50:22in 90 it's going to say 90 is not found
- 3:50:25cuz 90 is the first one and let's try a
- 3:50:28value like 50 now it says the number 50
- 3:50:31is being found at position 4 because now
- 3:50:33it's included in the range from two
- 3:50:35onwards because this is two onwards
- 3:50:37it'll be position two until the end of
- 3:50:39the array if you wanted a very specific
- 3:50:41range then you can actually give it
- 3:50:43another value so so we can say we want
- 3:50:45to go from zero and then we only want to
- 3:50:47go two forward so if you run that now
- 3:50:50and type in 22 it say is not being found
- 3:50:53cuz we' started here and we said 1 2 so
- 3:50:57now it's only searching between 90 and
- 3:50:59199 and if you wanted to include the
- 3:51:02middle ones you could say you want to
- 3:51:04start a position one and go two forward
- 3:51:06so now you get these two numbers and so
- 3:51:08on and so forth so now if we try number
- 3:51:1090 you'll see it's not being found
- 3:51:13because 90 is not one of these two the
- 3:51:15reason why you'd use different ones like
- 3:51:17this if I revert back the first
- 3:51:20one this will go from two till the end
- 3:51:23this will go specific range and this
- 3:51:25will go through everything the reason
- 3:51:27why these exist is if you have a very
- 3:51:29very big array let's say it's a th000
- 3:51:31indexes then you don't want to search
- 3:51:34everything unless you really need to
- 3:51:36let's say for example you had a nam's
- 3:51:37array and it was sorted alphabetically
- 3:51:40and the person's name that you wanted to
- 3:51:41search began with an a there is no point
- 3:51:44in running the search all the way from a
- 3:51:45to zed if you know it begins with a and
- 3:51:48let's say for example you found the
- 3:51:50first index of the user that has a name
- 3:51:53B so you have now the indexes from the
- 3:51:56start of the array up until the last
- 3:51:59array position that has the value of
- 3:52:02someone's name beginning with a so now
- 3:52:04you have the two indexes of every single
- 3:52:06a name inside this array if this was a
- 3:52:10this was B and this was C and this was D
- 3:52:12and you had these two positions then you
- 3:52:15should only be searching all of the a
- 3:52:17names instead of searching the entire
- 3:52:19array and the reason for that is you'll
- 3:52:21be searching lots more data for no
- 3:52:23apparent reason and it's going to take
- 3:52:24an extra long time for searching
- 3:52:27something that you're never going to
- 3:52:28find if you're searching for a name that
- 3:52:29begins with a there is no point in
- 3:52:31searching for anything A B C D and so on
- 3:52:34and so forth that's where you'd use the
- 3:52:36very specific range you can say you want
- 3:52:38to go from zero all the way up to the
- 3:52:40first B name so then you're only
- 3:52:42searching the a names the this is just
- 3:52:44one example and there is a lot more
- 3:52:47every situation is very unique so use
- 3:52:49the parameters that you want and always
- 3:52:52use the ones that can get you the best
- 3:52:54performance the least amount of things
- 3:52:56that you can search is the best way
- 3:52:58forward if you don't know what you're
- 3:53:00going to be searching then you should
- 3:53:02always use the top one which will just
- 3:53:03search everything if you know where the
- 3:53:06value is going to be or roughly going to
- 3:53:08be then you can use this one or this one
- 3:53:11to give you a better range and it's a
- 3:53:13bit more focused instead of searching
- 3:53:14for everything so a quick recap of this
- 3:53:17index function what it will do is it
- 3:53:19will return you back to position if it's
- 3:53:21found it the position will be zero based
- 3:53:23just as arrays are if it hasn't found
- 3:53:26the position then it's going to return
- 3:53:27this variable with minus one so
- 3:53:30therefore after you've run the code you
- 3:53:32can check if the value is greater than
- 3:53:34minus one which will be from zero
- 3:53:36onwards if the value position is zero
- 3:53:38onwards then you know that that number
- 3:53:40has been found and you can use the
- 3:53:42position variable to Output that to the
- 3:53:44screen if the value position is minus1
- 3:53:47it means the array index function has
- 3:53:50returned back negative and the value
- 3:53:52position is now minus one so you can
- 3:53:54output to the user that it has not been
- 3:53:56found just in my little explanation down
- 3:53:58below you can see that we can mimic this
- 3:54:00function by setting the value of
- 3:54:02position to minus one using a fall Loop
- 3:54:05that goes from zero to numbers length
- 3:54:07and if you want to two as well you could
- 3:54:09use these inside here if you wanted to
- 3:54:12mimic this this goes from two till the
- 3:54:14end so you can say I equals 2 and you
- 3:54:17still want to go to the length this one
- 3:54:19goes from one and then two ahead so you
- 3:54:22can actually put I equal 1 and then
- 3:54:24because it's two ahead from one instead
- 3:54:27of numbers. length it will just be I is
- 3:54:29less than 3 because 1 + 2 gives you the
- 3:54:32three so in order to do this manually
- 3:54:35you can Lube through the elements that
- 3:54:36you want check if the number you're up
- 3:54:38to based off the index position of I is
- 3:54:41equal to the search variable that the
- 3:54:43user typed in of here and if you find a
- 3:54:45match then you can assign the position
- 3:54:48to the value of I after the for Loop is
- 3:54:50run you can check the same if statement
- 3:54:52as we discussed above these are copy and
- 3:54:54pasted down below just to give you an
- 3:54:56example in this little
- 3:55:00section all these arrays have static
- 3:55:02data which means that when you
- 3:55:04initialize them with a certain length
- 3:55:05you can't increase that length or
- 3:55:07decrease the length you can only modify
- 3:55:09the data inside it and you can't delete
- 3:55:11any rows if you create an array of size
- 3:55:14is five and you only stall three people
- 3:55:16in then now you have two positions that
- 3:55:18are being unused and that's unnecessary
- 3:55:21amount of data stored in your memory if
- 3:55:23you're not going to be using it up in
- 3:55:25certain situations like when you're
- 3:55:27making a game for example Tic Tac Toe
- 3:55:29then you know you're always going to
- 3:55:31need a 3X3 grid so a 2d array will be
- 3:55:35perfect in this situation cuz you know
- 3:55:37the exact width and height of your array
- 3:55:39so you can do this perfectly whereas
- 3:55:42lists are quite Dynamic if if you don't
- 3:55:44know how many people are going to be
- 3:55:46within a system for example if you're
- 3:55:48signing up some sort of register for a
- 3:55:51class or for a school or something of
- 3:55:52the sort and you don't know how many
- 3:55:54people are going to be in that class
- 3:55:56then you can use something called a list
- 3:55:58and list a dynamic cuz what you do when
- 3:56:00you initialize it you just say that you
- 3:56:02simply want a list and you can add items
- 3:56:05and you can remove items and you can
- 3:56:07also modify them if you wanted to expand
- 3:56:10the size of an array it's slightly
- 3:56:12complicated but it's not impossible
- 3:56:14so if we had int numbers equals new int
- 3:56:17and let's make that length four and
- 3:56:19let's say we had a numbers array that we
- 3:56:21wanted to increase to length
- 3:56:24five we could have new numbers change
- 3:56:26that position five and then what we're
- 3:56:28going to have to do is from position
- 3:56:30zero all the way to three which is our
- 3:56:33fourth length from the numbers array
- 3:56:35we're going to have to copy that into 0
- 3:56:38to three of the new numbers array so now
- 3:56:40they have the extra data plus the last
- 3:56:43one which will give us our four so
- 3:56:45that'll be five length 0 to three gives
- 3:56:47us our four numbers and then we have one
- 3:56:50empty one so we've just made a new array
- 3:56:52and then copied all the data from
- 3:56:54numbers into new numbers now of course
- 3:56:56this is not really practical that if you
- 3:56:57want to reduce the size or increase the
- 3:56:59size you're going to have to do this
- 3:57:01each time of course you don't want to do
- 3:57:03this you just want to use something
- 3:57:04called a list so let's stick with an
- 3:57:06example and see what we can do so you
- 3:57:08make this length
- 3:57:09three and let's just put three numbers
- 3:57:12inside here if you remember from the
- 3:57:13array video we can also declare it like
- 3:57:15this we can say number 0al 1 and then 1
- 3:57:20and then two and change these values
- 3:57:22these two are doing exactly the same
- 3:57:24thing I'm initializing it with 1 2 and
- 3:57:26three and also just saying that all the
- 3:57:28positions in here are equal to 1 2 and
- 3:57:30three so we don't need these twice but I
- 3:57:32was just showing you as a quick recap so
- 3:57:34we're declaring array we're calling it
- 3:57:36numbers and we're giving it a length of
- 3:57:38three and initializing it to 1 2 and
- 3:57:41three now let's see the syntax to define
- 3:57:43a list
- 3:57:44you start off with a word list and then
- 3:57:46you have a left and a right arrow which
- 3:57:48makes a little section inside the
- 3:57:50section you actually want to State the
- 3:57:51data type that you want so if we're
- 3:57:53copying the numbers array we can have an
- 3:57:55INT inside here and that's it for the
- 3:57:57Declaration of the data type and then we
- 3:57:59can say new numbers or List numbers
- 3:58:02equals to new and then there you go it
- 3:58:06gets Auto filled for you but let's just
- 3:58:08type it in we have list open the arrow
- 3:58:11in in the middle and then close it so
- 3:58:13now it's the same same on both sides but
- 3:58:16because this is a new we need to add a
- 3:58:17Constructor so we can open the curly
- 3:58:19braces and the first Constructor says
- 3:58:21initialize a new instance of the list
- 3:58:23class that is empty and has the default
- 3:58:25initial capacity press the down arrow we
- 3:58:28can give it a capacity of the number of
- 3:58:30elements that the new list can initially
- 3:58:31store so we can start it off with five
- 3:58:34length or three length or whatever it
- 3:58:36doesn't actually matter it just means
- 3:58:38that we actually have three positions
- 3:58:39available immediately if we place three
- 3:58:42in the brackets
- 3:58:44and the last one we can actually give it
- 3:58:46entire collection so let's say if we
- 3:58:48loaded a list of numbers from a database
- 3:58:50or a list of something from database and
- 3:58:53we can actually insert it straight into
- 3:58:55this list by putting a list inside these
- 3:58:57brackets of course right now we don't
- 3:58:59have a list that we're going to assign
- 3:59:01it with so let's just leave this empty
- 3:59:03and close the brackets and add a
- 3:59:04semicolon so we could say list numbers
- 3:59:07do add and then we can add one and then
- 3:59:10duplicate this down and add two and add
- 3:59:12three so now these do have exactly the
- 3:59:15same contents instead of doing this you
- 3:59:17can have the same notation like this so
- 3:59:20you can have the curly braces in
- 3:59:22here and we can put in 1 two and three
- 3:59:25now that's doing exactly the same thing
- 3:59:27and you see I can just keep adding more
- 3:59:29numbers in here and the list simply
- 3:59:31doesn't care but as soon as I add one
- 3:59:33more into here then I'm going to get a
- 3:59:35syntax error because it complains that
- 3:59:37the length is not equal to the numbers
- 3:59:39that we
- 3:59:39have because the list are really Dynamic
- 3:59:42it doesn't matter how many you put in
- 3:59:43here because it can just go on forever
- 3:59:45of course until you run out of memory so
- 3:59:47let's have a little working example just
- 3:59:49to see how this
- 3:59:51works so we can have a for Loop and we
- 3:59:53can go from 0 to 10 or maybe 0 to three
- 3:59:56just so we don't have to enter as many
- 3:59:57values we can have a console right that
- 3:59:59kind of just says enter a number and
- 4:00:02then let's read that in and we can say
- 4:00:04int n equals convert to in32 and we can
- 4:00:08place a console do readline inside
- 4:00:11here and then after we've got the
- 4:00:13console read line we've got an inn
- 4:00:15that's being converted from the console
- 4:00:17input inside our integer num and from
- 4:00:20there we can take our list numbers do
- 4:00:22adds and we can add the num inside there
- 4:00:25or if you want to reduce a variable you
- 4:00:26can just take this line and place it
- 4:00:28straight into here now if you get rid of
- 4:00:31that when this follow Loop runs we'll
- 4:00:33have three iterations let's run the code
- 4:00:35and find out 1 2 three so now we've
- 4:00:39entered three numbers and now they've
- 4:00:40been stored in the list so let's create
- 4:00:43a follow loop after and we can go from
- 4:00:44zero all the way to list numbers do
- 4:00:47length but of course when we're using
- 4:00:49list we don't have a length anymore when
- 4:00:52we're using arrays we have length
- 4:00:55because arrays have a fixed length
- 4:00:57whereas lists have a count of the items
- 4:00:59inside it so instead of length we simply
- 4:01:02use do count and then inside here we
- 4:01:05could just do what we normally do and
- 4:01:07say list numbers square braet I and
- 4:01:10let's run that and we should see the
- 4:01:12numbers printed back out in the exam
- 4:01:13exact same order 1 2 and 3 and the same
- 4:01:16principle goes if you user for each Loop
- 4:01:19we can say V item inside list numbers
- 4:01:21and that's all you need to do and you
- 4:01:23can just print item to the screen now we
- 4:01:25should see the same output twice let's
- 4:01:27have a look at other values that we have
- 4:01:29inside here so we type in list numbers
- 4:01:31Dot and let's have a look at the
- 4:01:33function inside so we can add which you
- 4:01:35just used above we can convert it to an
- 4:01:37array we can remove a specific position
- 4:01:40and we can clear it completely they have
- 4:01:42other ones in here like B researchers we
- 4:01:45can check if it contains a certain
- 4:01:46element this is going to be very handy
- 4:01:48when we do our
- 4:01:49remove we can convert them all to a
- 4:01:51different type we have a copy to we
- 4:01:54check if a value exists
- 4:01:56inside we have a find finding all of
- 4:01:59them the difference between these two is
- 4:02:01the find will return back the first one
- 4:02:04whereas find all will retrieve all the
- 4:02:05elements that match it finding the index
- 4:02:08of a specific match finding the last
- 4:02:11index iterating through them all you can
- 4:02:13insert a specific position whereas the
- 4:02:16add will just insert at the end of the
- 4:02:17list adds an object to the end of the
- 4:02:20list inserts an element to the list at a
- 4:02:23specified index and then you have remove
- 4:02:26which gives you the first occurrence so
- 4:02:28you can type the number straight into
- 4:02:30there so you can ask the user what
- 4:02:32number do you want to delete and if they
- 4:02:34type zero it will delete the first zero
- 4:02:36in the list and you can remove all so if
- 4:02:39you want to remove every single zero
- 4:02:41inside the list you can do that as well
- 4:02:43or you can move at a specific position
- 4:02:45this is the most used function because
- 4:02:47what you can
- 4:02:48do and what you can do is use the find
- 4:02:51index search for an element that matches
- 4:02:53a condition and then returns to zerob
- 4:02:55based index of the first occurrent so if
- 4:02:58you want to delete the number zero from
- 4:03:00the list you could use find index and
- 4:03:02that returns you back with an INT and
- 4:03:04then you can use the remove at and then
- 4:03:06pass that index straight into there you
- 4:03:09can also sort it so it goes up in number
- 4:03:11order we did this with the array. sort
- 4:03:13function as well and if you wanted to
- 4:03:15you can also reverse the entire list
- 4:03:17this can be very helpful as we did with
- 4:03:19the array functions if you just wanted
- 4:03:21to reverse them all in case someone had
- 4:03:23something like a filter when you're
- 4:03:25doing from Price high to low and low to
- 4:03:27high the difference between them two
- 4:03:29lists is simply the list flipped so
- 4:03:31let's run a remove just inside here and
- 4:03:33let's see what happens so we can say
- 4:03:35remove that and let's say position
- 4:03:37zero so we type in one two and three
- 4:03:41you'll see that one two three gets
- 4:03:42printed out and then I'll hit this
- 4:03:44remove at and it will delete the first
- 4:03:47index and in this case the first index
- 4:03:49is going to be our number one so now
- 4:03:51this has became our first index and this
- 4:03:53is our second index and it's got rid of
- 4:03:55the number
- 4:03:57one so let's move on to different data
- 4:04:00structure called dictionaries
- 4:04:02dictionaries are generic collections
- 4:04:03that store key value pairs in no
- 4:04:05particular order they take in a t key
- 4:04:08and a t value and these just mean the
- 4:04:10key and the value can be any data type
- 4:04:13that you want dictionaries come under
- 4:04:14the system. collections. generic
- 4:04:16namespace and if you don't include this
- 4:04:18line at the top then you'll have to type
- 4:04:20in system. collections do generic space
- 4:04:23dictionary every single time you want to
- 4:04:25specify dictionary putting the name
- 4:04:27space at the top will include it within
- 4:04:29the project and you can use dictionaries
- 4:04:30freely so one thing to know is the keys
- 4:04:33must be unique and they cannot either be
- 4:04:34null values can be null or duplicate cuz
- 4:04:37they're the values depending on the key
- 4:04:39as long as the keys aren't the same then
- 4:04:41the values can be similar so let's have
- 4:04:43a look and see what we can do we can
- 4:04:45type in dictionary and you can see it
- 4:04:47has the arrows either side and you can
- 4:04:50see here it says T key and T value and
- 4:04:52what T key and T value is is it gives us
- 4:04:55the flexibility to add any data types
- 4:04:56that we want we can add in two strings
- 4:04:58in here for example or we can do
- 4:05:00anything that we please so in this
- 4:05:02example I just want to start a list of
- 4:05:04names and I want to give them a unique
- 4:05:07identifier and this unique identifier
- 4:05:09will just give them a number that their
- 4:05:11profile is up against it's the same way
- 4:05:13when whenever you make an account with
- 4:05:15any sort of website when your data gets
- 4:05:17stored in the database you have a unique
- 4:05:19ID for your account and this is the same
- 4:05:21thing that I'm trying to replicate here
- 4:05:23we have a unique ID and with that unique
- 4:05:26ID comes a name so we can say equals new
- 4:05:29dictionary just like we do with the list
- 4:05:32we can end the brackets and end the
- 4:05:34semicolon there and now we've created
- 4:05:36our dictionary and the exact same
- 4:05:38principle we can use with the list we
- 4:05:40can simply type in the name of the
- 4:05:42variable of the dictionary and type in
- 4:05:45add and then we have access to int key
- 4:05:47and string value and this parameter is
- 4:05:50going to change if this now becomes a
- 4:05:52string as I'll show you
- 4:05:54now and if we reopen the bracket you'll
- 4:05:57see this has turned into a string key so
- 4:06:00that will change depending on the data
- 4:06:01types that you specified so let's say we
- 4:06:04want to add one and we want to add
- 4:06:06ourselves then we can duplicate this
- 4:06:08down and add in two and three and we can
- 4:06:12say test and test
- 4:06:14and now if you run the code you'll see
- 4:06:15that we have no errors and nothing's
- 4:06:17going to show up I'm just demonstrate
- 4:06:18that it compiles okay and the same thing
- 4:06:21with the list we can also simplifies the
- 4:06:23initialization process and it looks
- 4:06:26something like this so because this is
- 4:06:29in key value pair we can no longer just
- 4:06:31type it in one line they need to be in a
- 4:06:33little block so when we wrap these
- 4:06:35around the curly brace we can have the
- 4:06:37key on the left and the value on the
- 4:06:39right and one of these is called a key
- 4:06:41value pair key P value pair and we will
- 4:06:45use these in the for each Loop in a
- 4:06:47second when we print out all the values
- 4:06:48to the console so let me just
- 4:06:50demonstrate something about what I said
- 4:06:52at the start I said that all keys must
- 4:06:54be unique and there's a reason why I
- 4:06:56said that if we duplicate the first
- 4:06:58entry therefore giv us the same key as
- 4:07:01one and we try run it and let's see what
- 4:07:03happens we get something called a
- 4:07:05runtime error this runtime error happens
- 4:07:08when we run the program and it's got
- 4:07:10nothing to do with compile time or our
- 4:07:12logic an item with a same key has
- 4:07:14already been added so we need to watch
- 4:07:16out if we do this because our program is
- 4:07:18going to compile but it's not going to
- 4:07:19work at runtime and let me demonstrate
- 4:07:21another fact if we make this number four
- 4:07:24and our values are exactly the same once
- 4:07:26we run this it will still work fine
- 4:07:29values can be the same but the keys
- 4:07:31cannot be the same the keys have to be
- 4:07:33unique so just like with our lists and
- 4:07:35our arrays we can use a for each or a
- 4:07:38for Loop to access this directly so we
- 4:07:40can say 4 in I equal 0 and we're want to
- 4:07:43go names do count and the same reason
- 4:07:45it's a do count for a dictionary as it
- 4:07:47is for a list but it's not for an array
- 4:07:50dictionaries don't have fixed length and
- 4:07:52neither do lists they are very Dynamic
- 4:07:54and basically are a list list can only
- 4:07:57store one different data type whereas
- 4:07:59dictionaries give you a key and a value
- 4:08:01so as we said above we have a key value
- 4:08:03pair so let's try and print this and see
- 4:08:05what happens have a console right line
- 4:08:07and we can say names square bracket I
- 4:08:10and that will give us back the position
- 4:08:12the issue with this is it will just give
- 4:08:13us back the string and let's just see
- 4:08:15what happens if we print
- 4:08:17this then we get an exception because no
- 4:08:20key was given and the problem with this
- 4:08:22situation is we can't use this like a
- 4:08:24list getso sets the value associated
- 4:08:27with a specified key what this means is
- 4:08:29when we give our square bracket I inside
- 4:08:31here this is actually not talking about
- 4:08:33the index this is actually trying to do
- 4:08:35a search on these keys so if you
- 4:08:38actually Chang this to one then we
- 4:08:40should get abber return back this is not
- 4:08:43actually doing an index lookup it's
- 4:08:45doing a key lookup and the reason for
- 4:08:47that is what we're going to have to do
- 4:08:49instead is say names. element art and
- 4:08:52then what we can do inside there is
- 4:08:53place our index so this element art will
- 4:08:56return us back with a key value pair as
- 4:08:58I said above here so how this variable
- 4:09:01works is we can do key value pair and
- 4:09:04then we need to specify this variable
- 4:09:06the same way we've got it up here so we
- 4:09:08have an INT inside a string and then we
- 4:09:11can say a pair and make that equal to
- 4:09:13the function called element at as that
- 4:09:16returns us back with a key value pair in
- 4:09:18string and of course if you wanted to
- 4:09:20make this a lot smaller you could just
- 4:09:22simply use V but let's keep it as key
- 4:09:24value pair just so you can understand
- 4:09:25what's going on and the reason why we
- 4:09:27need to get the key value pair is we
- 4:09:29want to retrieve the integer and we want
- 4:09:31to retrieve the string and now we can
- 4:09:33use pair. key and pair. value and
- 4:09:37because these are both integer and this
- 4:09:39one is a string it matches our pair
- 4:09:41exactly so we can take these values and
- 4:09:44go to our console right line and we can
- 4:09:46say something like this use string
- 4:09:47interpolation and say pair. key and then
- 4:09:50maybe add a hyphen and then say pair.
- 4:09:53value and then just remove these two
- 4:09:54lines and let's try and run it now there
- 4:09:58we go so we get all the values and it
- 4:10:00doesn't matter what this key is it can
- 4:10:01be something random it doesn't really
- 4:10:03make a difference like I said this
- 4:10:05doesn't have to be in a specific order
- 4:10:08so let's keep it as 1 2 3 there we go so
- 4:10:11you can see using a for each Loop to
- 4:10:12print this is is a little bit more
- 4:10:14complicated let's have a look and see
- 4:10:16what it takes when you use a for each
- 4:10:18Loop for each VAR item inside our
- 4:10:23names and as you can see here our item
- 4:10:25is automatically a key value pair of int
- 4:10:28and string and again what you can do is
- 4:10:31type this straight into here just so you
- 4:10:33can get used to the notation and there
- 4:10:35you go it's still a key value pair and
- 4:10:38then now what we can do is we can take
- 4:10:40this exact line and paste into here and
- 4:10:42instead of pair we have item and this
- 4:10:45should return us back the exact same
- 4:10:47values and if we just separate with a
- 4:10:48console right line there we go okay so
- 4:10:51now that we've understood this let's
- 4:10:53take a look at an example let's comment
- 4:10:55this out and let's create another
- 4:10:57dictionary so we can say dictionary and
- 4:10:59then we can up a string of
- 4:11:01string and we can call this teachers
- 4:11:04equals new dictionary string of string
- 4:11:06again and we can add our curly brace
- 4:11:08just so we can add our value straight to
- 4:11:10here so as you said before we need to
- 4:11:11open curly brace and close Cur
- 4:11:14and then we can say the math teacher is
- 4:11:15called abber and then we can add another
- 4:11:18one and we say the science teacher is
- 4:11:21called test there we go so we've
- 4:11:22initialized our teachers and we said
- 4:11:24that the value that we want to give it
- 4:11:26is a unique identifier which is going to
- 4:11:28be our string assigned to the subject
- 4:11:31and this is going to be the teacher that
- 4:11:32is assigned to the subject so what we
- 4:11:34can do inside here is we can have a
- 4:11:36console right line and we can do
- 4:11:38something like this so remember when I
- 4:11:40was saying in the for Loop we can't
- 4:11:41actually use the square bracket notation
- 4:11:43like this because this is referring to a
- 4:11:46key rather than a position so we had to
- 4:11:48use element at just to find out the key
- 4:11:50value pair and then to print out to the
- 4:11:52console so because the square brackets
- 4:11:54is an actual surge let's try and make
- 4:11:56use of that now so we know that there's
- 4:11:58a math class available so we can type
- 4:12:00math in here and what that should return
- 4:12:02us back is abber there we go so what
- 4:12:04that's doing is it's searching through
- 4:12:05our dictionary finding this element
- 4:12:08inside here and then return us back with
- 4:12:10the value for that key gets all sets the
- 4:12:13value associated with the specified key
- 4:12:15the problem with using this approach is
- 4:12:17it says here if the specified key is not
- 4:12:19found a operation throws a key not found
- 4:12:22exception let's just say for instance I
- 4:12:23mistyped the math and now we're just
- 4:12:25going to get a runtime error and it's
- 4:12:27going to be a horrible exception and our
- 4:12:29code is obviously going to break this is
- 4:12:31similar to what we do when we have in.
- 4:12:33trass we don't just trass it immediately
- 4:12:36and convert it without actually checking
- 4:12:38if it's a valid value so instead of
- 4:12:40doing this line what we can do is we can
- 4:12:43check if that key exists first before we
- 4:12:46then use it so we can say if
- 4:12:49teachers do try and get the value which
- 4:12:52is similar to what we have with triy
- 4:12:54pass and then the first parameter is the
- 4:12:57key that we want which is going to be
- 4:12:58our math and then out here is going to
- 4:13:01be the variable that we want to Output
- 4:13:03out so we can say out
- 4:13:06teacher and then open the curly braces
- 4:13:09and then inside here we can print out
- 4:13:11the teacher so we try to get the value
- 4:13:13for Math and if we found that key inside
- 4:13:15these then we return back the value
- 4:13:17inside our teacher the same way works
- 4:13:20for our trip pass you take in a string
- 4:13:22in here and the output is going to be an
- 4:13:24integer if it's successfully passed it
- 4:13:27and if it hasn't we can say cons output
- 4:13:30math teacher not found so let's try and
- 4:13:34run that now you see we get return back
- 4:13:36Abba and if you actually drop the lower
- 4:13:38case then it says the math teacher is
- 4:13:40not
- 4:13:41found alternatively what we can do
- 4:13:43inside here let's assume that this is
- 4:13:45correct we can take this value and place
- 4:13:48it straight inside the square brackets
- 4:13:50and do this again this is kind of
- 4:13:52redundant because we've told it to get
- 4:13:54the value for key place inside teacher
- 4:13:56and then we're just going back to the
- 4:13:57dictionary to get the value twice so
- 4:14:00make sure if you have an output variable
- 4:14:02that you actually use it inside here you
- 4:14:03can see that you're not using it when it
- 4:14:05goes a slightly dark color like this so
- 4:14:07just like in the last examples when we
- 4:14:09were placing values inside our list we
- 4:14:11can also do that in dictionaries
- 4:14:13we can say teachers square bracket math
- 4:14:16and then we can make that equal to
- 4:14:18something so if the math teacher were to
- 4:14:20change and now the teachers called Joe
- 4:14:22then we can run this line and print out
- 4:14:23to the screen let's just take that for
- 4:14:25each Loop and tailor it to the new
- 4:14:27dictionary and see what happens when we
- 4:14:29run it so let's just change this into a
- 4:14:31VAR to make it simple and then we can
- 4:14:33have teachers inside here there we go so
- 4:14:35now let's just run this and you see now
- 4:14:37it says abber is the first value that
- 4:14:39abber is coming from console right line
- 4:14:42here that says teacher this is one line
- 4:14:44before we change it and we change the
- 4:14:46value of the math teacher to Joe on the
- 4:14:49next line and there we go it's committed
- 4:14:51the change and just like in the last
- 4:14:53video as well when we try to delete
- 4:14:54something from the list let's see what
- 4:14:56we can do to delete it from here so we
- 4:14:58can have teachers do remove and you can
- 4:15:00see that it says key in there and again
- 4:15:03if we add the key in like this then it's
- 4:15:04going to work just fine there you go we
- 4:15:07just have science we don't have math
- 4:15:08anymore but if I drop the lowercase then
- 4:15:11it's going to cause us back with an
- 4:15:12error but in this case instead of it
- 4:15:14throwing an exception like it did when
- 4:15:16we were trying to do up here and up here
- 4:15:18as well the problem with using the
- 4:15:21remove is you just simply won't remove
- 4:15:23it but what it will do is it'll give you
- 4:15:25back a booing if you remember from the
- 4:15:27last video we actually had a look at a
- 4:15:29section where in the list it gave you
- 4:15:31back a booing whether the remove
- 4:15:32function worked or not alternatively
- 4:15:34what you could do instead of using the
- 4:15:36remove function is to check if the item
- 4:15:39exists in the first place we can say
- 4:15:41contains key and we can Place math
- 4:15:43inside here and if it does contain it
- 4:15:45then we can run the same line of code
- 4:15:47and we can say remove and then use math
- 4:15:50just to make sure that this has been
- 4:15:52actually removed and then in the else we
- 4:15:54can say math not found retrieving values
- 4:15:58back should be retrieve using try get
- 4:16:00just so you don't cause an exception if
- 4:16:02this is not found we can simply do an
- 4:16:04update by placing the value of the key
- 4:16:06inside the square brackets and then
- 4:16:08simply just assigning it to something
- 4:16:10else this assignment will change the
- 4:16:12value of the key which is inside the
- 4:16:14teacher dictionary and if you wanted to
- 4:16:17remove something it's best to use the
- 4:16:18contains key just to check if it
- 4:16:20contains it first and then you can do
- 4:16:22the remove and then if it doesn't
- 4:16:23contain it you can tell the user that
- 4:16:25math has not been found and then finally
- 4:16:28we can just print out like
- 4:16:30this so in this exercise I want you to
- 4:16:33create two lists with the integer data
- 4:16:35type one for even and one for odd and
- 4:16:37then we're going to Loop through from
- 4:16:38zero all the way till 20 and if the
- 4:16:41number is even add it to the even and if
- 4:16:43the number is odd add it to the odd list
- 4:16:45and at the end print out all the values
- 4:16:47for even and for odd I know you can just
- 4:16:49print these out inside the for Loop we
- 4:16:51don't have to have dedicated lists but
- 4:16:53this is just for list practice and I
- 4:16:55want to talk about different ways that
- 4:16:56we can do this solution so let's start
- 4:16:58we can have a list of int and we can say
- 4:17:01odd new list int and we don't know the
- 4:17:05size so we're just going to leave the
- 4:17:06gaps empty and if we duplicate this down
- 4:17:09we can simply just change this to even
- 4:17:11now the reason why we're doing lists
- 4:17:13we're going from 0 to 20 of course this
- 4:17:1520 value could be anything it could be a
- 4:17:17million it could be 100 but we don't
- 4:17:19know and the point is lists have counts
- 4:17:23whereas arrays have lengths if we set
- 4:17:25the array length to something like a
- 4:17:26thousand we know probably be able to
- 4:17:28contain all the numbers inside there but
- 4:17:30that's going to be a lot of positions
- 4:17:31that we don't exactly need because
- 4:17:33arrays are not really dynamic because
- 4:17:35they're static then it's best to use
- 4:17:37list in this case because we don't know
- 4:17:39how many numbers are going to be odd or
- 4:17:41are going to be even between between
- 4:17:43this range if we knew the exact number
- 4:17:45of positions that we needed in a given
- 4:17:47solution then array would be better
- 4:17:49since you don't want it to be dynamic if
- 4:17:51it doesn't need to be so let's start can
- 4:17:54have a for Loop that goes from 0 to 20
- 4:17:56if you want to include the 20 as well we
- 4:17:58can just place an equals the best way to
- 4:18:00determine if a number is odd or even is
- 4:18:02to use the modulus operator so we can
- 4:18:04say I percentage 2 equals equal Z and
- 4:18:07what this will do is it'll tell us if I
- 4:18:09is divisible by two if the value is
- 4:18:11equal to zero if you have two modulus 2
- 4:18:15then the remainder is going to be zero
- 4:18:17if you have three modulus 2 it's going
- 4:18:19to be 1 * by two which gets you to two
- 4:18:22and then to go from two to three you
- 4:18:24actually have one so if you insert an
- 4:18:27odd number and do a modulus two you're
- 4:18:29going to get a one returned if you do an
- 4:18:32odd number and you do modulus 2 then
- 4:18:34you're going to get one returned if you
- 4:18:35do an even number modulus 2 then you're
- 4:18:38going to get zero returned so what we
- 4:18:40can do in here is we can say even add
- 4:18:43and then we can simply add I and then in
- 4:18:45the else we can also simply just do odd
- 4:18:48do add and then have I because the
- 4:18:50values of the remainder can only be zero
- 4:18:52or one we don't need to have an lse if
- 4:18:54in here doing this is a little bit
- 4:18:56redundant although it's still
- 4:18:58technically correct the values can only
- 4:19:00ever be zero or one so if you run this
- 4:19:03calculation and the value is zero if you
- 4:19:05run this calculation and the value is
- 4:19:06zero the only other value can possibly
- 4:19:09be is one so therefore we don't need to
- 4:19:11additional condition so therefore we
- 4:19:13don't need this additional condition and
- 4:19:14we can simply get rid of it if I modulus
- 4:19:172 is equal to Z then we add the number
- 4:19:19to even and else we add it to odd
- 4:19:22normally when you have situations like
- 4:19:24this we can use the conditional operator
- 4:19:26where we have the question mark followed
- 4:19:28by the colon but in this case we're
- 4:19:30actually doing two different assignments
- 4:19:31we're either assigning into the even or
- 4:19:34assigning into the odd so there's no
- 4:19:36room for the conditional operator so at
- 4:19:38this point we should have all the even
- 4:19:39and odd values from zero all the way to
- 4:19:4120 inside our two lists so then we can
- 4:19:44now just print both the lists out so we
- 4:19:47can say printing even numbers and then
- 4:19:50we can have a for each Loop that goes v
- 4:19:52item inside out even and then we can do
- 4:19:55a console Right add some instring
- 4:19:57interpolation and then we can say item
- 4:19:59followed by space then we can print
- 4:20:02environment. new line just to give it
- 4:20:03some space and we can say printing odd
- 4:20:06numbers here we go and then we can just
- 4:20:08take this exact for Loop and copy it
- 4:20:10down instead of set in even we want to
- 4:20:13do in odd and this is exactly the same
- 4:20:15since the variable name is exactly the
- 4:20:17same let's try run the
- 4:20:19code there we go it's got zero all the
- 4:20:22way till 20 and you can see it goes up
- 4:20:24in twos hence all even numbers and we
- 4:20:26also get these going up in twos except
- 4:20:28it starts from one and this covers all
- 4:20:30of the odd numbers and if we change this
- 4:20:32number up to something like 50 then the
- 4:20:34list will just grow and they Cod should
- 4:20:36still work okay you can see that all of
- 4:20:38these numbers are still consistently
- 4:20:39even so now that our task is actually
- 4:20:41complete let's just have a little recap
- 4:20:44so the reason why we're using lists and
- 4:20:45we're not using arrays is for this exact
- 4:20:47nature if we have 0 to 10 then we can
- 4:20:51almost work out how many even numbers
- 4:20:53and how many odd numbers we can have we
- 4:20:55can manually count we have zero and then
- 4:20:58two 4 6 8 and 10 and then for odd we
- 4:21:01have 1 3 5 7 and 9 so we know how many
- 4:21:05numbers exist within here but what if we
- 4:21:08just change that number to 50 then now
- 4:21:10we have a lot more odd and a lot more
- 4:21:12even it's impractical to type these
- 4:21:14numbers in and then to Simply count them
- 4:21:16in your head and then make an array
- 4:21:18length based on that position that's a
- 4:21:19bad solution because not all cases
- 4:21:22you're going to have the flexibility of
- 4:21:24Simply typing in the numbers like this
- 4:21:25and Counting them yourself these numbers
- 4:21:27could be something that typed in by a
- 4:21:29user or something loaded for something
- 4:21:31external like a text file you need to
- 4:21:33understand that these values can be
- 4:21:35generated from anywhere and if you just
- 4:21:37type them in then it provides an
- 4:21:38unrealistic expectation of how to write
- 4:21:40some programs so what you have to bear
- 4:21:42in mind in situations like this is you
- 4:21:45simply don't know how many even and how
- 4:21:47many odd numbers will be appearing
- 4:21:49during this range so you want to use a
- 4:21:51list so it's as dynamic as possible we
- 4:21:53have two lists in this case one to start
- 4:21:56all the odd numbers and one to start all
- 4:21:57the even numbers we don't initialize
- 4:21:59them with any sort of capacity we just
- 4:22:01keep it empty for the exact reason we
- 4:22:03don't use an array we Loop through from
- 4:22:05zero to anything we don't even have to
- 4:22:07loop from zero we can also just start
- 4:22:09this at 100 and go all the way till
- 4:22:11th000 it really doesn't matter what our
- 4:22:14number range is the only thing that
- 4:22:15matters is that we having a list that is
- 4:22:18going to be dynamic and then we can
- 4:22:19calculate whether the number is odd or
- 4:22:22even and add it to the right list how
- 4:22:24this works is we use I which is the
- 4:22:26number we're currently up to and we
- 4:22:28calculate the modulus of it to check the
- 4:22:30remainder if the remainder is zero then
- 4:22:32it must be even as it's divisible by two
- 4:22:35and if it's anything else than zero
- 4:22:36which you can only ever be one in this
- 4:22:38case when using modulus 2 then we add it
- 4:22:40to the odd list after this volume is run
- 4:22:43now our odd and even lists have all the
- 4:22:45values that they should have and we
- 4:22:47simply just write these two blocks are
- 4:22:49exactly the same just print the even
- 4:22:51numbers and print them out print the odd
- 4:22:53numbers and simply print them out to the
- 4:22:55screen and that's it and if you run the
- 4:22:57code it might get a little bit crazy in
- 4:22:58here but as you can see these will
- 4:23:00actually work if you pick any random
- 4:23:02numbers you can see that they're all
- 4:23:04divisible by two and the same thing goes
- 4:23:06for odd you can see that they're not
- 4:23:08divisible at two cuz they're all odd
- 4:23:12so in this exercise we're going to cover
- 4:23:14a little task I like this call this task
- 4:23:16an array of multiples so we want to
- 4:23:18Define initialize two integers and num
- 4:23:20into length so in the case of this
- 4:23:22example we have a number seven and the
- 4:23:24length of five so what this will do is
- 4:23:26it will essentially give you seven times
- 4:23:28table but only the first five numbers 74
- 4:23:3221 28 and
- 4:23:3435 we take the first number which is num
- 4:23:37which will give us the multiples and we
- 4:23:39take the second number which is the
- 4:23:40length and that gives us the size of the
- 4:23:42array so we create the integer array
- 4:23:44with the size of length Loop through the
- 4:23:46array and insert the loop count times
- 4:23:48byome CU this is 1 * 7 2 * 7 and so on
- 4:23:52so forth until we reach the length and
- 4:23:54we place into the array and then finally
- 4:23:56we paint the array just to give us these
- 4:23:58values out to the console all right so
- 4:23:59let's get started we can have an in norm
- 4:24:02and make it equal to 7 and we have it in
- 4:24:04length and make that one equal to five
- 4:24:07now that we defined initialize the two
- 4:24:08integers let's create the int array with
- 4:24:11the size length in Array result equals
- 4:24:14new int and we want size length after
- 4:24:16that we want to create our for Loop now
- 4:24:18this is the tricky part if we try go
- 4:24:20from zero all the way to result. length
- 4:24:23then let's see what happens so we want
- 4:24:25to start result I you want to make that
- 4:24:27equal to number which is our array of
- 4:24:29multiples Times by the value that we're
- 4:24:32currently up to now let's print our
- 4:24:33array using for each Loop to see the
- 4:24:35value so we can say V item inside our
- 4:24:38result then we can have a console right
- 4:24:40that just prints one of them at a time
- 4:24:42so we can say item followed by spits now
- 4:24:45let's see what happens when we run
- 4:24:47it so we get 0 14 21 28 as you can see
- 4:24:51these are the first four numbers so
- 4:24:53we're close but we're nearly there
- 4:24:56because our array starts at zero and it
- 4:24:58goes to the length which in this case is
- 4:25:00going to be 0 to less than five which
- 4:25:03will cover the positions of 0 to
- 4:25:064 with these positions it's going to do
- 4:25:090 * 7 1 * 7 and then get to 4 * 7 and
- 4:25:13not actually get to the fifth because
- 4:25:14we're starting from zero so
- 4:25:17alternatively what we can do or what you
- 4:25:19think you can do is changes to a one and
- 4:25:22changes to equal to so now what it's
- 4:25:24doing is it's going from one all the way
- 4:25:26to five which is still covering the same
- 4:25:28amount of positions it's just one index
- 4:25:30forward so now if we try to run the code
- 4:25:33then we going a runtime exception and
- 4:25:35the reason for that is it's trying to
- 4:25:37access position number five if we hover
- 4:25:39over I we have five and if you hover
- 4:25:42over the result we actually have some of
- 4:25:45the right values except it's trying to
- 4:25:47get to the fifth position and it's not
- 4:25:49working the reason why the zeroth index
- 4:25:51is actually zero is simply because
- 4:25:53that's the same value as the default
- 4:25:55value because our for Loop started at in
- 4:25:57I it's actually only starting from here
- 4:25:59and going down so this calculation is
- 4:26:01actually correct but it needs to be back
- 4:26:04one we need to start the multiplication
- 4:26:06on IAL 0 but our for Loop needs to start
- 4:26:09on IAL 1 so we can get all the way
- 4:26:12through
- 4:26:13so we have two ways of doing this we
- 4:26:15could say I minus 1 so even though it
- 4:26:18goes from 1 to 5 it's still assigning
- 4:26:20into the array using 0 to four so if we
- 4:26:23run that we can see that that will give
- 4:26:25us the desired result alternatively what
- 4:26:27we can actually do instead of doing IUS
- 4:26:30one inside here we can define a variable
- 4:26:32called counter and make it equal to zero
- 4:26:35we can change this to counter and we
- 4:26:37don't need to change the multiplication
- 4:26:39because that's already going from one to
- 4:26:40the length which is 1 to 5 in this case
- 4:26:43after the for Loop is run we need to
- 4:26:45remember to increment counter so there
- 4:26:47we go we have the desired effect another
- 4:26:49hack if you're interested if you have
- 4:26:52two variables that you want to increment
- 4:26:53inside the for Loop you can see that we
- 4:26:55already have I ++ in here which will
- 4:26:57increment it by one and we want to
- 4:26:59increment counter by one as well every
- 4:27:01single time it loops around what you can
- 4:27:04actually do is remove this and you know
- 4:27:06how these are broken up by semicolons
- 4:27:09that indicates that the section has
- 4:27:10ended the first sections for declaration
- 4:27:13the second is for the range and the
- 4:27:14third is so you can increment the
- 4:27:16variable or decrement it when you trying
- 4:27:17to increment two variables inside a for
- 4:27:19Loop instead of placing a semicolon here
- 4:27:22and type in the next one you can
- 4:27:24actually place a comma and do counter
- 4:27:26Plus+ what that will tell the fall Loop
- 4:27:28is every single time you go through you
- 4:27:30want to do a series of these actions and
- 4:27:33because we have i++ and counter Plus+
- 4:27:36then that's going to work exactly the
- 4:27:37same way it just prevents us having
- 4:27:40counter Plus+ down here and we can move
- 4:27:42move it inside the for Loop just so it
- 4:27:43doesn't for us and it looks a lot
- 4:27:45cleaner cuz you can clearly see that
- 4:27:47every single time we loop around this
- 4:27:49for loop we're going to increment these
- 4:27:50two variables there we go so let's try
- 4:27:54to put some different values in here
- 4:27:55maybe make this 10 and we can have 10 on
- 4:27:57both to give us the first 10 numbers
- 4:28:00there we go from 10 all the way to 100
- 4:28:02let's have a little recap so we're
- 4:28:04essentially just making an array that
- 4:28:06will give us back all the multiples so
- 4:28:08it's like a mini times table but the
- 4:28:10fact that we're doing it dynamically
- 4:28:11using our array is quite important what
- 4:28:14you can do if you wanted to is to extend
- 4:28:16this program you can ask the user to
- 4:28:18type these into the console and then
- 4:28:20convert them and use them right now
- 4:28:22we're just statically typing them in and
- 4:28:23that's not really an issue since the
- 4:28:25main focus was doing this for Loop if
- 4:28:27you wanted to you could ask the user and
- 4:28:29you can type these values in so you
- 4:28:31don't have to change them every single
- 4:28:32time you compile so now we've got the
- 4:28:34number and the length let's make our
- 4:28:35interray call result and initialize it
- 4:28:37to the length we set up this counter so
- 4:28:39we simply didn't have to do I minus one
- 4:28:42here so it looks a little bit more
- 4:28:43intuitive we start the follow from one
- 4:28:46and go to the length so we can access 1
- 4:28:48to five and the reason why we want to do
- 4:28:50one to five so when we do our
- 4:28:52multiplication it doesn't start at zero
- 4:28:55these two are going through the exact
- 4:28:56same amount of indexes 1 to 5 is five
- 4:28:59numbers and 0 to four is also five
- 4:29:01numbers so when we do a multiplication
- 4:29:03we want to start from one since the Tim
- 4:29:05table normally start at one and we go to
- 4:29:07the length the reason why we're using
- 4:29:09the counter is so we don't have to do
- 4:29:10IUS one in here and it breaks up the
- 4:29:12logic a little bit and it looks nicer
- 4:29:14without our counter we start our counter
- 4:29:16at zero and instead of incrementing down
- 4:29:18here we use this fancy comma and we can
- 4:29:21say we want to increment I and counter
- 4:29:23every time the for Loop goes around so
- 4:29:25we don't need to include the counter
- 4:29:26increment inside the for Loop and this
- 4:29:28one line is pretty simple we say result
- 4:29:31the index of counter which will start at
- 4:29:33zero which is the start of the array
- 4:29:35since it's zero base and we say that's
- 4:29:37equal to the number and the number is
- 4:29:38going to be a fixed number since this is
- 4:29:40the multiple and we say number times by
- 4:29:43I and in this case it goes from one all
- 4:29:45the way to the length so we get
- 4:29:47essentially the times table and at the
- 4:29:49end we just use a simple for each Loop
- 4:29:51that will go through all the items
- 4:29:52inside the result array and just print
- 4:29:54them out to the screen with a comma
- 4:29:56after each other using console. right
- 4:29:58just so they're all on one line this is
- 4:30:00the start of the function section
- 4:30:02functions can also be called method they
- 4:30:04are used interchangeably in the
- 4:30:06introduction we discussed dry which
- 4:30:08means don't repeat yourself and the
- 4:30:10whole point of dry is so you don't have
- 4:30:12multiple sections of code that do
- 4:30:14basically the same thing you want to
- 4:30:16wrap it around in a function functions
- 4:30:18are used to improve the readability of
- 4:30:20your code and the reusability of the
- 4:30:22code as well having multiple section
- 4:30:25code that are basically exactly the same
- 4:30:27can be more prone to errors if you wrap
- 4:30:29them around and place in One Singular
- 4:30:31function you can make sure that the
- 4:30:33error prone and then wherever you use it
- 4:30:35it will be error prone as well if you
- 4:30:37have to copy and paste and tweak some
- 4:30:39code then you're actually exaggerating
- 4:30:41the amount of error that could occur and
- 4:30:43plus it makes your code look a lot
- 4:30:45cleaner and the structure a lot better
- 4:30:47since you can name the functions so
- 4:30:48let's take a little example we have a
- 4:30:51static void main straight into here this
- 4:30:54is a function or a method and these are
- 4:30:56the parameters for the function The NET
- 4:30:59Framework expects every C program to
- 4:31:02have a main function with a capital m
- 4:31:04and it needs to be a static and avoid
- 4:31:07the parameters don't exactly matter we
- 4:31:08can also remove them and it won't be a
- 4:31:10problem the only thing that matters is
- 4:31:12this needs to be called main if we drop
- 4:31:15the lower case on it and try and run the
- 4:31:17code then we're going to get a build
- 4:31:19error program does not contain a static
- 4:31:22main method suitable for an entry point
- 4:31:25so if you change this back to M and run
- 4:31:26it again and then it works fine so
- 4:31:29that's one thing to note about The NET
- 4:31:31Framework is you need to include the
- 4:31:33main function otherwise the program
- 4:31:35won't run and it won't even compile so
- 4:31:37let's take a look at how we can use this
- 4:31:40we can have something like a console
- 4:31:41right line and we can say welcome to my
- 4:31:44program this is a line that we're
- 4:31:46probably only going to use once but
- 4:31:48let's say this is welcome in someone's
- 4:31:49name welcome abber let's say you had
- 4:31:52this in your software in many different
- 4:31:53places you don't want to be recalling
- 4:31:55this or retyping out this message in
- 4:31:58case you type it wrong the fifth time
- 4:31:59and you get a error of course this won't
- 4:32:01be a syntax or a runtime error it'll
- 4:32:04just simply be a logic error because
- 4:32:06it's a typo so in order to make sure
- 4:32:07this typo never occurs we can actually
- 4:32:09make another function so outside of
- 4:32:12these cury braces and just still within
- 4:32:14the class we want to create a new
- 4:32:15function so we want to follow this exact
- 4:32:18same approach we can say static then
- 4:32:21void and we can call it welcome message
- 4:32:24and we don't need to give it any
- 4:32:25parameters cuz we're not really going to
- 4:32:26give it any values just for now now that
- 4:32:28we've made that we have no syntax errors
- 4:32:30and we can run it like this except of
- 4:32:32course there's no body so let's place
- 4:32:34our console right line within here now
- 4:32:36if you run the code nothing is going to
- 4:32:38happen and the reason for that is the
- 4:32:40code will still execute in the main we
- 4:32:43haven't called this function inside the
- 4:32:45main we've defined it and called it
- 4:32:47welcome message and we've gave it the
- 4:32:49contents but we haven't actually made
- 4:32:50use of it and you can see that when it's
- 4:32:53highlighted in a darker color that means
- 4:32:55it's not being used as it says here
- 4:32:57private member welcome message is unused
- 4:33:00so now we can simply type in here
- 4:33:02welcome message followed by an Open
- 4:33:04Bracket and then a Clos bracket to
- 4:33:06denote the parameters that we're going
- 4:33:07to give it and of course in this case
- 4:33:09there is no parameters and then we close
- 4:33:11the brackets and add a semicolon now if
- 4:33:13you run the code you can see it says
- 4:33:15welcome AB so just to go over some of
- 4:33:17these words that we're using static
- 4:33:19means that we can access it anywhere and
- 4:33:22everywhere what I mean by that is we
- 4:33:24don't have the keyword new just before
- 4:33:27this we don't need to make a new
- 4:33:28instance of the program class in order
- 4:33:31to access this so what that means is
- 4:33:33when we make future programs we're going
- 4:33:35to have some functions inside our class
- 4:33:37inside the class you can actually denote
- 4:33:40if a function is going to be static or
- 4:33:42it's not going to be static you don't
- 4:33:44have to specify is it static but this
- 4:33:46won't let you call it from here because
- 4:33:48it's unaware where this function's
- 4:33:49coming from an object reference is
- 4:33:51required for the non-static field method
- 4:33:53or property because this is static you
- 4:33:56need to reference something else from a
- 4:33:58static nature all static means is you
- 4:34:00don't have to create a new instance of
- 4:34:02this thing in order to use it we'll see
- 4:34:04different examples in the future where
- 4:34:06you have to create an instance in order
- 4:34:08to use something so let me just show you
- 4:34:10quickly if you have a test and I say
- 4:34:13static void
- 4:34:15something and we can just print out
- 4:34:17nothing to the screen and let's try and
- 4:34:19use this so because this class is
- 4:34:21outside of the program we have to use
- 4:34:24the notation test Dot and then it should
- 4:34:28be available within here and the reason
- 4:34:30why it's not is because we need to
- 4:34:31insert this public word just before it
- 4:34:33and public means everything has access
- 4:34:35to it and static means you don't need to
- 4:34:37create an instance now if I type in the
- 4:34:39dot again you can see we can now see
- 4:34:41something
- 4:34:43and the reason why we can see something
- 4:34:45is because this is a static now if I
- 4:34:47remove the static word then you see that
- 4:34:49we'll get a compile error because it
- 4:34:50doesn't know where this is now if you
- 4:34:53don't use static you need to do this you
- 4:34:55need to say test test equals new test so
- 4:34:58you make an instance of the class and
- 4:35:01then inside test then we get the
- 4:35:03something so that's the difference
- 4:35:05between static and not static
- 4:35:08essentially it's accessible from
- 4:35:09everywhere but if you don't want to be
- 4:35:11accessible from everywhere then you can
- 4:35:13just simply remove the static word and
- 4:35:15the user will have to create a new
- 4:35:17instance before they can use it so this
- 4:35:19is what it's like without
- 4:35:21static and this is what it's like with
- 4:35:23static don't worry if you don't
- 4:35:25understand this too much this was just a
- 4:35:27quick overiew but I have a dedicated
- 4:35:29video later on in the course so don't
- 4:35:31worry too much about it so now that
- 4:35:32we've made a function and we've called
- 4:35:34it successfully in the domain and it's
- 4:35:36prined out to the
- 4:35:39console so when we're declaring function
- 4:35:42we want to specify what return type
- 4:35:43we're going to have we have a static
- 4:35:45void Main in here if you hover over main
- 4:35:48it says void which means at the end of
- 4:35:50this function it doesn't return anything
- 4:35:51hence its void if you have a look at
- 4:35:54something like an in. trass when we open
- 4:35:56the brackets you can see that it takes
- 4:35:58in a string and it takes in an integer
- 4:36:00out parameter it also returns us back to
- 4:36:03the Boolean so we can have something
- 4:36:04like 1 2 3 out int result and then it
- 4:36:08doesn't actually complain about anything
- 4:36:10but what we can do is also so just store
- 4:36:12this in to a bu success and then print
- 4:36:14it out to the
- 4:36:17screen there we go so it says it was
- 4:36:19successfully true if we remove this or
- 4:36:22whether we keep it in or not trip pass
- 4:36:24function doesn't actually complain but
- 4:36:26the main point here is it defines back a
- 4:36:28bull that means at the end of this
- 4:36:30function it will always return you back
- 4:36:32with a bull and it's up to you if you
- 4:36:34want to make use of it and you can
- 4:36:35alternatively you can place this
- 4:36:37straight inside the console right line
- 4:36:39and get the value printed straight to
- 4:36:41the screen or you could wrap this around
- 4:36:43inside an if statement like we've done
- 4:36:44in the past Etc and make use of it that
- 4:36:47way and have the result being printed
- 4:36:48out inside here the point is you have
- 4:36:50functions that return something and
- 4:36:52functions that don't if you remember
- 4:36:54that we had the array. copy for example
- 4:36:57you'll see that they have void here
- 4:36:59because they just apply a function to
- 4:37:01something but they don't actually give
- 4:37:02you something back this is the
- 4:37:04difference of having something like this
- 4:37:06a string test that says hello and then
- 4:37:09you can do test. replace and then you
- 4:37:12can return you back with the string and
- 4:37:14it says here it returns a new string
- 4:37:16whereas when we had a string builder for
- 4:37:19example and we did string builder.
- 4:37:22replace you can see that although it
- 4:37:24does say it replaces a new string
- 4:37:26Builder you can type anything inside
- 4:37:28here and it will still affect the
- 4:37:30current string Builder it won't have to
- 4:37:31do SB equals s SP replace as you would
- 4:37:34need to do here because this returns you
- 4:37:36back with a new string and if you wanted
- 4:37:38to replace test you would literally have
- 4:37:40to say test equals test. replace with
- 4:37:44something like a string Builder all the
- 4:37:46array functions you don't actually have
- 4:37:48to do any of that so you can say array.
- 4:37:50empty and it will just return the empty
- 4:37:52array and the ones that we used before
- 4:37:54things like equals and that will give
- 4:37:56you back a Bool because that's
- 4:37:58determined whether it's equal or not but
- 4:38:00if you're doing functions like sort for
- 4:38:02example and doing things like reverse it
- 4:38:04just simply applies the data but it
- 4:38:07doesn't return you back with anything so
- 4:38:08you can literally just call it inside
- 4:38:10here put your array in the brackets and
- 4:38:12it'll use it happily now depending on
- 4:38:15your user case you need to define
- 4:38:17whether or not you need to use if return
- 4:38:19type or not for now let's just cover
- 4:38:22void which means it won't do anything so
- 4:38:24let's just Define a function static void
- 4:38:27create and print an array open the
- 4:38:29brackets close the brackets and we can
- 4:38:31have it int numbers make it equal to new
- 4:38:34int with length three and let's just
- 4:38:36have 0 1 and two inside here as the
- 4:38:39initializer let's just do a for each VAR
- 4:38:42item inside numbers and then just do a
- 4:38:45console right string interpolation and
- 4:38:47just have item followed by space and now
- 4:38:50that it's dark gray because we haven't
- 4:38:51called the function so let's make use of
- 4:38:53it at the top of our code and let's run
- 4:38:55it now you can see we get 012 printed to
- 4:38:58the screen and if we make this into a
- 4:39:00five and simply just add more values in
- 4:39:03here and then we run the code you'll see
- 4:39:05that now it all ofd but because this
- 4:39:07doesn't return us back or anything and
- 4:39:09we don't actually use the integer array
- 4:39:11I outside of these curly braces it's not
- 4:39:13actually available up here numbers won't
- 4:39:15be accessible inside here because it's
- 4:39:17simply not within the scope numbers is
- 4:39:20only available within this curly brace
- 4:39:23and this curly brace which means when
- 4:39:25this function finishes numbers has now
- 4:39:27been deleted from memory so you can't
- 4:39:29access it after this line void doesn't
- 4:39:31do anything it simply just does the
- 4:39:34contents of the function and as soon as
- 4:39:36it reaches the bottom that's all that
- 4:39:37happens you can have another function
- 4:39:39defined that can return a value
- 4:39:41like I said with in. trass for example
- 4:39:44at the end of the function you'll see a
- 4:39:46word that says something like this it
- 4:39:48will have return and then return back
- 4:39:50something followed by a semicolon and
- 4:39:52that will dictate that as soon as that
- 4:39:54function is done it's going to give you
- 4:39:56something back now we're going to cover
- 4:39:58that in the next video but for now we're
- 4:40:00only going to cover void functions so we
- 4:40:02can have something more obvious like a
- 4:40:04static void welcome
- 4:40:06message and simply just do a console
- 4:40:08right line welcome to my program and we
- 4:40:11don't need to return anything here
- 4:40:12because all this function will do is
- 4:40:14simply print out a welcome to the screen
- 4:40:17and we can use that just before the
- 4:40:18print array so we can say welcome
- 4:40:20message Open Bracket close bracket and
- 4:40:23then a semicolon and hit F5 to run and
- 4:40:26there we go as you can see our main is
- 4:40:28really tidy now it gives us function
- 4:40:30names that tells us exactly what's going
- 4:40:32on we're creating and printing an array
- 4:40:34and then welcoming the user to the
- 4:40:36console and then having a console read
- 4:40:38line at the end there isn't an awful lot
- 4:40:40of void functions all you need to know
- 4:40:42is that when you create a function you
- 4:40:43need to give it a return type it returns
- 4:40:45back a certain data type or it simply
- 4:40:47just doesn't and depending on what you
- 4:40:49want to do and what you want to achieve
- 4:40:51with that function really depends on if
- 4:40:53you want to return a data type or not in
- 4:40:55the case of these since they're just
- 4:40:57doing something and then moving on you
- 4:40:59don't actually need to return anything
- 4:41:00by it with in. tripod you want to make
- 4:41:02the function is executed successfully so
- 4:41:05when you use int. tripas you can see
- 4:41:07that returns back a bull and the bull
- 4:41:09determines whether the conversion has
- 4:41:11been successful and based on that return
- 4:41:13value you can determine the next steps
- 4:41:15of your program if all your function
- 4:41:16needs to do is provide some sort of
- 4:41:18functionality and then quit then that's
- 4:41:21fine and avoid is fine but if you need
- 4:41:23to provide some functionality and then
- 4:41:25also return a value then you'll need to
- 4:41:28specify the data type in here which
- 4:41:29we'll cover in the next
- 4:41:32video so far we've covered aoid
- 4:41:35functions but we haven't covered return
- 4:41:37types vo functions are where the
- 4:41:38function just does something but doesn't
- 4:41:40actually return anything hence the name
- 4:41:42void let's start with an example if you
- 4:41:45have a static void print
- 4:41:48introduction might want to just return
- 4:41:50back your name after you've printed this
- 4:41:52line it's going to print it to the
- 4:41:53console and that's all it's going to
- 4:41:55do there we go let's say there's a
- 4:41:58situation where you actually want to use
- 4:41:59this inside your code you can actually
- 4:42:01set the console's title by typing in
- 4:42:03console. title and type in your name
- 4:42:05inside the quotation marks or you can
- 4:42:06type in anything inside here if you run
- 4:42:08that you'll see Abra is in the title of
- 4:42:10the console right now and that looks
- 4:42:11pretty cool what you got to realize is
- 4:42:14now we have two references to the same
- 4:42:15value now at this point we can actually
- 4:42:18do something better we can type in
- 4:42:20static string name equals abber and then
- 4:42:23instead of using abber in here we can
- 4:42:25say name and instead of using abber in
- 4:42:27here we can also say a name and they're
- 4:42:29both using this variable and if you run
- 4:42:30the code now you see that the same
- 4:42:32output will happen you have abber twice
- 4:42:34both in the title and both in the
- 4:42:35console output now the problem with this
- 4:42:37approach is you're actually making use
- 4:42:38of a global variable which we do not
- 4:42:40like code in situation like this you
- 4:42:42shouldn't need to make a global variable
- 4:42:44because there are ways around it so
- 4:42:46instead of having this which has a print
- 4:42:48introduction and we've got the name used
- 4:42:49in there but the name is coming from
- 4:42:51here maybe we can use this somewhere
- 4:42:53else so let's say we make another
- 4:42:54function called Static and instead of
- 4:42:57void we want to actually return back a
- 4:42:58string so we can say string and then we
- 4:43:01can say my
- 4:43:03name or something like return name or
- 4:43:06anything you want just make sure you
- 4:43:08give it a meaningful name so it dictates
- 4:43:10what the function is trying to do so
- 4:43:11this ISS to return the name and the
- 4:43:13reason why there's a red underline is it
- 4:43:15says not all called path return a value
- 4:43:18since we need to use our keyword return
- 4:43:21now that we had some empty codes on the
- 4:43:22return it's going to execute fine and
- 4:43:24the compile error is gone so obviously
- 4:43:26we don't want to just return nothing we
- 4:43:27actually want to take this ab and return
- 4:43:29in here so let's get rid of our Global
- 4:43:32variable and let's see what we're going
- 4:43:33to break so we're going to break these
- 4:43:35two references to the name variable and
- 4:43:37simply inside here we can actually just
- 4:43:39call return name name and then here as
- 4:43:42well we can call return name and what
- 4:43:44this will do is inside here it will
- 4:43:47execute this function and then return
- 4:43:49the value in here if this is too many
- 4:43:52steps for you you can have a string name
- 4:43:54equals return name and then simply use
- 4:43:56the name inside the brackets these two
- 4:43:59lines are doing exactly the same thing
- 4:44:01except this is using a variable cuz
- 4:44:03we're storing it first and then we're
- 4:44:04using it whereas if you place it
- 4:44:06straight inside the brackets that's just
- 4:44:08going to store it and use it immediately
- 4:44:09and there's no additional variable but
- 4:44:11don't worry too much if you're going to
- 4:44:13do this notation because an extra
- 4:44:15variable in this case doesn't really
- 4:44:16matter because as soon as this function
- 4:44:18is executed the variable used to run
- 4:44:20this function gets deleted since it's
- 4:44:22inside the scope of these two curly
- 4:44:24braces so now if you run the code you'll
- 4:44:27see that again we have abber and abber
- 4:44:28but now what we've done is actually
- 4:44:30control everything throughout here so if
- 4:44:32your name were to change or maybe this
- 4:44:34changes in a different circumstance
- 4:44:36based on the user that's logged in then
- 4:44:38you can simply have the same thing now
- 4:44:40printed in two different locations in
- 4:44:42the title and in the console output and
- 4:44:45it's actually coming from one function
- 4:44:47so we can have a return name and we can
- 4:44:49make a static in now and can say return
- 4:44:52H and then we can have simply return 23
- 4:44:55and that's our name and our age being
- 4:44:57returned I Chang this back to abber and
- 4:44:59then instead of the print introduction
- 4:45:01maybe we actually want to print a little
- 4:45:02bit more than the name and maybe in the
- 4:45:05title what we can do is have string
- 4:45:07interpolation wrap the return name and
- 4:45:10maybe put a high in there and then we
- 4:45:12can say return age so if we on the code
- 4:45:14now you can see we have abber hyphen 23
- 4:45:18so maybe that's the title of our console
- 4:45:20but then in the introduction we can use
- 4:45:22a bit more than that so we can have int
- 4:45:24age equals return age and then in the
- 4:45:26console output we can have something
- 4:45:28like this hello my name is name and my
- 4:45:32age is age and then after we've compiled
- 4:45:35that together we can place the output
- 4:45:37inside here there we go so we're using
- 4:45:40the name and the age inside the console
- 4:45:42title and then we have this function
- 4:45:44that runs to get us back the name and
- 4:45:46the age and then we're using it using
- 4:45:48string interpolation of course these
- 4:45:50four lines can be all squished into one
- 4:45:53you can simply take this dollar sign let
- 4:45:55me duplicate this
- 4:45:57line you can take this and paste it
- 4:46:00straight into here and where you have
- 4:46:01name we can have our return name and
- 4:46:04where you have age you can have return
- 4:46:05age and that will actually rule out all
- 4:46:08of these lines and you can wrap it all
- 4:46:10into one
- 4:46:11there you
- 4:46:13go and if age were to change like I said
- 4:46:15before you can simply type this number
- 4:46:17in change it and you can see in both
- 4:46:19cases in the title and in the console
- 4:46:21output it has officially changed
- 4:46:23functions can be very helpful to reduce
- 4:46:25repetition in code let's show you an
- 4:46:27example if you had three sections where
- 4:46:30you said enter a
- 4:46:32number and you had int num one equals
- 4:46:35convert to n32 and then we add a
- 4:46:38console. readline
- 4:46:41and then you had this section and you
- 4:46:43wanted to do num
- 4:46:44two and you had this section again and
- 4:46:46you actually wanted to do num three now
- 4:46:49at this point even though these three
- 4:46:50are actually identical you want to be
- 4:46:52using array in this case so you could
- 4:46:54have int numbers equals new in square
- 4:46:58braet 3 and then here we could say
- 4:47:00numers
- 4:47:01zero numbers
- 4:47:03one numbers two and then we don't need
- 4:47:06the ins cuz it's already been defined
- 4:47:08and then we have them like this but
- 4:47:10because again all these three sections
- 4:47:11are exactly identical we can actually
- 4:47:13run a for Loop that goes from numbers do
- 4:47:16length and then we can place one of
- 4:47:18these straight inside here and then
- 4:47:20change this to I and now we've achieved
- 4:47:22exactly the same thing but we're not
- 4:47:24actually done yet so if you run the code
- 4:47:26and we do one 2 3 now we've stored all
- 4:47:28three numbers so we have enter a number
- 4:47:31and then we have the console read line
- 4:47:32and maybe we can actually make these two
- 4:47:34lines a little bit shorter so let's make
- 4:47:36a function static and because we
- 4:47:38essentially want to return an integer we
- 4:47:40can say static in read number from
- 4:47:45console and then if we take these two
- 4:47:47lines place them inside here and because
- 4:47:49we need to return back an integer this
- 4:47:51is the value that we're going to be
- 4:47:52returning so instead of assigning this
- 4:47:54position to something we'll actually
- 4:47:56just immediately return at this point so
- 4:47:58instead of doing this line now and
- 4:48:01instead of doing all of this we can
- 4:48:03simply make this into one line of code
- 4:48:05read number from console so every time
- 4:48:07this will execute will jump into here
- 4:48:09print out the value enter a number the
- 4:48:12user will typee it in it will read it in
- 4:48:14convert it to an integer 32 and this
- 4:48:16will return back the integer that the
- 4:48:18user's typed in and then that will
- 4:48:19return it back to the console and
- 4:48:20essentially replace this so if the user
- 4:48:23is typed in five then it essentially
- 4:48:24replace this with five store it loop
- 4:48:26around and have to placed in 10 the next
- 4:48:28time and then you replace this with 10
- 4:48:30essentially and then that gets stored
- 4:48:32inside and so on and so forth until all
- 4:48:34the positions have been complete let's
- 4:48:36test that 1 2 3 and it still works okay
- 4:48:40of course I'm not actually printing the
- 4:48:41values out to the screen so if you
- 4:48:42wanted to see that quickly for each can
- 4:48:46have V item inside our numbers and then
- 4:48:48do a console right with some string
- 4:48:50interpolation item followed by a space
- 4:48:53and let's run that one two and three and
- 4:48:55there we go we get one two and three of
- 4:48:57course you can make these functions and
- 4:48:58they can return any bits of data that
- 4:49:00you want you can even have something
- 4:49:02like an INT array and we can say create
- 4:49:05random array and then we can create an
- 4:49:06array call numbers make that equal to
- 4:49:09new int square bracket 3 and we can
- 4:49:12actually give it the values of 0 1 and
- 4:49:14two and then just at the end here we can
- 4:49:16actually just return numbers and then
- 4:49:18just in here we can have an INT array or
- 4:49:20we can call that new numbers since we
- 4:49:22have numbers above just above here and
- 4:49:24then we can make that equal to create
- 4:49:26random array and because this returns us
- 4:49:28back with an integer array there's no
- 4:49:31compile array inside here and we can
- 4:49:33have a four each follows the exact same
- 4:49:35approach as the one above new numbers
- 4:49:37console right line into a console right
- 4:49:40and then have item followed by a space
- 4:49:43now if we print these both out you can
- 4:49:45see that the zero one and two actually
- 4:49:47comes from the new one it's just we have
- 4:49:49two console rights right next to each
- 4:49:51other so there's no new line separating
- 4:49:52them so if we just add a console right
- 4:49:54line here 1 2 and three and there you go
- 4:49:581 2 and three comes from the three
- 4:49:59inputs we just typed in and 0 1 and two
- 4:50:02actually come from the function that
- 4:50:03we've just created of course if you
- 4:50:06don't like this notation then what you
- 4:50:08can do is you can make it a lot easier
- 4:50:09you don't even need to make a new
- 4:50:11variable you can actually just return
- 4:50:13the integer just like this so if we
- 4:50:15place
- 4:50:16this straight inside here actually just
- 4:50:20make it on one line just so it looks a
- 4:50:21bit neater then there you go we have our
- 4:50:23entire function reduced in one line the
- 4:50:26goal of functions is to make them do as
- 4:50:28little as possible if your function has
- 4:50:30two different things it's doing or more
- 4:50:32then you should break that out into
- 4:50:34another function for example if you had
- 4:50:35a function that did two distinct
- 4:50:37operations one was a conversion and one
- 4:50:39was a calculation then you want to split
- 4:50:41out into two the first function should
- 4:50:43be the conversion and the second
- 4:50:44function should be the calculation and
- 4:50:46the reason why they're separate is
- 4:50:48because if you want to reuse them as a
- 4:50:50pair then you won't be able to reuse
- 4:50:52each of the pairs if you wanted to have
- 4:50:54a calculation on its own then you can't
- 4:50:56do that if both functions do the same
- 4:50:58thing for example you could have static
- 4:51:02and it just could be something let's
- 4:51:03just call it void and it could just be
- 4:51:05test for example and if you had the
- 4:51:07conversion here and then if you had the
- 4:51:09calculation here then you need to split
- 4:51:11this up into two different functions you
- 4:51:13want have static valed
- 4:51:16conversion and static vied calculation
- 4:51:19if you wanted to use conversion or
- 4:51:21calculation separately you can't because
- 4:51:23now it's in this pair together so if you
- 4:51:24wanted to use them separately you simply
- 4:51:26can't it's best to keep functions on the
- 4:51:28RW anywhere just so they have a single
- 4:51:30purpose so you can give them a name cuz
- 4:51:32as you can see here we can't give this
- 4:51:34an appropriate name because it's doing
- 4:51:35more than one task this is doing the
- 4:51:37conversion this is doing the calculation
- 4:51:39and this is doing mult multiple things
- 4:51:41this could be doing something if you
- 4:51:43have to think too hard about the
- 4:51:44function title then you might be doing
- 4:51:46too much within each of functions these
- 4:51:48are very easy to be able to decide what
- 4:51:50they do cuz this one converts and this
- 4:51:52one's going to calculate reading number
- 4:51:54from console if this was reading the
- 4:51:56number from the console and doing
- 4:51:58something else then it's going to be a
- 4:52:00bit of a problem because now you've got
- 4:52:01a bigger name you don't want to be doing
- 4:52:03reading number from console and and
- 4:52:05something else cuz then your function
- 4:52:07name gets also too big and you got
- 4:52:09multiple dependencies so the best thing
- 4:52:11to do is to call them accordingly and
- 4:52:13keep them as short as possible so let's
- 4:52:15say we wanted to make a function that
- 4:52:16added two values we can have a static
- 4:52:18int call it add and then we can return 5
- 4:52:21+ 5 now this function is a little bit
- 4:52:23redundant although it will actually work
- 4:52:25so if we go back to our main and we do a
- 4:52:27console right line and actually print
- 4:52:29the value of
- 4:52:31ADD and we go through our function then
- 4:52:33we see we get the value of 10 which is
- 4:52:35indeed 5 +
- 4:52:375 but since it's 5 + 5 won't change
- 4:52:40range it's always going to be 5 + 5 this
- 4:52:43is where parameters will come in which
- 4:52:44we'll cover in the next videos
- 4:52:45parameters are a way of being able to
- 4:52:47give the function value so these two can
- 4:52:49be actually changed there's no point in
- 4:52:51having an add function if the only
- 4:52:53ability you can do is add 5 + 5 cuz you
- 4:52:56may as well just return 10 at this point
- 4:52:58so I covered a lot in this video and
- 4:52:59there's lots of different functions that
- 4:53:01do different things of course there is
- 4:53:03quite a lot of lines we're up to 87 this
- 4:53:05is probably the biggest project that
- 4:53:06we've wrote so far and even though this
- 4:53:08is not really a project it's just a buns
- 4:53:10functions that do little things that are
- 4:53:12made for demonstrations so let's have a
- 4:53:14recap so first here we're setting the
- 4:53:16title as a result of return name and
- 4:53:19return age we can actually take this and
- 4:53:22develop this further if you want to we
- 4:53:24could actually say return name age pair
- 4:53:27and if you wanted to collapse that
- 4:53:28inside one function then that could be
- 4:53:30really easy you could say static string
- 4:53:34return name age pair and then you can
- 4:53:37have return and literally the exact same
- 4:53:39line above you can have return name
- 4:53:41followed by a hyphen and then you can
- 4:53:43have a return age and then end the line
- 4:53:46like that so that just concatenates the
- 4:53:48two values that are returned from these
- 4:53:49two functions but the point is you keep
- 4:53:51them all separate if you were to type in
- 4:53:53very statically inside here AB 25 then
- 4:53:57obviously like I've talked about above
- 4:53:58then you're going to have duplication of
- 4:54:00data and of course we're going to use
- 4:54:01this name inside something like here and
- 4:54:04as you can see this name age pair is
- 4:54:06actually not applicable when it comes
- 4:54:07back to this format so it's always best
- 4:54:09to keep the these two separately so if
- 4:54:12you bring it back and we can actually
- 4:54:13keep it like this now instead of having
- 4:54:15this code we can actually say return
- 4:54:18name age pair that will give us the same
- 4:54:20value at the top of the code it will
- 4:54:22have ABA hyphen
- 4:54:2325 so that's that bit in our print
- 4:54:26introduction If We Hold control and we
- 4:54:28actually press on this function it will
- 4:54:30actually jump us right down and then if
- 4:54:32we press the back arrow top left we can
- 4:54:34go back to it there we go and then click
- 4:54:36forward so in this function we can break
- 4:54:38down all of the return values into to
- 4:54:40string variables we have a string name
- 4:54:42equals return name and inage equals
- 4:54:44return age then we have a string input
- 4:54:46that concatenates them both together
- 4:54:48hello my name is name and my age is age
- 4:54:51and then we print out to the screen or
- 4:54:52if you wanted to we could wrap all four
- 4:54:54of these lines straight up into one we
- 4:54:57don't need to assign these into
- 4:54:58variables although it does help the
- 4:55:00readability if you keep them separate
- 4:55:02but I prefer to keep it like this since
- 4:55:03I'm used to this notation we can place
- 4:55:06return name straight into there which
- 4:55:07will get rid of our name variable and
- 4:55:09the same thing with age which will get
- 4:55:11rid of these two variables and of course
- 4:55:13we're actually storing the string value
- 4:55:15before we printing it so we could just
- 4:55:17take all of this and place it straight
- 4:55:19instead of here scrolling back to the
- 4:55:23top this little section was
- 4:55:25demonstrating how we can use integer
- 4:55:27arrays to read in all the values from
- 4:55:29the console but wrap it around in a
- 4:55:31function so before in here you would
- 4:55:33have something like a console right line
- 4:55:35You' print out the value to the screen
- 4:55:36that would say something like a ter
- 4:55:38number and then you have on the next
- 4:55:39line a convert to in32 and then after
- 4:55:43you converted it in 32 you'll actually
- 4:55:45start straight to numbers but because
- 4:55:47this is going to be the same thing for
- 4:55:49inserting all the numbers you can
- 4:55:50actually wrap this around in a function
- 4:55:52so if we hold control again and go to
- 4:55:54this you'll see that them two lines are
- 4:55:55very simply placed in here console right
- 4:55:58Line enter a new number and then convert
- 4:56:00in 32 based on the console read line and
- 4:56:03then we simply just returning that back
- 4:56:05ready to be stored in the index of the
- 4:56:07numbers array and then straight after
- 4:56:09that we're just printing out all of
- 4:56:10values to make sure it's worked and that
- 4:56:12all succeeded fine here I was just
- 4:56:14demonstrating that you can have
- 4:56:15different return values and it can
- 4:56:16literally be anything so as you can see
- 4:56:18here if you hover over create random
- 4:56:20array it's inside the program class it's
- 4:56:23called create random array and it
- 4:56:24returns back an integer array so that's
- 4:56:26what we're signed into and if we hold
- 4:56:28control and click on this again you can
- 4:56:30see that all this simply does is returns
- 4:56:33us back a three length array with 0 1
- 4:56:36and two as the array positions and then
- 4:56:38if we scroll back up when we execute
- 4:56:40this for each Loop then we're simply
- 4:56:42going to get 0 1 and two just like that
- 4:56:45since it's a console right and it'll be
- 4:56:47on the same line with a space after each
- 4:56:51other this demonstration was slightly
- 4:56:54theoretical I was saying in one given
- 4:56:56situation a function should only do one
- 4:56:58task if your function converts and does
- 4:57:00a calculation at the same time then
- 4:57:02these are actually two different things
- 4:57:04you should split them so you use the
- 4:57:06conversion in this step and you use the
- 4:57:08calculation in this step if you want to
- 4:57:10convert something or calculate something
- 4:57:12in the future then you can only do it
- 4:57:14now because we've separated them whereas
- 4:57:16before if you have them both inside here
- 4:57:19then you simply can't do one operation
- 4:57:21because you've wrapped it around in one
- 4:57:23section the same is true when I was
- 4:57:25discussing this earlier on if we kept
- 4:57:27this name pair as the only way to access
- 4:57:29the age in the name then it's going to
- 4:57:31mess up when we use it in here and then
- 4:57:32we're going to have a duplication of
- 4:57:34data but because we separated the name
- 4:57:36and we separated the age we can use it
- 4:57:38in this function and we can in this
- 4:57:40function and it doesn't interrupt
- 4:57:42anything this simply Returns the name
- 4:57:44this simply Returns the age and these
- 4:57:46two functions make use of them if we had
- 4:57:49something else underneath there were
- 4:57:50another function that depended on the
- 4:57:52name of the edge then again we can
- 4:57:54simply use them if it was restricted to
- 4:57:56this function then it's going to be a
- 4:57:58problem using it in this function or any
- 4:58:00future
- 4:58:01ones and finally just to finish off add
- 4:58:04function and all it simply does is
- 4:58:06return 5 + 5 and I was saying as of
- 4:58:08right now even though return return
- 4:58:10returns back a value it's kind of
- 4:58:11pointless CU you may as well just return
- 4:58:13back 10 since it's doing the same thing
- 4:58:15the reason I included this function is
- 4:58:17that we can set it up and start planting
- 4:58:19the seeds to understand why functions
- 4:58:21exist and how to make use of parameters
- 4:58:23what a parameter is is it lets you put
- 4:58:25things Within These brackets so you can
- 4:58:27give this function some data so if you
- 4:58:29have an in. trass for example these
- 4:58:32things that we place inside the brackets
- 4:58:33are actually the parameters so we have a
- 4:58:35string s and out in something and that's
- 4:58:38how the parameters work on our trip pass
- 4:58:40function and just to show you this is
- 4:58:43how it's actually wrote It's a Bool and
- 4:58:45then it's called
- 4:58:53Triassic and I know it's give us an
- 4:58:55error because we don't have the control
- 4:58:57path if we just use result and assign it
- 4:58:59to zero and then return back true you
- 4:59:01can see that the compil error is now
- 4:59:03gone and as you can see here it says
- 4:59:04bull trip pass string s out in result
- 4:59:07and if we actually try and use the trip
- 4:59:09pass in here and press the bracket you
- 4:59:11can see that these parameters are
- 4:59:13exactly identical to parameters that
- 4:59:15were inside of the tripar coming from
- 4:59:17the integer Library so in the future
- 4:59:20we're going to study how we can use
- 4:59:21parameters to pass data inside our
- 4:59:23function but just for now know that this
- 4:59:26function is essentially pointless
- 4:59:28because it's only going to return back
- 4:59:2910 and this function will gain the
- 4:59:31functionality it needs when we start
- 4:59:33adding parameters this should
- 4:59:35essentially be add five + 5 except this
- 4:59:38is kind of a pointless function because
- 4:59:40we already know the value is going to be
- 4:59:4110 but again this is just helping you
- 4:59:44understand why parameters are very
- 4:59:45essential to
- 4:59:48functions we've covered how to create a
- 4:59:50VI function and a function that returns
- 4:59:52a data type but now we want to create a
- 4:59:54function that gives it some sort of data
- 4:59:56you may not realize it but all these
- 4:59:58functions that you normally use like
- 4:59:59console right line you're actually
- 5:00:01inserting data consistently into all of
- 5:00:03these functions if you hover over
- 5:00:05console right line you see his void
- 5:00:07console right line and right now there's
- 5:00:09Open Bracket and there's a Clos bracket
- 5:00:11if we delete this bracket and reopen it
- 5:00:12you see there's 19 different ways that
- 5:00:14you can call this function and as soon
- 5:00:15as you start to type a string you can
- 5:00:17see we have a string format and an
- 5:00:19object if you need to and if you type in
- 5:00:20another string you can see it says
- 5:00:22string value this string value is
- 5:00:24actually a parameter If We Hold control
- 5:00:26and press on right line you can see all
- 5:00:28of the different functions right here
- 5:00:30this is the most basic version that just
- 5:00:32inserts a right line character to the
- 5:00:33console and then these are all the
- 5:00:35different parameters that you can give
- 5:00:36the value of course you can't double
- 5:00:39click and view the source code because
- 5:00:40this is hidden inside Microsoft's
- 5:00:43libraries the values that are placed
- 5:00:45inside these parentheses are actually
- 5:00:46called the parameters this is known as
- 5:00:49functional overloading which we'll cover
- 5:00:51later on in the course basically that
- 5:00:53means you can have the same name
- 5:00:54function but give it different
- 5:00:56parameters and you'll see why this is
- 5:00:57helpful later on down the line so let's
- 5:00:59close this tab in the top right hand
- 5:01:01corner and let's bring back out code so
- 5:01:03now that we've passed hello into this
- 5:01:04function that was actually given this a
- 5:01:06string value parameter and that hello is
- 5:01:09going to get past straight into the
- 5:01:10console right line so then when we run
- 5:01:12the code it's going to print into the
- 5:01:13console so let's create our own function
- 5:01:15with our own parameters so if you take
- 5:01:17the function I had last time it was
- 5:01:19static int add Open Bracket close
- 5:01:21bracket and it simply returned 5 + 5
- 5:01:24which we all know is going to be 10 so
- 5:01:26if we place add inside these brackets
- 5:01:28and we run the code we should get 10
- 5:01:30return back there we go so of course
- 5:01:32this is a little bit pointless since
- 5:01:34it's always going to be adding 5 + 5
- 5:01:36there is no way we can change this
- 5:01:38without the use of parameters so in here
- 5:01:40we can actually just do what we do when
- 5:01:42we normally declare a variable we say
- 5:01:44the data type int and then the variable
- 5:01:46name and then if you want to add
- 5:01:48multiple parameters you can simply add a
- 5:01:49comma so now we can say int a and int B
- 5:01:53and you can see now we've got a syntax
- 5:01:54error because it expects us to have both
- 5:01:56of these values inside if you hover over
- 5:01:58it there is no argument given that
- 5:02:00corresponds to the required formal
- 5:02:01parameter a and then if we insert a by
- 5:02:04having five for example and if we hover
- 5:02:06over it it complains about not having B
- 5:02:09so we can do comma and then do five
- 5:02:11again so just this on its own is not
- 5:02:13actually going to do anything we're
- 5:02:14actually giving the add function the
- 5:02:16values of five and five and place them
- 5:02:18inside a and inside B variables these
- 5:02:21are now known as local variables and
- 5:02:23they only exist in memory between these
- 5:02:25two curly braces as soon as the function
- 5:02:27is done these are deleted from memory
- 5:02:29and you can no longer access in a and in
- 5:02:31B you can't just do something like a
- 5:02:33inside here and think that this can be
- 5:02:35used up here these are in two completely
- 5:02:38different Scopes this is one for
- 5:02:39function and this is another function
- 5:02:41and they're completely separate from
- 5:02:43each other when you type in five and
- 5:02:44five it goes in order so int a is the
- 5:02:47first one so five gets placed into here
- 5:02:49and this five gets placed into B so now
- 5:02:52that we need to make use of them we can
- 5:02:53simply change this to a and change this
- 5:02:56one to B now if you run the code we
- 5:02:58should still get 10 but now our function
- 5:03:00Works a bit better we can give it a set
- 5:03:02of data so if you actually just put
- 5:03:04zeros instead of here then we should get
- 5:03:06a 100 returned perfect so now that we
- 5:03:09figured out how to to do this let's have
- 5:03:10a look at different ways that we can
- 5:03:11make use of this function so let's say
- 5:03:13we had a situation where we did console
- 5:03:15right enter a
- 5:03:18number and we add intn equals convert to
- 5:03:23in32 and then you're converting the
- 5:03:25console do
- 5:03:27readline and let's wrap this inside a
- 5:03:30function so before what I was doing is
- 5:03:32simply just returning back in int it was
- 5:03:34static int read
- 5:03:37int and then we took these two lines and
- 5:03:40place it straight inside here and remove
- 5:03:42the fact that we have an integer and
- 5:03:43simply just change this to a return and
- 5:03:45what this now will say is let's output
- 5:03:47enter a number to the console let the
- 5:03:49user type it in inside the console read
- 5:03:52line and then read it in convert it to
- 5:03:54an integer and then give it back to
- 5:03:55whatever calls it so now we can get rid
- 5:03:57of this and get rid of this convert and
- 5:03:59simply just type in read in inside here
- 5:04:02and after that line let's just print it
- 5:04:04out to make sure it's worked okay enter
- 5:04:06a number 10 press enter and we have 10
- 5:04:09again and that's fine except if we want
- 5:04:11to use this in another case for example
- 5:04:13if you have num one maybe we don't
- 5:04:15actually want to enter a number maybe
- 5:04:17this time it's going to be enter an
- 5:04:19angle or something of this sort so what
- 5:04:21we can do if we undo that we can
- 5:04:23actually introduce a string message and
- 5:04:25what this will do now is actually
- 5:04:27specify the message that we put in here
- 5:04:29maybe there's a situation where we
- 5:04:31always want to enter something so maybe
- 5:04:33we can just replace this and put a
- 5:04:34string inter polation and wrap this
- 5:04:36number around in the curly brace and
- 5:04:38replace it to message so then in here we
- 5:04:40can say enter a and then we can pass the
- 5:04:44string variable which will get placed in
- 5:04:46here so if this is a number then we can
- 5:04:48type a number and maybe this is
- 5:04:49something like an
- 5:04:51angle and then here we can type an angle
- 5:04:54so we have 10 enter a number and then
- 5:04:56there you go it says enter an angle then
- 5:04:58we can have 100 and that's working as
- 5:05:00well alternatively if you want to
- 5:05:02specify the entire message then you can
- 5:05:04remove these and keep it like this and
- 5:05:07then in both of these situations we can
- 5:05:08say enter and number and copy that down
- 5:05:11like this and make sure you put the call
- 5:05:13on at the end followed by the space
- 5:05:15there we go enter a number 10 and an
- 5:05:18angle 20 maybe the situation where this
- 5:05:21text actually changed and you just want
- 5:05:23it to be angle and if you run that again
- 5:05:25then you'll see that these are
- 5:05:26completely different now one says enter
- 5:05:28a number and one says angle but this
- 5:05:30gives us a lot of flexibility in what we
- 5:05:32pass in if we keep it as enter a and
- 5:05:35then let the user decide on what this
- 5:05:36section is then you rule out any
- 5:05:38opportunity to be able to do that option
- 5:05:40if you wanted to to prevent the colon in
- 5:05:42the space always being at the end you
- 5:05:44can actually just add that in manually
- 5:05:46if you bring back the string
- 5:05:47interpolation we can say colon followed
- 5:05:49by space so you don't have to remember
- 5:05:51to always put that in there we go so
- 5:05:53that's doing the exact same thing and
- 5:05:54we're actually just reducing the amount
- 5:05:56of repetition because we have to
- 5:05:58remember to put a call on Follow by
- 5:06:00space inside here we might just forget
- 5:06:02it and then when you leave it like this
- 5:06:04then the cord's going to look ugly
- 5:06:05because it looks like this and it's all
- 5:06:07stuck together so let's make use of
- 5:06:09these two functions and let's see if we
- 5:06:10can write a really basic calculator we
- 5:06:13can say int first num equals read int
- 5:06:16and we can say enter first number and
- 5:06:19then duplicate that down change first to
- 5:06:21second and then change this also to
- 5:06:23Second and then inside a console right
- 5:06:25line we can actually have the add
- 5:06:27function and we can type in first number
- 5:06:29and then we can type in second number
- 5:06:31and if you don't want to call the
- 5:06:32function inside here you can actually
- 5:06:33just make another int and we can say int
- 5:06:35result equals add and just bring this
- 5:06:38line over to here and then in the
- 5:06:39console right line you can just print
- 5:06:41the
- 5:06:42result there we go so we read in both
- 5:06:44the values execute the ad function and
- 5:06:47then return it back to the user using a
- 5:06:49console right line so these are the
- 5:06:51other ones and then here they are so if
- 5:06:53we do 50 and we do 50 again and you see
- 5:06:55that I've messed up because we have
- 5:06:56double colums now so although our
- 5:06:58calculator did indeed work let's just go
- 5:07:00and correct that so we can take these
- 5:07:02off now because we added it inside
- 5:07:06here 50 and 25 we should get set 75
- 5:07:10perfect so now we've made a little basic
- 5:07:12calculator by simply making use these
- 5:07:14functions and if we just expand this
- 5:07:16just to show you what it would have
- 5:07:17looked like so we go in from this and we
- 5:07:21place our message inside to here and
- 5:07:24then we have our convert line just like
- 5:07:27this and then if we duplicate that down
- 5:07:29we can change this to Second n and then
- 5:07:31to second
- 5:07:32number and then as for the result we can
- 5:07:35say first num plus second
- 5:07:38n there we go you can see the code is
- 5:07:40drastically longer and it's a bit
- 5:07:41meaningless to have duplica code this is
- 5:07:44literally the exact same line in both of
- 5:07:47these sections except for the fact that
- 5:07:48this ISS first and this ISS second and
- 5:07:51we're starring in first and we're
- 5:07:52starring in second so let's undo that
- 5:07:54and get back to our more efficient code
- 5:07:56there we go you can see that these three
- 5:07:58lines are considerably shorter and
- 5:08:00easier to read this is telling us we
- 5:08:02want to read an integer here is the
- 5:08:03value and we want to return it inside
- 5:08:05our first Nome and so on and so forth
- 5:08:07let's have a look at another example
- 5:08:09where we can make use of this so we can
- 5:08:10have a string called name and we can
- 5:08:12read this in from the console console.
- 5:08:16readline but we need to Output a message
- 5:08:18so we can have another console right and
- 5:08:21we can say enter your
- 5:08:23name and then we can have this twice and
- 5:08:27we can do enter your age and then have
- 5:08:29an INT
- 5:08:30age but of course we don't want to be
- 5:08:32using this approach and putting a
- 5:08:34console right line inside here with a
- 5:08:36convert in32 because although this is
- 5:08:38actually technically correct now what
- 5:08:40UND doing the point Val function so
- 5:08:42let's change this into our readint and
- 5:08:44then as for our message well we have the
- 5:08:46message right here and again we don't
- 5:08:47need the space callon so let's remove
- 5:08:49that and then as for the name we can
- 5:08:51actually do this differently so this is
- 5:08:53actually going to do a string variable
- 5:08:55so what we want to do instead of having
- 5:08:56read in we can have another function
- 5:08:58called read string another string
- 5:09:01message parameter and then do exactly
- 5:09:03the same thing have a console right and
- 5:09:06we can place string interpolation and
- 5:09:08place a message inside here followed by
- 5:09:10callon space and then we can just simply
- 5:09:12return the console. readline there we go
- 5:09:16and instead of doing all of this we can
- 5:09:18actually just put our read string inside
- 5:09:20here and then place our ENT your name
- 5:09:22inside here just without the callon
- 5:09:25space because we don't need that so now
- 5:09:27we reduce this section to two lines as
- 5:09:29well so now what we can do is have a
- 5:09:31string details and make that equal to a
- 5:09:33function that we're going to use now we
- 5:09:35can say user details if we want to give
- 5:09:37it the name and give it the age and that
- 5:09:39will give us back a string that we can
- 5:09:41print to the console so we can say
- 5:09:43details so let's go ahead and make use
- 5:09:45of this function so we can have a static
- 5:09:47and it's going to return us back a
- 5:09:48string cuz we're placing it inside the
- 5:09:50string and then it's going to be user
- 5:09:52details and then our name is a string
- 5:09:55our age as an INT and then inside here
- 5:09:58we just want to concatenate them two
- 5:09:59values together so we can say hello my
- 5:10:02name is name and my age is age and
- 5:10:07because that's returning us back with a
- 5:10:09string we can make use of it inside
- 5:10:10string details or we can place this
- 5:10:13straight inside the console right line
- 5:10:15let's leave it as a function call assign
- 5:10:17into string details and then print out
- 5:10:19to the screen let's just skip over these
- 5:10:22sections enter your name abber enter
- 5:10:24your age 23 and then there you go hello
- 5:10:26my name is abber and my age is
- 5:10:2923 so this is how we can make use of
- 5:10:31parameters inside our functions so we
- 5:10:33can actually give it some sort of data
- 5:10:35that's meaningful if you didn't use
- 5:10:36parameters then what you're going to
- 5:10:37have to face is for example example what
- 5:10:39we showed you with this integer if you
- 5:10:41don't use parameters then you're going
- 5:10:42to have situations like I showed you
- 5:10:44before with this integer function it
- 5:10:46returns back an integer but it only gave
- 5:10:48us the value of 5 + 5 I mean technically
- 5:10:50you don't need to return anything you
- 5:10:52can actually just keep this aoid and
- 5:10:54just wrap this around in console right
- 5:10:55line the only issue with this approach
- 5:10:57is you don't have any option other than
- 5:10:59to print out of the screen what if you
- 5:11:01didn't want to print out of the screen
- 5:11:03and you simply just wanted to return the
- 5:11:04calculation and then store it inside
- 5:11:06another variable this doesn't give you
- 5:11:08the flexibility of that so we change
- 5:11:10this into an INT and instead of
- 5:11:11returning it back to the console we just
- 5:11:13return back the pure value and then if
- 5:11:15the user decides that they want to print
- 5:11:17this out to the screen that's fine like
- 5:11:19here we've made a decision that we want
- 5:11:21to print it out let's just say we had a
- 5:11:23variable that said in calculation then
- 5:11:25we place that inside but we actually
- 5:11:27never wanted to print it maybe we kept
- 5:11:29it like this and then use Cal for
- 5:11:31something else down below or maybe that
- 5:11:33was something like storing inside a
- 5:11:35database not everything will be printed
- 5:11:37out to the screen for the user to see
- 5:11:39the best thing to do is return the
- 5:11:40values in their purus form because you
- 5:11:42never know what the developer or the
- 5:11:44user is going to make use of that data
- 5:11:45later on down the line keeping that it's
- 5:11:47purus form is the best way possible for
- 5:11:49example in this case we're actually
- 5:11:51reading back the integer and then print
- 5:11:52it out to the screen but in this exact
- 5:11:54case which is the best case scenario
- 5:11:56that I just showed you we're actually
- 5:11:58making use of first n and second n but
- 5:12:00first n and second n are never actually
- 5:12:02print to the screen it's always just
- 5:12:04used for the calculation if we use
- 5:12:06console right line to print out first n
- 5:12:08and second them then we're not going to
- 5:12:10be able to use it inside our calculation
- 5:12:12and therefore our very basic calculator
- 5:12:14is now being rendered pointless so read
- 5:12:16in and add or return back values that we
- 5:12:19can use in their purest form and that is
- 5:12:21the whole goal of
- 5:12:24parameters so now that we've covered
- 5:12:26parameters that are always going to be
- 5:12:27required let's have a look at another
- 5:12:29concept called optional parameters so we
- 5:12:32had a static in add and we add int a and
- 5:12:35int B for example and then we simply
- 5:12:37just returned a plus b like use in the
- 5:12:39previous examples if we have an INT
- 5:12:41result and we want to call that function
- 5:12:44and we give it the value five and five
- 5:12:46and then print out to the screen
- 5:12:47everything runs okay because we've added
- 5:12:49in exactly what we expect let's say we
- 5:12:52don't actually have this additional five
- 5:12:53of course this doesn't make any sense
- 5:12:55cuz we're meant to be adding two numbers
- 5:12:57but just hum me for a minute then what
- 5:12:59this will do is says there's no argument
- 5:13:01given that corresponds to the parameter
- 5:13:03B now of course this is a problem
- 5:13:04because it's a red underline which means
- 5:13:06if we try and run the code we're going
- 5:13:07to get an issue let's say you were
- 5:13:09creating some sort of system that if you
- 5:13:12entered both values it uses both values
- 5:13:15but if you only entered one it
- 5:13:16automatically set the second value to
- 5:13:19something else and we can achieve this
- 5:13:21Behavior using optional parameters so an
- 5:13:24optional parameter doesn't exactly mean
- 5:13:26quite optional it kind of just means
- 5:13:28that it's going to be optional but you
- 5:13:30can also give it a default value this
- 5:13:32will always add two numbers A and B even
- 5:13:35if we make a or b optional it's going to
- 5:13:37still need two numbers to add in order
- 5:13:40for the function to run so in order to
- 5:13:41make a parameter optional let's just say
- 5:13:44we want to make B optional we can type
- 5:13:46in it equals straight after it and
- 5:13:48actually give it a value so as you can
- 5:13:49see our syntax error is now gone and if
- 5:13:52you run the code we should get
- 5:13:5455 and the reason why this works is a is
- 5:13:57now five if you hover over here we can
- 5:13:59see a is five and then the square
- 5:14:02bracket around the int B actually
- 5:14:04denotes that it's optional and it says
- 5:14:06int b equals 50 cuz we have that h here
- 5:14:09so it's actually using this as 50 and if
- 5:14:12we don't specify the second parameter
- 5:14:14then it's practically just doing this
- 5:14:16which is completely fine so the reason
- 5:14:18why I said it's not quite optional is
- 5:14:20because you still need to give it a
- 5:14:21value of course in here you can use the
- 5:14:23default keyword in the case of an
- 5:14:25integer the default value is actually
- 5:14:27zero so if you just run that code now
- 5:14:29it's going to say 5 + 0 which is just
- 5:14:32five so it can be optional but you still
- 5:14:34need to give it some sort of value of
- 5:14:36course if you're doing a calculator and
- 5:14:38you're adding to values if you only
- 5:14:40specify the first one then adding zero
- 5:14:42to it is simply not going to change it
- 5:14:44so zero or default is going to be the
- 5:14:46best user case when you're dealing with
- 5:14:48default values with integers so another
- 5:14:50way you can actually specify this is by
- 5:14:52inserting this thing called an attribute
- 5:14:54if you remove this equal zero and just
- 5:14:57before the end we can type in Open
- 5:14:59Bracket close bracket and type in
- 5:15:00optional with the capital O and then
- 5:15:03you're going to get a red underline if
- 5:15:05you hover over it it says we need to
- 5:15:07include this extra Library which which
- 5:15:09will import it at the top of our code as
- 5:15:11you can see here now that this is
- 5:15:13available it says indicates that a
- 5:15:15parameter is optional and what this will
- 5:15:17do by default is it'll give you the
- 5:15:19basic value for this number so what I
- 5:15:21mean by that is we can type in here just
- 5:15:23to show you this b equals and then B and
- 5:15:26this will just give you the default
- 5:15:28value based on the data type and as we
- 5:15:30just said a minute ago that value is
- 5:15:31actually going to be zero there you go B
- 5:15:34is zero so all it's doing is saying 5 +
- 5:15:370 = 5
- 5:15:40so that's another way of doing it or you
- 5:15:41could just leave it as I said before you
- 5:15:43can say into b equals default or into b
- 5:15:46equals 0 it's completely up to you I
- 5:15:48prefer using the default keyword so you
- 5:15:50don't have to keep remembering which
- 5:15:52different data types have the default
- 5:15:53values in the inter just case it's zero
- 5:15:56but I just like leaving the default and
- 5:15:57we can get rid of this line now let's
- 5:15:59have a look at different situations
- 5:16:01where this will be helpful if you had a
- 5:16:02function called print name and we add a
- 5:16:05string name in here and then we can
- 5:16:07simply just say console right line my
- 5:16:10name is and then name this is quite easy
- 5:16:13to use so we can say print name and then
- 5:16:16Place AB inside the brackets and then
- 5:16:18run the code and we get abber printed to
- 5:16:20the screen and that's fine let's say if
- 5:16:22we had someone else's name in there as
- 5:16:25Joe for example then we get Joe to the
- 5:16:26screen and let's just say as the
- 5:16:28developer you only want to default this
- 5:16:30to abber if you don't actually insert
- 5:16:32anything so now if you don't insert
- 5:16:34anything of course we're going to get a
- 5:16:35compile error because we need a
- 5:16:36parameter but if we assume what we did
- 5:16:39here and just follow the same approach
- 5:16:41if we use default then it's going to
- 5:16:43give us nothing because the default
- 5:16:45value for a string is just empty quotes
- 5:16:47which is just nothing but instead of
- 5:16:49default we can actually specify value so
- 5:16:52we can say if no one types their name
- 5:16:54inside the bracket then use
- 5:16:57abber there we go so you can have this
- 5:17:00optional parameter that defaults to
- 5:17:02something and if someone types in jaw or
- 5:17:04whatever inside the print name function
- 5:17:06then the default optional parameter is
- 5:17:07not going to be used since this will be
- 5:17:09overridden if you have a parameter
- 5:17:11placed inside here so now the ab is
- 5:17:13going to get overridden by the jaw
- 5:17:15because this is actually the parameter
- 5:17:17that's been used in the
- 5:17:19function so you've looked at functions
- 5:17:21with normal parameters optional
- 5:17:23parameters and now let's take a look at
- 5:17:25name parameters so let's say you had a
- 5:17:26situation where you had string name
- 5:17:28abber in age equal 23 and string address
- 5:17:32equals one something rad and then you
- 5:17:35had a function that would simply print
- 5:17:37these details out to the console so
- 5:17:39static void print details there string
- 5:17:42name inage String
- 5:17:45address so you have three console right
- 5:17:47lines that just print out a name value
- 5:17:49pair just like
- 5:17:51this and then when you go to call this
- 5:17:53function what you're going to have to do
- 5:17:55is specify the name then the age then
- 5:17:57the address in that specific order as
- 5:18:00per the parameter show if you try and
- 5:18:02run the code now we should get the
- 5:18:03desired
- 5:18:05output perfect let's say for example you
- 5:18:07wanted to rearrange the parameters and
- 5:18:09put Age first you'll see we have a
- 5:18:11compiler error now because age and name
- 5:18:13are in the wrong order string name
- 5:18:15should be first and int age to be second
- 5:18:17we can still achieve this by using
- 5:18:19something called name parameters we
- 5:18:21don't have to stick to this order if we
- 5:18:22tell the compiler which parameter we're
- 5:18:24directly talking about so we can say age
- 5:18:27callon because our parameter is now
- 5:18:29called age as you can see it's been
- 5:18:31highlighted and then after the colon
- 5:18:33It's the variables that we're talking
- 5:18:34about so it just happens to be that age
- 5:18:37is the same value as the edge
- 5:18:39so maybe we can change to age input and
- 5:18:42place input the end of these so then we
- 5:18:44can have age equals age input and name
- 5:18:47parameter equals the name input and the
- 5:18:49address parameter equals the address
- 5:18:51input now we can have it like this and
- 5:18:53to make it look even better you can
- 5:18:55actually add new lines in here just to
- 5:18:57break it off like this and this is a
- 5:18:59similar pattern that you'll see in
- 5:19:00normal production code putting a space
- 5:19:02here can separate them out a little bit
- 5:19:04and there we go it doesn't matter which
- 5:19:06order that we put these in right now we
- 5:19:07can even put address first first and
- 5:19:10it'll still work okay and if we run it
- 5:19:12you should still get exactly the same
- 5:19:13output it's just specified in a
- 5:19:15different way so let's just have a
- 5:19:17little recap to make use of name
- 5:19:18parameters you don't need to do anything
- 5:19:20to the original function you just write
- 5:19:22it exactly the way you're used to doing
- 5:19:23you specify the parameters in the order
- 5:19:25that you think is sensible and when you
- 5:19:27go to call it you can simply just type
- 5:19:29in the parameter's name first followed
- 5:19:31by callon and then the variable name
- 5:19:33that you want to address once you've
- 5:19:35done that then you can use name
- 5:19:36parameters you can go back to using the
- 5:19:38normal function Fun by just getting rid
- 5:19:39of the name followed by the colon and
- 5:19:41just replacing all of the parameters
- 5:19:43using the correct order and the original
- 5:19:45variables instead of the new notation
- 5:19:47having Name colon followed by name
- 5:19:51input so far we've covered functions
- 5:19:53with no parameters normal parameters
- 5:19:56optional parameters named parameters and
- 5:19:58let's look at a concept called out
- 5:20:00parameters let's demonstrate a quick
- 5:20:01example so we call a function called
- 5:20:03test and we pass in a value and let's
- 5:20:05just make this work so we can have an
- 5:20:07INT Norm equal zero and then we have a
- 5:20:09test function so we can say static void
- 5:20:12test and we can say in n and inside here
- 5:20:16let's make num the value of five now in
- 5:20:18this example if we print out num to the
- 5:20:20screen what do you think is going to
- 5:20:22happen is num going to take the value of
- 5:20:24five or is you're going to keep the
- 5:20:25value zero I'll give you a second to
- 5:20:27think and let's run the
- 5:20:29code so let's run it and see what
- 5:20:33happens and if you guess five you're
- 5:20:36indeed Incorrect and if you guess zero
- 5:20:38you are correct correct and the reason
- 5:20:40why it's not five is because when you
- 5:20:42pass a num inside this parameter it just
- 5:20:45becomes a parameter and therefore it
- 5:20:47becomes a local variable even though
- 5:20:49these two variables are actually called
- 5:20:51exactly the same thing you'll see that
- 5:20:53when I highlight it it only highlights
- 5:20:55this one and doesn't highlight this one
- 5:20:58and the same is true for this it
- 5:20:59highlights these three references but
- 5:21:01not the ones down here the num variable
- 5:21:03is only accessible in each other scope
- 5:21:06this one is available through here and
- 5:21:08here and this one is available through
- 5:21:10here and here therefore if we use this
- 5:21:13assignment then it's actually doing
- 5:21:14nothing if we print out gnome inside
- 5:21:16here num equals num and then we print
- 5:21:20that we can see that it is assigned the
- 5:21:23value five but because it doesn't do
- 5:21:24anything at the end of this function
- 5:21:26then it simply doesn't get returned back
- 5:21:28or anything happens to it so let's see
- 5:21:30how we can make this in to five when it
- 5:21:32gets printed out here so the options
- 5:21:34that we have is we can actually make
- 5:21:36this viid an integer and we can say num
- 5:21:39equals 5 and then we can return num or
- 5:21:41alternatively we could simply just
- 5:21:43return five and not do both of these
- 5:21:44lines so if we run that now then we
- 5:21:47still get zero and the reason why we get
- 5:21:49zero is the test function is being
- 5:21:51called and it returns back an integer
- 5:21:54but we're simply not doing anything with
- 5:21:55it so we can say num equals the return
- 5:21:58value of this function which will be
- 5:22:00five and now if we run it we should see
- 5:22:02how five perfect now this is a little
- 5:22:05bit cumbersome and let's say you wanted
- 5:22:07to return a value and return back
- 5:22:09another value at the same time than what
- 5:22:10are your options so let me show you a
- 5:22:12quick example when you're using int.
- 5:22:14trass we have a string s an out int
- 5:22:18result and a Bool so what this function
- 5:22:20is effectively doing is return you back
- 5:22:23a true or false value to dictate whether
- 5:22:25or not the conversion has been
- 5:22:27successful and it's also returned you an
- 5:22:29out in result and what this means is you
- 5:22:31can actually specify integer variable
- 5:22:34straight inside the brackets so in
- 5:22:35essence this function has gave you back
- 5:22:38two values it's give you back something
- 5:22:40from the integer parameter and it's also
- 5:22:42returned back a Boolean variable so
- 5:22:44let's convert this to code and see what
- 5:22:46the third option is of trying to return
- 5:22:48back five over to this so if we just
- 5:22:51comment out this line and as you saw in
- 5:22:53the trip pass it simply just need to put
- 5:22:55the word out just before
- 5:22:57here out int num now we don't actually
- 5:23:02need to return anything in this specific
- 5:23:03example because we want to just change a
- 5:23:05num variable using the out parameter but
- 5:23:09if we wanted to return something
- 5:23:10separate to the num like a boing like
- 5:23:12the tri pass did then we have that
- 5:23:14option so we can return back true but
- 5:23:16just before we return back true as
- 5:23:18there's a red underline here it will say
- 5:23:20that the out parameter num must be
- 5:23:22assigned so we need to give num a
- 5:23:24value we give it the value five because
- 5:23:27what we're actually doing here even if
- 5:23:29we double click on this we'll still get
- 5:23:31num being selected here cuz the scope is
- 5:23:34still exactly the same now except what's
- 5:23:36happening is when this variable gets
- 5:23:38returned it actually gets put into the
- 5:23:39variable that it was initially called
- 5:23:41with so let's fix these compiler errors
- 5:23:43first so the reason why it's complaining
- 5:23:45is because we need to give it the out
- 5:23:46keyword so when we're specifying in Tri
- 5:23:49pass our first parameter is a string so
- 5:23:51if the user types in 1 2 3 we put in the
- 5:23:53first parameter and then we'd say out in
- 5:23:56result or something along them lines you
- 5:23:58have an out and then you say you want to
- 5:24:00make a new integer and you want to call
- 5:24:02it result see in our case we don't need
- 5:24:04to put int just like in here because
- 5:24:06we've already created our num so the
- 5:24:08only keyword we need is how to follow by
- 5:24:10space and now of course we're trying to
- 5:24:12assign this back into an integer which
- 5:24:15we don't need anymore because this is a
- 5:24:16booing so we can say Bo
- 5:24:19success equals test out n so we print
- 5:24:22out
- 5:24:24Success Print them both the screen now
- 5:24:26we should have five and true the true
- 5:24:28comes from the return true and the five
- 5:24:30comes from num equals 5 so what's
- 5:24:33actually happening here is we're
- 5:24:34creating our new integer we're passing
- 5:24:36it into this function we're also telling
- 5:24:38it's going to be an out parameter so
- 5:24:40this function can still output things to
- 5:24:42our number variable and then when it
- 5:24:44gets placed inside here we assign num
- 5:24:46equal to five and then just because we
- 5:24:48have a bull we have to return true or we
- 5:24:50can return false of course we can just
- 5:24:53take this out completely take away the
- 5:24:55return true and just keep this as
- 5:24:57test of course in both scenarios these
- 5:25:00aren't actually do anything they're just
- 5:25:01completely random values it's just made
- 5:25:03for demonstration so bu is returned back
- 5:25:06true or false doesn't actually mean
- 5:25:07anything I was just demonstrating that
- 5:25:09you can have two return values from one
- 5:25:11function there we go we get five when we
- 5:25:13run it just like this so now I've
- 5:25:15completed this little example let's have
- 5:25:17a look at how it's used in the real
- 5:25:18world so before we continue let's see if
- 5:25:20we can rewrite the tripal function in
- 5:25:23our code so it's going to be a static
- 5:25:25returns back a bu we have a triy pass
- 5:25:27our first parameter is a string s and
- 5:25:29our second parameter is out into result
- 5:25:32and inside there we can just do result
- 5:25:35equals to zero and then we can return
- 5:25:37back true of course we actually need to
- 5:25:38do conversions inside here if you want
- 5:25:40this function to work but I'm just
- 5:25:42joining you for demonstration purpose if
- 5:25:44we uncomment this line out and have a
- 5:25:46look at the parameters again you'll see
- 5:25:47that now they line up perfectly string s
- 5:25:50out in result and it returns back a Bo
- 5:25:52which we have exactly the same here
- 5:25:54there you go Bo in. trass string s out
- 5:25:57in result so that's how this function
- 5:25:59has been declared of course they're
- 5:26:01doing something inside the body that can
- 5:26:03take in the string convert it to an
- 5:26:04integer place it inside result and then
- 5:26:07return whether or not the function has
- 5:26:08been executed successfully so now that
- 5:26:11we understand how to make an example and
- 5:26:13how to make the trip pass function
- 5:26:14ourselves and to give you the
- 5:26:15Declaration let's have a look at a real
- 5:26:17world example so if we had a list of
- 5:26:20string and we had our shopping list that
- 5:26:22was used from the examples we can have
- 5:26:24list string and let's just create our
- 5:26:27own initializer just with coffee and
- 5:26:28milk
- 5:26:29inside there we go so now that we've
- 5:26:31created that we've got our shopping list
- 5:26:33if you wanted to find something inside
- 5:26:35the shopping list we have the function
- 5:26:36index of and what that will do as it
- 5:26:38says here searches for the specified
- 5:26:40object and Returns the zero based index
- 5:26:43of the first occurrence so if we were
- 5:26:45search for coffee then we should get
- 5:26:47return back zero so let's try and run
- 5:26:50this and put it in the console right
- 5:26:51line and see what the value is there we
- 5:26:53go we get zero since it's the first item
- 5:26:56and if we place milk inside here we
- 5:26:57should get back one there we go so let's
- 5:27:01try and rewrite this function into our
- 5:27:03own code so all this will do is loop
- 5:27:06around all of the positions until it
- 5:27:07finds it and it'll find the first one
- 5:27:10and it'll return us back the index so in
- 5:27:12this case there's no out parameters but
- 5:27:14there is a return value so we can also
- 5:27:16include an out parameter and then
- 5:27:19instead of assigning the value of minus
- 5:27:20one we can actually assign the value of
- 5:27:22true or false to dictate whether or not
- 5:27:25that we found it so let's write the code
- 5:27:26normally in here and then let's wrap it
- 5:27:28around in a function so we can say int
- 5:27:30index equal minus1 because we want it to
- 5:27:32start a value that we know is incorrect
- 5:27:35since the lists are from zero onwards
- 5:27:37the value is only ever minus one then
- 5:27:40that means it's not in the list then we
- 5:27:41can then create a for Loop intire equal
- 5:27:43Z shopping list do count because we want
- 5:27:46to go through every single item inside
- 5:27:48the shopping list and inside here we
- 5:27:50want to do a lowercase comparison so we
- 5:27:52can do if the shopping list square
- 5:27:54bracket I do to
- 5:27:56lower do equals and then we want to make
- 5:27:59that equal to just something that we're
- 5:28:01going to type in statically so we can
- 5:28:03just say coffee we can make this into a
- 5:28:06parameter after but this is just for
- 5:28:07demonstration purposes so we have a
- 5:28:09bracket and if we found that match then
- 5:28:11we can say index equals to I cuz the
- 5:28:14index of the position that we want to
- 5:28:15find must be up to I cuz that's the one
- 5:28:17that we've made a match with and we
- 5:28:19don't need an else inside here we don't
- 5:28:22need to set an else inside here to make
- 5:28:24it equal to minus one because if it
- 5:28:25simply doesn't find it then the index
- 5:28:27variable won't change its value so it
- 5:28:29will always stay minus one if it doesn't
- 5:28:31find anything so after the followup is
- 5:28:33run let's just print index out to the
- 5:28:36screen there we go we have zero and if
- 5:28:39we change this over to milk run it again
- 5:28:41then we actually have one placed inside
- 5:28:44here perfect so now what our function is
- 5:28:47doing is giving us the values of the
- 5:28:49index if it finds it now if you type in
- 5:28:51something else if you just add another
- 5:28:53can let's see what happens then we get
- 5:28:55minus one and minus one means that it's
- 5:28:57incorrect and it hasn't found it and
- 5:29:00what we can do inside here we can say if
- 5:29:02the index is greater than minus1 then we
- 5:29:05can say found or else we can say not
- 5:29:08found so let's run the code now and see
- 5:29:10if we get a visual representation not
- 5:29:13found and if we changes back to milk run
- 5:29:15it again then we get found perfect so as
- 5:29:19you can see here we're actually checking
- 5:29:20if the value is greater than minus one
- 5:29:22but it'd be easier if you had a variable
- 5:29:24called found and it actually a booing
- 5:29:26variable so we can make a boo found and
- 5:29:28make that equal to an if statement
- 5:29:31condition so we can say something like
- 5:29:32index is greater than minus one which is
- 5:29:35exactly what we had here a second ago
- 5:29:38and because this is a condition it's
- 5:29:39either going to return back true or
- 5:29:41false and it's going to place the value
- 5:29:43inside found and then we're going to use
- 5:29:45found inside here this is easier logic
- 5:29:47to understand if this is true then it's
- 5:29:50found if this is not true then it's not
- 5:29:52found true true false false it's quite
- 5:29:56really straightforward and very easy and
- 5:29:58clear you have an index and you have
- 5:30:00whether or not it's been found so let's
- 5:30:02try and wrap this around inside a
- 5:30:03function so just as the trip pass does
- 5:30:06it returns us back with a Bool which in
- 5:30:08this case will be our found and it also
- 5:30:10gives us back an integer result but in
- 5:30:12this case it's going to be our index so
- 5:30:14let's see what we can do to write this
- 5:30:16so we can have a static we want to
- 5:30:17return back the Bo and we can say find
- 5:30:20in list we need to give it the item
- 5:30:23we're going to find so we're going to
- 5:30:25need a string s we need a string s for
- 5:30:27the item that we're going to find we
- 5:30:29need to give it the list as well and
- 5:30:31then finally we need our out in
- 5:30:34index there we go so let's comment out
- 5:30:37this code here
- 5:30:40and let's copy this down into our
- 5:30:42function and see what we need to do so
- 5:30:44index has the value of minus one we
- 5:30:46don't need the integer variable
- 5:30:48declaration here because we've already
- 5:30:49declared it inside the parameter so
- 5:30:51after declaring inside here we give it
- 5:30:53the value of minus one minus one
- 5:30:55indicates it hasn't been found since the
- 5:30:57index are zero based so instead of
- 5:31:00shopping list we now just have list and
- 5:31:03list here as well and then instead of
- 5:31:05having it being equal to milk we
- 5:31:07actually want to use our s variable and
- 5:31:09to make it better we can convert it to
- 5:31:10lower case just so we have lowercase
- 5:31:12conversions on both sides of the equals
- 5:31:15if we find a match between the list that
- 5:31:16we've passed in and the S variable which
- 5:31:18is our surge term that we've passed in
- 5:31:20then we assign I to the value of index
- 5:31:23and then it's still got a red underline
- 5:31:25because we haven't returned back
- 5:31:27bu so right now we actually want to
- 5:31:29return back the found variable so we
- 5:31:30have two options here we can return back
- 5:31:32found or we can simply take this and
- 5:31:35return it back in here we don't need to
- 5:31:37run this code to see if it's found or
- 5:31:38not and then store it and then return it
- 5:31:40after since we're not doing anything
- 5:31:42else with it we could just return it
- 5:31:43like this this makes the code
- 5:31:44considerably shorter because this will
- 5:31:46just return back a true or false if the
- 5:31:49index is greater than minus1 then it
- 5:31:50will turn back true if the index is less
- 5:31:52than minus1 then it will turn back false
- 5:31:55so let's see how we can use this
- 5:31:56function inside our code now so we
- 5:31:58wanted to search for milk so let's just
- 5:32:00copy and paste this line and let's see
- 5:32:01if we get the same output as the
- 5:32:03shopping list index of so now our
- 5:32:05function is called finding list you have
- 5:32:08a bracket and then our string s is going
- 5:32:09to be our milk our list is our shopping
- 5:32:12list and our out in
- 5:32:15index so this is going to return us back
- 5:32:18with a booing so we can print that
- 5:32:19booing to the screen and then if we just
- 5:32:21duplicate that down and we can print
- 5:32:23index to the screen and maybe using a
- 5:32:25console right line and just place a
- 5:32:27space right at the
- 5:32:29start so you print out whether it's
- 5:32:31successfully converted or not and we get
- 5:32:33the value back from
- 5:32:35it let's run the code
- 5:32:39so we get true plus the value of one so
- 5:32:42that means it's found it and it's found
- 5:32:43it at one index and the previous line
- 5:32:46before it which just run the normal
- 5:32:47index of function also return back one
- 5:32:50so they're both the same let's just get
- 5:32:51rid of that
- 5:32:56space so if we change these both to
- 5:32:59coffee then hopefully we should see the
- 5:33:01same result but with index zero there we
- 5:33:03go this zero comes from the index
- 5:33:05function this true is from our function
- 5:33:07tell us has been successful and this
- 5:33:09zero is the index so we can actually
- 5:33:11take this and put this straight into an
- 5:33:13if
- 5:33:14statement so just like we had down here
- 5:33:16with the found and not found we can say
- 5:33:18if and then this function call and then
- 5:33:21we can say found and then the item what
- 5:33:24we passed in so in this case it's going
- 5:33:25to be coffee found
- 5:33:27coffee found coffee at index index and
- 5:33:31if it hasn't been found then we can just
- 5:33:33print not
- 5:33:35found and this is getting an error
- 5:33:37because we have two indexes up here so
- 5:33:39let's comment out this line and let's
- 5:33:41try running
- 5:33:42out found coffee index zero and you can
- 5:33:45see these two are the same again one
- 5:33:47comes from here and one comes from that
- 5:33:48index we change these both to milk now
- 5:33:52milk milk and milk then run the code and
- 5:33:55you see found milk index one if you
- 5:33:58wanted to just before you pass in here
- 5:34:00you can ask the
- 5:34:02user enter an item to
- 5:34:06search store it inside string search
- 5:34:09equals console.
- 5:34:12readline and then instead of passing mil
- 5:34:14here we can just pass in Search and then
- 5:34:16instead of passing milk in here we can
- 5:34:18also use the search variable so now if
- 5:34:20we try run it again we can type in milk
- 5:34:22and then we go file milk index one and
- 5:34:24then coffee found coffee index zero and
- 5:34:26just to give this a test let's just try
- 5:34:28and find m not
- 5:34:31found
- 5:34:33perfect so let's have a little recap
- 5:34:35based on this function so initially we
- 5:34:37try to assign the value of zero and then
- 5:34:39print out a normal function and inside
- 5:34:41test we just had a simple num equals 5
- 5:34:45and what this was doing inside is just
- 5:34:46locally assigning it but not actually
- 5:34:48return anything and then we said we
- 5:34:50could assign it and then return it back
- 5:34:52and that was another option of being
- 5:34:53able to assign the value to five and
- 5:34:55then we use the out parameter which
- 5:34:57allowed you to pass in an integer when
- 5:34:59you assign in here it will pass it back
- 5:35:01into the original call and place it in
- 5:35:03this variable here so when we print out
- 5:35:05on the next line it says it's equal to
- 5:35:07five we wrote our own little shopping
- 5:35:09list for the list of strings and then we
- 5:35:11called the shopping list function index
- 5:35:13of and then we actually created our own
- 5:35:15index of function and made it into a
- 5:35:18function so just a little recap what's
- 5:35:20going on here we have a string s which
- 5:35:22is our search term the list full of the
- 5:35:24items in the index that we're going to
- 5:35:26pass back to the user as well as
- 5:35:27returning back a bu to indicate whether
- 5:35:29we found it or not we set the index to
- 5:35:32minus one so then that completes our
- 5:35:34logic at the bottom minus one means it
- 5:35:37hasn't been found found since the list
- 5:35:38is all zero based if this value is
- 5:35:41greater than minus1 then that means that
- 5:35:44we found it inside the list since the
- 5:35:46list is zero based and if it's not that
- 5:35:48and this returns back false then we
- 5:35:50mustn't have found it because this line
- 5:35:52would have executed so we Loop through
- 5:35:54the list we do a lowercase comparison on
- 5:35:57both sides of the equation on the list
- 5:35:58item and on the surge term if they both
- 5:36:01match we run the index and store the I
- 5:36:04variable inside the index and then we
- 5:36:06return whether index is greater than
- 5:36:07minus one so after we return this back
- 5:36:09from the end of the function we can see
- 5:36:11once we run it inside the console right
- 5:36:13Line This is just going to return us
- 5:36:14back with a true or false value as well
- 5:36:16as creating our new integer variable
- 5:36:18called index so on the next line we can
- 5:36:20use index and print out of the screen
- 5:36:22this line should return true or false
- 5:36:24since it's a Boolean return and this
- 5:36:26variable will either give us minus one
- 5:36:28or something above minus one but in this
- 5:36:30case when we actually make use of the
- 5:36:31function we can ask a user to enter
- 5:36:33something get them to type it in and
- 5:36:36pass that into the function but more
- 5:36:38importantly when this function returns
- 5:36:40if it's true we output it's been found
- 5:36:42and if it's false we output not found
- 5:36:45and we only make use of this integer
- 5:36:47variable should this return back true
- 5:36:49there is no point in printing minus one
- 5:36:52to this screen because that's what
- 5:36:53you're going to get if it's not
- 5:36:55found it's going to look something like
- 5:36:57this which is kind of redundant because
- 5:36:59the user doesn't care about this so we
- 5:37:01don't need to include it when we're
- 5:37:02printing not found but we should include
- 5:37:04it when we're printing out the found of
- 5:37:06course this is just a random example Le
- 5:37:07and you might not need to print it out
- 5:37:09to the screen or you might just do
- 5:37:10something else inside here you might be
- 5:37:12searching for something and then you
- 5:37:14might want to edit it or do something
- 5:37:15else with it and then you can make use
- 5:37:17of that index if you wanted to ask the
- 5:37:19user to search for an item and then tell
- 5:37:21them what they want to replace it with
- 5:37:23then you can also do that just as we've
- 5:37:25covered in the last
- 5:37:28examples so after we've covered out
- 5:37:30parameters and normal parameters let's
- 5:37:32have a look at a concept called passing
- 5:37:34by reference so we created inome equals
- 5:37:3610 and then let's call it assign and we
- 5:37:38passed it straight into here and we can
- 5:37:41have a static void assign in
- 5:37:45N if we say n equals 20 if we use a
- 5:37:48console right line up here then num is
- 5:37:50still going to be 10 because we're not
- 5:37:53returning it and we're not really
- 5:37:54affecting it now other ways we can do
- 5:37:57this is by changing this into an INT and
- 5:37:59then we can say return 20 and then we
- 5:38:02don't really need this anymore so we
- 5:38:04could just say
- 5:38:05num num equals a sign and then we'll get
- 5:38:0820 assign back in there we go all we can
- 5:38:11actually make this an out into num and
- 5:38:14then we can say num equals to
- 5:38:1720 and then inside here we can say out
- 5:38:20num we don't need to return back in
- 5:38:22anymore so we can make this valued and
- 5:38:24you see the value 20 so there are
- 5:38:26different ways that we can assign a
- 5:38:27variable straight from here so let's
- 5:38:29have a look at another method so instead
- 5:38:31of having out we can actually use a new
- 5:38:33keyw called ref and the reason why it's
- 5:38:35ref it means it's passing it by
- 5:38:37reference so when you pass a value in
- 5:38:38through a parameter normally you're
- 5:38:40actually passing by value and what that
- 5:38:42means is you're copying this value that
- 5:38:44gets passed inside the brackets into
- 5:38:47here and it's making a whole new
- 5:38:48variable what I mean by that is this
- 5:38:51might have memory dress location one and
- 5:38:53then when this variable gets created it
- 5:38:55has memory dress location 2 for example
- 5:38:57and then it's used within here and then
- 5:38:59two is now deleted because you've got
- 5:39:01rid of the variable because of this
- 5:39:02function but one is still alive since in
- 5:39:05the main function when you pass by
- 5:39:06reference what you're doing is you're
- 5:39:08saying give me the memory address
- 5:39:10location of the variable inside the
- 5:39:11brackets and give it into this function
- 5:39:14rather than copying the value so if this
- 5:39:15has memory just location one and you
- 5:39:17pass it into here then it's also going
- 5:39:19to have one so what this actually means
- 5:39:21is if we put this ref keyword just
- 5:39:23before the int just like we do with the
- 5:39:25out and we can place it here as well
- 5:39:27we're now saying we want to pass this by
- 5:39:29reference so when this 10 gets pass in
- 5:39:31here we actually have a link if you will
- 5:39:34to this gome is now linked to this gome
- 5:39:37which means if we simply do nor equals
- 5:39:3920 and run the code then we're going to
- 5:39:41see 20 the main differences between
- 5:39:44reference and using the out keyword is
- 5:39:46when you're using out within a function
- 5:39:48you actually need to make use of the
- 5:39:50value if I don't include anything within
- 5:39:52here then I'm going to get exception and
- 5:39:54it says the out parameter num must be
- 5:39:56assigned so when using the out keyword
- 5:39:59num has to be assigned when using the
- 5:40:02ref keyword you don't have to make any
- 5:40:03assignment because we're just passing a
- 5:40:05reference in with the out keyword word
- 5:40:07you literally tell the function I want
- 5:40:09to Output this value but if you haven't
- 5:40:11gave it a value to Output then of course
- 5:40:13it's going to be an error so we can
- 5:40:15assign in the reference if we really
- 5:40:17want to but you don't have to and it's
- 5:40:19not required so there are the two main
- 5:40:20differences between passing by reference
- 5:40:22and passing by value and also using the
- 5:40:25out parameter passing by reference is
- 5:40:27technically quicker because of
- 5:40:28efficiency when you don't pass it by
- 5:40:30reference you pass it by value which
- 5:40:32means it'll essentially place that 10
- 5:40:34inside these
- 5:40:35brackets instead of passing the variable
- 5:40:38in it actually just passes the value and
- 5:40:40then it makes a whole new variable down
- 5:40:42here when you pass it in by reference
- 5:40:44you're essentially actually giving it a
- 5:40:46link to this memory address location
- 5:40:48from inside this function which means
- 5:40:50that you don't actually have another
- 5:40:52local variable inside here because this
- 5:40:54one is affecting this one directly you
- 5:40:57can do this with any amount of
- 5:40:58references you want by simply just
- 5:41:00chaining them and adding another one in
- 5:41:02like that for example and then we can
- 5:41:04have a string name and then we can pass
- 5:41:07our ref name inside here and inside here
- 5:41:10we can say name equals hour and this
- 5:41:13doesn't like it if it's unassigned so we
- 5:41:15just give it empty quotes or we can sign
- 5:41:17it another name it doesn't really matter
- 5:41:19it's just a demonstration and if you run
- 5:41:21that but let's just print out the name
- 5:41:23as well to the
- 5:41:24screen there we go we get abber assigned
- 5:41:27into
- 5:41:28there and same thing with this variable
- 5:41:30is we're actually just passing in the
- 5:41:32variable as a reference rather than as a
- 5:41:34value it doesn't matter what the value
- 5:41:36is going to be B cuz it's just given
- 5:41:38this variable inside this function so
- 5:41:40we're actually saying name equals we
- 5:41:42basically just assign it back into the
- 5:41:44original variable so let's just have a
- 5:41:45look at another example so we can have
- 5:41:47some sort of like change name function
- 5:41:49and we can give it the name variable as
- 5:41:51a reference and then we can give it the
- 5:41:53new name that we're going to pass in in
- 5:41:54a second so if we make a new function
- 5:41:56static void change name you're going to
- 5:41:58need a ref string name and a string new
- 5:42:01name and we can simply say in here name
- 5:42:04equals new
- 5:42:05name perfect
- 5:42:07so let's give this a shot so if we just
- 5:42:09comment out this little section and we
- 5:42:11can say console right enter your new
- 5:42:14name then we have a string new name
- 5:42:17equals console.
- 5:42:20readline and then just up here as we've
- 5:42:22done before we could just declare the
- 5:42:23name as being Joe and then we enter the
- 5:42:26new name call this function and then
- 5:42:28after we can say your new name is and
- 5:42:32then we can pass in name perfect let's
- 5:42:34run
- 5:42:35that so at this point the name name is
- 5:42:37actually Joe cuz we've made it appear
- 5:42:39and then we'll enter a new name so we
- 5:42:40can type in abber now it says your new
- 5:42:43name is abber perfect so all this
- 5:42:45function is doing I know it's pretty
- 5:42:47pointless right now cuz it's just doing
- 5:42:48one line but all it's doing is assigning
- 5:42:51the reference to the variable that we
- 5:42:52pass in so what we can do now is make
- 5:42:54sure that we can actually assign this
- 5:42:56variable in and we can also pass in a
- 5:42:58Boolean variable so what we should do
- 5:43:00now is actually make sure that we can do
- 5:43:02the assignment properly cuz you don't
- 5:43:03want to assign it if the value is empty
- 5:43:05and then return back a Bo Lan type just
- 5:43:08to indicate whether or not it's being
- 5:43:09assigned so what we can do in here is
- 5:43:11just on the change name we can wrap this
- 5:43:13around an if statement and if this
- 5:43:15returns back true then your name has
- 5:43:17been changed and if not we can say
- 5:43:20console right Line new name cannot be
- 5:43:22empty or
- 5:43:24no in order to be able to do this check
- 5:43:27as we've used before we could say if new
- 5:43:30name and then just around that we can
- 5:43:32say string do is n or empty and wrap
- 5:43:34that inside the brackets and we want to
- 5:43:36say if it's not and the L empty then we
- 5:43:38want to do our
- 5:43:40assignment after we've done our
- 5:43:42assignment we can return back true
- 5:43:43inside here and then just outside we can
- 5:43:46return back false notice how in this
- 5:43:48little bit of code we actually don't
- 5:43:49have an else we don't need to write else
- 5:43:52inside here and place return false
- 5:43:55inside here because if this returns back
- 5:43:57true then it's going to execute this one
- 5:43:59line return back true and since it's a
- 5:44:02return it's going to jump to the end of
- 5:44:03the function so we don't need the else
- 5:44:05in there because the only way that this
- 5:44:07line will ever execute is if we actually
- 5:44:09had this function returning back false
- 5:44:12so if this returns back true it's going
- 5:44:13to hit this line hit the return jump to
- 5:44:16the end if this returns back false it's
- 5:44:18going to skip over the if statement and
- 5:44:19then just return back false to the user
- 5:44:21perfect so now we've got no compile
- 5:44:22errors let's give it a run so if we want
- 5:44:25to change your name to nothing if we
- 5:44:27just press enter then we can see it says
- 5:44:29new name cannot be empty or no and if we
- 5:44:31type something in then we get our name
- 5:44:33changed so just to round up this video
- 5:44:35let's talk about the main differences
- 5:44:37between the ref parameter and the out
- 5:44:38parameter when you sign in by reference
- 5:44:40you actually have to make the variable
- 5:44:42before you pass it in for example you
- 5:44:44can't just write ref string in here and
- 5:44:46that actually works whereas you can't
- 5:44:48actually do that with the in let me
- 5:44:50demonstrate so if you had the in. trass
- 5:44:53and that has an out parameter you can
- 5:44:55actually type out int index inside here
- 5:44:58you don't actually have to make the int
- 5:45:00index inside here and then specify it
- 5:45:03outside you could just initialize and
- 5:45:05declare the variable straight inside the
- 5:45:07brackets whereas because you're passing
- 5:45:09a reference with the string you actually
- 5:45:11need to make the string first and then
- 5:45:12pass it in so that's a big difference
- 5:45:14between the out and the ref when you
- 5:45:16make news of the actually inside the
- 5:45:18function if you're using a reference
- 5:45:20then you never have to assign it or at
- 5:45:22least it's not required you could have a
- 5:45:24series of if statements inside this body
- 5:45:26and then you might determine that you
- 5:45:27might not need to assign it which is
- 5:45:29exactly what we've got here the only way
- 5:45:31we assign name is if this returns back
- 5:45:33true if it doesn't return back true then
- 5:45:35we simply don't assign it and move to
- 5:45:37here this is a problem if the out
- 5:45:39variable if we change this to an out
- 5:45:41then we're going to get a compiler error
- 5:45:43because it doesn't return back a value
- 5:45:45so we'd have to write something like
- 5:45:46this at the top name equals something
- 5:45:48and then potentially sign it here and
- 5:45:50then return back false so with reference
- 5:45:52you don't have to worry about the fact
- 5:45:54that you might not assign in every
- 5:45:55single path of your code and finally the
- 5:45:58main difference is the fact that out
- 5:45:59parameters are still used by value and
- 5:46:02this is by reference reference will be
- 5:46:04slightly quicker since you'll get the
- 5:46:05memory just location being passed in
- 5:46:07instead of the Val we won't have two
- 5:46:09copies of the same
- 5:46:12variable the focus of this task is to
- 5:46:15create a function that calculates the
- 5:46:16area of a triangle so we're going to ask
- 5:46:18the user for width and height install
- 5:46:20them both create a function to calculate
- 5:46:21the area the function is going to
- 5:46:23calculate it using width times by height
- 5:46:26taking that calculation and dividing the
- 5:46:28value by two call it in the main and
- 5:46:29print out the area of the triangle to
- 5:46:31the screen so let's start with entering
- 5:46:33the width and the height so we can say
- 5:46:35enter width colon
- 5:46:37and have int width equals convert to
- 5:46:42in32 console.
- 5:46:45readline and if you copy these
- 5:46:47down change width to height and change
- 5:46:50WID here to height and just before we
- 5:46:52continue because we did copy and paste
- 5:46:54down then this is quite bad and we
- 5:46:56should use a function here so in both of
- 5:46:58the cases we're going to output
- 5:46:59something to the console and then read
- 5:47:00it in and convert it back as an integer
- 5:47:02so we can say static int because we
- 5:47:04return back an integer so we can store
- 5:47:06it we can say read int and we want a
- 5:47:09string message so we can pass in this
- 5:47:11value inside
- 5:47:12here so we take these two lines and
- 5:47:15place inside here instead of enter
- 5:47:18height we're now going to have message
- 5:47:19instead of int height we're just going
- 5:47:21to have a return value and then we'll
- 5:47:23return in the conversion from the
- 5:47:24console read line and now we can use it
- 5:47:26inside here read in inside the brackets
- 5:47:29you want to put the string output which
- 5:47:30is going to be enter height if we paste
- 5:47:32that inside here and then do the same
- 5:47:35thing for the previous section and and
- 5:47:36change height to width and now get rid
- 5:47:38of these so we just have these two lines
- 5:47:41perfect so let's just print these out to
- 5:47:43the screen on separate lines just to
- 5:47:45make sure it's
- 5:47:47working so the width of 10 and height of
- 5:47:5015 we get 10 and 15 Perfect Since that's
- 5:47:53working okay we can now create a
- 5:47:54function that can calculate the area for
- 5:47:56us so we can get static int calc area
- 5:48:00and we can have int width and int
- 5:48:05height and then taking this formula we
- 5:48:07can simply just do return bracket width
- 5:48:10times by height / two as you can see
- 5:48:13this is literally exactly the same as
- 5:48:15what we've got here it's crucial to
- 5:48:17include the brackets because we want to
- 5:48:19do this calculation first take the value
- 5:48:21of that and divide it by two if this is
- 5:48:23too many steps you can actually break it
- 5:48:25down we can say int calculation is equal
- 5:48:28to the width times by the height and
- 5:48:30then we can simply return calculation /
- 5:48:33two back to the function it's slightly
- 5:48:35gred out because we haven't used use it
- 5:48:37but that's not a problem we're going to
- 5:48:38use it now I personally just to prefer
- 5:48:40is to keep that inside here and just
- 5:48:42have the brackets since a bit more
- 5:48:43obvious to me what's going on but it's
- 5:48:45up to you as the programmer either way
- 5:48:47it makes no difference it's just more
- 5:48:49for the programmer's convenience so now
- 5:48:51we need to call the function above so we
- 5:48:53can say int result equals calc area and
- 5:48:55then pass in the width and pass in the
- 5:48:57height and then we can have console
- 5:48:59right line that just says the area is
- 5:49:01and then result and then let's run it
- 5:49:04you have 10 and 15 and there we go we
- 5:49:06get the area area is
- 5:49:0775 perfect if you didn't want to call
- 5:49:10the function inside here and stall it
- 5:49:11inside a variable you could just output
- 5:49:13this straight to the screen by placing
- 5:49:15the function call straight in the cly
- 5:49:16braces and if you run that again just to
- 5:49:18make sure 105 and we get 75 again
- 5:49:22perfect so just a quick little recap we
- 5:49:24have the console right plus the convert
- 5:49:26inside this function which will let us
- 5:49:28return as back an integer and we can
- 5:49:30pass in the message straight in the
- 5:49:31variable to make this a little bit
- 5:49:32quicker and a bit easier you can
- 5:49:34actually just change this one word
- 5:49:35inside here so you could actually say
- 5:49:38enter message call on space and then
- 5:49:41just inside here you can say height and
- 5:49:43just inside this one you can say width
- 5:49:45just to reduce on code because in this
- 5:49:47specific case the only thing that
- 5:49:49changes between them two string
- 5:49:50variables was the fact that one was
- 5:49:52width and one's
- 5:49:53height there we go you can see it's
- 5:49:55exactly the same now so after we've read
- 5:49:58in these two variables we pass it into
- 5:50:00the function and all the function will
- 5:50:01do is take width and height times them
- 5:50:04both together take that value and divide
- 5:50:06it by two and return it back to the user
- 5:50:08and then we can call it straight in here
- 5:50:09the area is calc area and pass in width
- 5:50:11and
- 5:50:15height in this task we're going to
- 5:50:16create a function that will return us
- 5:50:18back with the sum of numbers inside an
- 5:50:20integer array so first step is to create
- 5:50:23an initialize an array of numbers create
- 5:50:25the function with the int return type
- 5:50:27and it also takes a int array as a
- 5:50:28parameter the function should return a
- 5:50:30total of all the numbers inside the
- 5:50:32interray call it in the main output the
- 5:50:35total and just for extra bonus points
- 5:50:37inside the function instead of using a
- 5:50:39for Loop immediately you can check if
- 5:50:41the array length is greater than zero
- 5:50:43and then return minus one if it's empty
- 5:50:45and then check that inside the main and
- 5:50:47then output a specific message and also
- 5:50:49see if you can think of any other
- 5:50:51implementations you can use instead of
- 5:50:53having to do return minus one how else
- 5:50:55can you make this function whether that
- 5:50:56means you have to change the parameters
- 5:50:58or return type or anything like that
- 5:51:00just have a think about the situation
- 5:51:01and see what other options you can make
- 5:51:03of course the output at the end is still
- 5:51:04going to be the same but it's the way
- 5:51:06you do it and the problem solving skills
- 5:51:08that I'm trying to get you to get used
- 5:51:09to let's get started so the create
- 5:51:11initialize an array of numbers so you
- 5:51:13have int numbers equals new int and
- 5:51:16let's just allocate them manually 1 2 3
- 5:51:184 5 and add a semicolon there we go now
- 5:51:21let's create the function sum of
- 5:51:24numbers and we want it to return an
- 5:51:26integer back with the total and we want
- 5:51:27to take an interray called
- 5:51:30numbers and then inside here we need to
- 5:51:32actually return back an INT so we can
- 5:51:34create an in total make that equal to Z
- 5:51:37and at the end we can return total and
- 5:51:39then during the middle we're going to
- 5:51:40have to write our for Loop so we can go
- 5:51:42through the integer array and count
- 5:51:43through everything so we can use a for
- 5:51:45each V item inside our numbers and just
- 5:51:48inside here we can say total plus equals
- 5:51:51item perfect so if we can call it in
- 5:51:54here we can have a console right line
- 5:51:56and we can just say sum of numbers and
- 5:51:57pass our numbers array in the parameters
- 5:52:00so we're just going to Output it to the
- 5:52:01screen just for the time
- 5:52:02being so sum of values from zero all the
- 5:52:05way till 5 is 15
- 5:52:08perfect so now that we've done that
- 5:52:10we've created a function that Returns
- 5:52:11the total numbers call it in the main
- 5:52:13I'll put the total so we can just say
- 5:52:15the
- 5:52:15total the total is and then wrap this
- 5:52:19around in the C
- 5:52:20brace there we go the total is
- 5:52:2315 now with the extra points we can
- 5:52:25check the array length and return minus
- 5:52:27one if the array is empty so let's see
- 5:52:30what we can do in here before we even
- 5:52:31end up going inside anything to do with
- 5:52:33the total we can wrap this around in if
- 5:52:35statement that says if the numbers do
- 5:52:37length is greater than zero then we can
- 5:52:39proceed with this little section so if
- 5:52:41we highlight all of this hold alt and
- 5:52:43press upar key then because it's one
- 5:52:45back just press tab once and just press
- 5:52:47the delete key here and let's get rid of
- 5:52:48the empty Line This is going to return
- 5:52:50us back with an error because not all
- 5:52:52code path to return a value if this
- 5:52:54returns back true then we'll eventually
- 5:52:56hit a return but if this returns back
- 5:52:58false then we not do anything inside
- 5:53:00here so let's have a look at our
- 5:53:01requirements return minus one if the
- 5:53:04array is empty this dictates whether the
- 5:53:06ray is empty so if this returns back
- 5:53:08false then it's going to come into here
- 5:53:10and we can say return minus one so we've
- 5:53:13got this function call in here we don't
- 5:53:15want to print the total right now
- 5:53:17because we want to make sure that this
- 5:53:18total is actually greater than zero
- 5:53:21because we don't want to make a sum of
- 5:53:23the numbers if the array is empty so if
- 5:53:25you run this right now going to get the
- 5:53:27total is 15 and if we get rid of all
- 5:53:29these variables and just have the length
- 5:53:31of five but not give it any value so all
- 5:53:34the values are going to be zero then if
- 5:53:35we still run that then the total is
- 5:53:37going to be zero but that's still an
- 5:53:38acceptable value because that's actually
- 5:53:40got values
- 5:53:41inside but if you said the array length
- 5:53:44is zero then the total is minus one of
- 5:53:46course we don't want the user to see
- 5:53:48minus one so we want to bring this back
- 5:53:51and instead of calling the function
- 5:53:52immediately down here let's call it here
- 5:53:55we can say int result is equal to this
- 5:53:58and then we can say if the result is
- 5:54:00greater than minus one then we can
- 5:54:02output the total and again press into
- 5:54:04this line hold alt and press Arrow key
- 5:54:07and then we can place the result inside
- 5:54:09here and the L's to this will be canot
- 5:54:12add up an empty array there we go so now
- 5:54:15if you run it we should still see 15 but
- 5:54:18if we get rid of this and put it back to
- 5:54:20zero there we go we cannot add an empty
- 5:54:24array so we bring that back perfect so
- 5:54:27now if we check this we've got check in
- 5:54:28the main output the message and then now
- 5:54:31we've got this little solution do we
- 5:54:33need to return minus one how else can we
- 5:54:35make this so right now we're going to
- 5:54:37have to return back minus one cuz that's
- 5:54:39the only way that we can tell the user
- 5:54:41whether or not it's been successful if
- 5:54:43the r is empty we return minus one and
- 5:54:46if the ray is not empty then we return
- 5:54:47something greater than minus one which
- 5:54:49will be something above zero or zero
- 5:54:51itself but as long as it's greater than
- 5:54:53minus one that's how we know it's
- 5:54:54complete but we don't actually want to
- 5:54:56do this we want to be ideally returning
- 5:54:59back a Boolean so let's duplicate this
- 5:55:01function and show you both
- 5:55:03implementations let's change this into a
- 5:55:05Bo
- 5:55:07and then because we're still going to
- 5:55:08need to reference the total we're going
- 5:55:09to have an out in total so we need to
- 5:55:12change up a few things inside here
- 5:55:14instead of this return minus one we can
- 5:55:16do return false to indicate that it
- 5:55:18hasn't been successful instead of
- 5:55:20returning back total we can actually
- 5:55:21just return back true now we haven't
- 5:55:23done everything yet we don't need to
- 5:55:25redeclare in total inside here because
- 5:55:27we already declared it inside here so we
- 5:55:29could just initialize total to equal to
- 5:55:31zero but the problem here is if we make
- 5:55:34total equal to zero just within this
- 5:55:36path
- 5:55:36then it's still going to complain that
- 5:55:38total must be assigned so we can take
- 5:55:40this and just assign it right at the
- 5:55:41start of the function just like that
- 5:55:44there we go so we make total equal to
- 5:55:46zero that gets rid of the compil errors
- 5:55:48and gives it a value the reason why we
- 5:55:50have to give it a value is because this
- 5:55:51out in parameter doesn't like it if you
- 5:55:54don't assign the value that is a
- 5:55:55requirement for using the out parameter
- 5:55:57so we make total equal to zero check the
- 5:55:59numbers length If This Were to return
- 5:56:01back false then we return false to
- 5:56:03indicate that we can't do anything if it
- 5:56:06this returns back true it will go
- 5:56:07through and count all of the items place
- 5:56:10inside total and then return back true
- 5:56:13so let's have a look and see what else
- 5:56:15we can write this little section of code
- 5:56:16so instead of having to call inside here
- 5:56:18and use it within there then we can
- 5:56:20actually do this another way the reason
- 5:56:22why we actually calling this straight
- 5:56:23inside here is because we can't place
- 5:56:26this straight into here since it returns
- 5:56:27us back with an integer and now that is
- 5:56:29actually fine if that returns back of
- 5:56:31the integer cuz as you can see there's
- 5:56:33no problems here the issue arises is
- 5:56:35when we have our result variable we can
- 5:56:37use it inside the bracket and down here
- 5:56:39there's no point in calling this twice
- 5:56:41to get the same value although this
- 5:56:42would still technically work but this is
- 5:56:44not really efficient since you're
- 5:56:45calling the function twice it's best to
- 5:56:48call it
- 5:56:49once store the value and then make use
- 5:56:51of it twice when we use our out
- 5:56:54parameter you'll see that this gets a
- 5:56:55bit easier so straight inside the if
- 5:56:57statement we can say sum of numbers and
- 5:56:59then we can pass in our numbers and then
- 5:57:01space out into total and what that'll do
- 5:57:04now is because this returns us back with
- 5:57:06the fol we don't need any additional
- 5:57:08conditions like we have one here cuz
- 5:57:09this will turn us back with a true or
- 5:57:11false true if it succeeds false if it
- 5:57:14doesn't so we don't need to say if it's
- 5:57:16greater than minus one because we don't
- 5:57:17actually care about the value of the
- 5:57:19total right now we just care if this
- 5:57:20returns back with true or false so
- 5:57:23inside here we can take this same output
- 5:57:24message and instead of having result
- 5:57:26here we can have
- 5:57:27total and then we can take this exact L
- 5:57:30statement and place it here and let's
- 5:57:32see what happens now the total is 15 and
- 5:57:35the total is 15 so they both turning
- 5:57:37back the same values let's add something
- 5:57:39else on here just to make sure it works
- 5:57:41there we go so you can see that both
- 5:57:43these Solutions look very identical but
- 5:57:45one is considerably different we have to
- 5:57:47make a variable outside use inside here
- 5:57:50and use inside here whereas in this
- 5:57:52function we can actually just call the
- 5:57:53function straight inside that will
- 5:57:55process our new out into total and then
- 5:57:58we can simply use total inside here and
- 5:58:00we don't have to require another
- 5:58:01additional variable just outside here as
- 5:58:04this one will and you can see that the
- 5:58:06functions is a little bit better because
- 5:58:08we're returning true and returning false
- 5:58:10to indicate whether or not it's been
- 5:58:11successful whereas in this solution we
- 5:58:13have to return back an arbitrary value
- 5:58:15and then check that in the console the
- 5:58:18reason with returning minus one is kind
- 5:58:20of a problem is what if these numbers
- 5:58:22are actually all negative then this
- 5:58:24value is going to be very bad this can
- 5:58:27only really work if all of these values
- 5:58:29are positive if you had negatives for
- 5:58:32all of these and we tried running the
- 5:58:34code there you go this is a major issue
- 5:58:37cannot add up an empty array well it's
- 5:58:39not the fact that it is empty it's the
- 5:58:41fact that these values are smaller than
- 5:58:43minus one so you don't actually want to
- 5:58:45be going with something like this
- 5:58:46approach where you're comparing the
- 5:58:48value because you don't know what
- 5:58:50numbers are going to be typed into here
- 5:58:51if the user were type in positive or
- 5:58:53negative you want to provide a solution
- 5:58:55just like this one that works for either
- 5:58:57positive or negative you don't want to
- 5:58:59restrict that by checking if it's
- 5:59:01greater than minus one will only works
- 5:59:03if all of these are positive values all
- 5:59:05this is an index for the array since
- 5:59:07arrays start at zero and go upwards you
- 5:59:09can't have a negative -1 array position
- 5:59:11but in terms of numbers that are
- 5:59:13randomly typed inside of a ray you
- 5:59:14should always be wary of using this
- 5:59:16option because if some of them are
- 5:59:18negative then this could return back
- 5:59:20false so let's just have a quick recap
- 5:59:23so we have an integer array we have our
- 5:59:25values initialized down here and then
- 5:59:27the first solution we call the function
- 5:59:29passing the numbers array get our result
- 5:59:31back check if it's greater than minus
- 5:59:33one then that means it's been successful
- 5:59:35so we print it out and else we tell it
- 5:59:37it's an empty array now second option we
- 5:59:40actually call the function because it
- 5:59:41returns back a boly in this case so we
- 5:59:43can place it straight inside the if
- 5:59:44statement and then we can pass in the
- 5:59:46numbers AR and then we have an out in
- 5:59:48total which will give us the total and
- 5:59:50in this case we don't need to check the
- 5:59:51value because this returns us back with
- 5:59:53the true or false and in this case true
- 5:59:55determines whether it's been successful
- 5:59:57and false it doesn't if it returns back
- 5:59:59true then it has some values inside the
- 6:00:01numbers array that we can actually add
- 6:00:03up and if it returns back false then
- 6:00:05that means the array is empty so let's
- 6:00:07have a look at the implementations so
- 6:00:09without the out parameters this is the
- 6:00:10first solution up here we get the
- 6:00:13numbers length check if it's greater
- 6:00:15than zero if it's not we return minus
- 6:00:17one make a new variable called total
- 6:00:19make equal to zero and we run a for each
- 6:00:21Loop V item inside the numbers and we
- 6:00:24just say total plus equals item add them
- 6:00:26all up in the for Loop and simply return
- 6:00:28the total and then in the out solution
- 6:00:31we have a total equals zero which just
- 6:00:33initializes this variable otherwise
- 6:00:35we'll get a compiler then we can say if
- 6:00:37the numbers is greater than zero then we
- 6:00:39can say if numbers. length is greater
- 6:00:40than zero then we can do the for Loop
- 6:00:43and return back true and if it turn back
- 6:00:45false then we can say false inside here
- 6:00:47and then when we use it straight in the
- 6:00:49brackets we have access to it straight
- 6:00:50away since it uses a Boolean variable
- 6:00:53and then our value will be placed inside
- 6:00:54our out into total parameter which is
- 6:00:57used down here for the output to the
- 6:00:59screen and again if this returns back
- 6:01:01false then it will say I cannot add up
- 6:01:03an empty rray and then we hit the
- 6:01:04console read line and we at the the end
- 6:01:06there we go that covers everything
- 6:01:07inside
- 6:01:09here exceptions are used to prevent your
- 6:01:11code from crashing and then the program
- 6:01:13closes down let's have a look at a very
- 6:01:16very common example let's have a console
- 6:01:18right and we can say enter a number and
- 6:01:21then we can use int Norm equals convert
- 6:01:24to int32 and then we can have a console.
- 6:01:27readline inside here and let's just
- 6:01:29print out number to the screen now if we
- 6:01:31assume everything goes well and we
- 6:01:33actually enter an integer like 15 and we
- 6:01:35press enter then we simply get 15
- 6:01:38returned and as we saw in the past when
- 6:01:40we covered the trip pass function if you
- 6:01:42enter anything else that's not exactly a
- 6:01:44number then you get again exception
- 6:01:47exception unhandled unhandled means that
- 6:01:49we haven't wrote anything specific that
- 6:01:51will handle this exception so it's
- 6:01:53causing the program to close it's a
- 6:01:55system. format exception it says the
- 6:01:58input string was not in a correct format
- 6:02:00so this is a format exception because
- 6:02:01we've entered in a very weird set of
- 6:02:04characters that can't be converted over
- 6:02:05to an integer so now our program is
- 6:02:08forced to crash and now rest of our code
- 6:02:10is going to be deleted now because it
- 6:02:11won't be able to run so if you had any
- 6:02:14important data that was stored just
- 6:02:15before this point then it is now lost
- 6:02:17this is very crucial because if you have
- 6:02:19a system that rides to a database or it
- 6:02:21stores some very important information
- 6:02:23that someone's typed in you want to
- 6:02:24handle the exceptions so if something
- 6:02:26were to arise it doesn't actually close
- 6:02:28the program down and they lose all of
- 6:02:30their data I'm sure you've had it in the
- 6:02:31past where you've tried to do something
- 6:02:33and the program comes up Windows and it
- 6:02:35says this program had to stop responding
- 6:02:37when that normally comes up it means
- 6:02:39that Something's Happened that's so
- 6:02:41fatal that the program cannot continue
- 6:02:43it has to crash down and in most cases
- 6:02:45this is what's called an exception so if
- 6:02:47we run this code again let's have a look
- 6:02:48and see what else we can do to make this
- 6:02:50code break so of course as you heard
- 6:02:52before a gnome integer can only survive
- 6:02:55up to about 2 billion numbers so if you
- 6:02:57enter a number bigger than that let's
- 6:02:59see what happens then we get an overflow
- 6:03:01exception value is either too large or
- 6:03:04too small for an in3 too of course this
- 6:03:06number is very big and only a long would
- 6:03:09handle something like this an integer
- 6:03:11definitely won't so these are the kind
- 6:03:13of exceptions where you'd have to handle
- 6:03:15it yourself manually otherwise your
- 6:03:17program's going to crash coming up next
- 6:03:19we're going to go through the ways that
- 6:03:20we can handle these and make sure
- 6:03:22programs don't come to a halt exactly
- 6:03:24like these
- 6:03:27examples I want to explore a technique
- 6:03:29called trycatch this is very similar to
- 6:03:32when we use the int. trass what this
- 6:03:35essentially does does is it tries to do
- 6:03:37something and it lets you know if
- 6:03:39something went wrong this is exactly the
- 6:03:41same when we're dealing with exceptions
- 6:03:43except when we're doing a try catch what
- 6:03:45the catch means is it's catching the
- 6:03:47exceptions that happen and therefore it
- 6:03:49is now a handled exception instead of
- 6:03:51unhandled exception so before we try and
- 6:03:53fix how this works let's just explore
- 6:03:55the syntax so we can trpe in try Follow
- 6:03:58by curly brace and just after this curly
- 6:04:00brace we can say catch and then we're
- 6:04:03going to put something inside the
- 6:04:04brackets and then we have it opens
- 6:04:06brackets if you wanted a quick shortcut
- 6:04:08to this just like we've done the for
- 6:04:10Loop and we double tap tab we can
- 6:04:12actually delete this so you can type in
- 6:04:13try and press tab twice and then you get
- 6:04:16try the Open brackets you get the
- 6:04:17exception and then we get a throw so
- 6:04:20let's get rid of this and what we can do
- 6:04:22right now is we can actually place the
- 6:04:24code that we want inside here inside
- 6:04:26this Tri bit because we're telling the
- 6:04:29compiler that we want to try to do the
- 6:04:30things inside here and then we want to
- 6:04:32catch these things so inside these
- 6:04:34brackets what you want to actually do is
- 6:04:37decide what thing that you want to catch
- 6:04:39if you type in exception this will catch
- 6:04:42every single exception now the problem
- 6:04:44with that is you'll actually face some
- 6:04:46difficulties when you're trying to
- 6:04:48Output very very specific messages so as
- 6:04:51we've said before when you type in a
- 6:04:53value too large you get an overflow
- 6:04:54exception but when you type a value that
- 6:04:57can't be converted like a square bracket
- 6:04:59then you get an input format exception
- 6:05:01so first what you actually want to do is
- 6:05:03run this code outside the try and let's
- 6:05:06run it now and let's get them bad values
- 6:05:09so we had the square brackets and we can
- 6:05:10get this and let's copy it down and
- 6:05:12place it inside a comment just like this
- 6:05:15and then let's rerun
- 6:05:17it and if you type a number too big take
- 6:05:21this stop the program and put it inside
- 6:05:24here so now that you know what the
- 6:05:25exceptions are called we can actually
- 6:05:27write these inside the code so we have a
- 6:05:29form exception and an overflow exception
- 6:05:31but before we get into these specific
- 6:05:33exceptions let's just catch everything
- 6:05:35this is just a catch all and then see
- 6:05:38how this
- 6:05:39works so as we've put it back inside
- 6:05:42here let's see what actually happens so
- 6:05:44we can just put a console right on here
- 6:05:46and say something has went
- 6:05:49wrong the reason why it's something has
- 6:05:52went wrong is because this is a catch
- 6:05:54all any exception appears during this
- 6:05:57code it will always come into here
- 6:05:59regardless of what the exception is
- 6:06:01whether it's a format or an overflow it
- 6:06:03doesn't matter it will always come into
- 6:06:05here so it's going to be something
- 6:06:06because this is not specific at all so
- 6:06:09let's run the code enter a very big
- 6:06:12number something has went wrong but if
- 6:06:14you notice how the program doesn't
- 6:06:15actually clash it's still very much
- 6:06:17alive and we can test this by putting a
- 6:06:19simple message here at the end that says
- 6:06:21goodbye and we run it again enter a big
- 6:06:24number and there you go we get the
- 6:06:26message as goodbye so it successfully
- 6:06:28skipped over this section without
- 6:06:30actually causing an exception that
- 6:06:31breaks the program so even though we've
- 6:06:34entered something very fertile because
- 6:06:36we've handled it then there's no
- 6:06:38problems so if we now type the other one
- 6:06:40where we have the square brackets then
- 6:06:42you'll see we'll also get something has
- 6:06:43went
- 6:06:44wrong so if you wanted to catch a very
- 6:06:47specific exception what we can do is say
- 6:06:49catch open the brackets and we can say
- 6:06:52format exception just like it says above
- 6:06:54here and then place the curry braces and
- 6:06:57what you can do now is you can actually
- 6:06:58chain these and these go from top to
- 6:07:01bottom when an exception occurs up here
- 6:07:04it will match this one if if it doesn't
- 6:07:05match it will match this one and you
- 6:07:08want to put the catch all right at the
- 6:07:09end so if it doesn't match any of the
- 6:07:11ones above it it will always match the
- 6:07:13last one so you can have a generic
- 6:07:15message inside here but you can have a
- 6:07:17very specific message inside here
- 6:07:19because it's a format exception you can
- 6:07:21say please only enter numbers so now if
- 6:07:24you run the code and if we type in
- 6:07:25square brackets it'll say please only
- 6:07:27enter numbers there we go so this is a
- 6:07:29very specific example and if we copy and
- 6:07:32paste this down again and then we can
- 6:07:34say overflow exception
- 6:07:36and we can say please enter a number
- 6:07:40less than 2
- 6:07:43billion if you enter a very big number
- 6:07:46again please enter a number that's less
- 6:07:48than 2 billion
- 6:07:49perfect so this will get checked like if
- 6:07:52statement it will start on the first one
- 6:07:54and go down the list imagine this is a
- 6:07:56if and then we have an else if and then
- 6:07:58else if as well if you place this one
- 6:08:01right at the start then you're actually
- 6:08:03going to get compiled errors and you
- 6:08:05can't actually have the catch all right
- 6:08:07at the start so even if you wanted to
- 6:08:09the compiler literally doesn't let you
- 6:08:11and anyway you don't really want to be
- 6:08:13doing that because then you'll be ruling
- 6:08:14out these two and it's kind of pointless
- 6:08:16writing this code if it's never going to
- 6:08:18get used anyway so now when we run the
- 6:08:20code you can see even if we typ in a bad
- 6:08:22value our program will still run and
- 6:08:24everything works okay so now what we can
- 6:08:26do is actually make this into a real
- 6:08:28life
- 6:08:29situation so if we actually type in 10.2
- 6:08:32then it says please only enter numbers
- 6:08:34then we're going to have an issue here
- 6:08:35here because if we try and print the
- 6:08:37value down here then it's going to cause
- 6:08:38a problem let's try and print out num
- 6:08:41and you can see that num does not exist
- 6:08:43now we have an issue of scope in is only
- 6:08:45declared within here and here so if you
- 6:08:47wanted to print out the value of gome
- 6:08:49we'd have to print it out up here so now
- 6:08:51let's run it again and if we enter 20 20
- 6:08:53comes up if we've entered 15.5 then it
- 6:08:56actually skips over this console right
- 6:08:58line because the exception occurs right
- 6:09:00here and then it'll jump into one of
- 6:09:02these paths so in order to store the
- 6:09:04value of num you can either the move num
- 6:09:06outside here and make it equal to zero
- 6:09:08and then assign num inside here and then
- 6:09:11you can take this console right line and
- 6:09:13actually put it right after the triy
- 6:09:14pass but I don't really suggest this
- 6:09:16option so if you said 15.5 then you're
- 6:09:19just going to get zero returned but we
- 6:09:21don't really care about the zero because
- 6:09:23we're not going to get the value
- 6:09:24converted so why do we need to print the
- 6:09:26zero and the answer is we don't so what
- 6:09:29we should do is keep it all inside here
- 6:09:33again and then what we should do instead
- 6:09:36is wrap this around in a while loop so
- 6:09:39this will try to do the code this
- 6:09:41section will catch them all and then we
- 6:09:43simply have a good buy at the end but
- 6:09:45let's just make sure that this actually
- 6:09:46runs because if we type in
- 6:09:4915.5 then the user still hasn't entered
- 6:09:51the number but our program was closed so
- 6:09:54we want to loop around until the user
- 6:09:56has successfully entered a correct
- 6:09:58number so we can say bull looping equals
- 6:10:01true and we can have while the cord is
- 6:10:04looping that off the curly braces let's
- 6:10:06place our entire Tri cat inside here if
- 6:10:09we highlight it all hold alt and press
- 6:10:11the op power key and then tab it once
- 6:10:14and then get rid of this empty line so
- 6:10:16now we'll keep looping around here until
- 6:10:18this is going to be a different value so
- 6:10:20because the while loop will only keep
- 6:10:22looping until this is true and the
- 6:10:24moment when it's false is when it will
- 6:10:26cancel the W Loop so as I said before
- 6:10:28when we run this code and then just at
- 6:10:31this specific line if the exception
- 6:10:33occurs it will actually skip this line
- 6:10:34and jumped below here so if it skip this
- 6:10:37line then what we can actually do is we
- 6:10:39know if it reaches this line then the
- 6:10:41conversion has been successful so we can
- 6:10:44print out to the screen and we can say
- 6:10:45looping equals false and we don't need
- 6:10:48to place looping is equal to True inside
- 6:10:50all of these three paths because if we
- 6:10:52don't assign it to true then it's still
- 6:10:55going to be the value false no matter
- 6:10:56how many times you loop around so let's
- 6:10:58try and run that
- 6:11:01now enter a number 15.5 enter a number
- 6:11:0415
- 6:11:06and we can keep doing this forever as
- 6:11:08it's a while loop and as soon as we
- 6:11:10enter a valid character 20 and now it
- 6:11:12says goodbye because we hit the end of
- 6:11:14the
- 6:11:15program so that's it for this video but
- 6:11:17let's just have a little recap so we've
- 6:11:20determined when we run our code just
- 6:11:22these two lines that in both situations
- 6:11:24we can have a format exception and an
- 6:11:26overflow exception we can use these to
- 6:11:28catch them specific exceptions and then
- 6:11:30have a catch all at the end the reason
- 6:11:33why you want to catch multiple is you
- 6:11:34can have a very specific message
- 6:11:36overflow dictates we have a number
- 6:11:38that's too big or too small to fit
- 6:11:40inside of an integer it can be less than
- 6:11:42negative2 billion or can be greater than
- 6:11:452 billion and that's how you get the
- 6:11:46Overflow exception we get him a format
- 6:11:49exception if you entered something
- 6:11:50that's literally not a number or
- 6:11:52something that has decimal places and
- 6:11:54then our catch all can be if anything
- 6:11:55else goes wrong we can output a generic
- 6:11:57message by 99% of the cases you'll
- 6:12:00either enter number too big or too small
- 6:12:03or it in the wrong format and this AC
- 6:12:05ction is just so it catches everything
- 6:12:06else should anything else happen so at
- 6:12:09this point even though our program
- 6:12:10doesn't actually close and there's no
- 6:12:11exceptions the user hasn't had a chance
- 6:12:13to enter the number again so we use this
- 6:12:15inside a while loop and we can say while
- 6:12:17looping is true when we hit a conversion
- 6:12:20that's successful we say looping is
- 6:12:21equal to false and once it hits this
- 6:12:23line the Y Loop will then exit and then
- 6:12:25we'll get our goodbye message and we
- 6:12:27wrap the entire try catch inside the W
- 6:12:30Loop just so this keeps on looping until
- 6:12:32the user has typed in a successful value
- 6:12:35as demonstrated
- 6:12:37above we can type all this and 15.2 15
- 6:12:41Point 15 and then finally says 15 and
- 6:12:45then
- 6:12:47goodbye so I want to explore different
- 6:12:49ways you can output messages inside this
- 6:12:51catch block when you have a catch
- 6:12:53exception this is actually a parameter
- 6:12:55that we're passing in this parameter can
- 6:12:57actually have a variable as well
- 6:12:59normally when you specify parameters in
- 6:13:01a function form you say in and then some
- 6:13:06something whereas right now we're kind
- 6:13:08of defining the data type which is our
- 6:13:10exception in the same case as int here
- 6:13:13but we're actually not giving it a name
- 6:13:14so it looks something like this of
- 6:13:16course when we do this inside a function
- 6:13:17we're going to get a syntax error but
- 6:13:19for a catch we're not going to get any
- 6:13:21errors at all and the reason is because
- 6:13:22we can catch without having to use a
- 6:13:24local variable so if we actually add a
- 6:13:26local variable make you call it e for
- 6:13:29exception then we actually have lots of
- 6:13:31options inside here Type e then we have
- 6:13:33lots of data and the stack traces very
- 6:13:35helpful when you debuging but we'll
- 6:13:37cover that at later date the thing I
- 6:13:38want to focus on right now is the
- 6:13:40message so we can use this and print out
- 6:13:42to the screen so we can say error callon
- 6:13:46and then type in E message and let let
- 6:13:49run that now if you enter something
- 6:13:51invalid input string was not in a
- 6:13:54correct format if you enter a very big
- 6:13:56number then values either too large or
- 6:13:59too small for an in32 as you can see
- 6:14:01these error messag are pretty
- 6:14:02descriptive and normal users should be
- 6:14:04able to read this and understand it of
- 6:14:06course if you want this error message to
- 6:14:08be more specific then you can add some
- 6:14:10more catches inside here and we can have
- 6:14:12format
- 6:14:14exception this will be if you type in
- 6:14:16something other than number but if you
- 6:14:17do exactly the same thing in here then
- 6:14:20it's kind of redundant because you're
- 6:14:21just outputting exactly the same value
- 6:14:23if we try and put the square brackets
- 6:14:25again then we're going to get this
- 6:14:26string which is actually outputed from
- 6:14:28this line but even if we include the
- 6:14:30catch all exception you get exactly the
- 6:14:32same error message outputed from here
- 6:14:35the moral of the story is you should
- 6:14:37always have a catch all right at the end
- 6:14:39of your chain but if the only thing
- 6:14:41you're going to do inside here is simply
- 6:14:43output this message to the screen then
- 6:14:45you don't need any multiple catches what
- 6:14:47you should do is catch multiple
- 6:14:49exceptions that you know are going to
- 6:14:50get fired in your code and then you want
- 6:14:52to handle them and then have a catch all
- 6:14:54at the bottom that will display the
- 6:14:55message generically instead of
- 6:14:57displaying the message right here you
- 6:14:59can actually have a more specific
- 6:15:00message like we covered in the last
- 6:15:01video so this could be please only enter
- 6:15:05numbers and we don't actually need the
- 6:15:07message in this case because we know
- 6:15:09what exception we're going to catch
- 6:15:10because we've specified formal exception
- 6:15:13another little tip if you hover over the
- 6:15:15function call it says here format
- 6:15:17exception and overfall exception which
- 6:15:19are the two that we handled in the
- 6:15:20previous video this is very easy and
- 6:15:23understandable if you can just read the
- 6:15:24values from here and to see what
- 6:15:26exceptions can get raised and the same
- 6:15:28thing here you get an IO exception for
- 6:15:30the console right line the console right
- 6:15:32can also give back an IO exception as
- 6:15:34well generally speaking these console
- 6:15:36functions almost never return back
- 6:15:38exceptions I don't think I've ever hit a
- 6:15:41situation where this exception has been
- 6:15:42returned unless you do something
- 6:15:44ridiculous and maybe you type in way too
- 6:15:46many values that the console right line
- 6:15:48can handle let's try an example and we
- 6:15:51can do something like this if you enter
- 6:15:52a very big number that can't be
- 6:15:54specified inside the integer then
- 6:15:56console right line has so many overloads
- 6:15:58that actually processes it as a long
- 6:16:00right now so you almost can't break this
- 6:16:02unless something fundamental to your
- 6:16:04computer actually happens so you have
- 6:16:06exception e and with exception e you can
- 6:16:09actually take this local variable and
- 6:16:10print out the message this is the
- 6:16:12preferred pattern because instead of
- 6:16:14outputting here something went wrong
- 6:16:16just very generically you can actually
- 6:16:18give them the error message that comes
- 6:16:19back from the exception the reason why
- 6:16:21this is helpful is because if it doesn't
- 6:16:23hit any of these exceptions and it just
- 6:16:25happens to fall into the catch all at
- 6:16:27least the user has an idea of what the
- 6:16:29error message is going to be the
- 6:16:31preferred option is to try get to a
- 6:16:32point where you never have to reach the
- 6:16:34catch all and try chain as many catches
- 6:16:36as you can in order to handle as many
- 6:16:39exceptions based on the code that you're
- 6:16:40writing which can be found if you hover
- 6:16:42over the function and read the
- 6:16:44exceptions once you've covered as many
- 6:16:46options as you can throughout the
- 6:16:47catches then you almost never have to
- 6:16:49worry about the code entering this
- 6:16:51section because we've covered both the
- 6:16:53exceptions in format and overflow and we
- 6:16:55can do the Overflow right now please
- 6:16:58enter a value under 2
- 6:17:00billion then that means we've actually
- 6:17:02covered all of the catches that are to
- 6:17:04do with converting an integer 32 covered
- 6:17:07format and covered overflow so when we
- 6:17:09run this code the only two things that
- 6:17:11can possibly go wrong are these two
- 6:17:12functions the format exception overflow
- 6:17:15exception and we won't ever hit the
- 6:17:17catch all in this case but if anything
- 6:17:19goes wrong at least we have a belt and
- 6:17:21Brace option to catch it and we can tell
- 6:17:23the user what happened and because it's
- 6:17:25e. message then the value is going to be
- 6:17:27quite descriptive so the user can read
- 6:17:28it without any
- 6:17:31problems so in this task we want to be
- 6:17:34able to convert a string to an INT
- 6:17:36successfully without any errors
- 6:17:37occurring and without any exceptions
- 6:17:39that could break our code so in steps we
- 6:17:41want to create an INT and try and
- 6:17:43convert any string from the console to
- 6:17:45an integer notice the error that appears
- 6:17:47when you try to do that write a try
- 6:17:49catch handle around it to successfully
- 6:17:51operate that and output the message to
- 6:17:52the screen and you won't need to change
- 6:17:55the console out print and the convert
- 6:17:57in32 to be able to make this work so you
- 6:17:59don't want to change the current code
- 6:18:01you just want to add the triun Handler
- 6:18:03around it so why is a bad situation how
- 6:18:06can we know if it's been converted this
- 6:18:08is just a question that you should keep
- 6:18:09at the back of your head and we'll
- 6:18:10answer this shortly and then finally we
- 6:18:13want to create a custom tri pass
- 6:18:14function find the real trip pass
- 6:18:16function and copy the return Titan
- 6:18:18parameters and see if you can mimic Its
- 6:18:20Behavior read the tool tip it gives you
- 6:18:22to give you ideas on what to do so let's
- 6:18:24take these in steps first we can have a
- 6:18:26console right and say enter a number and
- 6:18:29then Inn equals convert to in32 and then
- 6:18:33we need a console read line
- 6:18:36and then if you run the
- 6:18:38code if we try and just convert some
- 6:18:41square brackets then we get a format
- 6:18:44exception so that's the exception that
- 6:18:46we're going to be handling right now so
- 6:18:48we can take this code and we can wrap it
- 6:18:50around and a
- 6:18:51try use alt and up Arrow key and then
- 6:18:55just after do a catch and then we want
- 6:18:56to do a format exception and then space
- 6:18:58e to give it a variable and then we do
- 6:19:00e. message out to the screen and now
- 6:19:03let's run the code square brackets input
- 6:19:06string was not in a correct format so we
- 6:19:08get our message and our exception has
- 6:19:09been successfully handled and our
- 6:19:11program hasn't crutched but the problem
- 6:19:13is is has got to this console read line
- 6:19:15and the user hasn't had a chance to
- 6:19:17actually enter a number so what we can
- 6:19:18do in here to ensure the user always has
- 6:19:21access to writing a number is we can say
- 6:19:23something like this bu success equals
- 6:19:26false and then just after here we can
- 6:19:28say success equals the
- 6:19:30true and just down here we can say
- 6:19:33console right line so success question
- 6:19:35mark yay and then call on or
- 6:19:40no so if it converts it 15 we should get
- 6:19:43a yay and if we convert it and it fails
- 6:19:45then we should get an o no along with
- 6:19:47the
- 6:19:48message of course what you can also do
- 6:19:50is wrap this entire section inside a
- 6:19:52while loop what we've already covered
- 6:19:53that inside the tutorial and that means
- 6:19:55it'll keep repeating this until this is
- 6:19:57set to true and therefore exit the while
- 6:19:59loop and then will the users finally
- 6:20:01typed in a number so let's go back so
- 6:20:03we've noticed an error and we've brought
- 6:20:04a TR catch handle around it now we want
- 6:20:06to catch the error and output the error
- 6:20:08message with through done and then we
- 6:20:09haven't changed the current code which
- 6:20:11was these two lines we've added a booing
- 6:20:13and set it to True here just so we can
- 6:20:15have a bit of an output message before
- 6:20:17so why is this a bad situation how can
- 6:20:19we know if it's being converted so this
- 6:20:21code is a bad situation because we just
- 6:20:23have these two lines outputed and then
- 6:20:25now that we've added our booing to tell
- 6:20:27the user whether it's been successful or
- 6:20:28not is actually making it a bit more
- 6:20:30meaningful the problem is even if we
- 6:20:32print or no then the user doesn't really
- 6:20:33have a chance to enter it again so like
- 6:20:35I said before you could loop around and
- 6:20:37use a while loop to make sure that this
- 6:20:38situation can go from something that
- 6:20:41gets handled into a situation where the
- 6:20:43user can actually type it in there's no
- 6:20:44point hand on an exception and realizing
- 6:20:46there's an error if you don't give the
- 6:20:48user a chance to enter another value if
- 6:20:50they enter out 15 followed by full stop
- 6:20:53when they only meant to type 15 then you
- 6:20:55need to give them another chance to type
- 6:20:56in that 15 value otherwise the program
- 6:20:59is simply just going to end and
- 6:21:00nothing's going to happen for example 15
- 6:21:03DOT it says or no or you can have
- 6:21:05another message that says something went
- 6:21:07wrong or you can have more specific
- 6:21:09message about the conversion and then
- 6:21:11once you press enter the program simply
- 6:21:12closes this is not an interaction that
- 6:21:14you want you want the US it to be able
- 6:21:16to reselect another value so that's why
- 6:21:18it's a bad situation because you know
- 6:21:20it's being converted or not heuse now
- 6:21:22build success but the problem is we
- 6:21:23don't have any sort of looping so let's
- 6:21:26have a look so now let's comment out
- 6:21:28this and let's see how we can make our
- 6:21:29custom trip pass so you type it in. trip
- 6:21:32pass you can see they have a bu inpass
- 6:21:35string s and out in result so let's try
- 6:21:38and copy that and make our own custom
- 6:21:39function so we say static Bull and it's
- 6:21:42Tri pass and then we have a string input
- 6:21:46and out in
- 6:21:49result perfect so now we can get rid of
- 6:21:52this line just so we use it for
- 6:21:53reference and let's use it straight in
- 6:21:54here and then we can write the
- 6:21:55implementation in a second so we can say
- 6:21:57if trass the thing that we want to pass
- 6:22:00is actually a console read line and then
- 6:22:03we just say out into
- 6:22:06result and the console read line is
- 6:22:08going to be answered to this question
- 6:22:10which is enter a number you place that
- 6:22:12straight inside here and then we say out
- 6:22:13in result so if this has been successful
- 6:22:16we can print out yay again and else if
- 6:22:19it's not successful we can print out or
- 6:22:20not of course you can do other things
- 6:22:22inside here or you can even print out
- 6:22:24the value straight from here so we can
- 6:22:26say yay plus a space and then result
- 6:22:29just so we can get that back to the user
- 6:22:30and then oh know if something goes wrong
- 6:22:32of course this is just so we can test
- 6:22:33things and see how they work so this Tri
- 6:22:36pass function it will take in a string
- 6:22:38input and it'll try and give us back the
- 6:22:40result so in order to be able to make
- 6:22:42this successful function what we need to
- 6:22:44do is essentially do the conversion
- 6:22:45ourself which will be this convert in 32
- 6:22:49so first we need to assign this out int
- 6:22:51so we can say a result equals minus1 so
- 6:22:53now that we've used that we actually
- 6:22:55need to have a return type as well but
- 6:22:56let's worry about that in a second so
- 6:22:58essentially what we actually want to do
- 6:23:00is say that the result is equal to
- 6:23:03convert to in 32 and then we want to
- 6:23:05convert the input store it inside the
- 6:23:07result and if this goes successful then
- 6:23:09we want to return true and if it doesn't
- 6:23:11go successful then we want to return
- 6:23:13false but of course we need to be able
- 6:23:15to do these things separately so like we
- 6:23:17said with the try you only wrap around
- 6:23:19the section of which is going to be
- 6:23:20problematic so in this case as we've
- 6:23:22done here we've wrapped the convert in
- 6:23:2432 inside the try so we're going to need
- 6:23:27to do that down here as well so we can
- 6:23:29open the try right here and place these
- 6:23:31two lines
- 6:23:32inside and then have a catch have a
- 6:23:35little think about what sort of catch do
- 6:23:37we need the whole point in this function
- 6:23:39is to Simply convert value and return
- 6:23:41back whether it's been successful or not
- 6:23:43there is no messages here we just need
- 6:23:46has it been successful yes or no and
- 6:23:49because we only need to know if it's a
- 6:23:50yes or a no then we can just have a
- 6:23:52generic exception and we don't need the
- 6:23:54E variable because we're not going to
- 6:23:56access the message we're going to keep
- 6:23:57it as an exception and instead of
- 6:23:59returning false down here we actually
- 6:24:00want to do this straight inside the
- 6:24:02catch so now the code reads like this
- 6:24:04we set the result to minus one and the
- 6:24:06reason why we do that is because if we
- 6:24:08don't then we're going to get a compiler
- 6:24:10error and it says that not all paths
- 6:24:12will going to assign result
- 6:24:13alternatively we can also assign result
- 6:24:15straight inside here just so we have an
- 6:24:17assignment in both paths or you could
- 6:24:19just leave it outside at the top so we
- 6:24:21assign result to minus one and then we
- 6:24:23place our problematic code straight
- 6:24:24inside here we try and convert the
- 6:24:26result and if this line returns back
- 6:24:28successful then it's going to execute
- 6:24:30the second line and return back true if
- 6:24:32this line returns back an exception it's
- 6:24:34going to miss this line completely and
- 6:24:36jump straight into the exception and
- 6:24:38then return us back with false so let's
- 6:24:40actually try this and see what happens
- 6:24:4215 DOT or no and then 15 y 15 so the
- 6:24:48reason why we don't care about the
- 6:24:49exception in here we can add in
- 6:24:51different values as we've used before
- 6:24:53with format exception overflow exception
- 6:24:55but as we saw with trass it simply just
- 6:24:57returns us back with the bull the bull
- 6:24:59simply tells us if it's been successful
- 6:25:01or not so we could stack these multiple
- 6:25:04times times we can say catch and then we
- 6:25:06have the format exception and then we
- 6:25:08can place a return false in there and
- 6:25:10then we have another catch for the
- 6:25:11Overflow exception and we can place
- 6:25:13another return false in there but as you
- 6:25:15can see we're just returning false
- 6:25:16inside here so now that we've changed
- 6:25:18these it's kind of pointless each of
- 6:25:20them return back false and because we're
- 6:25:22not doing anything very specific we
- 6:25:24don't actually need to chain them all
- 6:25:25together we don't care about the message
- 6:25:28we don't care about exception we only
- 6:25:30care about the fact that something has
- 6:25:32went wrong and if something has went
- 6:25:34wrong then we return back false if
- 6:25:36everything goes okay then we return back
- 6:25:37true and that's all this Tri pass
- 6:25:39function is going to need to do it's
- 6:25:41simply convert it or it doesn't convert
- 6:25:43it using an input and outputting a
- 6:25:46result so if you run it retype in a real
- 6:25:49number we get a number back and then 15
- 6:25:52DOT Z then we get all no because it
- 6:25:54can't convert it there we go we've met
- 6:25:56all our requirements for this
- 6:25:59program let's have a look at how we can
- 6:26:01do debugging using visual studio to to
- 6:26:04step through our code manually so this
- 6:26:06is the example I used last time int age
- 6:26:08equal 35 and ideally in this case you'd
- 6:26:11want something like 30 plus to be
- 6:26:12printed instead of 18 plus even though
- 6:26:1518 plus is still technically true we'd
- 6:26:17still like 30 to be printed in this case
- 6:26:19because this is if it's over 18 do 18 if
- 6:26:22it's over 30 do 30 plus so in order to
- 6:26:25start the debugging process you
- 6:26:26shouldn't run the code first if you run
- 6:26:28the code first it's going to execute so
- 6:26:31fast that you won't get a chance to
- 6:26:32actually put any code in and and try and
- 6:26:34debug it there we go we've already hit
- 6:26:36the console read line and it's executed
- 6:26:38for a spit second so the best thing to
- 6:26:41do just to the left of all the line
- 6:26:44numbers you want to press in the Gap and
- 6:26:46that will create a red dot and it'll
- 6:26:48tell you that this is a debuging red dot
- 6:26:50just in this little tool tip debugging
- 6:26:52do program. men debuging program. men so
- 6:26:56it tells us we're in the program class
- 6:26:57and we'll going inside the main you can
- 6:26:59also address the debug menu up here and
- 6:27:01it says we can have a toggle breakpoint
- 6:27:03we can add new one we can delete them
- 6:27:05all or we can actually disable deleting
- 6:27:08them will just simply get rid of them as
- 6:27:10you can see here if I set a couple more
- 6:27:13and press disable then it's going to
- 6:27:15hide them so when I run our code it
- 6:27:17doesn't actually apply the debugging but
- 6:27:18it doesn't delete them and then we can
- 6:27:20enable them and then we can also delete
- 6:27:22them all and it gives you a little
- 6:27:24prompt because if you're doing this
- 6:27:25inside a big project and you just spent
- 6:27:27a lot of time going through and
- 6:27:28debugging certain functions then you
- 6:27:30don't want to be able to delete them all
- 6:27:32without any confirmation so to delete
- 6:27:33them all and put it back on line 13
- 6:27:35which is our int Edge so let's run the
- 6:27:38code and see what happens so as you can
- 6:27:40see it minimizes our console for us and
- 6:27:42nothing's actually been printed yet
- 6:27:43because we're only up to this line and
- 6:27:45then we can use these buttons up here
- 6:27:47step into goes inside a function step
- 6:27:51over goes over the line so it will go
- 6:27:53from this line to this line step out is
- 6:27:56used after you've stepped in if you
- 6:27:58stepped into a function and then you
- 6:28:00don't want to see the rest of the
- 6:28:01function executing you can step out to
- 6:28:03jump back out of it which I'll
- 6:28:04demonstrate later on in this video so
- 6:28:06now that we've hit this line you can
- 6:28:08actually see what's going on so we have
- 6:28:09int age equals z because when this line
- 6:28:12is highlighted it means this line hasn't
- 6:28:14executed just yet so we can press this
- 6:28:17or we can press F10 on our keyboard if I
- 6:28:20press F10 you can now see int age has
- 6:28:22the value 35 and what you can actually
- 6:28:25do is double click on it and just change
- 6:28:26the value to 30 for example now if you
- 6:28:28highlight you can see the value is 30 so
- 6:28:30you need to double click change the
- 6:28:32value to something and then simply press
- 6:28:34enter if you change the value but leave
- 6:28:36the box like this then it won't change
- 6:28:38the value so you have to remember to
- 6:28:40press enter so now I've made the age 40
- 6:28:42but let's just change it back to 35 to
- 6:28:43keep it the same and then now what we
- 6:28:45can do we can explore what's going on
- 6:28:47here if age is greater than 18 then
- 6:28:51we're going to check if this condition
- 6:28:52is true or false so in order to check if
- 6:28:54this returns back true or false you can
- 6:28:56actually hover over the operator and it
- 6:28:58says here AG is greater than 18 is equal
- 6:29:00to true and if we hover over this one it
- 6:29:03says AG is greater than 30 is equal to
- 6:29:05two notice what we actually did there we
- 6:29:08essentially ran this condition through
- 6:29:10the debugger without actually reaching
- 6:29:12this line we're only up to line 15 as of
- 6:29:14right now but we've managed to execute
- 6:29:16this and give us back a function so as
- 6:29:18you can see age greater than 30 is true
- 6:29:21and age greater than 18 is true and
- 6:29:23because this is lsif chain it only cares
- 6:29:26about the first one that turns back true
- 6:29:28and because this one has turned back
- 6:29:29true then if you press F10 again you'll
- 6:29:32see that now it's went inside this curly
- 6:29:33bra is which will print out the console
- 6:29:35right line if I press F10 again you can
- 6:29:38see we're now waiting on here if I use
- 6:29:40alt tab I can get back to the console
- 6:29:42you see that 18 plus has now been
- 6:29:44printed now that we're waiting here we
- 6:29:46actually press F10 one more time and
- 6:29:48then it jump straight to this read line
- 6:29:50of course because this is L if chain as
- 6:29:52soon as one of these become true and it
- 6:29:54jumps over the else if because of course
- 6:29:56this is a chain the first one it hits
- 6:29:59that's true it skips over the rest if we
- 6:30:01didn't want this to be a chain then we
- 6:30:03could keep them as both independent if
- 6:30:05statements if age is greater than 18 and
- 6:30:08if age is greater than 30 and don't use
- 6:30:10the lse keyword but in this case because
- 6:30:12you do want to Output a different
- 6:30:13message for every single age range you
- 6:30:15do want to place the L's word in there
- 6:30:17and now that we've hit this line we can
- 6:30:19press
- 6:30:20F10 and then now we jump back in the
- 6:30:22console and now we're waiting right here
- 6:30:25and just a little tip when it comes to
- 6:30:26debugging if at any point you just want
- 6:30:28to exit the debuging you can either
- 6:30:30press continue or just press F5 and the
- 6:30:32program will run for example if we
- 6:30:35investigate this variable and we can see
- 6:30:37the value is 35 and then now we're happy
- 6:30:40with the rest of the Coe we can just
- 6:30:42press F5 and our code will run as normal
- 6:30:45we'll get the out print and we'll get a
- 6:30:46read line waiting here so we know our
- 6:30:49problem here is both conditions return
- 6:30:51back true so just like in the last video
- 6:30:53we actually change this condition so to
- 6:30:55make sure that it only goes between this
- 6:30:57range but let's not repeat ourselves
- 6:30:59let's have a look at a different example
- 6:31:00so we can have string name equals Abu
- 6:31:03and and then int age and then age equal
- 6:31:0523 which I'll demonstrate why I've done
- 6:31:07it like this in a second and then let's
- 6:31:09have a function just so I can show you
- 6:31:11how to step in so we can have a static
- 6:31:13string print details string name in
- 6:31:17Edge and here actually we want to just
- 6:31:20return back a void because we just want
- 6:31:22to print out to the screen and we can
- 6:31:23say name space edge and maybe just put a
- 6:31:27dash between them and let's run that
- 6:31:28code now we can see
- 6:31:31aa-23 of course making that function
- 6:31:33wasn't the point I actually want to show
- 6:31:35you what's going on so if we just print
- 6:31:37this a couple of times just so we can
- 6:31:38show you the step out and step in so if
- 6:31:40you run the code now we can see our
- 6:31:42string name if we go over that you can
- 6:31:45see it's called abber and then now that
- 6:31:46it's stepped over this line cuz there's
- 6:31:48nothing really to execute here it's just
- 6:31:50creating back a variable so you can see
- 6:31:52that age is zero cuz that's the default
- 6:31:53value for an integer and when we skip
- 6:31:56over that now it's got the value of 23
- 6:31:58so one line creates the integer with the
- 6:32:00default value equal to zero and then the
- 6:32:02next line will make it equal to 23 and
- 6:32:05then we're passing on the variables here
- 6:32:06and if you wanted to investigate it you
- 6:32:08could simply just hover over them like
- 6:32:09this so you can see what the values
- 6:32:11contain and then now that we've hit this
- 6:32:13function if we press F10 then it's just
- 6:32:16going to skip over it but what we
- 6:32:17actually want to do is be able to go
- 6:32:20inside the function so this is where our
- 6:32:22step in goes step over doesn't care if
- 6:32:25it's a function or a normal line it will
- 6:32:27just step over it regardless so we want
- 6:32:29to in here we want to use step into or
- 6:32:31we can press f11 once we press F 11 you
- 6:32:34can see that now it's jumped from print
- 6:32:36details up here to print details down
- 6:32:38here and then now look if I step through
- 6:32:41it's going to actually execute these one
- 6:32:43by one so right now it hasn't executed
- 6:32:45anything so I step over this once using
- 6:32:47F10 you'll see it now appears and then
- 6:32:49the same thing with this one and this
- 6:32:51one and now that I've executed three we
- 6:32:53should see three in the console perfect
- 6:32:55so now what I want to demonstrate is
- 6:32:57let's say what happy with the rest of
- 6:32:58this function and then now we want to
- 6:33:00just step out so I press this button and
- 6:33:02you can see it jumps back back up to
- 6:33:04print details f11 to go in and then
- 6:33:07shift f11 to go out so now that we're
- 6:33:09back in here the only thing we can do
- 6:33:11really from this point is either press
- 6:33:13F5 to stop debugging or press F10 to
- 6:33:16jump to this line and at this point
- 6:33:17we've already hit the last line now code
- 6:33:19so we don't really need to use F10
- 6:33:21anymore cuz we nothing to step over so
- 6:33:23we can just press F5 and let our program
- 6:33:25finish and there we go so let's have a
- 6:33:27look another example can have a string
- 6:33:29message equals read from console then we
- 6:33:32can pass this in and
- 6:33:34anything and then we can have a console
- 6:33:36right line and we can just print the
- 6:33:37message and then let's create this
- 6:33:39function so because it's a return value
- 6:33:41and start inside a string we're going to
- 6:33:43need a static string read from console
- 6:33:47and then this is one string parameter so
- 6:33:49we have a string
- 6:33:54message and then we can just do a
- 6:33:56console
- 6:33:57right and then print the message to the
- 6:33:59screen and then we just want to return
- 6:34:01the console. readline there we go and
- 6:34:05again let's put a break point here and
- 6:34:07we can remove this one so it doesn't
- 6:34:09always have to be the first line if
- 6:34:11we're perfectly happy with this amount
- 6:34:12of code then we can simply take that off
- 6:34:15and it will stop when it gets after this
- 6:34:18line now if you look at the console
- 6:34:20you'll see all of this is executed that
- 6:34:22function is executed four times to print
- 6:34:24the name and age of the screen and now
- 6:34:25we're waiting here so we have our read
- 6:34:27from console and now we can step into
- 6:34:30using f11 and you can see that if you
- 6:34:32hover over string m message has enter
- 6:34:34anything which is our parameter that we
- 6:34:36specified up here and then there you go
- 6:34:38and then that will print into the screen
- 6:34:40so you tab back in you'll see it says
- 6:34:41enter anything and then just at the read
- 6:34:43line we need to actually enter it
- 6:34:45now
- 6:34:47something and then press enter and then
- 6:34:49just right here it's going to return it
- 6:34:51back so if we use F10 it jumps back in
- 6:34:54here and then actually we need to
- 6:34:56execute this line so F10 again and then
- 6:34:58if you hover over a message you see it
- 6:35:00says something which is exactly what we
- 6:35:02typed in the console here and then we
- 6:35:04just print it back to the screen there
- 6:35:06you go and now we just have the console
- 6:35:08read line and we can press F5 to finish
- 6:35:10up there we go so debugging is mainly
- 6:35:13used by pressing this button on the side
- 6:35:15so debugging is used by pressing on the
- 6:35:17empty Gap just to the left of the line
- 6:35:19number and it'll create a red dot once
- 6:35:21you create a red dot it'll highlight
- 6:35:23this for you once you've clicked on it
- 6:35:24it should show up a red dot you can also
- 6:35:26disable or delete all the bra points
- 6:35:28from the debug window at the top of the
- 6:35:29screen when you run the code it will
- 6:35:31stop here and then you can either use F9
- 6:35:34as a shortcut as well click into the
- 6:35:35line that you want to debug and press F9
- 6:35:37please note there is specific cases
- 6:35:39where if you try and press this one
- 6:35:41right here it's not going to let you if
- 6:35:43the line doesn't really do anything for
- 6:35:45example this is creating an integer
- 6:35:47there's no real action that's happening
- 6:35:49here that you should or want to debug
- 6:35:52this is an assignment this is an
- 6:35:53assignment this is a function call and
- 6:35:55so on and so forth and this is also a
- 6:35:57function call when it comes to just
- 6:35:59declaring variables then a compiler does
- 6:36:02not let you put in a break point because
- 6:36:04there's nothing really to investigate
- 6:36:06when you're just creating a variable so
- 6:36:08don't get thrown off or think
- 6:36:09something's wrong if you click on the
- 6:36:10empty line and nothing appears so that's
- 6:36:12it for debugging but let's just have a
- 6:36:14little recap we can use F10 to step over
- 6:36:18f11 to step into functions shift f11 to
- 6:36:21step out or you can use the buttons that
- 6:36:23appear at the top of the screen when you
- 6:36:25run the code just right
- 6:36:28here hover over variables and you get
- 6:36:30their values as I showed you in the if
- 6:36:32statements if you hover over the
- 6:36:34operator inside the if statement it will
- 6:36:36actually tell you what the condition is
- 6:36:38and whether it's true or false so you
- 6:36:39say if 1 equals equals to one and let's
- 6:36:42just print out true to the screen just
- 6:36:44so I can demonstrate
- 6:36:46this if I hover over the equals equals
- 6:36:48it says 1 equals equals 1 is
- 6:36:53true so that's how you check if a
- 6:36:56condition is going to be true or false
- 6:36:57when you're
- 6:36:58debugging you can check the parameter
- 6:37:00values by simply hovering over it and
- 6:37:02the tool tip should include the
- 6:37:06value just like so and if you wanted to
- 6:37:08step into this function you can use step
- 6:37:10into and then use step over which is F10
- 6:37:14just here and then you can actually
- 6:37:16investigate these values as well or you
- 6:37:18can look at the parameters and once
- 6:37:19you're done with the function you can do
- 6:37:20shift f11 or you can use step out and
- 6:37:24then you can skip over this and the same
- 6:37:26thing with this you can have a look at
- 6:37:27the string variable once it gets
- 6:37:29returned something in here and now it
- 6:37:32says something thing in here and string
- 6:37:34message and then we can print out to the
- 6:37:36screen Etc so you can have a little bit
- 6:37:38of Freedom with what you can do in terms
- 6:37:40of debugging play around to debuging in
- 6:37:42the past exercises that we've made
- 6:37:44together and then what you can actually
- 6:37:45do is gain experience in debugging so it
- 6:37:48comes to more complicated projects you
- 6:37:49can also apply that learning there and
- 6:37:51make sure that your code is error
- 6:37:55free so we covered how to debug by
- 6:37:58pressing the white space just to the
- 6:38:00left of the line number if we run the
- 6:38:02code
- 6:38:04the program will stop where our
- 6:38:05breakpoint is and it won't execute the
- 6:38:07line that we're up to we have to step
- 6:38:09over it so we've actually used this
- 6:38:10before to analyze our functions and see
- 6:38:13what values are inside things so we have
- 6:38:15these extra windows called the local and
- 6:38:17the auto so let's take a look at them
- 6:38:19and see what they're about so again if
- 6:38:21you don't have this layout you can use
- 6:38:23window reset layout and it should be on
- 6:38:25the left panel not the right one this
- 6:38:27time so we've got watch which we'll
- 6:38:29explore later and we're going to have
- 6:38:31locals and autos so the autos because
- 6:38:34we're up to the age then it shows us the
- 6:38:37age has value zero and then that
- 6:38:39prevents us from hovering over it and if
- 6:38:41we step over it you'll see that now it's
- 6:38:43got age 23 and now name has appeared the
- 6:38:46auto tab will try and figure out what
- 6:38:48functions of what parameters you need to
- 6:38:51be displayed down here based on the
- 6:38:53section of Corde that you're currently
- 6:38:54up to so my guess is because we're up to
- 6:38:56this function and it takes in a name and
- 6:38:59age then we're getting name and age
- 6:39:00appearing before when we were on this
- 6:39:02line it was only showing back the age
- 6:39:04and because that section only used the
- 6:39:06age we only got age here but if you
- 6:39:09press locals you can see all the
- 6:39:10variables that are local to this main
- 6:39:12function are actually showing up we have
- 6:39:14a parameter called ARs and technically
- 6:39:16this is a parameter and it's a string
- 6:39:17array type but it's currently unused but
- 6:39:20it's still a parameter so you can see it
- 6:39:22down here and then in order we have the
- 6:39:24name variable down here then we've got
- 6:39:26the age and then we've got the message
- 6:39:28which is currently said to know because
- 6:39:30we haven't created this in memory yet
- 6:39:31but as you can see this changes depend
- 6:39:34on the scope of our variables so now
- 6:39:36that we're about to hit this function
- 6:39:37let's use the step into and you'll see
- 6:39:40that the locals have now changed we've
- 6:39:42got rid of name age argument and string
- 6:39:44message and it's replaced with name and
- 6:39:46age because remember what I was saying
- 6:39:48about the scope of the variables
- 6:39:49parameters inside a function are always
- 6:39:51going to be local to that function
- 6:39:53around the curly braces that they're
- 6:39:54surrounded by name and age is only
- 6:39:57within here so when it's trying to shows
- 6:39:59the local variables it's only local to
- 6:40:01what's inside these curly braces which
- 6:40:03in this case is name and age so now if
- 6:40:06we step over
- 6:40:07this and then if we step into this read
- 6:40:10from console you'll see that just before
- 6:40:12we step into this function that these
- 6:40:13have all came back cuz again the scope
- 6:40:15has changed back to this function which
- 6:40:18is our main function which has all these
- 6:40:20variables inside so our read from
- 6:40:22console will only have our string
- 6:40:24message so hopefully when we press f11
- 6:40:26or press this step into button at the
- 6:40:28top we should only see the message
- 6:40:30variable inside here there we go we get
- 6:40:33into anything cuz that's what was passed
- 6:40:34in over here and then as soon as we
- 6:40:37return back to
- 6:40:40that let we type something in and then
- 6:40:43we get all of the values again and you
- 6:40:45can see that system. console. rine had
- 6:40:47returned hello which is this line and
- 6:40:50then if we press F10 again you'll see
- 6:40:51that even though we've got it back here
- 6:40:53our message is about to be assigned once
- 6:40:55we step over this there you go it says
- 6:40:57hello and as you can see this changes in
- 6:41:00real time so as soon as we step over
- 6:41:02lines hello got play straight into there
- 6:41:04this helps us tremendously because we
- 6:41:06don't have to keep hovering over
- 6:41:07variables like this to keep checking
- 6:41:09over their values you can simply use
- 6:41:11this menu and it will always update
- 6:41:13because whenever you change Scopes
- 6:41:15between functions that will
- 6:41:16automatically update as you just saw in
- 6:41:18them examples there we go and then when
- 6:41:20the program ends obviously this window
- 6:41:22is going to close down so that's a very
- 6:41:23helpful method of being able to debug
- 6:41:25without having to hover over anything in
- 6:41:27the next video we're going to cover how
- 6:41:29to watch variables and this can be very
- 6:41:31handy when you're looking for specific
- 6:41:32areas
- 6:41:37another really good debuging technique
- 6:41:38is the watch window let's press a break
- 6:41:40point just of left of line 13 and let's
- 6:41:43run the code and let's see what this is
- 6:41:44about so we covered that we have the
- 6:41:47locals window and we have the auto
- 6:41:49window that will automatically give us
- 6:41:50the variables that we're currently in
- 6:41:52the scope of but let's forget about that
- 6:41:54right now let's have a look at the watch
- 6:41:55window it says here add an item to watch
- 6:41:59so there's a couple ways that we can do
- 6:42:00this you can actually double click on a
- 6:42:02variable and write click or you can just
- 6:42:04highlight it like this and then right
- 6:42:06click and it says here add watch and you
- 6:42:08can press it it says name value null
- 6:42:10because we haven't created it yet so the
- 6:42:12default value is null as of right now
- 6:42:14cuz it's not actually assigned anything
- 6:42:16so if you press F10 and step over it
- 6:42:18you'll see that immediately changes to
- 6:42:20abber without was having to do anything
- 6:42:22except simply press F10 which is the
- 6:42:24shortcut for our step over button right
- 6:42:26at the top of the screen this prevents
- 6:42:28us from having to hold over the
- 6:42:29variables to get the little tool tip so
- 6:42:31we can see the value the of this is we
- 6:42:33can watch variables and we don't have to
- 6:42:35worry about showing in the locals or the
- 6:42:37auto because it's always there and if
- 6:42:39you have lots of variables like this
- 6:42:41then you're only concerned about the one
- 6:42:43that you're watching locals and aluto
- 6:42:45will always change depending on the
- 6:42:46scope that you currently at whereas
- 6:42:48watch will only do what you want it to
- 6:42:50do so because we don't put age inside
- 6:42:53there then it simply won't come up just
- 6:42:54because it likes it if we check the
- 6:42:56locals we have agent there and the Autos
- 6:42:59we have agent there as well but we don't
- 6:43:00have it in the watch so what else you
- 6:43:02can you do is actually just type
- 6:43:04straight into here age
- 6:43:0723 if you really wanted to you can
- 6:43:09actually also type in conditions age
- 6:43:11equals equals 23 it returns back true so
- 6:43:14if you had an if statement that you
- 6:43:15wanted to track then you could do that
- 6:43:17as well so let's have a
- 6:43:20look so had this statement that said if
- 6:43:23age is greater than 18 then we can just
- 6:43:25print out 18 plus to the screen so let's
- 6:43:27run the code now and see what happens so
- 6:43:29as you know we have this condition right
- 6:43:31here so if you actually highlight this
- 6:43:32and and drop it you can see that the
- 6:43:34cursor changes and you can drop it
- 6:43:36inside here let's click on the age equal
- 6:43:38equal 23 and click delete so of course
- 6:43:41we can't really render this function
- 6:43:43right now because we haven't even
- 6:43:44created the integer variable so let's
- 6:43:46run over the integer variable and as you
- 6:43:48can see here it says AG is zero which is
- 6:43:51the default value for integer and that
- 6:43:53is indeed zero right now and then AG is
- 6:43:55greater than 18 is false but as soon as
- 6:43:58I step over this line and it makes age
- 6:44:00the value of 23 you'll see that these
- 6:44:02two immediately change you know they
- 6:44:03change because the values have went red
- 6:44:06red means that the values have just
- 6:44:07changed in the last button that you
- 6:44:08pressed whether that means you stepped
- 6:44:10into over or stepped out it doesn't
- 6:44:12matter red always dictates that the
- 6:44:14value is just changed so now it's
- 6:44:16telling us that age is greater than 18
- 6:44:18is returned back true so when I step
- 6:44:20over this line because this is returned
- 6:44:22back true I should render 18 plus let's
- 6:44:25have a look F10 F10 and there we go
- 6:44:28we've reached the middle which means
- 6:44:29that's going to get printed out
- 6:44:31now so so moving on to another example
- 6:44:34we can have print details if we go into
- 6:44:36this function and let's say we're only
- 6:44:38concerned about the name then we can
- 6:44:39print out the name down here but as you
- 6:44:41can see we already have it twice so
- 6:44:43that's just going to update depending on
- 6:44:45the parameter name and even if you
- 6:44:46wanted to show something like this you
- 6:44:48can drag this down here and it actually
- 6:44:50shows you the output this is very handy
- 6:44:52so you can see the output because
- 6:44:54sometimes when you have it like this it
- 6:44:55doesn't look very clear you can also
- 6:44:58hover over it right there and you can
- 6:44:59see the value placed here but again it's
- 6:45:02very handy to have it here so you don't
- 6:45:04have to keep hovering over the tool tape
- 6:45:06just to see the values and from here you
- 6:45:08can actually change the values so we can
- 6:45:10just say Harry is now the value for the
- 6:45:12name and Harry is going to be age 35 so
- 6:45:16when we press F5 and let the program run
- 6:45:18naturally you'll see that Harry is age
- 6:45:2035 now and that's all done through the
- 6:45:22watch list down here and it doesn't even
- 6:45:24matter because we have full control over
- 6:45:27the watch list variables and we can
- 6:45:28change their contents to see how our
- 6:45:30code acts in different situations so
- 6:45:33please use this watch list as much as
- 6:45:34you can and don't forget that once you
- 6:45:36can double click on variables and add it
- 6:45:38to the watch just from here you can also
- 6:45:41say quick watch and what that'll let you
- 6:45:43do is it'll give you the value and then
- 6:45:45if you want to add it you can press add
- 6:45:47watch here or you can just keep pressing
- 6:45:49re-evaluate once you've skipped over
- 6:45:51some lines so let's say we skip over it
- 6:45:53now and then go back onto it and because
- 6:45:56it has a shortcut shift F9 we can just
- 6:45:58quickly press it like this and it says
- 6:46:00Alber inside here and we can change the
- 6:46:02expression
- 6:46:03maybe do something like age and then
- 6:46:05click re-eval it and it changes the
- 6:46:07value to zero and then we can even
- 6:46:09change this to like 50 for example as
- 6:46:11you can see down here it's changed to 50
- 6:46:13as well so you can drag and drop it into
- 6:46:16here or you can double click and press
- 6:46:18add watch it's up to you please make use
- 6:46:20of this watch variable and don't rely
- 6:46:22too much on the autos and the local just
- 6:46:24so you can keep your own variables
- 6:46:25inside here and you can have complicate
- 6:46:27outputs like these you can check
- 6:46:29conditions you can check console outputs
- 6:46:31as well as just checking normal
- 6:46:32variables so using the watch window you
- 6:46:35have lots of different opportunities to
- 6:46:37keep track of your
- 6:46:40code so we've got another debugging task
- 6:46:42let's try and run the code and see what
- 6:46:45happens we're going to get printed over
- 6:46:4718 so our value is agal 35 and we're
- 6:46:50already getting over 18 printed to the
- 6:46:52screen so if we place a Brak line inside
- 6:46:55here and actually run the code we can
- 6:46:56see what's going on we can add them
- 6:46:58inside the watch just like this we can
- 6:47:01add in all the conditions inside
- 6:47:04just like
- 6:47:05this perfect so we can actually see here
- 6:47:08that because our age is over 35 it's
- 6:47:11saying greater than 18 is true greater
- 6:47:13than 30 is true but greater than 50
- 6:47:15isn't true and we can easily see that
- 6:47:17except because this is an if and an LSI
- 6:47:19chain as soon as the first one is true
- 6:47:21it's going to enter that one you press
- 6:47:23F10 then we get over 18 and then as soon
- 6:47:26as it finishes that path it actually
- 6:47:28skips all the way to the end the problem
- 6:47:30with this is we only want it to print
- 6:47:32over 30 since it's over 30 so we have to
- 6:47:35introduce ranges inside here and we can
- 6:47:37say age is less than 30 and then you can
- 6:47:40have AG is greater than 30 here and then
- 6:47:42you can also insert an and and say age
- 6:47:44is less than 50 and there we go and now
- 6:47:47it says over
- 6:47:4930 and if we change this to 55 then we
- 6:47:52should get over 50 and then finally if
- 6:47:54we change this to something like 23 then
- 6:47:57we should also get over 18 perfect so
- 6:48:00what was going on here is because we had
- 6:48:02if El if chain then you're only going to
- 6:48:04get one of them printed and because a
- 6:48:06value over 18 satisfied both the first
- 6:48:09path and the second path then it's only
- 6:48:11going to print the first path if we
- 6:48:13actually undo our changes all the way
- 6:48:15till the start and then we actually flip
- 6:48:17the order of this if we put our this one
- 6:48:19first and delete the else and change it
- 6:48:22to lse if inside here so now we have a
- 6:48:24new chain except the first one in the
- 6:48:26chain is actually if age is greater than
- 6:48:2830 now if you're on the code you'll see
- 6:48:31that the first one actually says over 30
- 6:48:3230 and it says over 30 and it doesn't
- 6:48:35say over 18 anymore but simply because
- 6:48:37this is the first one in the list as
- 6:48:39soon as it hits the first one as I'll
- 6:48:41demonstrate now it will go inside this
- 6:48:43bracket hit this curly brace and then
- 6:48:45jump straight to the
- 6:48:46end you should always order them from
- 6:48:49smallest to biggest because that will
- 6:48:51probably make the most logical sense
- 6:48:53when you're reviewing your code and in
- 6:48:55doing so you can actually have a more
- 6:48:57formatted code but you need to watch out
- 6:48:59that the ranges need to make sense if
- 6:49:01you have something like this then you
- 6:49:03need to restrict this one because it's
- 6:49:04going to be blocking the second one and
- 6:49:06you don't want to make these all ifs
- 6:49:08because then you get multiple outputs
- 6:49:10now we should get 18 and 30 being
- 6:49:12printed to the screen this is not
- 6:49:14something you want so make sure that you
- 6:49:16place your ranges inside here by adding
- 6:49:18an and and then placing the other end of
- 6:49:20it which will be 30 and then this one
- 6:49:22will continue from it so just to sum up
- 6:49:24what you need to do is make sure that
- 6:49:25you use l ifs and make sure that you
- 6:49:28have ranges just so when one exits it's
- 6:49:31going to check the other one and the
- 6:49:32other one you don't want the first
- 6:49:34condition to beat the second one if
- 6:49:36that's for the right path age 35 goes
- 6:49:39into this path or at least we want it to
- 6:49:41but it's been restricted by the fact
- 6:49:43that age is greater than 18 when we had
- 6:49:45it like
- 6:49:46this this is restricting everything over
- 6:49:4918 to go inside this path if this was
- 6:49:51even like 65 we should only fall into
- 6:49:54this one and we run the code then we're
- 6:49:56still going to get over 18 which is
- 6:49:58still technically true it is still over
- 6:50:0018 but we have different outputs for
- 6:50:02this sort of age range make sure that
- 6:50:03you use El ifs and you keep in an
- 6:50:05appropriate range and keep them in the
- 6:50:07order of the if statements going from
- 6:50:09lowest to highest or whatever makes
- 6:50:11sense in that situation that you're
- 6:50:12using
- 6:50:13it this is a start of objectoriented
- 6:50:16programming let's start with a concept
- 6:50:17called structures structures allow us to
- 6:50:20store different data types inside one
- 6:50:22data type so it's almost like a group of
- 6:50:25data types we can demonstrate this if
- 6:50:26you wanted to stall something like a
- 6:50:28person you would normally have something
- 6:50:29like a string name equals ABA ear it in
- 6:50:32age equal 23 so in here we have a string
- 6:50:35name that represents the name and inage
- 6:50:38which represents their age and you can
- 6:50:39see that because these are two different
- 6:50:41data types you can't wrap them around
- 6:50:43inside an array you can't have a string
- 6:50:45array called person and then say we want
- 6:50:47to have two different values although
- 6:50:49this will still compile we can't store
- 6:50:51both these data types inside our array
- 6:50:53unless you make the age and store it as
- 6:50:55a string which will be 23 inside the
- 6:50:58quotation marks and then you'll have to
- 6:51:00worry about converting it later on in
- 6:51:01your program so this is not really ideal
- 6:51:04so we have something called a structure
- 6:51:06so just outside here we can say struct
- 6:51:09which stands for structure and then we
- 6:51:11can have our name straight after so we
- 6:51:13can say person and the next time we just
- 6:51:15open the curly braces and let's see what
- 6:51:16we've just created so now in order to
- 6:51:18make an instance of this we can actually
- 6:51:20use that like a data type so we can say
- 6:51:22person person there we go so now we've
- 6:51:24created our person variable now we see
- 6:51:26we've got our own data type before we
- 6:51:28were just using string and ins and
- 6:51:30arrays and things like that but now we
- 6:51:32actually have a new person so because
- 6:51:34our person is made up of a name and an
- 6:51:36age let's place that in here string name
- 6:51:39and then int age of course you don't
- 6:51:41want to give them default values because
- 6:51:43like all variables you don't know what
- 6:51:45the value is going to be to begin with
- 6:51:47so now what we can do person Dot and we
- 6:51:50don't actually have access to using
- 6:51:52these values yet so what we're going to
- 6:51:53have to do there is a concept called
- 6:51:55access modifiers which we'll cover on
- 6:51:57later on in this course but just for now
- 6:52:00what this means is you either have
- 6:52:01private or public public and they're the
- 6:52:03main two words there's other words that
- 6:52:04you can have but they're the main two
- 6:52:06for now if you have it private it means
- 6:52:08you can't access it down here through
- 6:52:10person. name and but as soon as you make
- 6:52:12this
- 6:52:13public then now it becomes
- 6:52:17accessible there we go we can make that
- 6:52:19equal to something and if we take off
- 6:52:23public then you see we don't have access
- 6:52:25to it it's inaccessible due to its
- 6:52:28protection level because the protection
- 6:52:29level is private which means that you
- 6:52:31can only access name Within These two
- 6:52:33curly braces the second you make it
- 6:52:35public it means that anyone outside of
- 6:52:37the structure EG down here can also have
- 6:52:39access to name so if we do that to our
- 6:52:41public on in age and string name then we
- 6:52:44actually can access both now we can say
- 6:52:46person. Ag and it comes up like this the
- 6:52:49purple cube is a function and the blue
- 6:52:51is Fields you can actually fil them out
- 6:52:53by pressing them down here show only
- 6:52:55methods and show only Fields so we can
- 6:52:58do age equals 23 and we can actually
- 6:53:00just place abber inside here
- 6:53:03and now that this data is essentially
- 6:53:04the same as this the benefit that we
- 6:53:06have now is when we print to the screen
- 6:53:08we can say person. name and then hyphen
- 6:53:11person. age and if you run that
- 6:53:15now we get Abid
- 6:53:1723 if you actually copy and paste is
- 6:53:20lined out and just remove person dot
- 6:53:22just so we're using these two variables
- 6:53:24at the top of here we in the code you
- 6:53:26should see exactly the same values
- 6:53:27coming up there we go but now what's
- 6:53:30really handy about this situation is we
- 6:53:32have two data types that are wrapped
- 6:53:34inside of one data type instead of
- 6:53:36having a name and age we have a person
- 6:53:39because what gets really easy now is if
- 6:53:40you wanted to add something else inside
- 6:53:42here like for example birth month which
- 6:53:45for me is five so in here you'd have to
- 6:53:47make another one birth month equals 5
- 6:53:50and then all we have to do inside our
- 6:53:52person is just say person. birth month
- 6:53:55and we can just assign it right here
- 6:53:56instead of having to create a whole new
- 6:53:58variable we add it up here and then we
- 6:54:00can use it down here and then we can add
- 6:54:01that into here person. birthmonth
- 6:54:06make add this to this one and then get
- 6:54:08rid of birth month and as you can see
- 6:54:10it's exactly the
- 6:54:12same so we've taken these data types and
- 6:54:14placed it inside our structure so then
- 6:54:16we can just reference it by the person
- 6:54:19this becomes very very handy for this
- 6:54:21exact reason that I'm going to show you
- 6:54:22now let's say you wanted to read this
- 6:54:24data in from the user so you have static
- 6:54:27and then you're a bit stuck on what data
- 6:54:28type you want to return if you forget
- 6:54:30about the birth month then we have a
- 6:54:32name and age is a very basic person but
- 6:54:34our return type only lets us have one
- 6:54:36type so that's going to be a problem now
- 6:54:38so you could do a string return that
- 6:54:40will give us back the name and then we
- 6:54:42can say return person and inside the
- 6:54:45brackets what you can actually do is do
- 6:54:47out in Edge so you can have two output
- 6:54:49variables an out age and then a return
- 6:54:52string so inside here we could
- 6:54:55say enter your name and then string name
- 6:54:59equals console. readline
- 6:55:02if we copy and paste this down and then
- 6:55:04we can say enter your age and then we
- 6:55:06can say int age equals convert to in 32
- 6:55:10and then put the console read line
- 6:55:12inside there there we go now that we've
- 6:55:14got that we don't actually need in age
- 6:55:16because we've already got output
- 6:55:17parameter so we just need to be age and
- 6:55:20then after we've retrieved them two
- 6:55:21values we can return name there we go so
- 6:55:25let's actually try that up here so we
- 6:55:28can say new name is equal to return
- 6:55:32person and then out into new age and you
- 6:55:35can take this exact line paste it down
- 6:55:38here get rid of the birth month and then
- 6:55:40we can use new age in here and new name
- 6:55:42inside here so let's run that now ask us
- 6:55:46what is your name Abba what is your age
- 6:55:4823 and then we got Abid 23 which is the
- 6:55:50same as what we've got before but you
- 6:55:52see that this situation is pretty messy
- 6:55:54we have to return a string variable and
- 6:55:56also use an output in order to be able
- 6:55:58to give back two values alternatively
- 6:56:00what we could do is actually just turn
- 6:56:02back void and then we can say string
- 6:56:04name and then inage but we're going to
- 6:56:07have to use references inside here and
- 6:56:10then have string new name equals nothing
- 6:56:13and then new inage equals zero so we
- 6:56:17have string new name equals nothing and
- 6:56:19then into New Age also equal to nothing
- 6:56:21and then inside our function instead of
- 6:56:23having out parameters we can actually
- 6:56:25use a reference and we can say new name
- 6:56:27and then ref New Age and what that will
- 6:56:30do is we can just return it like this
- 6:56:32because it's void instead of declaring a
- 6:56:34new string name we can actually just
- 6:56:35assign it to the reference that just
- 6:56:37passed and because it's a void parameter
- 6:56:39now and for the return type we don't
- 6:56:40actually need to return anything so we
- 6:56:42can actually assign these by reference
- 6:56:44enter your name abber and 23 so as you
- 6:56:47can see it's still works fine but let's
- 6:56:49say we wanted to add another birth month
- 6:56:51so we' have to take the birth month
- 6:56:52variable new birth month make it equal
- 6:56:55to zero and then add yet another
- 6:56:58parameter ref new birth month and then
- 6:57:02we need a new birth month inside
- 6:57:04here ref int birth month and then we
- 6:57:09need another one from here enter your
- 6:57:12birth
- 6:57:12month and then we'd store it inside
- 6:57:15birth month and then finally we can also
- 6:57:18return it up
- 6:57:20here as new birth
- 6:57:22month Here We Go abber 23 and then five
- 6:57:26so we get them values being returned so
- 6:57:28in order to keep adding different
- 6:57:29properties to your code you have to make
- 6:57:30a new parameter and reference variable
- 6:57:33and then append the parameters inside
- 6:57:35the function now the problem that is
- 6:57:37even though you can have an infinite
- 6:57:38amount of parameters then this gets very
- 6:57:40tedious very quickly and you don't want
- 6:57:42this so let's have a look at how we can
- 6:57:44make this function exactly the same way
- 6:57:46just using our new structure you can see
- 6:57:50that instead of having to pass all of
- 6:57:51these different things in or return back
- 6:57:53a certain value we can actually just
- 6:57:55return back a complete person so all of
- 6:57:58this can be squished pretty quietly so
- 6:58:01actually let's just comment all of this
- 6:58:04out and let's comment out this function
- 6:58:06and let's have another
- 6:58:08rewrite so now that everything's gone we
- 6:58:10only want to be returning and dealing
- 6:58:12with the person so we can say static and
- 6:58:15instead of void we actually want to say
- 6:58:17we want to give the user back a person
- 6:58:19return person and we don't need any
- 6:58:21parameters in this case this is only
- 6:58:24passed in because it's a reference so we
- 6:58:26can assign these values back to the
- 6:58:28original variables but because we're
- 6:58:30going to be returning a person then we
- 6:58:31don't need to do that so we can
- 6:58:33essentially take this code and put it in
- 6:58:35here and we can say string name now we
- 6:58:38have an INT here and then also an in
- 6:58:40here there we go so now that we've
- 6:58:43constructed all our variables that we
- 6:58:45need for the person what we can do is we
- 6:58:47can make a new person person person and
- 6:58:50then person. name equals name and then
- 6:58:53duplicate this down we say person. AG is
- 6:58:56equal to age and finally the birth month
- 6:58:58is equal to the birth
- 6:59:00month and then now that we've
- 6:59:02constructed our person we still got a
- 6:59:04red underline because we haven't
- 6:59:05returned anything yet and after all of
- 6:59:07that we can say return
- 6:59:09person there we
- 6:59:11go and now simply in here we could just
- 6:59:14say person person equals return person
- 6:59:19there we go and then we can take this
- 6:59:22console right line here and place it
- 6:59:24straight underneath since it uses the
- 6:59:25same properties now let's try and run it
- 6:59:28h
- 6:59:30235 there you go so now instead of
- 6:59:32having to make a whole new variable all
- 6:59:35you have to do is simply just add
- 6:59:36another one down here so we can just say
- 6:59:38number this could be your favorite
- 6:59:40number or anything and we can add it
- 6:59:42onto
- 6:59:45here person.
- 6:59:47number and then here we can paste this
- 6:59:50down we can say enter your number and
- 6:59:53then it'll be number in here we can
- 6:59:56paste it down here and change this to
- 6:59:58number and there we go that's all we
- 7:00:00need to change and this C doesn't change
- 7:00:02cuz it's simply calling back the
- 7:00:04function and we're doing all the hard
- 7:00:05work inside here so now if you run this
- 7:00:08enter your name AB 23 5 and finally four
- 7:00:13and there you go we get the values being
- 7:00:16returned so this looks a little bit
- 7:00:18tedious we have to assign each one
- 7:00:20individually every single time we do it
- 7:00:22so we can actually use something called
- 7:00:24a Constructor in order to achieve this
- 7:00:26result we make the person we set them
- 7:00:28and then we return it it'd be easier if
- 7:00:30we could have the Constructor that we
- 7:00:32can assign straight inside here and then
- 7:00:34just return it immediately instead of
- 7:00:36having to do all of these lines and then
- 7:00:37reference each one individually so let's
- 7:00:40have a look and see what we can do
- 7:00:42inside here we can have a new function
- 7:00:44that'll just be called whatever the
- 7:00:46structure is called then you open the
- 7:00:47brackets close the brackets for now and
- 7:00:49this is called a Constructor in order to
- 7:00:51be able to use it down here we need to
- 7:00:53make it public in order for someone else
- 7:00:55to access it so this says that it needs
- 7:00:57explicit parameters in order to be able
- 7:01:00to compile it says that it needs to
- 7:01:02assign name age birth month and number
- 7:01:04so let's see how we can do that as we've
- 7:01:06used Constructors in the past for
- 7:01:09example when we had things like the
- 7:01:11string Builder we had s SP equals new
- 7:01:13string Builder and when you make a new
- 7:01:15something that means you make a new
- 7:01:17Constructor so all of these sixes are
- 7:01:20the parameters that go inside of this
- 7:01:22but each one of these is a separate
- 7:01:23Constructor if we control click this we
- 7:01:25should be able to see them all there we
- 7:01:27go you see that the class is called
- 7:01:29string Builder and this is called string
- 7:01:31Builder and these two names match
- 7:01:33exactly so to make a string Builder you
- 7:01:35can give it any one of these as long as
- 7:01:37your parameters match one of these six
- 7:01:40you can give it nothing or you can give
- 7:01:42it some other values and that's the same
- 7:01:44is true with our current
- 7:01:46person so what we need to do in this
- 7:01:48case is we don't have multiple ways that
- 7:01:51we can construct this in the case of our
- 7:01:53person we have to provide all of these
- 7:01:55details otherwise the compiler is not
- 7:01:57going to like it so we can have a string
- 7:01:59name int age int birth mon
- 7:02:02and in number now we got our parameters
- 7:02:05set up we actually need to make use of
- 7:02:06them in
- 7:02:07here so because we have name and name
- 7:02:11and they're exactly the same string by
- 7:02:13string then here we can't say name
- 7:02:15equals name in terms of global and local
- 7:02:18variables this is global within the
- 7:02:20structure as it's within these two Co
- 7:02:22braces and all of these variables are
- 7:02:24only Within These two curly braces local
- 7:02:27variables always take presidence over
- 7:02:29the main one if I double click on this
- 7:02:31you'll see that all three are
- 7:02:33referencing this Constructor if I double
- 7:02:35click on this you'll see that both name
- 7:02:37references on line 20 down here is all
- 7:02:40referencing this one in the parameter
- 7:02:42and not the one up top we want it to be
- 7:02:45assigned into here so you have two
- 7:02:46options in this case you could
- 7:02:48capitalize all of these so then you
- 7:02:50don't run into this issue and then
- 7:02:52instead of name equals name it's capital
- 7:02:54N equals name and that fixes it
- 7:02:56alternatively if you want it to be a
- 7:02:58lowercase then what you can simply do is
- 7:03:00you can place this Dot
- 7:03:03if you use this it goes back to the
- 7:03:04current scope and the current scope of
- 7:03:06name is going to be this which is within
- 7:03:09the
- 7:03:10structure when you use this it doesn't
- 7:03:12reference the local variables it
- 7:03:14references the global variables up here
- 7:03:16so when I double click on this now
- 7:03:17you'll see that that gets highlighted so
- 7:03:19we can say this. age equals age this.
- 7:03:22birth month equals birth month and
- 7:03:24finally this do number equals number so
- 7:03:27we're just getting all the parameters
- 7:03:29from here and putting them into these
- 7:03:31values this might look a little bit
- 7:03:33redundant but I'll show you why this is
- 7:03:34a lot
- 7:03:35cleaner so back in our return person
- 7:03:37function we have the variable and then
- 7:03:40we assign them all manually but look how
- 7:03:42easy this now has became if I comment
- 7:03:44out this return we can say a return
- 7:03:46person so now we can reference person
- 7:03:48with a capital P this time cuz I want to
- 7:03:50reference the structure directly so
- 7:03:52after the person we can open the
- 7:03:53brackets and then start to type in so
- 7:03:56our first one is name age and the birth
- 7:03:57month name comma age comma birth month
- 7:04:01comma and then number comma and then
- 7:04:02this is not going to like it because we
- 7:04:04also need to place new
- 7:04:09inside as I said before when you making
- 7:04:11a string Builder you have a new keyword
- 7:04:14and this new means that you want to
- 7:04:15create a new instance of this you could
- 7:04:18think of this structure as a template
- 7:04:20this is the template for something and
- 7:04:22until you start using that something
- 7:04:24it's actually not going to do anything
- 7:04:26so we have to make a new instance of it
- 7:04:28because it's not already available so we
- 7:04:30need to make it available through our
- 7:04:31data type so when we say we want a new
- 7:04:33person that gives us access to the
- 7:04:35Constructor if I remove that you'll see
- 7:04:37we've got one of two now we can create
- 7:04:39an empty person and that will just make
- 7:04:41us assign all the values individually
- 7:04:43but we don't want that we want to use
- 7:04:44the second path and if you drop the
- 7:04:46arrow you'll be able to see the
- 7:04:47different parameters which is the ones
- 7:04:49we just typed in name age birth month
- 7:04:52and then number and now that we've
- 7:04:54returned back this we actually don't
- 7:04:55even need to do any of these lines right
- 7:04:58here we can actually just return it
- 7:05:00exactly like this so now if you it again
- 7:05:02you should see exactly the same output
- 7:05:0323 5 and four perfect so let's just have
- 7:05:07a little recap so normally when we're
- 7:05:09creating variables we use common
- 7:05:11variables that we've used in the past
- 7:05:13string in double Flo bu Etc but with the
- 7:05:16use of structures we can create our own
- 7:05:19group of variables and then we can use
- 7:05:21them variables inside functions and
- 7:05:23inside our code to make things a lot
- 7:05:25cleaner and a lot easier so inside
- 7:05:27person we actually store four different
- 7:05:29variables and they can be an infin
- 7:05:31amount of variables should you will and
- 7:05:33you can easily remove them and then
- 7:05:35remove them from the parameters and from
- 7:05:37the assignment and then that's it if we
- 7:05:39use this without our structure then
- 7:05:40we're going to have to declare every
- 7:05:42single variable separately and I know
- 7:05:44this is the same as doing it up here
- 7:05:46it's actually a little bit more work cuz
- 7:05:47you need to create a new parameter and
- 7:05:49create all the the instances inside here
- 7:05:51and assign the
- 7:05:52values now the benefit of doing it
- 7:05:54within a structure is that when you go
- 7:05:56to do a function look how easy this line
- 7:05:59is before you'd have to have name a age
- 7:06:01birth month all as references or have
- 7:06:04them as out parameters and it gets a
- 7:06:05little bit long and a bit sloppy in
- 7:06:07order to include the month in here we'd
- 7:06:09have to have another in number and you
- 7:06:12can see that the parameters are growing
- 7:06:13if you wanted to store a user from a
- 7:06:15registration form you might have name
- 7:06:17address phone number country and the
- 7:06:19list goes on it can be a very long form
- 7:06:21and you therefore you'll have a lot of
- 7:06:23parameters so it's very easy to wrap it
- 7:06:25around inside a structure and then
- 7:06:27simply reference it like that and you'll
- 7:06:28need zero parameters because everything
- 7:06:31is contained within the data type and
- 7:06:33then inside here we can ask for the
- 7:06:34users's details individually store them
- 7:06:36as their integers and string variables
- 7:06:39and then instead of using this notation
- 7:06:40we can now use our fancy Constructor
- 7:06:43where we can pass it in in this exact
- 7:06:44order right here name age birth month
- 7:06:47and then the number perfect and then we
- 7:06:50can use inside here we can call each of
- 7:06:52these positions separately and
- 7:06:53concatenate them
- 7:06:56together so now that we covered
- 7:06:58structures known by struct as the
- 7:07:00keyword let's have a look look at
- 7:07:01classes and how to define them
- 7:07:03syntactically so we can make a struct
- 7:07:05called person and we can give them a
- 7:07:07string name and inage and to make them
- 7:07:10accessible we'll make them public and we
- 7:07:12can say public person string name and
- 7:07:15int Agee and that means we can assign
- 7:07:18these values using the Constructor so we
- 7:07:20can say this.name equals name and this.
- 7:07:23age equals age perfect so's get rid of
- 7:07:26our compiler error since we're assigning
- 7:07:28both the values and then down here we
- 7:07:30can use it we can say person person
- 7:07:32equals new person and then have abber
- 7:07:35and then 23 in the brackets and we can
- 7:07:38print to the screen person. name and
- 7:07:40then if use contrl D to duplicate that
- 7:07:42down and we can do age and if we run
- 7:07:45that right now we should see abber in 23
- 7:07:48being printed to the
- 7:07:50console perfect so as we talked about
- 7:07:52introduction syntactically the struct
- 7:07:54and the classes are very similar so
- 7:07:56let's comment this out so we don't get
- 7:07:58any compile errors and let's see how
- 7:08:00this is going to work so of course we're
- 7:08:01going to get error on this line but once
- 7:08:03we make our class the error should
- 7:08:04disappear from here so instead of struct
- 7:08:07person we're simply just going to have
- 7:08:09class person exactly the same and
- 7:08:12because these are fields we can also
- 7:08:14copy these down so we can have public
- 7:08:16string name and public int
- 7:08:18Edge and then we can have public person
- 7:08:22and then string name in Edge this. name
- 7:08:25equals name and this. age equals age so
- 7:08:29as you can see this is exactly the same
- 7:08:30as this except for the fact we have
- 7:08:32structure and we have class inside here
- 7:08:35now you see how compile error is gone
- 7:08:36and if youve won the code we should see
- 7:08:38exactly the same
- 7:08:40output there we go now the main
- 7:08:42difference about classes and about
- 7:08:43structures is that you don't actually
- 7:08:45have to give these a default value
- 7:08:47inside the
- 7:08:48Constructor if we just comment this one
- 7:08:50out for a
- 7:08:52second and if we bring the structure
- 7:08:54back and we actually remove the
- 7:08:56parameters from here then we're actually
- 7:08:58going to get compile errors and it says
- 7:09:00that the name and age must be assigned
- 7:09:02before it's returned and stroks cannot
- 7:09:04have parameterless Constructors now
- 7:09:06that's a bit of a problem in certain
- 7:09:08cases you might want to create an
- 7:09:10instance of something but not actually
- 7:09:12derive any values so you could actually
- 7:09:14make these into default values you can
- 7:09:16have string name equals nothing and int
- 7:09:18age equals z and therefore once we place
- 7:09:21inside here then there's no problems if
- 7:09:23you run the code then you will get the
- 7:09:25output this will be nothing this will be
- 7:09:27zero and you'll get errors and you won't
- 7:09:30get any compile errors doing this
- 7:09:31but the point is you have to include
- 7:09:33them and you don't have a
- 7:09:35choice so if we just comment this back
- 7:09:37out now what you'll notice about the
- 7:09:39class
- 7:09:40person is even if we get rid of it then
- 7:09:43it still lets us have it of course we
- 7:09:45can't actually give it parameters since
- 7:09:46we don't have a Constructor that lets us
- 7:09:48have parameters but what you can do as
- 7:09:50well is you can overload this and
- 7:09:52actually include it as well so you can
- 7:09:54have another set that literally let you
- 7:09:56define both of
- 7:09:58them and then you can even have another
- 7:10:00overload that lets you just include the
- 7:10:02name without the ede and then also
- 7:10:05another one that lets you include the
- 7:10:06int age without the name or you include
- 7:10:09both the details so in these cases what
- 7:10:12you can do is you can store the values
- 7:10:14up here as the fields let them use a
- 7:10:16default Constructor or only give it the
- 7:10:18name or the age or you can actually give
- 7:10:20it both so when we come down into here
- 7:10:23we actually now have four different
- 7:10:24Constructors we can make an empty person
- 7:10:27we can make it with an edge with a name
- 7:10:29or with both
- 7:10:32and what you can do in here is you can
- 7:10:33say this.name is equal to nothing and
- 7:10:36then here you can say this. AG is equal
- 7:10:38to minus one just to default it to a
- 7:10:40value that's incorrect so after we've
- 7:10:42got this let's have a look at the
- 7:10:44different ways so we can give it a
- 7:10:45person which gives us an empty value so
- 7:10:47if you run that now we get blank and
- 7:10:50zero and if we just give it the name
- 7:10:52which is Aba then we should get ABA and
- 7:10:54minus one yep and then if you just give
- 7:10:57it the ede you should get nothing in 23
- 7:11:00and if you actually give them both then
- 7:11:02you should get both printed out to the
- 7:11:03screen and there we
- 7:11:06go so now we have four different ways
- 7:11:08that we can create our person with our
- 7:11:10structure we're only really limited to
- 7:11:12one since all of these values need to be
- 7:11:14assigned as the structure is created
- 7:11:16structures are meant to be used as an
- 7:11:18extension to a data type rather than
- 7:11:20almost like a mini management system
- 7:11:23when we make a class we have so many
- 7:11:25options of different ways that we can do
- 7:11:26this inside here and later on in the
- 7:11:28course we're going to show you how to
- 7:11:30include functions inside here and then
- 7:11:32eventually do other Advanced Techniques
- 7:11:34with classes that aren't directly
- 7:11:36available structures if you have a basic
- 7:11:38example where you want to extend
- 7:11:40something to use inside a function or a
- 7:11:43data type like storing name and age
- 7:11:45instead of having to do two return types
- 7:11:47inside a function then a struct is
- 7:11:49perfect the second you want to do more
- 7:11:51complicated things or make it a bit more
- 7:11:53specific and flexible then classes of
- 7:11:55the way forward you'll see it very often
- 7:11:57in games where they have struct position
- 7:12:00you'll see in video games where they
- 7:12:01have something like a struct position
- 7:12:03and they might have int X int Y and int
- 7:12:05Zed and that's for their 3D position or
- 7:12:07they might even just have an X and Y or
- 7:12:09you could have something simple like a
- 7:12:11rectangle and you can give it a width
- 7:12:15and a length so now when you use that
- 7:12:17inside the code you're going to have
- 7:12:19different flexibilities of the amount of
- 7:12:20data types that you can store and then
- 7:12:22when you return that back from a
- 7:12:23function that'll be okay as well since
- 7:12:25you can use this as a data type so if
- 7:12:28you want something simple and
- 7:12:29straightforward then go for the
- 7:12:31the second you want something a bit more
- 7:12:33complicated and a bit flexible then you
- 7:12:35probably want a class we'll go into more
- 7:12:37different situations inside this course
- 7:12:38so you understand in which cases to use
- 7:12:40what and in most cases you'll notice
- 7:12:42that the class is more replicable so if
- 7:12:44you wanted to you could ask the user to
- 7:12:45enter these details in and then you can
- 7:12:47pass them inside the Constructor so we
- 7:12:49could say console right line change it
- 7:12:52to a right enter your
- 7:12:54name String name console. readline and
- 7:12:59then copy and pastees down changes into
- 7:13:01into a in Edge and then wrap this around
- 7:13:03inside of convert in 32 instead of enter
- 7:13:06your name it'll be ENT your Edge and
- 7:13:08then C these are grade out it means we
- 7:13:10haven't used them so we can change abber
- 7:13:11to your name and then change 23 to age
- 7:13:14perfect so now if you run the code now
- 7:13:17enter your name ABA enter your age 23
- 7:13:20then we get ABA 23 printed out to the
- 7:13:22screen so just a quick recap
- 7:13:25Constructors are basically defined the
- 7:13:26same way you have public followed by the
- 7:13:29class or the structure name and then the
- 7:13:30pr inside the brackets and then you can
- 7:13:33assign it after without structure you
- 7:13:35have to declare all of the variables
- 7:13:37inside the parameter and assign them all
- 7:13:39down here otherwise you won't be able to
- 7:13:40run your code with classes we have the
- 7:13:43flexibility of just including one
- 7:13:45default function which will just be our
- 7:13:47Constructor and even if it does nothing
- 7:13:49this is still acceptable and then we
- 7:13:51broke it down to multiple sections where
- 7:13:53you might have situations where you only
- 7:13:55want their name or their age or you
- 7:13:57might even collect both to store it if
- 7:13:59you collect their name then we'll
- 7:14:00initialize age to minus one just so you
- 7:14:02know that this is a bad value and if you
- 7:14:04declare only the age then will'll
- 7:14:06initialize aim to nothing just so you
- 7:14:08know that it's an empty value and if you
- 7:14:11accept both then we simply assign both
- 7:14:13and down here we have access to it we
- 7:14:16can even make this a little bit better
- 7:14:17by checking these values so we can say
- 7:14:20if string is not null or empty of
- 7:14:23person. name and we can put a knot
- 7:14:26inside here then we can print out this
- 7:14:31and then the same thing for age we can
- 7:14:32say if person. AG we can say greater
- 7:14:36than minus one since we set minus one as
- 7:14:38the bad value and then we can move this
- 7:14:40line up and let's try run it now so
- 7:14:43these two if statements are
- 7:14:44fundamentally separate from each other
- 7:14:46one is checking the name and one is
- 7:14:47checking the age so let's try and
- 7:14:49manually type these in and see what
- 7:14:51happens so enter your name let's make
- 7:14:54this blank and only include the age and
- 7:14:56let's see if this runs okay so you had
- 7:14:58no name and the age is 23 then you see
- 7:15:01we only get printed 23 because it'll
- 7:15:03skip over this line as you can see if we
- 7:15:05put a break point in now and just
- 7:15:07refresh the code we get empty name plus
- 7:15:1023 and when we press F10 over this line
- 7:15:13it jumps the next one because if we
- 7:15:15hover over this you'll see that it
- 7:15:16should return us back with false there
- 7:15:19we go we get strings null or empty is
- 7:15:20false so that we skip over that and
- 7:15:23because the age is 23 then this line
- 7:15:25going to execute let's run it again and
- 7:15:26do the opposite we could say name is Aba
- 7:15:30and then age is min one and then we get
- 7:15:32ABA printed be not the
- 7:15:34edge there we go so if you wanted to fix
- 7:15:37these in and type them manually so we
- 7:15:39can say
- 7:15:4023 there we go we get 23 and if we only
- 7:15:43include the
- 7:15:45name
- 7:15:47AB then we only get Al printed out and
- 7:15:49if we include both of
- 7:15:52them then we get them both printed out
- 7:15:56perfect so now that we've created our
- 7:15:58class we've gave it some fields and
- 7:16:00we've made our Constructor that lets us
- 7:16:02initialize it let's see how we can print
- 7:16:03the details out to the console so we can
- 7:16:05have a CW tab tab a console right line
- 7:16:08and then we can say string interpolation
- 7:16:11name callon space and then we can have
- 7:16:13our curly braces and we can say person.
- 7:16:16name we can access these fields using
- 7:16:18the dot operator and then we can say
- 7:16:20back sln for the new line and then say
- 7:16:22age and the same thing person. AG let's
- 7:16:25try run that and see what we've
- 7:16:27got so now we've outputed name and age
- 7:16:30out to the screen let's see how we can
- 7:16:31do this inside a function if you wanted
- 7:16:33to place this in different part of your
- 7:16:35code then you're going to have to copy
- 7:16:36and paste this and as we know this is
- 7:16:38bad for our code because it's j a
- 7:16:39duplication we want to be able to wrap
- 7:16:41this inside a function somewhere so
- 7:16:43let's see what we can do we can have a
- 7:16:44static string so it can return as back
- 7:16:47with this that we can output straight to
- 7:16:48the console and we can say return
- 7:16:51details and for our parameter we can
- 7:16:53have something like string name and int
- 7:16:56age because that's what our class is
- 7:16:57made up of and inside here we can simply
- 7:17:00take this
- 7:17:01put it inside here and put a return
- 7:17:03before it and then get rid of the person
- 7:17:05dot since we have an aim and age
- 7:17:07variables as local to the function so
- 7:17:10now instead of this line we can
- 7:17:11duplicate this down take out the
- 7:17:13contents and we can say return details
- 7:17:16and then we can place person. name and
- 7:17:18person. AG inside here let's run the
- 7:17:21code so we get exactly the same output
- 7:17:24but the problem with this function is
- 7:17:26we're having to type out the parameters
- 7:17:28manually we have them here and we're
- 7:17:30going to have to to type them again in
- 7:17:31here and again for the parameters this
- 7:17:33is a bit of a problem cuz we've just
- 7:17:35made our class but we haven't really
- 7:17:37used it when we're passing it in so
- 7:17:38instead of having string name and int
- 7:17:40age we can actually duplicate this
- 7:17:42method and just comment it out just so
- 7:17:43you can see it for reference and instead
- 7:17:45of doing this you can actually just pass
- 7:17:47in an entire person so now we have
- 7:17:50access to person and we can bring this
- 7:17:52back and we can say person. name and we
- 7:17:54can also say person. age and the reason
- 7:17:57why this method is preferred is if you
- 7:17:59actually have something else public int
- 7:18:01number and then you added another one
- 7:18:03inside here in number and then you said
- 7:18:05this do number equals number the
- 7:18:08parameters won't have to change inside
- 7:18:10the function all you have to do is
- 7:18:12simply pass in the person and then now
- 7:18:15we actually have access to doing person.
- 7:18:17number inside here should we need to if
- 7:18:19you're passing them all in one by one
- 7:18:21like this then you have to append into
- 7:18:24number on the end and then add it
- 7:18:25straight to here and then also add it
- 7:18:27inside here as you have like this it's
- 7:18:29easy to pass the class in because all of
- 7:18:31these properties above are just simply
- 7:18:33used throughout the function so now if
- 7:18:35we just bring this
- 7:18:39back and let's have a run and see if we
- 7:18:42get the same
- 7:18:43output which we do name ABA age 23
- 7:18:46perfect so now that we've thought of two
- 7:18:49functions how to do this we can manually
- 7:18:51type in the strings and the integer and
- 7:18:52any other parameters that we need all we
- 7:18:54can actually pass in the person the
- 7:18:56problem with these two approaches is the
- 7:18:58whole point of having classes and
- 7:19:00structures of of the like is so we can
- 7:19:02wrap around lots of functionality inside
- 7:19:03it if you want to send this to another
- 7:19:05developer for them to use in their
- 7:19:07project or maybe this is a file that is
- 7:19:08shared within the project then you don't
- 7:19:10want them to include a separate function
- 7:19:12that is simply outside of the class we
- 7:19:14want to include the functions inside the
- 7:19:16class so of course as you know these are
- 7:19:19functions so inside here we can also
- 7:19:22make functions so let's do it so we can
- 7:19:24have public and we want a string return
- 7:19:27details and then we don't need to pass
- 7:19:29anything in the the values that we're
- 7:19:32going to be returning are these two
- 7:19:33right here which is technically local to
- 7:19:35our class so therefore we're not going
- 7:19:37to have any parameters I'll explain that
- 7:19:40further in a second so we can take this
- 7:19:42exact line paste it straight into here
- 7:19:44and get rid of the person Dot and get
- 7:19:46rid of the person do on this side so
- 7:19:48then we have the name variable and the
- 7:19:50age variable if you double click on them
- 7:19:52you see it references these variables
- 7:19:54and the same thing for age the reason
- 7:19:56why we don't have parameters is when you
- 7:19:58call this function as I'll show you in a
- 7:19:59second you call it directly on the
- 7:20:01person so the person that you're trying
- 7:20:03to print has already got these contents
- 7:20:05inside of them so let's have a look we
- 7:20:07can duplicate this down and instead of
- 7:20:09calling return details and passing the
- 7:20:11person you actually do it the other way
- 7:20:13around you say the person object dot
- 7:20:16return details so now we're calling
- 7:20:18return details inside our person
- 7:20:21object now let's run the code you see
- 7:20:24that the output is exactly the same
- 7:20:26except now this functionality is a lot
- 7:20:27better our return details is wrapped
- 7:20:30inside our class which is the preferred
- 7:20:32way now we can actually get rid of these
- 7:20:34inside here and then we shouldn't really
- 7:20:36use this notation even though it is
- 7:20:38technically correct but you want to wrap
- 7:20:40all your functionality inside here and
- 7:20:42just bear in mind that scope is also
- 7:20:44applicable here return details is within
- 7:20:46this class which means if you try to run
- 7:20:49this function inside the main then it's
- 7:20:50not going to node exist this return
- 7:20:53details function was able to be used
- 7:20:55inside here because this was in the
- 7:20:57scope of the class program which is this
- 7:20:59curly brace and this curly bra but this
- 7:21:02return details is only within the scope
- 7:21:04of this class which means you can't just
- 7:21:06suddenly use it inside the main so
- 7:21:08please bear that in mind when you
- 7:21:09produce in these classes and you trying
- 7:21:11to understand where everything goes
- 7:21:13every function that you place inside
- 7:21:15here is accessible through the dot
- 7:21:16notation after the object once you've
- 7:21:18created the class say person equals new
- 7:21:21person and then after we type person dot
- 7:21:23then we'll get a list of all the
- 7:21:24functions the same way if you have
- 7:21:26something else just like this and then
- 7:21:30if you do person Dot and then you see
- 7:21:32that something has appeared there so all
- 7:21:34the functions that you create up inside
- 7:21:36in this class for every person that you
- 7:21:38make it'll be accessible through the do
- 7:21:40notation so just to round up this video
- 7:21:42you should always make your functions
- 7:21:44inside your class having them inside the
- 7:21:46class is the whole point of making a big
- 7:21:48class and have lots of functionality
- 7:21:50inside if you keep the function that
- 7:21:52does exactly the same thing outside of
- 7:21:54the class then if you were to share this
- 7:21:56file or come back to it in the future
- 7:21:58then you're not going to understand why
- 7:21:59you've done this and and it's outside
- 7:22:01the class keeping everything wrapped
- 7:22:02inside the class is the best way forward
- 7:22:04in most case
- 7:22:07scenarios so now we have our class
- 7:22:09person it's got a name and age a
- 7:22:11Constructor and a function to return
- 7:22:13back the details and we can simply use
- 7:22:15it like this and return back the details
- 7:22:17like this but so far we've covered how
- 7:22:19to make classes Constructors Etc and
- 7:22:22assign the values but the values are
- 7:22:24only get assigned throughout the
- 7:22:26Constructor but what if a different part
- 7:22:28of our program the user wants to change
- 7:22:30their name or update their age Etc then
- 7:22:32what options do we have so you know we
- 7:22:34can access our Fields using person. name
- 7:22:37and then you can simply type equals and
- 7:22:39then type in another name and we can say
- 7:22:41person. AG equals 25 now we've changed
- 7:22:44these properties if we use the console
- 7:22:46right line and return details you'll see
- 7:22:48that the values have indeed
- 7:22:50changed as you can see there it's
- 7:22:52changed the hurry and
- 7:22:5425 this is okay except for the fact that
- 7:22:57this shouldn't really be possible we
- 7:22:59should try and restrict this Direct
- 7:23:00corly by the use of gets and sets so for
- 7:23:03now we're going to cover fields and how
- 7:23:04to restrict certain values and later on
- 7:23:07in the course we're going to go through
- 7:23:08what properties are so before alter
- 7:23:10properties got invented we had to use
- 7:23:13gets and sets gets and sets are
- 7:23:15essentially functions that will prevent
- 7:23:17this from happening so the reason why we
- 7:23:20can do this is simply because this
- 7:23:22public word is placed right here if we
- 7:23:25change it to private it means that name
- 7:23:27is still accessible within here and here
- 7:23:30which is only the limit of the class but
- 7:23:32as you can see it's not applicable here
- 7:23:35this is why you need the public Getters
- 7:23:37and Setters so I'll explain that what
- 7:23:39that means now so we prevent these from
- 7:23:41accessing outside the class by making
- 7:23:44them private so this functionality no
- 7:23:46longer works so what you have to do
- 7:23:48instead is for every field that you have
- 7:23:50you need to create a GTO and a seter
- 7:23:53function so in order to set a value we
- 7:23:55can have public and because we're
- 7:23:57setting it we don't need to return
- 7:23:58anything so it'll be void and we can say
- 7:24:00set name and then we pass in string name
- 7:24:04and then we simply just say this. name
- 7:24:06equals name so when someone call set
- 7:24:09name and pass in the name like this we
- 7:24:11just override it so let's see how that
- 7:24:13looks so if we comment these two lines
- 7:24:15out we can simply say person do set name
- 7:24:18and then we can say Harry so this line
- 7:24:21is directly replaced by this line and
- 7:24:23now if you run this we'll see that Harry
- 7:24:25should be changed there you go abber is
- 7:24:28changed to Harry this is powerful
- 7:24:31because what we can do now is restrict
- 7:24:33the access inside here and make sure
- 7:24:35that the value contains something
- 7:24:36genuine we can say if string is null or
- 7:24:41empty and then pass the name inside
- 7:24:43there if the string isn't null or empty
- 7:24:46then we assign it and if it is null or
- 7:24:48empty then what we can do inside here is
- 7:24:51we can say else this do name equals
- 7:24:53nothing and we can keep it blank or we
- 7:24:55can use string. empty just to make sure
- 7:24:58that we know that the name has been
- 7:24:59blank out if they've entered something
- 7:25:02invalid if they've entered something
- 7:25:03that's valid we assign it if it's
- 7:25:05invalid then we don't assign it and we
- 7:25:07just initialize it back to nothing and
- 7:25:09because our options are endless with
- 7:25:10this we can actually make this into
- 7:25:12something else we can simply just say
- 7:25:14invalid name so then what happens is if
- 7:25:17we try and change this to nothing and we
- 7:25:19run the
- 7:25:20code we can see that we get invalid name
- 7:25:22being printed to the screen and that's a
- 7:25:24lot better than if we had string.
- 7:25:27empty because if you had string. empty
- 7:25:30then it would kind of say nothing then
- 7:25:31it looks like something's went wrong if
- 7:25:33you have something like invalid then
- 7:25:35that gives you a bit of a Barr message
- 7:25:37so we can have invalid name there so
- 7:25:40using our Setter Let's us change our
- 7:25:41name from down here without directly
- 7:25:44exposing the public if he expose the
- 7:25:46public then everyone who uses classes
- 7:25:48can simply change the name to whatever
- 7:25:50they want having it private and only
- 7:25:52changing the name within the set name
- 7:25:54function is very preferable because you
- 7:25:57can restrict the access and what people
- 7:25:58can set the values to if they try to set
- 7:26:01it to something that's not valid then we
- 7:26:03set it to invalid name whereas before
- 7:26:06when we did this and if we make this
- 7:26:08public
- 7:26:10again we can simply set this to
- 7:26:12something bad like null null is a very
- 7:26:14bad value but it still lets us have it
- 7:26:17and the reason why it's very bad is if
- 7:26:19you try to do something like this
- 7:26:21person. equals and just some empty
- 7:26:24brackets now even if I just leave this
- 7:26:26empty the fact that you try call equals
- 7:26:29on S inside person. name is going to be
- 7:26:32a bit of a
- 7:26:33problem so we can say person object the
- 7:26:36name property inside it and we checking
- 7:26:38if it equals to Aba for example but
- 7:26:40because we've just said it to null let's
- 7:26:42see what
- 7:26:43happens you see that we actually get an
- 7:26:45exception it's a null reference
- 7:26:47exception because if you hover over name
- 7:26:49you see that returns back null nulls are
- 7:26:51very bad in this case whereas if you
- 7:26:53actually bring our function back and we
- 7:26:56make this into a private and then if we
- 7:26:59try and take this value our null value
- 7:27:01and place it straight inside our set
- 7:27:03name if you uncomment it get rid of the
- 7:27:06quotation marks and if you actually try
- 7:27:08and assign it to null let's see what
- 7:27:10happens then we're going to get invalid
- 7:27:12name and this prevents us from breaking
- 7:27:14our code if a function like equals were
- 7:27:17to be called on any of our properties
- 7:27:18inside of name so now that we've done it
- 7:27:20for our name we can also do it for our
- 7:27:22age but before we do that we can also
- 7:27:25call a function called get name return
- 7:27:28details gives you back these details
- 7:27:30in a very specific fashion but what if
- 7:27:32you wanted to do something like this
- 7:27:34your name is and then you wanted to do
- 7:27:37person. name but of course you can't do
- 7:27:39that because name is no longer public so
- 7:27:42you can't reference it directly so we've
- 7:27:44got a set name and then now we need to
- 7:27:46do a get name so we can have public and
- 7:27:49because name is a string the return type
- 7:27:52is going to be string and we can say get
- 7:27:54name and this function is very simple it
- 7:27:56just returns back name and that's it
- 7:27:59there's no no parameters because all it
- 7:28:01does is return back a value and in this
- 7:28:03case it's just returning back name so
- 7:28:06instead of using person. name in here we
- 7:28:08can say person do get name open close
- 7:28:11the brackets to indicate it's a function
- 7:28:12but there's no parameters now you see
- 7:28:14that we have no compile errors so now if
- 7:28:16you run that you'll see that it says
- 7:28:18your name is invalid name so let's
- 7:28:21actually give it a
- 7:28:22value and we can say Harry again run the
- 7:28:25code there you go your name is Harry so
- 7:28:28it's officially change it now let's work
- 7:28:30on the
- 7:28:31edge so just following the exact same
- 7:28:33approach we can have a public void set
- 7:28:37Edge in age and instead of just doing
- 7:28:41this. age equals age let's try set some
- 7:28:43boundaries on this we can wrap this
- 7:28:46inside an if statement so as for the
- 7:28:48condition we want to validate if the age
- 7:28:50is successful so we want to do if age is
- 7:28:52greater than or equal to zero and age is
- 7:28:54less than or equal to 150 so this means
- 7:28:57if the age is less than zero or greater
- 7:28:59than 150 then this is going to return
- 7:29:02back false and then it's not going to
- 7:29:03assign the value and we can simply
- 7:29:05assign it to minus1 -1 is just so we
- 7:29:08know it's a bad value so now that we've
- 7:29:10created our set age we can actually make
- 7:29:12our get age so we want to return back in
- 7:29:14int because that's going to be the data
- 7:29:17type for age so we do public int get age
- 7:29:20we don't need any parameters cuz as like
- 7:29:22the name we're just simply returning
- 7:29:24something so we can say return age so
- 7:29:27now that you've made the set age and the
- 7:29:28get age let's actually use it quote so
- 7:29:31we've made our set name and let's say
- 7:29:33person do set age and we have 25 inside
- 7:29:36here just like above and then now that
- 7:29:39we've set the age let's appin this
- 7:29:40sentence we can say and your age is and
- 7:29:43then person. get age perfect so let's
- 7:29:47run the code and see what happens there
- 7:29:49we go we have 25 and Harry being
- 7:29:53changed so another little tip when we do
- 7:29:55in this code I get age as one line and
- 7:29:58I'll set age as a couple of lines but
- 7:30:00fundamentally only assigning Edge so
- 7:30:02what we can do is we can make these into
- 7:30:04the tertiary operator so as we can see
- 7:30:06here this is the condition if this
- 7:30:09returns back true then we assign age our
- 7:30:11local parameter and if this returns back
- 7:30:13false we assign minus one so we can
- 7:30:16actually make this into this. AG equals
- 7:30:19if you remember the format of our
- 7:30:20conditional operator use condition true
- 7:30:23and then condition false so this is our
- 7:30:26condition that is leading our if
- 7:30:28statement and if this returns back true
- 7:30:30then we do question mark and put age in
- 7:30:32there and then call on if it returns
- 7:30:34back false then it's simply minus one
- 7:30:36there we go so this is actually being
- 7:30:38entirely replaced by this one simple
- 7:30:41line and we can do the same thing for
- 7:30:43our string so we can say this.name is
- 7:30:46equal to string that is null or empty
- 7:30:48pass the name inside there and if that
- 7:30:50is true then we assign it to name and if
- 7:30:53that is false we say invalid name let's
- 7:30:55comment this one out and let's see if
- 7:30:57our code runs okay still
- 7:31:00hurry 25 hurry 25 and now let's try our
- 7:31:03bad values so we can try to set the age
- 7:31:05to 200 and the value to nothing and
- 7:31:08let's see what happens we get invalid
- 7:31:11name minus one and there you
- 7:31:13go perfect so that's working okay let's
- 7:31:15undo them values and you can see that
- 7:31:18this has been shortened completely
- 7:31:20another top tip for functions like this
- 7:31:22you can actually use something called
- 7:31:23Arrow functions and if you've done any
- 7:31:25JavaScript this might be quite familiar
- 7:31:27so we can have our get Edge and in
- 7:31:29instead of open the curly brace at this
- 7:31:31point we can actually place this Arrow
- 7:31:33inside so if we comment out this one
- 7:31:36with arrow functions you can only return
- 7:31:38back one simple line so because this is
- 7:31:41only one line we can have return Edge
- 7:31:43but our functions don't like the keyword
- 7:31:46return any variable that you put
- 7:31:48straight inside here is automatically
- 7:31:50applied that it's a return type so we
- 7:31:52literally just put age get age returns
- 7:31:55us back with age if this is confusing
- 7:31:57then stick to this approach but this can
- 7:32:00might tidy up your code because instead
- 7:32:02of having all of this and all the curly
- 7:32:04braces for one simple line you can use
- 7:32:06Arrow function so if you actually copy
- 7:32:08and paste but first comment out all of
- 7:32:11these functions you can see how small
- 7:32:13the code can actually get there we go
- 7:32:16and then let's actually have a public
- 7:32:18void set age let's put our R function in
- 7:32:22and then we can take this one line and
- 7:32:24paste it straight into here so you can
- 7:32:26see that these two lines are now being
- 7:32:28replaced by all of this
- 7:32:30and of course we need our int age inside
- 7:32:32here there we
- 7:32:35go so now we can do the same thing with
- 7:32:37our set name we can take
- 7:32:40this we can have public viid set name
- 7:32:44String name and then have our Arrow
- 7:32:46function and then simply paste that in
- 7:32:48you can see that this will get a little
- 7:32:49bit long now but it's still ideal I've
- 7:32:52got the code zoomed in but if you put
- 7:32:54that to 100% you'll see that this code
- 7:32:56is actually pretty small it's just I've
- 7:32:58zoomed in just so you can see the code a
- 7:33:00lot
- 7:33:01clearer and then finally for public
- 7:33:05string and we can get name and then we
- 7:33:07simply just do arrow and then name and
- 7:33:10you can see that with all this other
- 7:33:11code commented we're just relying on
- 7:33:13these functions as we just created and
- 7:33:15you'll see there's no console errors or
- 7:33:17anything else so if you try and run it
- 7:33:19you'll see that everything is going okay
- 7:33:21of course if you don't like this
- 7:33:23notation and you don't have to include
- 7:33:24it you can stick with the curly braces I
- 7:33:27don't use the arrow functions a lot in
- 7:33:28practice because there isn't many
- 7:33:30situations where you'd actually use them
- 7:33:32and even if you do some people might not
- 7:33:34like them or there might be certain
- 7:33:36guidelines that you might be following
- 7:33:37inside the
- 7:33:38company what I do suggest you stick with
- 7:33:41is the tertiary operator these are a lot
- 7:33:44good and a lot better to use since it
- 7:33:46reduces the code drastically if we
- 7:33:48actually remove this and I'll paste it
- 7:33:51back in just so you can see it for
- 7:33:52reference inside the resources but you
- 7:33:54can see that these four functions are
- 7:33:56now reduced to on one pitch you can see
- 7:33:59that they're all got one lines and
- 7:34:00they're pretty short so let me undo that
- 7:34:02just so you can see the original code
- 7:34:04and have it for
- 7:34:05reference so there's a lot covered
- 7:34:07inside this video so let's just have a
- 7:34:09little recap before we finish up so just
- 7:34:12to start at the beginning when we
- 7:34:14initialize our person with their name
- 7:34:16and their age we don't actually have any
- 7:34:18other opportunities to change their
- 7:34:19value which is where this comes in you
- 7:34:22can actually just say a person. name and
- 7:34:24assign any sort of value you can put
- 7:34:26anything inside there and it'll assign
- 7:34:28it directly unconditionally the problems
- 7:34:30of doing this is someone might expose it
- 7:34:32to null by accident and somewhere in
- 7:34:34your code if you're using something like
- 7:34:36equals this will actually prevent an
- 7:34:38exception of course you can wrap this
- 7:34:40inside a try catch but this is
- 7:34:41unnecessary because we can prevent this
- 7:34:43in the first place it's always best to
- 7:34:45prevent an exception then to put
- 7:34:47additional cord in like a try catch in
- 7:34:49order to catch something that you can
- 7:34:50prevent in the first place prevention is
- 7:34:52definitely better than the Cure in this
- 7:34:54case so having something like a person.
- 7:34:57name being exposed it's not so much for
- 7:34:59the integer because integers will always
- 7:35:02have number values whereas strings they
- 7:35:04can be initialized to null there is no
- 7:35:06such thing as null when our age that our
- 7:35:09default value for an integer is zero so
- 7:35:12we don't really have the same issue we
- 7:35:13started introducing the name function
- 7:35:15and the age function these are used so
- 7:35:17we can control what values get assigned
- 7:35:19to our local variables and we also have
- 7:35:22our get functions get name and get age
- 7:35:24so let's explore how these are made
- 7:35:26inside our Cass and expand on that and
- 7:35:28see how they work so first we need to
- 7:35:30make sure that our variables are
- 7:35:32actually private they're private so we
- 7:35:34can't access them just like this if I
- 7:35:36copy and paste this line You'll see
- 7:35:37we're going to get a compile error and
- 7:35:39this error is because we can't actually
- 7:35:41use it because as it says here it's
- 7:35:44inaccessible due to its protection level
- 7:35:46and the protection level comes from the
- 7:35:48fact that this is indeed private it's
- 7:35:51private so it's only accessible within
- 7:35:53class person so every reference and name
- 7:35:56can be used inside here just not outside
- 7:35:58so the only way to be able to change the
- 7:36:00name now you've made it private is by
- 7:36:02enforcing this function the good thing
- 7:36:04about this function is we can check if
- 7:36:06the value is invalid and then we can
- 7:36:08star it if it's valid and then change it
- 7:36:10to something else if it's invalid and
- 7:36:12then we can wrap this if statement
- 7:36:13around in a conditional Operator just to
- 7:36:15make it a little bit shorter and in
- 7:36:17order to retrieve a name because now
- 7:36:19that it made it public we can neither
- 7:36:21set or retrieve it so we have our get
- 7:36:24name function that simply just returns
- 7:36:26it back to the screen nothing special
- 7:36:28here and the same thing for our set age
- 7:36:30we can check if the age is between 0 and
- 7:36:32150 and assign it and if it's not assign
- 7:36:34it to minus1 and then again wrap it
- 7:36:37around in our conditional operator and
- 7:36:39then for get age we can just return age
- 7:36:41I demonstrated our functions inside here
- 7:36:44you can take all of these on line
- 7:36:45functions and actually add them all in
- 7:36:47one line without the need of curly
- 7:36:49braces if you want to return back in age
- 7:36:51then you simply just type in age just
- 7:36:53like you can see here and here when you
- 7:36:55do assignment you want to specify this.
- 7:36:58name equals but when you doing returns
- 7:37:00you just need to type in the variable
- 7:37:01that you need of course this looks a
- 7:37:03little bit cleaner and a bit shorter but
- 7:37:05not Everyone likes this notation so it's
- 7:37:07completely up to the developer all the
- 7:37:09environment that you're working in and
- 7:37:11finally we just have our return details
- 7:37:13and then we can make use of them just
- 7:37:15like this by using person which is the
- 7:37:17name of our local variable for our new
- 7:37:19instance followed by a DOT and then set
- 7:37:21name set age and then get name and then
- 7:37:24get
- 7:37:27age so now that we're dealing with
- 7:37:29classes it's important you focus on the
- 7:37:31scope we have a person person here
- 7:37:33that's defined within the main which
- 7:37:35means it's only Within These two curly
- 7:37:37braces so if we have a static void and a
- 7:37:39function that did something inside here
- 7:37:42we can't access person inside here if we
- 7:37:44take out the comment and we try it the
- 7:37:46only reference we have is accessing the
- 7:37:49class directly not this local variable
- 7:37:52if you want to access local variable
- 7:37:54from inside this function then you'd
- 7:37:56simply have to pass it in like this and
- 7:37:58then it'll finally become available as
- 7:38:00you can see now there's two and it says
- 7:38:02as a parameter and the same goes for
- 7:38:04this class above just because we have
- 7:38:07name and age up here it doesn't
- 7:38:09necessarily mean that we can magically
- 7:38:10use name down here if we have something
- 7:38:13like a static int defined right here
- 7:38:16then we can use this integer inside
- 7:38:18these functions as it says there it's
- 7:38:20available we can sign that to 10 and
- 7:38:23it's also down here available and we can
- 7:38:25sign that one to 20 and you see we have
- 7:38:26no compil erors but if we take this and
- 7:38:29put it inside our return details
- 7:38:30function then you're going to get an
- 7:38:32exception because X is not available
- 7:38:34within that scope and the functional
- 7:38:35scope matters inside here as well if you
- 7:38:38have a string test inside
- 7:38:40here we can just access it in another
- 7:38:42function for example The Constructor we
- 7:38:45can't just print out test as the screen
- 7:38:48the handy things or classes if you want
- 7:38:49variables to be available throughout the
- 7:38:51class then you put them at the top we
- 7:38:53can have that private string test and
- 7:38:56there you go you see that the compiler
- 7:38:57error is now gone and we give that
- 7:38:59initial value and then you can use that
- 7:39:01to print to the screen Etc and just to
- 7:39:04note when you have a variable that's
- 7:39:06called exactly the same thing we have a
- 7:39:09private string test up here and we can
- 7:39:11say hello then we also have a string
- 7:39:13test down here so let me demonstrate
- 7:39:16what happens when two variables are the
- 7:39:17same so let's move this into our
- 7:39:20Constructor so if we run this as it is
- 7:39:22the test equals hello and then we print
- 7:39:24out test and then run the
- 7:39:26code there you go we get hello printed
- 7:39:29and just ignore this this is from the
- 7:39:31return details function as we've got in
- 7:39:33the main down here return details so
- 7:39:37we've made our new Constructor and then
- 7:39:39assigned it the value with abber and 23
- 7:39:41and then we can get hello printed to the
- 7:39:43screen but let's say we actually made a
- 7:39:44string test inside here and we said hi
- 7:39:47instead of hello then which one do you
- 7:39:48think it's used the global one or the
- 7:39:51local one let's run it and find out it's
- 7:39:54actually the local one the compiler will
- 7:39:56always prioritize the local variables
- 7:39:58over the glob Global variables and this
- 7:40:00is the reason why you need to type this
- 7:40:02dot because when you use name it's
- 7:40:05always going to reference the parameter
- 7:40:06when it's name on its own use console
- 7:40:09right line and say name and then if we
- 7:40:11do after this line and another console
- 7:40:12right line and we say this. name then
- 7:40:14these are completely different this one
- 7:40:16is the parameter and this one is talking
- 7:40:18about the variables from the class and
- 7:40:20these are known as our Fields local
- 7:40:23variables take presidence overall Fields
- 7:40:26as long as they're called the same thing
- 7:40:28as you can see now says it's assigned
- 7:40:30but the value is never used if you take
- 7:40:32the string test out then you can see
- 7:40:33it's now being
- 7:40:36used there you go hello gets printed out
- 7:40:39to the screen and there you go the green
- 7:40:41line has went because we're using it
- 7:40:43down here scope is very important when
- 7:40:45you're designing these classes and just
- 7:40:47to note that the variables that are
- 7:40:49available inside here are not going to
- 7:40:52be necessarily available inside here
- 7:40:54unless you make them public and even
- 7:40:56when you make them public that doesn't
- 7:40:58necessarily mean mean that this is still
- 7:41:00available magically over here name will
- 7:41:04only be available through an instance of
- 7:41:06the person class so if we do person.
- 7:41:08name now we can see that that's
- 7:41:10available as our field so that's why
- 7:41:13that makes it
- 7:41:14public if we try and make this private
- 7:41:16again just to return that value
- 7:41:19back and we scroll down you'll see now
- 7:41:21we get a compile error because it's
- 7:41:23inaccessible because it's outside the
- 7:41:25scope because it's now a private
- 7:41:28variable
- 7:41:31so we've covered what it means to have a
- 7:41:33public or a private field if you have a
- 7:41:35private field then you're going to have
- 7:41:37to use Setters and Getters in order to
- 7:41:39work with this field if you make it
- 7:41:41directly public then you won't need any
- 7:41:43Setters and Getters because the user can
- 7:41:45just type in do name after their public
- 7:41:48instance so you can have person followed
- 7:41:50by the dot and right now you can't see
- 7:41:52name tied here and the blue slittle icon
- 7:41:55is no longer there the reason why it's
- 7:41:57not there is cuz there's no public
- 7:41:59Fields if we then change this to
- 7:42:02public remove the dot and read it you'll
- 7:42:04see that our blue square is now back
- 7:42:07because our field is now accessible and
- 7:42:09we can assign that to anything that we
- 7:42:11want the whole point of making this
- 7:42:12private is you only restrict the access
- 7:42:15so you know what you're doing and the
- 7:42:17reason why I say that is if they enter
- 7:42:20something that's invalid then we want to
- 7:42:21specify something like invalid if we
- 7:42:23give them Public Access then we're not
- 7:42:25going to know and we won't be able to
- 7:42:27validate what they've entered so in
- 7:42:29order to do it through a Setter we can
- 7:42:31make it private create our set function
- 7:42:33with a return type it's going to take in
- 7:42:35a string parameter and this string has
- 7:42:37to be the same data type as the prety
- 7:42:39you're talking about call it name or
- 7:42:42whatever you want it to be and because
- 7:42:44these two are called exactly the same
- 7:42:45thing we have to use this dot so we're
- 7:42:47referencing this name instead of this
- 7:42:49name in the parameters and here we're
- 7:42:51just using a conditional operator and
- 7:42:52we're saying if the string is not null
- 7:42:54or empty hence the exclamation point
- 7:42:56then we assign it to name name gets put
- 7:42:59in this.name and if this were to return
- 7:43:02back false then we place invalid name
- 7:43:04straight into this. name and for the get
- 7:43:07we simply just return a string since
- 7:43:09that's a data type for name and then
- 7:43:11just return it back and it's that simple
- 7:43:13and the same thing for age we validate
- 7:43:15the age range set it and if it's outside
- 7:43:17this range we can set it to minus one so
- 7:43:19as you can see this is a bit long we
- 7:43:21have our private variables then we have
- 7:43:24two functions one for set one for get
- 7:43:27and the same thing for age so these are
- 7:43:29called fields and gets and Setters so
- 7:43:32I'm going to introduce a notion called
- 7:43:34properties and properties make it easier
- 7:43:36to do this exact notation so let's now
- 7:43:38see how property is created since these
- 7:43:41fields are private we need to make
- 7:43:43something that's actually public so we
- 7:43:45can do something like this we can say
- 7:43:47public string and we can say name with a
- 7:43:49capital N so now you can see that name
- 7:43:53and name are not actually clashing and
- 7:43:55we can assign that in here should we
- 7:43:56want to and you see we don't need this
- 7:43:58do anymore because this is now different
- 7:44:01it's a capital N so this one always
- 7:44:04references this and this references this
- 7:44:07so we need to add something else onto
- 7:44:09here so if you create them
- 7:44:11both now you can see we can do age
- 7:44:14equals age and there'll be no problems
- 7:44:16with this so now that we've made our two
- 7:44:18new Fields so now we have our two new
- 7:44:20public variables which are known as our
- 7:44:22Fields let's see how we can turn them
- 7:44:24into properties so if you remove the
- 7:44:26semicolon you can actually add in some
- 7:44:28curve curly braces so inside here we
- 7:44:31need a getter and Setter just like we
- 7:44:34had down there but we have a quicker
- 7:44:35notation for it instead of making two
- 7:44:38distinct functions you can actually
- 7:44:39create a little section inside these
- 7:44:41curly braces to get and set the name so
- 7:44:44you got to think of this as a middleman
- 7:44:46the same way this is a middleman we call
- 7:44:49this function and this function will
- 7:44:51affect the name and this is going to be
- 7:44:53the same principle we make use of name
- 7:44:56with a capital N but it will actually be
- 7:44:58affecting the lowercase end variable
- 7:45:00these are going to be our new middle BS
- 7:45:02and they will directly replace the gets
- 7:45:04and sets here so what we can do in here
- 7:45:06is we can say get press enter go on the
- 7:45:09next line and make some C braces and
- 7:45:11then we can also have a set so this is
- 7:45:13like our mini get a mini set function
- 7:45:16just like we've got here but as you can
- 7:45:18see it's a lot shorter we don't need to
- 7:45:20give it a distinct name or set the
- 7:45:21public or anything else we just give it
- 7:45:24a simple set and get so if you have a
- 7:45:26look at the compiler error it says not
- 7:45:29all cause path return of value because
- 7:45:31as it says here this get function is
- 7:45:33actually a mini function built into this
- 7:45:36property person. name which is our name
- 7:45:38with a capital N and then it's do get
- 7:45:41and this returns back a string so if we
- 7:45:43actually just type in return this then
- 7:45:45it gets rid of the compiler error but of
- 7:45:47course we don't want to return nothing
- 7:45:48we actually want to return name because
- 7:45:50remember what I said string name and
- 7:45:52intage up here as our private field they
- 7:45:55actually start the data the public
- 7:45:57string name and the public inage is the
- 7:45:59middle mans that coordinates all of the
- 7:46:01gets and sets to do with the private
- 7:46:03variables so for the set just for now we
- 7:46:05can say name equals and then what do we
- 7:46:08give it the value of so if we hover over
- 7:46:11set it doesn't actually give you any
- 7:46:13indication of what it's going to be so
- 7:46:15let's have a look if we press control
- 7:46:17space then we don't also get the list of
- 7:46:19everything that we can add inside here
- 7:46:21now it's very difficult to understand
- 7:46:23what to assign this to when we have set
- 7:46:26name we've gave it a string name
- 7:46:28parameter so we can use name down here
- 7:46:30so what's our parameter in this case
- 7:46:32well the function itself doesn't
- 7:46:33actually tell us but I'm telling you
- 7:46:34right now that the parameter is actually
- 7:46:36just called value and this is a very
- 7:46:38default parameter that you'll see
- 7:46:40everywhere when you're doing gets and
- 7:46:42sets inside a property you don't have to
- 7:46:44specify value or anything it just gets
- 7:46:47simply assigned in just like here where
- 7:46:49we have string name this will be a
- 7:46:52string value so it will look something
- 7:46:53like this behind the scenes but of
- 7:46:55course you can't actually type this in
- 7:46:57cuz remember these properties they
- 7:46:59trying to give you a short hand on how
- 7:47:00to do gets and Setters so you just got
- 7:47:02to remember that when you're trying to
- 7:47:04set you get the property inserted as
- 7:47:06value so you can start that into name so
- 7:47:09that's it the name is actually sorted
- 7:47:11now so before we do the same thing for
- 7:47:13age let's see how we can use this inside
- 7:47:15the code so we scroll down to our main
- 7:47:18we have a person person and then we have
- 7:47:19a console right line that prints the
- 7:47:21details so we can actually do person Dot
- 7:47:24and you can see now that it's slightly
- 7:47:26different if you ignore the edge since
- 7:47:28start to return back as a field since we
- 7:47:30haven't done that yet you'll see that
- 7:47:32name now has a little Cog next to it
- 7:47:34sure only properties this is a new icon
- 7:47:37that we haven't saw yet so this is our
- 7:47:39new icon it says it's going to return us
- 7:47:41back with a string and we can actually
- 7:47:43execute a get and execute a set straight
- 7:47:46on this name then we can say name equals
- 7:47:48Harry and we don't get any compiler
- 7:47:50errors whatsoever this lets us do it
- 7:47:52completely so now if we do another
- 7:47:54console right line and we say person do
- 7:47:56return details and let's run that and
- 7:47:58you should see that the name gets
- 7:48:01changed there we go ABA gets changed to
- 7:48:04hurry perfect so after we've got person.
- 7:48:07name let's see how we can also get the
- 7:48:10value so we can just say string
- 7:48:12interpolation your name is and then we
- 7:48:15do person and followed by a DOT and as
- 7:48:18you can see the exact same list that we
- 7:48:20got given over here is given right here
- 7:48:22as well name also says get and set and
- 7:48:25it returns back a property which is a
- 7:48:27string type so we can open like this and
- 7:48:30you'll notice that we don't have the
- 7:48:31parentheses after it and the reason for
- 7:48:33that is because the brackets indicate
- 7:48:35that you're calling a function but a
- 7:48:37property is just a property it's almost
- 7:48:39like a variable so you're not actually
- 7:48:41needing to put any brackets after it
- 7:48:43because there's no function to call so
- 7:48:45as you can see this is exactly the same
- 7:48:47as this in this case we're actually
- 7:48:49setting the value and in this case we're
- 7:48:52actually getting the value so let's
- 7:48:54comment this line out and let's see what
- 7:48:56happens your name is Harry so that's
- 7:48:59worked okay so now let's do the same
- 7:49:01thing for
- 7:49:02age remove the semicolon add the curly
- 7:49:05brace we can say get and then return age
- 7:49:09and then simply do set and then do age
- 7:49:12equals value there we go so now we got
- 7:49:15that set up if you return back down here
- 7:49:17and we see a person dot so as you can
- 7:49:19see now the age is being removed as
- 7:49:21being a field as we saw in the blue icon
- 7:49:24it's now been turned into a spanner
- 7:49:26which means it's a property as shown
- 7:49:27here show only properties so now you can
- 7:49:30see it returns back an integer and we've
- 7:49:32got a get in a set so we can set that to
- 7:49:3425 for example and then we can append
- 7:49:36onto here and your AG is person. AG you
- 7:49:40see that it's a capital A just as this
- 7:49:42one is a capital N and now we have
- 7:49:44getting set available and again we don't
- 7:49:46need the brackets inside here since this
- 7:49:48is a property not a function now let's
- 7:49:50run it we should see Harry 25 there you
- 7:49:53go your name is Harry and your age is
- 7:49:5625 perfect
- 7:49:58so as you can see now is instead of
- 7:50:00having our set name and get name which
- 7:50:02are these two functions we actually have
- 7:50:04them contained within a
- 7:50:06property we call this property and it
- 7:50:09affects these variables but as you can
- 7:50:11see just as these functions below were a
- 7:50:14middleman we've now moved the middleman
- 7:50:16into these properties so as these are
- 7:50:18only one line you're probably thinking
- 7:50:21there's probably a better way of doing
- 7:50:22this and it's a lot quicker for syntax
- 7:50:24and you're right there is so let's
- 7:50:26explore that so I said before about
- 7:50:28about Arrow functions when you're doing
- 7:50:30something like this get name you can
- 7:50:32have public string get name and then you
- 7:50:35can simply do Arrow name this is a very
- 7:50:37common string function get a name that
- 7:50:40just returns back a value and you can
- 7:50:41make that into an arrow function by
- 7:50:43literally just putting back name so we
- 7:50:45can actually do the same thing but for
- 7:50:46our properties so these both contain one
- 7:50:49line so we can actually minimize this
- 7:50:52and if we just block comment this
- 7:50:54out we can say public string name and
- 7:50:58then we can have get and then the same
- 7:51:00thing with the arrow function when we
- 7:51:02doing Arrow function down here you
- 7:51:04simply won't use the return keyword you
- 7:51:06would just type in the variable you want
- 7:51:07to return so we can say get arrow and
- 7:51:10then simply lowcase name and then add a
- 7:51:12semicolon just so you know that that
- 7:51:14section is ended and then straight after
- 7:51:16we can have set and then arrow and then
- 7:51:19name equals value just like that so as
- 7:51:22you can see these contents are exactly
- 7:51:24the same we've actually returned them
- 7:51:26back in a short space and of course here
- 7:51:28as we got a compile error it just
- 7:51:30expects a semicolon to be right at the
- 7:51:32end here there you go and now we do the
- 7:51:34same thing for our
- 7:51:37age we can do public int ede get arrow
- 7:51:42ede and set Arrow age equals value
- 7:51:46perfect so now you can see that this has
- 7:51:48been drastically reduced if I cut this
- 7:51:50just underneath you'll see that almost
- 7:51:52all of these lines are just replaced
- 7:51:54into a two and you'll see that our
- 7:51:56functionality will still work exactly
- 7:51:57the same if we just run the code Harry
- 7:52:00and 25 is being reassigned over our
- 7:52:02default variables perfect of course now
- 7:52:05if you want to add some more
- 7:52:06functionality inside here for example in
- 7:52:09our set name we had a little check that
- 7:52:11makees sure that it's a valid name
- 7:52:12before we assign it so we actually take
- 7:52:16this and paste it over here then you see
- 7:52:19the lines grown quite a lot so what's
- 7:52:21pred to do is if you're going to have
- 7:52:23any data inside here that does a little
- 7:52:25bit of logic then it's best to keep the
- 7:52:27curly bra is since you can have a bigger
- 7:52:30output and it looks a bit more sensible
- 7:52:32if you're just simply getting and you're
- 7:52:34setting the variables then you can leave
- 7:52:35this notation so let's undo this and
- 7:52:38bring back the original ones in this
- 7:52:43case there we go and then we can take
- 7:52:45that line and in the set we can actually
- 7:52:47replace it here so as you can see this
- 7:52:49is a little bit grayed out so let's put
- 7:52:51the semicon at the end and let's see why
- 7:52:53this is grayed out name can be
- 7:52:55simplified so what we're doing now is
- 7:52:57name and all of these variables are
- 7:52:59actually looking at the private variable
- 7:53:02whereas if you actually look down here
- 7:53:03and we uncomment this you'll see that
- 7:53:06set name if we double click on this one
- 7:53:08this is the only reference to it which
- 7:53:11is our private variable up and the
- 7:53:13reason why this doesn't need to be used
- 7:53:15in this case is because our parameter is
- 7:53:18now called
- 7:53:21value so it's saying the name is equal
- 7:53:24to checking if the value is null or
- 7:53:26empty assigning the value into name if
- 7:53:29not and then if it is empty then we hide
- 7:53:32invalid straight into name in the case
- 7:53:34of our set name our name was our
- 7:53:37parameter the same goes here if we
- 7:53:39change this to Value we can change these
- 7:53:41two for Value as you can see now this
- 7:53:43dot is actually grade out and it can be
- 7:53:45simplified to just simply name and the
- 7:53:47same principle applies above so let's
- 7:53:50comment these back out and scroll back
- 7:53:52to the top so now that we've assigned
- 7:53:54name there we go we've got our check
- 7:53:57inside there and you can also leave this
- 7:53:58on one line if you want you can even
- 7:54:00drop the curly brace in just so it looks
- 7:54:02like this instead of having it all on
- 7:54:04one line so you could do this and this
- 7:54:07would be applicable as well but the
- 7:54:09second you start to put it all on one
- 7:54:11line then it gets a little bit messy
- 7:54:13quickly so if you leave the outer
- 7:54:15bracket just like that then it can be
- 7:54:17pretty short since you don't need more
- 7:54:19curly braces so now that we've done that
- 7:54:21for the name let's do it for the age as
- 7:54:23well so we had this. age we check the
- 7:54:26range and we assign the relevant values
- 7:54:28so let's take that paste it over this we
- 7:54:32don't need this dot anymore and age in
- 7:54:34this case is actually going to be the
- 7:54:36value gets passed
- 7:54:37in to replace all the ages with value
- 7:54:41there we go so if the value passes the
- 7:54:43range check we put the value inside age
- 7:54:46or not we can put minus one inside age
- 7:54:49and again we can have the same thing we
- 7:54:51can use our arrows inside here there we
- 7:54:54go so now we've got our Cod drastically
- 7:54:57reduced so we went from these big
- 7:54:59functions that have to be defined with
- 7:55:01specific names over to our properties
- 7:55:04and they went from really small to just
- 7:55:05a little bit bigger but now they have
- 7:55:08advanced connections just to make sure
- 7:55:10that the variables are okay before you
- 7:55:12assign it in so now that we've got our
- 7:55:13properties let's have a look so inside
- 7:55:15our Constructor we don't also need to
- 7:55:18use our local variables which is our
- 7:55:20private ones directly we should actually
- 7:55:23get rid of this now and we should always
- 7:55:25do everything through our properties
- 7:55:27name equals name and because this is a
- 7:55:29capital and this is a lower case we
- 7:55:31don't need this dot in
- 7:55:33there there we
- 7:55:35go so what you should do in the return
- 7:55:38details function is also use the capital
- 7:55:40n and the capital age so when you hover
- 7:55:42over these you're actually referring to
- 7:55:44the property because like I said before
- 7:55:47these are the middleman to communicate
- 7:55:48with the private variables you shouldn't
- 7:55:51need to access the private variables
- 7:55:53directly you should use the middle man
- 7:55:54as much as possible so now that we've
- 7:55:56done that I have one last thing to
- 7:55:58introduce so if we actually abandon all
- 7:56:00of this code just to keep it for
- 7:56:02reference we have something called an
- 7:56:03auto property and you briefly saw it
- 7:56:06when we saw it down here so if we
- 7:56:08actually just bring them back for a
- 7:56:11second and if you hover over a name down
- 7:56:13here it says string name get and set in
- 7:56:16the curly brace but you see how the get
- 7:56:18and set is empty it just has two values
- 7:56:21inside there and it just ends a curly
- 7:56:23brace with no actual logic you see in
- 7:56:25our get we're saying age and our set
- 7:56:28with saying age equals this so when
- 7:56:30we're doing Auto properties what you can
- 7:56:32do is you can actually make these public
- 7:56:37again change these to a capital N
- 7:56:39changes to a capital A and just straight
- 7:56:42after it you can say Get Set just like
- 7:56:47that and what that will do is although
- 7:56:49they're actually made public what it's
- 7:56:51doing behind the scenes is actually
- 7:56:53doing this for you it actually has a
- 7:56:56private and a public for you behind the
- 7:56:59scenes this is just a Shand these are
- 7:57:01what's called a auto property what it
- 7:57:04does is it makes an anonymous property
- 7:57:05behind the scenes as I'll show you in
- 7:57:07Microsoft documentation right now as it
- 7:57:10says here in c three or later Auto
- 7:57:12implemented properties make property
- 7:57:14declaration more concise when no
- 7:57:16additional logic is required and what
- 7:57:18that means is no additional logic all of
- 7:57:22these are additional logic if you're
- 7:57:24just simply assigning a value and
- 7:57:26reading it back just like here then this
- 7:57:29is no need to write get name and set
- 7:57:32name if all you're doing is getting and
- 7:57:33setting it if you're getting and setting
- 7:57:35it but there is a certain amount of
- 7:57:36logic as we have here we essentially
- 7:57:38have a conditional operator that's
- 7:57:40making a subconscious decision about
- 7:57:42what to assign the name to but if you
- 7:57:44just simply assign in something like
- 7:57:46this then an auto property is perfect
- 7:57:49they also enable client code to create
- 7:57:51objects when you declare a property as
- 7:57:53shown in the following example the
- 7:57:54compiler creates a private Anonymous
- 7:57:57backing field that can only only be
- 7:57:58accessed to the properties get and
- 7:58:01sets as you can see here just like in
- 7:58:03our example we have public int something
- 7:58:06and then get in set and then they have a
- 7:58:09public in something with the get in set
- 7:58:10as well and they can use it just here to
- 7:58:13access it
- 7:58:14directly and because they're simply just
- 7:58:16assigning and getting the values and
- 7:58:18there isn't any additional logic then
- 7:58:20this is the best way to do it and you
- 7:58:22get as it says here an anonymous private
- 7:58:24backing field so this would be the same
- 7:58:27thing as doing this private string name
- 7:58:31and private int age so once you do that
- 7:58:34and you make this into the get and set
- 7:58:36properties which are these two right
- 7:58:41here this and this line together make a
- 7:58:44property what the auto implemented
- 7:58:47property does is actually combine this
- 7:58:49line and this line together and make the
- 7:58:51private one behind the scenes so the
- 7:58:53programmer doesn't see it but it will be
- 7:58:55applicable when the program runs in the
- 7:58:56compiler and then you only get publicly
- 7:58:59exposed with this parameter which will
- 7:59:01be our name property so it will actually
- 7:59:04make the private for you if you just
- 7:59:06keep it like we had it a second
- 7:59:08ago just like this so if you leave it
- 7:59:11like this it will actually create the
- 7:59:13private variable behind the scenes as it
- 7:59:15says here it's Anonymous backing field
- 7:59:17and it can only be accessed through name
- 7:59:20and Edge and as you can see none of our
- 7:59:22code has actually changed all of these
- 7:59:25still function okay and if we run the
- 7:59:26code we should still get Harry and
- 7:59:2925 so just to sum this video up since
- 7:59:31there was a lot covered the bottom line
- 7:59:33is if you're going to have any sort of
- 7:59:36values that require any logic then you
- 7:59:38should use normal properties if you're
- 7:59:40simply just going to assign and get the
- 7:59:42value and it doesn't matter and you have
- 7:59:44no control over it then you can use the
- 7:59:46auto implemented properties since that
- 7:59:48will do the same thing with less code
- 7:59:50using the gets and sets is the old way
- 7:59:52of doing this and you should always use
- 7:59:54the properties going forward gets and
- 7:59:56Setters are very helpful to know just so
- 7:59:58you understand what they are and why
- 8:00:00they exist but you should note that
- 8:00:02properties are the best way forward
- 8:00:04properties give you access to doing the
- 8:00:05same logic as it says inside here if you
- 8:00:08do an auto implemented properties then
- 8:00:10you're not allowed to use the logic
- 8:00:12inside the set or the get so if you
- 8:00:14don't want to do any logic then don't
- 8:00:16use the auto implemented properties only
- 8:00:18use the normal properties to make a get
- 8:00:20and Setter you just do set name get the
- 8:00:22parameter and assign the local variable
- 8:00:25and the get just returns it getting back
- 8:00:27a property you want to capitalize your
- 8:00:29private variable if you had private
- 8:00:31string name with a lowercase n you want
- 8:00:33public string name with a capital N you
- 8:00:36get the name and then you can set the
- 8:00:37name with some sort of conditional
- 8:00:39operator or any if statement or anything
- 8:00:41inside here and the same thing goes with
- 8:00:43the age you can have a public int age
- 8:00:45with a capital A and A Private in age
- 8:00:47with a lowas A you can get the age and
- 8:00:50then set the age with a conditional
- 8:00:51operator again or any other code if you
- 8:00:54don't require any logic like these two
- 8:00:56lines indicate then you can simply just
- 8:00:57just use the auto properties as
- 8:00:59demonstrated above here and they can be
- 8:01:01used quite easily throughout the
- 8:01:03Constructor and our functions and even
- 8:01:05outside the function when the user is
- 8:01:07trying to call
- 8:01:09them but one thing that we haven't been
- 8:01:11able to solve and you may not have
- 8:01:13noticed this as a nuisance the problem
- 8:01:15is when you try to return details about
- 8:01:17something we're having to call this
- 8:01:19function every single time if we have an
- 8:01:21in age for example made that equal to 23
- 8:01:24and we did console right line and simply
- 8:01:25placed age inside the brackets we don't
- 8:01:27have to do anything like this because it
- 8:01:29simply just prints the value out if we
- 8:01:31run the code you'll see that it simply
- 8:01:34just says 23 this should be pretty
- 8:01:36simple for our person as well but it's
- 8:01:39not we need to call return details every
- 8:01:41single time we want to print the
- 8:01:43properties and if we don't do this the
- 8:01:45only way to do it is by making some
- 8:01:47string interpolation and we can say
- 8:01:49person.
- 8:01:51name and then also include person. AG
- 8:01:54Etc and then that can print out to the
- 8:01:56screen just like that
- 8:01:59of course it doesn't have the same
- 8:02:00format in but we can also just take this
- 8:02:03place it straight inside here add our
- 8:02:05person dot before them person Dot and
- 8:02:08there you go then you get exactly the
- 8:02:10same output as Callum return details but
- 8:02:13of course you don't want to do this
- 8:02:14every time and you certainly don't want
- 8:02:15to have to type in return details
- 8:02:17function every single time as well you
- 8:02:19want to keep it as just person now of
- 8:02:21course this is not going to work
- 8:02:23Straight Out of the Box let's try run
- 8:02:24that and see what happens so we get
- 8:02:26string override person and what's going
- 8:02:29on here is it's saying names space is
- 8:02:31string override and then it's a class
- 8:02:32person so it's just printing out this
- 8:02:34person object but it's not really doing
- 8:02:36anything because we haven't really told
- 8:02:38it to do something so what we can do
- 8:02:40here is we can actually override the two
- 8:02:42string method so let me explain so let
- 8:02:44me set up a couple of examples just to
- 8:02:47show you what's going to happen at the
- 8:02:48end so right now we call return details
- 8:02:50to return these details what we can also
- 8:02:52do is say dot two string and then we can
- 8:02:55also do these three options return the
- 8:02:58details convert it to a string or just
- 8:03:00print the person straight to the console
- 8:03:02at the end of this little section we're
- 8:03:04going to make all three of these print
- 8:03:05out the same values so let's have a look
- 8:03:07now two string and printing out normally
- 8:03:11these two lines right here are actually
- 8:03:13doing exactly the same thing behind the
- 8:03:15scenes when you place anything inside
- 8:03:17these brackets inside a console right
- 8:03:18line it will try and convert it to a
- 8:03:20string anywhere so whether you put two
- 8:03:22string or you leave it as person the
- 8:03:24console right line function will still
- 8:03:26accept it so just coming out the age for
- 8:03:28the time being in order to make the two
- 8:03:30string function work as well as just
- 8:03:32placing person inside the brackets we
- 8:03:34need to make a special function which is
- 8:03:36an override function so let's have a
- 8:03:38look so just underneath our return
- 8:03:40details still within the scope of the
- 8:03:42class we say public and then override
- 8:03:45after we press tab we can follow it by a
- 8:03:47space and we can see the functions that
- 8:03:49we can override so in this video we're
- 8:03:51actually going to cover two string and
- 8:03:52do equals get hash code will actually
- 8:03:55serve as a default hash function if I
- 8:03:57show you the the documentation it says
- 8:03:59here a hash code is numeric value that
- 8:04:01is used to insert and identify an object
- 8:04:03in a hash based collection this is
- 8:04:05normally use when you're doing hash
- 8:04:07functions as it says here and for the
- 8:04:09time being we're not actually going to
- 8:04:10be using this right now so the two we
- 8:04:13can do a two string and equals but as
- 8:04:15you can see our two string is available
- 8:04:17right now we'll cover equals as an
- 8:04:19extension in a minute so let's double
- 8:04:21click on our two string and we get this
- 8:04:23little template placed in if you don't
- 8:04:25want the template then it's just an
- 8:04:26override if returns back a string since
- 8:04:28it's converting to string it needs to be
- 8:04:31this exact notation if you drop the T
- 8:04:33then it's got nothing to override now
- 8:04:36you go be a compile error because
- 8:04:37there's nothing to override itself so
- 8:04:40play two string in the capitals there's
- 8:04:42no parameters passed in and then we're
- 8:04:43going to be returning something so the
- 8:04:46thing that we're going to be returning
- 8:04:47is exactly the same as our return
- 8:04:48details function we actually have two
- 8:04:50options here we could say return and
- 8:04:52then return details so what that will do
- 8:04:55is it will go to this function it will
- 8:04:57call this function grab that and
- 8:04:59essentially place inside here and then
- 8:05:01return it back to the console in the
- 8:05:03ideal world we don't actually need this
- 8:05:04return details function so we could
- 8:05:06comment it out but I'm going to leave it
- 8:05:08in just so the demonstration doesn't
- 8:05:09break down here so we can take this and
- 8:05:12place it straight inside here now we
- 8:05:14have two functions that essentially do
- 8:05:16the same thing but the advantage is this
- 8:05:18function is something that needs to be
- 8:05:19called explicitly do return details
- 8:05:22whereas this function the two string
- 8:05:24override actually just gets called
- 8:05:26naturally without having to add any
- 8:05:28extensions so if we run the code now
- 8:05:31you'll see that now they all equal
- 8:05:32exactly the same value we get three
- 8:05:34sections that have this maybe we can
- 8:05:37place back SLT instead just so we can
- 8:05:39have them all in the same
- 8:05:41line there we go so you can see all
- 8:05:44three are exactly the same so what we
- 8:05:47can do now is if we comment this option
- 8:05:49and we comment out this function we
- 8:05:51don't have to include two string if we
- 8:05:52don't want to we can also comment this
- 8:05:54one out and we can keep it just as
- 8:05:56person so as we do when we declare the
- 8:05:58integer we say integ equal 23 and then
- 8:06:00we simply print out and this is exactly
- 8:06:03the notation that we want it should
- 8:06:05really be this simple there you go one
- 8:06:07line creates the person and one line
- 8:06:10with just a variable name goes inside
- 8:06:12the brackets to print them out the same
- 8:06:13thing here person person Edge and Edge
- 8:06:16and let's run that code now and there
- 8:06:18you go you get the person printed and
- 8:06:20the edge printed exactly and
- 8:06:22straightforward as you may think it
- 8:06:24should be so now that we've done that
- 8:06:25let's explore what the equals function
- 8:06:27needs to be done let's make another
- 8:06:29person we can say person test equals new
- 8:06:32person and we can have hurry age
- 8:06:3423 then we don't need to print them out
- 8:06:36in this example we just need to
- 8:06:38demonstrate this equals function so if
- 8:06:40you remember with strings we can have a
- 8:06:41string called abber for example and we
- 8:06:43can say do equals straight after it we
- 8:06:46can say if ABA is equal to hurry then we
- 8:06:48can do something inside here what I'm
- 8:06:50trying to demonstrate is once you've got
- 8:06:52an object you can place do equals and
- 8:06:54then you can compare it like that so you
- 8:06:56can also do that without classes so we
- 8:06:58can say person do equals and then equals
- 8:07:01to test and in here we could just say
- 8:07:04same let's try and make these as same as
- 8:07:06possible and see what happens so let's
- 8:07:08keep the name exactly the same just like
- 8:07:11that and let's copy the 25 down just
- 8:07:13like that so now these two are
- 8:07:15instantiated exactly the same way let's
- 8:07:17see what happens when we've run the
- 8:07:19equals then we don't get anything
- 8:07:21printed which means they're not the same
- 8:07:23so because this doesn't automatically
- 8:07:25tell us that these two are exactly the
- 8:07:26same we can override that and provide
- 8:07:28our own functionality just like we did
- 8:07:30with the operate overload we override
- 8:07:33equals we get passed in an object and we
- 8:07:35need to check something inside it so
- 8:07:37let's see what we can do inside here so
- 8:07:39in order to check if something is equal
- 8:07:41to another thing what can we do so we
- 8:07:43have a name and age if this one is the
- 8:07:46same as this and this is the same as
- 8:07:48this then two people are exactly the
- 8:07:50same so let's try that out and see what
- 8:07:52happens so we can say name which is the
- 8:07:55property inside the class that we're
- 8:07:56currently inside
- 8:07:58and then we can use equals because this
- 8:07:59is just a string parameter and then we
- 8:08:02can say that equals to
- 8:08:04objectname but you can see we've got a
- 8:08:06little problem here this object is very
- 8:08:09generic it's just an object every class
- 8:08:12that you create is derived by an object
- 8:08:15what that means is every class or
- 8:08:17integer or double at the end of the day
- 8:08:19is also just known as an object through
- 8:08:21inheritance which we'll discuss further
- 8:08:23on this course but just know that
- 8:08:25everything inside C is always an object
- 8:08:27object somewhere down the line so what
- 8:08:29that means is we have to convert it from
- 8:08:31an object to our custom class which is
- 8:08:33person in this case so we can use this
- 8:08:36thing called casting so we can say
- 8:08:38person person which is just to find a
- 8:08:40new variable and we can make that
- 8:08:42immediately equal to object as
- 8:08:46person so what we've done here is we've
- 8:08:48said our object which is this parameter
- 8:08:51is currently an object right now we want
- 8:08:53to cast it as a person which is kind of
- 8:08:55like converting into a person and then
- 8:08:57play placing the values inside here so
- 8:08:59now if we get rid of this comment and
- 8:09:01instead of using object we actually have
- 8:09:02a new local variable called person so
- 8:09:05then here we can say person. name there
- 8:09:07we go now we have access to the person
- 8:09:09property since we've converted this
- 8:09:11object into a person and of course we
- 8:09:13know that we have name and age
- 8:09:15available so if the name is equal to the
- 8:09:19personname and the age is equal to the
- 8:09:22person.
- 8:09:24AG then we must have a match and of
- 8:09:26course want to keep using the properties
- 8:09:28we're going to use a capital A and there
- 8:09:30we go so we run the conversion on one
- 8:09:32line and then we check if they're equal
- 8:09:34to each other now in this specific case
- 8:09:36you can actually pass anything inside
- 8:09:38here so let's try it we can pass Five
- 8:09:40inside here and as you can see there's
- 8:09:42no compile error cuz like I said five is
- 8:09:45just an integer which is an object at
- 8:09:47the end of the day if we make it a
- 8:09:49double that works as well there is
- 8:09:50literally no compile errors and even if
- 8:09:52you put M to make it into a decimal
- 8:09:55again there's no compile errors because
- 8:09:56they're all object at the end of the day
- 8:09:58so let's pass test in there what you
- 8:10:00will have to know is if you do
- 8:10:01accidentally pass in a double for
- 8:10:03example like this you're going to hit an
- 8:10:05error here which will be a runtime error
- 8:10:07it will try and convert it as soon as it
- 8:10:08hits this line and obviously you can't
- 8:10:10convert a double into our person so what
- 8:10:13you could do just to wrap it around you
- 8:10:15could say if the object is a person then
- 8:10:19we can proceed with a conversion just
- 8:10:21like this and then what you can do for
- 8:10:23safety is just outside of this you can
- 8:10:25actually just return back false just so
- 8:10:28the user knows that this isn't equal to
- 8:10:30each other so if the object is a person
- 8:10:33then we convert it as a person is is
- 8:10:35used for checking and as is used for
- 8:10:38converting is this object a person and
- 8:10:41if it is then we try and convert it and
- 8:10:44then return back if they're equal to
- 8:10:45each other and if we accidentally pass
- 8:10:47something in that's not a person then we
- 8:10:49just return back false so let's just
- 8:10:51demonstrate this
- 8:10:52first not the same so let's just save
- 8:10:55five inside here it says not the same so
- 8:10:59let's actually go into this place a
- 8:11:01break point and let's run it
- 8:11:02again hover over object then we get
- 8:11:06five if we place this straight inside
- 8:11:09the watch you can see object is person
- 8:11:11is returning back false so it's skipping
- 8:11:13this entire function and going into
- 8:11:15return false and then it's returning
- 8:11:17back false so we get not same let's stop
- 8:11:20the program change this over to test and
- 8:11:23rerun it using F5 and now we can see
- 8:11:25object is person is is actually set to
- 8:11:27true so now if you put the object inside
- 8:11:30here you can see that generically it's
- 8:11:33an object right here it says object so
- 8:11:36what we can do now is we can place
- 8:11:38person down here as well and you see
- 8:11:40that person doesn't exist but if you
- 8:11:42just give it a second and we change this
- 8:11:43to just the
- 8:11:46P there we go so obviously the person
- 8:11:48doesn't exist cuz we're only up to this
- 8:11:49line we press F10 and F10 again so now
- 8:11:53it's created the variable and f101 so
- 8:11:55skip over it and now you can see the
- 8:11:57object has went from type object to type
- 8:12:00string. override. person string override
- 8:12:02is the name of our namespace and do
- 8:12:05person with a capital P is the name of
- 8:12:07our class now you can see that the
- 8:12:09object and the values are exactly the
- 8:12:11same 25 ab and 25 ABA as you can see
- 8:12:14right here so now both of them are
- 8:12:16exactly equal to each other so when we
- 8:12:18check this name here is Aba name here is
- 8:12:22ABBA age 25 and age is 25 so this must
- 8:12:26return back true and we can check that
- 8:12:28by highlighting everything dragging and
- 8:12:30dropping it down here and we can see it
- 8:12:31says true there so when it returns back
- 8:12:33to our code it's going to go and print
- 8:12:35their
- 8:12:37same perfect there you go so that's it
- 8:12:40for this video but let's just have a
- 8:12:41recap before we wrap up so before this
- 8:12:43video we'd always print the details out
- 8:12:45like this person do return details
- 8:12:47obviously we don't have that function
- 8:12:48anymore so let's just bring it back
- 8:12:50quickly there we go so we use return
- 8:12:53details and this is just a bit long have
- 8:12:55to do this every single time if if you
- 8:12:57want to print the details of the test
- 8:12:58object as well you have to do test Dot
- 8:13:00and then return details again it'd be so
- 8:13:02much easier if we could simply just use
- 8:13:04it like this as you would do with a
- 8:13:06normal variable like an integer double
- 8:13:08string Etc and now it's made possible by
- 8:13:11using our public override string two
- 8:13:13string function it will contain exactly
- 8:13:15the same contents as our previous
- 8:13:17function except we won't have to go
- 8:13:19through an additional function just to
- 8:13:20print out the details we can override
- 8:13:22the two string and then we can call it
- 8:13:24straight in the console like this and
- 8:13:25finally when you're doing equals inside
- 8:13:27an object you can actually build in some
- 8:13:30custom functionality we can convert the
- 8:13:32person using object as person but first
- 8:13:35before doing the conversion you want to
- 8:13:37check if the object is indeed a person
- 8:13:39type before doing the conversion this
- 8:13:42will give you a runtime error on this
- 8:13:44line so let me just demonstrate that
- 8:13:45before we continue so if I comment out
- 8:13:48this one and actually just change this
- 8:13:49into
- 8:13:515D and then let's press into the chorde
- 8:13:54right
- 8:13:55here it's going to hit this line if I
- 8:13:57press F10 then it's going to try and
- 8:13:59convert it but you're going to get back
- 8:14:00null and then if you run the code again
- 8:14:03then you're going to exception thrown
- 8:14:04here you're going to get a runtime
- 8:14:06exception because it's a null reference
- 8:14:08we're trying to run the equals function
- 8:14:10on something that is null which is right
- 8:14:12here null reference exception so you
- 8:14:14shouldn't do this you should also make
- 8:14:16sure that these are correct values
- 8:14:18before doing it this is a form of error
- 8:14:20checking if you don't do this then
- 8:14:22you're going to have to wrap this inside
- 8:14:24a TR catch and it will look something
- 8:14:26like this and and the cord will be
- 8:14:27considerably longer you can place these
- 8:14:29two
- 8:14:30lines inside here and then place your
- 8:14:33cat inside here etc etc and you can
- 8:14:36return false inside here but as you can
- 8:14:38see the code is a bit more complicated
- 8:14:40now and there's no reason for this extra
- 8:14:42complication if you can prevent it with
- 8:14:44an if statement so let's undo
- 8:14:46that so what's best in this case is to
- 8:14:49say if the object is of type person then
- 8:14:52we can run the conversion if we run the
- 8:14:54conversion and it's not of type person
- 8:14:56then as you can see it's going to return
- 8:14:57back null for the person and then when
- 8:14:59you run this line it's actually going to
- 8:15:01return you back a null reference
- 8:15:02exception at this point but anyway the
- 8:15:04whole point of this equals is to check
- 8:15:06if two objects are going to be equal to
- 8:15:08each other so we keep this back to test
- 8:15:10because this is a custom classes you
- 8:15:12can't just simply do person equals
- 8:15:14equals test and assume that that will
- 8:15:16just work these aren't buil-in
- 8:15:18functionalities for custom classes we
- 8:15:20covered operate overloading where you
- 8:15:22can actually overload what you do with
- 8:15:23the equal sign but we didn't cover the
- 8:15:25do equals which is what we're doing
- 8:15:27today in this video as well as the two
- 8:15:30string this is a different kind of
- 8:15:32overriding because in the other one when
- 8:15:33we're doing operator overloading that's
- 8:15:35specifically an operator that you're
- 8:15:37just changing the parameters of whereas
- 8:15:39this one is a strict override of a
- 8:15:41function equals and the functions two
- 8:15:43string this will make it easier to print
- 8:15:45your custom classes out to the screen as
- 8:15:47well as comparing them just like this
- 8:15:49person. equals putting test inside the
- 8:15:51bracket is a lot easier and quicker and
- 8:15:53you can build in your own custom
- 8:15:55functionality if you want only had the
- 8:15:57age in there then you can compare the
- 8:15:58age maybe you have a lot more properties
- 8:16:00and you can always just chain them
- 8:16:02inside a big if statement that's fine as
- 8:16:04well but the point is you have full
- 8:16:05control over how you compare your custom
- 8:16:08classes maybe you had a list of names
- 8:16:10inside a dictionary for example and you
- 8:16:12wanted to add a new name or a new age or
- 8:16:15a new user to this dictionary but first
- 8:16:18before you add a new user to the
- 8:16:19dictionary you want to check if the user
- 8:16:21already exists so maybe you could have a
- 8:16:23system where it uses the equals function
- 8:16:25and it Loops through all of the
- 8:16:26dictionary items and it checks if all
- 8:16:28the values are not equal to this one if
- 8:16:30you didn't build in this functionality
- 8:16:32so you could do the equals function then
- 8:16:34you won't be able to determine if this
- 8:16:36one is equal to this one and now with
- 8:16:39your equals function overrided you can
- 8:16:41now have the functionality to do so
- 8:16:44thank you for watching this C course I
- 8:16:46hope you found it helpful in your
- 8:16:47journey to becoming a proficient C
- 8:16:49developer if you have any questions or
- 8:16:51need further clarification on any topic
- 8:16:52we have covered please don't hesitate to
- 8:16:54leave a comment below and I will do my
- 8:16:55best to assist you for those of you who
- 8:16:57want to dive deeper in a cop consider
- 8:16:59checking out my udemy course you'll find
- 8:17:01more comprehensive lessons and Hands-On
- 8:17:03exercises to enhance your skills
- 8:17:05remember practice makes perfect keep
- 8:17:07coding and exploring and I wish you all
- 8:17:08the best on your programming Journey
About this transcript
This page contains the full transcript of Learn C# Programming – Full Course with Mini-Projects by freeCodeCamp.org, generated from the public captions YouTube serves with the video. The transcript has 102,522 words across 13,636 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.