All language subtitles for Learn C# Programming – Full Course with Mini-Projects (English_ASR)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

this comprehensive C course will take

you from the basics of C programming to

advanced concepts all while integrating

exciting mini projects to solidify your

learning this course offers a structured

path through essential topics such as

syntax object oriented programming data

structures and more ABA teaches this

course and he will help you understand C

both theoretically and also gain

practical experience in applying your

knowledge to real world scenarios hey

there free ABA here join me in Walking

you through everything you need to get a

setup with cop let's have a quick walk

through the course contents to let you

know what to install for you we'll start

with installing the visual studio

program required to develop C

applications you'll learn about data

types including integers strings and

many more and how to manipulate them

inside your code next we'll dive into

control flow you'll discover how to use

conditional statements and Loops to

control the flow of your program's

execution strings are essential in many

applications you'll Master powerful

string functions in C allowing you to

manipulate and transform Text

effortlessly data structures are crucial

for organizing and managing data from

arrays to collections you'll explore

various data structures in C and learn

how to use them effectively functions

are the backbone of any program you'll

uncover the power of functions in C

including how to create and call them to

organize your code no program is immune

to errors you'll learn how to handle

exceptions gracefully in C ensuring that

your applications remain robust and

reliable debugging is a crucial skill

for any developer you'll discover how to

use visual studios's powerful debugging

tools to identify and fix bugs in your C

code efficiently finally we'll delve

into object-oriented programming you'll

learn the principles of object-oriented

programming how to use classes objects

inheritance and polymorphism to create

modular and maintainable code by the end

of this course you'll have a solid

understanding of C programming and be

well equipped to tackle a wide range of

software development challenges thanks

for tuning in let's get

started so in order to start developing

with C we need to install the latest

version of visual studio in this case

it's currently 2022 so let's head over

Google and type in Visual Studio 2022

download we will be presented with two

links we have this one and this one we

want to use a second one which says

download visual studio tools so if you

press on that and now will present it

with the download area we have three

additions Community professional

Enterprise Community is free for

students open source contributors and

individuals which is exactly what we

need for the content of this course we

have the professional Enterprise Edition

which paed for and only used by bigger

companies or smaller teams so let's go

ahead and press free download the

download will start straight

away after the download is complete

let's start the installer so double

click on the executable and press yes to

allow it for admin permissions it'll pop

up like this Visual Studio installer

before you get started we need to set up

a few things you can configure your

installation you can read the privacy

policy in the software terms if you'd

like or you can just press continue so

it's getting the visual studio installed

already so so when you get to this

section everything here is all the

different packages and tools you can

install within Visual Studio 2022 the

main one that we're concerned within the

content to this CA is the neck desktop

environment so you can go ahead and

press this one it will then tell you in

the bottom right hand corner the total

space required is 6.52 GB please make

sure you have enough storage space

available for this and then you can go

ahead and press install if you want to

check out other packages you can have a

scroll through and read any other

packages you may need and may want to

install like I said the the only one

required for this course is the net

desktop

environment you can also deal with noj

development python development web

development if you need to deal with

individual components like specific

versions you can access it here if you

need some more languages installed you

can access it here this should

automatically select your default

language for you that's based on your

systems language and if you want it to

you can also change the installation

location in case you want to install it

on a different drive or you just want it

in a different folder so now that we

selected the neck desktop environment

we can go ahead and press the

installation button we do have two

options down here we have it install

while downloading and a download all

then install if you have a reasonably

fast computer you can stick with the

install while downloading and it will do

the installation and the downloading at

the same time if you have a slow

computer it's preferred that you use

this option to download it first and

then install it just means that it won't

slow down your computer too much so you

can go for this one and press install

now the installation will begin so now

that Visual Studio is finished finish

installing it will say done installing

Visual Studio has been successfully

installed we recommend rebooting soon to

clean up any remaining files so this

just means restart on your computer just

so it has a chance to clean up any files

that were kept during the installation

process so we can just press okay if you

have any other the versions of Visual

Studio installed they'll also show up

down here and now that Visual Studio

Community 2022 is installed we can just

press

launch so now it'll prompt you to sign

into Visual Studio we should definitely

sign in but just for now I'm going to

skip but feel free to create an account

or sign in if you already have one

existing we can skip this and then we

get to choose your theme I personally

prefer the Dark theme but you can go

with blue blue extra contrast or the

light mode so let's select dark and hit

start Visual Studio it will do a first

time

setup and it's already done and we're

already inside Visual Studio 2022 so

that's it for the visual studio

installation so now that we got Visual

Studio 2022 installed let's see how to

create your first project so once you've

opened Visual Studio 2022 you'll see

this window popping up you can clone a

repository from things like GitHub in as

you devops or open a project that you

already have or open a folder which may

have projects inside or you can create

one from scratch if you don't want to do

any of these options you can just open

the visual studio program just on its

own by clicking continue without code so

let's go down the create a project path

so click on create a new project and

we'll get a list of all these different

projects inside our system to be able to

use the ones we're going to be be

focusing on on this course is going to

be C in the languages dropdown box and

the platform is going to be windows and

the project type is going to be console

and they have one that uses net core

which is for Linux Mac and windows and

one that uses the Net Framework which is

Windows so we want to go for this one so

press on this one and then press

next here we have our project name our

location and our console app so we can

start with the project name and say my

first

project as you can see it'll start

copying over to the solution name as

well and it tells you where the project

will be created into so I'll be creating

a folder called my first project which

is the solution name plus my first

project which is the project name inside

that folder if we change the solution

name you'll see update you can see here

now we have multiple T's because we

appended the solution name the solution

name is almost like a folder that holds

lots of projects inside so to begin with

we'll have one solution that has one

project inside and then later on in the

course if you feel like you want to add

more projects to it you can just add

more to this solution so let's get

started and press

create so now we've created our very

first project and just to explain what's

here we have a static main void which is

the main entry point to our program if

you do a lowercase M then the system is

not going to recognize this main

function because the down NET Framework

requires this function to be found in

order to run a c console app and you can

see here in the errors the program

program does not contain a static main

function and you can see it's capital M

and C programming language is K

sensitive so make sure to include it so

now you understand how to use Visual

Studio 2022 to create a project and also

know where the main entry point is in C

let's get started with a Hello World

example to kick off this

course so as we know our main entry

point to our application is the static

void main if we go inside here and press

enter then we can actually start writing

some code inside here just a quick note

before we

continue static void main is inside the

class program and the class program is

inside the namespace hello world and the

reason why you can tell this is because

of the curly braces the curly braces of

hello world begins here and ends here

and Within These curly braces we have a

class program and also within the class

program we have the styo

men so just between these curly braces

we want to type console. right line or

open the brackets and we can type in

some speech marks and we can simply Type

Hello World there you go so as you can

see now system has actually been

highlighted so if we just delete this

line you can see now system has been gr

out and the reason why is it says it's

unnecessary because it hasn't been

used so if you undo it then this comes

up because I showed you before that

console right line is actually within

the system Library what happens if we

delete

it if we delete it then it doesn't know

where console exists from and because

that just said system we can actually

just use system. console. right line and

then open the brackets and type in hello

world and now this works again and the

reason why this works it's because we

don't have to Define that we're using

system we could just use it straight in

here if you have multiple lines like

this then you don't want to be using

system right at the start ideally you

want to have the reference to it at the

top of the screen and then use it

throughout so now that we've included

back in the project you can see that now

these have turned gray and it says that

it can be simplified if we press the

little light bulb and we can say

simplify member and it doesn't really

matter because we can just delete the

rest now so now that we've WR hello

world let's press F5 which is a shortcut

to the start

button as you can see well you couldn't

really see because the program closed

too quickly and the reason for that is

printed hello world to the console and

then it didn't do anything else cuz

there's nothing else for it to do so the

console simply

closed we can use another command called

console read Line open the brackets and

close the brackets and have a semicolon

at the end all this does is it waits

here until the user inputs something

reads the next line of characters from

the standard input

stream so although the program will

actually be reading stuff we just want

this line to stop the application from

closing so now if you press F5 and run

the program you can see it actually says

hello world and it's just waiting here

flashing and this flashing means it just

started this console read line so it

doesn't matter what we type into here it

just WS for us to press enter and it

soon as we press enter this line

executes and the program finishes

congratulations you just wrote your

first C program welcome to the amazing

journey of

programming so let's look at how to make

VAR Ables in our C code let's start

every code with a console read line and

then we can hit F5 just to make sure our

console doesn't

close so let's begin with an integer so

integers are whole numbers so we can

just say int age equals

23 so what we've actually done here is

we've said we want an INT we want to

call it age already made it equal to 23

this is called initializing and

declaring in the same line

so what you could do alternatively is

you can end the semicolon here and it

just says int Edge and then on the next

line you can write ede equals 23 so this

is declaring it in the first line and

then give it an initial value in the

second line if they're right underneath

each other like this case then you may

as well just make it together because

then you have less lines of code the

reason why it says it's a green under

line it's because it's the unnecessary

assignment because we're not actually

using ede so let's just point age to the

console if we make some new lines and we

can type in in console. right line and

then press tab to finish it we can write

Edge and if we run our code using F5 we

can see that 23 gets print out to the

screen so now that we've worked on the

Edge Let's explore all data types let's

define a long variable so let's start

with the type long followed by space and

then the variable name that we want and

let's add equals and type in a very big

number so now if we hover over it it

says it's a system int32 and int32 is

actually an integer but we're telling it

it's a long which makes no sense we have

an in 64 here but when we hover over the

number it comes up as n32 and the reason

for that is when C sees that you just

have a number typed in like this you

will always assume that it is an integer

to tell the compiler that you indeed

want this to be processed as a long

number then you can put a capital l

right at the end now if we hover over it

it says in

64 and this says in 64 so now we've got

it working and we've got the long there

so now we can take another console right

line and just print this out to the

screen and just a helpful tip if you

type in CW and press tab twice then you

get the console right line it's very

easy to write your code and then you can

type in big followed by a tab and it'll

come up straight away so as you can see

they're both being printed to the

console

now so now that we've made positive

numbers we can also make these negative

by simply just putting a negative sign

behind them there we go and just to to

show you how big these numbers are what

we can do we can type in int. max value

and then make another one using contr D

and type in int. Min

value so you can see integers can be

positive 2.1 billion or netive - 2.1

billion and if we do the same for

long you can see that long is a very

very big number because instead of being

an INT 32 it's an in 64 which which

takes up a lot more memory so now we've

covered whole numbers let's have a look

at decimals so we can have a double

negative for example and we can say it's

-

55.2 and in this case when you just have

a number typed in like this and it's got

a decimal point in it it will always

register it as a double but just to make

sure just as we're doing with the long

you can actually just put a capital D at

the

end now if we print this out to the

screen and also print out how big these

numbers can

be max value and Min value and print

this out so we get our minus 55.2 and

you can see that these numbers can be

very big e+ 308 means it's this number

times by 10 the^ of 308 which is a very

big number and we also have that as a

negative so we have float let's call

this precision and make equal

5.1 and then put a semic call the end

now we've got a problem here this is

trying to register as a double but we

don't want to register a double we want

to register as a float and this is the

same issue we have the long so when you

type any number that has a decimal point

in it the C compiler will always

register as a double the same way when

you type in a whole number it'll always

register as an INT so in this case we

need to put a capital f at the end and

it'll process this as you

float so now we can print this

out and then let's just print out the

max values and the Min values just to

show you what that looks

like so as you can see here we have our

number printed out and again we have

quite a big number 3.4 * by 10 to the^

of 38 so again it's a big number and we

have that positive and negative so quite

a big range of

numbers and the last one we're going to

cover is decimal for things like money

and currency

14.99 and again in this case it's trying

to assign a double into a decimal so in

order to denote what a decimal is you

can put a capital M right at the

end and for one last time let's print

out the

money and the decimal. max value contrl

D and then we'll do Min

value and you can see here that this is

a really big number as well and it's

even bigger than the

long so we have our number printed and

the Max and the minimum values so now

that we've understood that how to

declare a variable I have an extra

little tip for you so if we had three

variables int X an INT Y and an INT

Z and let's say you're always going to

declare them together like this you can

actually miss out and do X comma y comma

Z and that will do exactly the same

thing there you go and that's really

neat and it's a lot better let's say for

example you had these variables but you

actually had them set to something like

equals 10 equals 20 equal 30 this is

still practical but it might look a

little

uglier so you can actually type them in

straight into here and if you comment

these out again then it still works you

can even put these onto new lines like

this just so it looks a bit neater just

prevents you from typing the additional

int each time if they're all going to be

the same variable type so that's it for

the numbers inside our code so let's

just have a quick recap so in order to

Define a variable you want to use the

data type in this case an integer

followed by the variable name and then

you can either add a semicolon at that

point or you can add equals to give it a

value this is just called declaring a

value and this is called initializing a

value if you want to decide you want to

change the variable further down your

program for example age equals 50 here

then this is fine and this is now called

an

assignment it's called initialization

because as soon as you make it you give

it a value and then in later on in the

code if you give it another value this

is called simply assigning a value this

is just called assigning it a

value so now that we can store numbers

in C let's see how we can store

characters and letters so as you know

from the introduction we have a string

variable so let's call that name and

make it equal to

Aba please note that when you're

defining a string and giving it a value

that this value needs to be in speech

marks if you put it in quotation marks

it will not accept this because that's

the notation to declare a Char which

we'll get on to next so on the next line

we can do a Char letter equals and we

can't have something like this because

the Char will only accept One Singular

character so we have to wrap it around

the quotation mark once you've wrapped

it around the quotation mark then it

will accept it so let's print these two

to the screen so CW tab tab and we can

just print these to the screen and if we

just run the code you can see that we've

got the values entered in

here we can start to write

stuff your name

is and then we can output this and we

can also put it on one

line if we do a console right and a

console right then maybe we can take the

callon off and just have it space and we

can say your name is ABBA and we' see

that additional a because that's the

right line for the letter so we can

space it out with an empty console right

line and and they'll add a gap between

them both there we

go so now we know how to declare a

string variable and give it a value and

we can declare a Char variable and also

give it a value just a quick recap when

you're declaring and initializing string

variables you need to put the value in

speech marks but when you're declaring

and initializing a Char you don't have

to put it in speech marks you put them

in apostrophes whenever you see

apostrophes that that can only ever

refer to One Singular character when you

see a speech mark it could be one or

more characters

also things can be empty this just means

the string is empty this is also a valid

character it just means that we're

storing a string but it has no value

with a Char that's not exactly the same

you can't store an empty character

literal it has to be given something

Charles get defaulted at back sl0 which

is just a default character but don't

worry about that too much now we'll go

into Escape characters which is the back

SL layer on in the course for now just

note that whenever you declare a string

variable we need to put it in the speech

marks and when we declaring a Char

variable we need the quotation

mark So now that we know how to define

numbers in C and to also Define strings

and characters then let's look at how to

convert between them both so let's take

some similar code from our first video

and we can Define it age the number the

negative the Precision and the money and

we give it the same values from the

first video we simply just print them

out to the console here and if we just

run we can see them all but this doesn't

help us cuz we're just statically typing

in the numbers what if these these will

actually process a string and we want to

convert them into an INT so just above

each of the variables we're going to

make another variable so we can say a

string text age equals

-23 and then for the age what we're

going to use is this function called

convert to int 32 and we can put in the

string inside the brackets and what this

is now saying is we're taking our string

text Edge which is equal to -23 and

we're passing it into this function

convert to in32 and the reason why it's

in 32 is because an INT is by definition

an in32 which is a 32bit signed integer

so what we want to do here is convert

for our text age into int32 and store it

back into our int and hopefully we

should see the same value when we hit

run 23 so now that's actually went from

a string variable and then went into our

integer and we've printed out as an

integer and it's no longer a string

variable now we can do the same for the

other ones so we can have a string text

big number and make it equal to this

value just in the quotation

marks now we're typing this value in the

quotation marks we don't need to put the

L at the end the L is just to tell the

compiler that we want to use in 64 if we

take the L away then it's going to think

it wants in 32 this is not re applicable

when it comes to text because we're

going to explicitly stay on the next

line we want to convert to N64 so the

compiler already knows that it's in 64

for and we don't need to put an L at the

end now let's make sure this works and

there we go so what we should end up

with is the console should look exactly

the same as how we had it at the start

of the video but actually what we're

doing is converting it from a string

into the applicable value string text

negative equal -

55.2 and then in here we can do convert

to double add the text negative inside

here and let's just run it to check

and we still have the same value so it's

going well have a string text

Precision make it equal to

this and then instead of this we can

have a convert two and in this case it's

actually going to be a single and the

reason why it's a single when you hover

of a float the definition for a float is

indeed a single so we can add that in

text precision and we're just going to

do the last one and just check it out Al

together text money equals

14.99 and then instead of this we can

have a

convert and let's have a look at this a

decimal is indeed a decimal so we can

say two decimal and then add the text

money

in perfect now if you run the line of

code we can see that all of these values

are exactly as we had them now they're

being represented by their official

descriptions Based On A String

conversion as I mentioned before in the

error video the problem you'll have with

this is if this value actually has a

character inside you will get a runtime

error this can be fixed in the future

when we look at how to resolve these

errors and we can have exception

handling and use functions like try pass

so we can try and avoid these exceptions

occurring because when these exceptions

occur the rest of our code does not run

and it becomes fatal at this

point but for now don't worry about this

too much just get used to the notation

and how these things

work so this is video we're going to

focus on the Boolean data type so we can

make a Boolean data type by saying Bo

value equals

true this Boolean value is literally

just a true or false value we can store

anything in here and maybe something

like bull is M and we can just say it

true and later on our code boings are

used to make decisions we'll cover this

in the next section of this course but

boings are mainly used for making

decisions if one thing is equal to

another thing then we can take another

path or if something is true or

something is false then we can do

another path for example if you make an

output to the user you could check if

this value is true and if the value is

true then you can address the user as a

male and if the value is false then you

can address the user as a female these

are the kind of things that booleans can

help us with and just like any variable

if you want to assign it later on you

can just assign it again and if we print

these out to the

screen just like that you'll just see

the value is true and the value is false

appear on the screen as of right now in

the course there's not a whole lot to do

with boing just understand that they can

hold a true or false

value so now that we know how to define

different variables and how to convert

between them let's see how we can

perform operations on different data

types let's say we could have an INT age

equals 23 and then what we can do to

that age is we can say Age ++ and what

this age will do is it will actually

just increment age by one and if we do

age minus minus it will decrement it by

one so if we just print it out after and

after this as well what we'll notice is

there'll be no change because it'll go

forward one and then back one there we

go went from 23 plus plus to 24 and then

minus minus to

23 that's one of the important operators

that you'll see a lot especially during

the next section of this course so

another way to be able to add one to a

variable or anything to a variable we

can replace this with age equals age + 1

and what we're seeing here we want age

to have the value of itself added to one

which will give us the same result as

doing h++ another way of writing this is

a quick shorthand is you can say AG plus

equals 1 these three things are doing

exactly the same thing when you do an

age Plus+ you can only increment by one

and only one these two options give you

the flexibility of having something like

10 these two are doing exactly the same

a AG equal AG + 10 and this is going to

read exactly the same thing it's is when

you use plus equals it will take the

value of the current variable and add it

to 10 which exactly what this line does

to make it a lot shorter you can use age

plus equals and then just put the value

at the end just in case you forget to

mention age twice so now if we just get

rid of these two lines of code and we

can just have a look and see if this

runs so now the value should be 33

perfect and now as we said we're going

to cover plusus time and divide so just

as you did with plus equals you can

actually do a minus equals as well which

will give us 13 and you can do a times

equals which should give us

230 now if we try to do divide equals

we're going to run into an

issue it says

2 23 / 10 is

2.3 and when you take 2.3 and make it

into an integer it's just going to

truncate the3 and leave you with two

with integers if you add two integers or

take away two integers or times them

you'll always end up with an integer if

you divide two integers you don't know

what you'll end with for example if you

make this into 20 then you'll always end

up with two but that's not always the

case as you just saw 23 / 10 is 2.3 so

if you're ever going to do any division

it's best to actually make this a double

cuz now that we do the division we

actually get 2.3 which is technically

the right value so you can add minus

times and divide any variables that you

want these are just demonstrated in int

and double but you can do this the Flor

the decimal Etc so let's have a look and

see what the plus and the minus

operators do when you've got a string

variable so we can have a string name

equals

abber and then we can say the name plus

equals is

programming and then we can just print

out name to the screen and see what

happens there you go you've actually

just joined two strings together they're

completely separate from each other and

we've made them string

together so if we try to do a minus

equals you can't really remove from a

string because a string is just text

it'll be very difficult for the compiler

and for the C language to be able to

determine what's currently in the string

and whether you can remove it or not

this is not something that's built into

the language this is not something

that's built into the language so let's

remove that and we can have the same

thing with the

CH now the problem with the ch

is if we add a to B then let's see what

happens to the

output what it will try and do is

actually get the uni code values for

both of these items and try and add them

together as integer

variables so when you add a and b it

will take the unic code value and you

will actually get a capital A with a

squiggle at the top and if we open

character map you can see that this

squiggle at the top is right here a

which is unic cord 61 and 61 actually

means 97 because it's 6 * by 16 + the 1

and b means it's 6 * 16 which is 96 +

the 2 so you have 97 and 98 and if you

add them two values together you'll get

the hex code which is C3 just for this

character so what you're doing is you're

kind of adding them both together and

you're producing weird results so unless

you really want to do this then you

should be worryed about adding two

characters together one more thing to

note about when you're doing Plus+ let's

bring another int variable and we just

say int I equals z then we can do I ++

and then if we print out I the value

should be one which is fine what if we

did

i++ but we did Plus+ I so the value is

still going to be one but what happens

if we put this I ++ straight into these

brackets let's try and run it and you'll

still get the value of zero that's

strange because we've said I ++ and what

happens when you do Plus+ at the front

it will actually execute this and return

the value of I and then do the Plus+ so

if you actually print the value of I

after you'll realize that now it becomes

one after if you ever had a situation

where you need to increment first and

then use the value you can use plus plus

I now if you run it you should see the

value is one twice the difference is

that as the plus plus is before the

variable name it tells the compiler

please do this action first and then

return back the I if you don't want that

behavior then use I

++ finding the remainder between the

division of two integer variables can be

very handy when you're determining

whether a number is even or odd let's

look at an example let's have it in

first Nome design the value of 10 and in

second Nome and give it the value of

three now if we just printed the screen

first num divided by second n what do

you think you'll get so the value of

this in the calculator will be 3.3

reoccurring so we'd actually just get

three because it's truncating it because

of the

integer so now what happens if we need

the remainder between both of these you

could actually work this out by seeing

how many times three would be divided by

10 as close as possible and you see the

value is three and then 3 * by 3 is 9

which will give you one left over for

the 10 but that's a bit too long in our

code so we have something called a

modulus operator so we can actually

change this divide for a percentage sign

and what this will output is it will

only give us the remainder so let's say

10 / 3 is

3.3 as we've just said but the remainder

is different remainder is exactly how

you learned it at

school 10 / 3 is actually 3 remainder

one because 3 * 3 is 9 and then you have

one left over to get 10 so that'll be 3

* 3 which is equal to 9 and then one

left over to get to 10 so now let's run

our code and see if we do actually get

one there we go so the best way to

determine if a value is actually odd or

even is to divide it by two so we can

say 10 modulus 2 and if the value is

zero then that means the value for first

nor is actually even if the value is one

then it's OD let's see how this works

out if we had 10 modulus 2 then the

value is zero because 10 can be

divisible by two perfectly let's say we

had 11 modulus 2 the closest multiple to

11 is 10 which is 5 and then we have one

left over so you just have the remainder

is equal to one and if we just keep

going up in the chain we'll realize that

it's the same pattern every single time

you go up a value the value just toggles

so when we print this out to the console

and the value is actually zero that

means the number is even and if we make

the number odd on purpose and then rerun

the code you can see that it's a number

one now which means this value is now

odd let's look at a couple examples and

see if you can figure out the answer if

we do a th000 modulus 90 what do you

think the value will be and let's write

a couple more if we do 100 modulus 90

and maybe something like 71 1 modulus 10

so have a think about this in your head

and see if you can figure out the

answers pause the video and see if you

are

correct let's take a look at the

answers so we can see we have 10 10 and

1 so the closest multiple to 1,000 from

90 is

990 so we have 10 left over to get to

1,000 and the same goes here the closest

multiple of 100 to 90 is just indeed 90

so we only have 10 to get from 90 to 100

and in this case the closest multiple of

71 to 10 is 70 and we have one to get up

to 71 therefore these two answers are

indeed

correct so now that we know how to

really Define a variable by specifying

their type follow by the name and giving

it a value let's see how we can do a

very easy way of defining a variable so

normally we would type in Edge what we

can use this VAR keyword what this means

is the compiler will figure out what

type it needs to be based on the value

we initialize it with so if we just make

a variable called age then it's not

going to like it because you need to

give it a value this doesn't give the

compiler enough information to figure

out what this variable is going to be

just because it says age this is just

text to the compiler this does not mean

anything so if you make it equal to 23

then the compiler can see that this is

indeed in 32 because it knows that so

now if you hover over a VAR it says in

32 now let's try and do this for the big

number

our big number equals

90,000 and what will happen now is

because it's Define as in32 it's going

to say this is in32 as well and this is

the same issue we had we'll move first

defining the long variable in the first

video we need to explicitly give it an L

just so this knows that it's N64 and now

the VAR has updated to an

N64 instead of rewriting each one we can

just change each of v and inspect what

happens double

double and now we have a float single

single and finally the decimal we have a

decimal and we have a decimal and the

same thing goes for a string we can say

VAR name equals ABA and we can have a

VAR letter equals a there we go and

they'll both register as there pickable

data types as a string and as a Char and

if we just print this out to the screen

to make sure everything's working fine

and run the code you can see that

everything is exactly how we'd expect so

just a quick recap you don't have to do

this at the end of the day this will be

exactly the same as writing this because

when our code gets compiled down it

doesn't matter what these things are cuz

the compiler will deal with it however

way it wants to this is just for us to

read it in some cases if the value is

very obvious like as we know an age

cannot really be a point 23.1 years old

no one says that in real life when you

get asked your age so we can just assume

that this value is going to be in an

integer and that's fine because that's

an easy number that's an easy example

but if we say we want a negative or a

big number then it doesn't really tell

us what we're meant to be like this big

number could be somewhere up to 2.1

billion which is what an integer

supports but in this case if it's going

to be a big number and we specified the

L then we're going to need a really big

number bigger than 2.1 billion so in my

honest opinion it' be very beneficial to

only use VAR if it's very obvious what

the variable is going to be and want

it's being assigned to using VAR too

much might hurt the readability of your

code in the future because you have to

inspect each variable individually to

read what the type is going to be if

you're scrolling through your code and

you can see that this is along very

quickly and you don't have to hover over

it then it can improve your read time

this is really important as readability

of code will make more sense when it

comes to Big projects when you have 20

30 lines just like this project then it

makes almost no difference cuz it only

takes a couple of seconds when you have

big functions big software when you work

at a job then it's going to be very

difficult to be able to find your way

around and to understand what's going on

especially if you're new to the code

something very simple like this where

you know you're defining a name it's

very obvious and VAR will be accepted

fine because you can tell that this is

always going to be a string there's no

other data type in C get specified by a

speech mark whereas when you're defining

numbers it can be very very hard to

understand what different data types

that people are using throughout the

code especially when these numbers are

not of statically typed in they might be

inputs from the console from the user

from a different function from anywhere

so having good variable names along with

their explicit types will be very handy

to understand code in the

future so now that you've understood how

to make a variable let's look at a

concept called constants so we defined a

variable called vat and we set it to 20

just to represent the 20% V in the UK

now later on in the program we can

actually change it to anything we want

and that's because it's a variable

variables can change in certain

circumstances like vat you might not

want to change it and you want to

disable the ability to change it to the

code if it's done accidentally let's say

for example you're making a banking

application and you wanted to fix the V

and you don't want anyone else to change

it you can add this keyword called const

right before the Declaration and now

when you go to reassign it you'll get an

error message and it says at the left

hand side of the assignment so anything

before the equal sign has to be a very

Able Property or indexer and in this

case if you hover over vat it states

that it's a local constant local because

there's no access modifier before it and

constant because we specified the con

keyword so now if we try and run the

code we'll actually get a compile error

because the vat cannot be changed so if

we remove that and then we just print it

out to the console using CW tab tab and

then we can put vat in the brackets and

press F5 and now we can see our 20 being

printed

so let's try and calculate some values

using this code if you have an INT

balance make it equal to 1,000 and let's

say we wanted to calculate the vat based

off this then you'd have to do a little

bit of a complicated calculation you

have to take the balance and because the

vat is a percentage we need to convert

it from decimal to percentage so if we

do times and then open the parentheses

we can insert

vat divided by 100 and just to make sure

that this gets process as a double we

want write 100 D cuz right now it's in

32 and if we leave this as an integer

division then we'll lose any decimal

places and decimal places are very

important when it comes to percentages

so we put a 100 D at the end now you can

see it says double so it'll actually

calculate as if it was a decimal number

so now if we print that to the screen so

we can see here that 20% of the balance

is indeed 200 instead of having to do

this calculation each time what we can

actually do is make another const this

time making a double because we're not

sure what the output of this division

will be and we don't want to truncate it

so we want it as a double and we can say

percent vat and what we can do here when

we assign the value just because it's a

constant it means that we can't change

the value but that also means we can

read it as many times as we want so we

can say the percentage V is actually the

current V that we've got specified here

divided by

100d this will give us the vat from a

number form into a decimal form form so

now when we do this calculation if we

duplicate this line using contrl D we

can remove these entire brackets and

literally replace it with percentage vat

and you should see we get the same

values

perfect this can be very helpful in the

future if you have things like version

numbers in your code you can have

version 1.0 and then if you ever specify

this version later on in your code you

can just change it once up and it will

change the rest this can be very helpful

you'll avoid a lot of typos and you

won't have to to redeclare this value

several times if you had three different

occasions where you displaying the

version number in your code then it's

going to be problematic once you update

the version number and you have to

change every single occurrence and you

might miss them so inserting it once and

only having to change it once is the

preferred option just to note with

constants when you compile your code all

the constants will actually do is do a

find and replace so it will take this

vat and find all references of v and

just drop the 20 in

there so this is more of a feature for

maintaining your code from a user and

programmer's perspective rather than

anything to do with the compiler because

when it compiles it just removes all the

constant anywhere and then you're left

over with the literal values that we've

gave it nonetheless this is still

something very handy to learn and to use

throughout your

code first you want to Define a variable

to hold your name so we can just say

string name equals abber perfect so

that's the first one achieved says

Define a variable to hold your phone

number number so at first this could be

quite easy we could say int number

equals and then we can start to type our

number in here so now that's our number

completed let's move on to holding your

age and we can just have an INT age

equals

23 print variables line by line to the

screen so we can use CW tab tab and type

in name and we can use contrl D to copy

and paste the line then we can do number

crl D again and then we can use ede now

if we run the code let's see what

happens

here we've actually hit a slight problem

it says 1 2 3 4 for the number but I'm

pretty sure that I started it off with

zero this is a problem with storing your

number as an integer as a number zeros

at the front don't really matter if we

had a 100 zeros here then it's not going

to change the actual value the only time

zeros matter is if it's throughout the

number or towards the end that actually

changes the number but if we have lots

of zeros at the start then it doesn't

change anything so what you actually

want to do in order to store a number is

you want to store it as a

string therefore when you run the code

it will actually rain the zero that we

typed in so now that we've accomplished

all of these let's see how we can Define

the variables using the VAR keyword so

if we hover over these you can see that

it's picking up that it's a string and a

string here and finally this is an

integer so what we can do very simply is

just change these to VAR and then let's

hover over the variables and just see if

the tool tip reads the correct data type

string string and integer and if you run

the code you should see no

difference so in this exercise I want

you to create and initialize two integer

variables make a variable to work out

the remainder output the remainder to

the screen and then change the first

integer variable to another number

recalculate the remainder and output the

new remainder to the screen so let's

take this step by step so first we need

to create an initialize two ins let's

say int num 1 equals to 10 int num 2

equals to two make a variable to work

out the remainder so we can have an INT

remainder equals to and if you remember

how to do the remainder it uses the

modulus symbol which is the percentage

sign so we can say num one percentage

num

two now it says output remainder to the

screen so we can use CW tubtub add

remainder in and hit run and just for

now we can see that the remainder is

indeed zero if the remainder is zero

then that means 10 is even because 10

modulus 2 gives a zero and if it's zero

then that means it's even if the

remainder was one then it would mean

it's odd so if we try 11 remainer two we

get one so 11 is indeed an odd number so

let's change this back to 10 and then it

says change the first into variable to

another number so just after here we

could say num one equals 11 and then

recalculate the remainder so we

essentially just need to do this section

again and we don't need to write in

before these two variables cuz int is

only use when you're declaring it this

is reassigning the

variable so we'll reassign variable num

one to 11 and then we reassign the

remainder to the calculation again and

now we can use the exact same line of

code above to print out the remainder to

the screen now you can see that it's 0er

and one it's zero because 10 is even and

it's one because 11 is

odd so far throughout the course we've

been using console right line to Output

stuff to the screen for example I can

output hello my name is Aba and we can

see it in the console the issue is so

far we haven't really brought any code

to be able to read in values from the

console so let's explore that option

right now so in the console Library we

have a console right line but what you

don't realize is we can also use this

console right line to ask the user a

question and to let the user type

something in so let's use CW tab tab and

say enter your

name with a callon and then end the

quotation

marks and then in order to return a

value from the console into your program

we need to use console.readline and if

you hover over readline let's have a

read at description reads the next line

of characters from the standard input

stream perfect this is exactly what we

want to do right now it's use at the end

of our code code just to stop the

console from closing so we can actually

see the output so if we just run this

right now we actually get the

opportunity to type but when we press

enter our program closes this is because

in this case we're simply using readline

just to wait until the user has pressed

enter so so our program doesn't close

but what you don't actually see is this

is returning as a string which means

that anything that we type into here and

when we press enter it actually gets a

return back to the code but we're not

doing anything with it because we don't

care at this point so now let's actually

do something that will make us care

about the user's input so we can make a

new string variable and assign it to

console readline and it should give us

the ability to store Whatever Gets

entered inside the console so we can

just do string name and we can end the

semicolon here and if you wanted to on

the next line you could do name equals

console do

readline or if you wanted to have it all

in one line then you can get rid of the

semicolon and bring it up like this this

is the preferred method as both lines

were on top of each other and it wasn't

really necessary to split them here

we're doing a declaration of string name

and an initialization and given it the

value of console. readline and this is

whatever value the user is going to type

in let's run our code and see what

happens enter your name abber now when I

press enter it successfully started ABA

inside this string name variable and now

it's waiting at this line the console

read line just just right here so when I

press enter the program closes so now

that we've made a variable and not done

anything with it let's print out to the

screen to make sure that something's

actually worked so if we use CW tab tab

and we type in name in the brackets and

we can run this code again and let's see

what happens as soon as I type in abber

and press enter then this second abber

is actually the variable printing out to

the screen so here it says enter your

name with a colon and normally when you

see the colon it sometimes lets you type

on the same line so if you'd prefer the

user to type on the same line then we

actually have another function for that

instead of using right line we can just

use right and the difference between

right line and right is the right line

will actually have a Terminator at the

end of the line to tell the console that

he wants to push everything after it

onto the next line whereas console.

right will just print it out to the

screen and not worry about any new lines

let's try run it now and now we can type

in abber as you can see it's a little

close to the colon so we can fix that in

a second so you press enter you can see

now that comes up on the second line

so we just put a space after the callon

just to give it a bit of room now if you

type in Abba you can see that there's a

bit of a gap and it's quite nicer to

look at now if we press enter you can

see that AB has been stored

successfully so what we can do now is

maybe ask for your age so we can have a

console.

right enter your age call on space and

then do a string age equals console.

readline and then we can have a age

output as well and let's see how that

works so we can say abber then say 23

and press enter and now we have abber

stored within the name variable and have

23 stored within the age variable so

let's try and output a string that's a

bit more meaningful to the user let's

clean this console up and see what we

can do so we don't need to Output their

result back to the console because they

literally just type that in and we don't

want to have this line because we just

use that as an example so if we run the

code right now it'll look better but it

does look a lot more structured enter

your name and then enter your age so now

when you press enter the program will

just close so let's try and output these

two variables in a formatted fashion so

as you now know we can use console. WR

to print a lot of stuff on one line so

we can use console. right and we can say

your name is and then add a space and

finish that line there and then

duplicate it down we can write name

duplicate that down and say space and

your age is duplicate that one down and

then we can type in age and let's see

what happens now abber

23 there we go so we actually have the

name output and the age output all on

one line if we all change these to right

lines then they're going to be on

separate lines and it's not going to

look very nice nice so I'm just going to

introduce you to a notation called

string concatenation this is the notion

of being able to concatenate two

different strings which just means to

add one string onto another as you can

see this is taken up four lines of code

and it's getting a bit ugly the better

and easier way to do this is by

containing it all within one line so we

can have a console right line and we can

take the first section your name is keep

the space at the end we can end the

speech marks and put a plus and then we

can send name so now we've just added

these two together if we just comment

out the rest of them and run the code

and you can see that they should be on

one line there you go so we can say your

name is Aba and let's just keep changing

the plus symbol so we can say and your

age is another space and the speech

marks and plus the age now we've got all

of this contained within one line and it

looks a lot better enter your name Abba

enter your age 23 and now you can see

exactly the same output but reduced to

one

line as you can see these three lines

are all stuck to each other so if you

wanted to add a blank line between these

two you can just put an MC console right

line just in here and what this will do

is simply just print the right line

character to the screen which will force

the console to make it on the next line

just like so here's the console right

line from the empty line that we just

wrote and this is the console right line

to Output the Val values so now that

you've understood how to read values in

from the console let's try and convert

some values as I showed you how to do

earlier on in the cuse you can actually

convert these strings into integers

ideally you want to store variables in

their data types as we covered before

ages should not be stored as strings

they should be stored as integers but if

you try and change this to an INT then

you're going to have an issue console

read line will return back a string so

therefore it can't implicitly convert

from a string to an integer so we need

to do this in two steps we can rename

this to age input and then on the line

underneath it we can say int age equals

convert to int 32 and then add age input

in these

brackets there we go and now when we

actually print the line of code nothing

should actually

change your name is abber and your age

is 23 but now what's actually changed is

we're storing the variables in the

correct format this is the ideal method

because in the next section when we

cover if statements you'll realize why

this is very important if you wanted to

minimize the amount of steps you need to

convert this then you can actually move

the console read line and get rid of the

string input variable and we can just

paste it straight into the bracket of

the converter in 32 and you can get rid

of the string input variable and

everything will work the same h 23 and

now it's working exactly the same if

this is too many steps for you or if

this looks too complicated then just

please revert it back just to keep it

the same way for the time

being so now we've learned how to read

in values from the console and output

them back to the screen now let's see

how we can use if statements to divert

our code into multiple paths so in the

previous section I told you that it's

very crucial for our code to have this

int age and the reason why is age is

meant to be stored as a number and not

as a string so we read it in first as a

string and then convert it to an integer

using this function and store it into

our age variable this is the reason why

so our condition goes as follows we

would write the word if and between the

brackets this needs to be evaluated to

either a true or false value and then we

can have the curly braces to tell us

what to do if this becomes true so in

this case you want to just check if our

age input is greater than or equal to 18

let's say this was an entrance to a

place that only allowed overes to enter

so we can do age input which refer es

the age input variable and we can use

the operators equals greater than

greater than or equal to less than or

less than or equal to or not equal to so

these are all the different options that

we have we can say if the age input is

equal to

18 perfect so now that we've read in the

age input we can check if it's 18 let's

just process a message in here says you

are 18 and let's have a look and see if

this works

18 now I press enter it says you are

18 but this is the problem with storing

everything as a string and then trying

to use it as if it was a number if I

type in 18 followed by a space as you

can see there's a blank space here now

and press enter you can see that it

doesn't come up and the reason why is

it's processing it like this with a

space at the end in order to avoid

situations like this this is why we

would convert it to a number and the

reason why we're using equals to here is

we can't use greater than or equal to

because strings can't be greater than or

equal to another string they can only

ever be equal to or not equal to string

is just text so if something equals to

something then it's fine the only way

that we can use these different

operators is if we actually interact

with our integer variable now let's see

how we can use this so we can say age

equals equals 18 and now you can see we

actually reference it as a number now

let's run the code if I do 18 Follow by

space it still works and the reason why

is it actually dismisses that space when

it's converting into the integer so now

a just started as an integer and we can

actually manipulate this a bit better so

instead of saying you are 18 we can say

you are 18 or older and now instead of

doing equal to we can have greater than

if we have greater than then we're

actually dismissing the number 18

because if the age is greater than 18 so

this will read like this and 18 is not

greater than 18 so this will return back

false what we actually want to use is

greater than or equal to if the age that

we've inputed is greater than or equal

to 18 then you are 18 hence the equal to

are greater than if you're older so now

let's try and run this AB 23 perfect you

are 18 or older let's change so let's

change some other if statements to add

some additional conditions so we can say

else if and the only reason why it would

come into this bracket if this condition

returns false it will move down the next

condition in the else chain if this was

wrote as a separate if statement then

this would run completely independently

from this one if you're going to have

chained if statements you need to make

sure that you chain them correctly in

this case I want to check if it ages

between 18 and 25 and then move on to a

different age bracket and these two

things are grouped together so we should

have this as an else if because with an

edge bracket you can only be of one age

bracket and not multiple so we can say

if age is greater than or equal to 25

and we can copy this and we can say you

are 25 or older now let's see what

happens enter your name abber and let's

just say that I am

35 it says you're 18 or older but I told

it the program I was 35 now the problem

we're facing is any value that's over 18

will always enter inside here and it

will skip over this one completely this

is not the behavior that we want so so

what we can do in our if statements is

we can use the double pipe which

represents or or the double Ampersand

which represents and and we can change

some conditions in here to make sure

that you implement some sort of range so

we can actually have a range between 18

and 25 so we can say if the age is

greater than or equal to 18 and the age

is less than or equal to 25 and we can

tweak this message you are between 18

and

25 and let's just run that for now and

see what happens abber 25 now it says 18

and

25 so now that we've got between 18 and

25 here we can tweak this to be greater

than equal to

26 if you're 25 will enter this

condition and if you're 26 or older into

this condition so let's test that now if

we enter

35 now it says your 26 older that's

perfect because now that we've

implemented range checking it gives us

the ability to differentiate between

different age groups more precisely

let's Show an example of how to use the

R so if we just have a block comment

around all of this we can write an if

statement in here and we can say if the

age is smaller than zero or the age is

bigger than 150 then we can output to

the screen in valid Age and what this is

saying is if we're less than zero and

notice that this is less than zero so

this value will actually start at minus

one and this value start at 150 is minus

one or less or over 150 then this will

happen notice why it's an or one of

these need to be true in order for this

to be true when you've got an and both

sides need to be true in order for this

condition to invalu it is true so let's

try our code we can say Abu and we put

minus one and now it says invalid age

which is perfect because this is our

range checking and we can try again with

151 as it's just outside the range it

will say invalid age and if we have an

else on here we can print valid

age notice there is no condition in here

if you have an open else like this it

means If This Were to return false it

will always do this regardless of any

condition in this case it's perfectly

reasonable to do this because we know

that any values that are underneath zero

or over 150 is invalid and anything

between 0 and 150 is indeed valid what

you can do instead is actually move

these if statements inside here so

instead of just printing out valid age

we can actually check the age as

well so let's run this now and we can

say Aba minus one and it will only print

out invalid age and then move on because

once it reads the this line of code it

will actually skip over the L's and

finish up down here and now if you enter

a real

value you are between 18 and 25 so let's

follow the path it got to here and

realize that the range check was

actually correct so jumped out of this

curly brace and went to the else because

this had returned false we jump into

this else and now we start checking if

ages between 18 and 25 which it was

because our age variable is 23 then it

outputed this to the screen

now if we do the same thing but have 35

instead then it follows the exact same

path except towards here this will

return back false and then it will check

the next condition if age is over 26

then we'll return

this so now let's just make a quick

multiplication program using if

statement to see if you can get the

answer correct so let's just comment

everything

out and we'll start the next section

underneath

so let's make a console. write and ask

the user to enter the first

number and then we can say int number a

equals convert to in32 and the value

that we want to convert is a console.

readline which is whatever the user

entered from the console again if you're

unfamiliar with this format you can just

type it in manually number a input

equals console read line and then we can

put the number input in here now if you

use shift and highlight these lines and

press contrl D we can duplicate this

entire section and we can say enter the

second number change a to B change a to

B and then finally change a to B perfect

so now we've got enter the first number

and enter the second number and we can

say our

answer equal to number a multi by number

B and then we can have a console. WR

value of we can add to it number A plus

an X Plus number B and then finally plus

a callon for the user to type in and

then we can say string answer input

equals console. readline and then

int actual answer equals convert to in32

and then we can have the input in

here so now that we've got one number

from the console the second number from

the console we're just making a variable

that will multiply them together and

store inside answer and then we're just

going to ask the user what's the value

of number a times my number B and they

can input that we store it as a string

and then convert it to an integer and

store the answer into actual answer so

now just after the section we can write

an if statement that says if the answer

equals to the actual answer then we can

say Well

done and else and the reason why we

don't need to condition this else

because with equals to the only opposite

thing you can have is if it's not equal

to so if you really wanted to you could

also write if answer is not equal to

actual answer then in here you can say

close but it was wrong you don't

actually need this this is just for

learning purposes because this will

return back false if they're not equal

to each other so this statement is just

a given if they're not equal to each

other then we can just assume that

they're not equal to each other so we

don't need this additional statement if

this condition evaluates to being false

then that means they're not equal to

each other we don't need this additional

check to check if they're not equal to

each other because that's a given if

this returns back false so we can just

leave this as an else let's try run the

code 10 10 value of 10 * 10 100 now it

says well done and let's just try that

again and we can enter a different value

90 Clause but it was wrong so let's just

have have a quick

recap so in the previous section we

talked about how to input from the

console and we generated this line of

code which will output your name and

your age in one line to the console and

then we looked at how we can use these

different operators in our if statements

and we did some range check in to check

if the age was between 18 and 25 and it

would output something to the screen and

if it was over 26 you'd get a different

output to the screen and then we took

that a little bit further and started

using the r operator

if the age is smaller than zero or it's

greater than 150 then it must be invalid

and if this condition returns false it

must be between 0 and 150 so we can come

into this L's bracket and inside here we

can do our range checkin from the

previous bit of code we can check the

ranges between 18 and 25 and print a

value or if it's over 26 we can print

another value and you can keep changing

these if you want you could have 26 to

40 and 40 to 60 Etc Etc that's up to

you so this is just a quick little

program to simulate multiplication

program we can enter the first number

using console. WR and then we start it

as a string and we put that string into

the converter in32 function to give us

back an integer variable and we do

exactly the same thing for the second

number and then we make a variable to

store both numbers multiplied by each

other we ask the user what is the value

of number a Times by number B and we

give them an opportunity to type in a

value we read that value in convert it

to an integer store it as an into actual

answer and then in our if statement we

say if the answer is equal to the actual

answer then we print well done to the

screen double equals are used for

equality and single equals are used for

assignment don't get them mixed up we

don't need an else condition in here

because if this returns true then this

will execute if this is indeed false

that they're not equal to each other

then we always want to do this section

and we don't need to put an additional

if statement inside here if you wanted

to but it's not necessary you can have

lse if answer is not equal to actual

answer but this is just extra code and

it hurts the readability and it should

be

avoided if statements are one of the

methods we can decide different paths in

our code the next method we can use is

called switch statements so let's write

a little program so we can demonstrate

this so we can just have a console right

enter date of the week and we can have

in date equals convert to in32 and we

can place our console. readline inside

the brackets so we take whatever the

user typed into the console and then

place it inside here and inside here

we're going to convert whatever they've

typed in to an integer 32 and star it

into our D variable so if we just print

this out to the screen to make sure it's

working

fine if we just type in seven we can see

that seven is returned so it's all

working so normally what we do is type

if D equals equals 1 and then we can add

the Cy braces and use CW tab tab and we

can print out Munday now if you run the

code and try use number one then Monday

works and that's fine and then let's do

another LS if and we can say day equals

equals 2 and then we can print

Tuesday perfect so if we try that

now there we go so now we have the

ability to print out Monday and Tuesday

to the console depending what day the

week the user enters but since we're

going to have have seven different paths

because we got seven days of the week

then sometimes it might be better and

cleaner to use a switch statement so

let's have a look at the syntax so we

can type in switch and then inside the

brackets it's what variable we want to

have a switch on so in this case we want

to be using the day variable so we can

add D in the brackets this is not a

condition in the brackets this is just

telling us what variable we're going to

be using for the switch statement so

let's open the curly braces after and

inside here instead of having paths like

this we're going to use cases so this

can be translated to in the case of

number one we can do this in the case of

number two we can do this so let's try

and see how it works we can type in case

followed by number one and then add a

callon and then we can type out our

Monday in here and then we need to

follow each case off by finishing the

section off with a break this break's

very essential and you'll get a syntax

error if you don't put it in so now if

we do case two and we do exactly the

same thing there we go and if I dis miss

the break then you'll realize that it

says it cannot fall out so we have to

use a break just the compiler knows that

we want to break here the reason why

this break is placed inside here is

because what we can do is we can say

case two and then add another one in

here called case 3 and what this now

reads is if we enter two it says choose

there and if we enter three It also says

choose it so what you can do is you can

actually chain so this would be

equivalent to writing if D equals 2 or D

equals equals 3 now this is exact

translation these two are exactly the

same as of right now we have D equal

equal 1 which is our K1 and then equals

2 Ral 3 is right here if we go into the

case 2 path it will fall in the case 3

so we're saying for this break is we

want to end this section here so now if

we just revert this back and continue

the

chain I'll just skip this part just so

it's a little bit quicker

there we go so now that we've got all

seven days of the week let's just see if

it works let's try six and we have

Saturday so now we're kind of missing

something from here the only thing we're

missing is having our external lse just

in case the user types something invalid

so normally we would write something

like this enter value between 1 and 7

just to make sure that the user knows

what they're

doing now we don't seem to have anything

inside here that gives us the

opportunity to invalid so we have this

keyword called default and it works

exactly the same as the case except what

this will do is this is basically the

word that replaces else so we can

literally take this line of code and

place it again over here and add

ourselves a break this is exactly the

same as doing our L statement inside

here if none of these cases match this

variable then it'll go into the default

and Contin as normal so if we type in

zero we should see the message twice

because it's executing for the if

statement and executing down here and

let's do eight and you see the message

again so now that the case statement is

fully working let's just comment out the

if statement and make sure that it still

works one Monday let's just try another

one and we can do seven for Sunday so

just a quick recap case statements are

used by using the keyword switch

followed by the variable in question and

then if you want to check if this

variable number is equal to something

you put that something straight after

the word case followed by a colon and

then you can insert multiple lines of

Corde inside here and make sure you end

that line with a break if you want to

chain multiple like Dayal 2 or Dayal 3

then you can add the case above right

here so the case of zero or the case of

two it will do this little section so

it'll print out Tuesday if the user

enters zero or the user enters two of

course in this case we don't actually

want that after you filled out all of

the code make sure you add something

called the default the default is

equivalent to an lse keyword in our if

statement just to catch all the values

in case the user type something

incorrect and you want to let them know

what they should be typing

instead so now that we explored

different paths in our code using IF

statements and switch statements let's

have a look at how we can iterate

through our code an iteration in code is

just the ability to be able to Loop and

do a certain task so let's say we wanted

to print high to the console several

times then we just have to copy and

paste it down and this will be five

separate lines that do exactly the same

thing this will essentially achieve what

we want the problem is you just have

exactly the same code copy and pasted so

what we can do if we just block comment

this line for the time being we can

write something called a for Loop a for

Loop gives us the ability to Loop a

certain amount of times depending on the

parameters that we give it so this is

the syntax you can type for open the

bracket and we have to use a variable so

we can declare a new variable straight

into these brackets we can say inti

equals z and then have a semicolon and

there's three different sections to this

the first one is the variable decoration

and initialization and the second one is

the condition so in this case we want to

do this task five times so you want to

say I is less than five then we end that

semicolon there and now for the third

task every time this for Loop goes

around we want to increase I by one

because we want to go in steps of one so

we can do

i++ alternativ if you don't like using

Plus+ you can say I = I + 1 or to make

that quicker you could say I + equal 1 I

personally just prefer using Plus+ and

you'll see that everywhere else there is

certain cases where you would actually

want to change this to a different value

like I +al 2 and we'll show you that in

a second so this for Loop now says we

want to go from I equals 0 and once this

condition returns back false when I is

equal to 5 or greater then we want to

stop this for Loop every time this for

Loop goes around so in hits this curly

brace and comes back around we want to

do this action and in this case it's I

++ so if we just use console right line

and print out the value I we can see

what happens when the follow

runs you can see that the value of I is

increasing one each time and after the

fifth time is increased the value is

actually five so now this statement is

false because the value of I is equal to

five so therefore this is now false and

the for Loop is ended so now that we've

verified that you can repeat it five

times using this code we no longer want

to print I we want to print our high so

let's paste that inside the bracket and

run the code now you can see that these

two values will be exactly the same as

printing it out to the console just like

this but now we have a lot less code so

let's have a look at another example we

can say 4 in I equal 0 and we can say I

is less than or equal to 10 and then we

can do I + = 2 and then if we just print

I to the console so what this is now

saying is I want to go from zero all way

till 10 and I want to go up two times

each time so what this will be achieving

is printing out all of the even numbers

between 0 and

10 let's try and run

it now we can see we have all the even

numbers printed out between 0 and 10

because we're going up in

twos if we start this at one for example

and still keep every parameter exactly

the same then we'll be getting all of

the even numbers between 1 and 10 11 is

missing because our condition stops at

less than or equ equal to 10 what you

can also do with for Loops is you can

let the user determine what this value

is going to be let's test that so if we

block comment this section out what we

can do is we can appen this little

section so we can have a console. right

how many times do you want to say hi can

have a callon followed by speech marks

and then we can say in Loop counter

equals convert to in32 and we can have

console read line inside the

brackets how many times you want to say

hi the user types in the value you take

the console read line as a string

convert it to an integer and start

inside loop

counter so now what needs to change

inside here the five is what determines

how many times that we're going to Loop

so in this case we actually want to just

Loop by whatever the user types in the

counter let's just type five and make

sure it works and if we just type one it

works if we type zero however it will

just skip over the for Loop and the

reason for that is if you're going from

0o to I is less than zero then this is

immediately going to be false so then

the for Loop is just going to get

skipped immediately there is nothing to

do if the user is typed in zero which is

perfectly reasonable and you can type in

a really big number like a 100 and it

will output them all to the

screen if the user types in zero then

nothing's going to come up so you always

want to be able to let the user know if

there's something wrong so we can say if

the loop counter equals equals to zero

or maybe we can change this to less than

or equal to because it could type in

minus numbers as well we could just say

a conso right line using CW tab tab

sorry please enter a value above zero

and then if this condition has returned

back false then inside our else

statement we don't need to give it an

else if because if it's less than or

equal to zero then we want to process

the other values because it has to be

greater or equal to one

so when this is false this section has

to be greater than or equal to one so we

can just place our fall Loop inside here

and that's fine and let's try to run the

code now and if we type in zero please

enter a value of zero and if we just try

any negative values we'll get the same

message if you want to print additional

code in here you can literally do

anything you want you can even ask the

user what they want to print up here and

then you can replace this with a message

for example we could have a string

message

and we can just change this to hello

world and this could be something that

you reading from the console so we can

say message and we can put that in here

and if we print that there we

go so we can just say console.

right what do you want to repeat and

then we can read this

in and actually it might be easier if we

change this Loop counter to Second so if

you highlight the section hold alt and

press down arrow twice we can move it

down and say and how many times do you

want to repeat it there we go so now

what we can do is we can use this

message inside here and then Loop

depending on how many times the user

typed in whatever so we can type in

hello world get this exact value and we

can say 10 times there we go so now this

entire program is fully customized by

the users

input so let's just have a quick recap

so we started off this code I print in

the console right line five times to the

screen we wrote Our simple far Loop that

went from in I equals 0 to I is less

than five and we did I ++ because we

want to count up in ones you can place

the high inside here and it'll print it

five times for us so now we've made our

code from five lines down to just three

lines for the follow and then we allowed

the user to type in how many times that

they want to repeat it and we change the

condition from I is less than 5 to I is

less than whatever the user has typed in

installed within the loop counter and

then we hit a problem where if we

entered zero anything less than zero the

for Loop would just not run and it might

look like some things went wrong so we

added a little if statement in here to

make sure that the user has entered a

value above zero and if they haven't we

can let them know we don't need another

if statement in here because if this has

returned back false we can assume that

the value is something that we can work

with we later tailored the program so we

can change this message that was fixed

to I to something that the user can type

in

manually so now that we know how to use

for Loops let's have a look at while

Loops for loops and while Loops still

iterate through code but they're used in

slightly different contexts for Loops

are used mainly when you know how many

times you can iterate through as in this

example we're going to print to the

console exactly 10 times but in a while

loop you might print until a condition

is true for example if you asking the

user to type a number and they kept

typing in something else you don't know

how many times it's going to take the

user to type in correctly so in that

case you would use a y Loop if you

executing a certain amount of code and

you're only doing it a fixed amount of

times then it's always best to use a for

Loop so first let's try and convert this

simple for Loop that just prints I to

the console 10

times just like so from zero all the way

till 9 so let's comment out this for

Loop and have a look at this syntax for

the while loop so type the world while

and then in here we have our condition

and then we can open our curly braces

and while this condition returns back

true it will always go inside the curly

braces and when this condition returns

back false then we'll exit the while

loop so let's try and mimic exactly what

we're doing inside here there's three

sections to the for Loop we Define the

variable that we're going to use for the

for Loop counter and we give it an

initial value we give it the stopping

condition and we give it how many many

times the value needs to go up every

time the for Loop iterates in the while

loop situation we only have the

condition available so let's take our

condition and paste it straight into

here and now we need to store our

variable so just because it's a y Loop

and we can't store it inside here we

have to do it outside the while loop so

let's make it in I and we'll make it

equal to zero so we've got our variable

we've got our condition now we need to

set

i++ so inside the Y Loop we can say i++

and then now we've achieved this entire

line and successfully converted it let's

try and put the console right line in

and see what

happens and let's run the

code so it's going from 1 all the way

till 10 and the reason why it's going

from 1 till 10 not from 0 to 10 is

because the i++ is at the start let's

move it down online using alt and down

arrow key to move it down and let's run

it again and you can see it goes from

zero till 9 and if we just block comment

this out

and undo the for

Loop you can see that they're doing

exactly the same thing so let's bring

the Y loop

back and as you can see what we have is

we've defined our I variable made it

equals to zero while the I variable is

less than the value of 10 we print the I

to the console and increment I by one

and we keep looping this until this

statement returns back false and then we

exit the Y Loop and jump underneath the

the curly braces and hit the console

read line and this is the output that

we've got so now I've just copied the

code from the if statement video and

let's make this code a little bit better

so let's comment out our while loop and

our for Loop that was just used for

explanation purposes and this is the

code that you've saw earlier on in the C

we read in the first number Star as a

string use that string to convert to an

integer star number a and Then star

number B and these two blocks are

exactly the same except you have enter

first number and enter second number

and we store number a and store number B

then we store the value of them both

multiplied inside our int answer

variable then we ask the user what's the

value of both answers times together

give them a chance to input their value

convert it to an integer and then we

check the value and print well done of

Cl but it was wrong now if we run this

code again and we type in 10 and 10 and

we know the value is 100 but let's just

type in 90 now this is kind of a

problematic user case we've asked the

user to type in a value and they've got

it wrong and then when they press enter

the program just closes there's no

opportunity to Loop or try this again so

what we can do is add a y Loop in here

and we can say while the answer is

incorrect we can keep asking the user to

keep on guessing so instead of reading

the input here let's turn this little

section into a question what's the value

of number a * number B and instead of

allowing an input here let's just put a

question mark and then let's add a

console right line just to give a little

bit of a gap and then in order to do our

while loop we actually want to create a

boan variable just to track if the

answer has been correct or

not this is the section that we want to

loop around in the while loop we want to

read in the user's answer check it

output a message and then give them the

opportunity to read in another value so

they can guess again while the answer is

not equal to the actual answer

then we can loop around our code so we

have to modify a few things here so we

can't just take this code and place it

inside the problem with doing this is

the variables that we want to check

answer is outside of this y Loop but

actual answer is inside and the reason

why there's a red line here says it does

not exist in the current context because

we're trying to check a variable that

hasn't been defined yet so what we can

do is take the Declaration for the

actual answer and place it outside here

and make it equal to zero and instead of

redeclaring it inside the while loop we

can take this in and get rid of it so we

set the actual answers to zero set the

answer to the multiplication while the

answer is not equal to the actual answer

then we want to loop around so we don't

actually need another check in here this

while loop will keep on repeating if the

answer is incorrect so we don't need a

path for correct and incorrect if this

while loop still continues then the

answer will always be incorrect so what

we can do to double check is we can say

if the answer answer does not equal the

actual answer then we can place this

message inside here and get rid of this

branch and then after the while loop has

continued we can say Well done so let's

just read through the code while the

answer is not equal to the actual answer

and when this while loop first enters

it's going to be true because you're

going to have 10 * 10 which is 100 and

the value of answer is zero so this will

return back true because these two

values are not equal to each other then

we're going to ask the user to enter the

value then we're going to read into the

actual answer check if they're not equal

to each other and then we output a

message because we're asking a question

here we want to ask another one using a

console right Line enter your answer

callon here we go so let's have a look

and see how this

works 100 100 what's the value 100 * 100

and let's just get it wrong on purpose

90 CLA but is wrong and now we get to

answer again

and we can keep answering this as many

times as we want until we reach the

correct answer which in this case is

10,000 and we've got the answer well

done so to make the console look a

little bit better cuz this is all in one

block we can separate these with console

right lines maybe after this line we can

print out a new line and then after each

guess we can print out another new line

so after each guess we'll be down here

we can just print out a blank console

right line and just after you ask them

to enter the second number we can print

another one here and let's just see how

that looks 10 10 now it looks a lot

better because you ask the user to input

the numbers then you ask them the

question and then you get the answer and

then it keeps looping around like

this so there's a little gap between

each one so you can efficiently read it

instead of it all being closed off

together now if you answer it correctly

100 now it says well done and when we

press enter again the program now closes

so that was using a while loop and now

we have something else called a do while

and a do while is very close to a while

except that the while will only happen

if this condition returns back true and

a do while will always happen once and

it'll check the condition at the end so

let's have a look and see how this works

so we write the word do and then we have

the curly braces and then after it we

have our while condition and let's just

copy the condition from the old while

loop and we can say answer is not equal

to actual answer so as you can see with

a while loop it will always check the

condition first first and then it opens

the curly braces in the do there is no

condition it will always do everything

inside the curly braces once and then it

will check this while condition if this

condition is still true then it'll loop

around and check it every time but it

will only check it at the end while

Loops will check the condition as the

first thing it does and then check it

every iteration so let's move our cord

down into here and let's just comment

out our while loop just to see how this

works and if we run it again to make

sure it's working fine 10 10 90 90 50

and then finally 100 and it says well

done so you could be thinking what are

the differences between a while loop and

a do while in certain cases you want the

while loop to make sure that this

condition is true before even continuing

in this specific case we always want the

user to answer the question it doesn't

matter if it executes once because

actually that's the behavior that we

want we've asked the user a question

right here so we want to always be

looping at least one time the while loop

user case doesn't quite apply here

because we don't necessarily have to

check if they've answered correctly if

they haven't even inputed a value yet we

always want to be able to give them the

option to do it once and then we check

their answer at the end they could get

it on the first answer 10 10 100 and

it'll still be fine the same is true

with a while loop but it'll be easier

and it makes more sense to read your

code when you have a while loop you can

see that this bit of code depends on a

condition whereas in a do while the

first iteration does not depend on

anything but the second and third and so

forth depend on the condition so

depending on your user case and what

situation you want to use it you should

either pick a while loop which will only

occur through a condition or pick a do

while that will occur once or multiple

times after the first condition has

passed

so when you have if statements that are

pretty basic you can actually make them

quite easy to read through the

conditional operator also known as the

tary operator so let's see how this

works let's say we have an INT age and

we set it to a minus value and we know

that this value can never be someone's

age because it's negative but let's say

that we have a situation where you ask

the user for their age and they've

accidentally typed in -10 and we know

that that value is not actually true so

we could write something like this age

is greater than or equal to zero then we

can print out

valid and then in the else we can take

this console right line and we can print

out invalid so that means if we run our

code now you can see that it says

invalid and if we change it to positive

10 now it says valid so this seems a

little bit long we are just checking one

condition printing a value and printing

another value if that condition returns

back false so our conditional operator

works like this we have a condition

followed by question mark then the value

if it returns true then the value if it

returns false let's see how you can

rewrite this if statement into one

simple line so let's take this in

steps these two values are both strings

that we've typed in so let's make a

string and we can call it result and

let's make it equal to our Turner

operator so the first step is to get the

condition and the condition in this case

is if the age is greater than equal to

zero we can follow that by a question

mark and then if this returns back true

we want the string to say valid and we

can have a call on and if it returns

back false then we want the string to

say invalid and we can add a semicolon

and then straight after it we can just

have a console right line that just says

result as you can see now that we get it

twice we can verify one comes from the

if statement and one comes from the turn

operator and now if we make this say

positive 10 then you can see it both

says valid so if we comment out the if

statement and just see this running on

its own just to double check there we go

and because this is just returning back

a string we can actually just take all

of this code in the conditional operator

and paste it straight into the console

right line so you run the line of code

now we get exactly the same thing if

this hurts the readability of your code

then it's best to just store it and then

use it after if you can read this code

and it's better for you in the long run

later on you might want to dismiss this

variable and do it straight inside the

brackets just to save a variable and

then print itto the screen straight away

it'll be better for memory if you just

print it out straight away but if you're

not familiar with this notation yet and

you want to get used to it then that's

fine as well so a quick recap we had an

if statement with a condition and if

this returned back true we printed valid

and if it return back false we' print

invalid and this is pretty simple the

only difference between these two paths

is this string literal that we've typed

into here so we can pull the string

values out and place them in our

conditional operator we can say string

result is equal to if the age is greater

than or equal to zero then we store

valid inside result and if it's not

greater than equal to zero then we can

St invalid straight in our string after

we've stored this value we can print

results straight to the screen which

will either give us valid or

invalid when when you're dividing two

numbers you might get an ugly output

just like

this the user sometimes doesn't want to

see such long decimal places and maybe

you just want to prevent it to

81.3 in order to do this we need to use

a format pattern so the language

understands what we're trying to achieve

so if we make another console right line

what we can do is use string. format and

inside the brackets we can give it a

format and then the object that we want

to format so in this case the object in

the second parameter is our value so

let's open some speech marks go to the

next parameter using the comma and just

type in our value and then inside the

speech marks we want to type in the

pattern if you type in curly braces as

zero then zero represents the first

valid you in this list we can do a space

followed by curly braces one and then

add another comma and we can add a

thousand and what this means is value

will get place where zero is and 1,000

will get Place one is so let's run this

and you see the exact same thing's

happened plus dis place and now we have

the Thousand which follows this pattern

exactly if this is confusing don't worry

too much about it I have the next

section all about string functions so

let's get rid of the one followed by the

thousand and run it again and we should

see the output being exactly the same

there we go so now let's see how we can

make this better in terms of decimal

places so just after the zero and the

zero represents the first value we can

add a call on and this column will tell

us what format we want the value to be

displayed as we can type in

0. and what this means is all the

numbers before the decimal place will

get displayed and only two numbers after

the decimal place will get displayed so

let's try run this now and you can see

that the code output is a lot cleaner

one thing to note though by default this

will round it up 0.37 becomes

0.4 not to worry though cuz this is what

the user will see on the screen and it

won't affect the actual value of the

variable if we print out value straight

after you'll see that the difference

hasn't been changed it's still 0.3 in

the original variable so that's one

method of doing it what we can do now is

just use controll D and copy it down and

we can use different formats to show you

what it looks like if we remove the 0

and then one of the zeros and leave it

like this and it gains one decimal place

each time you'll see what the output

looks like we just have the 81 which

essentially makes it into an integer

because it's a whole number then we have

81.0 and the reason why it's 0 is cuz

0.3 will round down as it's under

0.5 the 03 is going to round down as

it's under 05 so it'll round down to

zero and then finally we have the

81.4 and just like before this will

round down

to4 and just like before this will round

down to

0.4 this will round down to 0 four let's

say you wanted to get rid of trailing

zeros because you don't want them then

you can actually change this zero to a

hashtag once you run the hashtag then it

will trim off any trailing zeros so what

this means is it will try and put it in

this format but if this format happens

to be a zero then it will just keep it

like this and if you just have 81 dot

then it'll also get rid of the dot for

you so then you just left with

81 it's really up to you as the

programmer what you want to display

depending on what context I'm just given

you the options that you have available

so let's say we want to work with money

now and we have a 10d / by a 3D and what

that value would be is somewhere between

3333 and this will be reoccurring cuz

it's exactly 1/3 the reason why I've got

D's in here is because these are

registering as doubles if you type the

value in normally it'll get registers at

in 32 and the problem with in32 is when

you do this division it will process the

division as an integer and then store it

as a a double the issue with that is in

this case and in this case both values

will get truncated let me demonstrate if

I remove the D from both sides and down

in here it will'll see what the

difference is and let's print out money

and let's see what's happened in the

first case this isn't actually an issue

because we have a decimal place here

which means this will be a double so an

integer divid by double will give us a

double but we just place the D here just

so we don't forget and it makes the

compiler's job a lot easier cuz it

doesn't have to convert it from an

integer to a double and in this case we

have an INT that's divided by an INT so

we'll get 3.3 reoccurring but because

it's processing as an integer then we

just get three being displayed and this

is not the intended result so let's just

replace these back to D's and we'll see

what we get now which is the 3.3 perfect

so let's say that we want to display

this as actual currency because you

never see it displayed like this it's

always to two decimal places just as

follows so we can follow the same

approach as

above we can do string.

format 0 callon 0.0 and the curly brace

and then common money cuz that's the

variable in reference now if we run that

let's see what happens

3.33 that's perfect and now what we can

do in here because this is the pattern

we can actually type in anything before

after this and it won't affect the

pattern just like so we've added some

Extra Spaces in here so if we get rid of

these spaces we can actually just play

is a pound sign in front of it and then

let's see what happens when we run it

perfect so now we actually have some

money and we can say in here £10

/3 equals this and now our output looks

a little bit better because we have some

formatting now the issue is when we

start getting into negatives let's see

what happens so we can say -10 IDE by 3

equals something and when we run we'll

see that the format is actually

incorrect now you never see the negative

symbol after the pound symbol it's

always this way around in this case what

we're doing is we're telling the

compiler hey we only want to see two

decimal places but always keep in this

format the pound sign followed by the

value and the value of money is -3 so

it'll Place -3 inside here and then add

the pound sign before it but we don't

exactly want that so we can add some

something called a culture info but we

can also give it some other formatting

some other way so let's explore some

other

options if you use money to string then

what we're actually doing here is using

the double variable and converting it to

a string I me get passed back a string

representative of this value and that's

fine and when we run the code you'll see

no difference between these two at this

point they're both being converted to

Strings but what we can do in this case

is when we open these brackets it tells

us there one of four what does this mean

exactly so in our two- string function

we have four ways that we can call this

function the first way you give nothing

in the

brackets and if we press the down arrow

the second way is to give it a format

provider the Third Way is just a string

format and the fourth way is both a

format and a provider so we'll use all

of these in a second but let's explore

the easiest one if you add a string in

there which gives us the third overload

string format and we can just type in a

Capital C let's just see what

happens perfect so now we have the best

format display to us and the reason why

this works is the C program will

recognize that the format of C is

telling us that we want to display

currency so then it's going to your

local machine and figuring out where you

are based on your date and time just

down here when you have adjust dat and

time it will figure it out based on this

we have UTC time and we have the date

and time listed up here based on that

information it can figure out that we

are indeed in the UK and we want to

display a pound sign and because the

program knows that it's - 3.33 it'll put

the pound sign in the right location so

now that we've explored C let's have a

look at the other ones that are

available so if we use contrl D and copy

this down three times we can use C 0 C1

and C2 and now if you run this again

you'll see what the output gets you c

will get you currency in it most native

form which is what we're used to using

in the UK c0 will only return you back

with the whole number C1 will give you

one decimal place and C2 mimic C on its

own as they're displaying the same

values now this is where it gets

interesting if you want to display

different currencies then you're going

to have to use this thing called a

culture info which will be able to

retrieve back different currency formats

based on a certain country code that you

give the

function so let's have a look and see

how that works if we scroll to the top

we can add a new library called system.

globalization inside globalization we

have a culture info so let's see how we

can tailor these to make it look a lot

better if you're worried about different

currencies so we us CW tab tab and we do

the same thing money. two string we add

our C and this is where our format

provider fits in so we can start type in

culture info it should come up like this

press enter and inside this is a class

that's why it went green if we press dot

then we get the current culture the

current culture will decide based on the

machine that the program is running on

if this program is running on a cloud

computer and the cloud computer is

stored in Europe then it might display

euros and if it's stored in America then

it'll display dollars so you want to

avoid using current culture if you're

going to scale your program worldwide in

the case of this example I'm going to

leave it because as you can see when I

run it it will display the same thing as

as the other ones because this one will

also recognize that my current culture

is UK and I'm also telling it to use the

current culture so in both of these

cases it's figured out that my current

culture is indeed the UK and it's

started using the pound sign but let's

say we want to give it a different

culture then what we can do if we

duplicate this line using contr D and

remove this section just after the dot

we can actually say create specific

culture and inside the parentheses we

can actually add a string and it says

here a predefined culture name or the

name of existing culture info object the

name is not case sensitive so in the

case of the UK you have English GB and

now we need another bracket at the end

and then if we use control D to copy

this down we can change GB to us let's

run this again and see what

happens these two don't actually output

anything different because we're just

doing the normal culture and then we're

telling it to find the GB culture which

is exactly the same thing for me so we

get these two values exactly the same

and apparently it's picking up that in

the US you have brackets around your

negative values which I can't personally

validate but this is how it's showing up

so let's try some other cultures and see

what happens so you can try Au for

Australian then you can see that they

actually use a negative followed by the

pound sign and then the value so even

though they're both in dollars one's US

Dollars and one is Australian dollars

and apparently the US uses brackets for

the negative values but Australians use

the negative value like we do so let's

just have a quick recap so we can

display the value just like this but

we'll have a problem with multiple

decimal places we can use these format

options to give us zero decimal places

one or two decimal places and then when

we use this format the problem with it

was when you display negative numbers

you get a nasty format where the

negative value actually comes after the

pound sign which is not correct and you

don't want to Output that to your users

instead you should use the C format to

tell the compiler that you want to

process this as a currency currency

processing is better because the

software will understand that the value

you're trying to represent depends on a

certain strict format just as follows

you don't want the negative to be here

instead of before it because that

doesn't look very nice to the end user

alternatively you could use this C info

if you're going to scale your program

scale your program means that it's going

to live on servers and this culture info

especially the current culture depends

on where the program is being run on

because it depends on the system time

different servers will have different

dat times set because they'll be in

different countries so you want to be

careful and if you want to display in a

certain currency you can create a

culture specification and pass the

country code inside the brackets and

then you'll get the correct output and

it doesn't matter where your program

lives if you always want to gener

generate in pound symbols then you

always use create specific culture and

always specify

NGB upon doing so you always get the

pound sign and it doesn't matter where

your program

lives so throughout this course you've

seen this notation a lot print out value

using console right to give the user an

opportunity to write on the same line

reading their value as a string and

store it in a variable and then use that

variable to convert it to an integer 32

and we're passing the string input

inside here and then we convert it to an

integer if we run the code you can see

that this actually works perfectly

assuming you actually enter a real

number like 10 we press enter and it

works and it outputs back to the screen

using this console right line and if you

press this restart button we can start

the program again and let's say we

accidentally type a character like L and

we press enter and we've get a format

exception now this is problematic

because our program will now close if we

press continue it doesn't even hit this

line or this as soon as it hits the

exception here our program just closes

if we use debug start with debugging and

now we're actually running the program

outside of visual studio so we won't get

that pop up when the error happens let's

see what happens if we type this in now

we can say 10p and press enter and now

we get that the program has stopped

working you've probably seen this pop up

a lot throughout Windows in different

versions of Windows because when a

program has a fatal error or an

exception you see there's pop up appear

here and the only thing you can do is

either debug or close it normally the

debug option isn't available but because

this is a c program and it's made

through Visual Studio and the visual

studio is being installed on the system

then it gives us the option but really

all we can do here is just terminate the

program it doesn't matter what happened

because we can't recover this if we

press close then it's literally just

going to close down it's going to tell

us the exception and then press any key

to continue and as soon as you press any

key then the program just closes so this

is very fatal for our code since if we

had some precious data stored in here or

anywhere else in the program it is now

lost before we can do anything this is

problematic and we want to avoid this

the best way to do this is to use

something called try pass right here the

program is assuming that your string

input is a perfect input and then you're

immediately converting it to an integer

now like I said what if this had 10 H in

it the program is going to cause an

exception and then the program is going

to close so let's see what we can do

with this trass function so the notation

is int.

trass the first parameter is a string s

string s is a string containing a number

to convert so what this trip pass

function will do as it says here

converts the string representation of a

number to its 32 bits signed integer

equivalent a return value indicates

whether the conversion succeeded okay

perfect so we'll get into that in a

second so our string s is a string

containing our number to convert and in

this case we want to be using the num

input so we copy and paste that down and

then we add a comma cuz we have more

than one parameter now we have an out in

result when this method returns result

which is our integer contains the 32bit

sign integer equivalent of the number

contained in S and S in this case is our

first parameter which is num input if

the conversion succeeded then we get the

value inside our result

or zero if the conversion failed the

conversion fails if the parameter s is

null or string empty is not of the

correct format or it represents a number

that's less or more than the maximum

value that the integer can store this

paramet is past uninitialize any value

originally supplies in result will be

overwritten let's dive in to see what

this means because it's an out in result

what this means is we can actually

process a variable inside here like intn

so we can say out num and what this will

do is when this line executes it will

try and convert num input and place the

value inside num so we don't need to run

this convert anymore we can just specify

that num is equal to zero so we've read

in the string that contains our 10 H and

we have our Nome that contains zero

let's just remove this console right

line so now we have our variable set up

let's see what happens if we do a

console right line and just print num

after this let's see what happens to the

code so if we type in 10 H and press

enter the value of num is zero but what

you notice now the program hasn't closed

because it's tried to pass it instead of

just passing it automatically and then

cause an exception so the reason why Try

pass is called try pass is first it

tries and then if it succeeds this will

contain the value and if it doesn't

succeed this will contain zero to know

if the number has actually been

converted we actually have to use the

bull return value so what this means is

because it returns us back a bull we can

store it inside a bull and we can give

it the name of success bu success equals

in. trass we try and pass the num input

and we place the value inside num the

reason why having this bu success is

very very essential let's say for

example the user actually enters

zero so technically as of this point num

zero is actually correct because we've '

got zero and we initialize it to zero

but of course after the trip pass runs

this is still going to be zero now we

don't know if the value zero is because

this has succeeded or the value is zero

because we initialized it to zero let me

demonstrate if I type in a zero here is

this zero being converted because this

was successful or is it presenting me

zero because this has been initialized

to zero and the only way we can know is

because our tripod pass will return us

back with true if s was converted

successfully otherwise false we can

actually write an if statement in here

if success open the curly braces and we

can start to type something in here so

we can take our console right line and

place it up here using alt and up Arrow

key and then in the lse we can say

failed to

convert so now we have a mini setup here

if it's been success uccessful we print

out the number if it hasn't been

successful then it'll go into this path

and it'll print fail to convert let's

have a look and see how this works now

if I press zero and I see Zero that

means it's followed down the success

path and this zero actually means it's

converted my input instead of just the

value that it's being initialized by if

you run the code again and type in 10 H

then we actually see it says fail to

convert and why that happened is we have

10 H appear and then this will return

back false when it tries to convert num

input and then after it hits this line

since success will be false it will

Dodge this path going into this path and

output fail to convert what you can do

if you really wanted to is you could

wrap this around in a while loop so

let's see what we can do so we can

specify a success and make that equal to

true and then we can add a while loop

with success in there and then open the

curly braces and put all of this code

inside here now we're going to have some

errors because we got success twice but

what we can do which will be a lot

easier is we can place this straight

inside our bracket and the reason why is

because this returns us back at booing

so we don't need to store it and then

use it another thing we can do is

instead of having Inn and then num right

here you can actually make this a

declaration as well so if we just remove

this value we can write out

in n and this will do exactly the same

thing it just uses less variables while

our success is true we come into here

and run this code if we failed to

convert then we still want it to be true

so we can loop around if we've converted

it and the value is successful then we

can actually make our success at this

point false cuz that will stop the W

loop from going around let's run the

code we can type in 10 H 10 a 10 a etc

etc and as soon as we type in a real

number then it will print it out and the

while loop will cancel and now right

here we're start on the console readline

now if we press enter the program will

close so let's have a

recap we use inter. trass so we can try

and pass a value before the program

assumes that the value is in the correct

format we give it a string variable and

it returns us back with a fresh new

integer called num or whatever you want

to call it inside here we we can place

this straight inside the condition of an

if statement because this function

returns us back with a true or false

value otherwise known as a bull if this

has been successful we stop the while

loop and print the value out to the

screen and if it hasn't been successful

then we print fail to convert and

because our success is still true it

will Lo back around to the start of the

while loop and ask the user to input

another value to try and convert this

while loop will only exit if it's indeed

been successful ful and the only way it

can exit is by having the success being

equal to false and the only way that can

happen is by having a successful

conversion the use of trass in every

program is very essential because if you

have a faat exception then your program

will close and you will lose all the

data the problem with that is you might

lose anything that you haven't processed

yet at that given point in your program

so you want to try and use triy pass

because you'll avoid any potential

exceptions and you can let the user know

if the conversion has been successful or

not and write yourself the same while

loop just like this so you can tell the

user hey you inputed it incorrectly but

this is how to do it right and make sure

that your program doesn't execute

incorrectly and make sure your program

doesn't just close unexpectedly and then

an error will be

caused in this exercise I want you to

print out some sort of times table and

what I mean by this is you want to ask

the user to input a number for the table

and then write WR a for Loop to print

out the x times table X is depend on the

value that they entered if they enter 10

in here then this will be the 10 times

table let's get started so we can have

our normal console.

write enter a

number and then use our int number

equals to convert to int32 and we can

say console. read line because we want

to read whatever the user types in then

we can have a four in I equals z i is is

less than 10 we're only going to print

up to the 10 * table so 1 * Something 2

* something all the way up to 10 times

the value that they

entered immediately we acknowledge an

error here when you do your 10 times

table for example you do 1 * 10 2 * 10

and so on and so forth the issue here is

we're going from 0 to 10 but because

it's less than 10 we're actually going

from 0 to 9 so let's change this to 1

and then less than or equal to 10 10 in

both cases we were actually looping the

same amount of times the issue is this

was starting on one and the other one

was starting on zero and the first one

was finishing on nine and this one's

finishing on 10 and we want them to go

from 1 till 10 instead of from 0 till 9

even though that's the same amount of

iterations it gives us different

values so in here we can just have a

console right line and this is how it's

going to work we're going to have our

first number times about our second

number and that's going to be equal to

something so our first number is our

value of I cuz that will say 1 *

Something 2 * something and so forth our

second number is whatever the user has

typed into the calculation which will be

number and our third number is going to

be the actual result of the calculation

which are these two times together so we

can have I star number and that should

be it let's run it and give it a shot

we can do our 10 times table and if we

press enter you can see that it's fully

working this will be our I variable

going down the side the second number is

what the user typed in and the third

number is this multiplied by this gives

you the output let's run it again with a

bigger number and it seems to handle

okay and because this is an integer

you'll expect to work okay up until

around 2.1 billion which is a big number

for some time tables anyway so just just

a quick recap we looped through from 1

till 10 instead of from 0 till 9 because

we made sure that when we do our

multiplications we don't want to start

the value at 0er * something we want to

start the first one at 1 * something and

we don't want to end at 9 we want to end

at 10 so using 1 and less than or equal

to 10 will get us the values that go

from 1 till 10 and that will make our

output look a lot more concise we're

using conditional format in here adding

the zero and curly braces and then the

one and then the two and in order to

insert the values in it's a comma

delimited list after the speech marks

the first one goes in the right order

zero is I one is number two is I times

the number running that code we'll get

this output there you

go so in this exercise I want you to

have a look and see if you can make the

Fizz bus game fiz buuz game is one of

the most popular interview questions

you'll come across

and the way to do this is to create a

for loop from one to something in this

case I'm just going to use 15 and inside

the for Loop you want to decide if the

numbers divisible by three and five then

you want to print Fizz buzz if it's

divisible by three then it's Fizz five

it's buzz and if it doesn't fall into

any of them categories then you want to

simply just print the number out to the

screen let's get started so we can have

a four and because we know how many we

want to do we want to do in I less than

15 i++

and then inside here we need to use the

modulus operator which is the percentage

sign so if you remember from the

tutorial in order to figure out if a

number is odd or even you could do the

number modulus 2 and if that value is

one then it must mean it's odd and if

that value is zero it must mean it's

even but what we can also differentiate

from here is if 10 modulus 2 equals 0

then that means that 10 is divisible by

2 but also it is even and if it's even

that means it's divisible by two if it's

not divisible by two that means it's not

even and therefore it's odd so in this

case if we try to divide three and five

we can have the same calculation but we

can work out differently so 10 modulus 3

actually gives us a value of one because

it's three remainder one so if this

value is anything other than zero then

we know that these two were not

divisible by each other 9 percentage 3

equal 0 which means it's divisible by it

so in our if statement we can say if I

which is the number that we're currently

up to

percentage 3 equals equals to zero and

because we want to check for five as

well we want to say and I percentage 5

equal equals to Zer and that means

they're both divisible by each other so

we can do a console right line and we

can say fiz buzz and then in our lse if

we're going to check if it's three and

we can repurpose this exact condition

because this just checks if it's

divisible by three and then we can print

Fizz and then if we copy this exact

structure down and change this to a five

to check if it's divisible by five then

we can print out buzz and in the worst

case scenario where none of them get

applied then we can simply print I to

the screen and actually there's no point

in starting at zero because normally

these start at one so we'll just do one

and we can make it 15 by making it less

than or equal to so instead of before it

was actually going from 0 to 14 now it's

going from 1 to 15 and they will tell

you this in the interview they might say

counting from 1 to 100 print out all of

the Fizz buzzers so let's run the code

and see what's going

on one two fizz fizz is actually three

so three is divisible by three then you

have four and then five is divisible by

five so you get buzz and six goes into

three so you get Fizz nine you get Fizz

because it's divisible by three then 10

/ 5 gives you buzz 11 12 goes into three

so you've got Fizz and finally when you

hit 15 it goes into three and goes into

five so we get Fizz buzz and the way to

increase this to 100 is by simply

changing this to 100 if you need to and

you can scroll through the entire input

and you can see all the history if you

wanted to make this slightly efficient

what you could actually do is store the

Boolean values for these calculations

before you can store the values for

these calculations before so we can say

bu 3 div equals false and bull 5 div

equals false and what we can do at the

start of each of the for Loop is we can

say three div equals this value and five

div also equals this

value and then every time we find a

calculation for three or calculation for

five we can replace it with either three

div or five div so let's change this one

here and this one here and our five here

and here and because these are values

now we want to actually store them as

true or falses so we don't need to check

if they're equal to zero so we can just

remove them so we can say if three div

and five div are both true and they'll

only be true if these two are both zero

now if we run the code and just make it

15 again just we have a shorter output

you can see that our code is exact the

same the reason why this is more

efficient is we're only doing this

calculation twice whereas before we were

doing it twice for three and twice for

five now we're doing it once storing it

and then making use of it this is a lot

more memory efficient than having to

calculate it every single time it

computes a lot faster and it's better

for readability if the rules of the game

happen to change and this was divisible

by four or six then you could just

change it very easily whereas in the

previous example you would have to

change in every single

case in C when you're constructing

strings that have backs slashes you're

going to need to use double backslash

and the reason for this is the first

backslash is an indication that it's

actually going to be an escape character

you're going to put an escape character

in and then put the character that you

want to display and the reason for that

is they've chose backslash as the

designated character so you can use

backslash and then a backs slash to to

show one backslash only and what this

means is it will use the first backslash

as a reference so it knows that it's

going to process the next character as

an escape character and what these

Escape characters are you can have

things like T for tab and for new line

and you can even use speech marks

because if you outputting some dialogue

and you wanted to say something like

this he

said and then you added your own quote

in there if you want to put something in

quotes then you can't because the speech

mark dictates that this is the end of

the string literal that's why it's cting

it off like this and then now it's not

recognizing that something is just WR

here and what it's actually doing is

you've got two strings specified one

that says he said space and one that's

just empty and then you just have this

something that's just in the middle of

course we actually want to insert the

speech marks within another pair of

speech marks marks this is where the

backslash comes in so you can insert a

backslash before here and this is now

acceptable and the same thing with the

normal backslash because the backslash

character is also going to be the Escape

character then you need a double

backslash so then that equals to a

single backslash when you print it out

let's Show an example so we have C colon

sl/ slash which will return us back a

single slash but visually right now it's

a double slash but when it gets

processed or outputed to the screen

you'll see that it's a single slash so

this common example is when you're

constructing a path together and you

want to use backslashes to denote the

different directories so let's output

this to the screen and see what

happens and as you can see we have a

single backslash because of the Escape

character we don't see both of them and

if we print the speech out to the screen

just to show you that as well then we

can see that there's only the speech

marks around the something and we don't

have the backslash you can tell when an

escape character is being used because

it goes a slight hint of yellow instead

of the normal color for a string so now

that we've worked out that this is how

this works let's see a way that we can

undo this to make it easier for us so

you've used the dollar sign for string

interpolation and you've used the plus

just to concatenate two variables

together and just as a quick example it

looked something like this your name is

and then the name variable and using the

plus you would say your name is followed

by a space and the quotation marks and

do plus name that's fine and now we have

another one so similar to how we place a

dollar sign before the speech marks we

have a verbum identifier and what this

means because it says verbatim it just

means whatever you see is whatever

happens so let's try and Define this

path a slightly different way but still

containing the exact same information

that you see when you print out to the

screen

so let's have a look we can reassign the

path value and just before the speech

marks we can put it at symbol and this

at symbol means it will actually negate

all of the Escape characters it will

process a single backlash as a single

backslash let's take this line of code

and place inside here now what you see

is that tint of yellow is no longer

there so if we print this past to the

screen let's see what

happens we actually get the double

backslash because it's ignoring all the

Escape characters which means if we take

them all out this actually looks exactly

how we want and you don't have to worry

about the Escape

characters there you

go these two are identical even though

one is being displayed using Escape

characters and one's using the verbatim

identifier please note that if you're

going to use the back sln for example

and I'll just demonstrate at the end of

this new line test and if we run that

code again you'll see that it's printed

the path on one line the back sln has

made it go on to the next line and now

it's printing new line test on a

dedicated line that's fine the problem

is if you use a back sln or a back SLT

or any of these Escape characters during

the at symbol then it's going to process

it as literal characters which means you

can't mix a back sln with an at symbol

AS this is just taking the text verbatim

so if we try and run the code now you'll

see the back sln will actually appear in

the code if you wanted to do this then

maybe you could remove the back sln and

add a plus at the end and then make

another string that just has a back

slash n and then this string will not be

processed as a Verbatim string because

we don't have the at symbol right before

it so then we can have the exact same

thing here just to show you

this so now the these two are identical

again just as shown here and as shown

here the difference being that we can't

specify a back sln in the same string we

actually have to concatenate them

together because of the verbatim

identifier so let's have a look at

speech

marks so if we have a string name and we

can make this equal to hello and then

someone and we said before that we have

to use the backslash so we can activate

the Escape characters and that will work

and if we just print that to the screen

to make sure then we get hello someone

in the speech marks if we add the verbum

identify at the start then we see we've

got an error it's trying to process it

differently this is a little Quirk with

the verbatim identifier if you want to

insert a speech marks you actually want

a double speech mark on either side the

double speech marks will get processed

as a single speech mark because it's

within a speech mark the verb identifier

needs to know that you're specifying a

speech mark and this acts as the same

reason why you have a backslash here

it's slightly quirky doing it this way

but it still works as per you

expect and when you're specifying

something like name and we can say hello

someone so let's print name to the

screen and make sure it works in this

case we don't actually need to specify

any sort of Escape character because the

quotation mark is a separate character

to the speech mark and it doesn't

interrupt anything by placing a

quotation mark inside the string just

represents that as its own character

let's run that and make sure it works

there you go so just as a quick recap

Escape characters are very essential

because we can use things like back SLT

to give us a tab character in our

console output this can be very helpful

cuz you can almost mimic the use of

columns inside our console output you'll

notice that back sln is used quite a lot

in the console output so you can

separate lines without having to use

multiple console right

lines if you have a single backslash you

can't have it on its own if the string

is not a Verbatim string you have to

pair it with another character otherwise

you will get a syntax error if you want

to use the verbatim string then you can

put an add symbol before the string

definition and then it will ignore all

of the Escape characters for example

using backs sln Within These speech

marks will mean that the back sln will

literally be printed to the screen and

you won't get get the new line

functionality that backlash M provides

when you want to print speech marks to

the screen using the verbatim identifier

you have to use a double speech marks in

order for it to show up and as you can

see it slightly goes yellow and that's

the similar notation as using Escape

character when you don't have an a

Verbatim string if you're using other

characters that are similar to a speech

mark like a quotation mark because this

doesn't interrupt the syntax of the

language then this is fine because a

character can just be in inside a string

and to the compiler this is just a

character even though they are very

similar the quotation mark doesn't

affect the speech

mark the most common approach for

outputting values to the console is by

having this format you type something in

as a static string and then you add a

variable to it but when you're doing

long sentences this format can get messy

quite quickly so this will just output

both of these on separate lines the name

follow follow by the name variable and

the age followed by the age variable I

just have a console right line to split

up these two sections just so you can

see it visually in the console so if we

run this code you can see that both the

outputs are exactly the same and the

reason why is we can use this back sln

what this back sln is doing is inserting

a new line character just where my

cursor is here which is forcing the age

to jump onto the next line the reason

why this is a slightly different shade

to the age text is because it's an

Escape character back sln means new line

we'll explain this in a further video in

this section so don't worry about that

just know that it means that it produces

a new line so as you can see since we're

adding the name followed by the name and

then the age followed by the age then

it's getting a bit long and a little bit

tedious it's not too bad in this case

because we're not really printing out a

sentence we're just going straight to

another line but let's see what this is

going to look like if we start dealing

with more multiple pits of data your

name is followed by the name and then we

can add a comma and your age is and then

add the age and as you can see while

we're structuring a sentence then it

gets a little bit long we have to

remember to put in the spaces otherwise

our code is going to look funky when we

print out to the screen and the reason

for that is there's no spaces between

the name and the age variables so let's

put them back in and make sure the

output looks all

right now that the output looks like

this let's explore composite formatting

and what this means is we can get rid of

all of these plus symbols and replace it

with curly braces so let's actually

output this string again but just output

it completely static your name is

abber and your age is

23 and now if we've run this you'll see

that both outputs are identical this is

because I know the values for the string

variables appear here and I can input it

straight into here so now that we know

what our sentence is going to be we

don't need to add any pluses what we do

need to do is remove our variables so in

the case of this sentence our name

variable is here and our age variable is

here so we can replace this with this

curly brace notation we can have curly

braces and then we want to start this

value at

zero throughout the string you want to

have these values starting from zero and

going up incrementally from left to

right so our next variable would be our

23 and we can store it like this now

that it's got your name zero and your

age one let's see what happens when you

output it then we just get this and this

is very confusing that's because we're

not done yet so now that we've got our

string typed in we can actually place a

comma in here and now it lets us put any

object in and any object could be a

string an in a double or any data type

so we want to insert the values inside

here going from left to right so our

name is first and then we do a comma and

we type in Age and what this now says is

the name will get replaced by the curly

brace zero and the age will get replaced

by the curly brace one so now if we run

the code let's see what happens and now

you'll see that the output is exactly

the same and now look at the difference

between these two lines you'll see that

these multiple amount of Plus es and not

confusing and let's say you forget this

one then you're going to get a syntax

error and it's a bit frustrating to keep

on typing when you're trying to join

things together now we can clearly see

that this Flo is like a sentence and

this space here looks very unnatural but

this space here looks like it's part of

the sentence which makes our code a bit

more

complete and the same goes if you want

to print something like this and let's

convert this one so if we copy this line

down into here and we just remove the

spaces then it can look something like

this name followed by the name we don't

actually need this space here because

we're going to be going into a brake

line we can replace our name with curly

braces zero and our age with curly

braces one and then straight after we

can type name and age and if we output

this we'll see it's exactly the same

there we go so just a quick recap

printing strings and any other variables

to the screen has been not normally

achieved by doing a string followed by a

plus and then adding another variable to

it adding more than one can be a little

bit complicated and hard to track of all

the spaces that you're going to put in

and you might accidentally miss one of

them like this one here or this one here

are very crucial to the output if you

miss this one then it's going to say is

followed by the name all together with

no space we then introduce composite

formatting which lets us use this

notation of curly bra Z

and curly brace one to give us the

ability to store some variables outside

of the string and then insert them in

dynamically we took both of these

examples and made them down here using

composite

formatting so let's explore a new method

of concatenation that makes this look a

lot better the reason why this is an

issue is you have spaces in here and

sometimes you might forget these spaces

and it could mess up your program this

string here looks very very unnatural

because you're forcing the space but

when you run it you'll see that it is

actually what you expect the spaces are

complete and it makes perfect sense but

when you're writing it it kind of looks

a little bit stupid because you're

forcing the space and then adding a plus

and then the name and then another plus

and then another static string and this

just get a little bit long to type it's

fine when you have a smaller sentence

like this but when the program starts to

grow then it could be a little bit

problematic and you'll have a lot of

syntax ER is when you forget to type in

all the pluses luckily this can be fixed

let's duplicate this line down and let's

see what we can do so we have this thing

called string interpolation and it uses

a dollar sign as a reference we can

place the dollar sign before the

quotation marks and then what you can do

is you can actually keep this as one big

sentence so let's remove all of the

values and have it like this so now of

course this actually doesn't print out

variables because we've just replaced it

with text so if we just run this then

it's completely messed up because now

we're not using our variables and this

can be easily fixed because we've used

our dollar sign now we can actually wrap

our variables in this curly brace and

now you can see it went white if I

double click on it you can see that it's

referencing the

variable wrapping the variables in the

curly brace means that they can actually

be processed variables even though they

are still within the speech marks please

do not remove the dollar sign because

now this will be interpreted as string

text as you can see here putting a

dollar sign at the start will make into

string interpolation and then we get the

ability to have our brackets this can

make string concatenation very easy to

do because you don't have to worry about

all the pluses and adding them together

and don't have to worry about the spaces

as well this looks more like a natural

sentence and what you can do for

practice is you can actually just type

out the sentence your name is Aba your

age is 23 and then from there you can

actually just break down the variables

and know that you'll need one here so

you can replace that with name and then

you'll know you'll need one here so you

can replace that one with age and then

you can place the dollar sign right at

the end and then there you go you have

your solution this method means that

it's a lot cleaner to read easier to

maintain and make changes in the future

and that's the whole point of

programming if your code base is very

difficult to go around and understand

what's going on and potentially make

changes in the future then you've

already made a bad step and you don't

want to get into these nasty habits it's

fine if you're just going to have

something like this where it's not

really complicated at all a string plus

another string that's quite simple and

that's fine you don't have to go the

extra effort to put in the dollar sign

and wrap it around in the curly braces

even though I personally still would

because in my personal opinion this

looks a lot

better than having it like that you can

see that it's a lot neater it takes up

less code and you can see visually that

you're using a specific variable and you

don't have to worry too much about the

spaces because this looks like a real

sentence another method of string

concatenation which is the idea of

taking strings or objects and add them

together to form a sentence of sorts we

can say your name is followed by space

and then add the name followed by space

add your ages and then add the actual

age variable and we can also write the

exact same line using string

interpolation or composite formatting

just like this and like this we can

specify the dollar symbol and put our

variable in the bracket which is shown

here and here and for the composite

formatting we can take the content where

our variable would go and replace it

with incremental numbers 0er and one

wrapped around in a curly brace and then

specify in the correct order zero takes

a value of name and one takes a value of

age if we run this code you'll see that

that the outputs are

identical so let's explore this other

method we have a string test for example

and then inside the string Library we

have this function called concat and it

can takeing any amount of parameters

that we want to give it so we could say

your name is space and the quotation

marks and then we can add a comma and

then we can say we want to add the name

and then another comma and we want to

add and your

ages and the speech marks add add a

comma add the age variable and that's it

and we could just use CW tab tab and

type in test and let's run it and see

what happens and now as you can see we

get the exact same output and because

this just returns us back with the

string we can also just take this entire

line and paste it straight into the

console right line you don't necessarily

have to store it but you can if you want

to and there you go all methods produce

the same output another way you can use

this concap function is by giving it

array we haven't covered array so far so

I'm just going to show you this for

demonstration purposes don't worry about

understanding it so just a quick

explanation arrays are just ways of

storing multiple of the same data type

so if we have these three names inside

our names we can actually use the

console to print them all out so we can

say string. concat and then we can

concat the names together which will

just add all of these three together now

the problem is when you run it you'll

see they're stuck together so what you

could do is just add a space between

them and as you can see this is getting

a little bit tedious

now there's other methods that we can

use to concat some arrays together and

I'll show you that in the next section

but for now don't worry about it this is

concentrating on the string concat

function and we'll just demonstrate that

again so if we run the line of code we

can see that they're all the same so now

that you have four different methods of

printing out exactly the same

information you might be asking yourself

what's the best method I personally

prefer to use the string interpolation

because with this one you can easily

miss the spaces and you could produce a

bad output and it might be stuck

together it's fine when you can see the

lines right in front of you and you can

easily identify the spaces but when

you're adding about 10 variables

together then it could be a problem the

reason why like string interpolation

using the dollar signs and the variables

in the curly braces is that you don't

have to rely on missing out spaces

because it actually forms a sentence and

you just replace the values with your

variable composite format in which is

this one is very efficient and very nice

to look at because it allows you to

specify the sentence and then plug in

the variables after the only problem

with this is if you were to change this

value in the future or to change a

sentence you have to make sure that the

numbers add up just like in the first

example if you only have a short

sentence then this isn't really an issue

since you can see it all visually and

you also got to realize that these need

to be in the right order because if you

put Age first then it's going to put age

in this one and name in the second one

and that's going to be problematic

because it now mess up the sentence

using string interpolation you don't

actually have to worry about anything

because if you wanted to change this to

say your age is then you simply just do

this and that's very easy to do because

in this case you'd say your age is and

then you'd have to change a string and

then now that this is zero we need to

change this from being age and this one

to be name whereas in the first example

you could just easily change the value

in the curly braces instead of changing

this reference and the end reference

conar is not widely used just because

you have all of these methods available

where you can easily add stuff together

but there could be user cases later on

down the line where uses just so it's a

bit more obvious and it helps the

readability of your code and maintain

within a big project or just on your

personal projects when you're looking

back at your previous

code when you create an initialized a

string variable you might want to give

it the value of an empty quot and this

will just default the value to

essentially nothing you're giving it a

value but at the same time the value

doesn't actually contain any sort of

characters if we print this out to the

screen then you won't really see

anything this video is going to focus on

string.empty and string. empty will

actually just represent these quotation

marks string.empty will represent these

speech marks so what we can do is we can

type in string using a DOT and then we

can type in empty just like that we've

initialize our string name to a value of

empty now this isn't necessarily used

when you're declaring an initializing it

because this is almost identical to just

defining a string on its own main reason

why this is used let's say we actually

ask them user to input their name so we

can say a console. right enter your name

and then we can take this string

variable move it down using alt and then

arrow keys and remove this and let's

initialize it to a console. readline to

read in the value from the

console and now what we can do is using

our is string interpolation method that

we just learned we can print to the user

your name is and then the name in The

Curly braces now let's just see what

happens so let's actually type our name

in abber and it says your name is ABBA

but what if the user just types nothing

and just presses enter Then it still

says your name is blank now let's see

what we can do here we don't necessarily

want to say your name is blank we want

to check what this value is so we can

say if the name is not equal to empty

quotation marks then we can output your

name is and then then else we can say

name is empty

and then if we run it now and press

enter it says name is empty so we can

use string. empty instead of these

quotation

marks this looks a lot better when

you're trying to compare it your

name is and then when we press enter

it says name is empty this just helps to

keep your code a lot tidier just so you

don't have to write the two quotes if

you accidentally put a space in there

now your if statement is incorrect if

you always use string. empty then it

just helps you to make sure that your

code is going to to be maintainable and

readable in the future reading string.

empty gives you a better representation

of what's going on instead of two open

quotes if there is a situation where you

want to check if one string equals

another string then you can do the

following if message equals equals

compare then we can print out same and

then else we can print out

different and as we can see here they

both have a capital H and they both say

hello so they should be identical let's

run the code and find out so they say

same which is exactly what we'd expect

so we have another method of being able

to do this so even though we can apply

equals equals to two strings it's

normally better practice to use equals

and then place the other string inside

the brackets so you're saying if message

equals compare now if you run the code

you'll see that it's the same

output in certain circumstances where

you want to ask what the user has

entered enter your name and then we can

use a string name equals console.

readline we can say if the name does not

equal to empty strings then from there

what we can do is type in your name is

plus

name and then we get some output

your name is but since we're

checking if it's not equal to that that

means they've entered something in let's

see if there's another way that we can

actually write this so if we just

duplicate it and comment it out so we

got it for reference so what we want to

do is we're checking if name is not

equal to empty quotes so we can say name

do equals empty quotes but what this is

telling us if the name equals empty

quotes then we should do this so then

what we can do when we learn about the

if statements is we can use the not

operator and we can say if the name not

equals to empty quotes then we proceed

with this line else we can just

say and valid name

input let's run that and see what

happens if you type in then it

passes fine and if we type in nothing

then we get an invalid name input the

reason why equals equals and the equals

function are different is because lower

level using equals equals will actually

compare the reference in the memory and

the reference in the memory is what's

used up when you actually create these

variables so what this is doing is it's

comparing the value that's actually

stored in memory to the other one that's

stored in memory now the problem is when

it comes to something like arrays where

you actually have the same value but

they're slightly different let me

demonstrate so I'm just going to use

this code because we haven't covered

arrays yet so I'm just showing you an

example if we have a chart array that

literally says hello and then we can use

this line of code to join them together

but this is actually an object instead

of a string but if you see what we can

do in the if statement we can say if the

message which is equal to hello we can

use message. equals and we got the new

compare and this is actually an object

so now we're comparing a string with an

object and generically speaking an

object can hold any sort of value

because all of these variables that are

strings in double Etc they're all deriv

from something called an object which is

just a very generic way of storing

information so since these two data

types are little bit different they

still evaluate the same thing what the

equals function is doing is actually

checking the literal values of the

variables instead of checking the values

that are held inside the memory address

location and the difference here is even

though we have a character array that is

giving us the values hello and our

string is also given us the value of

hello if we run this line of code that

uses do equals then you'll actually see

that the second same actually represents

the bottom if statement so what that's

doing is it's actually giving us back

the comparison saying that it's true and

it is indeed true because hello is is

literally equal to hello but if you were

to use the double

equals then what you'll realize when you

run the code is now it'll say it's

different because what it's doing now is

it's taking a string object and

comparing it to an object and they are

not different because of their memory

and that's what the equals equals is

comparing it's not only comparing the

value it's comparing the memory address

location and that's going to be vastly

confusing because you don't want to hit

these situations so it's always best to

use the equals function because the

equals function is concerned about the

values rather than where they are in

your memory I hope that made sense and

it wasn't just confusing but basically

when you use equals equals it checks the

values that are actually wrote in your

computer's memory instead of the value

itself it will do both it will check the

memory and check the value whereas when

use do equals it is only concerned about

the values and that's all that matters

when we're doing an if statement like

this we're checking if they're both the

same so it shouldn't really matter how

and where they are stored the only thing

that matters is the values contained

within the variable we're only caring

about the values and not where they're

stored so when we have a string variable

defined in C we have it like this and

when we print it out to the console we

get the entire string being

displayed but what if you want to return

the first element back and not the rest

then what are your options so in the

later video in the of course we covered

the substring function and technically

you could go from the first character

till the second character and you will

just get C back so that's index zero

till index one and you'll get the cback

now the problem with that is that's an

extra function call of something that

should be quite simple and it is a

message is a string and strings tend to

be an array of characters which is

similar to this we haven't learned about

array so far and we'll cover that in the

next section but what an array

essentially is is it lets you store

multiple of that data type so we could

have a chart array that holds exactly

the same value and each position will

hold each one of these characters and

the reason why I'm mentioning this is

because with strings you can use the

square bracket notation so if you add

some square brackets at the end it says

here it returns back a Char and it takes

in an index gets the CH object at the

specified position in the current string

object so if we use message zero you can

see that returns us back with a ch ch

right here gets the CH object so we can

call this on a string and it can return

us back with a character because like I

said with the array of characters that's

essentially what a string is each one of

these is just its own independent

character and a string lets you pair

them all together so if we print this

out then we should get C because it's

zerob based so if we copy this down and

use

one 2 and three then what should we get

we get

C then we get actually nothing

technically because it's a space

character and then the third character

which is the fourth in the list because

it's zero based is going to be our I so

let's just print this out to the screen

see what happens there we go so even

though we don't see it visually there's

actually a space character right there

and that's the third character in The

List which is right here so of course we

don't tend to know how long the string

is unless we can see it visually and if

you were to reference one of these

values and the string is not actually

that long then you have a problem let's

demonstrate if we have the string just

called C and let's comment out the rest

then that's only two characters so only

zero and one will be applicable these

two will return back something let's

have a look and see what happens so the

first character and the second character

will run fine and let's put a console

read line in there just to break it up

so the other code doesn't run yet if we

run the code we get our C and that's the

max length of our string so far it's

zero and one now if we press enter let's

see what happens we get an index out of

range exception index was outside the

bounds of the array and the reason for

that is we only have two characters

inside our message and we're trying to

display the third and the fourth this is

problematic because we literally don't

have any of them characters to

display so what we can do instead is

actually write ourselves a little Loop

to go through the entire string and

return us back all of these character

positions instead of us just guessing

how long the string is so if we return

this string back to normal and then just

after this little section we can write a

for Loop in I equals 0 and I is less

than message and then we have access to

dot length this gets the number of

characters in the current string so we

don't have to worry about the maximum

length in the square brackets we can

always rely on dot length because we

don't have to keep track of it and the

language can know that for us and give

us the

value in this case we're going from zero

to whatever the length of these

characters are and then in here we can

actually just conso right line and we

can do the same notation we did here

we're saying we want to print the

message but not only the message we want

to print the index and the index is

going to be I in this case because we

start at zero since it's zero based and

then we're going to the last character

so let's print this and see what

happens so now we get C Is Awesome from

here down to the bottom c is awesome

print on every single line and that's a

little bit annoying to read so what we

could do is just change this to a

console right and maybe we can just

comment this section out just so we can

see this

properly there we go so we get c is

awesome and because it's a console right

it's right in them all next to each

other and they're being printed out one

by one let's add a cool little effect I

can demonstrate you for now so inside

the system. threading Library we have

this sleep function so what we can do is

we can add another using command system.

threading we already have threading do

tasks in there but the function that we

need is inside the threading Library so

there's two ways we can write this we

can actually write everything by hand

like this dot sleep or you could just

remove the system. threading because

we've added it at the Top If you don't

want to add it at the top that's not

really a problem you can just include it

straight into here adding it at the top

will include it within the entire

project but if you're only going to use

it once or twice then sometimes it's

better to just include it straight into

here because it'll make your overall

program size a lot smaller so let's just

keep keep it in so we can make our Corde

a bit more readable so we have thread

dos sleep and what this will do is it

will suspend the current thread for a

specified number of milliseconds there's

a th000 milliseconds in 1 second so what

we can do in here and we're trying to

mimic a typewriter effect so we can say

something like 200 which will be roughly

a quarter of a second so if we've run

the code now let's see what happens so

we get this cool effect where after it

prints out each of the characters it

will print out the next character only

after it's being asleep for a quarter of

a second and that looks kind of cool if

you wanted to speed it up you could just

decrease this number there you

go of course if you make it too low then

it might be too fast for you to actually

acknowledge what's happening and if you

also make it too slow then it's just

going to take too long to turn off and

at that point you can use contrl C on

your keyboard or use the stop button at

the top to terminate the program okay so

let's have a look at another thing that

we can do with this so maybe we can make

this 100 for now in the previous videos

we've used use a function called

contains and let's say we're just

checking if it contains C and we know it

does because we can see it visually

let's run the

code and we see we have true being

printed and the reason why true is

actually on the same line as c is

awesome is because we're using console.

right here so it's actually not made a

new line character just after the E so

when it's printing true it prints it

straight after that so what we can do to

fix that is just place a new line

character in here using console.

readline and we can just drop this a

little bit lower to 50 there we go so we

have true and what we can do with this

is we can actually make our own little

version of the contains function let's

demonstrate so if we make this exact for

loop again in I equals z i is less than

the message. length and i++ so first

let's kind of understand what's going on

with this function returns a value

indicated whether a specified subring

occurs within this string and what that

means is it will return a Boolean for us

if C is inside our message then this

will return back true and we know this

is true because we just outputed it to

the screen and we can see this visually

so what we need to do is we need to Loop

through our message and try and figure

out if we can find C inside our message

and then we can give back a value is

true so maybe we can do a bu contains

equal false and the reason why we

initial it to false and the reason why

we initialize it to false is because we

want to set it up telling it that we

haven't found it and then inside the for

Loop if we find it then we can set it to

True let's have a look so after the for

Loop we can print contains of the screen

and then inside the for Loop what we can

do as we've seen here it returns back

every single character so you'll get

back C sharp then the space then I then

s and so on and so forth and what we

want to do is we want to go through each

of the characters and then just ask if

the variable is equal to C if C is equal

to C then we can assign this to true and

then go through #is equal to C if space

is equal to C and so on and so forth so

we can write a if statement here that

says message square bracket I is equal

to equal to apostrophe C and the reason

why I said apostrophe is because this is

a character and this also returns us

back with a character so we need to

compare them both like that if we use a

string in here then it might complain

that we're trying to convert between a

Char and a string and it won't let you

do the equals equals operator between

two different variables maybe what we

can do instead is to follow the approach

that we used before and use the equals

function and place our C inside here so

if the character that we're currently up

to inside the for Loop is equal to our C

which is what we want to search for then

we can set our contains variable equal

to true we don't need to write an L

statement in here to set our contains to

false because we've already gave a value

at the start if we didn't have this

value then it'll be beneficial to do

this but since we have it then we can

remove this other path and we don't

exactly need it so let's have a look and

see if this

works perfect it says true maybe we can

change this character to something like

a make sure this

works which it does as well so perfect

so now we've made our own mini little

contains function inside here and we can

also use the C library one but the

purpose of this exercise was just to

show you how we can iterate through a

string variable and then decide what

each of the characters can be equal to

and then we can decide if they're equal

to another one and then let the user

know that we found a match for their

search

term so far in the course we've covered

the equals function and the contains

function when you're searching through

strings or trying to find values let's

have a look at a method that makes sure

a string is valid before we can process

information on it so in this quick

example we have a name output we're

reading the value into a string name and

then we simply output it to the screen

if we run the code and just press enter

then it says your name is blank and we

don't really want that because we want

to check if your name is equal to

something before we continue so let's

look at some methods so we have if the

name is not equal to nothing and then

let's just print zero to the screen and

that's one of the methods and we can say

if name is not equals to quotation marks

then let's print out number one so what

this is doing is checking if the name is

not equal to empty quotes and then this

is doing exactly the same thing except

we're using the equals method and let's

just run the code and press enter and

you can see the zero or the one doesn't

appear because it's detecting that it

contains nothing so if we type in a real

value we can see Zero and one coming up

so these are both valid ways of checking

if the string is not empty so let's look

at a third one and this one is actually

very important for reasons I'll explain

in a second so inside our string Library

we have is null or empty so we place our

name inside here and let's see what this

says now indicates whether the specified

string is null or at an empty string so

this can be very effective so what this

reads now is if the string is null or

empty then we print a value but we want

it to be not no lower empty because it

wants to have some contents so if we

type a value in again you'll see now we

get all three numbers and what them

three numbers mean is that it's past all

of these checks and each of the checks

just ensures that the value has got

something inside and that's fine but it

can be slightly problematic for this

reason using this method and this method

are fine in some cases except as it says

here it checks if the string is null and

null means it doesn't have a value but

it just has nothing instead of something

let me demonstrate if we assign the

value of empty quotation marks this

actually has a value but the contents is

empty but it still a string as it comes

up in here if we assign this the value

of null then it's just a key word that

we can use to denote that this literally

has no contents it's not that it has an

empty string because an empty string is

still actually a string whereas a null

it just means the variable is blank now

this is where the issue arises if we try

and call a method on something that's

null Watch What

Happens we actually get an exception and

our program just crushes you can't run

any any code on something if that

variable is actually defined as

null it doesn't mind if you check the

value of it but if you try and run a

function then you've got a problem so if

we comment this line out this ensures

everything it ensures it's null or it's

empty and this is the belt and braces

option because if it's null we don't

actually get an exception when we try

and run this code as

follows we get the zero showing up

because our variable has something

that's not speech marks and it's

actually null so we want to avoid this

option as well and also avoid this

option in case our string is actually

genuinely

null using is null or empty we check

both for the value of null which is

right now and if it's got empty quotes

EG the value is empty so if we just run

this again and have these two paths

commented out we can see that two is not

appearing if we change this back to our

console read line and we try and do it

then we get our number two because it's

validated that this string variable has

got something inside it and the variable

is not actually no this is a very good

way of being able to make sure that your

program doesn't get exceptions as with

calling these equals if you want to call

equals then call it inside here you can

check if that variable equals something

by simply placing that if statement in

inside here after you validated if the

name variable able actually has some

contents if it's null or it's empty

running equals on it will just break the

code further and you don't need to check

if it's equal to Something in here maybe

you were checking if the value is equal

to abber or something along them lines

and now if we run it again we actually

get the output

correct it's best to Nest them like this

because if you run this function on

something that's null it's actually

really safe but as you saw previously if

you run a function on something that's

null then you're going to get a null

reference exception and your program

will close down and break you want to

avoid exceptions at all cost so nesting

it inside a is null or Remy check will

be perfect for your code it improves the

stab ability of your program to ensure

that exceptions don't

happen so in this exercise I want you to

ask the user to input any message and

then I want you to print the message in

Order character by character and then

also print it in reverse character by

character let's get started so we could

just have a console right line but let's

change it to a right and just say enter

your message call on and then speech

marks and then just have a string

message

equals console. readline perfect so in

order to be able to Loop through the

string message we need to have a follow

Loop if you remember from our previous

videos we can have this string variable

followed by a square bracket and then we

can have an index inside and that will

print us back any sort of index based on

that position so if we just have this is

a test then it's prob print as the T

because that's the first index and so on

and so forth so what we can do with this

information is take what we can do with

the string index and then also use the

property which is do length and then use

these two values to be able to process

this is a test so we have the first

character Square braet 0 and the last

character is at length 14 so our message

variable now contains 14 characters

obviously When I close the program then

that value is lost and I have to reenter

it but because of that exact reason we

would always use do length because we

never know how long the users input is

going to be so we can have a for Loop so

we can say 4 into I equal 0 I is less

than and we can use the message. length

here and then we can say I

++ so we want to start at zero because

our first index position is indeed zero

and we want to go from the index zero

all the way to the length

now in that example I just printed out

14 for the length but you got to realize

is there might be 14 positions but

there's only 13 indexes so it's not

actually 13 index it's just up to 13

when you start at zero going from 0 to

13 gives you 14 positions so when we're

saying 0 is less than the message length

then that'll actually take us from 0 to

13 instead of 0 to 14 if we made this

greater than or equal to and change this

to to one for example then that's going

to go from 1 to 14 and first we're going

to miss out the first character and

we're actually going to go one over the

message length so you always want to

start from zero if we're going through

the entire string so this will go from 0

to 13 and then here we can just have a

console right line and just print

message Square braet I and then just for

the time being we can comment out these

lines just so it doesn't saturate the

output so let's run this code and see

what

happens this is a test and then we get

them all on separate lines maybe we can

just use a console. right instead so we

get each character on the same line

instead of a new line character being

outputed at the end of each of the lines

so we can say this as a test again and

there we go we get this as a test

outputed as well so now that we've

printed that in order let's see how we

can print it in reverse so just a quick

little tip throughout this course I've

been quite hard on you I've made you Mar

the for Loop out exactly the way it

should be but all done manually this is

so you can get used to the syntax and

after a while once you understand

exactly what to do then what you can do

is when you type in the fall you can see

that it says as a force snippet tab

twice to insert the force snippet and we

can do that now and it actually gives

you a little bit of an output you can

see these sections that have been

highlighted the reason they're

highlighted is once you're done with one

section you can actually press Tab and

it'll jump to the next one so now my

cursor is jumped on the length and we

can type in message. length if I press

tab again then it'll just keep cycling

through them until we're done and we can

press Escape upon pressing escape then

it gets out of the little message and

there we go we've now just defined a

really quick for Loop please only use

this once you're comfortable writing for

loops and don't use them immediately

because you might get stuck on what to

do and how to do it so it's best to

always write the for Loop syntax out

from scratch so you can get used to the

three different sections so this is

actually a full loop that goes forwards

it's going from 0 to 13 but what we

actually want to do now is go from 13 to

zero so let's actually change this for

Loop completely and we can do our little

shocket again so if we do four and then

we don't actually want to start on the

value of zero we want to start on

message. length and then you want a

minus one and just as we said here we

never actually want to get to the length

because we want to go from 0 to 13 so if

we want to go backwards we want to go

from 13 to zero and the only way that we

can get this 13 is by getting the 14

value which is message. length and

simply minusing one so now that we've

got our first variable which is in I

equals message length minus one which

will give us the 13 we want to go from I

and instead of less than it's going to

be greater than or equal to and then we

actually want to change it to a zero

because we actually want to stop at zero

in this case and then we don't want to

use I ++ because if we start at 13 we

want to go down in one instead of up in

one so we can simply just just do minus

minus we can take this exact line and

print out to the

screen and if we just print something

small in between like a hyphen just to

separate the two outputs and let's see

what

happens test there we go so we have test

and then t s e and t is now in Reverse

but now you might be wondering if we had

a shortcut for the four what's this four

with an additional R cords n it for a

reverse for Loop so if we tap tab twice

you can see that we have exactly the

same format inti equals something minus

one which we have right here and then we

have I is greater than or equal to zero

as is we have here and then we have

iusus just like here so we can literally

just change this into message do length

and then these two now are exactly the

same so just copying this down we'll get

us exactly what we need but obviously we

just wrote this I just wanted to show

you the shortcut so let's try it with a

longer string this is a test there you

go and if you wanted to you could just

print these on separate lines so you can

just change this into a right line for

example and have it like that it doesn't

really matter the rest is up to you but

the task is just so you can print it one

way and then print it the other way I

realized that what you can also do is

just simply print a console right line

and have message in there and just get

rid of this completely and the output

will still look exactly the same there

you go and and the reason why I told you

to write the for Loop is not because

it'll be easier if you do this it's more

for your Educational Learning purposes

having more practice to how to iterate a

string variable is very helpful for the

future and you can understand it fully

if you just print out to the screen then

you're not learning anything extra

you're just applying what you already

know so let's just have a quick recap we

ask the user to enter the message we STW

it into a string variable and then I

demonstrated that you could use the

square brackets to Output the first

index then you can output the length of

the entire variable using a follow Lo we

can go from zero to message length which

will give us from indexes 0 up until 13

in this case and then we can go through

i++ increment in one and then we can

have a console right which will just

print out message index I for the index

that we're currently up to and then we

break it off with a console right line

just in the middle because these are

printing on the same line so we need to

insert a new line character at the end

after we've done that what we can do now

is print it in reverse and now we can do

exactly the same thing except from 0 to

13 we go from 13 to 0 so we start at the

message length minus one because the

length was 14 and now we minus one to

get to 13 and we're going from I is

greater than or equal to zero because

that's the value that we want to stop at

and then we can do IUS minus if we were

to take out this equals operator let's

see what happens test and now we're

actually missing the last character

because what we've done is we've

actually went from 13 to 1 instead of 13

to zero so let's put that back in and

make sure it works and then we have t

set for test and then we're outputting

the exact same thing in both for Loops

because we're simply just printing the

character that we're currently up to and

because this for Loop starts at zero and

this for Loop starts at the length minus

one then they output in the same values

or what it appears to be the same values

it's just starting at different

indexes in this exercise we're going to

be building some sort of a password

validor these can be present in

something like a register form where you

ask the user to enter their password and

then enter it again to make sure that

they've entered it correctly you want to

ask user to enter their password in

twice andall them both check if they

both contain something if they do we can

print out password match if they don't

passwords do not match and if they both

contain nothing then you want to Output

please enter a password so let's get

started first we can have a console

console right we can have enter password

string password equals console.

readline and then we can just copy these

two lines down using contrl D and then

we can have a password C for password

confirmation and we can say enter

password again and let just print these

out to the screen just to make sure we

know what's going on just like so so we

can have abber and then abber again so

now we have both abber started and

that's

fine so let's see what we can do now we

need to check if they both contain

something so what are the different ways

that we can do this so we can have if

password do equals and then we can make

it equals to string. empty and because

we want to check if it's not empty then

we can simply place the not operator

right at the start and then we can join

them with another one not password.

equals and then exactly the same thing

string. empty but in this case we don't

want to use password we want to use

password see there we go so if we've

came into here then that means they must

have entered something so let's just

quickly do our exit path which is if

they're empty please enter a password so

we can say else and we don't need a

condition here because if this were to

fail then it has to mean that their

password's empty so we can have a

console. right line and we can just do

please enter a

password and let's just test if this

path works first and we just press enter

twice and we get please enter a password

and maybe if we type something in for

both and then we get nothing because

it's actually came here now perfect so

if we come through and we can say

password equals equals password C or we

could use the equals function and we can

say password. equals and then password C

and if they both equal to each other

then we can print out passwords

match there we go and if that were to

fail then we can print out passwords do

not

match here we go let's run that let's

find out Aba and abber and we get

passwords match and if we try abber and

abber with two A's and then we get

passwords do not match so what are some

other ways that we can do this you can

structure this completely differently

right now we're doing both checks in one

single if statement and we can actually

split this up into two and then provide

a different error message so right now

this is please enter a password you

might have entered the second one but

not have ented the first one or vice

versa but it doesn't matter because you

have to enter both of them in order to

pass this validation check so what you

could do instead is actually put another

if statement inside and paste this one

inside here and then just keep these as

two separate if statements and then you

can actually provide different error

messages so let's Nest this in one

deeper so we can say if the password is

not equal to empty and if this returns

back true then this variable has

something inside it and then once we go

inside these curly braces we get another

check and if the password C is not equal

to nothing then that means if we've came

in here then we pass both of these

validation checks and both of them

contain something and then we can

proceed to check the variables so what

you can do in the first L statement is

you can say please enter a password but

on the second one so just after this

curly brace we can have another L

statement or we can say please enter a

password

confirmation so I know that was a bit

quick so let's just have a quick recap

so we can have have one if statement

that processes the first password that

you type in and if this is empty then it

says please enter a password and if it's

not empty then it'll go inside this

curly brace hit the first line and check

if the password confirmation is not

empty if it is empty then it'll say

please enter a password confirmation if

both of these return back true then it

will go inside the success path in which

it will actually check if password is

equal to the password confirmation and

we can say the passwords match or if

they don't equal to each other we can

say the passwords do not match let's run

that and see what we get so if we type

something in for the second one but not

the first one so for the first one we

just press enter and then let's type

something in for the second one and it

says please enter a password and then if

we run that and do the complete opposite

type in p and then press enter for the

second one then it says please enter a

password confirmation so you can see

immediately what we can do is provide a

different error message for different

situations if you include these both in

one

then you can only give out more of a

generic error message this is similar

for when you go to login and when you

get your password or email wrong it

doesn't say which one you've got wrong

it just says them generically which one

you've got wrong it says invalid

credentials or invalid username or email

it'll say them generically like invalid

username or password but it won't tell

you which one it is because hackers can

exploit this and you don't want to be

doing this but in the case of a password

confirmation and this is just for a

register form then it's not exactly the

the worst thing to do this because

sometimes what you'll see is the text

boxes might go red to indicate that

they're both not equal to each other and

this is fine on registration but it's

not fine on the login what we can also

do in here once we've checked if they're

both not equal to something we can

actually just validate how long the

string needs to be we can have if

password. length and let's say you had a

restriction and it can only be over six

characters and we can say six and

password

c.length is greater than equal to six

and once it passes all three of these

checks then we can check if they're

equal to each other if they got

something inside and if they've got

something inside and if they've got both

the length is greater than or equal to

six then we can check it so we can have

something like password and then

password and then this returns back with

password matched if we have something

like abber then it's actually returns

back nothing because we don't have an

else for this specific bracket so we can

say else conso right line please enter

six or more

characters so if we type in Alber twice

then you can see we get this message now

please enter six or more characters so

right now we have a validation for the

first password and the second password

and for the length and if all these

checks go by then you can process the

actual passwords that they've entered

and check if they're equal to each other

you can technically do all of these

checks in one line it'll just be a

pretty long line but what that means is

you can't have three separate error

messages like this if you have it

process in one line the only error

message that you can give is very

generic there's no one generic message

that can fit for password empty and the

confirmation password empty and for the

length so maybe what you could do is

chain the both of them to be check if

they're not empty so then you can

process a message with something like

please enter a password and then you can

have another check for the password

length just to let them know that they

need to enter six or more characters

splitting these off into two separate

checks is going to be very helpful

because it'll let the user know

something a bit better than a generic

error message so in the previous

examples throughout the course we've

actually talked about using the to low

function and I said in majority of cases

where you want actually compare two

different things you want to actually

use the two lower function or the two

upper function and you can achieve this

by having two lower just here and then

also two lower just here

now the problem with comparing if

they're both equal to each other but

checking the lower gue values is this is

indeed a password if you're checking if

the user has inputed any capitals

lowercase symbols Etc then there's no

point implying a too low function

because that will just ruin their actual

password and reduce the security so in

certain cases you don't always want to

use two lower and two upper for

comparison you want to just check the

value straight up and the same thing is

when you're doing a login you don't want

to be checking the values lowcase

because that's their login these are

some of the specific situations where

you don't want to be using the two low

function if the data that your

processing is pretty sensitive and will

be messed up if you use the to function

that you should always avoid it

passwords fit this perfectly and

usernames as well if someone's added

some capitals inside the username then

you don't want to ruin that by doing

lower case or upper

case if your username is spelled like

this and you try and log on like this

then they're not equal to each other

because this will probably be two

different accounts you can have an

account with a capital B and have

account with a lowercase b so you don't

want to be lowercasing the input and

then doing a comparison because what you

might actually retrieve is a different

account than you're looking

for so now that we talked about stuff

theoretically let's see how we can do

things practically so as you heard in

the last video arrays are a way that we

can store multiple variables of the same

data type in one little group so if you

had something like num one equals 5 in

number 2 = 10 in number 3 = 15 and we

had a similar case in one of the

exercises like this where we had to work

out if a triangle was actually a

triangle so we asked the user to input

three different angles and then we

stalled them all and then we added them

up after and it looked something like

this in total equals num 1 plus num 2

and then finally plus num3 and then we

had some sort of if statement after to

check if the

total was equal to

180 and then we did something in the

curly braces just to Output that it's

valid and then else output it's

invalid you can see how this gets a

little tedious we have to create a new

variable every single time we want some

more data to be stored and in this case

we just simply want another number that

we can store but we have to just keep a

pending one to it in that case we could

have angle one angle two and angle three

but how about if you had something that

just called angles for now let's just

talk about how arrays work how we can

use them and then after I've went

through this example about arrays and

taught you what to do with it then I'll

will go get the code from the exercise

from the triangle video and actually

transform it into using an array so you

can see what the difference is and how

the new code will look like so let's get

rid of this little section so now that

we've got three integer variables let's

see how we can do this another way so

the Syntax for arrays you first type in

the data type that you want just like

before and then you add on a square

bracket and a closed square bracket

there's not nothing that needs to be

inside as of right now then you press

space and type in the variable name as

per always and then we can make that

equal to new in three so what this side

of the equal sign means is we want a new

instance of an integer array and we want

to have three positions since we have

three variables here that we want to

store we need to have three positions to

store them so this line in total says

that we want to integer array we want to

call it numbers and we want to

instantiate our new integer array with

three positions instancia is just the

term that we use when you turn something

into a new class at this point if we

don't use new then we're going to get

compiler error because it's unsure what

this is trying to tell you it's trying

to start an integer inside integer array

and that makes no sense so it won't

recognize the square brackets in this

case because it has no idea what it's

trying to do and this syntax is

completely incorrect so the compiler

doesn't know what error message to give

you so we can have a new int and what we

can do now is by us using the square

bracket reference we can actually

specify these three variables inside our

numbers array so now we have access to

numbers 0 = 5 and then if we use

controll D to copy this down twice we

change this to one and two and then

print 10 here and 15 here so now this is

exactly the same as this we have three

integer variables defined separately as

5 10 15 and now that we've created an

array of numbers we can specify all

three these values inside our numbers

array so instead of having three

individual variables now we just have

one that holds everything like a group

and just to show you how this works if

we have a console right line we can say

num one space num two space num three

and if we use controll D to duplicate

that down then what we can have instead

of these num ones is simply just place

these values

inside changes to one and changes to two

and what you should notice is these two

lines are going to print the the exact

same

values there we go so in this example

we've took our sloppy code that will

roll us around appending one to this

number creating a new array and then

printing it out so let's say we want to

actually use these to read in values

from the console what can we do and

you'd use these exactly the same as how

You' use variables up here this is just

an integer variable at a specific

position held inside a group so we could

simply have a console right line delete

the right and just say enter a

number and then instead of starring five

we can say convert to in32 and then we

can have a console read

line and that will read in from the

console convert it to an integer 32 and

then store it into the first position of

our numbers array and now if we just

copy this down

twice and we can get rid of these two

lines cuz this is just staring it

statically and add one and two to here

so now what we're saying is enter a

number star into to 0 1 and 2 and let's

see what happens now so now we get asked

the numbers 10 20 and 30 and you can see

that they've been stored here of course

these haven't changed cuz this is still

referencing our static variables up here

so what you can see in this example is

now that we've made an array we can

specify each of the values very easily

inside here and if so we wanted to have

a number four instead of having to

duplicate it and rename it and change

the value for example we can just simply

turn this into four duplicate this

little section and then just change this

to a three one thing to note in C and

many other languages almost every single

index will start at zero in the last

section we covered the string functions

and they also started at zero when

you're dealing with strings and their

positions when you have a for loop with

the string then you always want to use

index Z as the first position the same

is true here we have four positions

which means it goes from zero all the

way till three it never goes from 1 to 4

or from 0 to 4 these are bad values it

always goes from 0 to

three you might be thinking this looks

really good but when we go to print it

we're going to have to copy and paste

this little section add another one and

rename this to three this gets a little

bit tedious if you just trying to print

them all to the screen so let's see how

we can fix that we can use a four Loop

for this so we can have four inti equals

z

I is less than and then we can have

numbers length and if you notice from

iterating through a string variable the

syntax is exactly the same we have a do

length property on the arrays as well as

we do with a string

variable so in order to print out this

line what we can do is print one of them

every time we go around the for Loop

instead of using a console right line we

can just use a console

right so we can have console.

right and then what we can simply do in

here is we can say numbers square

bracket I and then we can have a space

right after it just to M make what

happens here we have the number printed

out and then a space after it number

printed out a space after it and so on

and so forth so let's see what happens

now if we run the code 10 20 30 40 there

you go so you can see that these two

lines are exactly the same and now what

happens is let's just comment these two

lines out and if we were to change this

to five and we can duplicate the

section change this to a four and then

now we have five variables started and

we don't need to touch this for Loop

because the numbers do length will get

updated anyway so we can have 1 2 3 4 5

and you can see all five variables have

been printed automatically what also is

quite tedious here is we're having to

add another one to this just so we can

ask the user how many times that they

want to enter a number again this is

quite tedious but we also have a fix for

that

because we know how many times we want

to ask the user to enter a number then

we can use a for Loop to iterate through

and ask the user as you can see every

single line here is exactly the same

except for the index position that we're

dealing with and the same is true for

this for Loop this line will always be

the same as we're printing out here and

the only thing that changes between this

section and this section is the fact

that this says one and this says zero so

we can translate this into a f Loop to

make it

Dynamic and we can do exactly the same

thing above here so let's actually copy

and paste this for

Loop because we want to do exactly the

same

approach we want to go through all the

positions inside our numbers array and

then we can actually ask the user to

enter a number in so let's block comment

out this massive section and then we can

take one of them and place it inside the

for Loop so now we're asking the user

enter a number and then we're storing

inside index IND Z and of course if we

keep using index zero then it's just

going to override the first one and

we're not going to store any data so we

can change that to an i let's see what

happens when we run it now 1 2 3 4 5 and

then we get the five numbers being

printed this is awesome because what

we've got now is five output commands

coming straight to the console just from

a for Loop the for Loop is responsible

for printing them all out to the screen

reading in the value convert it to an

integer and then storing it inside our

array and then we have a separate for

Loop to print out to the screen what's

really really powerful about these two

for Loops that we've built is if we want

to have 10 numbers now we simply just

change this to 10 and then just rerun

the code as you can see we now have 10

numbers there you go so what we've just

done if we move this code down a lot now

just so we can have it close

by we've made our entire program that

was actually pretty lengthy into just a

couple of lines and two for Loops one

for Loop is responsible for looping x

amount of times x depends on whatever

that number is and then we go through

and ask the user to enter a number read

in from the console convert it to an

integer 32 and start it within the array

after this for Loop has finished running

then we have all the numers stored

inside our aray and then we can go

through and print them all out to the

console another way we can print this

out to the screen is by actually using a

for each so we can say int num and then

we want to be a number inside our number

so for each of the integers inside the

number we want to create a new local

variable called num and then we can use

it in here and if we take this line and

copy it down everything's exactly the

same except we don't need to specify the

main array numbers now and we don't need

to specify the square brackets we can

just change this completely and replace

it with n and if we just make this

number smaller so we don't have to

Output a lot of

values 1 2 and three and you can see

that they're exactly the same there's no

line between them CU I don't have a

console right line so let's quickly add

that

in and there we go 1 2 3 and you can see

they're outputting the exact same

values the reason why I needed a console

right line is because these fall Loops

are using console. right the problem

with console. right is even after

finishing writing all of the lines it

doesn't continue to the next line it

stays there so if I don't break up the

two outputs with the console right line

then all six outputs from the console

right and the other console right will

all go on one line this is just for

demonstration purpose anyway either one

of these Solutions are fine more people

tend to use this one the problem with

these two options is you can't always

use them

interchangeably this one will always go

through every single position it will

always go from zero and it will always

go to the length the problem with this

fall Loop is you can't choose how many

times you want to iterate through before

you stop there might be a very specific

case where you have a big array of data

but you only want to select a specific

range if you example where you had a

th000 array positions so from 0 to 999

and let's say you wanted to request and

run a for loop from 500 to 600 so you

would literally type in 500 in here and

then 600 inside here and this would be

fine as long as your array has that many

positions but with the 4 each Loop if

you run a 4 each loop on a thousand

length array then it's going to go

through every sing single position in

some cases you might not want that and

you only want to go through specific

cases so if you know you're going to go

through every single case then you can

use the for each but if you know you're

going to go through a certain amount of

cases then it's always best to use the

for Loop okay so we covered quite a lot

in this video so let's just have a recap

so initially we started off saying we

have our gome one gome two and gome

three then we had our static variables

stored inside there and then we created

our integer array which is now down here

and just just to make it a little bit

easier to store multiple variables

because if you wanted five variables

you'd now have to duplicate that and

change that to number five and this will

get very tedious if we had something

like 10 like before this is very long

and you don't need be specifying number

10 in your code because this also hurts

the readability of it using arrays can

solve this problem quite a lot you can

easily specify array using this notation

int square brackets followed by the name

and you make that equal to a new int

three the three can be any number that

you want as long as it's a whole number

cuz this tells us how many positions

that we've got we then outputed lots of

values to the console and read them into

our Ray but we had the same problem as

we did above where we just had to keep

duplicating the sections so what we did

to prevent that is by writing a little

for Loop that went through every single

position in numbers array outp puted

something to the screen read in the

value converted it and stored it into

the array and then after all that was

done we can use a for Loop or for each

Loop and print that back to the screen

now that we finished the base content of

this video and because I said at the

start that I was going to take the

triangle exercise and convert that into

an array so let's do that now so now

I've copy and paste a solution from the

triangle exercise straight into this

project I've just block commented

everything above just so it doesn't

interfere with our code so let's just

run this code to familiarizes with

what's going on so we enter 60 60 and 60

then it says valid

and if you run it again and enter

something else that doesn't equal to 180

then we get invalid so let's see how we

can make this code a lot better by the

use of arrays so as we said before we

don't want to be using this angle one

angle two and angle three let's make an

INT square bracket angles equal new int

and then in this case we have three

angles and what you could do as well is

you could say const int angle count

equals

3 and then you can simp L use that

inside here so later on in the code if

we wanted to specify this to a different

value we can just change that later

on so now that we've got three angles we

can get rid of these three lines and

write ourselves a little for Loop so I

goes from zero all the way to the

angles. length and then every single

time we go around in the for Loop we

want to

say console. right enter angle one and

then we can say angles square bracket

and not zero in this case cuz we don't

be specifying index as it will go from

zero to one to two we want to be using I

I because we're inside a for Loop that's

already going through all the indexes

for us and make that equal to convert to

in32 and then a console.

readline now we've got all the values

stored inside here let's deal with this

problem so if we just comment out the

total for the time being let's run the

code

oh we have one more error down here

sorry let's run the code again and we

can say enter angle one and we can type

the values in but as you can see we've

got angle one for each of the times what

we can do in here is use our I index to

print out the value so we can turn this

into interpolation and then wrap the one

inside curly braces and we can simply

change as one to an i and let's see what

happens now so we have enter angle zero

and then one and then two and this is

fine but humans don't like reading zero

they like reading 1 2 and three so what

we can simply do is say I + 1 and

because we're using I and then adding

one to it it's not going to affect the

variable inside the for Loop so there's

no problems by doing this this is simply

just for the output purposes so now we

can have one two and three just shown up

like that perfect so what we can do now

that we have all the angles stored now

we need to replace this line by writing

a for Loop that can add up all the

values for us so we can have four each

in angle inside our angles and we can

just say int angle sum plus equals

angle the problem with this is we can't

specify a new variable inside here and

then use it outside an if statement so

what do we do in this case you want to

take angle sum and initialize it out

outside the for Loop and then give it a

value of zero and then inside the for

Loop we can remove the int and we can

say angle sum plus equals the whatever

angle that we're currently up to so if

we had three values that's going to say

0 + 60 and then take that value and add

another 60 and then take that value and

add another 60 so it'll keep a running

total for us and if we just print out

angles some to the screen let's see what

happens 10 20 30 and now we have 60 and

that's the correct calculation if you

add them three together so now that we

have a running total for angle sum then

we're pretty much done if we get rid of

this line of code which is our previous

total and we actually bring back our if

statement there you go there's no error

now and all this is doing is checking if

the angle sum is equal to 180 and then

print valid and invalid so we can have

60 60 60 and it prints out valid and

let's just comment out our

output and then we can have 66 6059 and

then it prints out in valid which is the

exact same behavior that we saw at the

start so let's remove the extra cord in

here and keep it like this there you go

so now we've got our solution just like

this and it's a lot better because if

triangles were a transform in the future

and we wanted more than three sides then

we can easily update this value because

now that we're using AR raise or we're

using a for Loop to enter out the values

what we can do we don't actually need to

store it what I mean by that is we can

actually just get rid rid of this array

completely let me

demonstrate we can take this angle sum

and place it underneath here we can keep

the count so we can use that in the for

Loop so we're going from 0o to three

because that's our angle count and then

we can get rid of all

this and let's see what we can do now so

we have an angle count equals 3 so we go

from 0 to three which you'll just do as

we said before from 0 to two because

it's less than the angle count and let's

just print this to the screen see what

happens so the output's still exactly

the same but what's happening is these

values are not being stored they're just

being shown to the screen and then not

done anything there is no equal sign

here to assign it into anything so what

we can do is we can say angle sum plus

equals the value straight away and now

if we try it 60 60 60 then we get valid

the main differences between these two

situations is in the first one we used

an array to store all the different

values and then we added the total but

if we're not going to use these values

after then what's the point in storing

them we can just use a for Loop to ask

the user how many times they want to

input the values and then just add them

every single time that they add the

calculation in and at the end we could

just simply use a conditional operator

to be able to print at the screen

whether it's valid or not but let me

just undo that solution so I can show

you the array solution and we can have a

quick recap that was just an extra

little tip just in case you wanted to

know know what different ways you could

use to make this work there we go so

let's just have a quick recap here so we

have a Conant which all we'll do is let

us specify three inside here and then we

can use it here this isn't entirely

necessary but it does clean up your code

a little bit because it gives this

number three a bit of a meaning it says

the amount of angles is our angle count

having a three left over like this is

what we call a magic number they're

quite bad because it hurts the

readability of your code people want to

know what the three actually means so if

you bring in an angle count then you can

actually read we want a set of angles

and this is the amount that we need and

that's denoted by angle count it adds

text to a variable it gives you a way of

making sure that the text equivalent of

a variable is present so someone can

just read it after we've created our

integer we can actually make a for Loop

that goes from zero to the length and

then we ask the user to enter the angle

and in the previous example we had enter

angle one two and three so now we can

mimic that by using I but because I

starts at zero we want to use plus one

every time we loop around the for Loop

we ask the user to enter the number read

it in through console read line convert

it using convert 2 in 32 and then St

into the array after this for Loop is

finished we have all three values stored

in the angles array and then we have an

In Sum to count up all the values that

we need and then we use the fall Loop to

go through every single integer value

inside and make a running to total and

store it inside angle sum once angle sum

has the total count then what we can do

is use console right line along with a

conditional operator to print whether

it's valid or invalid to the

screen in this video we're going to look

at how we can use the built-in array

functions to be able to sort an array so

if you have an INT array called numbers

and then using the previous notation

that we looked at we can actually

specify any numbers that we want using a

common delimited list so if we just have

nine numbers inside here then we've

actually defined an integer array and

then the compiler can count the amount

of items that are inside here and give

it the length but we don't have to worry

about that ourselves so if we use a for

each and we can just print

Inn inside numbers we can do a console

right line on the new local variable

called num if print it out we can see

all of the values and maybe we can

actually just use a console

right followed by a space just to give

them all on the same line there we go so

as you saw before if you had a string

called test and we wanted to do

something to it for example if we wanted

to use the replace function if we wanted

to remove all spaces with an empty

string then if we check the replace tool

tie we can see that it returns a new

string most of the string functions will

return you back the new string after

doing something with it but in this case

if we type in Array followed by do sort

and we can place our array straight in

the bracket

what you realize is if you hover over

sord it will sort the elements in the

entire array but it will indeed return

back a void and vo just means it returns

back nothing so in this case what it's

doing is you're actually affecting the

numbers array straight away and you

don't have to do numbers equals array do

sort numbers for example if we were

doing the test replace we would have to

do this otherwise this would just be

returning back a string but we wouldn't

be using it the same is not true for the

arrays when you run the array functions

they tend to return back void and you

don't have to do anything other than

give it the array that you wanted to

sort so now if you run this code you

should just see the same output since

they already sorted anyway so let's

change some of these numbers

up and just see what happens

now there we go 02 55 6 7 8 999 and

that's it sorted just like that there

isn't a whole lot to this sorting

function but what you should note is you

can use this new array syntax to Define

how many items that you want in there

instead of having to change the amount

of positions that you have using the

square brackets you can just denote any

number of items that you want inside the

compiler will count them for you and PR

the length straight into the bracket for

you so you don't have to worry about it

using the array. sort function all you

have to do is give it the parameter of

the array that you want to sort and it

will affect the array straight away and

you don't have to restore it and then

we're just using a simple 4 each Loop to

print them out to the

screen in this video I want to discuss

the array reverse function so if you had

an INT numbers equal new int and if you

just had lots of numbers in here

defined just up till five then we can

add a semicolon and again using this

array function we can say array. reverse

and pass our array

inside and then we can just use a for H

of our item inside our

numbers and simply just print them out

using a console. write and we can use

string interpolation and just put a

space at the end there we go and if you

run the code then you can see our

numbers have just been reversed there

isn't a lot to this video I just wanted

to show you about the different

functions that you have available for

arrays these can come in handy when you

have something like a list returned from

a database for example and it's ordered

in a certain way let's say you're

returning back a list of lots of people

who are ordered back alphabetically and

it's going from a to zed and let's say

you have some sort of user interface

allows you to flip the order instead of

having to request the database to get

the data back in the opposite order you

can use this array function to just

reverse the list the same thing goes if

you're trying to order by lowest price

or highest price you can use this to

show the different values in different

orders that's where this array function

comes in handy when you have different

situations like the names or the price

by ascending or descending where you can

just flip the data instead of having to

request it back from the database again

flipping it locally is a lot easier than

going back to the database is that uses

more data and more computational power

on the back end and then return it to

the front end it's always best to be

able to use these functions instead of

having to use external things to be able

to return back the same data using it

locally like this will give us a

performance Advantage because it's done

directly on the data instead of having

to request everything again if you

wanted to do this manually let's have a

look and see what your options are you

can have your integer array just defined

like this with a dynamic list and then

we can have a sorted array using the

same length as the current array what

you can do instead of using reverse we

can actually just comment these lines

out you can bring back a normal for Loop

but if we use two RS it gives us a

reverse for Loop so the length is going

to be our numbers do length and the rest

of the paramet is exactly the same we

want to go from the last minus one all

the way till zero and we want to

decrement in one and we can say our

sorted number I is equal to numbers all

right but all this will do right now is

just give us back the same value stored

so what we need to do in a case like

this is make another variable something

like X and we can store that into zero

and what we can say is X is assigned the

value of whatever this numbers is

because this is going backwards but we

want this one to go forwards cuz we want

it to be reversed and we can say x++ and

see what happens when we run it

now so nothing's going to come up

because we don't have any output so

let's use a for

each we can have VAR

number inside our sorted numbers and we

can just take this and place it in here

and just print out the item and we can

use

num and there you go it's been flipped

the exact same value is just the other

way so this is obviously a little bit

more complicated because you have to use

an additional variable use a backwards

for Loop and then incent your own

separate variable and as well as

completely make a whole new array of

course you should only do this if you

need it as two separate arrays if not

you can always just affect the one that

you're up to right

now because I said before that arrays

are actually static data and they're

going to be fixed size then there's no

way to remove or add an item but you can

clear what you have already so let's

have a look array. clear sets a range of

elements in an array to the default

value of each element type so the first

parameter takes our array so let's use

numbers and we want to start off the

index that we want to clear from and

let's say zero and because we want to

clear the entire array let's go all the

way to numbers. length and now let's

just produce a for each our item inside

numbers and then just have a console

right string interpolation which just

prints out the item followed by space

and now let's run the code and see what

happens so we have lots of zeros in the

console and this is to be expected

because as we've talked about before the

default value for an integer is indeed

zero so all this is doing when it says

is it's clearing it's just actually

assigning all of the positions the value

of zero and we can mimic this quite

easily through a for Loop so if we

comment this out and write ourselves as

a for Loop in I equals z and we want to

go to numbers. length and then simply

inside we just want to say numbers

square braet I equals to 0 what you can

also write is the word default this will

pick up the default value for the data

type that you specified you can type in

zero or you can type in default it's up

to you but I think in this case if

you're trying to reset it back to

default then you don't have to worry

about remembering it or any typos you

can just use the keyword default so if

we run this code now let's see what

happens so effectively we've mimicked

what this function is doing but let's

have a look and see what other

parameters we have access to so if we

comment this out and drop the value then

you can still see we only have one

Constructor normally you'd see the open

down arrows on the left hand side to say

one of two in this case we only have

access to one Constructor it will only

give us a range of elements we want to

remove but really we just want to clear

it does say here in the tool tape the

array whose elements need to be cleared

and because it is a clear and not a

remove you got to remember when using

this function it doesn't actually remove

any of the positions it just resets the

values inside the array so what we can

do in this case is we can say numbers

comma 5 comma 5 and what that will do is

the first param arer is where you want

to start so if you want to start at the

fifth position and then we want to count

five forward and because we have 10

numbers then this should return back

okay let's comment out this for Loop so

it doesn't take an effect and let's run

the code and see what happens now so now

we took the position five which is

actually the sixth number because it's

zero base and said we want to remove

five forward so it's s and then four

more forward and there we go we have

five numbers being removed so instead of

removing all of the different values we

can actually clear a certain amount that

we want and to mimic this in a for Loop

we can do the following we can start it

at five and we want to go to 10 and

let's run the code and you can see the

output is exactly the same when doing in

a for Loop you need to actually

calculate what this is going to be so we

need to take both the numbers the fifth

index and the five length which is 5 + 5

and then we get 10 and we don't make

this less than or equal to 10 because we

have 10 numbers so we actually want to

go from position five to position 9

which will equate to less than 10 we

actually want to go from position five

all the way to position 9 which actually

covers five numbers 5 6 7 8 9 even

though it feels like the difference

between the two numbers is only four

because 9 - 5 is 4 but we actually have

five numbers between that 1 2 3 4 5

there we go so when we print the values

from 5 to till less than 10 we're going

from 5 to 9 which is exactly what this

is doing this is the index that you

start at and this is how many numbers

you want to count forward so let's just

have a quick recap of this function we

defined our numbers array using array.

clear it doesn't actually clear the

values it just gives us access to

removing them back to their default

values it doesn't actually clear the

array it just resets the values back to

their default type we can have numbers

parameter which will equal to our array

and we want to say we want to go from

index zero all the way to numbers.

length which will be every single value

inside the array and all that will do is

go through the array and set them all to

their default value which is similar to

what this for Loop is doing and before

we had in I equals 0 and I is less than

numbers. length and that will go through

every single position you can

technically use zero instead of default

but that would mean you have to remember

the default value of every single data

type it's easier to just the default

keyword and it doesn't promote any

typers using the default keyword is by

far the easiest and the most safest

option if you're going to have this

situation so making every number's index

equal to default in this case because

we're using integers it's going to

assign the value of zero and then after

this follow loes run with I is zero and

I is less than numbers. length it's

going to make all of these positions

equal to zero which is in essence what

this function is here for because arrays

can't be added or changed in terms terms

of the values that they have after

they've been created you can only really

modify what's already there or you can

reset them all back to zero using this

notation from zero to numbers length you

can get rid of all the values and make

them all zero using 5 to five means it

will start index 5 which is the sixth

position and then go five numbers

forward so it will delete these and

reset them back to zero you can mimic

this in a follow Loop just like this

start in I at five and then go to less

than 10 and the reason why it's less

than 10 is 5 + 5 gives you the 10th

index but because it's I is less than 10

you actually want to go to the 9th index

because from 5 to 9 that's actually

covering five different numbers and then

we can go through and assign the default

value and then when we print it out

you'll see that all of these numbers

have turned into zeros just like

so searching through arrays are very

helpful when you try to find some data

inside your array and you want to return

back the position to maybe do something

else with it so let's have a look at

this example so we have a numbers array

and let's say we want to search number

199 and find the index of it so first

let's ask the user what the number they

want to search enter number to search

then we can have in search is equal to

convert to in32 and we're converting the

console.

readline and then let's have a look at

the function so inside the array Library

we have an index of surges for the

specified object and Returns the index

of the first occurrence in a

one-dimensional array this is very

important as it says the first

occurrence which means if there was

another 199 at the end it's only going

to return you back this one and then

stop immediately and not give you the

value of this one so let's have a look

in the parameters and see what it takes

it takes our array and then a value so

our array is called numbers and the

value you want to search by is our

variable called search if you hover over

the index function again it says that it

returns back at integer zerob based

index of the first occurrence of the

value in the entire array if it was

found otherwise the value is going to be

minus one so we can say inter position

is equal to this and because it says if

it's going to be found then it's going

to have the value of a zero index and if

it's not going to be found it's going to

have the value of minus1 so we can say

if the position is greater than minus1

because if it's greater than minus1 that

means the value is zero onwards we can

also write this by saying if the

position is greater than or equal to

zero it's just up to you they both mean

the same thing and then we can say in

here number

search has been found at position

position there we go so let's run the

code and let's do a perfect situation so

we can say 50 and you can see it says

number 50 has been found at position

three and that's the fourth index since

it's zero based and what you could do if

you wanted to is actually add one to

this just so it looks like a bit more of

a readable number because four is going

to be the fourth number 1 2 3 4 and

let's try find a number that hasn't came

up like 60 it's going to return us back

with nothing this is really bad because

it looks like the search has failed

which it has but you need to Output a

message to let the user know so have

cons right line and say number search

has not been

found let's type that again 60 number 60

has not been found so let's see what we

can do to be able to write this function

ourselves if we just block comment this

for the time being we can actually use

in

position equals to minus1 and the reason

why I make equal to minus1 because when

we read the tool tip with this index

function it said if it's not going to be

found it's set to minus one so why don't

we set it to minus one first and then we

can try and find it so inti equals z and

we want to go to numbers. length and

inside here we want to scan each of the

positions so we can say numbers square

braet I and if that is equal to our

search then we can set the position

equal to I if the number we're up to is

equal to what the user has typed in then

we want to set our position to the index

we're currently up to and we don't need

to have an else because we've already

set it to minus one and after the for

Loop we can actually take this if

statement and put it back down here

because the code's going to be the same

if the position is greater than minus1

which in this case is our default value

so in essence if this value has changed

then this should always pass EG we've

found the number so let's run the code

and let's type a number in 30 now it's

found at position five now if we try 60

again number 60 has not been found there

we go so this function is exactly doing

some something similar to this it's

going through every single position

finding the variable and if they match

assigning the index and then after just

returning back to position back to the

console so let's comment our example and

let's see what other variants of the

function we can use and let's comment

back the first

one so using this line we can duplicate

it and just comment it so we have some

other parameters inside here if we add a

comma we can actually give it a start

Index this is the starting index of the

search and let's say you only want to

search from this second number onwards

then you can just type in the number two

so now if you've run the code and type

in 90 it's going to say 90 is not found

cuz 90 is the first one and let's try a

value like 50 now it says the number 50

is being found at position 4 because now

it's included in the range from two

onwards because this is two onwards

it'll be position two until the end of

the array if you wanted a very specific

range then you can actually give it

another value so so we can say we want

to go from zero and then we only want to

go two forward so if you run that now

and type in 22 it say is not being found

cuz we' started here and we said 1 2 so

now it's only searching between 90 and

199 and if you wanted to include the

middle ones you could say you want to

start a position one and go two forward

so now you get these two numbers and so

on and so forth so now if we try number

90 you'll see it's not being found

because 90 is not one of these two the

reason why you'd use different ones like

this if I revert back the first

one this will go from two till the end

this will go specific range and this

will go through everything the reason

why these exist is if you have a very

very big array let's say it's a th000

indexes then you don't want to search

everything unless you really need to

let's say for example you had a nam's

array and it was sorted alphabetically

and the person's name that you wanted to

search began with an a there is no point

in running the search all the way from a

to zed if you know it begins with a and

let's say for example you found the

first index of the user that has a name

B so you have now the indexes from the

start of the array up until the last

array position that has the value of

someone's name beginning with a so now

you have the two indexes of every single

a name inside this array if this was a

this was B and this was C and this was D

and you had these two positions then you

should only be searching all of the a

names instead of searching the entire

array and the reason for that is you'll

be searching lots more data for no

apparent reason and it's going to take

an extra long time for searching

something that you're never going to

find if you're searching for a name that

begins with a there is no point in

searching for anything A B C D and so on

and so forth that's where you'd use the

very specific range you can say you want

to go from zero all the way up to the

first B name so then you're only

searching the a names the this is just

one example and there is a lot more

every situation is very unique so use

the parameters that you want and always

use the ones that can get you the best

performance the least amount of things

that you can search is the best way

forward if you don't know what you're

going to be searching then you should

always use the top one which will just

search everything if you know where the

value is going to be or roughly going to

be then you can use this one or this one

to give you a better range and it's a

bit more focused instead of searching

for everything so a quick recap of this

index function what it will do is it

will return you back to position if it's

found it the position will be zero based

just as arrays are if it hasn't found

the position then it's going to return

this variable with minus one so

therefore after you've run the code you

can check if the value is greater than

minus one which will be from zero

onwards if the value position is zero

onwards then you know that that number

has been found and you can use the

position variable to Output that to the

screen if the value position is minus1

it means the array index function has

returned back negative and the value

position is now minus one so you can

output to the user that it has not been

found just in my little explanation down

below you can see that we can mimic this

function by setting the value of

position to minus one using a fall Loop

that goes from zero to numbers length

and if you want to two as well you could

use these inside here if you wanted to

mimic this this goes from two till the

end so you can say I equals 2 and you

still want to go to the length this one

goes from one and then two ahead so you

can actually put I equal 1 and then

because it's two ahead from one instead

of numbers. length it will just be I is

less than 3 because 1 + 2 gives you the

three so in order to do this manually

you can Lube through the elements that

you want check if the number you're up

to based off the index position of I is

equal to the search variable that the

user typed in of here and if you find a

match then you can assign the position

to the value of I after the for Loop is

run you can check the same if statement

as we discussed above these are copy and

pasted down below just to give you an

example in this little

section all these arrays have static

data which means that when you

initialize them with a certain length

you can't increase that length or

decrease the length you can only modify

the data inside it and you can't delete

any rows if you create an array of size

is five and you only stall three people

in then now you have two positions that

are being unused and that's unnecessary

amount of data stored in your memory if

you're not going to be using it up in

certain situations like when you're

making a game for example Tic Tac Toe

then you know you're always going to

need a 3X3 grid so a 2d array will be

perfect in this situation cuz you know

the exact width and height of your array

so you can do this perfectly whereas

lists are quite Dynamic if if you don't

know how many people are going to be

within a system for example if you're

signing up some sort of register for a

class or for a school or something of

the sort and you don't know how many

people are going to be in that class

then you can use something called a list

and list a dynamic cuz what you do when

you initialize it you just say that you

simply want a list and you can add items

and you can remove items and you can

also modify them if you wanted to expand

the size of an array it's slightly

complicated but it's not impossible

so if we had int numbers equals new int

and let's make that length four and

let's say we had a numbers array that we

wanted to increase to length

five we could have new numbers change

that position five and then what we're

going to have to do is from position

zero all the way to three which is our

fourth length from the numbers array

we're going to have to copy that into 0

to three of the new numbers array so now

they have the extra data plus the last

one which will give us our four so

that'll be five length 0 to three gives

us our four numbers and then we have one

empty one so we've just made a new array

and then copied all the data from

numbers into new numbers now of course

this is not really practical that if you

want to reduce the size or increase the

size you're going to have to do this

each time of course you don't want to do

this you just want to use something

called a list so let's stick with an

example and see what we can do so you

make this length

three and let's just put three numbers

inside here if you remember from the

array video we can also declare it like

this we can say number 0al 1 and then 1

and then two and change these values

these two are doing exactly the same

thing I'm initializing it with 1 2 and

three and also just saying that all the

positions in here are equal to 1 2 and

three so we don't need these twice but I

was just showing you as a quick recap so

we're declaring array we're calling it

numbers and we're giving it a length of

three and initializing it to 1 2 and

three now let's see the syntax to define

a list

you start off with a word list and then

you have a left and a right arrow which

makes a little section inside the

section you actually want to State the

data type that you want so if we're

copying the numbers array we can have an

INT inside here and that's it for the

Declaration of the data type and then we

can say new numbers or List numbers

equals to new and then there you go it

gets Auto filled for you but let's just

type it in we have list open the arrow

in in the middle and then close it so

now it's the same same on both sides but

because this is a new we need to add a

Constructor so we can open the curly

braces and the first Constructor says

initialize a new instance of the list

class that is empty and has the default

initial capacity press the down arrow we

can give it a capacity of the number of

elements that the new list can initially

store so we can start it off with five

length or three length or whatever it

doesn't actually matter it just means

that we actually have three positions

available immediately if we place three

in the brackets

and the last one we can actually give it

entire collection so let's say if we

loaded a list of numbers from a database

or a list of something from database and

we can actually insert it straight into

this list by putting a list inside these

brackets of course right now we don't

have a list that we're going to assign

it with so let's just leave this empty

and close the brackets and add a

semicolon so we could say list numbers

do add and then we can add one and then

duplicate this down and add two and add

three so now these do have exactly the

same contents instead of doing this you

can have the same notation like this so

you can have the curly braces in

here and we can put in 1 two and three

now that's doing exactly the same thing

and you see I can just keep adding more

numbers in here and the list simply

doesn't care but as soon as I add one

more into here then I'm going to get a

syntax error because it complains that

the length is not equal to the numbers

that we

have because the list are really Dynamic

it doesn't matter how many you put in

here because it can just go on forever

of course until you run out of memory so

let's have a little working example just

to see how this

works so we can have a for Loop and we

can go from 0 to 10 or maybe 0 to three

just so we don't have to enter as many

values we can have a console right that

kind of just says enter a number and

then let's read that in and we can say

int n equals convert to in32 and we can

place a console do readline inside

here and then after we've got the

console read line we've got an inn

that's being converted from the console

input inside our integer num and from

there we can take our list numbers do

adds and we can add the num inside there

or if you want to reduce a variable you

can just take this line and place it

straight into here now if you get rid of

that when this follow Loop runs we'll

have three iterations let's run the code

and find out 1 2 three so now we've

entered three numbers and now they've

been stored in the list so let's create

a follow loop after and we can go from

zero all the way to list numbers do

length but of course when we're using

list we don't have a length anymore when

we're using arrays we have length

because arrays have a fixed length

whereas lists have a count of the items

inside it so instead of length we simply

use do count and then inside here we

could just do what we normally do and

say list numbers square braet I and

let's run that and we should see the

numbers printed back out in the exam

exact same order 1 2 and 3 and the same

principle goes if you user for each Loop

we can say V item inside list numbers

and that's all you need to do and you

can just print item to the screen now we

should see the same output twice let's

have a look at other values that we have

inside here so we type in list numbers

Dot and let's have a look at the

function inside so we can add which you

just used above we can convert it to an

array we can remove a specific position

and we can clear it completely they have

other ones in here like B researchers we

can check if it contains a certain

element this is going to be very handy

when we do our

remove we can convert them all to a

different type we have a copy to we

check if a value exists

inside we have a find finding all of

them the difference between these two is

the find will return back the first one

whereas find all will retrieve all the

elements that match it finding the index

of a specific match finding the last

index iterating through them all you can

insert a specific position whereas the

add will just insert at the end of the

list adds an object to the end of the

list inserts an element to the list at a

specified index and then you have remove

which gives you the first occurrence so

you can type the number straight into

there so you can ask the user what

number do you want to delete and if they

type zero it will delete the first zero

in the list and you can remove all so if

you want to remove every single zero

inside the list you can do that as well

or you can move at a specific position

this is the most used function because

what you can

do and what you can do is use the find

index search for an element that matches

a condition and then returns to zerob

based index of the first occurrent so if

you want to delete the number zero from

the list you could use find index and

that returns you back with an INT and

then you can use the remove at and then

pass that index straight into there you

can also sort it so it goes up in number

order we did this with the array. sort

function as well and if you wanted to

you can also reverse the entire list

this can be very helpful as we did with

the array functions if you just wanted

to reverse them all in case someone had

something like a filter when you're

doing from Price high to low and low to

high the difference between them two

lists is simply the list flipped so

let's run a remove just inside here and

let's see what happens so we can say

remove that and let's say position

zero so we type in one two and three

you'll see that one two three gets

printed out and then I'll hit this

remove at and it will delete the first

index and in this case the first index

is going to be our number one so now

this has became our first index and this

is our second index and it's got rid of

the number

one so let's move on to different data

structure called dictionaries

dictionaries are generic collections

that store key value pairs in no

particular order they take in a t key

and a t value and these just mean the

key and the value can be any data type

that you want dictionaries come under

the system. collections. generic

namespace and if you don't include this

line at the top then you'll have to type

in system. collections do generic space

dictionary every single time you want to

specify dictionary putting the name

space at the top will include it within

the project and you can use dictionaries

freely so one thing to know is the keys

must be unique and they cannot either be

null values can be null or duplicate cuz

they're the values depending on the key

as long as the keys aren't the same then

the values can be similar so let's have

a look and see what we can do we can

type in dictionary and you can see it

has the arrows either side and you can

see here it says T key and T value and

what T key and T value is is it gives us

the flexibility to add any data types

that we want we can add in two strings

in here for example or we can do

anything that we please so in this

example I just want to start a list of

names and I want to give them a unique

identifier and this unique identifier

will just give them a number that their

profile is up against it's the same way

when whenever you make an account with

any sort of website when your data gets

stored in the database you have a unique

ID for your account and this is the same

thing that I'm trying to replicate here

we have a unique ID and with that unique

ID comes a name so we can say equals new

dictionary just like we do with the list

we can end the brackets and end the

semicolon there and now we've created

our dictionary and the exact same

principle we can use with the list we

can simply type in the name of the

variable of the dictionary and type in

add and then we have access to int key

and string value and this parameter is

going to change if this now becomes a

string as I'll show you

now and if we reopen the bracket you'll

see this has turned into a string key so

that will change depending on the data

types that you specified so let's say we

want to add one and we want to add

ourselves then we can duplicate this

down and add in two and three and we can

say test and test

and now if you run the code you'll see

that we have no errors and nothing's

going to show up I'm just demonstrate

that it compiles okay and the same thing

with the list we can also simplifies the

initialization process and it looks

something like this so because this is

in key value pair we can no longer just

type it in one line they need to be in a

little block so when we wrap these

around the curly brace we can have the

key on the left and the value on the

right and one of these is called a key

value pair key P value pair and we will

use these in the for each Loop in a

second when we print out all the values

to the console so let me just

demonstrate something about what I said

at the start I said that all keys must

be unique and there's a reason why I

said that if we duplicate the first

entry therefore giv us the same key as

one and we try run it and let's see what

happens we get something called a

runtime error this runtime error happens

when we run the program and it's got

nothing to do with compile time or our

logic an item with a same key has

already been added so we need to watch

out if we do this because our program is

going to compile but it's not going to

work at runtime and let me demonstrate

another fact if we make this number four

and our values are exactly the same once

we run this it will still work fine

values can be the same but the keys

cannot be the same the keys have to be

unique so just like with our lists and

our arrays we can use a for each or a

for Loop to access this directly so we

can say 4 in I equal 0 and we're want to

go names do count and the same reason

it's a do count for a dictionary as it

is for a list but it's not for an array

dictionaries don't have fixed length and

neither do lists they are very Dynamic

and basically are a list list can only

store one different data type whereas

dictionaries give you a key and a value

so as we said above we have a key value

pair so let's try and print this and see

what happens have a console right line

and we can say names square bracket I

and that will give us back the position

the issue with this is it will just give

us back the string and let's just see

what happens if we print

this then we get an exception because no

key was given and the problem with this

situation is we can't use this like a

list getso sets the value associated

with a specified key what this means is

when we give our square bracket I inside

here this is actually not talking about

the index this is actually trying to do

a search on these keys so if you

actually Chang this to one then we

should get abber return back this is not

actually doing an index lookup it's

doing a key lookup and the reason for

that is what we're going to have to do

instead is say names. element art and

then what we can do inside there is

place our index so this element art will

return us back with a key value pair as

I said above here so how this variable

works is we can do key value pair and

then we need to specify this variable

the same way we've got it up here so we

have an INT inside a string and then we

can say a pair and make that equal to

the function called element at as that

returns us back with a key value pair in

string and of course if you wanted to

make this a lot smaller you could just

simply use V but let's keep it as key

value pair just so you can understand

what's going on and the reason why we

need to get the key value pair is we

want to retrieve the integer and we want

to retrieve the string and now we can

use pair. key and pair. value and

because these are both integer and this

one is a string it matches our pair

exactly so we can take these values and

go to our console right line and we can

say something like this use string

interpolation and say pair. key and then

maybe add a hyphen and then say pair.

value and then just remove these two

lines and let's try and run it now there

we go so we get all the values and it

doesn't matter what this key is it can

be something random it doesn't really

make a difference like I said this

doesn't have to be in a specific order

so let's keep it as 1 2 3 there we go so

you can see using a for each Loop to

print this is is a little bit more

complicated let's have a look and see

what it takes when you use a for each

Loop for each VAR item inside our

names and as you can see here our item

is automatically a key value pair of int

and string and again what you can do is

type this straight into here just so you

can get used to the notation and there

you go it's still a key value pair and

then now what we can do is we can take

this exact line and paste into here and

instead of pair we have item and this

should return us back the exact same

values and if we just separate with a

console right line there we go okay so

now that we've understood this let's

take a look at an example let's comment

this out and let's create another

dictionary so we can say dictionary and

then we can up a string of

string and we can call this teachers

equals new dictionary string of string

again and we can add our curly brace

just so we can add our value straight to

here so as you said before we need to

open curly brace and close Cur

and then we can say the math teacher is

called abber and then we can add another

one and we say the science teacher is

called test there we go so we've

initialized our teachers and we said

that the value that we want to give it

is a unique identifier which is going to

be our string assigned to the subject

and this is going to be the teacher that

is assigned to the subject so what we

can do inside here is we can have a

console right line and we can do

something like this so remember when I

was saying in the for Loop we can't

actually use the square bracket notation

like this because this is referring to a

key rather than a position so we had to

use element at just to find out the key

value pair and then to print out to the

console so because the square brackets

is an actual surge let's try and make

use of that now so we know that there's

a math class available so we can type

math in here and what that should return

us back is abber there we go so what

that's doing is it's searching through

our dictionary finding this element

inside here and then return us back with

the value for that key gets all sets the

value associated with the specified key

the problem with using this approach is

it says here if the specified key is not

found a operation throws a key not found

exception let's just say for instance I

mistyped the math and now we're just

going to get a runtime error and it's

going to be a horrible exception and our

code is obviously going to break this is

similar to what we do when we have in.

trass we don't just trass it immediately

and convert it without actually checking

if it's a valid value so instead of

doing this line what we can do is we can

check if that key exists first before we

then use it so we can say if

teachers do try and get the value which

is similar to what we have with triy

pass and then the first parameter is the

key that we want which is going to be

our math and then out here is going to

be the variable that we want to Output

out so we can say out

teacher and then open the curly braces

and then inside here we can print out

the teacher so we try to get the value

for Math and if we found that key inside

these then we return back the value

inside our teacher the same way works

for our trip pass you take in a string

in here and the output is going to be an

integer if it's successfully passed it

and if it hasn't we can say cons output

math teacher not found so let's try and

run that now you see we get return back

Abba and if you actually drop the lower

case then it says the math teacher is

not

found alternatively what we can do

inside here let's assume that this is

correct we can take this value and place

it straight inside the square brackets

and do this again this is kind of

redundant because we've told it to get

the value for key place inside teacher

and then we're just going back to the

dictionary to get the value twice so

make sure if you have an output variable

that you actually use it inside here you

can see that you're not using it when it

goes a slightly dark color like this so

just like in the last examples when we

were placing values inside our list we

can also do that in dictionaries

we can say teachers square bracket math

and then we can make that equal to

something so if the math teacher were to

change and now the teachers called Joe

then we can run this line and print out

to the screen let's just take that for

each Loop and tailor it to the new

dictionary and see what happens when we

run it so let's just change this into a

VAR to make it simple and then we can

have teachers inside here there we go so

now let's just run this and you see now

it says abber is the first value that

abber is coming from console right line

here that says teacher this is one line

before we change it and we change the

value of the math teacher to Joe on the

next line and there we go it's committed

the change and just like in the last

video as well when we try to delete

something from the list let's see what

we can do to delete it from here so we

can have teachers do remove and you can

see that it says key in there and again

if we add the key in like this then it's

going to work just fine there you go we

just have science we don't have math

anymore but if I drop the lowercase then

it's going to cause us back with an

error but in this case instead of it

throwing an exception like it did when

we were trying to do up here and up here

as well the problem with using the

remove is you just simply won't remove

it but what it will do is it'll give you

back a booing if you remember from the

last video we actually had a look at a

section where in the list it gave you

back a booing whether the remove

function worked or not alternatively

what you could do instead of using the

remove function is to check if the item

exists in the first place we can say

contains key and we can Place math

inside here and if it does contain it

then we can run the same line of code

and we can say remove and then use math

just to make sure that this has been

actually removed and then in the else we

can say math not found retrieving values

back should be retrieve using try get

just so you don't cause an exception if

this is not found we can simply do an

update by placing the value of the key

inside the square brackets and then

simply just assigning it to something

else this assignment will change the

value of the key which is inside the

teacher dictionary and if you wanted to

remove something it's best to use the

contains key just to check if it

contains it first and then you can do

the remove and then if it doesn't

contain it you can tell the user that

math has not been found and then finally

we can just print out like

this so in this exercise I want you to

create two lists with the integer data

type one for even and one for odd and

then we're going to Loop through from

zero all the way till 20 and if the

number is even add it to the even and if

the number is odd add it to the odd list

and at the end print out all the values

for even and for odd I know you can just

print these out inside the for Loop we

don't have to have dedicated lists but

this is just for list practice and I

want to talk about different ways that

we can do this solution so let's start

we can have a list of int and we can say

odd new list int and we don't know the

size so we're just going to leave the

gaps empty and if we duplicate this down

we can simply just change this to even

now the reason why we're doing lists

we're going from 0 to 20 of course this

20 value could be anything it could be a

million it could be 100 but we don't

know and the point is lists have counts

whereas arrays have lengths if we set

the array length to something like a

thousand we know probably be able to

contain all the numbers inside there but

that's going to be a lot of positions

that we don't exactly need because

arrays are not really dynamic because

they're static then it's best to use

list in this case because we don't know

how many numbers are going to be odd or

are going to be even between between

this range if we knew the exact number

of positions that we needed in a given

solution then array would be better

since you don't want it to be dynamic if

it doesn't need to be so let's start can

have a for Loop that goes from 0 to 20

if you want to include the 20 as well we

can just place an equals the best way to

determine if a number is odd or even is

to use the modulus operator so we can

say I percentage 2 equals equal Z and

what this will do is it'll tell us if I

is divisible by two if the value is

equal to zero if you have two modulus 2

then the remainder is going to be zero

if you have three modulus 2 it's going

to be 1 * by two which gets you to two

and then to go from two to three you

actually have one so if you insert an

odd number and do a modulus two you're

going to get a one returned if you do an

odd number and you do modulus 2 then

you're going to get one returned if you

do an even number modulus 2 then you're

going to get zero returned so what we

can do in here is we can say even add

and then we can simply add I and then in

the else we can also simply just do odd

do add and then have I because the

values of the remainder can only be zero

or one we don't need to have an lse if

in here doing this is a little bit

redundant although it's still

technically correct the values can only

ever be zero or one so if you run this

calculation and the value is zero if you

run this calculation and the value is

zero the only other value can possibly

be is one so therefore we don't need to

additional condition so therefore we

don't need this additional condition and

we can simply get rid of it if I modulus

2 is equal to Z then we add the number

to even and else we add it to odd

normally when you have situations like

this we can use the conditional operator

where we have the question mark followed

by the colon but in this case we're

actually doing two different assignments

we're either assigning into the even or

assigning into the odd so there's no

room for the conditional operator so at

this point we should have all the even

and odd values from zero all the way to

20 inside our two lists so then we can

now just print both the lists out so we

can say printing even numbers and then

we can have a for each Loop that goes v

item inside out even and then we can do

a console Right add some instring

interpolation and then we can say item

followed by space then we can print

environment. new line just to give it

some space and we can say printing odd

numbers here we go and then we can just

take this exact for Loop and copy it

down instead of set in even we want to

do in odd and this is exactly the same

since the variable name is exactly the

same let's try run the

code there we go it's got zero all the

way till 20 and you can see it goes up

in twos hence all even numbers and we

also get these going up in twos except

it starts from one and this covers all

of the odd numbers and if we change this

number up to something like 50 then the

list will just grow and they Cod should

still work okay you can see that all of

these numbers are still consistently

even so now that our task is actually

complete let's just have a little recap

so the reason why we're using lists and

we're not using arrays is for this exact

nature if we have 0 to 10 then we can

almost work out how many even numbers

and how many odd numbers we can have we

can manually count we have zero and then

two 4 6 8 and 10 and then for odd we

have 1 3 5 7 and 9 so we know how many

numbers exist within here but what if we

just change that number to 50 then now

we have a lot more odd and a lot more

even it's impractical to type these

numbers in and then to Simply count them

in your head and then make an array

length based on that position that's a

bad solution because not all cases

you're going to have the flexibility of

Simply typing in the numbers like this

and Counting them yourself these numbers

could be something that typed in by a

user or something loaded for something

external like a text file you need to

understand that these values can be

generated from anywhere and if you just

type them in then it provides an

unrealistic expectation of how to write

some programs so what you have to bear

in mind in situations like this is you

simply don't know how many even and how

many odd numbers will be appearing

during this range so you want to use a

list so it's as dynamic as possible we

have two lists in this case one to start

all the odd numbers and one to start all

the even numbers we don't initialize

them with any sort of capacity we just

keep it empty for the exact reason we

don't use an array we Loop through from

zero to anything we don't even have to

loop from zero we can also just start

this at 100 and go all the way till

th000 it really doesn't matter what our

number range is the only thing that

matters is that we having a list that is

going to be dynamic and then we can

calculate whether the number is odd or

even and add it to the right list how

this works is we use I which is the

number we're currently up to and we

calculate the modulus of it to check the

remainder if the remainder is zero then

it must be even as it's divisible by two

and if it's anything else than zero

which you can only ever be one in this

case when using modulus 2 then we add it

to the odd list after this volume is run

now our odd and even lists have all the

values that they should have and we

simply just write these two blocks are

exactly the same just print the even

numbers and print them out print the odd

numbers and simply print them out to the

screen and that's it and if you run the

code it might get a little bit crazy in

here but as you can see these will

actually work if you pick any random

numbers you can see that they're all

divisible by two and the same thing goes

for odd you can see that they're not

divisible at two cuz they're all odd

so in this exercise we're going to cover

a little task I like this call this task

an array of multiples so we want to

Define initialize two integers and num

into length so in the case of this

example we have a number seven and the

length of five so what this will do is

it will essentially give you seven times

table but only the first five numbers 74

21 28 and

35 we take the first number which is num

which will give us the multiples and we

take the second number which is the

length and that gives us the size of the

array so we create the integer array

with the size of length Loop through the

array and insert the loop count times

byome CU this is 1 * 7 2 * 7 and so on

so forth until we reach the length and

we place into the array and then finally

we paint the array just to give us these

values out to the console all right so

let's get started we can have an in norm

and make it equal to 7 and we have it in

length and make that one equal to five

now that we defined initialize the two

integers let's create the int array with

the size length in Array result equals

new int and we want size length after

that we want to create our for Loop now

this is the tricky part if we try go

from zero all the way to result. length

then let's see what happens so we want

to start result I you want to make that

equal to number which is our array of

multiples Times by the value that we're

currently up to now let's print our

array using for each Loop to see the

value so we can say V item inside our

result then we can have a console right

that just prints one of them at a time

so we can say item followed by spits now

let's see what happens when we run

it so we get 0 14 21 28 as you can see

these are the first four numbers so

we're close but we're nearly there

because our array starts at zero and it

goes to the length which in this case is

going to be 0 to less than five which

will cover the positions of 0 to

4 with these positions it's going to do

0 * 7 1 * 7 and then get to 4 * 7 and

not actually get to the fifth because

we're starting from zero so

alternatively what we can do or what you

think you can do is changes to a one and

changes to equal to so now what it's

doing is it's going from one all the way

to five which is still covering the same

amount of positions it's just one index

forward so now if we try to run the code

then we going a runtime exception and

the reason for that is it's trying to

access position number five if we hover

over I we have five and if you hover

over the result we actually have some of

the right values except it's trying to

get to the fifth position and it's not

working the reason why the zeroth index

is actually zero is simply because

that's the same value as the default

value because our for Loop started at in

I it's actually only starting from here

and going down so this calculation is

actually correct but it needs to be back

one we need to start the multiplication

on IAL 0 but our for Loop needs to start

on IAL 1 so we can get all the way

through

so we have two ways of doing this we

could say I minus 1 so even though it

goes from 1 to 5 it's still assigning

into the array using 0 to four so if we

run that we can see that that will give

us the desired result alternatively what

we can actually do instead of doing IUS

one inside here we can define a variable

called counter and make it equal to zero

we can change this to counter and we

don't need to change the multiplication

because that's already going from one to

the length which is 1 to 5 in this case

after the for Loop is run we need to

remember to increment counter so there

we go we have the desired effect another

hack if you're interested if you have

two variables that you want to increment

inside the for Loop you can see that we

already have I ++ in here which will

increment it by one and we want to

increment counter by one as well every

single time it loops around what you can

actually do is remove this and you know

how these are broken up by semicolons

that indicates that the section has

ended the first sections for declaration

the second is for the range and the

third is so you can increment the

variable or decrement it when you trying

to increment two variables inside a for

Loop instead of placing a semicolon here

and type in the next one you can

actually place a comma and do counter

Plus+ what that will tell the fall Loop

is every single time you go through you

want to do a series of these actions and

because we have i++ and counter Plus+

then that's going to work exactly the

same way it just prevents us having

counter Plus+ down here and we can move

move it inside the for Loop just so it

doesn't for us and it looks a lot

cleaner cuz you can clearly see that

every single time we loop around this

for loop we're going to increment these

two variables there we go so let's try

to put some different values in here

maybe make this 10 and we can have 10 on

both to give us the first 10 numbers

there we go from 10 all the way to 100

let's have a little recap so we're

essentially just making an array that

will give us back all the multiples so

it's like a mini times table but the

fact that we're doing it dynamically

using our array is quite important what

you can do if you wanted to is to extend

this program you can ask the user to

type these into the console and then

convert them and use them right now

we're just statically typing them in and

that's not really an issue since the

main focus was doing this for Loop if

you wanted to you could ask the user and

you can type these values in so you

don't have to change them every single

time you compile so now we've got the

number and the length let's make our

interray call result and initialize it

to the length we set up this counter so

we simply didn't have to do I minus one

here so it looks a little bit more

intuitive we start the follow from one

and go to the length so we can access 1

to five and the reason why we want to do

one to five so when we do our

multiplication it doesn't start at zero

these two are going through the exact

same amount of indexes 1 to 5 is five

numbers and 0 to four is also five

numbers so when we do a multiplication

we want to start from one since the Tim

table normally start at one and we go to

the length the reason why we're using

the counter is so we don't have to do

IUS one in here and it breaks up the

logic a little bit and it looks nicer

without our counter we start our counter

at zero and instead of incrementing down

here we use this fancy comma and we can

say we want to increment I and counter

every time the for Loop goes around so

we don't need to include the counter

increment inside the for Loop and this

one line is pretty simple we say result

the index of counter which will start at

zero which is the start of the array

since it's zero base and we say that's

equal to the number and the number is

going to be a fixed number since this is

the multiple and we say number times by

I and in this case it goes from one all

the way to the length so we get

essentially the times table and at the

end we just use a simple for each Loop

that will go through all the items

inside the result array and just print

them out to the screen with a comma

after each other using console. right

just so they're all on one line this is

the start of the function section

functions can also be called method they

are used interchangeably in the

introduction we discussed dry which

means don't repeat yourself and the

whole point of dry is so you don't have

multiple sections of code that do

basically the same thing you want to

wrap it around in a function functions

are used to improve the readability of

your code and the reusability of the

code as well having multiple section

code that are basically exactly the same

can be more prone to errors if you wrap

them around and place in One Singular

function you can make sure that the

error prone and then wherever you use it

it will be error prone as well if you

have to copy and paste and tweak some

code then you're actually exaggerating

the amount of error that could occur and

plus it makes your code look a lot

cleaner and the structure a lot better

since you can name the functions so

let's take a little example we have a

static void main straight into here this

is a function or a method and these are

the parameters for the function The NET

Framework expects every C program to

have a main function with a capital m

and it needs to be a static and avoid

the parameters don't exactly matter we

can also remove them and it won't be a

problem the only thing that matters is

this needs to be called main if we drop

the lower case on it and try and run the

code then we're going to get a build

error program does not contain a static

main method suitable for an entry point

so if you change this back to M and run

it again and then it works fine so

that's one thing to note about The NET

Framework is you need to include the

main function otherwise the program

won't run and it won't even compile so

let's take a look at how we can use this

we can have something like a console

right line and we can say welcome to my

program this is a line that we're

probably only going to use once but

let's say this is welcome in someone's

name welcome abber let's say you had

this in your software in many different

places you don't want to be recalling

this or retyping out this message in

case you type it wrong the fifth time

and you get a error of course this won't

be a syntax or a runtime error it'll

just simply be a logic error because

it's a typo so in order to make sure

this typo never occurs we can actually

make another function so outside of

these cury braces and just still within

the class we want to create a new

function so we want to follow this exact

same approach we can say static then

void and we can call it welcome message

and we don't need to give it any

parameters cuz we're not really going to

give it any values just for now now that

we've made that we have no syntax errors

and we can run it like this except of

course there's no body so let's place

our console right line within here now

if you run the code nothing is going to

happen and the reason for that is the

code will still execute in the main we

haven't called this function inside the

main we've defined it and called it

welcome message and we've gave it the

contents but we haven't actually made

use of it and you can see that when it's

highlighted in a darker color that means

it's not being used as it says here

private member welcome message is unused

so now we can simply type in here

welcome message followed by an Open

Bracket and then a Clos bracket to

denote the parameters that we're going

to give it and of course in this case

there is no parameters and then we close

the brackets and add a semicolon now if

you run the code you can see it says

welcome AB so just to go over some of

these words that we're using static

means that we can access it anywhere and

everywhere what I mean by that is we

don't have the keyword new just before

this we don't need to make a new

instance of the program class in order

to access this so what that means is

when we make future programs we're going

to have some functions inside our class

inside the class you can actually denote

if a function is going to be static or

it's not going to be static you don't

have to specify is it static but this

won't let you call it from here because

it's unaware where this function's

coming from an object reference is

required for the non-static field method

or property because this is static you

need to reference something else from a

static nature all static means is you

don't have to create a new instance of

this thing in order to use it we'll see

different examples in the future where

you have to create an instance in order

to use something so let me just show you

quickly if you have a test and I say

static void

something and we can just print out

nothing to the screen and let's try and

use this so because this class is

outside of the program we have to use

the notation test Dot and then it should

be available within here and the reason

why it's not is because we need to

insert this public word just before it

and public means everything has access

to it and static means you don't need to

create an instance now if I type in the

dot again you can see we can now see

something

and the reason why we can see something

is because this is a static now if I

remove the static word then you see that

we'll get a compile error because it

doesn't know where this is now if you

don't use static you need to do this you

need to say test test equals new test so

you make an instance of the class and

then inside test then we get the

something so that's the difference

between static and not static

essentially it's accessible from

everywhere but if you don't want to be

accessible from everywhere then you can

just simply remove the static word and

the user will have to create a new

instance before they can use it so this

is what it's like without

static and this is what it's like with

static don't worry if you don't

understand this too much this was just a

quick overiew but I have a dedicated

video later on in the course so don't

worry too much about it so now that

we've made a function and we've called

it successfully in the domain and it's

prined out to the

console so when we're declaring function

we want to specify what return type

we're going to have we have a static

void Main in here if you hover over main

it says void which means at the end of

this function it doesn't return anything

hence its void if you have a look at

something like an in. trass when we open

the brackets you can see that it takes

in a string and it takes in an integer

out parameter it also returns us back to

the Boolean so we can have something

like 1 2 3 out int result and then it

doesn't actually complain about anything

but what we can do is also so just store

this in to a bu success and then print

it out to the

screen there we go so it says it was

successfully true if we remove this or

whether we keep it in or not trip pass

function doesn't actually complain but

the main point here is it defines back a

bull that means at the end of this

function it will always return you back

with a bull and it's up to you if you

want to make use of it and you can

alternatively you can place this

straight inside the console right line

and get the value printed straight to

the screen or you could wrap this around

inside an if statement like we've done

in the past Etc and make use of it that

way and have the result being printed

out inside here the point is you have

functions that return something and

functions that don't if you remember

that we had the array. copy for example

you'll see that they have void here

because they just apply a function to

something but they don't actually give

you something back this is the

difference of having something like this

a string test that says hello and then

you can do test. replace and then you

can return you back with the string and

it says here it returns a new string

whereas when we had a string builder for

example and we did string builder.

replace you can see that although it

does say it replaces a new string

Builder you can type anything inside

here and it will still affect the

current string Builder it won't have to

do SB equals s SP replace as you would

need to do here because this returns you

back with a new string and if you wanted

to replace test you would literally have

to say test equals test. replace with

something like a string Builder all the

array functions you don't actually have

to do any of that so you can say array.

empty and it will just return the empty

array and the ones that we used before

things like equals and that will give

you back a Bool because that's

determined whether it's equal or not but

if you're doing functions like sort for

example and doing things like reverse it

just simply applies the data but it

doesn't return you back with anything so

you can literally just call it inside

here put your array in the brackets and

it'll use it happily now depending on

your user case you need to define

whether or not you need to use if return

type or not for now let's just cover

void which means it won't do anything so

let's just Define a function static void

create and print an array open the

brackets close the brackets and we can

have it int numbers make it equal to new

int with length three and let's just

have 0 1 and two inside here as the

initializer let's just do a for each VAR

item inside numbers and then just do a

console right string interpolation and

just have item followed by space and now

that it's dark gray because we haven't

called the function so let's make use of

it at the top of our code and let's run

it now you can see we get 012 printed to

the screen and if we make this into a

five and simply just add more values in

here and then we run the code you'll see

that now it all ofd but because this

doesn't return us back or anything and

we don't actually use the integer array

I outside of these curly braces it's not

actually available up here numbers won't

be accessible inside here because it's

simply not within the scope numbers is

only available within this curly brace

and this curly brace which means when

this function finishes numbers has now

been deleted from memory so you can't

access it after this line void doesn't

do anything it simply just does the

contents of the function and as soon as

it reaches the bottom that's all that

happens you can have another function

defined that can return a value

like I said with in. trass for example

at the end of the function you'll see a

word that says something like this it

will have return and then return back

something followed by a semicolon and

that will dictate that as soon as that

function is done it's going to give you

something back now we're going to cover

that in the next video but for now we're

only going to cover void functions so we

can have something more obvious like a

static void welcome

message and simply just do a console

right line welcome to my program and we

don't need to return anything here

because all this function will do is

simply print out a welcome to the screen

and we can use that just before the

print array so we can say welcome

message Open Bracket close bracket and

then a semicolon and hit F5 to run and

there we go as you can see our main is

really tidy now it gives us function

names that tells us exactly what's going

on we're creating and printing an array

and then welcoming the user to the

console and then having a console read

line at the end there isn't an awful lot

of void functions all you need to know

is that when you create a function you

need to give it a return type it returns

back a certain data type or it simply

just doesn't and depending on what you

want to do and what you want to achieve

with that function really depends on if

you want to return a data type or not in

the case of these since they're just

doing something and then moving on you

don't actually need to return anything

by it with in. tripod you want to make

the function is executed successfully so

when you use int. tripas you can see

that returns back a bull and the bull

determines whether the conversion has

been successful and based on that return

value you can determine the next steps

of your program if all your function

needs to do is provide some sort of

functionality and then quit then that's

fine and avoid is fine but if you need

to provide some functionality and then

also return a value then you'll need to

specify the data type in here which

we'll cover in the next

video so far we've covered aoid

functions but we haven't covered return

types vo functions are where the

function just does something but doesn't

actually return anything hence the name

void let's start with an example if you

have a static void print

introduction might want to just return

back your name after you've printed this

line it's going to print it to the

console and that's all it's going to

do there we go let's say there's a

situation where you actually want to use

this inside your code you can actually

set the console's title by typing in

console. title and type in your name

inside the quotation marks or you can

type in anything inside here if you run

that you'll see Abra is in the title of

the console right now and that looks

pretty cool what you got to realize is

now we have two references to the same

value now at this point we can actually

do something better we can type in

static string name equals abber and then

instead of using abber in here we can

say name and instead of using abber in

here we can also say a name and they're

both using this variable and if you run

the code now you see that the same

output will happen you have abber twice

both in the title and both in the

console output now the problem with this

approach is you're actually making use

of a global variable which we do not

like code in situation like this you

shouldn't need to make a global variable

because there are ways around it so

instead of having this which has a print

introduction and we've got the name used

in there but the name is coming from

here maybe we can use this somewhere

else so let's say we make another

function called Static and instead of

void we want to actually return back a

string so we can say string and then we

can say my

name or something like return name or

anything you want just make sure you

give it a meaningful name so it dictates

what the function is trying to do so

this ISS to return the name and the

reason why there's a red underline is it

says not all called path return a value

since we need to use our keyword return

now that we had some empty codes on the

return it's going to execute fine and

the compile error is gone so obviously

we don't want to just return nothing we

actually want to take this ab and return

in here so let's get rid of our Global

variable and let's see what we're going

to break so we're going to break these

two references to the name variable and

simply inside here we can actually just

call return name name and then here as

well we can call return name and what

this will do is inside here it will

execute this function and then return

the value in here if this is too many

steps for you you can have a string name

equals return name and then simply use

the name inside the brackets these two

lines are doing exactly the same thing

except this is using a variable cuz

we're storing it first and then we're

using it whereas if you place it

straight inside the brackets that's just

going to store it and use it immediately

and there's no additional variable but

don't worry too much if you're going to

do this notation because an extra

variable in this case doesn't really

matter because as soon as this function

is executed the variable used to run

this function gets deleted since it's

inside the scope of these two curly

braces so now if you run the code you'll

see that again we have abber and abber

but now what we've done is actually

control everything throughout here so if

your name were to change or maybe this

changes in a different circumstance

based on the user that's logged in then

you can simply have the same thing now

printed in two different locations in

the title and in the console output and

it's actually coming from one function

so we can have a return name and we can

make a static in now and can say return

H and then we can have simply return 23

and that's our name and our age being

returned I Chang this back to abber and

then instead of the print introduction

maybe we actually want to print a little

bit more than the name and maybe in the

title what we can do is have string

interpolation wrap the return name and

maybe put a high in there and then we

can say return age so if we on the code

now you can see we have abber hyphen 23

so maybe that's the title of our console

but then in the introduction we can use

a bit more than that so we can have int

age equals return age and then in the

console output we can have something

like this hello my name is name and my

age is age and then after we've compiled

that together we can place the output

inside here there we go so we're using

the name and the age inside the console

title and then we have this function

that runs to get us back the name and

the age and then we're using it using

string interpolation of course these

four lines can be all squished into one

you can simply take this dollar sign let

me duplicate this

line you can take this and paste it

straight into here and where you have

name we can have our return name and

where you have age you can have return

age and that will actually rule out all

of these lines and you can wrap it all

into one

there you

go and if age were to change like I said

before you can simply type this number

in change it and you can see in both

cases in the title and in the console

output it has officially changed

functions can be very helpful to reduce

repetition in code let's show you an

example if you had three sections where

you said enter a

number and you had int num one equals

convert to n32 and then we add a

console. readline

and then you had this section and you

wanted to do num

two and you had this section again and

you actually wanted to do num three now

at this point even though these three

are actually identical you want to be

using array in this case so you could

have int numbers equals new in square

braet 3 and then here we could say

numers

zero numbers

one numbers two and then we don't need

the ins cuz it's already been defined

and then we have them like this but

because again all these three sections

are exactly identical we can actually

run a for Loop that goes from numbers do

length and then we can place one of

these straight inside here and then

change this to I and now we've achieved

exactly the same thing but we're not

actually done yet so if you run the code

and we do one 2 3 now we've stored all

three numbers so we have enter a number

and then we have the console read line

and maybe we can actually make these two

lines a little bit shorter so let's make

a function static and because we

essentially want to return an integer we

can say static in read number from

console and then if we take these two

lines place them inside here and because

we need to return back an integer this

is the value that we're going to be

returning so instead of assigning this

position to something we'll actually

just immediately return at this point so

instead of doing this line now and

instead of doing all of this we can

simply make this into one line of code

read number from console so every time

this will execute will jump into here

print out the value enter a number the

user will typee it in it will read it in

convert it to an integer 32 and this

will return back the integer that the

user's typed in and then that will

return it back to the console and

essentially replace this so if the user

is typed in five then it essentially

replace this with five store it loop

around and have to placed in 10 the next

time and then you replace this with 10

essentially and then that gets stored

inside and so on and so forth until all

the positions have been complete let's

test that 1 2 3 and it still works okay

of course I'm not actually printing the

values out to the screen so if you

wanted to see that quickly for each can

have V item inside our numbers and then

do a console right with some string

interpolation item followed by a space

and let's run that one two and three and

there we go we get one two and three of

course you can make these functions and

they can return any bits of data that

you want you can even have something

like an INT array and we can say create

random array and then we can create an

array call numbers make that equal to

new int square bracket 3 and we can

actually give it the values of 0 1 and

two and then just at the end here we can

actually just return numbers and then

just in here we can have an INT array or

we can call that new numbers since we

have numbers above just above here and

then we can make that equal to create

random array and because this returns us

back with an integer array there's no

compile array inside here and we can

have a four each follows the exact same

approach as the one above new numbers

console right line into a console right

and then have item followed by a space

now if we print these both out you can

see that the zero one and two actually

comes from the new one it's just we have

two console rights right next to each

other so there's no new line separating

them so if we just add a console right

line here 1 2 and three and there you go

1 2 and three comes from the three

inputs we just typed in and 0 1 and two

actually come from the function that

we've just created of course if you

don't like this notation then what you

can do is you can make it a lot easier

you don't even need to make a new

variable you can actually just return

the integer just like this so if we

place

this straight inside here actually just

make it on one line just so it looks a

bit neater then there you go we have our

entire function reduced in one line the

goal of functions is to make them do as

little as possible if your function has

two different things it's doing or more

then you should break that out into

another function for example if you had

a function that did two distinct

operations one was a conversion and one

was a calculation then you want to split

out into two the first function should

be the conversion and the second

function should be the calculation and

the reason why they're separate is

because if you want to reuse them as a

pair then you won't be able to reuse

each of the pairs if you wanted to have

a calculation on its own then you can't

do that if both functions do the same

thing for example you could have static

and it just could be something let's

just call it void and it could just be

test for example and if you had the

conversion here and then if you had the

calculation here then you need to split

this up into two different functions you

want have static valed

conversion and static vied calculation

if you wanted to use conversion or

calculation separately you can't because

now it's in this pair together so if you

wanted to use them separately you simply

can't it's best to keep functions on the

RW anywhere just so they have a single

purpose so you can give them a name cuz

as you can see here we can't give this

an appropriate name because it's doing

more than one task this is doing the

conversion this is doing the calculation

and this is doing mult multiple things

this could be doing something if you

have to think too hard about the

function title then you might be doing

too much within each of functions these

are very easy to be able to decide what

they do cuz this one converts and this

one's going to calculate reading number

from console if this was reading the

number from the console and doing

something else then it's going to be a

bit of a problem because now you've got

a bigger name you don't want to be doing

reading number from console and and

something else cuz then your function

name gets also too big and you got

multiple dependencies so the best thing

to do is to call them accordingly and

keep them as short as possible so let's

say we wanted to make a function that

added two values we can have a static

int call it add and then we can return 5

+ 5 now this function is a little bit

redundant although it will actually work

so if we go back to our main and we do a

console right line and actually print

the value of

ADD and we go through our function then

we see we get the value of 10 which is

indeed 5 +

5 but since it's 5 + 5 won't change

range it's always going to be 5 + 5 this

is where parameters will come in which

we'll cover in the next videos

parameters are a way of being able to

give the function value so these two can

be actually changed there's no point in

having an add function if the only

ability you can do is add 5 + 5 cuz you

may as well just return 10 at this point

so I covered a lot in this video and

there's lots of different functions that

do different things of course there is

quite a lot of lines we're up to 87 this

is probably the biggest project that

we've wrote so far and even though this

is not really a project it's just a buns

functions that do little things that are

made for demonstrations so let's have a

recap so first here we're setting the

title as a result of return name and

return age we can actually take this and

develop this further if you want to we

could actually say return name age pair

and if you wanted to collapse that

inside one function then that could be

really easy you could say static string

return name age pair and then you can

have return and literally the exact same

line above you can have return name

followed by a hyphen and then you can

have a return age and then end the line

like that so that just concatenates the

two values that are returned from these

two functions but the point is you keep

them all separate if you were to type in

very statically inside here AB 25 then

obviously like I've talked about above

then you're going to have duplication of

data and of course we're going to use

this name inside something like here and

as you can see this name age pair is

actually not applicable when it comes

back to this format so it's always best

to keep the these two separately so if

you bring it back and we can actually

keep it like this now instead of having

this code we can actually say return

name age pair that will give us the same

value at the top of the code it will

have ABA hyphen

25 so that's that bit in our print

introduction If We Hold control and we

actually press on this function it will

actually jump us right down and then if

we press the back arrow top left we can

go back to it there we go and then click

forward so in this function we can break

down all of the return values into to

string variables we have a string name

equals return name and inage equals

return age then we have a string input

that concatenates them both together

hello my name is name and my age is age

and then we print out to the screen or

if you wanted to we could wrap all four

of these lines straight up into one we

don't need to assign these into

variables although it does help the

readability if you keep them separate

but I prefer to keep it like this since

I'm used to this notation we can place

return name straight into there which

will get rid of our name variable and

the same thing with age which will get

rid of these two variables and of course

we're actually storing the string value

before we printing it so we could just

take all of this and place it straight

instead of here scrolling back to the

top this little section was

demonstrating how we can use integer

arrays to read in all the values from

the console but wrap it around in a

function so before in here you would

have something like a console right line

You' print out the value to the screen

that would say something like a ter

number and then you have on the next

line a convert to in32 and then after

you converted it in 32 you'll actually

start straight to numbers but because

this is going to be the same thing for

inserting all the numbers you can

actually wrap this around in a function

so if we hold control again and go to

this you'll see that them two lines are

very simply placed in here console right

Line enter a new number and then convert

in 32 based on the console read line and

then we simply just returning that back

ready to be stored in the index of the

numbers array and then straight after

that we're just printing out all of

values to make sure it's worked and that

all succeeded fine here I was just

demonstrating that you can have

different return values and it can

literally be anything so as you can see

here if you hover over create random

array it's inside the program class it's

called create random array and it

returns back an integer array so that's

what we're signed into and if we hold

control and click on this again you can

see that all this simply does is returns

us back a three length array with 0 1

and two as the array positions and then

if we scroll back up when we execute

this for each Loop then we're simply

going to get 0 1 and two just like that

since it's a console right and it'll be

on the same line with a space after each

other this demonstration was slightly

theoretical I was saying in one given

situation a function should only do one

task if your function converts and does

a calculation at the same time then

these are actually two different things

you should split them so you use the

conversion in this step and you use the

calculation in this step if you want to

convert something or calculate something

in the future then you can only do it

now because we've separated them whereas

before if you have them both inside here

then you simply can't do one operation

because you've wrapped it around in one

section the same is true when I was

discussing this earlier on if we kept

this name pair as the only way to access

the age in the name then it's going to

mess up when we use it in here and then

we're going to have a duplication of

data but because we separated the name

and we separated the age we can use it

in this function and we can in this

function and it doesn't interrupt

anything this simply Returns the name

this simply Returns the age and these

two functions make use of them if we had

something else underneath there were

another function that depended on the

name of the edge then again we can

simply use them if it was restricted to

this function then it's going to be a

problem using it in this function or any

future

ones and finally just to finish off add

function and all it simply does is

return 5 + 5 and I was saying as of

right now even though return return

returns back a value it's kind of

pointless CU you may as well just return

back 10 since it's doing the same thing

the reason I included this function is

that we can set it up and start planting

the seeds to understand why functions

exist and how to make use of parameters

what a parameter is is it lets you put

things Within These brackets so you can

give this function some data so if you

have an in. trass for example these

things that we place inside the brackets

are actually the parameters so we have a

string s and out in something and that's

how the parameters work on our trip pass

function and just to show you this is

how it's actually wrote It's a Bool and

then it's called

Triassic and I know it's give us an

error because we don't have the control

path if we just use result and assign it

to zero and then return back true you

can see that the compil error is now

gone and as you can see here it says

bull trip pass string s out in result

and if we actually try and use the trip

pass in here and press the bracket you

can see that these parameters are

exactly identical to parameters that

were inside of the tripar coming from

the integer Library so in the future

we're going to study how we can use

parameters to pass data inside our

function but just for now know that this

function is essentially pointless

because it's only going to return back

10 and this function will gain the

functionality it needs when we start

adding parameters this should

essentially be add five + 5 except this

is kind of a pointless function because

we already know the value is going to be

10 but again this is just helping you

understand why parameters are very

essential to

functions we've covered how to create a

VI function and a function that returns

a data type but now we want to create a

function that gives it some sort of data

you may not realize it but all these

functions that you normally use like

console right line you're actually

inserting data consistently into all of

these functions if you hover over

console right line you see his void

console right line and right now there's

Open Bracket and there's a Clos bracket

if we delete this bracket and reopen it

you see there's 19 different ways that

you can call this function and as soon

as you start to type a string you can

see we have a string format and an

object if you need to and if you type in

another string you can see it says

string value this string value is

actually a parameter If We Hold control

and press on right line you can see all

of the different functions right here

this is the most basic version that just

inserts a right line character to the

console and then these are all the

different parameters that you can give

the value of course you can't double

click and view the source code because

this is hidden inside Microsoft's

libraries the values that are placed

inside these parentheses are actually

called the parameters this is known as

functional overloading which we'll cover

later on in the course basically that

means you can have the same name

function but give it different

parameters and you'll see why this is

helpful later on down the line so let's

close this tab in the top right hand

corner and let's bring back out code so

now that we've passed hello into this

function that was actually given this a

string value parameter and that hello is

going to get past straight into the

console right line so then when we run

the code it's going to print into the

console so let's create our own function

with our own parameters so if you take

the function I had last time it was

static int add Open Bracket close

bracket and it simply returned 5 + 5

which we all know is going to be 10 so

if we place add inside these brackets

and we run the code we should get 10

return back there we go so of course

this is a little bit pointless since

it's always going to be adding 5 + 5

there is no way we can change this

without the use of parameters so in here

we can actually just do what we do when

we normally declare a variable we say

the data type int and then the variable

name and then if you want to add

multiple parameters you can simply add a

comma so now we can say int a and int B

and you can see now we've got a syntax

error because it expects us to have both

of these values inside if you hover over

it there is no argument given that

corresponds to the required formal

parameter a and then if we insert a by

having five for example and if we hover

over it it complains about not having B

so we can do comma and then do five

again so just this on its own is not

actually going to do anything we're

actually giving the add function the

values of five and five and place them

inside a and inside B variables these

are now known as local variables and

they only exist in memory between these

two curly braces as soon as the function

is done these are deleted from memory

and you can no longer access in a and in

B you can't just do something like a

inside here and think that this can be

used up here these are in two completely

different Scopes this is one for

function and this is another function

and they're completely separate from

each other when you type in five and

five it goes in order so int a is the

first one so five gets placed into here

and this five gets placed into B so now

that we need to make use of them we can

simply change this to a and change this

one to B now if you run the code we

should still get 10 but now our function

Works a bit better we can give it a set

of data so if you actually just put

zeros instead of here then we should get

a 100 returned perfect so now that we

figured out how to to do this let's have

a look at different ways that we can

make use of this function so let's say

we had a situation where we did console

right enter a

number and we add intn equals convert to

in32 and then you're converting the

console do

readline and let's wrap this inside a

function so before what I was doing is

simply just returning back in int it was

static int read

int and then we took these two lines and

place it straight inside here and remove

the fact that we have an integer and

simply just change this to a return and

what this now will say is let's output

enter a number to the console let the

user type it in inside the console read

line and then read it in convert it to

an integer and then give it back to

whatever calls it so now we can get rid

of this and get rid of this convert and

simply just type in read in inside here

and after that line let's just print it

out to make sure it's worked okay enter

a number 10 press enter and we have 10

again and that's fine except if we want

to use this in another case for example

if you have num one maybe we don't

actually want to enter a number maybe

this time it's going to be enter an

angle or something of this sort so what

we can do if we undo that we can

actually introduce a string message and

what this will do now is actually

specify the message that we put in here

maybe there's a situation where we

always want to enter something so maybe

we can just replace this and put a

string inter polation and wrap this

number around in the curly brace and

replace it to message so then in here we

can say enter a and then we can pass the

string variable which will get placed in

here so if this is a number then we can

type a number and maybe this is

something like an

angle and then here we can type an angle

so we have 10 enter a number and then

there you go it says enter an angle then

we can have 100 and that's working as

well alternatively if you want to

specify the entire message then you can

remove these and keep it like this and

then in both of these situations we can

say enter and number and copy that down

like this and make sure you put the call

on at the end followed by the space

there we go enter a number 10 and an

angle 20 maybe the situation where this

text actually changed and you just want

it to be angle and if you run that again

then you'll see that these are

completely different now one says enter

a number and one says angle but this

gives us a lot of flexibility in what we

pass in if we keep it as enter a and

then let the user decide on what this

section is then you rule out any

opportunity to be able to do that option

if you wanted to to prevent the colon in

the space always being at the end you

can actually just add that in manually

if you bring back the string

interpolation we can say colon followed

by space so you don't have to remember

to always put that in there we go so

that's doing the exact same thing and

we're actually just reducing the amount

of repetition because we have to

remember to put a call on Follow by

space inside here we might just forget

it and then when you leave it like this

then the cord's going to look ugly

because it looks like this and it's all

stuck together so let's make use of

these two functions and let's see if we

can write a really basic calculator we

can say int first num equals read int

and we can say enter first number and

then duplicate that down change first to

second and then change this also to

Second and then inside a console right

line we can actually have the add

function and we can type in first number

and then we can type in second number

and if you don't want to call the

function inside here you can actually

just make another int and we can say int

result equals add and just bring this

line over to here and then in the

console right line you can just print

the

result there we go so we read in both

the values execute the ad function and

then return it back to the user using a

console right line so these are the

other ones and then here they are so if

we do 50 and we do 50 again and you see

that I've messed up because we have

double colums now so although our

calculator did indeed work let's just go

and correct that so we can take these

off now because we added it inside

here 50 and 25 we should get set 75

perfect so now we've made a little basic

calculator by simply making use these

functions and if we just expand this

just to show you what it would have

looked like so we go in from this and we

place our message inside to here and

then we have our convert line just like

this and then if we duplicate that down

we can change this to Second n and then

to second

number and then as for the result we can

say first num plus second

n there we go you can see the code is

drastically longer and it's a bit

meaningless to have duplica code this is

literally the exact same line in both of

these sections except for the fact that

this ISS first and this ISS second and

we're starring in first and we're

starring in second so let's undo that

and get back to our more efficient code

there we go you can see that these three

lines are considerably shorter and

easier to read this is telling us we

want to read an integer here is the

value and we want to return it inside

our first Nome and so on and so forth

let's have a look at another example

where we can make use of this so we can

have a string called name and we can

read this in from the console console.

readline but we need to Output a message

so we can have another console right and

we can say enter your

name and then we can have this twice and

we can do enter your age and then have

an INT

age but of course we don't want to be

using this approach and putting a

console right line inside here with a

convert in32 because although this is

actually technically correct now what

UND doing the point Val function so

let's change this into our readint and

then as for our message well we have the

message right here and again we don't

need the space callon so let's remove

that and then as for the name we can

actually do this differently so this is

actually going to do a string variable

so what we want to do instead of having

read in we can have another function

called read string another string

message parameter and then do exactly

the same thing have a console right and

we can place string interpolation and

place a message inside here followed by

callon space and then we can just simply

return the console. readline there we go

and instead of doing all of this we can

actually just put our read string inside

here and then place our ENT your name

inside here just without the callon

space because we don't need that so now

we reduce this section to two lines as

well so now what we can do is have a

string details and make that equal to a

function that we're going to use now we

can say user details if we want to give

it the name and give it the age and that

will give us back a string that we can

print to the console so we can say

details so let's go ahead and make use

of this function so we can have a static

and it's going to return us back a

string cuz we're placing it inside the

string and then it's going to be user

details and then our name is a string

our age as an INT and then inside here

we just want to concatenate them two

values together so we can say hello my

name is name and my age is age and

because that's returning us back with a

string we can make use of it inside

string details or we can place this

straight inside the console right line

let's leave it as a function call assign

into string details and then print out

to the screen let's just skip over these

sections enter your name abber enter

your age 23 and then there you go hello

my name is abber and my age is

23 so this is how we can make use of

parameters inside our functions so we

can actually give it some sort of data

that's meaningful if you didn't use

parameters then what you're going to

have to face is for example example what

we showed you with this integer if you

don't use parameters then you're going

to have situations like I showed you

before with this integer function it

returns back an integer but it only gave

us the value of 5 + 5 I mean technically

you don't need to return anything you

can actually just keep this aoid and

just wrap this around in console right

line the only issue with this approach

is you don't have any option other than

to print out of the screen what if you

didn't want to print out of the screen

and you simply just wanted to return the

calculation and then store it inside

another variable this doesn't give you

the flexibility of that so we change

this into an INT and instead of

returning it back to the console we just

return back the pure value and then if

the user decides that they want to print

this out to the screen that's fine like

here we've made a decision that we want

to print it out let's just say we had a

variable that said in calculation then

we place that inside but we actually

never wanted to print it maybe we kept

it like this and then use Cal for

something else down below or maybe that

was something like storing inside a

database not everything will be printed

out to the screen for the user to see

the best thing to do is return the

values in their purus form because you

never know what the developer or the

user is going to make use of that data

later on down the line keeping that it's

purus form is the best way possible for

example in this case we're actually

reading back the integer and then print

it out to the screen but in this exact

case which is the best case scenario

that I just showed you we're actually

making use of first n and second n but

first n and second n are never actually

print to the screen it's always just

used for the calculation if we use

console right line to print out first n

and second them then we're not going to

be able to use it inside our calculation

and therefore our very basic calculator

is now being rendered pointless so read

in and add or return back values that we

can use in their purest form and that is

the whole goal of

parameters so now that we've covered

parameters that are always going to be

required let's have a look at another

concept called optional parameters so we

had a static in add and we add int a and

int B for example and then we simply

just returned a plus b like use in the

previous examples if we have an INT

result and we want to call that function

and we give it the value five and five

and then print out to the screen

everything runs okay because we've added

in exactly what we expect let's say we

don't actually have this additional five

of course this doesn't make any sense

cuz we're meant to be adding two numbers

but just hum me for a minute then what

this will do is says there's no argument

given that corresponds to the parameter

B now of course this is a problem

because it's a red underline which means

if we try and run the code we're going

to get an issue let's say you were

creating some sort of system that if you

entered both values it uses both values

but if you only entered one it

automatically set the second value to

something else and we can achieve this

Behavior using optional parameters so an

optional parameter doesn't exactly mean

quite optional it kind of just means

that it's going to be optional but you

can also give it a default value this

will always add two numbers A and B even

if we make a or b optional it's going to

still need two numbers to add in order

for the function to run so in order to

make a parameter optional let's just say

we want to make B optional we can type

in it equals straight after it and

actually give it a value so as you can

see our syntax error is now gone and if

you run the code we should get

55 and the reason why this works is a is

now five if you hover over here we can

see a is five and then the square

bracket around the int B actually

denotes that it's optional and it says

int b equals 50 cuz we have that h here

so it's actually using this as 50 and if

we don't specify the second parameter

then it's practically just doing this

which is completely fine so the reason

why I said it's not quite optional is

because you still need to give it a

value of course in here you can use the

default keyword in the case of an

integer the default value is actually

zero so if you just run that code now

it's going to say 5 + 0 which is just

five so it can be optional but you still

need to give it some sort of value of

course if you're doing a calculator and

you're adding to values if you only

specify the first one then adding zero

to it is simply not going to change it

so zero or default is going to be the

best user case when you're dealing with

default values with integers so another

way you can actually specify this is by

inserting this thing called an attribute

if you remove this equal zero and just

before the end we can type in Open

Bracket close bracket and type in

optional with the capital O and then

you're going to get a red underline if

you hover over it it says we need to

include this extra Library which which

will import it at the top of our code as

you can see here now that this is

available it says indicates that a

parameter is optional and what this will

do by default is it'll give you the

basic value for this number so what I

mean by that is we can type in here just

to show you this b equals and then B and

this will just give you the default

value based on the data type and as we

just said a minute ago that value is

actually going to be zero there you go B

is zero so all it's doing is saying 5 +

0 = 5

so that's another way of doing it or you

could just leave it as I said before you

can say into b equals default or into b

equals 0 it's completely up to you I

prefer using the default keyword so you

don't have to keep remembering which

different data types have the default

values in the inter just case it's zero

but I just like leaving the default and

we can get rid of this line now let's

have a look at different situations

where this will be helpful if you had a

function called print name and we add a

string name in here and then we can

simply just say console right line my

name is and then name this is quite easy

to use so we can say print name and then

Place AB inside the brackets and then

run the code and we get abber printed to

the screen and that's fine let's say if

we had someone else's name in there as

Joe for example then we get Joe to the

screen and let's just say as the

developer you only want to default this

to abber if you don't actually insert

anything so now if you don't insert

anything of course we're going to get a

compile error because we need a

parameter but if we assume what we did

here and just follow the same approach

if we use default then it's going to

give us nothing because the default

value for a string is just empty quotes

which is just nothing but instead of

default we can actually specify value so

we can say if no one types their name

inside the bracket then use

abber there we go so you can have this

optional parameter that defaults to

something and if someone types in jaw or

whatever inside the print name function

then the default optional parameter is

not going to be used since this will be

overridden if you have a parameter

placed inside here so now the ab is

going to get overridden by the jaw

because this is actually the parameter

that's been used in the

function so you've looked at functions

with normal parameters optional

parameters and now let's take a look at

name parameters so let's say you had a

situation where you had string name

abber in age equal 23 and string address

equals one something rad and then you

had a function that would simply print

these details out to the console so

static void print details there string

name inage String

address so you have three console right

lines that just print out a name value

pair just like

this and then when you go to call this

function what you're going to have to do

is specify the name then the age then

the address in that specific order as

per the parameter show if you try and

run the code now we should get the

desired

output perfect let's say for example you

wanted to rearrange the parameters and

put Age first you'll see we have a

compiler error now because age and name

are in the wrong order string name

should be first and int age to be second

we can still achieve this by using

something called name parameters we

don't have to stick to this order if we

tell the compiler which parameter we're

directly talking about so we can say age

callon because our parameter is now

called age as you can see it's been

highlighted and then after the colon

It's the variables that we're talking

about so it just happens to be that age

is the same value as the edge

so maybe we can change to age input and

place input the end of these so then we

can have age equals age input and name

parameter equals the name input and the

address parameter equals the address

input now we can have it like this and

to make it look even better you can

actually add new lines in here just to

break it off like this and this is a

similar pattern that you'll see in

normal production code putting a space

here can separate them out a little bit

and there we go it doesn't matter which

order that we put these in right now we

can even put address first first and

it'll still work okay and if we run it

you should still get exactly the same

output it's just specified in a

different way so let's just have a

little recap to make use of name

parameters you don't need to do anything

to the original function you just write

it exactly the way you're used to doing

you specify the parameters in the order

that you think is sensible and when you

go to call it you can simply just type

in the parameter's name first followed

by callon and then the variable name

that you want to address once you've

done that then you can use name

parameters you can go back to using the

normal function Fun by just getting rid

of the name followed by the colon and

just replacing all of the parameters

using the correct order and the original

variables instead of the new notation

having Name colon followed by name

input so far we've covered functions

with no parameters normal parameters

optional parameters named parameters and

let's look at a concept called out

parameters let's demonstrate a quick

example so we call a function called

test and we pass in a value and let's

just make this work so we can have an

INT Norm equal zero and then we have a

test function so we can say static void

test and we can say in n and inside here

let's make num the value of five now in

this example if we print out num to the

screen what do you think is going to

happen is num going to take the value of

five or is you're going to keep the

value zero I'll give you a second to

think and let's run the

code so let's run it and see what

happens and if you guess five you're

indeed Incorrect and if you guess zero

you are correct correct and the reason

why it's not five is because when you

pass a num inside this parameter it just

becomes a parameter and therefore it

becomes a local variable even though

these two variables are actually called

exactly the same thing you'll see that

when I highlight it it only highlights

this one and doesn't highlight this one

and the same is true for this it

highlights these three references but

not the ones down here the num variable

is only accessible in each other scope

this one is available through here and

here and this one is available through

here and here therefore if we use this

assignment then it's actually doing

nothing if we print out gnome inside

here num equals num and then we print

that we can see that it is assigned the

value five but because it doesn't do

anything at the end of this function

then it simply doesn't get returned back

or anything happens to it so let's see

how we can make this in to five when it

gets printed out here so the options

that we have is we can actually make

this viid an integer and we can say num

equals 5 and then we can return num or

alternatively we could simply just

return five and not do both of these

lines so if we run that now then we

still get zero and the reason why we get

zero is the test function is being

called and it returns back an integer

but we're simply not doing anything with

it so we can say num equals the return

value of this function which will be

five and now if we run it we should see

how five perfect now this is a little

bit cumbersome and let's say you wanted

to return a value and return back

another value at the same time than what

are your options so let me show you a

quick example when you're using int.

trass we have a string s an out int

result and a Bool so what this function

is effectively doing is return you back

a true or false value to dictate whether

or not the conversion has been

successful and it's also returned you an

out in result and what this means is you

can actually specify integer variable

straight inside the brackets so in

essence this function has gave you back

two values it's give you back something

from the integer parameter and it's also

returned back a Boolean variable so

let's convert this to code and see what

the third option is of trying to return

back five over to this so if we just

comment out this line and as you saw in

the trip pass it simply just need to put

the word out just before

here out int num now we don't actually

need to return anything in this specific

example because we want to just change a

num variable using the out parameter but

if we wanted to return something

separate to the num like a boing like

the tri pass did then we have that

option so we can return back true but

just before we return back true as

there's a red underline here it will say

that the out parameter num must be

assigned so we need to give num a

value we give it the value five because

what we're actually doing here even if

we double click on this we'll still get

num being selected here cuz the scope is

still exactly the same now except what's

happening is when this variable gets

returned it actually gets put into the

variable that it was initially called

with so let's fix these compiler errors

first so the reason why it's complaining

is because we need to give it the out

keyword so when we're specifying in Tri

pass our first parameter is a string so

if the user types in 1 2 3 we put in the

first parameter and then we'd say out in

result or something along them lines you

have an out and then you say you want to

make a new integer and you want to call

it result see in our case we don't need

to put int just like in here because

we've already created our num so the

only keyword we need is how to follow by

space and now of course we're trying to

assign this back into an integer which

we don't need anymore because this is a

booing so we can say Bo

success equals test out n so we print

out

Success Print them both the screen now

we should have five and true the true

comes from the return true and the five

comes from num equals 5 so what's

actually happening here is we're

creating our new integer we're passing

it into this function we're also telling

it's going to be an out parameter so

this function can still output things to

our number variable and then when it

gets placed inside here we assign num

equal to five and then just because we

have a bull we have to return true or we

can return false of course we can just

take this out completely take away the

return true and just keep this as

test of course in both scenarios these

aren't actually do anything they're just

completely random values it's just made

for demonstration so bu is returned back

true or false doesn't actually mean

anything I was just demonstrating that

you can have two return values from one

function there we go we get five when we

run it just like this so now I've

completed this little example let's have

a look at how it's used in the real

world so before we continue let's see if

we can rewrite the tripal function in

our code so it's going to be a static

returns back a bu we have a triy pass

our first parameter is a string s and

our second parameter is out into result

and inside there we can just do result

equals to zero and then we can return

back true of course we actually need to

do conversions inside here if you want

this function to work but I'm just

joining you for demonstration purpose if

we uncomment this line out and have a

look at the parameters again you'll see

that now they line up perfectly string s

out in result and it returns back a Bo

which we have exactly the same here

there you go Bo in. trass string s out

in result so that's how this function

has been declared of course they're

doing something inside the body that can

take in the string convert it to an

integer place it inside result and then

return whether or not the function has

been executed successfully so now that

we understand how to make an example and

how to make the trip pass function

ourselves and to give you the

Declaration let's have a look at a real

world example so if we had a list of

string and we had our shopping list that

was used from the examples we can have

list string and let's just create our

own initializer just with coffee and

milk

inside there we go so now that we've

created that we've got our shopping list

if you wanted to find something inside

the shopping list we have the function

index of and what that will do as it

says here searches for the specified

object and Returns the zero based index

of the first occurrence so if we were

search for coffee then we should get

return back zero so let's try and run

this and put it in the console right

line and see what the value is there we

go we get zero since it's the first item

and if we place milk inside here we

should get back one there we go so let's

try and rewrite this function into our

own code so all this will do is loop

around all of the positions until it

finds it and it'll find the first one

and it'll return us back the index so in

this case there's no out parameters but

there is a return value so we can also

include an out parameter and then

instead of assigning the value of minus

one we can actually assign the value of

true or false to dictate whether or not

that we found it so let's write the code

normally in here and then let's wrap it

around in a function so we can say int

index equal minus1 because we want it to

start a value that we know is incorrect

since the lists are from zero onwards

the value is only ever minus one then

that means it's not in the list then we

can then create a for Loop intire equal

Z shopping list do count because we want

to go through every single item inside

the shopping list and inside here we

want to do a lowercase comparison so we

can do if the shopping list square

bracket I do to

lower do equals and then we want to make

that equal to just something that we're

going to type in statically so we can

just say coffee we can make this into a

parameter after but this is just for

demonstration purposes so we have a

bracket and if we found that match then

we can say index equals to I cuz the

index of the position that we want to

find must be up to I cuz that's the one

that we've made a match with and we

don't need an else inside here we don't

need to set an else inside here to make

it equal to minus one because if it

simply doesn't find it then the index

variable won't change its value so it

will always stay minus one if it doesn't

find anything so after the followup is

run let's just print index out to the

screen there we go we have zero and if

we change this over to milk run it again

then we actually have one placed inside

here perfect so now what our function is

doing is giving us the values of the

index if it finds it now if you type in

something else if you just add another

can let's see what happens then we get

minus one and minus one means that it's

incorrect and it hasn't found it and

what we can do inside here we can say if

the index is greater than minus1 then we

can say found or else we can say not

found so let's run the code now and see

if we get a visual representation not

found and if we changes back to milk run

it again then we get found perfect so as

you can see here we're actually checking

if the value is greater than minus one

but it'd be easier if you had a variable

called found and it actually a booing

variable so we can make a boo found and

make that equal to an if statement

condition so we can say something like

index is greater than minus one which is

exactly what we had here a second ago

and because this is a condition it's

either going to return back true or

false and it's going to place the value

inside found and then we're going to use

found inside here this is easier logic

to understand if this is true then it's

found if this is not true then it's not

found true true false false it's quite

really straightforward and very easy and

clear you have an index and you have

whether or not it's been found so let's

try and wrap this around inside a

function so just as the trip pass does

it returns us back with a Bool which in

this case will be our found and it also

gives us back an integer result but in

this case it's going to be our index so

let's see what we can do to write this

so we can have a static we want to

return back the Bo and we can say find

in list we need to give it the item

we're going to find so we're going to

need a string s we need a string s for

the item that we're going to find we

need to give it the list as well and

then finally we need our out in

index there we go so let's comment out

this code here

and let's copy this down into our

function and see what we need to do so

index has the value of minus one we

don't need the integer variable

declaration here because we've already

declared it inside the parameter so

after declaring inside here we give it

the value of minus one minus one

indicates it hasn't been found since the

index are zero based so instead of

shopping list we now just have list and

list here as well and then instead of

having it being equal to milk we

actually want to use our s variable and

to make it better we can convert it to

lower case just so we have lowercase

conversions on both sides of the equals

if we find a match between the list that

we've passed in and the S variable which

is our surge term that we've passed in

then we assign I to the value of index

and then it's still got a red underline

because we haven't returned back

bu so right now we actually want to

return back the found variable so we

have two options here we can return back

found or we can simply take this and

return it back in here we don't need to

run this code to see if it's found or

not and then store it and then return it

after since we're not doing anything

else with it we could just return it

like this this makes the code

considerably shorter because this will

just return back a true or false if the

index is greater than minus1 then it

will turn back true if the index is less

than minus1 then it will turn back false

so let's see how we can use this

function inside our code now so we

wanted to search for milk so let's just

copy and paste this line and let's see

if we get the same output as the

shopping list index of so now our

function is called finding list you have

a bracket and then our string s is going

to be our milk our list is our shopping

list and our out in

index so this is going to return us back

with a booing so we can print that

booing to the screen and then if we just

duplicate that down and we can print

index to the screen and maybe using a

console right line and just place a

space right at the

start so you print out whether it's

successfully converted or not and we get

the value back from

it let's run the code

so we get true plus the value of one so

that means it's found it and it's found

it at one index and the previous line

before it which just run the normal

index of function also return back one

so they're both the same let's just get

rid of that

space so if we change these both to

coffee then hopefully we should see the

same result but with index zero there we

go this zero comes from the index

function this true is from our function

tell us has been successful and this

zero is the index so we can actually

take this and put this straight into an

if

statement so just like we had down here

with the found and not found we can say

if and then this function call and then

we can say found and then the item what

we passed in so in this case it's going

to be coffee found

coffee found coffee at index index and

if it hasn't been found then we can just

print not

found and this is getting an error

because we have two indexes up here so

let's comment out this line and let's

try running

out found coffee index zero and you can

see these two are the same again one

comes from here and one comes from that

index we change these both to milk now

milk milk and milk then run the code and

you see found milk index one if you

wanted to just before you pass in here

you can ask the

user enter an item to

search store it inside string search

equals console.

readline and then instead of passing mil

here we can just pass in Search and then

instead of passing milk in here we can

also use the search variable so now if

we try run it again we can type in milk

and then we go file milk index one and

then coffee found coffee index zero and

just to give this a test let's just try

and find m not

found

perfect so let's have a little recap

based on this function so initially we

try to assign the value of zero and then

print out a normal function and inside

test we just had a simple num equals 5

and what this was doing inside is just

locally assigning it but not actually

return anything and then we said we

could assign it and then return it back

and that was another option of being

able to assign the value to five and

then we use the out parameter which

allowed you to pass in an integer when

you assign in here it will pass it back

into the original call and place it in

this variable here so when we print out

on the next line it says it's equal to

five we wrote our own little shopping

list for the list of strings and then we

called the shopping list function index

of and then we actually created our own

index of function and made it into a

function so just a little recap what's

going on here we have a string s which

is our search term the list full of the

items in the index that we're going to

pass back to the user as well as

returning back a bu to indicate whether

we found it or not we set the index to

minus one so then that completes our

logic at the bottom minus one means it

hasn't been found found since the list

is all zero based if this value is

greater than minus1 then that means that

we found it inside the list since the

list is zero based and if it's not that

and this returns back false then we

mustn't have found it because this line

would have executed so we Loop through

the list we do a lowercase comparison on

both sides of the equation on the list

item and on the surge term if they both

match we run the index and store the I

variable inside the index and then we

return whether index is greater than

minus one so after we return this back

from the end of the function we can see

once we run it inside the console right

Line This is just going to return us

back with a true or false value as well

as creating our new integer variable

called index so on the next line we can

use index and print out of the screen

this line should return true or false

since it's a Boolean return and this

variable will either give us minus one

or something above minus one but in this

case when we actually make use of the

function we can ask a user to enter

something get them to type it in and

pass that into the function but more

importantly when this function returns

if it's true we output it's been found

and if it's false we output not found

and we only make use of this integer

variable should this return back true

there is no point in printing minus one

to this screen because that's what

you're going to get if it's not

found it's going to look something like

this which is kind of redundant because

the user doesn't care about this so we

don't need to include it when we're

printing not found but we should include

it when we're printing out the found of

course this is just a random example Le

and you might not need to print it out

to the screen or you might just do

something else inside here you might be

searching for something and then you

might want to edit it or do something

else with it and then you can make use

of that index if you wanted to ask the

user to search for an item and then tell

them what they want to replace it with

then you can also do that just as we've

covered in the last

examples so after we've covered out

parameters and normal parameters let's

have a look at a concept called passing

by reference so we created inome equals

10 and then let's call it assign and we

passed it straight into here and we can

have a static void assign in

N if we say n equals 20 if we use a

console right line up here then num is

still going to be 10 because we're not

returning it and we're not really

affecting it now other ways we can do

this is by changing this into an INT and

then we can say return 20 and then we

don't really need this anymore so we

could just say

num num equals a sign and then we'll get

20 assign back in there we go all we can

actually make this an out into num and

then we can say num equals to

20 and then inside here we can say out

num we don't need to return back in

anymore so we can make this valued and

you see the value 20 so there are

different ways that we can assign a

variable straight from here so let's

have a look at another method so instead

of having out we can actually use a new

keyw called ref and the reason why it's

ref it means it's passing it by

reference so when you pass a value in

through a parameter normally you're

actually passing by value and what that

means is you're copying this value that

gets passed inside the brackets into

here and it's making a whole new

variable what I mean by that is this

might have memory dress location one and

then when this variable gets created it

has memory dress location 2 for example

and then it's used within here and then

two is now deleted because you've got

rid of the variable because of this

function but one is still alive since in

the main function when you pass by

reference what you're doing is you're

saying give me the memory address

location of the variable inside the

brackets and give it into this function

rather than copying the value so if this

has memory just location one and you

pass it into here then it's also going

to have one so what this actually means

is if we put this ref keyword just

before the int just like we do with the

out and we can place it here as well

we're now saying we want to pass this by

reference so when this 10 gets pass in

here we actually have a link if you will

to this gome is now linked to this gome

which means if we simply do nor equals

20 and run the code then we're going to

see 20 the main differences between

reference and using the out keyword is

when you're using out within a function

you actually need to make use of the

value if I don't include anything within

here then I'm going to get exception and

it says the out parameter num must be

assigned so when using the out keyword

num has to be assigned when using the

ref keyword you don't have to make any

assignment because we're just passing a

reference in with the out keyword word

you literally tell the function I want

to Output this value but if you haven't

gave it a value to Output then of course

it's going to be an error so we can

assign in the reference if we really

want to but you don't have to and it's

not required so there are the two main

differences between passing by reference

and passing by value and also using the

out parameter passing by reference is

technically quicker because of

efficiency when you don't pass it by

reference you pass it by value which

means it'll essentially place that 10

inside these

brackets instead of passing the variable

in it actually just passes the value and

then it makes a whole new variable down

here when you pass it in by reference

you're essentially actually giving it a

link to this memory address location

from inside this function which means

that you don't actually have another

local variable inside here because this

one is affecting this one directly you

can do this with any amount of

references you want by simply just

chaining them and adding another one in

like that for example and then we can

have a string name and then we can pass

our ref name inside here and inside here

we can say name equals hour and this

doesn't like it if it's unassigned so we

just give it empty quotes or we can sign

it another name it doesn't really matter

it's just a demonstration and if you run

that but let's just print out the name

as well to the

screen there we go we get abber assigned

into

there and same thing with this variable

is we're actually just passing in the

variable as a reference rather than as a

value it doesn't matter what the value

is going to be B cuz it's just given

this variable inside this function so

we're actually saying name equals we

basically just assign it back into the

original variable so let's just have a

look at another example so we can have

some sort of like change name function

and we can give it the name variable as

a reference and then we can give it the

new name that we're going to pass in in

a second so if we make a new function

static void change name you're going to

need a ref string name and a string new

name and we can simply say in here name

equals new

name perfect

so let's give this a shot so if we just

comment out this little section and we

can say console right enter your new

name then we have a string new name

equals console.

readline and then just up here as we've

done before we could just declare the

name as being Joe and then we enter the

new name call this function and then

after we can say your new name is and

then we can pass in name perfect let's

run

that so at this point the name name is

actually Joe cuz we've made it appear

and then we'll enter a new name so we

can type in abber now it says your new

name is abber perfect so all this

function is doing I know it's pretty

pointless right now cuz it's just doing

one line but all it's doing is assigning

the reference to the variable that we

pass in so what we can do now is make

sure that we can actually assign this

variable in and we can also pass in a

Boolean variable so what we should do

now is actually make sure that we can do

the assignment properly cuz you don't

want to assign it if the value is empty

and then return back a Bo Lan type just

to indicate whether or not it's being

assigned so what we can do in here is

just on the change name we can wrap this

around an if statement and if this

returns back true then your name has

been changed and if not we can say

console right Line new name cannot be

empty or

no in order to be able to do this check

as we've used before we could say if new

name and then just around that we can

say string do is n or empty and wrap

that inside the brackets and we want to

say if it's not and the L empty then we

want to do our

assignment after we've done our

assignment we can return back true

inside here and then just outside we can

return back false notice how in this

little bit of code we actually don't

have an else we don't need to write else

inside here and place return false

inside here because if this returns back

true then it's going to execute this one

line return back true and since it's a

return it's going to jump to the end of

the function so we don't need the else

in there because the only way that this

line will ever execute is if we actually

had this function returning back false

so if this returns back true it's going

to hit this line hit the return jump to

the end if this returns back false it's

going to skip over the if statement and

then just return back false to the user

perfect so now we've got no compile

errors let's give it a run so if we want

to change your name to nothing if we

just press enter then we can see it says

new name cannot be empty or no and if we

type something in then we get our name

changed so just to round up this video

let's talk about the main differences

between the ref parameter and the out

parameter when you sign in by reference

you actually have to make the variable

before you pass it in for example you

can't just write ref string in here and

that actually works whereas you can't

actually do that with the in let me

demonstrate so if you had the in. trass

and that has an out parameter you can

actually type out int index inside here

you don't actually have to make the int

index inside here and then specify it

outside you could just initialize and

declare the variable straight inside the

brackets whereas because you're passing

a reference with the string you actually

need to make the string first and then

pass it in so that's a big difference

between the out and the ref when you

make news of the actually inside the

function if you're using a reference

then you never have to assign it or at

least it's not required you could have a

series of if statements inside this body

and then you might determine that you

might not need to assign it which is

exactly what we've got here the only way

we assign name is if this returns back

true if it doesn't return back true then

we simply don't assign it and move to

here this is a problem if the out

variable if we change this to an out

then we're going to get a compiler error

because it doesn't return back a value

so we'd have to write something like

this at the top name equals something

and then potentially sign it here and

then return back false so with reference

you don't have to worry about the fact

that you might not assign in every

single path of your code and finally the

main difference is the fact that out

parameters are still used by value and

this is by reference reference will be

slightly quicker since you'll get the

memory just location being passed in

instead of the Val we won't have two

copies of the same

variable the focus of this task is to

create a function that calculates the

area of a triangle so we're going to ask

the user for width and height install

them both create a function to calculate

the area the function is going to

calculate it using width times by height

taking that calculation and dividing the

value by two call it in the main and

print out the area of the triangle to

the screen so let's start with entering

the width and the height so we can say

enter width colon

and have int width equals convert to

in32 console.

readline and if you copy these

down change width to height and change

WID here to height and just before we

continue because we did copy and paste

down then this is quite bad and we

should use a function here so in both of

the cases we're going to output

something to the console and then read

it in and convert it back as an integer

so we can say static int because we

return back an integer so we can store

it we can say read int and we want a

string message so we can pass in this

value inside

here so we take these two lines and

place inside here instead of enter

height we're now going to have message

instead of int height we're just going

to have a return value and then we'll

return in the conversion from the

console read line and now we can use it

inside here read in inside the brackets

you want to put the string output which

is going to be enter height if we paste

that inside here and then do the same

thing for the previous section and and

change height to width and now get rid

of these so we just have these two lines

perfect so let's just print these out to

the screen on separate lines just to

make sure it's

working so the width of 10 and height of

15 we get 10 and 15 Perfect Since that's

working okay we can now create a

function that can calculate the area for

us so we can get static int calc area

and we can have int width and int

height and then taking this formula we

can simply just do return bracket width

times by height / two as you can see

this is literally exactly the same as

what we've got here it's crucial to

include the brackets because we want to

do this calculation first take the value

of that and divide it by two if this is

too many steps you can actually break it

down we can say int calculation is equal

to the width times by the height and

then we can simply return calculation /

two back to the function it's slightly

gred out because we haven't used use it

but that's not a problem we're going to

use it now I personally just to prefer

is to keep that inside here and just

have the brackets since a bit more

obvious to me what's going on but it's

up to you as the programmer either way

it makes no difference it's just more

for the programmer's convenience so now

we need to call the function above so we

can say int result equals calc area and

then pass in the width and pass in the

height and then we can have console

right line that just says the area is

and then result and then let's run it

you have 10 and 15 and there we go we

get the area area is

75 perfect if you didn't want to call

the function inside here and stall it

inside a variable you could just output

this straight to the screen by placing

the function call straight in the cly

braces and if you run that again just to

make sure 105 and we get 75 again

perfect so just a quick little recap we

have the console right plus the convert

inside this function which will let us

return as back an integer and we can

pass in the message straight in the

variable to make this a little bit

quicker and a bit easier you can

actually just change this one word

inside here so you could actually say

enter message call on space and then

just inside here you can say height and

just inside this one you can say width

just to reduce on code because in this

specific case the only thing that

changes between them two string

variables was the fact that one was

width and one's

height there we go you can see it's

exactly the same now so after we've read

in these two variables we pass it into

the function and all the function will

do is take width and height times them

both together take that value and divide

it by two and return it back to the user

and then we can call it straight in here

the area is calc area and pass in width

and

height in this task we're going to

create a function that will return us

back with the sum of numbers inside an

integer array so first step is to create

an initialize an array of numbers create

the function with the int return type

and it also takes a int array as a

parameter the function should return a

total of all the numbers inside the

interray call it in the main output the

total and just for extra bonus points

inside the function instead of using a

for Loop immediately you can check if

the array length is greater than zero

and then return minus one if it's empty

and then check that inside the main and

then output a specific message and also

see if you can think of any other

implementations you can use instead of

having to do return minus one how else

can you make this function whether that

means you have to change the parameters

or return type or anything like that

just have a think about the situation

and see what other options you can make

of course the output at the end is still

going to be the same but it's the way

you do it and the problem solving skills

that I'm trying to get you to get used

to let's get started so the create

initialize an array of numbers so you

have int numbers equals new int and

let's just allocate them manually 1 2 3

4 5 and add a semicolon there we go now

let's create the function sum of

numbers and we want it to return an

integer back with the total and we want

to take an interray called

numbers and then inside here we need to

actually return back an INT so we can

create an in total make that equal to Z

and at the end we can return total and

then during the middle we're going to

have to write our for Loop so we can go

through the integer array and count

through everything so we can use a for

each V item inside our numbers and just

inside here we can say total plus equals

item perfect so if we can call it in

here we can have a console right line

and we can just say sum of numbers and

pass our numbers array in the parameters

so we're just going to Output it to the

screen just for the time

being so sum of values from zero all the

way till 5 is 15

perfect so now that we've done that

we've created a function that Returns

the total numbers call it in the main

I'll put the total so we can just say

the

total the total is and then wrap this

around in the C

brace there we go the total is

15 now with the extra points we can

check the array length and return minus

one if the array is empty so let's see

what we can do in here before we even

end up going inside anything to do with

the total we can wrap this around in if

statement that says if the numbers do

length is greater than zero then we can

proceed with this little section so if

we highlight all of this hold alt and

press upar key then because it's one

back just press tab once and just press

the delete key here and let's get rid of

the empty Line This is going to return

us back with an error because not all

code path to return a value if this

returns back true then we'll eventually

hit a return but if this returns back

false then we not do anything inside

here so let's have a look at our

requirements return minus one if the

array is empty this dictates whether the

ray is empty so if this returns back

false then it's going to come into here

and we can say return minus one so we've

got this function call in here we don't

want to print the total right now

because we want to make sure that this

total is actually greater than zero

because we don't want to make a sum of

the numbers if the array is empty so if

you run this right now going to get the

total is 15 and if we get rid of all

these variables and just have the length

of five but not give it any value so all

the values are going to be zero then if

we still run that then the total is

going to be zero but that's still an

acceptable value because that's actually

got values

inside but if you said the array length

is zero then the total is minus one of

course we don't want the user to see

minus one so we want to bring this back

and instead of calling the function

immediately down here let's call it here

we can say int result is equal to this

and then we can say if the result is

greater than minus one then we can

output the total and again press into

this line hold alt and press Arrow key

and then we can place the result inside

here and the L's to this will be canot

add up an empty array there we go so now

if you run it we should still see 15 but

if we get rid of this and put it back to

zero there we go we cannot add an empty

array so we bring that back perfect so

now if we check this we've got check in

the main output the message and then now

we've got this little solution do we

need to return minus one how else can we

make this so right now we're going to

have to return back minus one cuz that's

the only way that we can tell the user

whether or not it's been successful if

the r is empty we return minus one and

if the ray is not empty then we return

something greater than minus one which

will be something above zero or zero

itself but as long as it's greater than

minus one that's how we know it's

complete but we don't actually want to

do this we want to be ideally returning

back a Boolean so let's duplicate this

function and show you both

implementations let's change this into a

Bo

and then because we're still going to

need to reference the total we're going

to have an out in total so we need to

change up a few things inside here

instead of this return minus one we can

do return false to indicate that it

hasn't been successful instead of

returning back total we can actually

just return back true now we haven't

done everything yet we don't need to

redeclare in total inside here because

we already declared it inside here so we

could just initialize total to equal to

zero but the problem here is if we make

total equal to zero just within this

path

then it's still going to complain that

total must be assigned so we can take

this and just assign it right at the

start of the function just like that

there we go so we make total equal to

zero that gets rid of the compil errors

and gives it a value the reason why we

have to give it a value is because this

out in parameter doesn't like it if you

don't assign the value that is a

requirement for using the out parameter

so we make total equal to zero check the

numbers length If This Were to return

back false then we return false to

indicate that we can't do anything if it

this returns back true it will go

through and count all of the items place

inside total and then return back true

so let's have a look and see what else

we can write this little section of code

so instead of having to call inside here

and use it within there then we can

actually do this another way the reason

why we actually calling this straight

inside here is because we can't place

this straight into here since it returns

us back with an integer and now that is

actually fine if that returns back of

the integer cuz as you can see there's

no problems here the issue arises is

when we have our result variable we can

use it inside the bracket and down here

there's no point in calling this twice

to get the same value although this

would still technically work but this is

not really efficient since you're

calling the function twice it's best to

call it

once store the value and then make use

of it twice when we use our out

parameter you'll see that this gets a

bit easier so straight inside the if

statement we can say sum of numbers and

then we can pass in our numbers and then

space out into total and what that'll do

now is because this returns us back with

the fol we don't need any additional

conditions like we have one here cuz

this will turn us back with a true or

false true if it succeeds false if it

doesn't so we don't need to say if it's

greater than minus one because we don't

actually care about the value of the

total right now we just care if this

returns back with true or false so

inside here we can take this same output

message and instead of having result

here we can have

total and then we can take this exact L

statement and place it here and let's

see what happens now the total is 15 and

the total is 15 so they both turning

back the same values let's add something

else on here just to make sure it works

there we go so you can see that both

these Solutions look very identical but

one is considerably different we have to

make a variable outside use inside here

and use inside here whereas in this

function we can actually just call the

function straight inside that will

process our new out into total and then

we can simply use total inside here and

we don't have to require another

additional variable just outside here as

this one will and you can see that the

functions is a little bit better because

we're returning true and returning false

to indicate whether or not it's been

successful whereas in this solution we

have to return back an arbitrary value

and then check that in the console the

reason with returning minus one is kind

of a problem is what if these numbers

are actually all negative then this

value is going to be very bad this can

only really work if all of these values

are positive if you had negatives for

all of these and we tried running the

code there you go this is a major issue

cannot add up an empty array well it's

not the fact that it is empty it's the

fact that these values are smaller than

minus one so you don't actually want to

be going with something like this

approach where you're comparing the

value because you don't know what

numbers are going to be typed into here

if the user were type in positive or

negative you want to provide a solution

just like this one that works for either

positive or negative you don't want to

restrict that by checking if it's

greater than minus one will only works

if all of these are positive values all

this is an index for the array since

arrays start at zero and go upwards you

can't have a negative -1 array position

but in terms of numbers that are

randomly typed inside of a ray you

should always be wary of using this

option because if some of them are

negative then this could return back

false so let's just have a quick recap

so we have an integer array we have our

values initialized down here and then

the first solution we call the function

passing the numbers array get our result

back check if it's greater than minus

one then that means it's been successful

so we print it out and else we tell it

it's an empty array now second option we

actually call the function because it

returns back a boly in this case so we

can place it straight inside the if

statement and then we can pass in the

numbers AR and then we have an out in

total which will give us the total and

in this case we don't need to check the

value because this returns us back with

the true or false and in this case true

determines whether it's been successful

and false it doesn't if it returns back

true then it has some values inside the

numbers array that we can actually add

up and if it returns back false then

that means the array is empty so let's

have a look at the implementations so

without the out parameters this is the

first solution up here we get the

numbers length check if it's greater

than zero if it's not we return minus

one make a new variable called total

make equal to zero and we run a for each

Loop V item inside the numbers and we

just say total plus equals item add them

all up in the for Loop and simply return

the total and then in the out solution

we have a total equals zero which just

initializes this variable otherwise

we'll get a compiler then we can say if

the numbers is greater than zero then we

can say if numbers. length is greater

than zero then we can do the for Loop

and return back true and if it turn back

false then we can say false inside here

and then when we use it straight in the

brackets we have access to it straight

away since it uses a Boolean variable

and then our value will be placed inside

our out into total parameter which is

used down here for the output to the

screen and again if this returns back

false then it will say I cannot add up

an empty rray and then we hit the

console read line and we at the the end

there we go that covers everything

inside

here exceptions are used to prevent your

code from crashing and then the program

closes down let's have a look at a very

very common example let's have a console

right and we can say enter a number and

then we can use int Norm equals convert

to int32 and then we can have a console.

readline inside here and let's just

print out number to the screen now if we

assume everything goes well and we

actually enter an integer like 15 and we

press enter then we simply get 15

returned and as we saw in the past when

we covered the trip pass function if you

enter anything else that's not exactly a

number then you get again exception

exception unhandled unhandled means that

we haven't wrote anything specific that

will handle this exception so it's

causing the program to close it's a

system. format exception it says the

input string was not in a correct format

so this is a format exception because

we've entered in a very weird set of

characters that can't be converted over

to an integer so now our program is

forced to crash and now rest of our code

is going to be deleted now because it

won't be able to run so if you had any

important data that was stored just

before this point then it is now lost

this is very crucial because if you have

a system that rides to a database or it

stores some very important information

that someone's typed in you want to

handle the exceptions so if something

were to arise it doesn't actually close

the program down and they lose all of

their data I'm sure you've had it in the

past where you've tried to do something

and the program comes up Windows and it

says this program had to stop responding

when that normally comes up it means

that Something's Happened that's so

fatal that the program cannot continue

it has to crash down and in most cases

this is what's called an exception so if

we run this code again let's have a look

and see what else we can do to make this

code break so of course as you heard

before a gnome integer can only survive

up to about 2 billion numbers so if you

enter a number bigger than that let's

see what happens then we get an overflow

exception value is either too large or

too small for an in3 too of course this

number is very big and only a long would

handle something like this an integer

definitely won't so these are the kind

of exceptions where you'd have to handle

it yourself manually otherwise your

program's going to crash coming up next

we're going to go through the ways that

we can handle these and make sure

programs don't come to a halt exactly

like these

examples I want to explore a technique

called trycatch this is very similar to

when we use the int. trass what this

essentially does does is it tries to do

something and it lets you know if

something went wrong this is exactly the

same when we're dealing with exceptions

except when we're doing a try catch what

the catch means is it's catching the

exceptions that happen and therefore it

is now a handled exception instead of

unhandled exception so before we try and

fix how this works let's just explore

the syntax so we can trpe in try Follow

by curly brace and just after this curly

brace we can say catch and then we're

going to put something inside the

brackets and then we have it opens

brackets if you wanted a quick shortcut

to this just like we've done the for

Loop and we double tap tab we can

actually delete this so you can type in

try and press tab twice and then you get

try the Open brackets you get the

exception and then we get a throw so

let's get rid of this and what we can do

right now is we can actually place the

code that we want inside here inside

this Tri bit because we're telling the

compiler that we want to try to do the

things inside here and then we want to

catch these things so inside these

brackets what you want to actually do is

decide what thing that you want to catch

if you type in exception this will catch

every single exception now the problem

with that is you'll actually face some

difficulties when you're trying to

Output very very specific messages so as

we've said before when you type in a

value too large you get an overflow

exception but when you type a value that

can't be converted like a square bracket

then you get an input format exception

so first what you actually want to do is

run this code outside the try and let's

run it now and let's get them bad values

so we had the square brackets and we can

get this and let's copy it down and

place it inside a comment just like this

and then let's rerun

it and if you type a number too big take

this stop the program and put it inside

here so now that you know what the

exceptions are called we can actually

write these inside the code so we have a

form exception and an overflow exception

but before we get into these specific

exceptions let's just catch everything

this is just a catch all and then see

how this

works so as we've put it back inside

here let's see what actually happens so

we can just put a console right on here

and say something has went

wrong the reason why it's something has

went wrong is because this is a catch

all any exception appears during this

code it will always come into here

regardless of what the exception is

whether it's a format or an overflow it

doesn't matter it will always come into

here so it's going to be something

because this is not specific at all so

let's run the code enter a very big

number something has went wrong but if

you notice how the program doesn't

actually clash it's still very much

alive and we can test this by putting a

simple message here at the end that says

goodbye and we run it again enter a big

number and there you go we get the

message as goodbye so it successfully

skipped over this section without

actually causing an exception that

breaks the program so even though we've

entered something very fertile because

we've handled it then there's no

problems so if we now type the other one

where we have the square brackets then

you'll see we'll also get something has

went

wrong so if you wanted to catch a very

specific exception what we can do is say

catch open the brackets and we can say

format exception just like it says above

here and then place the curry braces and

what you can do now is you can actually

chain these and these go from top to

bottom when an exception occurs up here

it will match this one if if it doesn't

match it will match this one and you

want to put the catch all right at the

end so if it doesn't match any of the

ones above it it will always match the

last one so you can have a generic

message inside here but you can have a

very specific message inside here

because it's a format exception you can

say please only enter numbers so now if

you run the code and if we type in

square brackets it'll say please only

enter numbers there we go so this is a

very specific example and if we copy and

paste this down again and then we can

say overflow exception

and we can say please enter a number

less than 2

billion if you enter a very big number

again please enter a number that's less

than 2 billion

perfect so this will get checked like if

statement it will start on the first one

and go down the list imagine this is a

if and then we have an else if and then

else if as well if you place this one

right at the start then you're actually

going to get compiled errors and you

can't actually have the catch all right

at the start so even if you wanted to

the compiler literally doesn't let you

and anyway you don't really want to be

doing that because then you'll be ruling

out these two and it's kind of pointless

writing this code if it's never going to

get used anyway so now when we run the

code you can see even if we typ in a bad

value our program will still run and

everything works okay so now what we can

do is actually make this into a real

life

situation so if we actually type in 10.2

then it says please only enter numbers

then we're going to have an issue here

here because if we try and print the

value down here then it's going to cause

a problem let's try and print out num

and you can see that num does not exist

now we have an issue of scope in is only

declared within here and here so if you

wanted to print out the value of gome

we'd have to print it out up here so now

let's run it again and if we enter 20 20

comes up if we've entered 15.5 then it

actually skips over this console right

line because the exception occurs right

here and then it'll jump into one of

these paths so in order to store the

value of num you can either the move num

outside here and make it equal to zero

and then assign num inside here and then

you can take this console right line and

actually put it right after the triy

pass but I don't really suggest this

option so if you said 15.5 then you're

just going to get zero returned but we

don't really care about the zero because

we're not going to get the value

converted so why do we need to print the

zero and the answer is we don't so what

we should do is keep it all inside here

again and then what we should do instead

is wrap this around in a while loop so

this will try to do the code this

section will catch them all and then we

simply have a good buy at the end but

let's just make sure that this actually

runs because if we type in

15.5 then the user still hasn't entered

the number but our program was closed so

we want to loop around until the user

has successfully entered a correct

number so we can say bull looping equals

true and we can have while the cord is

looping that off the curly braces let's

place our entire Tri cat inside here if

we highlight it all hold alt and press

the op power key and then tab it once

and then get rid of this empty line so

now we'll keep looping around here until

this is going to be a different value so

because the while loop will only keep

looping until this is true and the

moment when it's false is when it will

cancel the W Loop so as I said before

when we run this code and then just at

this specific line if the exception

occurs it will actually skip this line

and jumped below here so if it skip this

line then what we can actually do is we

know if it reaches this line then the

conversion has been successful so we can

print out to the screen and we can say

looping equals false and we don't need

to place looping is equal to True inside

all of these three paths because if we

don't assign it to true then it's still

going to be the value false no matter

how many times you loop around so let's

try and run that

now enter a number 15.5 enter a number

15

and we can keep doing this forever as

it's a while loop and as soon as we

enter a valid character 20 and now it

says goodbye because we hit the end of

the

program so that's it for this video but

let's just have a little recap so we've

determined when we run our code just

these two lines that in both situations

we can have a format exception and an

overflow exception we can use these to

catch them specific exceptions and then

have a catch all at the end the reason

why you want to catch multiple is you

can have a very specific message

overflow dictates we have a number

that's too big or too small to fit

inside of an integer it can be less than

negative2 billion or can be greater than

2 billion and that's how you get the

Overflow exception we get him a format

exception if you entered something

that's literally not a number or

something that has decimal places and

then our catch all can be if anything

else goes wrong we can output a generic

message by 99% of the cases you'll

either enter number too big or too small

or it in the wrong format and this AC

ction is just so it catches everything

else should anything else happen so at

this point even though our program

doesn't actually close and there's no

exceptions the user hasn't had a chance

to enter the number again so we use this

inside a while loop and we can say while

looping is true when we hit a conversion

that's successful we say looping is

equal to false and once it hits this

line the Y Loop will then exit and then

we'll get our goodbye message and we

wrap the entire try catch inside the W

Loop just so this keeps on looping until

the user has typed in a successful value

as demonstrated

above we can type all this and 15.2 15

Point 15 and then finally says 15 and

then

goodbye so I want to explore different

ways you can output messages inside this

catch block when you have a catch

exception this is actually a parameter

that we're passing in this parameter can

actually have a variable as well

normally when you specify parameters in

a function form you say in and then some

something whereas right now we're kind

of defining the data type which is our

exception in the same case as int here

but we're actually not giving it a name

so it looks something like this of

course when we do this inside a function

we're going to get a syntax error but

for a catch we're not going to get any

errors at all and the reason is because

we can catch without having to use a

local variable so if we actually add a

local variable make you call it e for

exception then we actually have lots of

options inside here Type e then we have

lots of data and the stack traces very

helpful when you debuging but we'll

cover that at later date the thing I

want to focus on right now is the

message so we can use this and print out

to the screen so we can say error callon

and then type in E message and let let

run that now if you enter something

invalid input string was not in a

correct format if you enter a very big

number then values either too large or

too small for an in32 as you can see

these error messag are pretty

descriptive and normal users should be

able to read this and understand it of

course if you want this error message to

be more specific then you can add some

more catches inside here and we can have

format

exception this will be if you type in

something other than number but if you

do exactly the same thing in here then

it's kind of redundant because you're

just outputting exactly the same value

if we try and put the square brackets

again then we're going to get this

string which is actually outputed from

this line but even if we include the

catch all exception you get exactly the

same error message outputed from here

the moral of the story is you should

always have a catch all right at the end

of your chain but if the only thing

you're going to do inside here is simply

output this message to the screen then

you don't need any multiple catches what

you should do is catch multiple

exceptions that you know are going to

get fired in your code and then you want

to handle them and then have a catch all

at the bottom that will display the

message generically instead of

displaying the message right here you

can actually have a more specific

message like we covered in the last

video so this could be please only enter

numbers and we don't actually need the

message in this case because we know

what exception we're going to catch

because we've specified formal exception

another little tip if you hover over the

function call it says here format

exception and overfall exception which

are the two that we handled in the

previous video this is very easy and

understandable if you can just read the

values from here and to see what

exceptions can get raised and the same

thing here you get an IO exception for

the console right line the console right

can also give back an IO exception as

well generally speaking these console

functions almost never return back

exceptions I don't think I've ever hit a

situation where this exception has been

returned unless you do something

ridiculous and maybe you type in way too

many values that the console right line

can handle let's try an example and we

can do something like this if you enter

a very big number that can't be

specified inside the integer then

console right line has so many overloads

that actually processes it as a long

right now so you almost can't break this

unless something fundamental to your

computer actually happens so you have

exception e and with exception e you can

actually take this local variable and

print out the message this is the

preferred pattern because instead of

outputting here something went wrong

just very generically you can actually

give them the error message that comes

back from the exception the reason why

this is helpful is because if it doesn't

hit any of these exceptions and it just

happens to fall into the catch all at

least the user has an idea of what the

error message is going to be the

preferred option is to try get to a

point where you never have to reach the

catch all and try chain as many catches

as you can in order to handle as many

exceptions based on the code that you're

writing which can be found if you hover

over the function and read the

exceptions once you've covered as many

options as you can throughout the

catches then you almost never have to

worry about the code entering this

section because we've covered both the

exceptions in format and overflow and we

can do the Overflow right now please

enter a value under 2

billion then that means we've actually

covered all of the catches that are to

do with converting an integer 32 covered

format and covered overflow so when we

run this code the only two things that

can possibly go wrong are these two

functions the format exception overflow

exception and we won't ever hit the

catch all in this case but if anything

goes wrong at least we have a belt and

Brace option to catch it and we can tell

the user what happened and because it's

e. message then the value is going to be

quite descriptive so the user can read

it without any

problems so in this task we want to be

able to convert a string to an INT

successfully without any errors

occurring and without any exceptions

that could break our code so in steps we

want to create an INT and try and

convert any string from the console to

an integer notice the error that appears

when you try to do that write a try

catch handle around it to successfully

operate that and output the message to

the screen and you won't need to change

the console out print and the convert

in32 to be able to make this work so you

don't want to change the current code

you just want to add the triun Handler

around it so why is a bad situation how

can we know if it's been converted this

is just a question that you should keep

at the back of your head and we'll

answer this shortly and then finally we

want to create a custom tri pass

function find the real trip pass

function and copy the return Titan

parameters and see if you can mimic Its

Behavior read the tool tip it gives you

to give you ideas on what to do so let's

take these in steps first we can have a

console right and say enter a number and

then Inn equals convert to in32 and then

we need a console read line

and then if you run the

code if we try and just convert some

square brackets then we get a format

exception so that's the exception that

we're going to be handling right now so

we can take this code and we can wrap it

around and a

try use alt and up Arrow key and then

just after do a catch and then we want

to do a format exception and then space

e to give it a variable and then we do

e. message out to the screen and now

let's run the code square brackets input

string was not in a correct format so we

get our message and our exception has

been successfully handled and our

program hasn't crutched but the problem

is is has got to this console read line

and the user hasn't had a chance to

actually enter a number so what we can

do in here to ensure the user always has

access to writing a number is we can say

something like this bu success equals

false and then just after here we can

say success equals the

true and just down here we can say

console right line so success question

mark yay and then call on or

no so if it converts it 15 we should get

a yay and if we convert it and it fails

then we should get an o no along with

the

message of course what you can also do

is wrap this entire section inside a

while loop what we've already covered

that inside the tutorial and that means

it'll keep repeating this until this is

set to true and therefore exit the while

loop and then will the users finally

typed in a number so let's go back so

we've noticed an error and we've brought

a TR catch handle around it now we want

to catch the error and output the error

message with through done and then we

haven't changed the current code which

was these two lines we've added a booing

and set it to True here just so we can

have a bit of an output message before

so why is this a bad situation how can

we know if it's being converted so this

code is a bad situation because we just

have these two lines outputed and then

now that we've added our booing to tell

the user whether it's been successful or

not is actually making it a bit more

meaningful the problem is even if we

print or no then the user doesn't really

have a chance to enter it again so like

I said before you could loop around and

use a while loop to make sure that this

situation can go from something that

gets handled into a situation where the

user can actually type it in there's no

point hand on an exception and realizing

there's an error if you don't give the

user a chance to enter another value if

they enter out 15 followed by full stop

when they only meant to type 15 then you

need to give them another chance to type

in that 15 value otherwise the program

is simply just going to end and

nothing's going to happen for example 15

DOT it says or no or you can have

another message that says something went

wrong or you can have more specific

message about the conversion and then

once you press enter the program simply

closes this is not an interaction that

you want you want the US it to be able

to reselect another value so that's why

it's a bad situation because you know

it's being converted or not heuse now

build success but the problem is we

don't have any sort of looping so let's

have a look so now let's comment out

this and let's see how we can make our

custom trip pass so you type it in. trip

pass you can see they have a bu inpass

string s and out in result so let's try

and copy that and make our own custom

function so we say static Bull and it's

Tri pass and then we have a string input

and out in

result perfect so now we can get rid of

this line just so we use it for

reference and let's use it straight in

here and then we can write the

implementation in a second so we can say

if trass the thing that we want to pass

is actually a console read line and then

we just say out into

result and the console read line is

going to be answered to this question

which is enter a number you place that

straight inside here and then we say out

in result so if this has been successful

we can print out yay again and else if

it's not successful we can print out or

not of course you can do other things

inside here or you can even print out

the value straight from here so we can

say yay plus a space and then result

just so we can get that back to the user

and then oh know if something goes wrong

of course this is just so we can test

things and see how they work so this Tri

pass function it will take in a string

input and it'll try and give us back the

result so in order to be able to make

this successful function what we need to

do is essentially do the conversion

ourself which will be this convert in 32

so first we need to assign this out int

so we can say a result equals minus1 so

now that we've used that we actually

need to have a return type as well but

let's worry about that in a second so

essentially what we actually want to do

is say that the result is equal to

convert to in 32 and then we want to

convert the input store it inside the

result and if this goes successful then

we want to return true and if it doesn't

go successful then we want to return

false but of course we need to be able

to do these things separately so like we

said with the try you only wrap around

the section of which is going to be

problematic so in this case as we've

done here we've wrapped the convert in

32 inside the try so we're going to need

to do that down here as well so we can

open the try right here and place these

two lines

inside and then have a catch have a

little think about what sort of catch do

we need the whole point in this function

is to Simply convert value and return

back whether it's been successful or not

there is no messages here we just need

has it been successful yes or no and

because we only need to know if it's a

yes or a no then we can just have a

generic exception and we don't need the

E variable because we're not going to

access the message we're going to keep

it as an exception and instead of

returning false down here we actually

want to do this straight inside the

catch so now the code reads like this

we set the result to minus one and the

reason why we do that is because if we

don't then we're going to get a compiler

error and it says that not all paths

will going to assign result

alternatively we can also assign result

straight inside here just so we have an

assignment in both paths or you could

just leave it outside at the top so we

assign result to minus one and then we

place our problematic code straight

inside here we try and convert the

result and if this line returns back

successful then it's going to execute

the second line and return back true if

this line returns back an exception it's

going to miss this line completely and

jump straight into the exception and

then return us back with false so let's

actually try this and see what happens

15 DOT or no and then 15 y 15 so the

reason why we don't care about the

exception in here we can add in

different values as we've used before

with format exception overflow exception

but as we saw with trass it simply just

returns us back with the bull the bull

simply tells us if it's been successful

or not so we could stack these multiple

times times we can say catch and then we

have the format exception and then we

can place a return false in there and

then we have another catch for the

Overflow exception and we can place

another return false in there but as you

can see we're just returning false

inside here so now that we've changed

these it's kind of pointless each of

them return back false and because we're

not doing anything very specific we

don't actually need to chain them all

together we don't care about the message

we don't care about exception we only

care about the fact that something has

went wrong and if something has went

wrong then we return back false if

everything goes okay then we return back

true and that's all this Tri pass

function is going to need to do it's

simply convert it or it doesn't convert

it using an input and outputting a

result so if you run it retype in a real

number we get a number back and then 15

DOT Z then we get all no because it

can't convert it there we go we've met

all our requirements for this

program let's have a look at how we can

do debugging using visual studio to to

step through our code manually so this

is the example I used last time int age

equal 35 and ideally in this case you'd

want something like 30 plus to be

printed instead of 18 plus even though

18 plus is still technically true we'd

still like 30 to be printed in this case

because this is if it's over 18 do 18 if

it's over 30 do 30 plus so in order to

start the debugging process you

shouldn't run the code first if you run

the code first it's going to execute so

fast that you won't get a chance to

actually put any code in and and try and

debug it there we go we've already hit

the console read line and it's executed

for a spit second so the best thing to

do just to the left of all the line

numbers you want to press in the Gap and

that will create a red dot and it'll

tell you that this is a debuging red dot

just in this little tool tip debugging

do program. men debuging program. men so

it tells us we're in the program class

and we'll going inside the main you can

also address the debug menu up here and

it says we can have a toggle breakpoint

we can add new one we can delete them

all or we can actually disable deleting

them will just simply get rid of them as

you can see here if I set a couple more

and press disable then it's going to

hide them so when I run our code it

doesn't actually apply the debugging but

it doesn't delete them and then we can

enable them and then we can also delete

them all and it gives you a little

prompt because if you're doing this

inside a big project and you just spent

a lot of time going through and

debugging certain functions then you

don't want to be able to delete them all

without any confirmation so to delete

them all and put it back on line 13

which is our int Edge so let's run the

code and see what happens so as you can

see it minimizes our console for us and

nothing's actually been printed yet

because we're only up to this line and

then we can use these buttons up here

step into goes inside a function step

over goes over the line so it will go

from this line to this line step out is

used after you've stepped in if you

stepped into a function and then you

don't want to see the rest of the

function executing you can step out to

jump back out of it which I'll

demonstrate later on in this video so

now that we've hit this line you can

actually see what's going on so we have

int age equals z because when this line

is highlighted it means this line hasn't

executed just yet so we can press this

or we can press F10 on our keyboard if I

press F10 you can now see int age has

the value 35 and what you can actually

do is double click on it and just change

the value to 30 for example now if you

highlight you can see the value is 30 so

you need to double click change the

value to something and then simply press

enter if you change the value but leave

the box like this then it won't change

the value so you have to remember to

press enter so now I've made the age 40

but let's just change it back to 35 to

keep it the same and then now what we

can do we can explore what's going on

here if age is greater than 18 then

we're going to check if this condition

is true or false so in order to check if

this returns back true or false you can

actually hover over the operator and it

says here AG is greater than 18 is equal

to true and if we hover over this one it

says AG is greater than 30 is equal to

two notice what we actually did there we

essentially ran this condition through

the debugger without actually reaching

this line we're only up to line 15 as of

right now but we've managed to execute

this and give us back a function so as

you can see age greater than 30 is true

and age greater than 18 is true and

because this is lsif chain it only cares

about the first one that turns back true

and because this one has turned back

true then if you press F10 again you'll

see that now it's went inside this curly

bra is which will print out the console

right line if I press F10 again you can

see we're now waiting on here if I use

alt tab I can get back to the console

you see that 18 plus has now been

printed now that we're waiting here we

actually press F10 one more time and

then it jump straight to this read line

of course because this is L if chain as

soon as one of these become true and it

jumps over the else if because of course

this is a chain the first one it hits

that's true it skips over the rest if we

didn't want this to be a chain then we

could keep them as both independent if

statements if age is greater than 18 and

if age is greater than 30 and don't use

the lse keyword but in this case because

you do want to Output a different

message for every single age range you

do want to place the L's word in there

and now that we've hit this line we can

press

F10 and then now we jump back in the

console and now we're waiting right here

and just a little tip when it comes to

debugging if at any point you just want

to exit the debuging you can either

press continue or just press F5 and the

program will run for example if we

investigate this variable and we can see

the value is 35 and then now we're happy

with the rest of the Coe we can just

press F5 and our code will run as normal

we'll get the out print and we'll get a

read line waiting here so we know our

problem here is both conditions return

back true so just like in the last video

we actually change this condition so to

make sure that it only goes between this

range but let's not repeat ourselves

let's have a look at a different example

so we can have string name equals Abu

and and then int age and then age equal

23 which I'll demonstrate why I've done

it like this in a second and then let's

have a function just so I can show you

how to step in so we can have a static

string print details string name in

Edge and here actually we want to just

return back a void because we just want

to print out to the screen and we can

say name space edge and maybe just put a

dash between them and let's run that

code now we can see

aa-23 of course making that function

wasn't the point I actually want to show

you what's going on so if we just print

this a couple of times just so we can

show you the step out and step in so if

you run the code now we can see our

string name if we go over that you can

see it's called abber and then now that

it's stepped over this line cuz there's

nothing really to execute here it's just

creating back a variable so you can see

that age is zero cuz that's the default

value for an integer and when we skip

over that now it's got the value of 23

so one line creates the integer with the

default value equal to zero and then the

next line will make it equal to 23 and

then we're passing on the variables here

and if you wanted to investigate it you

could simply just hover over them like

this so you can see what the values

contain and then now that we've hit this

function if we press F10 then it's just

going to skip over it but what we

actually want to do is be able to go

inside the function so this is where our

step in goes step over doesn't care if

it's a function or a normal line it will

just step over it regardless so we want

to in here we want to use step into or

we can press f11 once we press F 11 you

can see that now it's jumped from print

details up here to print details down

here and then now look if I step through

it's going to actually execute these one

by one so right now it hasn't executed

anything so I step over this once using

F10 you'll see it now appears and then

the same thing with this one and this

one and now that I've executed three we

should see three in the console perfect

so now what I want to demonstrate is

let's say what happy with the rest of

this function and then now we want to

just step out so I press this button and

you can see it jumps back back up to

print details f11 to go in and then

shift f11 to go out so now that we're

back in here the only thing we can do

really from this point is either press

F5 to stop debugging or press F10 to

jump to this line and at this point

we've already hit the last line now code

so we don't really need to use F10

anymore cuz we nothing to step over so

we can just press F5 and let our program

finish and there we go so let's have a

look another example can have a string

message equals read from console then we

can pass this in and

anything and then we can have a console

right line and we can just print the

message and then let's create this

function so because it's a return value

and start inside a string we're going to

need a static string read from console

and then this is one string parameter so

we have a string

message and then we can just do a

console

right and then print the message to the

screen and then we just want to return

the console. readline there we go and

again let's put a break point here and

we can remove this one so it doesn't

always have to be the first line if

we're perfectly happy with this amount

of code then we can simply take that off

and it will stop when it gets after this

line now if you look at the console

you'll see all of this is executed that

function is executed four times to print

the name and age of the screen and now

we're waiting here so we have our read

from console and now we can step into

using f11 and you can see that if you

hover over string m message has enter

anything which is our parameter that we

specified up here and then there you go

and then that will print into the screen

so you tab back in you'll see it says

enter anything and then just at the read

line we need to actually enter it

now

something and then press enter and then

just right here it's going to return it

back so if we use F10 it jumps back in

here and then actually we need to

execute this line so F10 again and then

if you hover over a message you see it

says something which is exactly what we

typed in the console here and then we

just print it back to the screen there

you go and now we just have the console

read line and we can press F5 to finish

up there we go so debugging is mainly

used by pressing this button on the side

so debugging is used by pressing on the

empty Gap just to the left of the line

number and it'll create a red dot once

you create a red dot it'll highlight

this for you once you've clicked on it

it should show up a red dot you can also

disable or delete all the bra points

from the debug window at the top of the

screen when you run the code it will

stop here and then you can either use F9

as a shortcut as well click into the

line that you want to debug and press F9

please note there is specific cases

where if you try and press this one

right here it's not going to let you if

the line doesn't really do anything for

example this is creating an integer

there's no real action that's happening

here that you should or want to debug

this is an assignment this is an

assignment this is a function call and

so on and so forth and this is also a

function call when it comes to just

declaring variables then a compiler does

not let you put in a break point because

there's nothing really to investigate

when you're just creating a variable so

don't get thrown off or think

something's wrong if you click on the

empty line and nothing appears so that's

it for debugging but let's just have a

little recap we can use F10 to step over

f11 to step into functions shift f11 to

step out or you can use the buttons that

appear at the top of the screen when you

run the code just right

here hover over variables and you get

their values as I showed you in the if

statements if you hover over the

operator inside the if statement it will

actually tell you what the condition is

and whether it's true or false so you

say if 1 equals equals to one and let's

just print out true to the screen just

so I can demonstrate

this if I hover over the equals equals

it says 1 equals equals 1 is

true so that's how you check if a

condition is going to be true or false

when you're

debugging you can check the parameter

values by simply hovering over it and

the tool tip should include the

value just like so and if you wanted to

step into this function you can use step

into and then use step over which is F10

just here and then you can actually

investigate these values as well or you

can look at the parameters and once

you're done with the function you can do

shift f11 or you can use step out and

then you can skip over this and the same

thing with this you can have a look at

the string variable once it gets

returned something in here and now it

says something thing in here and string

message and then we can print out to the

screen Etc so you can have a little bit

of Freedom with what you can do in terms

of debugging play around to debuging in

the past exercises that we've made

together and then what you can actually

do is gain experience in debugging so it

comes to more complicated projects you

can also apply that learning there and

make sure that your code is error

free so we covered how to debug by

pressing the white space just to the

left of the line number if we run the

code

the program will stop where our

breakpoint is and it won't execute the

line that we're up to we have to step

over it so we've actually used this

before to analyze our functions and see

what values are inside things so we have

these extra windows called the local and

the auto so let's take a look at them

and see what they're about so again if

you don't have this layout you can use

window reset layout and it should be on

the left panel not the right one this

time so we've got watch which we'll

explore later and we're going to have

locals and autos so the autos because

we're up to the age then it shows us the

age has value zero and then that

prevents us from hovering over it and if

we step over it you'll see that now it's

got age 23 and now name has appeared the

auto tab will try and figure out what

functions of what parameters you need to

be displayed down here based on the

section of Corde that you're currently

up to so my guess is because we're up to

this function and it takes in a name and

age then we're getting name and age

appearing before when we were on this

line it was only showing back the age

and because that section only used the

age we only got age here but if you

press locals you can see all the

variables that are local to this main

function are actually showing up we have

a parameter called ARs and technically

this is a parameter and it's a string

array type but it's currently unused but

it's still a parameter so you can see it

down here and then in order we have the

name variable down here then we've got

the age and then we've got the message

which is currently said to know because

we haven't created this in memory yet

but as you can see this changes depend

on the scope of our variables so now

that we're about to hit this function

let's use the step into and you'll see

that the locals have now changed we've

got rid of name age argument and string

message and it's replaced with name and

age because remember what I was saying

about the scope of the variables

parameters inside a function are always

going to be local to that function

around the curly braces that they're

surrounded by name and age is only

within here so when it's trying to shows

the local variables it's only local to

what's inside these curly braces which

in this case is name and age so now if

we step over

this and then if we step into this read

from console you'll see that just before

we step into this function that these

have all came back cuz again the scope

has changed back to this function which

is our main function which has all these

variables inside so our read from

console will only have our string

message so hopefully when we press f11

or press this step into button at the

top we should only see the message

variable inside here there we go we get

into anything cuz that's what was passed

in over here and then as soon as we

return back to

that let we type something in and then

we get all of the values again and you

can see that system. console. rine had

returned hello which is this line and

then if we press F10 again you'll see

that even though we've got it back here

our message is about to be assigned once

we step over this there you go it says

hello and as you can see this changes in

real time so as soon as we step over

lines hello got play straight into there

this helps us tremendously because we

don't have to keep hovering over

variables like this to keep checking

over their values you can simply use

this menu and it will always update

because whenever you change Scopes

between functions that will

automatically update as you just saw in

them examples there we go and then when

the program ends obviously this window

is going to close down so that's a very

helpful method of being able to debug

without having to hover over anything in

the next video we're going to cover how

to watch variables and this can be very

handy when you're looking for specific

areas

another really good debuging technique

is the watch window let's press a break

point just of left of line 13 and let's

run the code and let's see what this is

about so we covered that we have the

locals window and we have the auto

window that will automatically give us

the variables that we're currently in

the scope of but let's forget about that

right now let's have a look at the watch

window it says here add an item to watch

so there's a couple ways that we can do

this you can actually double click on a

variable and write click or you can just

highlight it like this and then right

click and it says here add watch and you

can press it it says name value null

because we haven't created it yet so the

default value is null as of right now

cuz it's not actually assigned anything

so if you press F10 and step over it

you'll see that immediately changes to

abber without was having to do anything

except simply press F10 which is the

shortcut for our step over button right

at the top of the screen this prevents

us from having to hold over the

variables to get the little tool tip so

we can see the value the of this is we

can watch variables and we don't have to

worry about showing in the locals or the

auto because it's always there and if

you have lots of variables like this

then you're only concerned about the one

that you're watching locals and aluto

will always change depending on the

scope that you currently at whereas

watch will only do what you want it to

do so because we don't put age inside

there then it simply won't come up just

because it likes it if we check the

locals we have agent there and the Autos

we have agent there as well but we don't

have it in the watch so what else you

can you do is actually just type

straight into here age

23 if you really wanted to you can

actually also type in conditions age

equals equals 23 it returns back true so

if you had an if statement that you

wanted to track then you could do that

as well so let's have a

look so had this statement that said if

age is greater than 18 then we can just

print out 18 plus to the screen so let's

run the code now and see what happens so

as you know we have this condition right

here so if you actually highlight this

and and drop it you can see that the

cursor changes and you can drop it

inside here let's click on the age equal

equal 23 and click delete so of course

we can't really render this function

right now because we haven't even

created the integer variable so let's

run over the integer variable and as you

can see here it says AG is zero which is

the default value for integer and that

is indeed zero right now and then AG is

greater than 18 is false but as soon as

I step over this line and it makes age

the value of 23 you'll see that these

two immediately change you know they

change because the values have went red

red means that the values have just

changed in the last button that you

pressed whether that means you stepped

into over or stepped out it doesn't

matter red always dictates that the

value is just changed so now it's

telling us that age is greater than 18

is returned back true so when I step

over this line because this is returned

back true I should render 18 plus let's

have a look F10 F10 and there we go

we've reached the middle which means

that's going to get printed out

now so so moving on to another example

we can have print details if we go into

this function and let's say we're only

concerned about the name then we can

print out the name down here but as you

can see we already have it twice so

that's just going to update depending on

the parameter name and even if you

wanted to show something like this you

can drag this down here and it actually

shows you the output this is very handy

so you can see the output because

sometimes when you have it like this it

doesn't look very clear you can also

hover over it right there and you can

see the value placed here but again it's

very handy to have it here so you don't

have to keep hovering over the tool tape

just to see the values and from here you

can actually change the values so we can

just say Harry is now the value for the

name and Harry is going to be age 35 so

when we press F5 and let the program run

naturally you'll see that Harry is age

35 now and that's all done through the

watch list down here and it doesn't even

matter because we have full control over

the watch list variables and we can

change their contents to see how our

code acts in different situations so

please use this watch list as much as

you can and don't forget that once you

can double click on variables and add it

to the watch just from here you can also

say quick watch and what that'll let you

do is it'll give you the value and then

if you want to add it you can press add

watch here or you can just keep pressing

re-evaluate once you've skipped over

some lines so let's say we skip over it

now and then go back onto it and because

it has a shortcut shift F9 we can just

quickly press it like this and it says

Alber inside here and we can change the

expression

maybe do something like age and then

click re-eval it and it changes the

value to zero and then we can even

change this to like 50 for example as

you can see down here it's changed to 50

as well so you can drag and drop it into

here or you can double click and press

add watch it's up to you please make use

of this watch variable and don't rely

too much on the autos and the local just

so you can keep your own variables

inside here and you can have complicate

outputs like these you can check

conditions you can check console outputs

as well as just checking normal

variables so using the watch window you

have lots of different opportunities to

keep track of your

code so we've got another debugging task

let's try and run the code and see what

happens we're going to get printed over

18 so our value is agal 35 and we're

already getting over 18 printed to the

screen so if we place a Brak line inside

here and actually run the code we can

see what's going on we can add them

inside the watch just like this we can

add in all the conditions inside

just like

this perfect so we can actually see here

that because our age is over 35 it's

saying greater than 18 is true greater

than 30 is true but greater than 50

isn't true and we can easily see that

except because this is an if and an LSI

chain as soon as the first one is true

it's going to enter that one you press

F10 then we get over 18 and then as soon

as it finishes that path it actually

skips all the way to the end the problem

with this is we only want it to print

over 30 since it's over 30 so we have to

introduce ranges inside here and we can

say age is less than 30 and then you can

have AG is greater than 30 here and then

you can also insert an and and say age

is less than 50 and there we go and now

it says over

30 and if we change this to 55 then we

should get over 50 and then finally if

we change this to something like 23 then

we should also get over 18 perfect so

what was going on here is because we had

if El if chain then you're only going to

get one of them printed and because a

value over 18 satisfied both the first

path and the second path then it's only

going to print the first path if we

actually undo our changes all the way

till the start and then we actually flip

the order of this if we put our this one

first and delete the else and change it

to lse if inside here so now we have a

new chain except the first one in the

chain is actually if age is greater than

30 now if you're on the code you'll see

that the first one actually says over 30

30 and it says over 30 and it doesn't

say over 18 anymore but simply because

this is the first one in the list as

soon as it hits the first one as I'll

demonstrate now it will go inside this

bracket hit this curly brace and then

jump straight to the

end you should always order them from

smallest to biggest because that will

probably make the most logical sense

when you're reviewing your code and in

doing so you can actually have a more

formatted code but you need to watch out

that the ranges need to make sense if

you have something like this then you

need to restrict this one because it's

going to be blocking the second one and

you don't want to make these all ifs

because then you get multiple outputs

now we should get 18 and 30 being

printed to the screen this is not

something you want so make sure that you

place your ranges inside here by adding

an and and then placing the other end of

it which will be 30 and then this one

will continue from it so just to sum up

what you need to do is make sure that

you use l ifs and make sure that you

have ranges just so when one exits it's

going to check the other one and the

other one you don't want the first

condition to beat the second one if

that's for the right path age 35 goes

into this path or at least we want it to

but it's been restricted by the fact

that age is greater than 18 when we had

it like

this this is restricting everything over

18 to go inside this path if this was

even like 65 we should only fall into

this one and we run the code then we're

still going to get over 18 which is

still technically true it is still over

18 but we have different outputs for

this sort of age range make sure that

you use El ifs and you keep in an

appropriate range and keep them in the

order of the if statements going from

lowest to highest or whatever makes

sense in that situation that you're

using

it this is a start of objectoriented

programming let's start with a concept

called structures structures allow us to

store different data types inside one

data type so it's almost like a group of

data types we can demonstrate this if

you wanted to stall something like a

person you would normally have something

like a string name equals ABA ear it in

age equal 23 so in here we have a string

name that represents the name and inage

which represents their age and you can

see that because these are two different

data types you can't wrap them around

inside an array you can't have a string

array called person and then say we want

to have two different values although

this will still compile we can't store

both these data types inside our array

unless you make the age and store it as

a string which will be 23 inside the

quotation marks and then you'll have to

worry about converting it later on in

your program so this is not really ideal

so we have something called a structure

so just outside here we can say struct

which stands for structure and then we

can have our name straight after so we

can say person and the next time we just

open the curly braces and let's see what

we've just created so now in order to

make an instance of this we can actually

use that like a data type so we can say

person person there we go so now we've

created our person variable now we see

we've got our own data type before we

were just using string and ins and

arrays and things like that but now we

actually have a new person so because

our person is made up of a name and an

age let's place that in here string name

and then int age of course you don't

want to give them default values because

like all variables you don't know what

the value is going to be to begin with

so now what we can do person Dot and we

don't actually have access to using

these values yet so what we're going to

have to do there is a concept called

access modifiers which we'll cover on

later on in this course but just for now

what this means is you either have

private or public public and they're the

main two words there's other words that

you can have but they're the main two

for now if you have it private it means

you can't access it down here through

person. name and but as soon as you make

this

public then now it becomes

accessible there we go we can make that

equal to something and if we take off

public then you see we don't have access

to it it's inaccessible due to its

protection level because the protection

level is private which means that you

can only access name Within These two

curly braces the second you make it

public it means that anyone outside of

the structure EG down here can also have

access to name so if we do that to our

public on in age and string name then we

actually can access both now we can say

person. Ag and it comes up like this the

purple cube is a function and the blue

is Fields you can actually fil them out

by pressing them down here show only

methods and show only Fields so we can

do age equals 23 and we can actually

just place abber inside here

and now that this data is essentially

the same as this the benefit that we

have now is when we print to the screen

we can say person. name and then hyphen

person. age and if you run that

now we get Abid

23 if you actually copy and paste is

lined out and just remove person dot

just so we're using these two variables

at the top of here we in the code you

should see exactly the same values

coming up there we go but now what's

really handy about this situation is we

have two data types that are wrapped

inside of one data type instead of

having a name and age we have a person

because what gets really easy now is if

you wanted to add something else inside

here like for example birth month which

for me is five so in here you'd have to

make another one birth month equals 5

and then all we have to do inside our

person is just say person. birth month

and we can just assign it right here

instead of having to create a whole new

variable we add it up here and then we

can use it down here and then we can add

that into here person. birthmonth

make add this to this one and then get

rid of birth month and as you can see

it's exactly the

same so we've taken these data types and

placed it inside our structure so then

we can just reference it by the person

this becomes very very handy for this

exact reason that I'm going to show you

now let's say you wanted to read this

data in from the user so you have static

and then you're a bit stuck on what data

type you want to return if you forget

about the birth month then we have a

name and age is a very basic person but

our return type only lets us have one

type so that's going to be a problem now

so you could do a string return that

will give us back the name and then we

can say return person and inside the

brackets what you can actually do is do

out in Edge so you can have two output

variables an out age and then a return

string so inside here we could

say enter your name and then string name

equals console. readline

if we copy and paste this down and then

we can say enter your age and then we

can say int age equals convert to in 32

and then put the console read line

inside there there we go now that we've

got that we don't actually need in age

because we've already got output

parameter so we just need to be age and

then after we've retrieved them two

values we can return name there we go so

let's actually try that up here so we

can say new name is equal to return

person and then out into new age and you

can take this exact line paste it down

here get rid of the birth month and then

we can use new age in here and new name

inside here so let's run that now ask us

what is your name Abba what is your age

23 and then we got Abid 23 which is the

same as what we've got before but you

see that this situation is pretty messy

we have to return a string variable and

also use an output in order to be able

to give back two values alternatively

what we could do is actually just turn

back void and then we can say string

name and then inage but we're going to

have to use references inside here and

then have string new name equals nothing

and then new inage equals zero so we

have string new name equals nothing and

then into New Age also equal to nothing

and then inside our function instead of

having out parameters we can actually

use a reference and we can say new name

and then ref New Age and what that will

do is we can just return it like this

because it's void instead of declaring a

new string name we can actually just

assign it to the reference that just

passed and because it's a void parameter

now and for the return type we don't

actually need to return anything so we

can actually assign these by reference

enter your name abber and 23 so as you

can see it's still works fine but let's

say we wanted to add another birth month

so we' have to take the birth month

variable new birth month make it equal

to zero and then add yet another

parameter ref new birth month and then

we need a new birth month inside

here ref int birth month and then we

need another one from here enter your

birth

month and then we'd store it inside

birth month and then finally we can also

return it up

here as new birth

month Here We Go abber 23 and then five

so we get them values being returned so

in order to keep adding different

properties to your code you have to make

a new parameter and reference variable

and then append the parameters inside

the function now the problem that is

even though you can have an infinite

amount of parameters then this gets very

tedious very quickly and you don't want

this so let's have a look at how we can

make this function exactly the same way

just using our new structure you can see

that instead of having to pass all of

these different things in or return back

a certain value we can actually just

return back a complete person so all of

this can be squished pretty quietly so

actually let's just comment all of this

out and let's comment out this function

and let's have another

rewrite so now that everything's gone we

only want to be returning and dealing

with the person so we can say static and

instead of void we actually want to say

we want to give the user back a person

return person and we don't need any

parameters in this case this is only

passed in because it's a reference so we

can assign these values back to the

original variables but because we're

going to be returning a person then we

don't need to do that so we can

essentially take this code and put it in

here and we can say string name now we

have an INT here and then also an in

here there we go so now that we've

constructed all our variables that we

need for the person what we can do is we

can make a new person person person and

then person. name equals name and then

duplicate this down we say person. AG is

equal to age and finally the birth month

is equal to the birth

month and then now that we've

constructed our person we still got a

red underline because we haven't

returned anything yet and after all of

that we can say return

person there we

go and now simply in here we could just

say person person equals return person

there we go and then we can take this

console right line here and place it

straight underneath since it uses the

same properties now let's try and run it

h

235 there you go so now instead of

having to make a whole new variable all

you have to do is simply just add

another one down here so we can just say

number this could be your favorite

number or anything and we can add it

onto

here person.

number and then here we can paste this

down we can say enter your number and

then it'll be number in here we can

paste it down here and change this to

number and there we go that's all we

need to change and this C doesn't change

cuz it's simply calling back the

function and we're doing all the hard

work inside here so now if you run this

enter your name AB 23 5 and finally four

and there you go we get the values being

returned so this looks a little bit

tedious we have to assign each one

individually every single time we do it

so we can actually use something called

a Constructor in order to achieve this

result we make the person we set them

and then we return it it'd be easier if

we could have the Constructor that we

can assign straight inside here and then

just return it immediately instead of

having to do all of these lines and then

reference each one individually so let's

have a look and see what we can do

inside here we can have a new function

that'll just be called whatever the

structure is called then you open the

brackets close the brackets for now and

this is called a Constructor in order to

be able to use it down here we need to

make it public in order for someone else

to access it so this says that it needs

explicit parameters in order to be able

to compile it says that it needs to

assign name age birth month and number

so let's see how we can do that as we've

used Constructors in the past for

example when we had things like the

string Builder we had s SP equals new

string Builder and when you make a new

something that means you make a new

Constructor so all of these sixes are

the parameters that go inside of this

but each one of these is a separate

Constructor if we control click this we

should be able to see them all there we

go you see that the class is called

string Builder and this is called string

Builder and these two names match

exactly so to make a string Builder you

can give it any one of these as long as

your parameters match one of these six

you can give it nothing or you can give

it some other values and that's the same

is true with our current

person so what we need to do in this

case is we don't have multiple ways that

we can construct this in the case of our

person we have to provide all of these

details otherwise the compiler is not

going to like it so we can have a string

name int age int birth mon

and in number now we got our parameters

set up we actually need to make use of

them in

here so because we have name and name

and they're exactly the same string by

string then here we can't say name

equals name in terms of global and local

variables this is global within the

structure as it's within these two Co

braces and all of these variables are

only Within These two curly braces local

variables always take presidence over

the main one if I double click on this

you'll see that all three are

referencing this Constructor if I double

click on this you'll see that both name

references on line 20 down here is all

referencing this one in the parameter

and not the one up top we want it to be

assigned into here so you have two

options in this case you could

capitalize all of these so then you

don't run into this issue and then

instead of name equals name it's capital

N equals name and that fixes it

alternatively if you want it to be a

lowercase then what you can simply do is

you can place this Dot

if you use this it goes back to the

current scope and the current scope of

name is going to be this which is within

the

structure when you use this it doesn't

reference the local variables it

references the global variables up here

so when I double click on this now

you'll see that that gets highlighted so

we can say this. age equals age this.

birth month equals birth month and

finally this do number equals number so

we're just getting all the parameters

from here and putting them into these

values this might look a little bit

redundant but I'll show you why this is

a lot

cleaner so back in our return person

function we have the variable and then

we assign them all manually but look how

easy this now has became if I comment

out this return we can say a return

person so now we can reference person

with a capital P this time cuz I want to

reference the structure directly so

after the person we can open the

brackets and then start to type in so

our first one is name age and the birth

month name comma age comma birth month

comma and then number comma and then

this is not going to like it because we

also need to place new

inside as I said before when you making

a string Builder you have a new keyword

and this new means that you want to

create a new instance of this you could

think of this structure as a template

this is the template for something and

until you start using that something

it's actually not going to do anything

so we have to make a new instance of it

because it's not already available so we

need to make it available through our

data type so when we say we want a new

person that gives us access to the

Constructor if I remove that you'll see

we've got one of two now we can create

an empty person and that will just make

us assign all the values individually

but we don't want that we want to use

the second path and if you drop the

arrow you'll be able to see the

different parameters which is the ones

we just typed in name age birth month

and then number and now that we've

returned back this we actually don't

even need to do any of these lines right

here we can actually just return it

exactly like this so now if you it again

you should see exactly the same output

23 5 and four perfect so let's just have

a little recap so normally when we're

creating variables we use common

variables that we've used in the past

string in double Flo bu Etc but with the

use of structures we can create our own

group of variables and then we can use

them variables inside functions and

inside our code to make things a lot

cleaner and a lot easier so inside

person we actually store four different

variables and they can be an infin

amount of variables should you will and

you can easily remove them and then

remove them from the parameters and from

the assignment and then that's it if we

use this without our structure then

we're going to have to declare every

single variable separately and I know

this is the same as doing it up here

it's actually a little bit more work cuz

you need to create a new parameter and

create all the the instances inside here

and assign the

values now the benefit of doing it

within a structure is that when you go

to do a function look how easy this line

is before you'd have to have name a age

birth month all as references or have

them as out parameters and it gets a

little bit long and a bit sloppy in

order to include the month in here we'd

have to have another in number and you

can see that the parameters are growing

if you wanted to store a user from a

registration form you might have name

address phone number country and the

list goes on it can be a very long form

and you therefore you'll have a lot of

parameters so it's very easy to wrap it

around inside a structure and then

simply reference it like that and you'll

need zero parameters because everything

is contained within the data type and

then inside here we can ask for the

users's details individually store them

as their integers and string variables

and then instead of using this notation

we can now use our fancy Constructor

where we can pass it in in this exact

order right here name age birth month

and then the number perfect and then we

can use inside here we can call each of

these positions separately and

concatenate them

together so now that we covered

structures known by struct as the

keyword let's have a look look at

classes and how to define them

syntactically so we can make a struct

called person and we can give them a

string name and inage and to make them

accessible we'll make them public and we

can say public person string name and

int Agee and that means we can assign

these values using the Constructor so we

can say this.name equals name and this.

age equals age perfect so's get rid of

our compiler error since we're assigning

both the values and then down here we

can use it we can say person person

equals new person and then have abber

and then 23 in the brackets and we can

print to the screen person. name and

then if use contrl D to duplicate that

down and we can do age and if we run

that right now we should see abber in 23

being printed to the

console perfect so as we talked about

introduction syntactically the struct

and the classes are very similar so

let's comment this out so we don't get

any compile errors and let's see how

this is going to work so of course we're

going to get error on this line but once

we make our class the error should

disappear from here so instead of struct

person we're simply just going to have

class person exactly the same and

because these are fields we can also

copy these down so we can have public

string name and public int

Edge and then we can have public person

and then string name in Edge this. name

equals name and this. age equals age so

as you can see this is exactly the same

as this except for the fact we have

structure and we have class inside here

now you see how compile error is gone

and if youve won the code we should see

exactly the same

output there we go now the main

difference about classes and about

structures is that you don't actually

have to give these a default value

inside the

Constructor if we just comment this one

out for a

second and if we bring the structure

back and we actually remove the

parameters from here then we're actually

going to get compile errors and it says

that the name and age must be assigned

before it's returned and stroks cannot

have parameterless Constructors now

that's a bit of a problem in certain

cases you might want to create an

instance of something but not actually

derive any values so you could actually

make these into default values you can

have string name equals nothing and int

age equals z and therefore once we place

inside here then there's no problems if

you run the code then you will get the

output this will be nothing this will be

zero and you'll get errors and you won't

get any compile errors doing this

but the point is you have to include

them and you don't have a

choice so if we just comment this back

out now what you'll notice about the

class

person is even if we get rid of it then

it still lets us have it of course we

can't actually give it parameters since

we don't have a Constructor that lets us

have parameters but what you can do as

well is you can overload this and

actually include it as well so you can

have another set that literally let you

define both of

them and then you can even have another

overload that lets you just include the

name without the ede and then also

another one that lets you include the

int age without the name or you include

both the details so in these cases what

you can do is you can store the values

up here as the fields let them use a

default Constructor or only give it the

name or the age or you can actually give

it both so when we come down into here

we actually now have four different

Constructors we can make an empty person

we can make it with an edge with a name

or with both

and what you can do in here is you can

say this.name is equal to nothing and

then here you can say this. AG is equal

to minus one just to default it to a

value that's incorrect so after we've

got this let's have a look at the

different ways so we can give it a

person which gives us an empty value so

if you run that now we get blank and

zero and if we just give it the name

which is Aba then we should get ABA and

minus one yep and then if you just give

it the ede you should get nothing in 23

and if you actually give them both then

you should get both printed out to the

screen and there we

go so now we have four different ways

that we can create our person with our

structure we're only really limited to

one since all of these values need to be

assigned as the structure is created

structures are meant to be used as an

extension to a data type rather than

almost like a mini management system

when we make a class we have so many

options of different ways that we can do

this inside here and later on in the

course we're going to show you how to

include functions inside here and then

eventually do other Advanced Techniques

with classes that aren't directly

available structures if you have a basic

example where you want to extend

something to use inside a function or a

data type like storing name and age

instead of having to do two return types

inside a function then a struct is

perfect the second you want to do more

complicated things or make it a bit more

specific and flexible then classes of

the way forward you'll see it very often

in games where they have struct position

you'll see in video games where they

have something like a struct position

and they might have int X int Y and int

Zed and that's for their 3D position or

they might even just have an X and Y or

you could have something simple like a

rectangle and you can give it a width

and a length so now when you use that

inside the code you're going to have

different flexibilities of the amount of

data types that you can store and then

when you return that back from a

function that'll be okay as well since

you can use this as a data type so if

you want something simple and

straightforward then go for the

the second you want something a bit more

complicated and a bit flexible then you

probably want a class we'll go into more

different situations inside this course

so you understand in which cases to use

what and in most cases you'll notice

that the class is more replicable so if

you wanted to you could ask the user to

enter these details in and then you can

pass them inside the Constructor so we

could say console right line change it

to a right enter your

name String name console. readline and

then copy and pastees down changes into

into a in Edge and then wrap this around

inside of convert in 32 instead of enter

your name it'll be ENT your Edge and

then C these are grade out it means we

haven't used them so we can change abber

to your name and then change 23 to age

perfect so now if you run the code now

enter your name ABA enter your age 23

then we get ABA 23 printed out to the

screen so just a quick recap

Constructors are basically defined the

same way you have public followed by the

class or the structure name and then the

pr inside the brackets and then you can

assign it after without structure you

have to declare all of the variables

inside the parameter and assign them all

down here otherwise you won't be able to

run your code with classes we have the

flexibility of just including one

default function which will just be our

Constructor and even if it does nothing

this is still acceptable and then we

broke it down to multiple sections where

you might have situations where you only

want their name or their age or you

might even collect both to store it if

you collect their name then we'll

initialize age to minus one just so you

know that this is a bad value and if you

declare only the age then will'll

initialize aim to nothing just so you

know that it's an empty value and if you

accept both then we simply assign both

and down here we have access to it we

can even make this a little bit better

by checking these values so we can say

if string is not null or empty of

person. name and we can put a knot

inside here then we can print out this

and then the same thing for age we can

say if person. AG we can say greater

than minus one since we set minus one as

the bad value and then we can move this

line up and let's try run it now so

these two if statements are

fundamentally separate from each other

one is checking the name and one is

checking the age so let's try and

manually type these in and see what

happens so enter your name let's make

this blank and only include the age and

let's see if this runs okay so you had

no name and the age is 23 then you see

we only get printed 23 because it'll

skip over this line as you can see if we

put a break point in now and just

refresh the code we get empty name plus

23 and when we press F10 over this line

it jumps the next one because if we

hover over this you'll see that it

should return us back with false there

we go we get strings null or empty is

false so that we skip over that and

because the age is 23 then this line

going to execute let's run it again and

do the opposite we could say name is Aba

and then age is min one and then we get

ABA printed be not the

edge there we go so if you wanted to fix

these in and type them manually so we

can say

23 there we go we get 23 and if we only

include the

name

AB then we only get Al printed out and

if we include both of

them then we get them both printed out

perfect so now that we've created our

class we've gave it some fields and

we've made our Constructor that lets us

initialize it let's see how we can print

the details out to the console so we can

have a CW tab tab a console right line

and then we can say string interpolation

name callon space and then we can have

our curly braces and we can say person.

name we can access these fields using

the dot operator and then we can say

back sln for the new line and then say

age and the same thing person. AG let's

try run that and see what we've

got so now we've outputed name and age

out to the screen let's see how we can

do this inside a function if you wanted

to place this in different part of your

code then you're going to have to copy

and paste this and as we know this is

bad for our code because it's j a

duplication we want to be able to wrap

this inside a function somewhere so

let's see what we can do we can have a

static string so it can return as back

with this that we can output straight to

the console and we can say return

details and for our parameter we can

have something like string name and int

age because that's what our class is

made up of and inside here we can simply

take this

put it inside here and put a return

before it and then get rid of the person

dot since we have an aim and age

variables as local to the function so

now instead of this line we can

duplicate this down take out the

contents and we can say return details

and then we can place person. name and

person. AG inside here let's run the

code so we get exactly the same output

but the problem with this function is

we're having to type out the parameters

manually we have them here and we're

going to have to to type them again in

here and again for the parameters this

is a bit of a problem cuz we've just

made our class but we haven't really

used it when we're passing it in so

instead of having string name and int

age we can actually duplicate this

method and just comment it out just so

you can see it for reference and instead

of doing this you can actually just pass

in an entire person so now we have

access to person and we can bring this

back and we can say person. name and we

can also say person. age and the reason

why this method is preferred is if you

actually have something else public int

number and then you added another one

inside here in number and then you said

this do number equals number the

parameters won't have to change inside

the function all you have to do is

simply pass in the person and then now

we actually have access to doing person.

number inside here should we need to if

you're passing them all in one by one

like this then you have to append into

number on the end and then add it

straight to here and then also add it

inside here as you have like this it's

easy to pass the class in because all of

these properties above are just simply

used throughout the function so now if

we just bring this

back and let's have a run and see if we

get the same

output which we do name ABA age 23

perfect so now that we've thought of two

functions how to do this we can manually

type in the strings and the integer and

any other parameters that we need all we

can actually pass in the person the

problem with these two approaches is the

whole point of having classes and

structures of of the like is so we can

wrap around lots of functionality inside

it if you want to send this to another

developer for them to use in their

project or maybe this is a file that is

shared within the project then you don't

want them to include a separate function

that is simply outside of the class we

want to include the functions inside the

class so of course as you know these are

functions so inside here we can also

make functions so let's do it so we can

have public and we want a string return

details and then we don't need to pass

anything in the the values that we're

going to be returning are these two

right here which is technically local to

our class so therefore we're not going

to have any parameters I'll explain that

further in a second so we can take this

exact line paste it straight into here

and get rid of the person Dot and get

rid of the person do on this side so

then we have the name variable and the

age variable if you double click on them

you see it references these variables

and the same thing for age the reason

why we don't have parameters is when you

call this function as I'll show you in a

second you call it directly on the

person so the person that you're trying

to print has already got these contents

inside of them so let's have a look we

can duplicate this down and instead of

calling return details and passing the

person you actually do it the other way

around you say the person object dot

return details so now we're calling

return details inside our person

object now let's run the code you see

that the output is exactly the same

except now this functionality is a lot

better our return details is wrapped

inside our class which is the preferred

way now we can actually get rid of these

inside here and then we shouldn't really

use this notation even though it is

technically correct but you want to wrap

all your functionality inside here and

just bear in mind that scope is also

applicable here return details is within

this class which means if you try to run

this function inside the main then it's

not going to node exist this return

details function was able to be used

inside here because this was in the

scope of the class program which is this

curly brace and this curly bra but this

return details is only within the scope

of this class which means you can't just

suddenly use it inside the main so

please bear that in mind when you

produce in these classes and you trying

to understand where everything goes

every function that you place inside

here is accessible through the dot

notation after the object once you've

created the class say person equals new

person and then after we type person dot

then we'll get a list of all the

functions the same way if you have

something else just like this and then

if you do person Dot and then you see

that something has appeared there so all

the functions that you create up inside

in this class for every person that you

make it'll be accessible through the do

notation so just to round up this video

you should always make your functions

inside your class having them inside the

class is the whole point of making a big

class and have lots of functionality

inside if you keep the function that

does exactly the same thing outside of

the class then if you were to share this

file or come back to it in the future

then you're not going to understand why

you've done this and and it's outside

the class keeping everything wrapped

inside the class is the best way forward

in most case

scenarios so now we have our class

person it's got a name and age a

Constructor and a function to return

back the details and we can simply use

it like this and return back the details

like this but so far we've covered how

to make classes Constructors Etc and

assign the values but the values are

only get assigned throughout the

Constructor but what if a different part

of our program the user wants to change

their name or update their age Etc then

what options do we have so you know we

can access our Fields using person. name

and then you can simply type equals and

then type in another name and we can say

person. AG equals 25 now we've changed

these properties if we use the console

right line and return details you'll see

that the values have indeed

changed as you can see there it's

changed the hurry and

25 this is okay except for the fact that

this shouldn't really be possible we

should try and restrict this Direct

corly by the use of gets and sets so for

now we're going to cover fields and how

to restrict certain values and later on

in the course we're going to go through

what properties are so before alter

properties got invented we had to use

gets and sets gets and sets are

essentially functions that will prevent

this from happening so the reason why we

can do this is simply because this

public word is placed right here if we

change it to private it means that name

is still accessible within here and here

which is only the limit of the class but

as you can see it's not applicable here

this is why you need the public Getters

and Setters so I'll explain that what

that means now so we prevent these from

accessing outside the class by making

them private so this functionality no

longer works so what you have to do

instead is for every field that you have

you need to create a GTO and a seter

function so in order to set a value we

can have public and because we're

setting it we don't need to return

anything so it'll be void and we can say

set name and then we pass in string name

and then we simply just say this. name

equals name so when someone call set

name and pass in the name like this we

just override it so let's see how that

looks so if we comment these two lines

out we can simply say person do set name

and then we can say Harry so this line

is directly replaced by this line and

now if you run this we'll see that Harry

should be changed there you go abber is

changed to Harry this is powerful

because what we can do now is restrict

the access inside here and make sure

that the value contains something

genuine we can say if string is null or

empty and then pass the name inside

there if the string isn't null or empty

then we assign it and if it is null or

empty then what we can do inside here is

we can say else this do name equals

nothing and we can keep it blank or we

can use string. empty just to make sure

that we know that the name has been

blank out if they've entered something

invalid if they've entered something

that's valid we assign it if it's

invalid then we don't assign it and we

just initialize it back to nothing and

because our options are endless with

this we can actually make this into

something else we can simply just say

invalid name so then what happens is if

we try and change this to nothing and we

run the

code we can see that we get invalid name

being printed to the screen and that's a

lot better than if we had string.

empty because if you had string. empty

then it would kind of say nothing then

it looks like something's went wrong if

you have something like invalid then

that gives you a bit of a Barr message

so we can have invalid name there so

using our Setter Let's us change our

name from down here without directly

exposing the public if he expose the

public then everyone who uses classes

can simply change the name to whatever

they want having it private and only

changing the name within the set name

function is very preferable because you

can restrict the access and what people

can set the values to if they try to set

it to something that's not valid then we

set it to invalid name whereas before

when we did this and if we make this

public

again we can simply set this to

something bad like null null is a very

bad value but it still lets us have it

and the reason why it's very bad is if

you try to do something like this

person. equals and just some empty

brackets now even if I just leave this

empty the fact that you try call equals

on S inside person. name is going to be

a bit of a

problem so we can say person object the

name property inside it and we checking

if it equals to Aba for example but

because we've just said it to null let's

see what

happens you see that we actually get an

exception it's a null reference

exception because if you hover over name

you see that returns back null nulls are

very bad in this case whereas if you

actually bring our function back and we

make this into a private and then if we

try and take this value our null value

and place it straight inside our set

name if you uncomment it get rid of the

quotation marks and if you actually try

and assign it to null let's see what

happens then we're going to get invalid

name and this prevents us from breaking

our code if a function like equals were

to be called on any of our properties

inside of name so now that we've done it

for our name we can also do it for our

age but before we do that we can also

call a function called get name return

details gives you back these details

in a very specific fashion but what if

you wanted to do something like this

your name is and then you wanted to do

person. name but of course you can't do

that because name is no longer public so

you can't reference it directly so we've

got a set name and then now we need to

do a get name so we can have public and

because name is a string the return type

is going to be string and we can say get

name and this function is very simple it

just returns back name and that's it

there's no no parameters because all it

does is return back a value and in this

case it's just returning back name so

instead of using person. name in here we

can say person do get name open close

the brackets to indicate it's a function

but there's no parameters now you see

that we have no compile errors so now if

you run that you'll see that it says

your name is invalid name so let's

actually give it a

value and we can say Harry again run the

code there you go your name is Harry so

it's officially change it now let's work

on the

edge so just following the exact same

approach we can have a public void set

Edge in age and instead of just doing

this. age equals age let's try set some

boundaries on this we can wrap this

inside an if statement so as for the

condition we want to validate if the age

is successful so we want to do if age is

greater than or equal to zero and age is

less than or equal to 150 so this means

if the age is less than zero or greater

than 150 then this is going to return

back false and then it's not going to

assign the value and we can simply

assign it to minus1 -1 is just so we

know it's a bad value so now that we've

created our set age we can actually make

our get age so we want to return back in

int because that's going to be the data

type for age so we do public int get age

we don't need any parameters cuz as like

the name we're just simply returning

something so we can say return age so

now that you've made the set age and the

get age let's actually use it quote so

we've made our set name and let's say

person do set age and we have 25 inside

here just like above and then now that

we've set the age let's appin this

sentence we can say and your age is and

then person. get age perfect so let's

run the code and see what happens there

we go we have 25 and Harry being

changed so another little tip when we do

in this code I get age as one line and

I'll set age as a couple of lines but

fundamentally only assigning Edge so

what we can do is we can make these into

the tertiary operator so as we can see

here this is the condition if this

returns back true then we assign age our

local parameter and if this returns back

false we assign minus one so we can

actually make this into this. AG equals

if you remember the format of our

conditional operator use condition true

and then condition false so this is our

condition that is leading our if

statement and if this returns back true

then we do question mark and put age in

there and then call on if it returns

back false then it's simply minus one

there we go so this is actually being

entirely replaced by this one simple

line and we can do the same thing for

our string so we can say this.name is

equal to string that is null or empty

pass the name inside there and if that

is true then we assign it to name and if

that is false we say invalid name let's

comment this one out and let's see if

our code runs okay still

hurry 25 hurry 25 and now let's try our

bad values so we can try to set the age

to 200 and the value to nothing and

let's see what happens we get invalid

name minus one and there you

go perfect so that's working okay let's

undo them values and you can see that

this has been shortened completely

another top tip for functions like this

you can actually use something called

Arrow functions and if you've done any

JavaScript this might be quite familiar

so we can have our get Edge and in

instead of open the curly brace at this

point we can actually place this Arrow

inside so if we comment out this one

with arrow functions you can only return

back one simple line so because this is

only one line we can have return Edge

but our functions don't like the keyword

return any variable that you put

straight inside here is automatically

applied that it's a return type so we

literally just put age get age returns

us back with age if this is confusing

then stick to this approach but this can

might tidy up your code because instead

of having all of this and all the curly

braces for one simple line you can use

Arrow function so if you actually copy

and paste but first comment out all of

these functions you can see how small

the code can actually get there we go

and then let's actually have a public

void set age let's put our R function in

and then we can take this one line and

paste it straight into here so you can

see that these two lines are now being

replaced by all of this

and of course we need our int age inside

here there we

go so now we can do the same thing with

our set name we can take

this we can have public viid set name

String name and then have our Arrow

function and then simply paste that in

you can see that this will get a little

bit long now but it's still ideal I've

got the code zoomed in but if you put

that to 100% you'll see that this code

is actually pretty small it's just I've

zoomed in just so you can see the code a

lot

clearer and then finally for public

string and we can get name and then we

simply just do arrow and then name and

you can see that with all this other

code commented we're just relying on

these functions as we just created and

you'll see there's no console errors or

anything else so if you try and run it

you'll see that everything is going okay

of course if you don't like this

notation and you don't have to include

it you can stick with the curly braces I

don't use the arrow functions a lot in

practice because there isn't many

situations where you'd actually use them

and even if you do some people might not

like them or there might be certain

guidelines that you might be following

inside the

company what I do suggest you stick with

is the tertiary operator these are a lot

good and a lot better to use since it

reduces the code drastically if we

actually remove this and I'll paste it

back in just so you can see it for

reference inside the resources but you

can see that these four functions are

now reduced to on one pitch you can see

that they're all got one lines and

they're pretty short so let me undo that

just so you can see the original code

and have it for

reference so there's a lot covered

inside this video so let's just have a

little recap before we finish up so just

to start at the beginning when we

initialize our person with their name

and their age we don't actually have any

other opportunities to change their

value which is where this comes in you

can actually just say a person. name and

assign any sort of value you can put

anything inside there and it'll assign

it directly unconditionally the problems

of doing this is someone might expose it

to null by accident and somewhere in

your code if you're using something like

equals this will actually prevent an

exception of course you can wrap this

inside a try catch but this is

unnecessary because we can prevent this

in the first place it's always best to

prevent an exception then to put

additional cord in like a try catch in

order to catch something that you can

prevent in the first place prevention is

definitely better than the Cure in this

case so having something like a person.

name being exposed it's not so much for

the integer because integers will always

have number values whereas strings they

can be initialized to null there is no

such thing as null when our age that our

default value for an integer is zero so

we don't really have the same issue we

started introducing the name function

and the age function these are used so

we can control what values get assigned

to our local variables and we also have

our get functions get name and get age

so let's explore how these are made

inside our Cass and expand on that and

see how they work so first we need to

make sure that our variables are

actually private they're private so we

can't access them just like this if I

copy and paste this line You'll see

we're going to get a compile error and

this error is because we can't actually

use it because as it says here it's

inaccessible due to its protection level

and the protection level comes from the

fact that this is indeed private it's

private so it's only accessible within

class person so every reference and name

can be used inside here just not outside

so the only way to be able to change the

name now you've made it private is by

enforcing this function the good thing

about this function is we can check if

the value is invalid and then we can

star it if it's valid and then change it

to something else if it's invalid and

then we can wrap this if statement

around in a conditional Operator just to

make it a little bit shorter and in

order to retrieve a name because now

that it made it public we can neither

set or retrieve it so we have our get

name function that simply just returns

it back to the screen nothing special

here and the same thing for our set age

we can check if the age is between 0 and

150 and assign it and if it's not assign

it to minus1 and then again wrap it

around in our conditional operator and

then for get age we can just return age

I demonstrated our functions inside here

you can take all of these on line

functions and actually add them all in

one line without the need of curly

braces if you want to return back in age

then you simply just type in age just

like you can see here and here when you

do assignment you want to specify this.

name equals but when you doing returns

you just need to type in the variable

that you need of course this looks a

little bit cleaner and a bit shorter but

not Everyone likes this notation so it's

completely up to the developer all the

environment that you're working in and

finally we just have our return details

and then we can make use of them just

like this by using person which is the

name of our local variable for our new

instance followed by a DOT and then set

name set age and then get name and then

get

age so now that we're dealing with

classes it's important you focus on the

scope we have a person person here

that's defined within the main which

means it's only Within These two curly

braces so if we have a static void and a

function that did something inside here

we can't access person inside here if we

take out the comment and we try it the

only reference we have is accessing the

class directly not this local variable

if you want to access local variable

from inside this function then you'd

simply have to pass it in like this and

then it'll finally become available as

you can see now there's two and it says

as a parameter and the same goes for

this class above just because we have

name and age up here it doesn't

necessarily mean that we can magically

use name down here if we have something

like a static int defined right here

then we can use this integer inside

these functions as it says there it's

available we can sign that to 10 and

it's also down here available and we can

sign that one to 20 and you see we have

no compil erors but if we take this and

put it inside our return details

function then you're going to get an

exception because X is not available

within that scope and the functional

scope matters inside here as well if you

have a string test inside

here we can just access it in another

function for example The Constructor we

can't just print out test as the screen

the handy things or classes if you want

variables to be available throughout the

class then you put them at the top we

can have that private string test and

there you go you see that the compiler

error is now gone and we give that

initial value and then you can use that

to print to the screen Etc and just to

note when you have a variable that's

called exactly the same thing we have a

private string test up here and we can

say hello then we also have a string

test down here so let me demonstrate

what happens when two variables are the

same so let's move this into our

Constructor so if we run this as it is

the test equals hello and then we print

out test and then run the

code there you go we get hello printed

and just ignore this this is from the

return details function as we've got in

the main down here return details so

we've made our new Constructor and then

assigned it the value with abber and 23

and then we can get hello printed to the

screen but let's say we actually made a

string test inside here and we said hi

instead of hello then which one do you

think it's used the global one or the

local one let's run it and find out it's

actually the local one the compiler will

always prioritize the local variables

over the glob Global variables and this

is the reason why you need to type this

dot because when you use name it's

always going to reference the parameter

when it's name on its own use console

right line and say name and then if we

do after this line and another console

right line and we say this. name then

these are completely different this one

is the parameter and this one is talking

about the variables from the class and

these are known as our Fields local

variables take presidence overall Fields

as long as they're called the same thing

as you can see now says it's assigned

but the value is never used if you take

the string test out then you can see

it's now being

used there you go hello gets printed out

to the screen and there you go the green

line has went because we're using it

down here scope is very important when

you're designing these classes and just

to note that the variables that are

available inside here are not going to

be necessarily available inside here

unless you make them public and even

when you make them public that doesn't

necessarily mean mean that this is still

available magically over here name will

only be available through an instance of

the person class so if we do person.

name now we can see that that's

available as our field so that's why

that makes it

public if we try and make this private

again just to return that value

back and we scroll down you'll see now

we get a compile error because it's

inaccessible because it's outside the

scope because it's now a private

variable

so we've covered what it means to have a

public or a private field if you have a

private field then you're going to have

to use Setters and Getters in order to

work with this field if you make it

directly public then you won't need any

Setters and Getters because the user can

just type in do name after their public

instance so you can have person followed

by the dot and right now you can't see

name tied here and the blue slittle icon

is no longer there the reason why it's

not there is cuz there's no public

Fields if we then change this to

public remove the dot and read it you'll

see that our blue square is now back

because our field is now accessible and

we can assign that to anything that we

want the whole point of making this

private is you only restrict the access

so you know what you're doing and the

reason why I say that is if they enter

something that's invalid then we want to

specify something like invalid if we

give them Public Access then we're not

going to know and we won't be able to

validate what they've entered so in

order to do it through a Setter we can

make it private create our set function

with a return type it's going to take in

a string parameter and this string has

to be the same data type as the prety

you're talking about call it name or

whatever you want it to be and because

these two are called exactly the same

thing we have to use this dot so we're

referencing this name instead of this

name in the parameters and here we're

just using a conditional operator and

we're saying if the string is not null

or empty hence the exclamation point

then we assign it to name name gets put

in this.name and if this were to return

back false then we place invalid name

straight into this. name and for the get

we simply just return a string since

that's a data type for name and then

just return it back and it's that simple

and the same thing for age we validate

the age range set it and if it's outside

this range we can set it to minus one so

as you can see this is a bit long we

have our private variables then we have

two functions one for set one for get

and the same thing for age so these are

called fields and gets and Setters so

I'm going to introduce a notion called

properties and properties make it easier

to do this exact notation so let's now

see how property is created since these

fields are private we need to make

something that's actually public so we

can do something like this we can say

public string and we can say name with a

capital N so now you can see that name

and name are not actually clashing and

we can assign that in here should we

want to and you see we don't need this

do anymore because this is now different

it's a capital N so this one always

references this and this references this

so we need to add something else onto

here so if you create them

both now you can see we can do age

equals age and there'll be no problems

with this so now that we've made our two

new Fields so now we have our two new

public variables which are known as our

Fields let's see how we can turn them

into properties so if you remove the

semicolon you can actually add in some

curve curly braces so inside here we

need a getter and Setter just like we

had down there but we have a quicker

notation for it instead of making two

distinct functions you can actually

create a little section inside these

curly braces to get and set the name so

you got to think of this as a middleman

the same way this is a middleman we call

this function and this function will

affect the name and this is going to be

the same principle we make use of name

with a capital N but it will actually be

affecting the lowercase end variable

these are going to be our new middle BS

and they will directly replace the gets

and sets here so what we can do in here

is we can say get press enter go on the

next line and make some C braces and

then we can also have a set so this is

like our mini get a mini set function

just like we've got here but as you can

see it's a lot shorter we don't need to

give it a distinct name or set the

public or anything else we just give it

a simple set and get so if you have a

look at the compiler error it says not

all cause path return of value because

as it says here this get function is

actually a mini function built into this

property person. name which is our name

with a capital N and then it's do get

and this returns back a string so if we

actually just type in return this then

it gets rid of the compiler error but of

course we don't want to return nothing

we actually want to return name because

remember what I said string name and

intage up here as our private field they

actually start the data the public

string name and the public inage is the

middle mans that coordinates all of the

gets and sets to do with the private

variables so for the set just for now we

can say name equals and then what do we

give it the value of so if we hover over

set it doesn't actually give you any

indication of what it's going to be so

let's have a look if we press control

space then we don't also get the list of

everything that we can add inside here

now it's very difficult to understand

what to assign this to when we have set

name we've gave it a string name

parameter so we can use name down here

so what's our parameter in this case

well the function itself doesn't

actually tell us but I'm telling you

right now that the parameter is actually

just called value and this is a very

default parameter that you'll see

everywhere when you're doing gets and

sets inside a property you don't have to

specify value or anything it just gets

simply assigned in just like here where

we have string name this will be a

string value so it will look something

like this behind the scenes but of

course you can't actually type this in

cuz remember these properties they

trying to give you a short hand on how

to do gets and Setters so you just got

to remember that when you're trying to

set you get the property inserted as

value so you can start that into name so

that's it the name is actually sorted

now so before we do the same thing for

age let's see how we can use this inside

the code so we scroll down to our main

we have a person person and then we have

a console right line that prints the

details so we can actually do person Dot

and you can see now that it's slightly

different if you ignore the edge since

start to return back as a field since we

haven't done that yet you'll see that

name now has a little Cog next to it

sure only properties this is a new icon

that we haven't saw yet so this is our

new icon it says it's going to return us

back with a string and we can actually

execute a get and execute a set straight

on this name then we can say name equals

Harry and we don't get any compiler

errors whatsoever this lets us do it

completely so now if we do another

console right line and we say person do

return details and let's run that and

you should see that the name gets

changed there we go ABA gets changed to

hurry perfect so after we've got person.

name let's see how we can also get the

value so we can just say string

interpolation your name is and then we

do person and followed by a DOT and as

you can see the exact same list that we

got given over here is given right here

as well name also says get and set and

it returns back a property which is a

string type so we can open like this and

you'll notice that we don't have the

parentheses after it and the reason for

that is because the brackets indicate

that you're calling a function but a

property is just a property it's almost

like a variable so you're not actually

needing to put any brackets after it

because there's no function to call so

as you can see this is exactly the same

as this in this case we're actually

setting the value and in this case we're

actually getting the value so let's

comment this line out and let's see what

happens your name is Harry so that's

worked okay so now let's do the same

thing for

age remove the semicolon add the curly

brace we can say get and then return age

and then simply do set and then do age

equals value there we go so now we got

that set up if you return back down here

and we see a person dot so as you can

see now the age is being removed as

being a field as we saw in the blue icon

it's now been turned into a spanner

which means it's a property as shown

here show only properties so now you can

see it returns back an integer and we've

got a get in a set so we can set that to

25 for example and then we can append

onto here and your AG is person. AG you

see that it's a capital A just as this

one is a capital N and now we have

getting set available and again we don't

need the brackets inside here since this

is a property not a function now let's

run it we should see Harry 25 there you

go your name is Harry and your age is

25 perfect

so as you can see now is instead of

having our set name and get name which

are these two functions we actually have

them contained within a

property we call this property and it

affects these variables but as you can

see just as these functions below were a

middleman we've now moved the middleman

into these properties so as these are

only one line you're probably thinking

there's probably a better way of doing

this and it's a lot quicker for syntax

and you're right there is so let's

explore that so I said before about

about Arrow functions when you're doing

something like this get name you can

have public string get name and then you

can simply do Arrow name this is a very

common string function get a name that

just returns back a value and you can

make that into an arrow function by

literally just putting back name so we

can actually do the same thing but for

our properties so these both contain one

line so we can actually minimize this

and if we just block comment this

out we can say public string name and

then we can have get and then the same

thing with the arrow function when we

doing Arrow function down here you

simply won't use the return keyword you

would just type in the variable you want

to return so we can say get arrow and

then simply lowcase name and then add a

semicolon just so you know that that

section is ended and then straight after

we can have set and then arrow and then

name equals value just like that so as

you can see these contents are exactly

the same we've actually returned them

back in a short space and of course here

as we got a compile error it just

expects a semicolon to be right at the

end here there you go and now we do the

same thing for our

age we can do public int ede get arrow

ede and set Arrow age equals value

perfect so now you can see that this has

been drastically reduced if I cut this

just underneath you'll see that almost

all of these lines are just replaced

into a two and you'll see that our

functionality will still work exactly

the same if we just run the code Harry

and 25 is being reassigned over our

default variables perfect of course now

if you want to add some more

functionality inside here for example in

our set name we had a little check that

makees sure that it's a valid name

before we assign it so we actually take

this and paste it over here then you see

the lines grown quite a lot so what's

pred to do is if you're going to have

any data inside here that does a little

bit of logic then it's best to keep the

curly bra is since you can have a bigger

output and it looks a bit more sensible

if you're just simply getting and you're

setting the variables then you can leave

this notation so let's undo this and

bring back the original ones in this

case there we go and then we can take

that line and in the set we can actually

replace it here so as you can see this

is a little bit grayed out so let's put

the semicon at the end and let's see why

this is grayed out name can be

simplified so what we're doing now is

name and all of these variables are

actually looking at the private variable

whereas if you actually look down here

and we uncomment this you'll see that

set name if we double click on this one

this is the only reference to it which

is our private variable up and the

reason why this doesn't need to be used

in this case is because our parameter is

now called

value so it's saying the name is equal

to checking if the value is null or

empty assigning the value into name if

not and then if it is empty then we hide

invalid straight into name in the case

of our set name our name was our

parameter the same goes here if we

change this to Value we can change these

two for Value as you can see now this

dot is actually grade out and it can be

simplified to just simply name and the

same principle applies above so let's

comment these back out and scroll back

to the top so now that we've assigned

name there we go we've got our check

inside there and you can also leave this

on one line if you want you can even

drop the curly brace in just so it looks

like this instead of having it all on

one line so you could do this and this

would be applicable as well but the

second you start to put it all on one

line then it gets a little bit messy

quickly so if you leave the outer

bracket just like that then it can be

pretty short since you don't need more

curly braces so now that we've done that

for the name let's do it for the age as

well so we had this. age we check the

range and we assign the relevant values

so let's take that paste it over this we

don't need this dot anymore and age in

this case is actually going to be the

value gets passed

in to replace all the ages with value

there we go so if the value passes the

range check we put the value inside age

or not we can put minus one inside age

and again we can have the same thing we

can use our arrows inside here there we

go so now we've got our Cod drastically

reduced so we went from these big

functions that have to be defined with

specific names over to our properties

and they went from really small to just

a little bit bigger but now they have

advanced connections just to make sure

that the variables are okay before you

assign it in so now that we've got our

properties let's have a look so inside

our Constructor we don't also need to

use our local variables which is our

private ones directly we should actually

get rid of this now and we should always

do everything through our properties

name equals name and because this is a

capital and this is a lower case we

don't need this dot in

there there we

go so what you should do in the return

details function is also use the capital

n and the capital age so when you hover

over these you're actually referring to

the property because like I said before

these are the middleman to communicate

with the private variables you shouldn't

need to access the private variables

directly you should use the middle man

as much as possible so now that we've

done that I have one last thing to

introduce so if we actually abandon all

of this code just to keep it for

reference we have something called an

auto property and you briefly saw it

when we saw it down here so if we

actually just bring them back for a

second and if you hover over a name down

here it says string name get and set in

the curly brace but you see how the get

and set is empty it just has two values

inside there and it just ends a curly

brace with no actual logic you see in

our get we're saying age and our set

with saying age equals this so when

we're doing Auto properties what you can

do is you can actually make these public

again change these to a capital N

changes to a capital A and just straight

after it you can say Get Set just like

that and what that will do is although

they're actually made public what it's

doing behind the scenes is actually

doing this for you it actually has a

private and a public for you behind the

scenes this is just a Shand these are

what's called a auto property what it

does is it makes an anonymous property

behind the scenes as I'll show you in

Microsoft documentation right now as it

says here in c three or later Auto

implemented properties make property

declaration more concise when no

additional logic is required and what

that means is no additional logic all of

these are additional logic if you're

just simply assigning a value and

reading it back just like here then this

is no need to write get name and set

name if all you're doing is getting and

setting it if you're getting and setting

it but there is a certain amount of

logic as we have here we essentially

have a conditional operator that's

making a subconscious decision about

what to assign the name to but if you

just simply assign in something like

this then an auto property is perfect

they also enable client code to create

objects when you declare a property as

shown in the following example the

compiler creates a private Anonymous

backing field that can only only be

accessed to the properties get and

sets as you can see here just like in

our example we have public int something

and then get in set and then they have a

public in something with the get in set

as well and they can use it just here to

access it

directly and because they're simply just

assigning and getting the values and

there isn't any additional logic then

this is the best way to do it and you

get as it says here an anonymous private

backing field so this would be the same

thing as doing this private string name

and private int age so once you do that

and you make this into the get and set

properties which are these two right

here this and this line together make a

property what the auto implemented

property does is actually combine this

line and this line together and make the

private one behind the scenes so the

programmer doesn't see it but it will be

applicable when the program runs in the

compiler and then you only get publicly

exposed with this parameter which will

be our name property so it will actually

make the private for you if you just

keep it like we had it a second

ago just like this so if you leave it

like this it will actually create the

private variable behind the scenes as it

says here it's Anonymous backing field

and it can only be accessed through name

and Edge and as you can see none of our

code has actually changed all of these

still function okay and if we run the

code we should still get Harry and

25 so just to sum this video up since

there was a lot covered the bottom line

is if you're going to have any sort of

values that require any logic then you

should use normal properties if you're

simply just going to assign and get the

value and it doesn't matter and you have

no control over it then you can use the

auto implemented properties since that

will do the same thing with less code

using the gets and sets is the old way

of doing this and you should always use

the properties going forward gets and

Setters are very helpful to know just so

you understand what they are and why

they exist but you should note that

properties are the best way forward

properties give you access to doing the

same logic as it says inside here if you

do an auto implemented properties then

you're not allowed to use the logic

inside the set or the get so if you

don't want to do any logic then don't

use the auto implemented properties only

use the normal properties to make a get

and Setter you just do set name get the

parameter and assign the local variable

and the get just returns it getting back

a property you want to capitalize your

private variable if you had private

string name with a lowercase n you want

public string name with a capital N you

get the name and then you can set the

name with some sort of conditional

operator or any if statement or anything

inside here and the same thing goes with

the age you can have a public int age

with a capital A and A Private in age

with a lowas A you can get the age and

then set the age with a conditional

operator again or any other code if you

don't require any logic like these two

lines indicate then you can simply just

just use the auto properties as

demonstrated above here and they can be

used quite easily throughout the

Constructor and our functions and even

outside the function when the user is

trying to call

them but one thing that we haven't been

able to solve and you may not have

noticed this as a nuisance the problem

is when you try to return details about

something we're having to call this

function every single time if we have an

in age for example made that equal to 23

and we did console right line and simply

placed age inside the brackets we don't

have to do anything like this because it

simply just prints the value out if we

run the code you'll see that it simply

just says 23 this should be pretty

simple for our person as well but it's

not we need to call return details every

single time we want to print the

properties and if we don't do this the

only way to do it is by making some

string interpolation and we can say

person.

name and then also include person. AG

Etc and then that can print out to the

screen just like that

of course it doesn't have the same

format in but we can also just take this

place it straight inside here add our

person dot before them person Dot and

there you go then you get exactly the

same output as Callum return details but

of course you don't want to do this

every time and you certainly don't want

to have to type in return details

function every single time as well you

want to keep it as just person now of

course this is not going to work

Straight Out of the Box let's try run

that and see what happens so we get

string override person and what's going

on here is it's saying names space is

string override and then it's a class

person so it's just printing out this

person object but it's not really doing

anything because we haven't really told

it to do something so what we can do

here is we can actually override the two

string method so let me explain so let

me set up a couple of examples just to

show you what's going to happen at the

end so right now we call return details

to return these details what we can also

do is say dot two string and then we can

also do these three options return the

details convert it to a string or just

print the person straight to the console

at the end of this little section we're

going to make all three of these print

out the same values so let's have a look

now two string and printing out normally

these two lines right here are actually

doing exactly the same thing behind the

scenes when you place anything inside

these brackets inside a console right

line it will try and convert it to a

string anywhere so whether you put two

string or you leave it as person the

console right line function will still

accept it so just coming out the age for

the time being in order to make the two

string function work as well as just

placing person inside the brackets we

need to make a special function which is

an override function so let's have a

look so just underneath our return

details still within the scope of the

class we say public and then override

after we press tab we can follow it by a

space and we can see the functions that

we can override so in this video we're

actually going to cover two string and

do equals get hash code will actually

serve as a default hash function if I

show you the the documentation it says

here a hash code is numeric value that

is used to insert and identify an object

in a hash based collection this is

normally use when you're doing hash

functions as it says here and for the

time being we're not actually going to

be using this right now so the two we

can do a two string and equals but as

you can see our two string is available

right now we'll cover equals as an

extension in a minute so let's double

click on our two string and we get this

little template placed in if you don't

want the template then it's just an

override if returns back a string since

it's converting to string it needs to be

this exact notation if you drop the T

then it's got nothing to override now

you go be a compile error because

there's nothing to override itself so

play two string in the capitals there's

no parameters passed in and then we're

going to be returning something so the

thing that we're going to be returning

is exactly the same as our return

details function we actually have two

options here we could say return and

then return details so what that will do

is it will go to this function it will

call this function grab that and

essentially place inside here and then

return it back to the console in the

ideal world we don't actually need this

return details function so we could

comment it out but I'm going to leave it

in just so the demonstration doesn't

break down here so we can take this and

place it straight inside here now we

have two functions that essentially do

the same thing but the advantage is this

function is something that needs to be

called explicitly do return details

whereas this function the two string

override actually just gets called

naturally without having to add any

extensions so if we run the code now

you'll see that now they all equal

exactly the same value we get three

sections that have this maybe we can

place back SLT instead just so we can

have them all in the same

line there we go so you can see all

three are exactly the same so what we

can do now is if we comment this option

and we comment out this function we

don't have to include two string if we

don't want to we can also comment this

one out and we can keep it just as

person so as we do when we declare the

integer we say integ equal 23 and then

we simply print out and this is exactly

the notation that we want it should

really be this simple there you go one

line creates the person and one line

with just a variable name goes inside

the brackets to print them out the same

thing here person person Edge and Edge

and let's run that code now and there

you go you get the person printed and

the edge printed exactly and

straightforward as you may think it

should be so now that we've done that

let's explore what the equals function

needs to be done let's make another

person we can say person test equals new

person and we can have hurry age

23 then we don't need to print them out

in this example we just need to

demonstrate this equals function so if

you remember with strings we can have a

string called abber for example and we

can say do equals straight after it we

can say if ABA is equal to hurry then we

can do something inside here what I'm

trying to demonstrate is once you've got

an object you can place do equals and

then you can compare it like that so you

can also do that without classes so we

can say person do equals and then equals

to test and in here we could just say

same let's try and make these as same as

possible and see what happens so let's

keep the name exactly the same just like

that and let's copy the 25 down just

like that so now these two are

instantiated exactly the same way let's

see what happens when we've run the

equals then we don't get anything

printed which means they're not the same

so because this doesn't automatically

tell us that these two are exactly the

same we can override that and provide

our own functionality just like we did

with the operate overload we override

equals we get passed in an object and we

need to check something inside it so

let's see what we can do inside here so

in order to check if something is equal

to another thing what can we do so we

have a name and age if this one is the

same as this and this is the same as

this then two people are exactly the

same so let's try that out and see what

happens so we can say name which is the

property inside the class that we're

currently inside

and then we can use equals because this

is just a string parameter and then we

can say that equals to

objectname but you can see we've got a

little problem here this object is very

generic it's just an object every class

that you create is derived by an object

what that means is every class or

integer or double at the end of the day

is also just known as an object through

inheritance which we'll discuss further

on this course but just know that

everything inside C is always an object

object somewhere down the line so what

that means is we have to convert it from

an object to our custom class which is

person in this case so we can use this

thing called casting so we can say

person person which is just to find a

new variable and we can make that

immediately equal to object as

person so what we've done here is we've

said our object which is this parameter

is currently an object right now we want

to cast it as a person which is kind of

like converting into a person and then

play placing the values inside here so

now if we get rid of this comment and

instead of using object we actually have

a new local variable called person so

then here we can say person. name there

we go now we have access to the person

property since we've converted this

object into a person and of course we

know that we have name and age

available so if the name is equal to the

personname and the age is equal to the

person.

AG then we must have a match and of

course want to keep using the properties

we're going to use a capital A and there

we go so we run the conversion on one

line and then we check if they're equal

to each other now in this specific case

you can actually pass anything inside

here so let's try it we can pass Five

inside here and as you can see there's

no compile error cuz like I said five is

just an integer which is an object at

the end of the day if we make it a

double that works as well there is

literally no compile errors and even if

you put M to make it into a decimal

again there's no compile errors because

they're all object at the end of the day

so let's pass test in there what you

will have to know is if you do

accidentally pass in a double for

example like this you're going to hit an

error here which will be a runtime error

it will try and convert it as soon as it

hits this line and obviously you can't

convert a double into our person so what

you could do just to wrap it around you

could say if the object is a person then

we can proceed with a conversion just

like this and then what you can do for

safety is just outside of this you can

actually just return back false just so

the user knows that this isn't equal to

each other so if the object is a person

then we convert it as a person is is

used for checking and as is used for

converting is this object a person and

if it is then we try and convert it and

then return back if they're equal to

each other and if we accidentally pass

something in that's not a person then we

just return back false so let's just

demonstrate this

first not the same so let's just save

five inside here it says not the same so

let's actually go into this place a

break point and let's run it

again hover over object then we get

five if we place this straight inside

the watch you can see object is person

is returning back false so it's skipping

this entire function and going into

return false and then it's returning

back false so we get not same let's stop

the program change this over to test and

rerun it using F5 and now we can see

object is person is is actually set to

true so now if you put the object inside

here you can see that generically it's

an object right here it says object so

what we can do now is we can place

person down here as well and you see

that person doesn't exist but if you

just give it a second and we change this

to just the

P there we go so obviously the person

doesn't exist cuz we're only up to this

line we press F10 and F10 again so now

it's created the variable and f101 so

skip over it and now you can see the

object has went from type object to type

string. override. person string override

is the name of our namespace and do

person with a capital P is the name of

our class now you can see that the

object and the values are exactly the

same 25 ab and 25 ABA as you can see

right here so now both of them are

exactly equal to each other so when we

check this name here is Aba name here is

ABBA age 25 and age is 25 so this must

return back true and we can check that

by highlighting everything dragging and

dropping it down here and we can see it

says true there so when it returns back

to our code it's going to go and print

their

same perfect there you go so that's it

for this video but let's just have a

recap before we wrap up so before this

video we'd always print the details out

like this person do return details

obviously we don't have that function

anymore so let's just bring it back

quickly there we go so we use return

details and this is just a bit long have

to do this every single time if if you

want to print the details of the test

object as well you have to do test Dot

and then return details again it'd be so

much easier if we could simply just use

it like this as you would do with a

normal variable like an integer double

string Etc and now it's made possible by

using our public override string two

string function it will contain exactly

the same contents as our previous

function except we won't have to go

through an additional function just to

print out the details we can override

the two string and then we can call it

straight in the console like this and

finally when you're doing equals inside

an object you can actually build in some

custom functionality we can convert the

person using object as person but first

before doing the conversion you want to

check if the object is indeed a person

type before doing the conversion this

will give you a runtime error on this

line so let me just demonstrate that

before we continue so if I comment out

this one and actually just change this

into

5D and then let's press into the chorde

right

here it's going to hit this line if I

press F10 then it's going to try and

convert it but you're going to get back

null and then if you run the code again

then you're going to exception thrown

here you're going to get a runtime

exception because it's a null reference

we're trying to run the equals function

on something that is null which is right

here null reference exception so you

shouldn't do this you should also make

sure that these are correct values

before doing it this is a form of error

checking if you don't do this then

you're going to have to wrap this inside

a TR catch and it will look something

like this and and the cord will be

considerably longer you can place these

two

lines inside here and then place your

cat inside here etc etc and you can

return false inside here but as you can

see the code is a bit more complicated

now and there's no reason for this extra

complication if you can prevent it with

an if statement so let's undo

that so what's best in this case is to

say if the object is of type person then

we can run the conversion if we run the

conversion and it's not of type person

then as you can see it's going to return

back null for the person and then when

you run this line it's actually going to

return you back a null reference

exception at this point but anyway the

whole point of this equals is to check

if two objects are going to be equal to

each other so we keep this back to test

because this is a custom classes you

can't just simply do person equals

equals test and assume that that will

just work these aren't buil-in

functionalities for custom classes we

covered operate overloading where you

can actually overload what you do with

the equal sign but we didn't cover the

do equals which is what we're doing

today in this video as well as the two

string this is a different kind of

overriding because in the other one when

we're doing operator overloading that's

specifically an operator that you're

just changing the parameters of whereas

this one is a strict override of a

function equals and the functions two

string this will make it easier to print

your custom classes out to the screen as

well as comparing them just like this

person. equals putting test inside the

bracket is a lot easier and quicker and

you can build in your own custom

functionality if you want only had the

age in there then you can compare the

age maybe you have a lot more properties

and you can always just chain them

inside a big if statement that's fine as

well but the point is you have full

control over how you compare your custom

classes maybe you had a list of names

inside a dictionary for example and you

wanted to add a new name or a new age or

a new user to this dictionary but first

before you add a new user to the

dictionary you want to check if the user

already exists so maybe you could have a

system where it uses the equals function

and it Loops through all of the

dictionary items and it checks if all

the values are not equal to this one if

you didn't build in this functionality

so you could do the equals function then

you won't be able to determine if this

one is equal to this one and now with

your equals function overrided you can

now have the functionality to do so

thank you for watching this C course I

hope you found it helpful in your

journey to becoming a proficient C

developer if you have any questions or

need further clarification on any topic

we have covered please don't hesitate to

leave a comment below and I will do my

best to assist you for those of you who

want to dive deeper in a cop consider

checking out my udemy course you'll find

more comprehensive lessons and Hands-On

exercises to enhance your skills

remember practice makes perfect keep

coding and exploring and I wish you all

the best on your programming Journey

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.