All language subtitles for Learn C# Programming – Full Course with Mini-Projects (English_ASR)
Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,080 --> 00:00:04,680
this comprehensive C course will take
2
00:00:02,200 --> 00:00:07,240
you from the basics of C programming to
3
00:00:04,680 --> 00:00:09,639
advanced concepts all while integrating
4
00:00:07,240 --> 00:00:12,200
exciting mini projects to solidify your
5
00:00:09,639 --> 00:00:14,080
learning this course offers a structured
6
00:00:12,200 --> 00:00:16,760
path through essential topics such as
7
00:00:14,080 --> 00:00:19,320
syntax object oriented programming data
8
00:00:16,760 --> 00:00:21,960
structures and more ABA teaches this
9
00:00:19,320 --> 00:00:24,199
course and he will help you understand C
10
00:00:21,960 --> 00:00:25,920
both theoretically and also gain
11
00:00:24,199 --> 00:00:28,560
practical experience in applying your
12
00:00:25,920 --> 00:00:31,160
knowledge to real world scenarios hey
13
00:00:28,560 --> 00:00:32,439
there free ABA here join me in Walking
14
00:00:31,160 --> 00:00:34,440
you through everything you need to get a
15
00:00:32,439 --> 00:00:35,760
setup with cop let's have a quick walk
16
00:00:34,440 --> 00:00:37,640
through the course contents to let you
17
00:00:35,760 --> 00:00:39,079
know what to install for you we'll start
18
00:00:37,640 --> 00:00:40,920
with installing the visual studio
19
00:00:39,079 --> 00:00:42,879
program required to develop C
20
00:00:40,920 --> 00:00:44,559
applications you'll learn about data
21
00:00:42,879 --> 00:00:46,399
types including integers strings and
22
00:00:44,559 --> 00:00:48,320
many more and how to manipulate them
23
00:00:46,399 --> 00:00:50,480
inside your code next we'll dive into
24
00:00:48,320 --> 00:00:51,960
control flow you'll discover how to use
25
00:00:50,480 --> 00:00:53,520
conditional statements and Loops to
26
00:00:51,960 --> 00:00:55,760
control the flow of your program's
27
00:00:53,520 --> 00:00:57,640
execution strings are essential in many
28
00:00:55,760 --> 00:00:59,960
applications you'll Master powerful
29
00:00:57,640 --> 00:01:01,280
string functions in C allowing you to
30
00:00:59,960 --> 00:01:03,359
manipulate and transform Text
31
00:01:01,280 --> 00:01:05,519
effortlessly data structures are crucial
32
00:01:03,359 --> 00:01:07,320
for organizing and managing data from
33
00:01:05,519 --> 00:01:09,240
arrays to collections you'll explore
34
00:01:07,320 --> 00:01:11,280
various data structures in C and learn
35
00:01:09,240 --> 00:01:13,159
how to use them effectively functions
36
00:01:11,280 --> 00:01:15,280
are the backbone of any program you'll
37
00:01:13,159 --> 00:01:17,080
uncover the power of functions in C
38
00:01:15,280 --> 00:01:19,439
including how to create and call them to
39
00:01:17,080 --> 00:01:21,360
organize your code no program is immune
40
00:01:19,439 --> 00:01:23,640
to errors you'll learn how to handle
41
00:01:21,360 --> 00:01:25,280
exceptions gracefully in C ensuring that
42
00:01:23,640 --> 00:01:27,720
your applications remain robust and
43
00:01:25,280 --> 00:01:29,560
reliable debugging is a crucial skill
44
00:01:27,720 --> 00:01:31,479
for any developer you'll discover how to
45
00:01:29,560 --> 00:01:34,159
use visual studios's powerful debugging
46
00:01:31,479 --> 00:01:36,360
tools to identify and fix bugs in your C
47
00:01:34,159 --> 00:01:38,320
code efficiently finally we'll delve
48
00:01:36,360 --> 00:01:40,040
into object-oriented programming you'll
49
00:01:38,320 --> 00:01:42,240
learn the principles of object-oriented
50
00:01:40,040 --> 00:01:44,439
programming how to use classes objects
51
00:01:42,240 --> 00:01:46,880
inheritance and polymorphism to create
52
00:01:44,439 --> 00:01:48,640
modular and maintainable code by the end
53
00:01:46,880 --> 00:01:50,320
of this course you'll have a solid
54
00:01:48,640 --> 00:01:52,079
understanding of C programming and be
55
00:01:50,320 --> 00:01:54,079
well equipped to tackle a wide range of
56
00:01:52,079 --> 00:01:56,640
software development challenges thanks
57
00:01:54,079 --> 00:01:58,759
for tuning in let's get
58
00:01:56,640 --> 00:02:00,399
started so in order to start developing
59
00:01:58,759 --> 00:02:02,200
with C we need to install the latest
60
00:02:00,399 --> 00:02:04,600
version of visual studio in this case
61
00:02:02,200 --> 00:02:07,000
it's currently 2022 so let's head over
62
00:02:04,600 --> 00:02:09,080
Google and type in Visual Studio 2022
63
00:02:07,000 --> 00:02:11,560
download we will be presented with two
64
00:02:09,080 --> 00:02:12,920
links we have this one and this one we
65
00:02:11,560 --> 00:02:14,959
want to use a second one which says
66
00:02:12,920 --> 00:02:16,360
download visual studio tools so if you
67
00:02:14,959 --> 00:02:18,440
press on that and now will present it
68
00:02:16,360 --> 00:02:20,239
with the download area we have three
69
00:02:18,440 --> 00:02:22,160
additions Community professional
70
00:02:20,239 --> 00:02:24,080
Enterprise Community is free for
71
00:02:22,160 --> 00:02:25,680
students open source contributors and
72
00:02:24,080 --> 00:02:27,599
individuals which is exactly what we
73
00:02:25,680 --> 00:02:29,440
need for the content of this course we
74
00:02:27,599 --> 00:02:31,319
have the professional Enterprise Edition
75
00:02:29,440 --> 00:02:33,680
which paed for and only used by bigger
76
00:02:31,319 --> 00:02:35,440
companies or smaller teams so let's go
77
00:02:33,680 --> 00:02:37,040
ahead and press free download the
78
00:02:35,440 --> 00:02:39,280
download will start straight
79
00:02:37,040 --> 00:02:41,360
away after the download is complete
80
00:02:39,280 --> 00:02:43,760
let's start the installer so double
81
00:02:41,360 --> 00:02:45,920
click on the executable and press yes to
82
00:02:43,760 --> 00:02:47,440
allow it for admin permissions it'll pop
83
00:02:45,920 --> 00:02:48,760
up like this Visual Studio installer
84
00:02:47,440 --> 00:02:50,120
before you get started we need to set up
85
00:02:48,760 --> 00:02:52,080
a few things you can configure your
86
00:02:50,120 --> 00:02:53,480
installation you can read the privacy
87
00:02:52,080 --> 00:02:55,920
policy in the software terms if you'd
88
00:02:53,480 --> 00:02:58,560
like or you can just press continue so
89
00:02:55,920 --> 00:03:00,400
it's getting the visual studio installed
90
00:02:58,560 --> 00:03:01,920
already so so when you get to this
91
00:03:00,400 --> 00:03:03,599
section everything here is all the
92
00:03:01,920 --> 00:03:06,440
different packages and tools you can
93
00:03:03,599 --> 00:03:07,799
install within Visual Studio 2022 the
94
00:03:06,440 --> 00:03:09,920
main one that we're concerned within the
95
00:03:07,799 --> 00:03:11,360
content to this CA is the neck desktop
96
00:03:09,920 --> 00:03:13,560
environment so you can go ahead and
97
00:03:11,360 --> 00:03:15,000
press this one it will then tell you in
98
00:03:13,560 --> 00:03:18,799
the bottom right hand corner the total
99
00:03:15,000 --> 00:03:20,120
space required is 6.52 GB please make
100
00:03:18,799 --> 00:03:21,640
sure you have enough storage space
101
00:03:20,120 --> 00:03:23,480
available for this and then you can go
102
00:03:21,640 --> 00:03:24,959
ahead and press install if you want to
103
00:03:23,480 --> 00:03:26,519
check out other packages you can have a
104
00:03:24,959 --> 00:03:28,280
scroll through and read any other
105
00:03:26,519 --> 00:03:30,120
packages you may need and may want to
106
00:03:28,280 --> 00:03:31,680
install like I said the the only one
107
00:03:30,120 --> 00:03:32,640
required for this course is the net
108
00:03:31,680 --> 00:03:34,920
desktop
109
00:03:32,640 --> 00:03:36,959
environment you can also deal with noj
110
00:03:34,920 --> 00:03:38,720
development python development web
111
00:03:36,959 --> 00:03:40,319
development if you need to deal with
112
00:03:38,720 --> 00:03:42,599
individual components like specific
113
00:03:40,319 --> 00:03:44,200
versions you can access it here if you
114
00:03:42,599 --> 00:03:45,599
need some more languages installed you
115
00:03:44,200 --> 00:03:47,120
can access it here this should
116
00:03:45,599 --> 00:03:48,560
automatically select your default
117
00:03:47,120 --> 00:03:51,200
language for you that's based on your
118
00:03:48,560 --> 00:03:52,760
systems language and if you want it to
119
00:03:51,200 --> 00:03:54,519
you can also change the installation
120
00:03:52,760 --> 00:03:56,120
location in case you want to install it
121
00:03:54,519 --> 00:03:58,200
on a different drive or you just want it
122
00:03:56,120 --> 00:04:00,239
in a different folder so now that we
123
00:03:58,200 --> 00:04:01,319
selected the neck desktop environment
124
00:04:00,239 --> 00:04:03,200
we can go ahead and press the
125
00:04:01,319 --> 00:04:05,159
installation button we do have two
126
00:04:03,200 --> 00:04:06,920
options down here we have it install
127
00:04:05,159 --> 00:04:09,079
while downloading and a download all
128
00:04:06,920 --> 00:04:10,799
then install if you have a reasonably
129
00:04:09,079 --> 00:04:12,480
fast computer you can stick with the
130
00:04:10,799 --> 00:04:14,360
install while downloading and it will do
131
00:04:12,480 --> 00:04:16,160
the installation and the downloading at
132
00:04:14,360 --> 00:04:17,560
the same time if you have a slow
133
00:04:16,160 --> 00:04:19,440
computer it's preferred that you use
134
00:04:17,560 --> 00:04:21,280
this option to download it first and
135
00:04:19,440 --> 00:04:23,840
then install it just means that it won't
136
00:04:21,280 --> 00:04:26,240
slow down your computer too much so you
137
00:04:23,840 --> 00:04:28,800
can go for this one and press install
138
00:04:26,240 --> 00:04:29,960
now the installation will begin so now
139
00:04:28,800 --> 00:04:31,880
that Visual Studio is finished finish
140
00:04:29,960 --> 00:04:33,360
installing it will say done installing
141
00:04:31,880 --> 00:04:35,320
Visual Studio has been successfully
142
00:04:33,360 --> 00:04:37,160
installed we recommend rebooting soon to
143
00:04:35,320 --> 00:04:38,759
clean up any remaining files so this
144
00:04:37,160 --> 00:04:40,520
just means restart on your computer just
145
00:04:38,759 --> 00:04:41,960
so it has a chance to clean up any files
146
00:04:40,520 --> 00:04:45,240
that were kept during the installation
147
00:04:41,960 --> 00:04:46,680
process so we can just press okay if you
148
00:04:45,240 --> 00:04:48,400
have any other the versions of Visual
149
00:04:46,680 --> 00:04:50,440
Studio installed they'll also show up
150
00:04:48,400 --> 00:04:52,400
down here and now that Visual Studio
151
00:04:50,440 --> 00:04:54,600
Community 2022 is installed we can just
152
00:04:52,400 --> 00:04:54,600
press
153
00:04:55,320 --> 00:04:59,160
launch so now it'll prompt you to sign
154
00:04:57,400 --> 00:05:00,680
into Visual Studio we should definitely
155
00:04:59,160 --> 00:05:02,560
sign in but just for now I'm going to
156
00:05:00,680 --> 00:05:04,160
skip but feel free to create an account
157
00:05:02,560 --> 00:05:06,280
or sign in if you already have one
158
00:05:04,160 --> 00:05:08,199
existing we can skip this and then we
159
00:05:06,280 --> 00:05:09,639
get to choose your theme I personally
160
00:05:08,199 --> 00:05:11,800
prefer the Dark theme but you can go
161
00:05:09,639 --> 00:05:13,840
with blue blue extra contrast or the
162
00:05:11,800 --> 00:05:16,080
light mode so let's select dark and hit
163
00:05:13,840 --> 00:05:17,440
start Visual Studio it will do a first
164
00:05:16,080 --> 00:05:19,880
time
165
00:05:17,440 --> 00:05:22,280
setup and it's already done and we're
166
00:05:19,880 --> 00:05:25,479
already inside Visual Studio 2022 so
167
00:05:22,280 --> 00:05:25,479
that's it for the visual studio
168
00:05:25,680 --> 00:05:30,080
installation so now that we got Visual
169
00:05:27,680 --> 00:05:31,960
Studio 2022 installed let's see how to
170
00:05:30,080 --> 00:05:34,240
create your first project so once you've
171
00:05:31,960 --> 00:05:36,199
opened Visual Studio 2022 you'll see
172
00:05:34,240 --> 00:05:37,919
this window popping up you can clone a
173
00:05:36,199 --> 00:05:39,960
repository from things like GitHub in as
174
00:05:37,919 --> 00:05:42,039
you devops or open a project that you
175
00:05:39,960 --> 00:05:43,960
already have or open a folder which may
176
00:05:42,039 --> 00:05:46,000
have projects inside or you can create
177
00:05:43,960 --> 00:05:47,400
one from scratch if you don't want to do
178
00:05:46,000 --> 00:05:49,160
any of these options you can just open
179
00:05:47,400 --> 00:05:51,280
the visual studio program just on its
180
00:05:49,160 --> 00:05:53,240
own by clicking continue without code so
181
00:05:51,280 --> 00:05:55,000
let's go down the create a project path
182
00:05:53,240 --> 00:05:56,400
so click on create a new project and
183
00:05:55,000 --> 00:05:58,319
we'll get a list of all these different
184
00:05:56,400 --> 00:05:59,840
projects inside our system to be able to
185
00:05:58,319 --> 00:06:01,680
use the ones we're going to be be
186
00:05:59,840 --> 00:06:04,120
focusing on on this course is going to
187
00:06:01,680 --> 00:06:06,319
be C in the languages dropdown box and
188
00:06:04,120 --> 00:06:08,759
the platform is going to be windows and
189
00:06:06,319 --> 00:06:10,680
the project type is going to be console
190
00:06:08,759 --> 00:06:12,479
and they have one that uses net core
191
00:06:10,680 --> 00:06:14,360
which is for Linux Mac and windows and
192
00:06:12,479 --> 00:06:16,560
one that uses the Net Framework which is
193
00:06:14,360 --> 00:06:18,639
Windows so we want to go for this one so
194
00:06:16,560 --> 00:06:21,479
press on this one and then press
195
00:06:18,639 --> 00:06:24,000
next here we have our project name our
196
00:06:21,479 --> 00:06:26,639
location and our console app so we can
197
00:06:24,000 --> 00:06:27,800
start with the project name and say my
198
00:06:26,639 --> 00:06:29,759
first
199
00:06:27,800 --> 00:06:31,280
project as you can see it'll start
200
00:06:29,759 --> 00:06:32,880
copying over to the solution name as
201
00:06:31,280 --> 00:06:34,759
well and it tells you where the project
202
00:06:32,880 --> 00:06:37,000
will be created into so I'll be creating
203
00:06:34,759 --> 00:06:39,080
a folder called my first project which
204
00:06:37,000 --> 00:06:40,880
is the solution name plus my first
205
00:06:39,080 --> 00:06:42,800
project which is the project name inside
206
00:06:40,880 --> 00:06:45,360
that folder if we change the solution
207
00:06:42,800 --> 00:06:46,960
name you'll see update you can see here
208
00:06:45,360 --> 00:06:49,000
now we have multiple T's because we
209
00:06:46,960 --> 00:06:51,240
appended the solution name the solution
210
00:06:49,000 --> 00:06:53,639
name is almost like a folder that holds
211
00:06:51,240 --> 00:06:55,800
lots of projects inside so to begin with
212
00:06:53,639 --> 00:06:57,599
we'll have one solution that has one
213
00:06:55,800 --> 00:06:59,039
project inside and then later on in the
214
00:06:57,599 --> 00:07:00,879
course if you feel like you want to add
215
00:06:59,039 --> 00:07:02,919
more projects to it you can just add
216
00:07:00,879 --> 00:07:05,599
more to this solution so let's get
217
00:07:02,919 --> 00:07:05,599
started and press
218
00:07:08,039 --> 00:07:12,440
create so now we've created our very
219
00:07:10,280 --> 00:07:15,400
first project and just to explain what's
220
00:07:12,440 --> 00:07:17,960
here we have a static main void which is
221
00:07:15,400 --> 00:07:19,759
the main entry point to our program if
222
00:07:17,960 --> 00:07:21,400
you do a lowercase M then the system is
223
00:07:19,759 --> 00:07:23,120
not going to recognize this main
224
00:07:21,400 --> 00:07:24,919
function because the down NET Framework
225
00:07:23,120 --> 00:07:27,840
requires this function to be found in
226
00:07:24,919 --> 00:07:29,520
order to run a c console app and you can
227
00:07:27,840 --> 00:07:31,400
see here in the errors the program
228
00:07:29,520 --> 00:07:33,840
program does not contain a static main
229
00:07:31,400 --> 00:07:35,720
function and you can see it's capital M
230
00:07:33,840 --> 00:07:37,680
and C programming language is K
231
00:07:35,720 --> 00:07:38,960
sensitive so make sure to include it so
232
00:07:37,680 --> 00:07:41,599
now you understand how to use Visual
233
00:07:38,960 --> 00:07:43,840
Studio 2022 to create a project and also
234
00:07:41,599 --> 00:07:45,199
know where the main entry point is in C
235
00:07:43,840 --> 00:07:48,039
let's get started with a Hello World
236
00:07:45,199 --> 00:07:50,560
example to kick off this
237
00:07:48,039 --> 00:07:52,240
course so as we know our main entry
238
00:07:50,560 --> 00:07:54,879
point to our application is the static
239
00:07:52,240 --> 00:07:56,840
void main if we go inside here and press
240
00:07:54,879 --> 00:07:59,199
enter then we can actually start writing
241
00:07:56,840 --> 00:08:00,639
some code inside here just a quick note
242
00:07:59,199 --> 00:08:03,759
before we
243
00:08:00,639 --> 00:08:06,159
continue static void main is inside the
244
00:08:03,759 --> 00:08:08,560
class program and the class program is
245
00:08:06,159 --> 00:08:10,159
inside the namespace hello world and the
246
00:08:08,560 --> 00:08:12,879
reason why you can tell this is because
247
00:08:10,159 --> 00:08:15,520
of the curly braces the curly braces of
248
00:08:12,879 --> 00:08:17,360
hello world begins here and ends here
249
00:08:15,520 --> 00:08:19,599
and Within These curly braces we have a
250
00:08:17,360 --> 00:08:22,520
class program and also within the class
251
00:08:19,599 --> 00:08:25,440
program we have the styo
252
00:08:22,520 --> 00:08:29,520
men so just between these curly braces
253
00:08:25,440 --> 00:08:31,199
we want to type console. right line or
254
00:08:29,520 --> 00:08:33,680
open the brackets and we can type in
255
00:08:31,199 --> 00:08:36,959
some speech marks and we can simply Type
256
00:08:33,680 --> 00:08:39,080
Hello World there you go so as you can
257
00:08:36,959 --> 00:08:42,320
see now system has actually been
258
00:08:39,080 --> 00:08:45,040
highlighted so if we just delete this
259
00:08:42,320 --> 00:08:47,000
line you can see now system has been gr
260
00:08:45,040 --> 00:08:50,480
out and the reason why is it says it's
261
00:08:47,000 --> 00:08:50,480
unnecessary because it hasn't been
262
00:08:50,720 --> 00:08:57,880
used so if you undo it then this comes
263
00:08:55,560 --> 00:08:59,760
up because I showed you before that
264
00:08:57,880 --> 00:09:01,680
console right line is actually within
265
00:08:59,760 --> 00:09:02,839
the system Library what happens if we
266
00:09:01,680 --> 00:09:05,320
delete
267
00:09:02,839 --> 00:09:07,600
it if we delete it then it doesn't know
268
00:09:05,320 --> 00:09:09,480
where console exists from and because
269
00:09:07,600 --> 00:09:12,720
that just said system we can actually
270
00:09:09,480 --> 00:09:14,800
just use system. console. right line and
271
00:09:12,720 --> 00:09:17,560
then open the brackets and type in hello
272
00:09:14,800 --> 00:09:19,120
world and now this works again and the
273
00:09:17,560 --> 00:09:21,320
reason why this works it's because we
274
00:09:19,120 --> 00:09:24,640
don't have to Define that we're using
275
00:09:21,320 --> 00:09:27,440
system we could just use it straight in
276
00:09:24,640 --> 00:09:29,000
here if you have multiple lines like
277
00:09:27,440 --> 00:09:31,360
this then you don't want to be using
278
00:09:29,000 --> 00:09:32,760
system right at the start ideally you
279
00:09:31,360 --> 00:09:35,240
want to have the reference to it at the
280
00:09:32,760 --> 00:09:37,360
top of the screen and then use it
281
00:09:35,240 --> 00:09:39,240
throughout so now that we've included
282
00:09:37,360 --> 00:09:41,160
back in the project you can see that now
283
00:09:39,240 --> 00:09:42,920
these have turned gray and it says that
284
00:09:41,160 --> 00:09:44,720
it can be simplified if we press the
285
00:09:42,920 --> 00:09:47,079
little light bulb and we can say
286
00:09:44,720 --> 00:09:48,279
simplify member and it doesn't really
287
00:09:47,079 --> 00:09:51,079
matter because we can just delete the
288
00:09:48,279 --> 00:09:53,279
rest now so now that we've WR hello
289
00:09:51,079 --> 00:09:55,279
world let's press F5 which is a shortcut
290
00:09:53,279 --> 00:09:58,399
to the start
291
00:09:55,279 --> 00:09:59,880
button as you can see well you couldn't
292
00:09:58,399 --> 00:10:02,200
really see because the program closed
293
00:09:59,880 --> 00:10:04,320
too quickly and the reason for that is
294
00:10:02,200 --> 00:10:05,720
printed hello world to the console and
295
00:10:04,320 --> 00:10:07,399
then it didn't do anything else cuz
296
00:10:05,720 --> 00:10:08,880
there's nothing else for it to do so the
297
00:10:07,399 --> 00:10:12,040
console simply
298
00:10:08,880 --> 00:10:14,480
closed we can use another command called
299
00:10:12,040 --> 00:10:16,200
console read Line open the brackets and
300
00:10:14,480 --> 00:10:18,800
close the brackets and have a semicolon
301
00:10:16,200 --> 00:10:21,079
at the end all this does is it waits
302
00:10:18,800 --> 00:10:22,720
here until the user inputs something
303
00:10:21,079 --> 00:10:25,519
reads the next line of characters from
304
00:10:22,720 --> 00:10:25,519
the standard input
305
00:10:25,600 --> 00:10:29,640
stream so although the program will
306
00:10:27,680 --> 00:10:31,680
actually be reading stuff we just want
307
00:10:29,640 --> 00:10:33,760
this line to stop the application from
308
00:10:31,680 --> 00:10:35,560
closing so now if you press F5 and run
309
00:10:33,760 --> 00:10:38,959
the program you can see it actually says
310
00:10:35,560 --> 00:10:41,200
hello world and it's just waiting here
311
00:10:38,959 --> 00:10:42,959
flashing and this flashing means it just
312
00:10:41,200 --> 00:10:44,760
started this console read line so it
313
00:10:42,959 --> 00:10:46,920
doesn't matter what we type into here it
314
00:10:44,760 --> 00:10:48,440
just WS for us to press enter and it
315
00:10:46,920 --> 00:10:50,680
soon as we press enter this line
316
00:10:48,440 --> 00:10:52,279
executes and the program finishes
317
00:10:50,680 --> 00:10:55,200
congratulations you just wrote your
318
00:10:52,279 --> 00:10:56,880
first C program welcome to the amazing
319
00:10:55,200 --> 00:10:58,959
journey of
320
00:10:56,880 --> 00:11:01,600
programming so let's look at how to make
321
00:10:58,959 --> 00:11:04,360
VAR Ables in our C code let's start
322
00:11:01,600 --> 00:11:06,120
every code with a console read line and
323
00:11:04,360 --> 00:11:07,920
then we can hit F5 just to make sure our
324
00:11:06,120 --> 00:11:11,200
console doesn't
325
00:11:07,920 --> 00:11:13,240
close so let's begin with an integer so
326
00:11:11,200 --> 00:11:15,639
integers are whole numbers so we can
327
00:11:13,240 --> 00:11:18,440
just say int age equals
328
00:11:15,639 --> 00:11:20,639
23 so what we've actually done here is
329
00:11:18,440 --> 00:11:25,160
we've said we want an INT we want to
330
00:11:20,639 --> 00:11:27,600
call it age already made it equal to 23
331
00:11:25,160 --> 00:11:30,040
this is called initializing and
332
00:11:27,600 --> 00:11:31,600
declaring in the same line
333
00:11:30,040 --> 00:11:33,519
so what you could do alternatively is
334
00:11:31,600 --> 00:11:36,000
you can end the semicolon here and it
335
00:11:33,519 --> 00:11:38,880
just says int Edge and then on the next
336
00:11:36,000 --> 00:11:40,760
line you can write ede equals 23 so this
337
00:11:38,880 --> 00:11:42,399
is declaring it in the first line and
338
00:11:40,760 --> 00:11:44,279
then give it an initial value in the
339
00:11:42,399 --> 00:11:46,120
second line if they're right underneath
340
00:11:44,279 --> 00:11:47,959
each other like this case then you may
341
00:11:46,120 --> 00:11:49,600
as well just make it together because
342
00:11:47,959 --> 00:11:50,920
then you have less lines of code the
343
00:11:49,600 --> 00:11:52,480
reason why it says it's a green under
344
00:11:50,920 --> 00:11:54,040
line it's because it's the unnecessary
345
00:11:52,480 --> 00:11:56,399
assignment because we're not actually
346
00:11:54,040 --> 00:11:58,760
using ede so let's just point age to the
347
00:11:56,399 --> 00:12:00,959
console if we make some new lines and we
348
00:11:58,760 --> 00:12:03,560
can type in in console. right line and
349
00:12:00,959 --> 00:12:06,720
then press tab to finish it we can write
350
00:12:03,560 --> 00:12:09,480
Edge and if we run our code using F5 we
351
00:12:06,720 --> 00:12:11,519
can see that 23 gets print out to the
352
00:12:09,480 --> 00:12:14,279
screen so now that we've worked on the
353
00:12:11,519 --> 00:12:16,160
Edge Let's explore all data types let's
354
00:12:14,279 --> 00:12:18,800
define a long variable so let's start
355
00:12:16,160 --> 00:12:20,680
with the type long followed by space and
356
00:12:18,800 --> 00:12:23,240
then the variable name that we want and
357
00:12:20,680 --> 00:12:25,600
let's add equals and type in a very big
358
00:12:23,240 --> 00:12:29,440
number so now if we hover over it it
359
00:12:25,600 --> 00:12:31,639
says it's a system int32 and int32 is
360
00:12:29,440 --> 00:12:34,320
actually an integer but we're telling it
361
00:12:31,639 --> 00:12:36,920
it's a long which makes no sense we have
362
00:12:34,320 --> 00:12:40,199
an in 64 here but when we hover over the
363
00:12:36,920 --> 00:12:42,839
number it comes up as n32 and the reason
364
00:12:40,199 --> 00:12:44,519
for that is when C sees that you just
365
00:12:42,839 --> 00:12:47,320
have a number typed in like this you
366
00:12:44,519 --> 00:12:49,160
will always assume that it is an integer
367
00:12:47,320 --> 00:12:50,800
to tell the compiler that you indeed
368
00:12:49,160 --> 00:12:52,880
want this to be processed as a long
369
00:12:50,800 --> 00:12:54,920
number then you can put a capital l
370
00:12:52,880 --> 00:12:56,240
right at the end now if we hover over it
371
00:12:54,920 --> 00:12:59,480
it says in
372
00:12:56,240 --> 00:13:01,760
64 and this says in 64 so now we've got
373
00:12:59,480 --> 00:13:03,600
it working and we've got the long there
374
00:13:01,760 --> 00:13:04,800
so now we can take another console right
375
00:13:03,600 --> 00:13:06,959
line and just print this out to the
376
00:13:04,800 --> 00:13:09,720
screen and just a helpful tip if you
377
00:13:06,959 --> 00:13:11,320
type in CW and press tab twice then you
378
00:13:09,720 --> 00:13:13,279
get the console right line it's very
379
00:13:11,320 --> 00:13:15,320
easy to write your code and then you can
380
00:13:13,279 --> 00:13:17,320
type in big followed by a tab and it'll
381
00:13:15,320 --> 00:13:18,399
come up straight away so as you can see
382
00:13:17,320 --> 00:13:19,440
they're both being printed to the
383
00:13:18,399 --> 00:13:21,839
console
384
00:13:19,440 --> 00:13:24,199
now so now that we've made positive
385
00:13:21,839 --> 00:13:26,079
numbers we can also make these negative
386
00:13:24,199 --> 00:13:29,199
by simply just putting a negative sign
387
00:13:26,079 --> 00:13:31,079
behind them there we go and just to to
388
00:13:29,199 --> 00:13:34,399
show you how big these numbers are what
389
00:13:31,079 --> 00:13:36,560
we can do we can type in int. max value
390
00:13:34,399 --> 00:13:38,680
and then make another one using contr D
391
00:13:36,560 --> 00:13:41,320
and type in int. Min
392
00:13:38,680 --> 00:13:46,560
value so you can see integers can be
393
00:13:41,320 --> 00:13:46,560
positive 2.1 billion or netive - 2.1
394
00:13:46,880 --> 00:13:50,839
billion and if we do the same for
395
00:13:52,600 --> 00:13:56,959
long you can see that long is a very
396
00:13:55,079 --> 00:13:59,199
very big number because instead of being
397
00:13:56,959 --> 00:14:02,079
an INT 32 it's an in 64 which which
398
00:13:59,199 --> 00:14:04,120
takes up a lot more memory so now we've
399
00:14:02,079 --> 00:14:07,320
covered whole numbers let's have a look
400
00:14:04,120 --> 00:14:09,560
at decimals so we can have a double
401
00:14:07,320 --> 00:14:10,959
negative for example and we can say it's
402
00:14:09,560 --> 00:14:13,720
-
403
00:14:10,959 --> 00:14:15,959
55.2 and in this case when you just have
404
00:14:13,720 --> 00:14:17,839
a number typed in like this and it's got
405
00:14:15,959 --> 00:14:20,560
a decimal point in it it will always
406
00:14:17,839 --> 00:14:22,399
register it as a double but just to make
407
00:14:20,560 --> 00:14:24,360
sure just as we're doing with the long
408
00:14:22,399 --> 00:14:25,399
you can actually just put a capital D at
409
00:14:24,360 --> 00:14:27,480
the
410
00:14:25,399 --> 00:14:30,519
end now if we print this out to the
411
00:14:27,480 --> 00:14:31,959
screen and also print out how big these
412
00:14:30,519 --> 00:14:36,160
numbers can
413
00:14:31,959 --> 00:14:39,399
be max value and Min value and print
414
00:14:36,160 --> 00:14:40,880
this out so we get our minus 55.2 and
415
00:14:39,399 --> 00:14:45,199
you can see that these numbers can be
416
00:14:40,880 --> 00:14:48,920
very big e+ 308 means it's this number
417
00:14:45,199 --> 00:14:51,480
times by 10 the^ of 308 which is a very
418
00:14:48,920 --> 00:14:53,839
big number and we also have that as a
419
00:14:51,480 --> 00:14:57,480
negative so we have float let's call
420
00:14:53,839 --> 00:15:00,240
this precision and make equal
421
00:14:57,480 --> 00:15:01,920
5.1 and then put a semic call the end
422
00:15:00,240 --> 00:15:03,720
now we've got a problem here this is
423
00:15:01,920 --> 00:15:05,040
trying to register as a double but we
424
00:15:03,720 --> 00:15:06,839
don't want to register a double we want
425
00:15:05,040 --> 00:15:09,160
to register as a float and this is the
426
00:15:06,839 --> 00:15:11,040
same issue we have the long so when you
427
00:15:09,160 --> 00:15:13,160
type any number that has a decimal point
428
00:15:11,040 --> 00:15:15,680
in it the C compiler will always
429
00:15:13,160 --> 00:15:17,279
register as a double the same way when
430
00:15:15,680 --> 00:15:19,680
you type in a whole number it'll always
431
00:15:17,279 --> 00:15:22,079
register as an INT so in this case we
432
00:15:19,680 --> 00:15:24,320
need to put a capital f at the end and
433
00:15:22,079 --> 00:15:27,440
it'll process this as you
434
00:15:24,320 --> 00:15:29,720
float so now we can print this
435
00:15:27,440 --> 00:15:33,079
out and then let's just print out the
436
00:15:29,720 --> 00:15:34,560
max values and the Min values just to
437
00:15:33,079 --> 00:15:36,800
show you what that looks
438
00:15:34,560 --> 00:15:38,880
like so as you can see here we have our
439
00:15:36,800 --> 00:15:42,160
number printed out and again we have
440
00:15:38,880 --> 00:15:44,560
quite a big number 3.4 * by 10 to the^
441
00:15:42,160 --> 00:15:46,959
of 38 so again it's a big number and we
442
00:15:44,560 --> 00:15:48,600
have that positive and negative so quite
443
00:15:46,959 --> 00:15:50,639
a big range of
444
00:15:48,600 --> 00:15:53,839
numbers and the last one we're going to
445
00:15:50,639 --> 00:15:55,560
cover is decimal for things like money
446
00:15:53,839 --> 00:15:58,440
and currency
447
00:15:55,560 --> 00:16:01,079
14.99 and again in this case it's trying
448
00:15:58,440 --> 00:16:03,160
to assign a double into a decimal so in
449
00:16:01,079 --> 00:16:05,680
order to denote what a decimal is you
450
00:16:03,160 --> 00:16:08,120
can put a capital M right at the
451
00:16:05,680 --> 00:16:09,680
end and for one last time let's print
452
00:16:08,120 --> 00:16:13,880
out the
453
00:16:09,680 --> 00:16:17,319
money and the decimal. max value contrl
454
00:16:13,880 --> 00:16:19,519
D and then we'll do Min
455
00:16:17,319 --> 00:16:21,319
value and you can see here that this is
456
00:16:19,519 --> 00:16:23,079
a really big number as well and it's
457
00:16:21,319 --> 00:16:25,519
even bigger than the
458
00:16:23,079 --> 00:16:27,880
long so we have our number printed and
459
00:16:25,519 --> 00:16:29,440
the Max and the minimum values so now
460
00:16:27,880 --> 00:16:31,040
that we've understood that how to
461
00:16:29,440 --> 00:16:33,519
declare a variable I have an extra
462
00:16:31,040 --> 00:16:37,399
little tip for you so if we had three
463
00:16:33,519 --> 00:16:39,399
variables int X an INT Y and an INT
464
00:16:37,399 --> 00:16:41,560
Z and let's say you're always going to
465
00:16:39,399 --> 00:16:44,519
declare them together like this you can
466
00:16:41,560 --> 00:16:47,519
actually miss out and do X comma y comma
467
00:16:44,519 --> 00:16:50,040
Z and that will do exactly the same
468
00:16:47,519 --> 00:16:52,360
thing there you go and that's really
469
00:16:50,040 --> 00:16:55,000
neat and it's a lot better let's say for
470
00:16:52,360 --> 00:16:56,680
example you had these variables but you
471
00:16:55,000 --> 00:17:01,759
actually had them set to something like
472
00:16:56,680 --> 00:17:03,560
equals 10 equals 20 equal 30 this is
473
00:17:01,759 --> 00:17:04,520
still practical but it might look a
474
00:17:03,560 --> 00:17:06,880
little
475
00:17:04,520 --> 00:17:09,199
uglier so you can actually type them in
476
00:17:06,880 --> 00:17:12,039
straight into here and if you comment
477
00:17:09,199 --> 00:17:13,799
these out again then it still works you
478
00:17:12,039 --> 00:17:16,880
can even put these onto new lines like
479
00:17:13,799 --> 00:17:18,439
this just so it looks a bit neater just
480
00:17:16,880 --> 00:17:20,280
prevents you from typing the additional
481
00:17:18,439 --> 00:17:22,439
int each time if they're all going to be
482
00:17:20,280 --> 00:17:24,559
the same variable type so that's it for
483
00:17:22,439 --> 00:17:27,319
the numbers inside our code so let's
484
00:17:24,559 --> 00:17:29,120
just have a quick recap so in order to
485
00:17:27,319 --> 00:17:31,480
Define a variable you want to use the
486
00:17:29,120 --> 00:17:33,720
data type in this case an integer
487
00:17:31,480 --> 00:17:35,360
followed by the variable name and then
488
00:17:33,720 --> 00:17:37,559
you can either add a semicolon at that
489
00:17:35,360 --> 00:17:40,120
point or you can add equals to give it a
490
00:17:37,559 --> 00:17:42,480
value this is just called declaring a
491
00:17:40,120 --> 00:17:44,559
value and this is called initializing a
492
00:17:42,480 --> 00:17:47,039
value if you want to decide you want to
493
00:17:44,559 --> 00:17:50,160
change the variable further down your
494
00:17:47,039 --> 00:17:52,080
program for example age equals 50 here
495
00:17:50,160 --> 00:17:53,559
then this is fine and this is now called
496
00:17:52,080 --> 00:17:56,280
an
497
00:17:53,559 --> 00:17:58,000
assignment it's called initialization
498
00:17:56,280 --> 00:18:00,440
because as soon as you make it you give
499
00:17:58,000 --> 00:18:02,520
it a value and then in later on in the
500
00:18:00,440 --> 00:18:05,000
code if you give it another value this
501
00:18:02,520 --> 00:18:08,480
is called simply assigning a value this
502
00:18:05,000 --> 00:18:08,480
is just called assigning it a
503
00:18:08,559 --> 00:18:13,000
value so now that we can store numbers
504
00:18:10,799 --> 00:18:15,200
in C let's see how we can store
505
00:18:13,000 --> 00:18:16,960
characters and letters so as you know
506
00:18:15,200 --> 00:18:19,039
from the introduction we have a string
507
00:18:16,960 --> 00:18:20,960
variable so let's call that name and
508
00:18:19,039 --> 00:18:23,000
make it equal to
509
00:18:20,960 --> 00:18:25,720
Aba please note that when you're
510
00:18:23,000 --> 00:18:28,080
defining a string and giving it a value
511
00:18:25,720 --> 00:18:30,320
that this value needs to be in speech
512
00:18:28,080 --> 00:18:32,640
marks if you put it in quotation marks
513
00:18:30,320 --> 00:18:35,159
it will not accept this because that's
514
00:18:32,640 --> 00:18:37,760
the notation to declare a Char which
515
00:18:35,159 --> 00:18:40,360
we'll get on to next so on the next line
516
00:18:37,760 --> 00:18:42,440
we can do a Char letter equals and we
517
00:18:40,360 --> 00:18:44,520
can't have something like this because
518
00:18:42,440 --> 00:18:46,720
the Char will only accept One Singular
519
00:18:44,520 --> 00:18:49,559
character so we have to wrap it around
520
00:18:46,720 --> 00:18:50,880
the quotation mark once you've wrapped
521
00:18:49,559 --> 00:18:52,840
it around the quotation mark then it
522
00:18:50,880 --> 00:18:56,400
will accept it so let's print these two
523
00:18:52,840 --> 00:18:58,799
to the screen so CW tab tab and we can
524
00:18:56,400 --> 00:19:00,320
just print these to the screen and if we
525
00:18:58,799 --> 00:19:03,440
just run the code you can see that we've
526
00:19:00,320 --> 00:19:05,960
got the values entered in
527
00:19:03,440 --> 00:19:08,840
here we can start to write
528
00:19:05,960 --> 00:19:11,320
stuff your name
529
00:19:08,840 --> 00:19:13,600
is and then we can output this and we
530
00:19:11,320 --> 00:19:16,280
can also put it on one
531
00:19:13,600 --> 00:19:18,360
line if we do a console right and a
532
00:19:16,280 --> 00:19:20,520
console right then maybe we can take the
533
00:19:18,360 --> 00:19:22,679
callon off and just have it space and we
534
00:19:20,520 --> 00:19:24,559
can say your name is ABBA and we' see
535
00:19:22,679 --> 00:19:26,240
that additional a because that's the
536
00:19:24,559 --> 00:19:28,159
right line for the letter so we can
537
00:19:26,240 --> 00:19:29,799
space it out with an empty console right
538
00:19:28,159 --> 00:19:32,159
line and and they'll add a gap between
539
00:19:29,799 --> 00:19:34,480
them both there we
540
00:19:32,159 --> 00:19:36,480
go so now we know how to declare a
541
00:19:34,480 --> 00:19:38,840
string variable and give it a value and
542
00:19:36,480 --> 00:19:41,280
we can declare a Char variable and also
543
00:19:38,840 --> 00:19:43,120
give it a value just a quick recap when
544
00:19:41,280 --> 00:19:45,159
you're declaring and initializing string
545
00:19:43,120 --> 00:19:47,039
variables you need to put the value in
546
00:19:45,159 --> 00:19:49,039
speech marks but when you're declaring
547
00:19:47,039 --> 00:19:50,520
and initializing a Char you don't have
548
00:19:49,039 --> 00:19:52,320
to put it in speech marks you put them
549
00:19:50,520 --> 00:19:53,760
in apostrophes whenever you see
550
00:19:52,320 --> 00:19:55,880
apostrophes that that can only ever
551
00:19:53,760 --> 00:19:57,880
refer to One Singular character when you
552
00:19:55,880 --> 00:19:59,600
see a speech mark it could be one or
553
00:19:57,880 --> 00:20:02,080
more characters
554
00:19:59,600 --> 00:20:04,320
also things can be empty this just means
555
00:20:02,080 --> 00:20:05,880
the string is empty this is also a valid
556
00:20:04,320 --> 00:20:08,200
character it just means that we're
557
00:20:05,880 --> 00:20:10,440
storing a string but it has no value
558
00:20:08,200 --> 00:20:11,960
with a Char that's not exactly the same
559
00:20:10,440 --> 00:20:14,240
you can't store an empty character
560
00:20:11,960 --> 00:20:17,080
literal it has to be given something
561
00:20:14,240 --> 00:20:19,000
Charles get defaulted at back sl0 which
562
00:20:17,080 --> 00:20:20,520
is just a default character but don't
563
00:20:19,000 --> 00:20:22,280
worry about that too much now we'll go
564
00:20:20,520 --> 00:20:24,240
into Escape characters which is the back
565
00:20:22,280 --> 00:20:26,039
SL layer on in the course for now just
566
00:20:24,240 --> 00:20:27,799
note that whenever you declare a string
567
00:20:26,039 --> 00:20:29,520
variable we need to put it in the speech
568
00:20:27,799 --> 00:20:32,360
marks and when we declaring a Char
569
00:20:29,520 --> 00:20:34,760
variable we need the quotation
570
00:20:32,360 --> 00:20:37,200
mark So now that we know how to define
571
00:20:34,760 --> 00:20:39,320
numbers in C and to also Define strings
572
00:20:37,200 --> 00:20:41,400
and characters then let's look at how to
573
00:20:39,320 --> 00:20:43,559
convert between them both so let's take
574
00:20:41,400 --> 00:20:45,840
some similar code from our first video
575
00:20:43,559 --> 00:20:47,799
and we can Define it age the number the
576
00:20:45,840 --> 00:20:49,360
negative the Precision and the money and
577
00:20:47,799 --> 00:20:51,919
we give it the same values from the
578
00:20:49,360 --> 00:20:53,320
first video we simply just print them
579
00:20:51,919 --> 00:20:55,480
out to the console here and if we just
580
00:20:53,320 --> 00:20:57,159
run we can see them all but this doesn't
581
00:20:55,480 --> 00:20:58,840
help us cuz we're just statically typing
582
00:20:57,159 --> 00:21:00,520
in the numbers what if these these will
583
00:20:58,840 --> 00:21:03,039
actually process a string and we want to
584
00:21:00,520 --> 00:21:04,200
convert them into an INT so just above
585
00:21:03,039 --> 00:21:06,240
each of the variables we're going to
586
00:21:04,200 --> 00:21:08,960
make another variable so we can say a
587
00:21:06,240 --> 00:21:11,080
string text age equals
588
00:21:08,960 --> 00:21:12,919
-23 and then for the age what we're
589
00:21:11,080 --> 00:21:16,559
going to use is this function called
590
00:21:12,919 --> 00:21:18,880
convert to int 32 and we can put in the
591
00:21:16,559 --> 00:21:21,039
string inside the brackets and what this
592
00:21:18,880 --> 00:21:24,200
is now saying is we're taking our string
593
00:21:21,039 --> 00:21:25,679
text Edge which is equal to -23 and
594
00:21:24,200 --> 00:21:28,200
we're passing it into this function
595
00:21:25,679 --> 00:21:31,640
convert to in32 and the reason why it's
596
00:21:28,200 --> 00:21:35,600
in 32 is because an INT is by definition
597
00:21:31,640 --> 00:21:37,720
an in32 which is a 32bit signed integer
598
00:21:35,600 --> 00:21:41,360
so what we want to do here is convert
599
00:21:37,720 --> 00:21:43,400
for our text age into int32 and store it
600
00:21:41,360 --> 00:21:46,520
back into our int and hopefully we
601
00:21:43,400 --> 00:21:49,640
should see the same value when we hit
602
00:21:46,520 --> 00:21:52,559
run 23 so now that's actually went from
603
00:21:49,640 --> 00:21:54,240
a string variable and then went into our
604
00:21:52,559 --> 00:21:56,159
integer and we've printed out as an
605
00:21:54,240 --> 00:21:58,360
integer and it's no longer a string
606
00:21:56,159 --> 00:22:01,360
variable now we can do the same for the
607
00:21:58,360 --> 00:22:04,240
other ones so we can have a string text
608
00:22:01,360 --> 00:22:07,080
big number and make it equal to this
609
00:22:04,240 --> 00:22:09,360
value just in the quotation
610
00:22:07,080 --> 00:22:11,240
marks now we're typing this value in the
611
00:22:09,360 --> 00:22:13,200
quotation marks we don't need to put the
612
00:22:11,240 --> 00:22:15,799
L at the end the L is just to tell the
613
00:22:13,200 --> 00:22:17,400
compiler that we want to use in 64 if we
614
00:22:15,799 --> 00:22:20,039
take the L away then it's going to think
615
00:22:17,400 --> 00:22:21,840
it wants in 32 this is not re applicable
616
00:22:20,039 --> 00:22:23,480
when it comes to text because we're
617
00:22:21,840 --> 00:22:26,320
going to explicitly stay on the next
618
00:22:23,480 --> 00:22:28,520
line we want to convert to N64 so the
619
00:22:26,320 --> 00:22:30,240
compiler already knows that it's in 64
620
00:22:28,520 --> 00:22:32,600
for and we don't need to put an L at the
621
00:22:30,240 --> 00:22:34,720
end now let's make sure this works and
622
00:22:32,600 --> 00:22:36,480
there we go so what we should end up
623
00:22:34,720 --> 00:22:38,279
with is the console should look exactly
624
00:22:36,480 --> 00:22:39,880
the same as how we had it at the start
625
00:22:38,279 --> 00:22:41,720
of the video but actually what we're
626
00:22:39,880 --> 00:22:46,640
doing is converting it from a string
627
00:22:41,720 --> 00:22:49,080
into the applicable value string text
628
00:22:46,640 --> 00:22:53,240
negative equal -
629
00:22:49,080 --> 00:22:56,520
55.2 and then in here we can do convert
630
00:22:53,240 --> 00:22:59,799
to double add the text negative inside
631
00:22:56,520 --> 00:23:01,640
here and let's just run it to check
632
00:22:59,799 --> 00:23:06,960
and we still have the same value so it's
633
00:23:01,640 --> 00:23:06,960
going well have a string text
634
00:23:07,400 --> 00:23:13,400
Precision make it equal to
635
00:23:10,760 --> 00:23:16,960
this and then instead of this we can
636
00:23:13,400 --> 00:23:18,720
have a convert two and in this case it's
637
00:23:16,960 --> 00:23:20,640
actually going to be a single and the
638
00:23:18,720 --> 00:23:22,679
reason why it's a single when you hover
639
00:23:20,640 --> 00:23:25,360
of a float the definition for a float is
640
00:23:22,679 --> 00:23:27,159
indeed a single so we can add that in
641
00:23:25,360 --> 00:23:28,720
text precision and we're just going to
642
00:23:27,159 --> 00:23:33,200
do the last one and just check it out Al
643
00:23:28,720 --> 00:23:35,440
together text money equals
644
00:23:33,200 --> 00:23:36,480
14.99 and then instead of this we can
645
00:23:35,440 --> 00:23:38,640
have a
646
00:23:36,480 --> 00:23:40,640
convert and let's have a look at this a
647
00:23:38,640 --> 00:23:43,039
decimal is indeed a decimal so we can
648
00:23:40,640 --> 00:23:43,880
say two decimal and then add the text
649
00:23:43,039 --> 00:23:46,440
money
650
00:23:43,880 --> 00:23:48,799
in perfect now if you run the line of
651
00:23:46,440 --> 00:23:50,960
code we can see that all of these values
652
00:23:48,799 --> 00:23:52,679
are exactly as we had them now they're
653
00:23:50,960 --> 00:23:54,679
being represented by their official
654
00:23:52,679 --> 00:23:56,799
descriptions Based On A String
655
00:23:54,679 --> 00:23:58,720
conversion as I mentioned before in the
656
00:23:56,799 --> 00:24:00,960
error video the problem you'll have with
657
00:23:58,720 --> 00:24:03,360
this is if this value actually has a
658
00:24:00,960 --> 00:24:05,799
character inside you will get a runtime
659
00:24:03,360 --> 00:24:07,840
error this can be fixed in the future
660
00:24:05,799 --> 00:24:09,480
when we look at how to resolve these
661
00:24:07,840 --> 00:24:12,159
errors and we can have exception
662
00:24:09,480 --> 00:24:14,240
handling and use functions like try pass
663
00:24:12,159 --> 00:24:16,200
so we can try and avoid these exceptions
664
00:24:14,240 --> 00:24:18,600
occurring because when these exceptions
665
00:24:16,200 --> 00:24:20,720
occur the rest of our code does not run
666
00:24:18,600 --> 00:24:22,960
and it becomes fatal at this
667
00:24:20,720 --> 00:24:25,000
point but for now don't worry about this
668
00:24:22,960 --> 00:24:27,000
too much just get used to the notation
669
00:24:25,000 --> 00:24:29,039
and how these things
670
00:24:27,000 --> 00:24:31,279
work so this is video we're going to
671
00:24:29,039 --> 00:24:34,120
focus on the Boolean data type so we can
672
00:24:31,279 --> 00:24:36,159
make a Boolean data type by saying Bo
673
00:24:34,120 --> 00:24:38,840
value equals
674
00:24:36,159 --> 00:24:41,240
true this Boolean value is literally
675
00:24:38,840 --> 00:24:42,919
just a true or false value we can store
676
00:24:41,240 --> 00:24:45,360
anything in here and maybe something
677
00:24:42,919 --> 00:24:47,919
like bull is M and we can just say it
678
00:24:45,360 --> 00:24:50,320
true and later on our code boings are
679
00:24:47,919 --> 00:24:52,520
used to make decisions we'll cover this
680
00:24:50,320 --> 00:24:54,320
in the next section of this course but
681
00:24:52,520 --> 00:24:56,559
boings are mainly used for making
682
00:24:54,320 --> 00:24:58,080
decisions if one thing is equal to
683
00:24:56,559 --> 00:25:00,000
another thing then we can take another
684
00:24:58,080 --> 00:25:01,320
path or if something is true or
685
00:25:00,000 --> 00:25:03,440
something is false then we can do
686
00:25:01,320 --> 00:25:05,200
another path for example if you make an
687
00:25:03,440 --> 00:25:07,440
output to the user you could check if
688
00:25:05,200 --> 00:25:09,240
this value is true and if the value is
689
00:25:07,440 --> 00:25:11,240
true then you can address the user as a
690
00:25:09,240 --> 00:25:13,520
male and if the value is false then you
691
00:25:11,240 --> 00:25:15,039
can address the user as a female these
692
00:25:13,520 --> 00:25:17,440
are the kind of things that booleans can
693
00:25:15,039 --> 00:25:18,919
help us with and just like any variable
694
00:25:17,440 --> 00:25:21,000
if you want to assign it later on you
695
00:25:18,919 --> 00:25:23,559
can just assign it again and if we print
696
00:25:21,000 --> 00:25:23,559
these out to the
697
00:25:24,120 --> 00:25:28,559
screen just like that you'll just see
698
00:25:26,600 --> 00:25:30,760
the value is true and the value is false
699
00:25:28,559 --> 00:25:32,200
appear on the screen as of right now in
700
00:25:30,760 --> 00:25:33,960
the course there's not a whole lot to do
701
00:25:32,200 --> 00:25:36,520
with boing just understand that they can
702
00:25:33,960 --> 00:25:38,960
hold a true or false
703
00:25:36,520 --> 00:25:40,600
value so now that we know how to define
704
00:25:38,960 --> 00:25:42,679
different variables and how to convert
705
00:25:40,600 --> 00:25:44,720
between them let's see how we can
706
00:25:42,679 --> 00:25:47,240
perform operations on different data
707
00:25:44,720 --> 00:25:49,679
types let's say we could have an INT age
708
00:25:47,240 --> 00:25:53,080
equals 23 and then what we can do to
709
00:25:49,679 --> 00:25:54,840
that age is we can say Age ++ and what
710
00:25:53,080 --> 00:25:57,360
this age will do is it will actually
711
00:25:54,840 --> 00:25:59,559
just increment age by one and if we do
712
00:25:57,360 --> 00:26:01,799
age minus minus it will decrement it by
713
00:25:59,559 --> 00:26:03,679
one so if we just print it out after and
714
00:26:01,799 --> 00:26:05,480
after this as well what we'll notice is
715
00:26:03,679 --> 00:26:07,480
there'll be no change because it'll go
716
00:26:05,480 --> 00:26:10,919
forward one and then back one there we
717
00:26:07,480 --> 00:26:12,679
go went from 23 plus plus to 24 and then
718
00:26:10,919 --> 00:26:15,360
minus minus to
719
00:26:12,679 --> 00:26:17,399
23 that's one of the important operators
720
00:26:15,360 --> 00:26:19,320
that you'll see a lot especially during
721
00:26:17,399 --> 00:26:21,120
the next section of this course so
722
00:26:19,320 --> 00:26:23,320
another way to be able to add one to a
723
00:26:21,120 --> 00:26:28,679
variable or anything to a variable we
724
00:26:23,320 --> 00:26:30,880
can replace this with age equals age + 1
725
00:26:28,679 --> 00:26:34,120
and what we're seeing here we want age
726
00:26:30,880 --> 00:26:35,960
to have the value of itself added to one
727
00:26:34,120 --> 00:26:38,799
which will give us the same result as
728
00:26:35,960 --> 00:26:41,720
doing h++ another way of writing this is
729
00:26:38,799 --> 00:26:44,559
a quick shorthand is you can say AG plus
730
00:26:41,720 --> 00:26:47,039
equals 1 these three things are doing
731
00:26:44,559 --> 00:26:49,720
exactly the same thing when you do an
732
00:26:47,039 --> 00:26:52,399
age Plus+ you can only increment by one
733
00:26:49,720 --> 00:26:54,559
and only one these two options give you
734
00:26:52,399 --> 00:26:58,000
the flexibility of having something like
735
00:26:54,559 --> 00:27:00,440
10 these two are doing exactly the same
736
00:26:58,000 --> 00:27:02,360
a AG equal AG + 10 and this is going to
737
00:27:00,440 --> 00:27:04,559
read exactly the same thing it's is when
738
00:27:02,360 --> 00:27:06,840
you use plus equals it will take the
739
00:27:04,559 --> 00:27:09,200
value of the current variable and add it
740
00:27:06,840 --> 00:27:11,320
to 10 which exactly what this line does
741
00:27:09,200 --> 00:27:13,000
to make it a lot shorter you can use age
742
00:27:11,320 --> 00:27:15,039
plus equals and then just put the value
743
00:27:13,000 --> 00:27:17,320
at the end just in case you forget to
744
00:27:15,039 --> 00:27:19,080
mention age twice so now if we just get
745
00:27:17,320 --> 00:27:20,840
rid of these two lines of code and we
746
00:27:19,080 --> 00:27:24,080
can just have a look and see if this
747
00:27:20,840 --> 00:27:26,080
runs so now the value should be 33
748
00:27:24,080 --> 00:27:30,080
perfect and now as we said we're going
749
00:27:26,080 --> 00:27:31,880
to cover plusus time and divide so just
750
00:27:30,080 --> 00:27:34,080
as you did with plus equals you can
751
00:27:31,880 --> 00:27:36,799
actually do a minus equals as well which
752
00:27:34,080 --> 00:27:39,039
will give us 13 and you can do a times
753
00:27:36,799 --> 00:27:42,080
equals which should give us
754
00:27:39,039 --> 00:27:43,880
230 now if we try to do divide equals
755
00:27:42,080 --> 00:27:46,360
we're going to run into an
756
00:27:43,880 --> 00:27:50,120
issue it says
757
00:27:46,360 --> 00:27:53,760
2 23 / 10 is
758
00:27:50,120 --> 00:27:55,159
2.3 and when you take 2.3 and make it
759
00:27:53,760 --> 00:27:58,200
into an integer it's just going to
760
00:27:55,159 --> 00:28:00,600
truncate the3 and leave you with two
761
00:27:58,200 --> 00:28:02,919
with integers if you add two integers or
762
00:28:00,600 --> 00:28:05,080
take away two integers or times them
763
00:28:02,919 --> 00:28:07,279
you'll always end up with an integer if
764
00:28:05,080 --> 00:28:09,159
you divide two integers you don't know
765
00:28:07,279 --> 00:28:11,440
what you'll end with for example if you
766
00:28:09,159 --> 00:28:13,480
make this into 20 then you'll always end
767
00:28:11,440 --> 00:28:17,519
up with two but that's not always the
768
00:28:13,480 --> 00:28:19,480
case as you just saw 23 / 10 is 2.3 so
769
00:28:17,519 --> 00:28:22,159
if you're ever going to do any division
770
00:28:19,480 --> 00:28:23,679
it's best to actually make this a double
771
00:28:22,159 --> 00:28:25,840
cuz now that we do the division we
772
00:28:23,679 --> 00:28:28,200
actually get 2.3 which is technically
773
00:28:25,840 --> 00:28:30,200
the right value so you can add minus
774
00:28:28,200 --> 00:28:32,240
times and divide any variables that you
775
00:28:30,200 --> 00:28:34,080
want these are just demonstrated in int
776
00:28:32,240 --> 00:28:36,480
and double but you can do this the Flor
777
00:28:34,080 --> 00:28:37,880
the decimal Etc so let's have a look and
778
00:28:36,480 --> 00:28:39,840
see what the plus and the minus
779
00:28:37,880 --> 00:28:41,880
operators do when you've got a string
780
00:28:39,840 --> 00:28:43,880
variable so we can have a string name
781
00:28:41,880 --> 00:28:48,559
equals
782
00:28:43,880 --> 00:28:51,279
abber and then we can say the name plus
783
00:28:48,559 --> 00:28:53,080
equals is
784
00:28:51,279 --> 00:28:54,360
programming and then we can just print
785
00:28:53,080 --> 00:28:56,240
out name to the screen and see what
786
00:28:54,360 --> 00:28:58,360
happens there you go you've actually
787
00:28:56,240 --> 00:28:59,720
just joined two strings together they're
788
00:28:58,360 --> 00:29:02,799
completely separate from each other and
789
00:28:59,720 --> 00:29:02,799
we've made them string
790
00:29:03,159 --> 00:29:07,320
together so if we try to do a minus
791
00:29:05,480 --> 00:29:09,720
equals you can't really remove from a
792
00:29:07,320 --> 00:29:11,480
string because a string is just text
793
00:29:09,720 --> 00:29:13,279
it'll be very difficult for the compiler
794
00:29:11,480 --> 00:29:15,360
and for the C language to be able to
795
00:29:13,279 --> 00:29:17,080
determine what's currently in the string
796
00:29:15,360 --> 00:29:18,559
and whether you can remove it or not
797
00:29:17,080 --> 00:29:20,279
this is not something that's built into
798
00:29:18,559 --> 00:29:22,279
the language this is not something
799
00:29:20,279 --> 00:29:24,000
that's built into the language so let's
800
00:29:22,279 --> 00:29:26,120
remove that and we can have the same
801
00:29:24,000 --> 00:29:28,760
thing with the
802
00:29:26,120 --> 00:29:31,600
CH now the problem with the ch
803
00:29:28,760 --> 00:29:34,200
is if we add a to B then let's see what
804
00:29:31,600 --> 00:29:34,200
happens to the
805
00:29:36,159 --> 00:29:40,519
output what it will try and do is
806
00:29:38,640 --> 00:29:43,120
actually get the uni code values for
807
00:29:40,519 --> 00:29:45,799
both of these items and try and add them
808
00:29:43,120 --> 00:29:48,039
together as integer
809
00:29:45,799 --> 00:29:49,640
variables so when you add a and b it
810
00:29:48,039 --> 00:29:51,320
will take the unic code value and you
811
00:29:49,640 --> 00:29:53,120
will actually get a capital A with a
812
00:29:51,320 --> 00:29:54,480
squiggle at the top and if we open
813
00:29:53,120 --> 00:29:57,399
character map you can see that this
814
00:29:54,480 --> 00:30:00,440
squiggle at the top is right here a
815
00:29:57,399 --> 00:30:05,159
which is unic cord 61 and 61 actually
816
00:30:00,440 --> 00:30:08,640
means 97 because it's 6 * by 16 + the 1
817
00:30:05,159 --> 00:30:12,159
and b means it's 6 * 16 which is 96 +
818
00:30:08,640 --> 00:30:14,279
the 2 so you have 97 and 98 and if you
819
00:30:12,159 --> 00:30:17,440
add them two values together you'll get
820
00:30:14,279 --> 00:30:18,919
the hex code which is C3 just for this
821
00:30:17,440 --> 00:30:20,640
character so what you're doing is you're
822
00:30:18,919 --> 00:30:23,159
kind of adding them both together and
823
00:30:20,640 --> 00:30:24,799
you're producing weird results so unless
824
00:30:23,159 --> 00:30:26,399
you really want to do this then you
825
00:30:24,799 --> 00:30:28,720
should be worryed about adding two
826
00:30:26,399 --> 00:30:31,240
characters together one more thing to
827
00:30:28,720 --> 00:30:33,799
note about when you're doing Plus+ let's
828
00:30:31,240 --> 00:30:37,320
bring another int variable and we just
829
00:30:33,799 --> 00:30:39,200
say int I equals z then we can do I ++
830
00:30:37,320 --> 00:30:42,360
and then if we print out I the value
831
00:30:39,200 --> 00:30:44,039
should be one which is fine what if we
832
00:30:42,360 --> 00:30:47,559
did
833
00:30:44,039 --> 00:30:49,880
i++ but we did Plus+ I so the value is
834
00:30:47,559 --> 00:30:52,840
still going to be one but what happens
835
00:30:49,880 --> 00:30:55,200
if we put this I ++ straight into these
836
00:30:52,840 --> 00:30:57,279
brackets let's try and run it and you'll
837
00:30:55,200 --> 00:31:00,399
still get the value of zero that's
838
00:30:57,279 --> 00:31:03,000
strange because we've said I ++ and what
839
00:31:00,399 --> 00:31:05,360
happens when you do Plus+ at the front
840
00:31:03,000 --> 00:31:08,639
it will actually execute this and return
841
00:31:05,360 --> 00:31:10,320
the value of I and then do the Plus+ so
842
00:31:08,639 --> 00:31:12,559
if you actually print the value of I
843
00:31:10,320 --> 00:31:15,679
after you'll realize that now it becomes
844
00:31:12,559 --> 00:31:17,720
one after if you ever had a situation
845
00:31:15,679 --> 00:31:20,559
where you need to increment first and
846
00:31:17,720 --> 00:31:22,519
then use the value you can use plus plus
847
00:31:20,559 --> 00:31:25,360
I now if you run it you should see the
848
00:31:22,519 --> 00:31:27,039
value is one twice the difference is
849
00:31:25,360 --> 00:31:29,080
that as the plus plus is before the
850
00:31:27,039 --> 00:31:31,440
variable name it tells the compiler
851
00:31:29,080 --> 00:31:33,519
please do this action first and then
852
00:31:31,440 --> 00:31:36,760
return back the I if you don't want that
853
00:31:33,519 --> 00:31:36,760
behavior then use I
854
00:31:37,000 --> 00:31:41,720
++ finding the remainder between the
855
00:31:39,559 --> 00:31:43,559
division of two integer variables can be
856
00:31:41,720 --> 00:31:45,760
very handy when you're determining
857
00:31:43,559 --> 00:31:48,039
whether a number is even or odd let's
858
00:31:45,760 --> 00:31:51,120
look at an example let's have it in
859
00:31:48,039 --> 00:31:52,679
first Nome design the value of 10 and in
860
00:31:51,120 --> 00:31:55,080
second Nome and give it the value of
861
00:31:52,679 --> 00:31:57,480
three now if we just printed the screen
862
00:31:55,080 --> 00:31:59,519
first num divided by second n what do
863
00:31:57,480 --> 00:32:01,679
you think you'll get so the value of
864
00:31:59,519 --> 00:32:03,799
this in the calculator will be 3.3
865
00:32:01,679 --> 00:32:05,799
reoccurring so we'd actually just get
866
00:32:03,799 --> 00:32:08,080
three because it's truncating it because
867
00:32:05,799 --> 00:32:10,440
of the
868
00:32:08,080 --> 00:32:12,519
integer so now what happens if we need
869
00:32:10,440 --> 00:32:14,360
the remainder between both of these you
870
00:32:12,519 --> 00:32:16,720
could actually work this out by seeing
871
00:32:14,360 --> 00:32:19,360
how many times three would be divided by
872
00:32:16,720 --> 00:32:23,240
10 as close as possible and you see the
873
00:32:19,360 --> 00:32:24,919
value is three and then 3 * by 3 is 9
874
00:32:23,240 --> 00:32:27,080
which will give you one left over for
875
00:32:24,919 --> 00:32:28,960
the 10 but that's a bit too long in our
876
00:32:27,080 --> 00:32:31,159
code so we have something called a
877
00:32:28,960 --> 00:32:34,240
modulus operator so we can actually
878
00:32:31,159 --> 00:32:35,600
change this divide for a percentage sign
879
00:32:34,240 --> 00:32:38,320
and what this will output is it will
880
00:32:35,600 --> 00:32:42,120
only give us the remainder so let's say
881
00:32:38,320 --> 00:32:45,039
10 / 3 is
882
00:32:42,120 --> 00:32:46,960
3.3 as we've just said but the remainder
883
00:32:45,039 --> 00:32:48,240
is different remainder is exactly how
884
00:32:46,960 --> 00:32:52,760
you learned it at
885
00:32:48,240 --> 00:32:56,559
school 10 / 3 is actually 3 remainder
886
00:32:52,760 --> 00:32:59,960
one because 3 * 3 is 9 and then you have
887
00:32:56,559 --> 00:33:02,840
one left over to get 10 so that'll be 3
888
00:32:59,960 --> 00:33:06,000
* 3 which is equal to 9 and then one
889
00:33:02,840 --> 00:33:07,840
left over to get to 10 so now let's run
890
00:33:06,000 --> 00:33:11,320
our code and see if we do actually get
891
00:33:07,840 --> 00:33:13,960
one there we go so the best way to
892
00:33:11,320 --> 00:33:17,159
determine if a value is actually odd or
893
00:33:13,960 --> 00:33:21,440
even is to divide it by two so we can
894
00:33:17,159 --> 00:33:23,880
say 10 modulus 2 and if the value is
895
00:33:21,440 --> 00:33:27,360
zero then that means the value for first
896
00:33:23,880 --> 00:33:29,320
nor is actually even if the value is one
897
00:33:27,360 --> 00:33:32,600
then it's OD let's see how this works
898
00:33:29,320 --> 00:33:34,519
out if we had 10 modulus 2 then the
899
00:33:32,600 --> 00:33:37,000
value is zero because 10 can be
900
00:33:34,519 --> 00:33:41,159
divisible by two perfectly let's say we
901
00:33:37,000 --> 00:33:44,559
had 11 modulus 2 the closest multiple to
902
00:33:41,159 --> 00:33:47,480
11 is 10 which is 5 and then we have one
903
00:33:44,559 --> 00:33:50,440
left over so you just have the remainder
904
00:33:47,480 --> 00:33:52,039
is equal to one and if we just keep
905
00:33:50,440 --> 00:33:54,240
going up in the chain we'll realize that
906
00:33:52,039 --> 00:33:59,200
it's the same pattern every single time
907
00:33:54,240 --> 00:33:59,200
you go up a value the value just toggles
908
00:33:59,760 --> 00:34:04,000
so when we print this out to the console
909
00:34:01,919 --> 00:34:06,600
and the value is actually zero that
910
00:34:04,000 --> 00:34:08,679
means the number is even and if we make
911
00:34:06,600 --> 00:34:10,159
the number odd on purpose and then rerun
912
00:34:08,679 --> 00:34:12,599
the code you can see that it's a number
913
00:34:10,159 --> 00:34:14,960
one now which means this value is now
914
00:34:12,599 --> 00:34:17,119
odd let's look at a couple examples and
915
00:34:14,960 --> 00:34:20,639
see if you can figure out the answer if
916
00:34:17,119 --> 00:34:22,480
we do a th000 modulus 90 what do you
917
00:34:20,639 --> 00:34:26,079
think the value will be and let's write
918
00:34:22,480 --> 00:34:29,879
a couple more if we do 100 modulus 90
919
00:34:26,079 --> 00:34:31,520
and maybe something like 71 1 modulus 10
920
00:34:29,879 --> 00:34:32,679
so have a think about this in your head
921
00:34:31,520 --> 00:34:34,760
and see if you can figure out the
922
00:34:32,679 --> 00:34:35,960
answers pause the video and see if you
923
00:34:34,760 --> 00:34:38,399
are
924
00:34:35,960 --> 00:34:41,399
correct let's take a look at the
925
00:34:38,399 --> 00:34:44,359
answers so we can see we have 10 10 and
926
00:34:41,399 --> 00:34:45,960
1 so the closest multiple to 1,000 from
927
00:34:44,359 --> 00:34:49,399
90 is
928
00:34:45,960 --> 00:34:51,960
990 so we have 10 left over to get to
929
00:34:49,399 --> 00:34:55,159
1,000 and the same goes here the closest
930
00:34:51,960 --> 00:34:58,320
multiple of 100 to 90 is just indeed 90
931
00:34:55,159 --> 00:35:00,520
so we only have 10 to get from 90 to 100
932
00:34:58,320 --> 00:35:04,160
and in this case the closest multiple of
933
00:35:00,520 --> 00:35:06,760
71 to 10 is 70 and we have one to get up
934
00:35:04,160 --> 00:35:08,640
to 71 therefore these two answers are
935
00:35:06,760 --> 00:35:10,760
indeed
936
00:35:08,640 --> 00:35:12,800
correct so now that we know how to
937
00:35:10,760 --> 00:35:14,800
really Define a variable by specifying
938
00:35:12,800 --> 00:35:16,920
their type follow by the name and giving
939
00:35:14,800 --> 00:35:19,599
it a value let's see how we can do a
940
00:35:16,920 --> 00:35:21,960
very easy way of defining a variable so
941
00:35:19,599 --> 00:35:24,400
normally we would type in Edge what we
942
00:35:21,960 --> 00:35:26,200
can use this VAR keyword what this means
943
00:35:24,400 --> 00:35:27,960
is the compiler will figure out what
944
00:35:26,200 --> 00:35:30,400
type it needs to be based on the value
945
00:35:27,960 --> 00:35:32,400
we initialize it with so if we just make
946
00:35:30,400 --> 00:35:33,800
a variable called age then it's not
947
00:35:32,400 --> 00:35:35,800
going to like it because you need to
948
00:35:33,800 --> 00:35:37,960
give it a value this doesn't give the
949
00:35:35,800 --> 00:35:39,800
compiler enough information to figure
950
00:35:37,960 --> 00:35:41,640
out what this variable is going to be
951
00:35:39,800 --> 00:35:43,680
just because it says age this is just
952
00:35:41,640 --> 00:35:46,800
text to the compiler this does not mean
953
00:35:43,680 --> 00:35:48,760
anything so if you make it equal to 23
954
00:35:46,800 --> 00:35:51,960
then the compiler can see that this is
955
00:35:48,760 --> 00:35:54,640
indeed in 32 because it knows that so
956
00:35:51,960 --> 00:35:57,240
now if you hover over a VAR it says in
957
00:35:54,640 --> 00:35:59,359
32 now let's try and do this for the big
958
00:35:57,240 --> 00:36:02,760
number
959
00:35:59,359 --> 00:36:05,160
our big number equals
960
00:36:02,760 --> 00:36:07,640
90,000 and what will happen now is
961
00:36:05,160 --> 00:36:10,040
because it's Define as in32 it's going
962
00:36:07,640 --> 00:36:11,920
to say this is in32 as well and this is
963
00:36:10,040 --> 00:36:13,880
the same issue we had we'll move first
964
00:36:11,920 --> 00:36:16,560
defining the long variable in the first
965
00:36:13,880 --> 00:36:19,200
video we need to explicitly give it an L
966
00:36:16,560 --> 00:36:21,400
just so this knows that it's N64 and now
967
00:36:19,200 --> 00:36:24,079
the VAR has updated to an
968
00:36:21,400 --> 00:36:26,160
N64 instead of rewriting each one we can
969
00:36:24,079 --> 00:36:28,200
just change each of v and inspect what
970
00:36:26,160 --> 00:36:32,359
happens double
971
00:36:28,200 --> 00:36:35,599
double and now we have a float single
972
00:36:32,359 --> 00:36:37,800
single and finally the decimal we have a
973
00:36:35,599 --> 00:36:39,640
decimal and we have a decimal and the
974
00:36:37,800 --> 00:36:42,720
same thing goes for a string we can say
975
00:36:39,640 --> 00:36:46,000
VAR name equals ABA and we can have a
976
00:36:42,720 --> 00:36:47,920
VAR letter equals a there we go and
977
00:36:46,000 --> 00:36:52,079
they'll both register as there pickable
978
00:36:47,920 --> 00:36:53,400
data types as a string and as a Char and
979
00:36:52,079 --> 00:36:56,000
if we just print this out to the screen
980
00:36:53,400 --> 00:36:57,640
to make sure everything's working fine
981
00:36:56,000 --> 00:37:00,160
and run the code you can see that
982
00:36:57,640 --> 00:37:02,280
everything is exactly how we'd expect so
983
00:37:00,160 --> 00:37:03,920
just a quick recap you don't have to do
984
00:37:02,280 --> 00:37:06,280
this at the end of the day this will be
985
00:37:03,920 --> 00:37:07,920
exactly the same as writing this because
986
00:37:06,280 --> 00:37:09,400
when our code gets compiled down it
987
00:37:07,920 --> 00:37:11,040
doesn't matter what these things are cuz
988
00:37:09,400 --> 00:37:13,200
the compiler will deal with it however
989
00:37:11,040 --> 00:37:15,760
way it wants to this is just for us to
990
00:37:13,200 --> 00:37:18,240
read it in some cases if the value is
991
00:37:15,760 --> 00:37:22,000
very obvious like as we know an age
992
00:37:18,240 --> 00:37:23,560
cannot really be a point 23.1 years old
993
00:37:22,000 --> 00:37:25,839
no one says that in real life when you
994
00:37:23,560 --> 00:37:27,760
get asked your age so we can just assume
995
00:37:25,839 --> 00:37:29,920
that this value is going to be in an
996
00:37:27,760 --> 00:37:32,240
integer and that's fine because that's
997
00:37:29,920 --> 00:37:34,640
an easy number that's an easy example
998
00:37:32,240 --> 00:37:36,359
but if we say we want a negative or a
999
00:37:34,640 --> 00:37:38,400
big number then it doesn't really tell
1000
00:37:36,359 --> 00:37:40,920
us what we're meant to be like this big
1001
00:37:38,400 --> 00:37:42,640
number could be somewhere up to 2.1
1002
00:37:40,920 --> 00:37:44,720
billion which is what an integer
1003
00:37:42,640 --> 00:37:46,440
supports but in this case if it's going
1004
00:37:44,720 --> 00:37:48,400
to be a big number and we specified the
1005
00:37:46,440 --> 00:37:51,480
L then we're going to need a really big
1006
00:37:48,400 --> 00:37:53,560
number bigger than 2.1 billion so in my
1007
00:37:51,480 --> 00:37:56,400
honest opinion it' be very beneficial to
1008
00:37:53,560 --> 00:37:58,119
only use VAR if it's very obvious what
1009
00:37:56,400 --> 00:38:00,079
the variable is going to be and want
1010
00:37:58,119 --> 00:38:02,119
it's being assigned to using VAR too
1011
00:38:00,079 --> 00:38:04,280
much might hurt the readability of your
1012
00:38:02,119 --> 00:38:06,400
code in the future because you have to
1013
00:38:04,280 --> 00:38:08,160
inspect each variable individually to
1014
00:38:06,400 --> 00:38:09,880
read what the type is going to be if
1015
00:38:08,160 --> 00:38:11,400
you're scrolling through your code and
1016
00:38:09,880 --> 00:38:13,200
you can see that this is along very
1017
00:38:11,400 --> 00:38:15,560
quickly and you don't have to hover over
1018
00:38:13,200 --> 00:38:17,040
it then it can improve your read time
1019
00:38:15,560 --> 00:38:18,800
this is really important as readability
1020
00:38:17,040 --> 00:38:20,880
of code will make more sense when it
1021
00:38:18,800 --> 00:38:22,800
comes to Big projects when you have 20
1022
00:38:20,880 --> 00:38:24,800
30 lines just like this project then it
1023
00:38:22,800 --> 00:38:26,680
makes almost no difference cuz it only
1024
00:38:24,800 --> 00:38:29,119
takes a couple of seconds when you have
1025
00:38:26,680 --> 00:38:31,119
big functions big software when you work
1026
00:38:29,119 --> 00:38:32,640
at a job then it's going to be very
1027
00:38:31,119 --> 00:38:34,760
difficult to be able to find your way
1028
00:38:32,640 --> 00:38:37,119
around and to understand what's going on
1029
00:38:34,760 --> 00:38:38,800
especially if you're new to the code
1030
00:38:37,119 --> 00:38:40,720
something very simple like this where
1031
00:38:38,800 --> 00:38:43,040
you know you're defining a name it's
1032
00:38:40,720 --> 00:38:44,440
very obvious and VAR will be accepted
1033
00:38:43,040 --> 00:38:46,319
fine because you can tell that this is
1034
00:38:44,440 --> 00:38:49,200
always going to be a string there's no
1035
00:38:46,319 --> 00:38:51,160
other data type in C get specified by a
1036
00:38:49,200 --> 00:38:53,839
speech mark whereas when you're defining
1037
00:38:51,160 --> 00:38:55,560
numbers it can be very very hard to
1038
00:38:53,839 --> 00:38:57,040
understand what different data types
1039
00:38:55,560 --> 00:38:59,240
that people are using throughout the
1040
00:38:57,040 --> 00:39:01,560
code especially when these numbers are
1041
00:38:59,240 --> 00:39:03,680
not of statically typed in they might be
1042
00:39:01,560 --> 00:39:05,800
inputs from the console from the user
1043
00:39:03,680 --> 00:39:08,160
from a different function from anywhere
1044
00:39:05,800 --> 00:39:10,240
so having good variable names along with
1045
00:39:08,160 --> 00:39:13,440
their explicit types will be very handy
1046
00:39:10,240 --> 00:39:15,680
to understand code in the
1047
00:39:13,440 --> 00:39:17,359
future so now that you've understood how
1048
00:39:15,680 --> 00:39:19,800
to make a variable let's look at a
1049
00:39:17,359 --> 00:39:22,839
concept called constants so we defined a
1050
00:39:19,800 --> 00:39:25,920
variable called vat and we set it to 20
1051
00:39:22,839 --> 00:39:27,480
just to represent the 20% V in the UK
1052
00:39:25,920 --> 00:39:29,240
now later on in the program we can
1053
00:39:27,480 --> 00:39:30,599
actually change it to anything we want
1054
00:39:29,240 --> 00:39:32,440
and that's because it's a variable
1055
00:39:30,599 --> 00:39:34,640
variables can change in certain
1056
00:39:32,440 --> 00:39:35,800
circumstances like vat you might not
1057
00:39:34,640 --> 00:39:37,680
want to change it and you want to
1058
00:39:35,800 --> 00:39:39,920
disable the ability to change it to the
1059
00:39:37,680 --> 00:39:41,400
code if it's done accidentally let's say
1060
00:39:39,920 --> 00:39:43,960
for example you're making a banking
1061
00:39:41,400 --> 00:39:45,480
application and you wanted to fix the V
1062
00:39:43,960 --> 00:39:48,000
and you don't want anyone else to change
1063
00:39:45,480 --> 00:39:49,800
it you can add this keyword called const
1064
00:39:48,000 --> 00:39:51,400
right before the Declaration and now
1065
00:39:49,800 --> 00:39:53,400
when you go to reassign it you'll get an
1066
00:39:51,400 --> 00:39:55,319
error message and it says at the left
1067
00:39:53,400 --> 00:39:57,480
hand side of the assignment so anything
1068
00:39:55,319 --> 00:39:59,800
before the equal sign has to be a very
1069
00:39:57,480 --> 00:40:02,200
Able Property or indexer and in this
1070
00:39:59,800 --> 00:40:04,280
case if you hover over vat it states
1071
00:40:02,200 --> 00:40:06,440
that it's a local constant local because
1072
00:40:04,280 --> 00:40:08,560
there's no access modifier before it and
1073
00:40:06,440 --> 00:40:10,760
constant because we specified the con
1074
00:40:08,560 --> 00:40:12,760
keyword so now if we try and run the
1075
00:40:10,760 --> 00:40:15,720
code we'll actually get a compile error
1076
00:40:12,760 --> 00:40:17,160
because the vat cannot be changed so if
1077
00:40:15,720 --> 00:40:19,880
we remove that and then we just print it
1078
00:40:17,160 --> 00:40:21,920
out to the console using CW tab tab and
1079
00:40:19,880 --> 00:40:24,880
then we can put vat in the brackets and
1080
00:40:21,920 --> 00:40:27,839
press F5 and now we can see our 20 being
1081
00:40:24,880 --> 00:40:29,640
printed
1082
00:40:27,839 --> 00:40:32,480
so let's try and calculate some values
1083
00:40:29,640 --> 00:40:35,359
using this code if you have an INT
1084
00:40:32,480 --> 00:40:37,280
balance make it equal to 1,000 and let's
1085
00:40:35,359 --> 00:40:39,280
say we wanted to calculate the vat based
1086
00:40:37,280 --> 00:40:41,079
off this then you'd have to do a little
1087
00:40:39,280 --> 00:40:42,720
bit of a complicated calculation you
1088
00:40:41,079 --> 00:40:44,960
have to take the balance and because the
1089
00:40:42,720 --> 00:40:47,359
vat is a percentage we need to convert
1090
00:40:44,960 --> 00:40:49,599
it from decimal to percentage so if we
1091
00:40:47,359 --> 00:40:51,280
do times and then open the parentheses
1092
00:40:49,599 --> 00:40:55,160
we can insert
1093
00:40:51,280 --> 00:40:57,000
vat divided by 100 and just to make sure
1094
00:40:55,160 --> 00:40:59,520
that this gets process as a double we
1095
00:40:57,000 --> 00:41:01,960
want write 100 D cuz right now it's in
1096
00:40:59,520 --> 00:41:03,720
32 and if we leave this as an integer
1097
00:41:01,960 --> 00:41:05,640
division then we'll lose any decimal
1098
00:41:03,720 --> 00:41:07,440
places and decimal places are very
1099
00:41:05,640 --> 00:41:09,800
important when it comes to percentages
1100
00:41:07,440 --> 00:41:11,240
so we put a 100 D at the end now you can
1101
00:41:09,800 --> 00:41:13,319
see it says double so it'll actually
1102
00:41:11,240 --> 00:41:15,680
calculate as if it was a decimal number
1103
00:41:13,319 --> 00:41:18,920
so now if we print that to the screen so
1104
00:41:15,680 --> 00:41:21,760
we can see here that 20% of the balance
1105
00:41:18,920 --> 00:41:23,800
is indeed 200 instead of having to do
1106
00:41:21,760 --> 00:41:26,200
this calculation each time what we can
1107
00:41:23,800 --> 00:41:27,760
actually do is make another const this
1108
00:41:26,200 --> 00:41:29,280
time making a double because we're not
1109
00:41:27,760 --> 00:41:31,079
sure what the output of this division
1110
00:41:29,280 --> 00:41:33,079
will be and we don't want to truncate it
1111
00:41:31,079 --> 00:41:36,480
so we want it as a double and we can say
1112
00:41:33,079 --> 00:41:38,599
percent vat and what we can do here when
1113
00:41:36,480 --> 00:41:40,240
we assign the value just because it's a
1114
00:41:38,599 --> 00:41:42,480
constant it means that we can't change
1115
00:41:40,240 --> 00:41:44,760
the value but that also means we can
1116
00:41:42,480 --> 00:41:47,400
read it as many times as we want so we
1117
00:41:44,760 --> 00:41:50,560
can say the percentage V is actually the
1118
00:41:47,400 --> 00:41:52,400
current V that we've got specified here
1119
00:41:50,560 --> 00:41:55,640
divided by
1120
00:41:52,400 --> 00:41:58,280
100d this will give us the vat from a
1121
00:41:55,640 --> 00:42:00,000
number form into a decimal form form so
1122
00:41:58,280 --> 00:42:02,280
now when we do this calculation if we
1123
00:42:00,000 --> 00:42:03,800
duplicate this line using contrl D we
1124
00:42:02,280 --> 00:42:06,319
can remove these entire brackets and
1125
00:42:03,800 --> 00:42:07,880
literally replace it with percentage vat
1126
00:42:06,319 --> 00:42:09,640
and you should see we get the same
1127
00:42:07,880 --> 00:42:11,839
values
1128
00:42:09,640 --> 00:42:13,599
perfect this can be very helpful in the
1129
00:42:11,839 --> 00:42:15,599
future if you have things like version
1130
00:42:13,599 --> 00:42:18,280
numbers in your code you can have
1131
00:42:15,599 --> 00:42:20,359
version 1.0 and then if you ever specify
1132
00:42:18,280 --> 00:42:22,119
this version later on in your code you
1133
00:42:20,359 --> 00:42:25,280
can just change it once up and it will
1134
00:42:22,119 --> 00:42:26,680
change the rest this can be very helpful
1135
00:42:25,280 --> 00:42:28,480
you'll avoid a lot of typos and you
1136
00:42:26,680 --> 00:42:30,680
won't have to to redeclare this value
1137
00:42:28,480 --> 00:42:32,040
several times if you had three different
1138
00:42:30,680 --> 00:42:33,800
occasions where you displaying the
1139
00:42:32,040 --> 00:42:35,520
version number in your code then it's
1140
00:42:33,800 --> 00:42:36,599
going to be problematic once you update
1141
00:42:35,520 --> 00:42:38,480
the version number and you have to
1142
00:42:36,599 --> 00:42:40,880
change every single occurrence and you
1143
00:42:38,480 --> 00:42:42,480
might miss them so inserting it once and
1144
00:42:40,880 --> 00:42:44,520
only having to change it once is the
1145
00:42:42,480 --> 00:42:46,760
preferred option just to note with
1146
00:42:44,520 --> 00:42:49,000
constants when you compile your code all
1147
00:42:46,760 --> 00:42:50,760
the constants will actually do is do a
1148
00:42:49,000 --> 00:42:54,119
find and replace so it will take this
1149
00:42:50,760 --> 00:42:56,119
vat and find all references of v and
1150
00:42:54,119 --> 00:42:58,520
just drop the 20 in
1151
00:42:56,119 --> 00:43:00,559
there so this is more of a feature for
1152
00:42:58,520 --> 00:43:02,400
maintaining your code from a user and
1153
00:43:00,559 --> 00:43:04,400
programmer's perspective rather than
1154
00:43:02,400 --> 00:43:06,280
anything to do with the compiler because
1155
00:43:04,400 --> 00:43:07,920
when it compiles it just removes all the
1156
00:43:06,280 --> 00:43:09,440
constant anywhere and then you're left
1157
00:43:07,920 --> 00:43:11,240
over with the literal values that we've
1158
00:43:09,440 --> 00:43:13,200
gave it nonetheless this is still
1159
00:43:11,240 --> 00:43:15,599
something very handy to learn and to use
1160
00:43:13,200 --> 00:43:18,000
throughout your
1161
00:43:15,599 --> 00:43:20,240
code first you want to Define a variable
1162
00:43:18,000 --> 00:43:23,720
to hold your name so we can just say
1163
00:43:20,240 --> 00:43:25,480
string name equals abber perfect so
1164
00:43:23,720 --> 00:43:27,000
that's the first one achieved says
1165
00:43:25,480 --> 00:43:28,880
Define a variable to hold your phone
1166
00:43:27,000 --> 00:43:31,760
number number so at first this could be
1167
00:43:28,880 --> 00:43:33,640
quite easy we could say int number
1168
00:43:31,760 --> 00:43:36,400
equals and then we can start to type our
1169
00:43:33,640 --> 00:43:38,079
number in here so now that's our number
1170
00:43:36,400 --> 00:43:40,359
completed let's move on to holding your
1171
00:43:38,079 --> 00:43:41,359
age and we can just have an INT age
1172
00:43:40,359 --> 00:43:44,200
equals
1173
00:43:41,359 --> 00:43:46,960
23 print variables line by line to the
1174
00:43:44,200 --> 00:43:49,680
screen so we can use CW tab tab and type
1175
00:43:46,960 --> 00:43:51,960
in name and we can use contrl D to copy
1176
00:43:49,680 --> 00:43:54,760
and paste the line then we can do number
1177
00:43:51,960 --> 00:43:56,160
crl D again and then we can use ede now
1178
00:43:54,760 --> 00:43:58,839
if we run the code let's see what
1179
00:43:56,160 --> 00:44:00,720
happens
1180
00:43:58,839 --> 00:44:03,640
here we've actually hit a slight problem
1181
00:44:00,720 --> 00:44:05,200
it says 1 2 3 4 for the number but I'm
1182
00:44:03,640 --> 00:44:07,480
pretty sure that I started it off with
1183
00:44:05,200 --> 00:44:10,079
zero this is a problem with storing your
1184
00:44:07,480 --> 00:44:12,319
number as an integer as a number zeros
1185
00:44:10,079 --> 00:44:15,040
at the front don't really matter if we
1186
00:44:12,319 --> 00:44:17,319
had a 100 zeros here then it's not going
1187
00:44:15,040 --> 00:44:19,000
to change the actual value the only time
1188
00:44:17,319 --> 00:44:21,760
zeros matter is if it's throughout the
1189
00:44:19,000 --> 00:44:23,680
number or towards the end that actually
1190
00:44:21,760 --> 00:44:25,319
changes the number but if we have lots
1191
00:44:23,680 --> 00:44:27,400
of zeros at the start then it doesn't
1192
00:44:25,319 --> 00:44:29,359
change anything so what you actually
1193
00:44:27,400 --> 00:44:31,160
want to do in order to store a number is
1194
00:44:29,359 --> 00:44:33,400
you want to store it as a
1195
00:44:31,160 --> 00:44:35,319
string therefore when you run the code
1196
00:44:33,400 --> 00:44:37,800
it will actually rain the zero that we
1197
00:44:35,319 --> 00:44:39,599
typed in so now that we've accomplished
1198
00:44:37,800 --> 00:44:41,880
all of these let's see how we can Define
1199
00:44:39,599 --> 00:44:43,640
the variables using the VAR keyword so
1200
00:44:41,880 --> 00:44:45,839
if we hover over these you can see that
1201
00:44:43,640 --> 00:44:48,319
it's picking up that it's a string and a
1202
00:44:45,839 --> 00:44:51,280
string here and finally this is an
1203
00:44:48,319 --> 00:44:53,000
integer so what we can do very simply is
1204
00:44:51,280 --> 00:44:55,079
just change these to VAR and then let's
1205
00:44:53,000 --> 00:44:57,720
hover over the variables and just see if
1206
00:44:55,079 --> 00:45:01,160
the tool tip reads the correct data type
1207
00:44:57,720 --> 00:45:04,440
string string and integer and if you run
1208
00:45:01,160 --> 00:45:04,440
the code you should see no
1209
00:45:05,800 --> 00:45:10,079
difference so in this exercise I want
1210
00:45:07,960 --> 00:45:12,240
you to create and initialize two integer
1211
00:45:10,079 --> 00:45:14,240
variables make a variable to work out
1212
00:45:12,240 --> 00:45:16,240
the remainder output the remainder to
1213
00:45:14,240 --> 00:45:18,440
the screen and then change the first
1214
00:45:16,240 --> 00:45:20,359
integer variable to another number
1215
00:45:18,440 --> 00:45:22,520
recalculate the remainder and output the
1216
00:45:20,359 --> 00:45:25,400
new remainder to the screen so let's
1217
00:45:22,520 --> 00:45:27,400
take this step by step so first we need
1218
00:45:25,400 --> 00:45:31,920
to create an initialize two ins let's
1219
00:45:27,400 --> 00:45:34,319
say int num 1 equals to 10 int num 2
1220
00:45:31,920 --> 00:45:36,599
equals to two make a variable to work
1221
00:45:34,319 --> 00:45:39,319
out the remainder so we can have an INT
1222
00:45:36,599 --> 00:45:41,040
remainder equals to and if you remember
1223
00:45:39,319 --> 00:45:43,240
how to do the remainder it uses the
1224
00:45:41,040 --> 00:45:46,599
modulus symbol which is the percentage
1225
00:45:43,240 --> 00:45:48,280
sign so we can say num one percentage
1226
00:45:46,599 --> 00:45:50,960
num
1227
00:45:48,280 --> 00:45:53,680
two now it says output remainder to the
1228
00:45:50,960 --> 00:45:56,680
screen so we can use CW tubtub add
1229
00:45:53,680 --> 00:45:58,319
remainder in and hit run and just for
1230
00:45:56,680 --> 00:46:01,040
now we can see that the remainder is
1231
00:45:58,319 --> 00:46:03,599
indeed zero if the remainder is zero
1232
00:46:01,040 --> 00:46:06,880
then that means 10 is even because 10
1233
00:46:03,599 --> 00:46:08,839
modulus 2 gives a zero and if it's zero
1234
00:46:06,880 --> 00:46:11,040
then that means it's even if the
1235
00:46:08,839 --> 00:46:13,880
remainder was one then it would mean
1236
00:46:11,040 --> 00:46:17,599
it's odd so if we try 11 remainer two we
1237
00:46:13,880 --> 00:46:19,480
get one so 11 is indeed an odd number so
1238
00:46:17,599 --> 00:46:21,520
let's change this back to 10 and then it
1239
00:46:19,480 --> 00:46:23,599
says change the first into variable to
1240
00:46:21,520 --> 00:46:27,599
another number so just after here we
1241
00:46:23,599 --> 00:46:29,440
could say num one equals 11 and then
1242
00:46:27,599 --> 00:46:31,319
recalculate the remainder so we
1243
00:46:29,440 --> 00:46:33,480
essentially just need to do this section
1244
00:46:31,319 --> 00:46:35,720
again and we don't need to write in
1245
00:46:33,480 --> 00:46:37,800
before these two variables cuz int is
1246
00:46:35,720 --> 00:46:39,720
only use when you're declaring it this
1247
00:46:37,800 --> 00:46:42,520
is reassigning the
1248
00:46:39,720 --> 00:46:45,040
variable so we'll reassign variable num
1249
00:46:42,520 --> 00:46:47,359
one to 11 and then we reassign the
1250
00:46:45,040 --> 00:46:48,880
remainder to the calculation again and
1251
00:46:47,359 --> 00:46:51,119
now we can use the exact same line of
1252
00:46:48,880 --> 00:46:53,720
code above to print out the remainder to
1253
00:46:51,119 --> 00:46:56,680
the screen now you can see that it's 0er
1254
00:46:53,720 --> 00:46:59,880
and one it's zero because 10 is even and
1255
00:46:56,680 --> 00:47:02,240
it's one because 11 is
1256
00:46:59,880 --> 00:47:04,359
odd so far throughout the course we've
1257
00:47:02,240 --> 00:47:06,400
been using console right line to Output
1258
00:47:04,359 --> 00:47:08,720
stuff to the screen for example I can
1259
00:47:06,400 --> 00:47:11,480
output hello my name is Aba and we can
1260
00:47:08,720 --> 00:47:13,240
see it in the console the issue is so
1261
00:47:11,480 --> 00:47:14,920
far we haven't really brought any code
1262
00:47:13,240 --> 00:47:17,160
to be able to read in values from the
1263
00:47:14,920 --> 00:47:19,400
console so let's explore that option
1264
00:47:17,160 --> 00:47:21,920
right now so in the console Library we
1265
00:47:19,400 --> 00:47:23,720
have a console right line but what you
1266
00:47:21,920 --> 00:47:25,559
don't realize is we can also use this
1267
00:47:23,720 --> 00:47:27,520
console right line to ask the user a
1268
00:47:25,559 --> 00:47:31,240
question and to let the user type
1269
00:47:27,520 --> 00:47:33,880
something in so let's use CW tab tab and
1270
00:47:31,240 --> 00:47:36,160
say enter your
1271
00:47:33,880 --> 00:47:37,640
name with a callon and then end the
1272
00:47:36,160 --> 00:47:40,119
quotation
1273
00:47:37,640 --> 00:47:42,520
marks and then in order to return a
1274
00:47:40,119 --> 00:47:45,359
value from the console into your program
1275
00:47:42,520 --> 00:47:47,119
we need to use console.readline and if
1276
00:47:45,359 --> 00:47:49,640
you hover over readline let's have a
1277
00:47:47,119 --> 00:47:51,520
read at description reads the next line
1278
00:47:49,640 --> 00:47:53,880
of characters from the standard input
1279
00:47:51,520 --> 00:47:56,359
stream perfect this is exactly what we
1280
00:47:53,880 --> 00:47:58,079
want to do right now it's use at the end
1281
00:47:56,359 --> 00:47:59,880
of our code code just to stop the
1282
00:47:58,079 --> 00:48:02,000
console from closing so we can actually
1283
00:47:59,880 --> 00:48:03,359
see the output so if we just run this
1284
00:48:02,000 --> 00:48:05,440
right now we actually get the
1285
00:48:03,359 --> 00:48:07,960
opportunity to type but when we press
1286
00:48:05,440 --> 00:48:10,440
enter our program closes this is because
1287
00:48:07,960 --> 00:48:12,559
in this case we're simply using readline
1288
00:48:10,440 --> 00:48:15,400
just to wait until the user has pressed
1289
00:48:12,559 --> 00:48:17,440
enter so so our program doesn't close
1290
00:48:15,400 --> 00:48:19,599
but what you don't actually see is this
1291
00:48:17,440 --> 00:48:21,839
is returning as a string which means
1292
00:48:19,599 --> 00:48:23,760
that anything that we type into here and
1293
00:48:21,839 --> 00:48:25,599
when we press enter it actually gets a
1294
00:48:23,760 --> 00:48:27,480
return back to the code but we're not
1295
00:48:25,599 --> 00:48:30,480
doing anything with it because we don't
1296
00:48:27,480 --> 00:48:32,160
care at this point so now let's actually
1297
00:48:30,480 --> 00:48:34,480
do something that will make us care
1298
00:48:32,160 --> 00:48:36,599
about the user's input so we can make a
1299
00:48:34,480 --> 00:48:38,720
new string variable and assign it to
1300
00:48:36,599 --> 00:48:40,559
console readline and it should give us
1301
00:48:38,720 --> 00:48:43,200
the ability to store Whatever Gets
1302
00:48:40,559 --> 00:48:45,920
entered inside the console so we can
1303
00:48:43,200 --> 00:48:47,839
just do string name and we can end the
1304
00:48:45,920 --> 00:48:49,920
semicolon here and if you wanted to on
1305
00:48:47,839 --> 00:48:52,000
the next line you could do name equals
1306
00:48:49,920 --> 00:48:54,040
console do
1307
00:48:52,000 --> 00:48:55,559
readline or if you wanted to have it all
1308
00:48:54,040 --> 00:48:58,040
in one line then you can get rid of the
1309
00:48:55,559 --> 00:49:00,119
semicolon and bring it up like this this
1310
00:48:58,040 --> 00:49:01,640
is the preferred method as both lines
1311
00:49:00,119 --> 00:49:03,960
were on top of each other and it wasn't
1312
00:49:01,640 --> 00:49:06,119
really necessary to split them here
1313
00:49:03,960 --> 00:49:08,240
we're doing a declaration of string name
1314
00:49:06,119 --> 00:49:10,640
and an initialization and given it the
1315
00:49:08,240 --> 00:49:12,760
value of console. readline and this is
1316
00:49:10,640 --> 00:49:15,040
whatever value the user is going to type
1317
00:49:12,760 --> 00:49:18,319
in let's run our code and see what
1318
00:49:15,040 --> 00:49:21,240
happens enter your name abber now when I
1319
00:49:18,319 --> 00:49:23,520
press enter it successfully started ABA
1320
00:49:21,240 --> 00:49:25,599
inside this string name variable and now
1321
00:49:23,520 --> 00:49:28,160
it's waiting at this line the console
1322
00:49:25,599 --> 00:49:31,000
read line just just right here so when I
1323
00:49:28,160 --> 00:49:32,680
press enter the program closes so now
1324
00:49:31,000 --> 00:49:34,359
that we've made a variable and not done
1325
00:49:32,680 --> 00:49:35,960
anything with it let's print out to the
1326
00:49:34,359 --> 00:49:38,440
screen to make sure that something's
1327
00:49:35,960 --> 00:49:40,520
actually worked so if we use CW tab tab
1328
00:49:38,440 --> 00:49:42,359
and we type in name in the brackets and
1329
00:49:40,520 --> 00:49:44,599
we can run this code again and let's see
1330
00:49:42,359 --> 00:49:47,119
what happens as soon as I type in abber
1331
00:49:44,599 --> 00:49:48,799
and press enter then this second abber
1332
00:49:47,119 --> 00:49:51,440
is actually the variable printing out to
1333
00:49:48,799 --> 00:49:53,359
the screen so here it says enter your
1334
00:49:51,440 --> 00:49:55,319
name with a colon and normally when you
1335
00:49:53,359 --> 00:49:57,720
see the colon it sometimes lets you type
1336
00:49:55,319 --> 00:49:59,839
on the same line so if you'd prefer the
1337
00:49:57,720 --> 00:50:01,799
user to type on the same line then we
1338
00:49:59,839 --> 00:50:04,079
actually have another function for that
1339
00:50:01,799 --> 00:50:06,799
instead of using right line we can just
1340
00:50:04,079 --> 00:50:09,440
use right and the difference between
1341
00:50:06,799 --> 00:50:11,480
right line and right is the right line
1342
00:50:09,440 --> 00:50:13,520
will actually have a Terminator at the
1343
00:50:11,480 --> 00:50:15,319
end of the line to tell the console that
1344
00:50:13,520 --> 00:50:17,920
he wants to push everything after it
1345
00:50:15,319 --> 00:50:19,520
onto the next line whereas console.
1346
00:50:17,920 --> 00:50:22,520
right will just print it out to the
1347
00:50:19,520 --> 00:50:25,240
screen and not worry about any new lines
1348
00:50:22,520 --> 00:50:27,319
let's try run it now and now we can type
1349
00:50:25,240 --> 00:50:29,240
in abber as you can see it's a little
1350
00:50:27,319 --> 00:50:31,440
close to the colon so we can fix that in
1351
00:50:29,240 --> 00:50:34,319
a second so you press enter you can see
1352
00:50:31,440 --> 00:50:36,359
now that comes up on the second line
1353
00:50:34,319 --> 00:50:38,720
so we just put a space after the callon
1354
00:50:36,359 --> 00:50:40,280
just to give it a bit of room now if you
1355
00:50:38,720 --> 00:50:42,200
type in Abba you can see that there's a
1356
00:50:40,280 --> 00:50:44,359
bit of a gap and it's quite nicer to
1357
00:50:42,200 --> 00:50:47,000
look at now if we press enter you can
1358
00:50:44,359 --> 00:50:49,160
see that AB has been stored
1359
00:50:47,000 --> 00:50:52,040
successfully so what we can do now is
1360
00:50:49,160 --> 00:50:53,720
maybe ask for your age so we can have a
1361
00:50:52,040 --> 00:50:58,000
console.
1362
00:50:53,720 --> 00:51:00,839
right enter your age call on space and
1363
00:50:58,000 --> 00:51:03,280
then do a string age equals console.
1364
00:51:00,839 --> 00:51:05,559
readline and then we can have a age
1365
00:51:03,280 --> 00:51:08,599
output as well and let's see how that
1366
00:51:05,559 --> 00:51:10,839
works so we can say abber then say 23
1367
00:51:08,599 --> 00:51:13,240
and press enter and now we have abber
1368
00:51:10,839 --> 00:51:16,000
stored within the name variable and have
1369
00:51:13,240 --> 00:51:17,640
23 stored within the age variable so
1370
00:51:16,000 --> 00:51:19,680
let's try and output a string that's a
1371
00:51:17,640 --> 00:51:21,440
bit more meaningful to the user let's
1372
00:51:19,680 --> 00:51:24,240
clean this console up and see what we
1373
00:51:21,440 --> 00:51:25,960
can do so we don't need to Output their
1374
00:51:24,240 --> 00:51:28,000
result back to the console because they
1375
00:51:25,960 --> 00:51:29,440
literally just type that in and we don't
1376
00:51:28,000 --> 00:51:31,520
want to have this line because we just
1377
00:51:29,440 --> 00:51:34,559
use that as an example so if we run the
1378
00:51:31,520 --> 00:51:36,400
code right now it'll look better but it
1379
00:51:34,559 --> 00:51:39,000
does look a lot more structured enter
1380
00:51:36,400 --> 00:51:40,359
your name and then enter your age so now
1381
00:51:39,000 --> 00:51:42,960
when you press enter the program will
1382
00:51:40,359 --> 00:51:45,640
just close so let's try and output these
1383
00:51:42,960 --> 00:51:47,799
two variables in a formatted fashion so
1384
00:51:45,640 --> 00:51:50,160
as you now know we can use console. WR
1385
00:51:47,799 --> 00:51:53,119
to print a lot of stuff on one line so
1386
00:51:50,160 --> 00:51:56,880
we can use console. right and we can say
1387
00:51:53,119 --> 00:51:59,000
your name is and then add a space and
1388
00:51:56,880 --> 00:52:02,240
finish that line there and then
1389
00:51:59,000 --> 00:52:05,720
duplicate it down we can write name
1390
00:52:02,240 --> 00:52:09,920
duplicate that down and say space and
1391
00:52:05,720 --> 00:52:11,799
your age is duplicate that one down and
1392
00:52:09,920 --> 00:52:14,960
then we can type in age and let's see
1393
00:52:11,799 --> 00:52:17,559
what happens now abber
1394
00:52:14,960 --> 00:52:20,440
23 there we go so we actually have the
1395
00:52:17,559 --> 00:52:23,040
name output and the age output all on
1396
00:52:20,440 --> 00:52:24,200
one line if we all change these to right
1397
00:52:23,040 --> 00:52:25,839
lines then they're going to be on
1398
00:52:24,200 --> 00:52:27,880
separate lines and it's not going to
1399
00:52:25,839 --> 00:52:29,640
look very nice nice so I'm just going to
1400
00:52:27,880 --> 00:52:32,200
introduce you to a notation called
1401
00:52:29,640 --> 00:52:33,880
string concatenation this is the notion
1402
00:52:32,200 --> 00:52:35,799
of being able to concatenate two
1403
00:52:33,880 --> 00:52:38,119
different strings which just means to
1404
00:52:35,799 --> 00:52:40,040
add one string onto another as you can
1405
00:52:38,119 --> 00:52:42,359
see this is taken up four lines of code
1406
00:52:40,040 --> 00:52:44,319
and it's getting a bit ugly the better
1407
00:52:42,359 --> 00:52:47,000
and easier way to do this is by
1408
00:52:44,319 --> 00:52:49,079
containing it all within one line so we
1409
00:52:47,000 --> 00:52:52,160
can have a console right line and we can
1410
00:52:49,079 --> 00:52:53,960
take the first section your name is keep
1411
00:52:52,160 --> 00:52:56,079
the space at the end we can end the
1412
00:52:53,960 --> 00:52:58,000
speech marks and put a plus and then we
1413
00:52:56,079 --> 00:53:00,119
can send name so now we've just added
1414
00:52:58,000 --> 00:53:02,920
these two together if we just comment
1415
00:53:00,119 --> 00:53:04,559
out the rest of them and run the code
1416
00:53:02,920 --> 00:53:07,520
and you can see that they should be on
1417
00:53:04,559 --> 00:53:09,880
one line there you go so we can say your
1418
00:53:07,520 --> 00:53:13,040
name is Aba and let's just keep changing
1419
00:53:09,880 --> 00:53:16,280
the plus symbol so we can say and your
1420
00:53:13,040 --> 00:53:19,280
age is another space and the speech
1421
00:53:16,280 --> 00:53:21,960
marks and plus the age now we've got all
1422
00:53:19,280 --> 00:53:25,079
of this contained within one line and it
1423
00:53:21,960 --> 00:53:27,480
looks a lot better enter your name Abba
1424
00:53:25,079 --> 00:53:29,880
enter your age 23 and now you can see
1425
00:53:27,480 --> 00:53:32,160
exactly the same output but reduced to
1426
00:53:29,880 --> 00:53:32,160
one
1427
00:53:32,359 --> 00:53:36,599
line as you can see these three lines
1428
00:53:34,960 --> 00:53:38,480
are all stuck to each other so if you
1429
00:53:36,599 --> 00:53:41,119
wanted to add a blank line between these
1430
00:53:38,480 --> 00:53:43,640
two you can just put an MC console right
1431
00:53:41,119 --> 00:53:45,520
line just in here and what this will do
1432
00:53:43,640 --> 00:53:47,240
is simply just print the right line
1433
00:53:45,520 --> 00:53:50,119
character to the screen which will force
1434
00:53:47,240 --> 00:53:52,160
the console to make it on the next line
1435
00:53:50,119 --> 00:53:53,680
just like so here's the console right
1436
00:53:52,160 --> 00:53:55,640
line from the empty line that we just
1437
00:53:53,680 --> 00:53:58,319
wrote and this is the console right line
1438
00:53:55,640 --> 00:53:59,960
to Output the Val values so now that
1439
00:53:58,319 --> 00:54:01,920
you've understood how to read values in
1440
00:53:59,960 --> 00:54:04,040
from the console let's try and convert
1441
00:54:01,920 --> 00:54:05,599
some values as I showed you how to do
1442
00:54:04,040 --> 00:54:07,760
earlier on in the cuse you can actually
1443
00:54:05,599 --> 00:54:09,440
convert these strings into integers
1444
00:54:07,760 --> 00:54:11,920
ideally you want to store variables in
1445
00:54:09,440 --> 00:54:14,000
their data types as we covered before
1446
00:54:11,920 --> 00:54:16,079
ages should not be stored as strings
1447
00:54:14,000 --> 00:54:17,839
they should be stored as integers but if
1448
00:54:16,079 --> 00:54:19,880
you try and change this to an INT then
1449
00:54:17,839 --> 00:54:22,640
you're going to have an issue console
1450
00:54:19,880 --> 00:54:25,000
read line will return back a string so
1451
00:54:22,640 --> 00:54:27,559
therefore it can't implicitly convert
1452
00:54:25,000 --> 00:54:30,680
from a string to an integer so we need
1453
00:54:27,559 --> 00:54:33,680
to do this in two steps we can rename
1454
00:54:30,680 --> 00:54:37,240
this to age input and then on the line
1455
00:54:33,680 --> 00:54:41,119
underneath it we can say int age equals
1456
00:54:37,240 --> 00:54:42,040
convert to int 32 and then add age input
1457
00:54:41,119 --> 00:54:44,599
in these
1458
00:54:42,040 --> 00:54:47,000
brackets there we go and now when we
1459
00:54:44,599 --> 00:54:49,000
actually print the line of code nothing
1460
00:54:47,000 --> 00:54:51,480
should actually
1461
00:54:49,000 --> 00:54:54,480
change your name is abber and your age
1462
00:54:51,480 --> 00:54:56,000
is 23 but now what's actually changed is
1463
00:54:54,480 --> 00:54:58,520
we're storing the variables in the
1464
00:54:56,000 --> 00:54:59,880
correct format this is the ideal method
1465
00:54:58,520 --> 00:55:01,960
because in the next section when we
1466
00:54:59,880 --> 00:55:04,240
cover if statements you'll realize why
1467
00:55:01,960 --> 00:55:06,079
this is very important if you wanted to
1468
00:55:04,240 --> 00:55:08,359
minimize the amount of steps you need to
1469
00:55:06,079 --> 00:55:10,599
convert this then you can actually move
1470
00:55:08,359 --> 00:55:12,839
the console read line and get rid of the
1471
00:55:10,599 --> 00:55:14,920
string input variable and we can just
1472
00:55:12,839 --> 00:55:17,359
paste it straight into the bracket of
1473
00:55:14,920 --> 00:55:19,160
the converter in 32 and you can get rid
1474
00:55:17,359 --> 00:55:22,640
of the string input variable and
1475
00:55:19,160 --> 00:55:24,599
everything will work the same h 23 and
1476
00:55:22,640 --> 00:55:26,359
now it's working exactly the same if
1477
00:55:24,599 --> 00:55:28,079
this is too many steps for you or if
1478
00:55:26,359 --> 00:55:29,880
this looks too complicated then just
1479
00:55:28,079 --> 00:55:32,760
please revert it back just to keep it
1480
00:55:29,880 --> 00:55:35,079
the same way for the time
1481
00:55:32,760 --> 00:55:36,799
being so now we've learned how to read
1482
00:55:35,079 --> 00:55:38,720
in values from the console and output
1483
00:55:36,799 --> 00:55:40,559
them back to the screen now let's see
1484
00:55:38,720 --> 00:55:43,520
how we can use if statements to divert
1485
00:55:40,559 --> 00:55:45,160
our code into multiple paths so in the
1486
00:55:43,520 --> 00:55:47,280
previous section I told you that it's
1487
00:55:45,160 --> 00:55:50,280
very crucial for our code to have this
1488
00:55:47,280 --> 00:55:52,160
int age and the reason why is age is
1489
00:55:50,280 --> 00:55:54,359
meant to be stored as a number and not
1490
00:55:52,160 --> 00:55:56,559
as a string so we read it in first as a
1491
00:55:54,359 --> 00:55:58,599
string and then convert it to an integer
1492
00:55:56,559 --> 00:56:01,839
using this function and store it into
1493
00:55:58,599 --> 00:56:04,000
our age variable this is the reason why
1494
00:56:01,839 --> 00:56:06,079
so our condition goes as follows we
1495
00:56:04,000 --> 00:56:08,319
would write the word if and between the
1496
00:56:06,079 --> 00:56:10,880
brackets this needs to be evaluated to
1497
00:56:08,319 --> 00:56:12,799
either a true or false value and then we
1498
00:56:10,880 --> 00:56:15,240
can have the curly braces to tell us
1499
00:56:12,799 --> 00:56:16,880
what to do if this becomes true so in
1500
00:56:15,240 --> 00:56:19,720
this case you want to just check if our
1501
00:56:16,880 --> 00:56:21,079
age input is greater than or equal to 18
1502
00:56:19,720 --> 00:56:23,839
let's say this was an entrance to a
1503
00:56:21,079 --> 00:56:26,680
place that only allowed overes to enter
1504
00:56:23,839 --> 00:56:29,200
so we can do age input which refer es
1505
00:56:26,680 --> 00:56:32,039
the age input variable and we can use
1506
00:56:29,200 --> 00:56:34,440
the operators equals greater than
1507
00:56:32,039 --> 00:56:36,920
greater than or equal to less than or
1508
00:56:34,440 --> 00:56:38,280
less than or equal to or not equal to so
1509
00:56:36,920 --> 00:56:40,960
these are all the different options that
1510
00:56:38,280 --> 00:56:42,799
we have we can say if the age input is
1511
00:56:40,960 --> 00:56:45,480
equal to
1512
00:56:42,799 --> 00:56:48,200
18 perfect so now that we've read in the
1513
00:56:45,480 --> 00:56:50,760
age input we can check if it's 18 let's
1514
00:56:48,200 --> 00:56:53,599
just process a message in here says you
1515
00:56:50,760 --> 00:56:56,760
are 18 and let's have a look and see if
1516
00:56:53,599 --> 00:56:56,760
this works
1517
00:56:56,920 --> 00:57:02,760
18 now I press enter it says you are
1518
00:57:00,440 --> 00:57:04,400
18 but this is the problem with storing
1519
00:57:02,760 --> 00:57:07,200
everything as a string and then trying
1520
00:57:04,400 --> 00:57:10,200
to use it as if it was a number if I
1521
00:57:07,200 --> 00:57:11,599
type in 18 followed by a space as you
1522
00:57:10,200 --> 00:57:13,240
can see there's a blank space here now
1523
00:57:11,599 --> 00:57:15,240
and press enter you can see that it
1524
00:57:13,240 --> 00:57:17,280
doesn't come up and the reason why is
1525
00:57:15,240 --> 00:57:19,640
it's processing it like this with a
1526
00:57:17,280 --> 00:57:21,680
space at the end in order to avoid
1527
00:57:19,640 --> 00:57:24,000
situations like this this is why we
1528
00:57:21,680 --> 00:57:26,200
would convert it to a number and the
1529
00:57:24,000 --> 00:57:28,240
reason why we're using equals to here is
1530
00:57:26,200 --> 00:57:29,839
we can't use greater than or equal to
1531
00:57:28,240 --> 00:57:32,039
because strings can't be greater than or
1532
00:57:29,839 --> 00:57:35,359
equal to another string they can only
1533
00:57:32,039 --> 00:57:37,960
ever be equal to or not equal to string
1534
00:57:35,359 --> 00:57:40,039
is just text so if something equals to
1535
00:57:37,960 --> 00:57:41,280
something then it's fine the only way
1536
00:57:40,039 --> 00:57:43,440
that we can use these different
1537
00:57:41,280 --> 00:57:45,760
operators is if we actually interact
1538
00:57:43,440 --> 00:57:48,000
with our integer variable now let's see
1539
00:57:45,760 --> 00:57:50,680
how we can use this so we can say age
1540
00:57:48,000 --> 00:57:52,640
equals equals 18 and now you can see we
1541
00:57:50,680 --> 00:57:55,799
actually reference it as a number now
1542
00:57:52,640 --> 00:57:59,240
let's run the code if I do 18 Follow by
1543
00:57:55,799 --> 00:58:01,319
space it still works and the reason why
1544
00:57:59,240 --> 00:58:03,480
is it actually dismisses that space when
1545
00:58:01,319 --> 00:58:05,200
it's converting into the integer so now
1546
00:58:03,480 --> 00:58:07,319
a just started as an integer and we can
1547
00:58:05,200 --> 00:58:09,920
actually manipulate this a bit better so
1548
00:58:07,319 --> 00:58:12,280
instead of saying you are 18 we can say
1549
00:58:09,920 --> 00:58:15,160
you are 18 or older and now instead of
1550
00:58:12,280 --> 00:58:16,520
doing equal to we can have greater than
1551
00:58:15,160 --> 00:58:18,680
if we have greater than then we're
1552
00:58:16,520 --> 00:58:21,240
actually dismissing the number 18
1553
00:58:18,680 --> 00:58:23,960
because if the age is greater than 18 so
1554
00:58:21,240 --> 00:58:26,400
this will read like this and 18 is not
1555
00:58:23,960 --> 00:58:28,599
greater than 18 so this will return back
1556
00:58:26,400 --> 00:58:31,400
false what we actually want to use is
1557
00:58:28,599 --> 00:58:33,480
greater than or equal to if the age that
1558
00:58:31,400 --> 00:58:37,720
we've inputed is greater than or equal
1559
00:58:33,480 --> 00:58:40,160
to 18 then you are 18 hence the equal to
1560
00:58:37,720 --> 00:58:44,520
are greater than if you're older so now
1561
00:58:40,160 --> 00:58:47,559
let's try and run this AB 23 perfect you
1562
00:58:44,520 --> 00:58:49,359
are 18 or older let's change so let's
1563
00:58:47,559 --> 00:58:51,839
change some other if statements to add
1564
00:58:49,359 --> 00:58:54,799
some additional conditions so we can say
1565
00:58:51,839 --> 00:58:57,280
else if and the only reason why it would
1566
00:58:54,799 --> 00:59:00,160
come into this bracket if this condition
1567
00:58:57,280 --> 00:59:02,599
returns false it will move down the next
1568
00:59:00,160 --> 00:59:04,760
condition in the else chain if this was
1569
00:59:02,599 --> 00:59:06,799
wrote as a separate if statement then
1570
00:59:04,760 --> 00:59:08,880
this would run completely independently
1571
00:59:06,799 --> 00:59:10,599
from this one if you're going to have
1572
00:59:08,880 --> 00:59:12,920
chained if statements you need to make
1573
00:59:10,599 --> 00:59:15,000
sure that you chain them correctly in
1574
00:59:12,920 --> 00:59:18,319
this case I want to check if it ages
1575
00:59:15,000 --> 00:59:20,200
between 18 and 25 and then move on to a
1576
00:59:18,319 --> 00:59:22,079
different age bracket and these two
1577
00:59:20,200 --> 00:59:24,400
things are grouped together so we should
1578
00:59:22,079 --> 00:59:26,839
have this as an else if because with an
1579
00:59:24,400 --> 00:59:29,319
edge bracket you can only be of one age
1580
00:59:26,839 --> 00:59:32,599
bracket and not multiple so we can say
1581
00:59:29,319 --> 00:59:34,359
if age is greater than or equal to 25
1582
00:59:32,599 --> 00:59:36,799
and we can copy this and we can say you
1583
00:59:34,359 --> 00:59:39,119
are 25 or older now let's see what
1584
00:59:36,799 --> 00:59:40,680
happens enter your name abber and let's
1585
00:59:39,119 --> 00:59:44,119
just say that I am
1586
00:59:40,680 --> 00:59:46,920
35 it says you're 18 or older but I told
1587
00:59:44,119 --> 00:59:49,720
it the program I was 35 now the problem
1588
00:59:46,920 --> 00:59:51,839
we're facing is any value that's over 18
1589
00:59:49,720 --> 00:59:53,880
will always enter inside here and it
1590
00:59:51,839 --> 00:59:56,160
will skip over this one completely this
1591
00:59:53,880 --> 00:59:57,920
is not the behavior that we want so so
1592
00:59:56,160 --> 00:59:59,520
what we can do in our if statements is
1593
00:59:57,920 --> 01:00:01,880
we can use the double pipe which
1594
00:59:59,520 --> 01:00:04,160
represents or or the double Ampersand
1595
01:00:01,880 --> 01:00:06,000
which represents and and we can change
1596
01:00:04,160 --> 01:00:09,000
some conditions in here to make sure
1597
01:00:06,000 --> 01:00:11,039
that you implement some sort of range so
1598
01:00:09,000 --> 01:00:14,319
we can actually have a range between 18
1599
01:00:11,039 --> 01:00:18,440
and 25 so we can say if the age is
1600
01:00:14,319 --> 01:00:21,000
greater than or equal to 18 and the age
1601
01:00:18,440 --> 01:00:25,440
is less than or equal to 25 and we can
1602
01:00:21,000 --> 01:00:27,720
tweak this message you are between 18
1603
01:00:25,440 --> 01:00:30,000
and
1604
01:00:27,720 --> 01:00:34,240
25 and let's just run that for now and
1605
01:00:30,000 --> 01:00:35,960
see what happens abber 25 now it says 18
1606
01:00:34,240 --> 01:00:38,799
and
1607
01:00:35,960 --> 01:00:41,960
25 so now that we've got between 18 and
1608
01:00:38,799 --> 01:00:44,559
25 here we can tweak this to be greater
1609
01:00:41,960 --> 01:00:47,599
than equal to
1610
01:00:44,559 --> 01:00:50,280
26 if you're 25 will enter this
1611
01:00:47,599 --> 01:00:52,480
condition and if you're 26 or older into
1612
01:00:50,280 --> 01:00:53,640
this condition so let's test that now if
1613
01:00:52,480 --> 01:00:57,280
we enter
1614
01:00:53,640 --> 01:00:58,480
35 now it says your 26 older that's
1615
01:00:57,280 --> 01:01:00,599
perfect because now that we've
1616
01:00:58,480 --> 01:01:02,359
implemented range checking it gives us
1617
01:01:00,599 --> 01:01:04,799
the ability to differentiate between
1618
01:01:02,359 --> 01:01:06,720
different age groups more precisely
1619
01:01:04,799 --> 01:01:09,000
let's Show an example of how to use the
1620
01:01:06,720 --> 01:01:10,799
R so if we just have a block comment
1621
01:01:09,000 --> 01:01:13,559
around all of this we can write an if
1622
01:01:10,799 --> 01:01:16,880
statement in here and we can say if the
1623
01:01:13,559 --> 01:01:19,599
age is smaller than zero or the age is
1624
01:01:16,880 --> 01:01:23,280
bigger than 150 then we can output to
1625
01:01:19,599 --> 01:01:26,400
the screen in valid Age and what this is
1626
01:01:23,280 --> 01:01:28,640
saying is if we're less than zero and
1627
01:01:26,400 --> 01:01:30,359
notice that this is less than zero so
1628
01:01:28,640 --> 01:01:34,359
this value will actually start at minus
1629
01:01:30,359 --> 01:01:38,680
one and this value start at 150 is minus
1630
01:01:34,359 --> 01:01:41,319
one or less or over 150 then this will
1631
01:01:38,680 --> 01:01:43,440
happen notice why it's an or one of
1632
01:01:41,319 --> 01:01:46,599
these need to be true in order for this
1633
01:01:43,440 --> 01:01:49,039
to be true when you've got an and both
1634
01:01:46,599 --> 01:01:51,440
sides need to be true in order for this
1635
01:01:49,039 --> 01:01:54,000
condition to invalu it is true so let's
1636
01:01:51,440 --> 01:01:57,279
try our code we can say Abu and we put
1637
01:01:54,000 --> 01:01:58,960
minus one and now it says invalid age
1638
01:01:57,279 --> 01:02:02,880
which is perfect because this is our
1639
01:01:58,960 --> 01:02:05,400
range checking and we can try again with
1640
01:02:02,880 --> 01:02:08,480
151 as it's just outside the range it
1641
01:02:05,400 --> 01:02:12,039
will say invalid age and if we have an
1642
01:02:08,480 --> 01:02:15,279
else on here we can print valid
1643
01:02:12,039 --> 01:02:17,839
age notice there is no condition in here
1644
01:02:15,279 --> 01:02:20,440
if you have an open else like this it
1645
01:02:17,839 --> 01:02:22,960
means If This Were to return false it
1646
01:02:20,440 --> 01:02:25,359
will always do this regardless of any
1647
01:02:22,960 --> 01:02:27,599
condition in this case it's perfectly
1648
01:02:25,359 --> 01:02:30,000
reasonable to do this because we know
1649
01:02:27,599 --> 01:02:33,480
that any values that are underneath zero
1650
01:02:30,000 --> 01:02:37,599
or over 150 is invalid and anything
1651
01:02:33,480 --> 01:02:40,119
between 0 and 150 is indeed valid what
1652
01:02:37,599 --> 01:02:42,960
you can do instead is actually move
1653
01:02:40,119 --> 01:02:45,160
these if statements inside here so
1654
01:02:42,960 --> 01:02:47,559
instead of just printing out valid age
1655
01:02:45,160 --> 01:02:50,000
we can actually check the age as
1656
01:02:47,559 --> 01:02:52,599
well so let's run this now and we can
1657
01:02:50,000 --> 01:02:55,079
say Aba minus one and it will only print
1658
01:02:52,599 --> 01:02:56,920
out invalid age and then move on because
1659
01:02:55,079 --> 01:02:59,039
once it reads the this line of code it
1660
01:02:56,920 --> 01:03:01,680
will actually skip over the L's and
1661
01:02:59,039 --> 01:03:03,559
finish up down here and now if you enter
1662
01:03:01,680 --> 01:03:07,000
a real
1663
01:03:03,559 --> 01:03:09,240
value you are between 18 and 25 so let's
1664
01:03:07,000 --> 01:03:10,640
follow the path it got to here and
1665
01:03:09,240 --> 01:03:12,599
realize that the range check was
1666
01:03:10,640 --> 01:03:14,839
actually correct so jumped out of this
1667
01:03:12,599 --> 01:03:16,880
curly brace and went to the else because
1668
01:03:14,839 --> 01:03:19,200
this had returned false we jump into
1669
01:03:16,880 --> 01:03:22,039
this else and now we start checking if
1670
01:03:19,200 --> 01:03:24,520
ages between 18 and 25 which it was
1671
01:03:22,039 --> 01:03:27,760
because our age variable is 23 then it
1672
01:03:24,520 --> 01:03:27,760
outputed this to the screen
1673
01:03:28,520 --> 01:03:32,920
now if we do the same thing but have 35
1674
01:03:30,960 --> 01:03:35,200
instead then it follows the exact same
1675
01:03:32,920 --> 01:03:36,960
path except towards here this will
1676
01:03:35,200 --> 01:03:40,240
return back false and then it will check
1677
01:03:36,960 --> 01:03:42,160
the next condition if age is over 26
1678
01:03:40,240 --> 01:03:44,400
then we'll return
1679
01:03:42,160 --> 01:03:46,039
this so now let's just make a quick
1680
01:03:44,400 --> 01:03:47,599
multiplication program using if
1681
01:03:46,039 --> 01:03:49,839
statement to see if you can get the
1682
01:03:47,599 --> 01:03:51,200
answer correct so let's just comment
1683
01:03:49,839 --> 01:03:53,559
everything
1684
01:03:51,200 --> 01:03:56,240
out and we'll start the next section
1685
01:03:53,559 --> 01:03:58,640
underneath
1686
01:03:56,240 --> 01:04:01,480
so let's make a console. write and ask
1687
01:03:58,640 --> 01:04:05,599
the user to enter the first
1688
01:04:01,480 --> 01:04:09,720
number and then we can say int number a
1689
01:04:05,599 --> 01:04:11,680
equals convert to in32 and the value
1690
01:04:09,720 --> 01:04:13,839
that we want to convert is a console.
1691
01:04:11,680 --> 01:04:16,119
readline which is whatever the user
1692
01:04:13,839 --> 01:04:18,200
entered from the console again if you're
1693
01:04:16,119 --> 01:04:21,039
unfamiliar with this format you can just
1694
01:04:18,200 --> 01:04:23,200
type it in manually number a input
1695
01:04:21,039 --> 01:04:25,039
equals console read line and then we can
1696
01:04:23,200 --> 01:04:26,720
put the number input in here now if you
1697
01:04:25,039 --> 01:04:28,880
use shift and highlight these lines and
1698
01:04:26,720 --> 01:04:31,160
press contrl D we can duplicate this
1699
01:04:28,880 --> 01:04:35,319
entire section and we can say enter the
1700
01:04:31,160 --> 01:04:38,960
second number change a to B change a to
1701
01:04:35,319 --> 01:04:40,400
B and then finally change a to B perfect
1702
01:04:38,960 --> 01:04:42,520
so now we've got enter the first number
1703
01:04:40,400 --> 01:04:44,119
and enter the second number and we can
1704
01:04:42,520 --> 01:04:49,920
say our
1705
01:04:44,119 --> 01:04:54,119
answer equal to number a multi by number
1706
01:04:49,920 --> 01:04:58,079
B and then we can have a console. WR
1707
01:04:54,119 --> 01:05:03,279
value of we can add to it number A plus
1708
01:04:58,079 --> 01:05:05,319
an X Plus number B and then finally plus
1709
01:05:03,279 --> 01:05:07,960
a callon for the user to type in and
1710
01:05:05,319 --> 01:05:11,359
then we can say string answer input
1711
01:05:07,960 --> 01:05:17,079
equals console. readline and then
1712
01:05:11,359 --> 01:05:19,000
int actual answer equals convert to in32
1713
01:05:17,079 --> 01:05:21,200
and then we can have the input in
1714
01:05:19,000 --> 01:05:23,200
here so now that we've got one number
1715
01:05:21,200 --> 01:05:25,400
from the console the second number from
1716
01:05:23,200 --> 01:05:27,400
the console we're just making a variable
1717
01:05:25,400 --> 01:05:29,920
that will multiply them together and
1718
01:05:27,400 --> 01:05:31,720
store inside answer and then we're just
1719
01:05:29,920 --> 01:05:34,039
going to ask the user what's the value
1720
01:05:31,720 --> 01:05:36,240
of number a times my number B and they
1721
01:05:34,039 --> 01:05:38,119
can input that we store it as a string
1722
01:05:36,240 --> 01:05:41,319
and then convert it to an integer and
1723
01:05:38,119 --> 01:05:43,000
store the answer into actual answer so
1724
01:05:41,319 --> 01:05:45,559
now just after the section we can write
1725
01:05:43,000 --> 01:05:48,119
an if statement that says if the answer
1726
01:05:45,559 --> 01:05:50,119
equals to the actual answer then we can
1727
01:05:48,119 --> 01:05:53,319
say Well
1728
01:05:50,119 --> 01:05:55,000
done and else and the reason why we
1729
01:05:53,319 --> 01:05:57,240
don't need to condition this else
1730
01:05:55,000 --> 01:05:59,119
because with equals to the only opposite
1731
01:05:57,240 --> 01:06:01,880
thing you can have is if it's not equal
1732
01:05:59,119 --> 01:06:04,520
to so if you really wanted to you could
1733
01:06:01,880 --> 01:06:08,119
also write if answer is not equal to
1734
01:06:04,520 --> 01:06:11,680
actual answer then in here you can say
1735
01:06:08,119 --> 01:06:13,160
close but it was wrong you don't
1736
01:06:11,680 --> 01:06:15,279
actually need this this is just for
1737
01:06:13,160 --> 01:06:16,880
learning purposes because this will
1738
01:06:15,279 --> 01:06:18,920
return back false if they're not equal
1739
01:06:16,880 --> 01:06:20,960
to each other so this statement is just
1740
01:06:18,920 --> 01:06:22,720
a given if they're not equal to each
1741
01:06:20,960 --> 01:06:24,240
other then we can just assume that
1742
01:06:22,720 --> 01:06:26,720
they're not equal to each other so we
1743
01:06:24,240 --> 01:06:29,200
don't need this additional statement if
1744
01:06:26,720 --> 01:06:30,720
this condition evaluates to being false
1745
01:06:29,200 --> 01:06:32,720
then that means they're not equal to
1746
01:06:30,720 --> 01:06:34,559
each other we don't need this additional
1747
01:06:32,720 --> 01:06:36,559
check to check if they're not equal to
1748
01:06:34,559 --> 01:06:38,799
each other because that's a given if
1749
01:06:36,559 --> 01:06:42,079
this returns back false so we can just
1750
01:06:38,799 --> 01:06:47,039
leave this as an else let's try run the
1751
01:06:42,079 --> 01:06:49,400
code 10 10 value of 10 * 10 100 now it
1752
01:06:47,039 --> 01:06:51,760
says well done and let's just try that
1753
01:06:49,400 --> 01:06:55,359
again and we can enter a different value
1754
01:06:51,760 --> 01:06:56,559
90 Clause but it was wrong so let's just
1755
01:06:55,359 --> 01:06:59,119
have have a quick
1756
01:06:56,559 --> 01:07:00,680
recap so in the previous section we
1757
01:06:59,119 --> 01:07:02,760
talked about how to input from the
1758
01:07:00,680 --> 01:07:04,599
console and we generated this line of
1759
01:07:02,760 --> 01:07:07,119
code which will output your name and
1760
01:07:04,599 --> 01:07:08,760
your age in one line to the console and
1761
01:07:07,119 --> 01:07:10,839
then we looked at how we can use these
1762
01:07:08,760 --> 01:07:12,599
different operators in our if statements
1763
01:07:10,839 --> 01:07:15,760
and we did some range check in to check
1764
01:07:12,599 --> 01:07:18,000
if the age was between 18 and 25 and it
1765
01:07:15,760 --> 01:07:20,160
would output something to the screen and
1766
01:07:18,000 --> 01:07:22,640
if it was over 26 you'd get a different
1767
01:07:20,160 --> 01:07:24,200
output to the screen and then we took
1768
01:07:22,640 --> 01:07:26,079
that a little bit further and started
1769
01:07:24,200 --> 01:07:28,720
using the r operator
1770
01:07:26,079 --> 01:07:31,680
if the age is smaller than zero or it's
1771
01:07:28,720 --> 01:07:34,039
greater than 150 then it must be invalid
1772
01:07:31,680 --> 01:07:37,200
and if this condition returns false it
1773
01:07:34,039 --> 01:07:39,599
must be between 0 and 150 so we can come
1774
01:07:37,200 --> 01:07:41,480
into this L's bracket and inside here we
1775
01:07:39,599 --> 01:07:43,960
can do our range checkin from the
1776
01:07:41,480 --> 01:07:46,480
previous bit of code we can check the
1777
01:07:43,960 --> 01:07:48,960
ranges between 18 and 25 and print a
1778
01:07:46,480 --> 01:07:50,799
value or if it's over 26 we can print
1779
01:07:48,960 --> 01:07:52,960
another value and you can keep changing
1780
01:07:50,799 --> 01:07:57,480
these if you want you could have 26 to
1781
01:07:52,960 --> 01:07:59,960
40 and 40 to 60 Etc Etc that's up to
1782
01:07:57,480 --> 01:08:02,079
you so this is just a quick little
1783
01:07:59,960 --> 01:08:04,119
program to simulate multiplication
1784
01:08:02,079 --> 01:08:06,440
program we can enter the first number
1785
01:08:04,119 --> 01:08:08,799
using console. WR and then we start it
1786
01:08:06,440 --> 01:08:11,480
as a string and we put that string into
1787
01:08:08,799 --> 01:08:14,000
the converter in32 function to give us
1788
01:08:11,480 --> 01:08:15,920
back an integer variable and we do
1789
01:08:14,000 --> 01:08:18,199
exactly the same thing for the second
1790
01:08:15,920 --> 01:08:20,359
number and then we make a variable to
1791
01:08:18,199 --> 01:08:22,759
store both numbers multiplied by each
1792
01:08:20,359 --> 01:08:25,120
other we ask the user what is the value
1793
01:08:22,759 --> 01:08:26,920
of number a Times by number B and we
1794
01:08:25,120 --> 01:08:29,560
give them an opportunity to type in a
1795
01:08:26,920 --> 01:08:32,080
value we read that value in convert it
1796
01:08:29,560 --> 01:08:34,279
to an integer store it as an into actual
1797
01:08:32,080 --> 01:08:36,520
answer and then in our if statement we
1798
01:08:34,279 --> 01:08:40,080
say if the answer is equal to the actual
1799
01:08:36,520 --> 01:08:42,400
answer then we print well done to the
1800
01:08:40,080 --> 01:08:44,880
screen double equals are used for
1801
01:08:42,400 --> 01:08:47,520
equality and single equals are used for
1802
01:08:44,880 --> 01:08:49,199
assignment don't get them mixed up we
1803
01:08:47,520 --> 01:08:51,679
don't need an else condition in here
1804
01:08:49,199 --> 01:08:54,359
because if this returns true then this
1805
01:08:51,679 --> 01:08:55,960
will execute if this is indeed false
1806
01:08:54,359 --> 01:08:57,640
that they're not equal to each other
1807
01:08:55,960 --> 01:08:59,159
then we always want to do this section
1808
01:08:57,640 --> 01:09:01,480
and we don't need to put an additional
1809
01:08:59,159 --> 01:09:03,759
if statement inside here if you wanted
1810
01:09:01,480 --> 01:09:07,719
to but it's not necessary you can have
1811
01:09:03,759 --> 01:09:09,920
lse if answer is not equal to actual
1812
01:09:07,719 --> 01:09:12,080
answer but this is just extra code and
1813
01:09:09,920 --> 01:09:14,120
it hurts the readability and it should
1814
01:09:12,080 --> 01:09:16,239
be
1815
01:09:14,120 --> 01:09:18,000
avoided if statements are one of the
1816
01:09:16,239 --> 01:09:20,199
methods we can decide different paths in
1817
01:09:18,000 --> 01:09:22,000
our code the next method we can use is
1818
01:09:20,199 --> 01:09:23,799
called switch statements so let's write
1819
01:09:22,000 --> 01:09:26,319
a little program so we can demonstrate
1820
01:09:23,799 --> 01:09:29,480
this so we can just have a console right
1821
01:09:26,319 --> 01:09:32,759
enter date of the week and we can have
1822
01:09:29,480 --> 01:09:35,679
in date equals convert to in32 and we
1823
01:09:32,759 --> 01:09:37,319
can place our console. readline inside
1824
01:09:35,679 --> 01:09:39,400
the brackets so we take whatever the
1825
01:09:37,319 --> 01:09:41,520
user typed into the console and then
1826
01:09:39,400 --> 01:09:42,880
place it inside here and inside here
1827
01:09:41,520 --> 01:09:45,480
we're going to convert whatever they've
1828
01:09:42,880 --> 01:09:47,480
typed in to an integer 32 and star it
1829
01:09:45,480 --> 01:09:48,799
into our D variable so if we just print
1830
01:09:47,480 --> 01:09:49,640
this out to the screen to make sure it's
1831
01:09:48,799 --> 01:09:52,199
working
1832
01:09:49,640 --> 01:09:53,719
fine if we just type in seven we can see
1833
01:09:52,199 --> 01:09:56,120
that seven is returned so it's all
1834
01:09:53,719 --> 01:09:59,679
working so normally what we do is type
1835
01:09:56,120 --> 01:10:02,679
if D equals equals 1 and then we can add
1836
01:09:59,679 --> 01:10:04,600
the Cy braces and use CW tab tab and we
1837
01:10:02,679 --> 01:10:07,000
can print out Munday now if you run the
1838
01:10:04,600 --> 01:10:09,280
code and try use number one then Monday
1839
01:10:07,000 --> 01:10:11,679
works and that's fine and then let's do
1840
01:10:09,280 --> 01:10:14,080
another LS if and we can say day equals
1841
01:10:11,679 --> 01:10:16,760
equals 2 and then we can print
1842
01:10:14,080 --> 01:10:18,880
Tuesday perfect so if we try that
1843
01:10:16,760 --> 01:10:20,880
now there we go so now we have the
1844
01:10:18,880 --> 01:10:22,760
ability to print out Monday and Tuesday
1845
01:10:20,880 --> 01:10:25,080
to the console depending what day the
1846
01:10:22,760 --> 01:10:26,640
week the user enters but since we're
1847
01:10:25,080 --> 01:10:28,440
going to have have seven different paths
1848
01:10:26,640 --> 01:10:29,920
because we got seven days of the week
1849
01:10:28,440 --> 01:10:32,040
then sometimes it might be better and
1850
01:10:29,920 --> 01:10:33,800
cleaner to use a switch statement so
1851
01:10:32,040 --> 01:10:35,760
let's have a look at the syntax so we
1852
01:10:33,800 --> 01:10:37,400
can type in switch and then inside the
1853
01:10:35,760 --> 01:10:39,440
brackets it's what variable we want to
1854
01:10:37,400 --> 01:10:41,440
have a switch on so in this case we want
1855
01:10:39,440 --> 01:10:43,440
to be using the day variable so we can
1856
01:10:41,440 --> 01:10:44,719
add D in the brackets this is not a
1857
01:10:43,440 --> 01:10:46,640
condition in the brackets this is just
1858
01:10:44,719 --> 01:10:48,600
telling us what variable we're going to
1859
01:10:46,640 --> 01:10:50,719
be using for the switch statement so
1860
01:10:48,600 --> 01:10:52,640
let's open the curly braces after and
1861
01:10:50,719 --> 01:10:55,320
inside here instead of having paths like
1862
01:10:52,640 --> 01:10:57,440
this we're going to use cases so this
1863
01:10:55,320 --> 01:11:00,199
can be translated to in the case of
1864
01:10:57,440 --> 01:11:03,120
number one we can do this in the case of
1865
01:11:00,199 --> 01:11:05,280
number two we can do this so let's try
1866
01:11:03,120 --> 01:11:07,159
and see how it works we can type in case
1867
01:11:05,280 --> 01:11:09,120
followed by number one and then add a
1868
01:11:07,159 --> 01:11:12,040
callon and then we can type out our
1869
01:11:09,120 --> 01:11:14,480
Monday in here and then we need to
1870
01:11:12,040 --> 01:11:16,920
follow each case off by finishing the
1871
01:11:14,480 --> 01:11:18,840
section off with a break this break's
1872
01:11:16,920 --> 01:11:20,760
very essential and you'll get a syntax
1873
01:11:18,840 --> 01:11:22,679
error if you don't put it in so now if
1874
01:11:20,760 --> 01:11:25,600
we do case two and we do exactly the
1875
01:11:22,679 --> 01:11:27,080
same thing there we go and if I dis miss
1876
01:11:25,600 --> 01:11:29,400
the break then you'll realize that it
1877
01:11:27,080 --> 01:11:31,719
says it cannot fall out so we have to
1878
01:11:29,400 --> 01:11:33,800
use a break just the compiler knows that
1879
01:11:31,719 --> 01:11:35,920
we want to break here the reason why
1880
01:11:33,800 --> 01:11:38,360
this break is placed inside here is
1881
01:11:35,920 --> 01:11:41,040
because what we can do is we can say
1882
01:11:38,360 --> 01:11:43,840
case two and then add another one in
1883
01:11:41,040 --> 01:11:46,360
here called case 3 and what this now
1884
01:11:43,840 --> 01:11:49,679
reads is if we enter two it says choose
1885
01:11:46,360 --> 01:11:52,239
there and if we enter three It also says
1886
01:11:49,679 --> 01:11:53,920
choose it so what you can do is you can
1887
01:11:52,239 --> 01:11:57,920
actually chain so this would be
1888
01:11:53,920 --> 01:12:00,480
equivalent to writing if D equals 2 or D
1889
01:11:57,920 --> 01:12:02,679
equals equals 3 now this is exact
1890
01:12:00,480 --> 01:12:04,800
translation these two are exactly the
1891
01:12:02,679 --> 01:12:07,679
same as of right now we have D equal
1892
01:12:04,800 --> 01:12:11,560
equal 1 which is our K1 and then equals
1893
01:12:07,679 --> 01:12:13,840
2 Ral 3 is right here if we go into the
1894
01:12:11,560 --> 01:12:15,520
case 2 path it will fall in the case 3
1895
01:12:13,840 --> 01:12:17,960
so we're saying for this break is we
1896
01:12:15,520 --> 01:12:19,520
want to end this section here so now if
1897
01:12:17,960 --> 01:12:20,679
we just revert this back and continue
1898
01:12:19,520 --> 01:12:22,920
the
1899
01:12:20,679 --> 01:12:26,239
chain I'll just skip this part just so
1900
01:12:22,920 --> 01:12:27,520
it's a little bit quicker
1901
01:12:26,239 --> 01:12:29,000
there we go so now that we've got all
1902
01:12:27,520 --> 01:12:31,840
seven days of the week let's just see if
1903
01:12:29,000 --> 01:12:33,920
it works let's try six and we have
1904
01:12:31,840 --> 01:12:35,800
Saturday so now we're kind of missing
1905
01:12:33,920 --> 01:12:38,199
something from here the only thing we're
1906
01:12:35,800 --> 01:12:40,280
missing is having our external lse just
1907
01:12:38,199 --> 01:12:41,760
in case the user types something invalid
1908
01:12:40,280 --> 01:12:46,120
so normally we would write something
1909
01:12:41,760 --> 01:12:47,600
like this enter value between 1 and 7
1910
01:12:46,120 --> 01:12:48,639
just to make sure that the user knows
1911
01:12:47,600 --> 01:12:51,040
what they're
1912
01:12:48,639 --> 01:12:52,520
doing now we don't seem to have anything
1913
01:12:51,040 --> 01:12:55,199
inside here that gives us the
1914
01:12:52,520 --> 01:12:57,639
opportunity to invalid so we have this
1915
01:12:55,199 --> 01:12:59,800
keyword called default and it works
1916
01:12:57,639 --> 01:13:01,480
exactly the same as the case except what
1917
01:12:59,800 --> 01:13:03,520
this will do is this is basically the
1918
01:13:01,480 --> 01:13:05,400
word that replaces else so we can
1919
01:13:03,520 --> 01:13:07,159
literally take this line of code and
1920
01:13:05,400 --> 01:13:09,760
place it again over here and add
1921
01:13:07,159 --> 01:13:11,760
ourselves a break this is exactly the
1922
01:13:09,760 --> 01:13:14,320
same as doing our L statement inside
1923
01:13:11,760 --> 01:13:16,280
here if none of these cases match this
1924
01:13:14,320 --> 01:13:18,400
variable then it'll go into the default
1925
01:13:16,280 --> 01:13:20,480
and Contin as normal so if we type in
1926
01:13:18,400 --> 01:13:21,880
zero we should see the message twice
1927
01:13:20,480 --> 01:13:24,159
because it's executing for the if
1928
01:13:21,880 --> 01:13:26,239
statement and executing down here and
1929
01:13:24,159 --> 01:13:28,199
let's do eight and you see the message
1930
01:13:26,239 --> 01:13:29,760
again so now that the case statement is
1931
01:13:28,199 --> 01:13:31,639
fully working let's just comment out the
1932
01:13:29,760 --> 01:13:34,840
if statement and make sure that it still
1933
01:13:31,639 --> 01:13:38,120
works one Monday let's just try another
1934
01:13:34,840 --> 01:13:39,679
one and we can do seven for Sunday so
1935
01:13:38,120 --> 01:13:41,320
just a quick recap case statements are
1936
01:13:39,679 --> 01:13:43,520
used by using the keyword switch
1937
01:13:41,320 --> 01:13:44,760
followed by the variable in question and
1938
01:13:43,520 --> 01:13:46,760
then if you want to check if this
1939
01:13:44,760 --> 01:13:48,440
variable number is equal to something
1940
01:13:46,760 --> 01:13:50,920
you put that something straight after
1941
01:13:48,440 --> 01:13:52,679
the word case followed by a colon and
1942
01:13:50,920 --> 01:13:54,800
then you can insert multiple lines of
1943
01:13:52,679 --> 01:13:56,719
Corde inside here and make sure you end
1944
01:13:54,800 --> 01:14:00,760
that line with a break if you want to
1945
01:13:56,719 --> 01:14:02,560
chain multiple like Dayal 2 or Dayal 3
1946
01:14:00,760 --> 01:14:05,800
then you can add the case above right
1947
01:14:02,560 --> 01:14:08,120
here so the case of zero or the case of
1948
01:14:05,800 --> 01:14:09,840
two it will do this little section so
1949
01:14:08,120 --> 01:14:12,560
it'll print out Tuesday if the user
1950
01:14:09,840 --> 01:14:13,880
enters zero or the user enters two of
1951
01:14:12,560 --> 01:14:16,360
course in this case we don't actually
1952
01:14:13,880 --> 01:14:18,239
want that after you filled out all of
1953
01:14:16,360 --> 01:14:19,800
the code make sure you add something
1954
01:14:18,239 --> 01:14:21,560
called the default the default is
1955
01:14:19,800 --> 01:14:23,880
equivalent to an lse keyword in our if
1956
01:14:21,560 --> 01:14:25,400
statement just to catch all the values
1957
01:14:23,880 --> 01:14:27,239
in case the user type something
1958
01:14:25,400 --> 01:14:30,080
incorrect and you want to let them know
1959
01:14:27,239 --> 01:14:32,120
what they should be typing
1960
01:14:30,080 --> 01:14:33,639
instead so now that we explored
1961
01:14:32,120 --> 01:14:35,400
different paths in our code using IF
1962
01:14:33,639 --> 01:14:36,880
statements and switch statements let's
1963
01:14:35,400 --> 01:14:39,320
have a look at how we can iterate
1964
01:14:36,880 --> 01:14:41,120
through our code an iteration in code is
1965
01:14:39,320 --> 01:14:43,719
just the ability to be able to Loop and
1966
01:14:41,120 --> 01:14:45,960
do a certain task so let's say we wanted
1967
01:14:43,719 --> 01:14:47,960
to print high to the console several
1968
01:14:45,960 --> 01:14:49,800
times then we just have to copy and
1969
01:14:47,960 --> 01:14:51,719
paste it down and this will be five
1970
01:14:49,800 --> 01:14:54,000
separate lines that do exactly the same
1971
01:14:51,719 --> 01:14:56,840
thing this will essentially achieve what
1972
01:14:54,000 --> 01:14:59,360
we want the problem is you just have
1973
01:14:56,840 --> 01:15:01,080
exactly the same code copy and pasted so
1974
01:14:59,360 --> 01:15:03,000
what we can do if we just block comment
1975
01:15:01,080 --> 01:15:05,239
this line for the time being we can
1976
01:15:03,000 --> 01:15:07,320
write something called a for Loop a for
1977
01:15:05,239 --> 01:15:08,880
Loop gives us the ability to Loop a
1978
01:15:07,320 --> 01:15:10,639
certain amount of times depending on the
1979
01:15:08,880 --> 01:15:13,320
parameters that we give it so this is
1980
01:15:10,639 --> 01:15:16,000
the syntax you can type for open the
1981
01:15:13,320 --> 01:15:17,679
bracket and we have to use a variable so
1982
01:15:16,000 --> 01:15:19,760
we can declare a new variable straight
1983
01:15:17,679 --> 01:15:22,639
into these brackets we can say inti
1984
01:15:19,760 --> 01:15:24,520
equals z and then have a semicolon and
1985
01:15:22,639 --> 01:15:26,639
there's three different sections to this
1986
01:15:24,520 --> 01:15:28,920
the first one is the variable decoration
1987
01:15:26,639 --> 01:15:30,920
and initialization and the second one is
1988
01:15:28,920 --> 01:15:33,199
the condition so in this case we want to
1989
01:15:30,920 --> 01:15:35,400
do this task five times so you want to
1990
01:15:33,199 --> 01:15:37,480
say I is less than five then we end that
1991
01:15:35,400 --> 01:15:39,800
semicolon there and now for the third
1992
01:15:37,480 --> 01:15:42,360
task every time this for Loop goes
1993
01:15:39,800 --> 01:15:44,360
around we want to increase I by one
1994
01:15:42,360 --> 01:15:45,719
because we want to go in steps of one so
1995
01:15:44,360 --> 01:15:48,320
we can do
1996
01:15:45,719 --> 01:15:52,360
i++ alternativ if you don't like using
1997
01:15:48,320 --> 01:15:55,360
Plus+ you can say I = I + 1 or to make
1998
01:15:52,360 --> 01:15:57,719
that quicker you could say I + equal 1 I
1999
01:15:55,360 --> 01:15:59,880
personally just prefer using Plus+ and
2000
01:15:57,719 --> 01:16:01,280
you'll see that everywhere else there is
2001
01:15:59,880 --> 01:16:03,159
certain cases where you would actually
2002
01:16:01,280 --> 01:16:05,360
want to change this to a different value
2003
01:16:03,159 --> 01:16:08,080
like I +al 2 and we'll show you that in
2004
01:16:05,360 --> 01:16:11,120
a second so this for Loop now says we
2005
01:16:08,080 --> 01:16:13,400
want to go from I equals 0 and once this
2006
01:16:11,120 --> 01:16:15,520
condition returns back false when I is
2007
01:16:13,400 --> 01:16:17,679
equal to 5 or greater then we want to
2008
01:16:15,520 --> 01:16:19,679
stop this for Loop every time this for
2009
01:16:17,679 --> 01:16:21,679
Loop goes around so in hits this curly
2010
01:16:19,679 --> 01:16:24,000
brace and comes back around we want to
2011
01:16:21,679 --> 01:16:26,800
do this action and in this case it's I
2012
01:16:24,000 --> 01:16:28,960
++ so if we just use console right line
2013
01:16:26,800 --> 01:16:31,760
and print out the value I we can see
2014
01:16:28,960 --> 01:16:34,440
what happens when the follow
2015
01:16:31,760 --> 01:16:36,840
runs you can see that the value of I is
2016
01:16:34,440 --> 01:16:39,120
increasing one each time and after the
2017
01:16:36,840 --> 01:16:41,360
fifth time is increased the value is
2018
01:16:39,120 --> 01:16:44,400
actually five so now this statement is
2019
01:16:41,360 --> 01:16:46,880
false because the value of I is equal to
2020
01:16:44,400 --> 01:16:49,560
five so therefore this is now false and
2021
01:16:46,880 --> 01:16:51,199
the for Loop is ended so now that we've
2022
01:16:49,560 --> 01:16:53,320
verified that you can repeat it five
2023
01:16:51,199 --> 01:16:55,840
times using this code we no longer want
2024
01:16:53,320 --> 01:16:57,280
to print I we want to print our high so
2025
01:16:55,840 --> 01:17:00,040
let's paste that inside the bracket and
2026
01:16:57,280 --> 01:17:02,159
run the code now you can see that these
2027
01:17:00,040 --> 01:17:03,920
two values will be exactly the same as
2028
01:17:02,159 --> 01:17:07,320
printing it out to the console just like
2029
01:17:03,920 --> 01:17:08,960
this but now we have a lot less code so
2030
01:17:07,320 --> 01:17:13,000
let's have a look at another example we
2031
01:17:08,960 --> 01:17:16,280
can say 4 in I equal 0 and we can say I
2032
01:17:13,000 --> 01:17:19,960
is less than or equal to 10 and then we
2033
01:17:16,280 --> 01:17:22,239
can do I + = 2 and then if we just print
2034
01:17:19,960 --> 01:17:25,199
I to the console so what this is now
2035
01:17:22,239 --> 01:17:28,199
saying is I want to go from zero all way
2036
01:17:25,199 --> 01:17:30,480
till 10 and I want to go up two times
2037
01:17:28,199 --> 01:17:32,560
each time so what this will be achieving
2038
01:17:30,480 --> 01:17:33,960
is printing out all of the even numbers
2039
01:17:32,560 --> 01:17:36,280
between 0 and
2040
01:17:33,960 --> 01:17:38,880
10 let's try and run
2041
01:17:36,280 --> 01:17:41,120
it now we can see we have all the even
2042
01:17:38,880 --> 01:17:42,840
numbers printed out between 0 and 10
2043
01:17:41,120 --> 01:17:45,800
because we're going up in
2044
01:17:42,840 --> 01:17:47,639
twos if we start this at one for example
2045
01:17:45,800 --> 01:17:49,360
and still keep every parameter exactly
2046
01:17:47,639 --> 01:17:52,280
the same then we'll be getting all of
2047
01:17:49,360 --> 01:17:54,239
the even numbers between 1 and 10 11 is
2048
01:17:52,280 --> 01:17:56,000
missing because our condition stops at
2049
01:17:54,239 --> 01:17:57,760
less than or equ equal to 10 what you
2050
01:17:56,000 --> 01:18:00,040
can also do with for Loops is you can
2051
01:17:57,760 --> 01:18:03,199
let the user determine what this value
2052
01:18:00,040 --> 01:18:05,280
is going to be let's test that so if we
2053
01:18:03,199 --> 01:18:06,760
block comment this section out what we
2054
01:18:05,280 --> 01:18:09,960
can do is we can appen this little
2055
01:18:06,760 --> 01:18:13,199
section so we can have a console. right
2056
01:18:09,960 --> 01:18:15,199
how many times do you want to say hi can
2057
01:18:13,199 --> 01:18:18,360
have a callon followed by speech marks
2058
01:18:15,199 --> 01:18:21,280
and then we can say in Loop counter
2059
01:18:18,360 --> 01:18:23,760
equals convert to in32 and we can have
2060
01:18:21,280 --> 01:18:25,840
console read line inside the
2061
01:18:23,760 --> 01:18:28,120
brackets how many times you want to say
2062
01:18:25,840 --> 01:18:30,120
hi the user types in the value you take
2063
01:18:28,120 --> 01:18:31,760
the console read line as a string
2064
01:18:30,120 --> 01:18:33,080
convert it to an integer and start
2065
01:18:31,760 --> 01:18:35,280
inside loop
2066
01:18:33,080 --> 01:18:38,159
counter so now what needs to change
2067
01:18:35,280 --> 01:18:40,199
inside here the five is what determines
2068
01:18:38,159 --> 01:18:41,679
how many times that we're going to Loop
2069
01:18:40,199 --> 01:18:43,920
so in this case we actually want to just
2070
01:18:41,679 --> 01:18:46,159
Loop by whatever the user types in the
2071
01:18:43,920 --> 01:18:50,760
counter let's just type five and make
2072
01:18:46,159 --> 01:18:54,159
sure it works and if we just type one it
2073
01:18:50,760 --> 01:18:56,560
works if we type zero however it will
2074
01:18:54,159 --> 01:18:58,320
just skip over the for Loop and the
2075
01:18:56,560 --> 01:19:00,760
reason for that is if you're going from
2076
01:18:58,320 --> 01:19:03,000
0o to I is less than zero then this is
2077
01:19:00,760 --> 01:19:04,120
immediately going to be false so then
2078
01:19:03,000 --> 01:19:06,040
the for Loop is just going to get
2079
01:19:04,120 --> 01:19:08,440
skipped immediately there is nothing to
2080
01:19:06,040 --> 01:19:10,760
do if the user is typed in zero which is
2081
01:19:08,440 --> 01:19:12,679
perfectly reasonable and you can type in
2082
01:19:10,760 --> 01:19:15,600
a really big number like a 100 and it
2083
01:19:12,679 --> 01:19:18,080
will output them all to the
2084
01:19:15,600 --> 01:19:19,800
screen if the user types in zero then
2085
01:19:18,080 --> 01:19:21,320
nothing's going to come up so you always
2086
01:19:19,800 --> 01:19:23,320
want to be able to let the user know if
2087
01:19:21,320 --> 01:19:26,719
there's something wrong so we can say if
2088
01:19:23,320 --> 01:19:28,239
the loop counter equals equals to zero
2089
01:19:26,719 --> 01:19:30,080
or maybe we can change this to less than
2090
01:19:28,239 --> 01:19:32,679
or equal to because it could type in
2091
01:19:30,080 --> 01:19:35,800
minus numbers as well we could just say
2092
01:19:32,679 --> 01:19:40,560
a conso right line using CW tab tab
2093
01:19:35,800 --> 01:19:42,320
sorry please enter a value above zero
2094
01:19:40,560 --> 01:19:44,520
and then if this condition has returned
2095
01:19:42,320 --> 01:19:46,360
back false then inside our else
2096
01:19:44,520 --> 01:19:48,719
statement we don't need to give it an
2097
01:19:46,360 --> 01:19:50,880
else if because if it's less than or
2098
01:19:48,719 --> 01:19:53,080
equal to zero then we want to process
2099
01:19:50,880 --> 01:19:56,239
the other values because it has to be
2100
01:19:53,080 --> 01:19:59,159
greater or equal to one
2101
01:19:56,239 --> 01:20:01,480
so when this is false this section has
2102
01:19:59,159 --> 01:20:03,480
to be greater than or equal to one so we
2103
01:20:01,480 --> 01:20:05,360
can just place our fall Loop inside here
2104
01:20:03,480 --> 01:20:08,000
and that's fine and let's try to run the
2105
01:20:05,360 --> 01:20:10,040
code now and if we type in zero please
2106
01:20:08,000 --> 01:20:12,080
enter a value of zero and if we just try
2107
01:20:10,040 --> 01:20:14,159
any negative values we'll get the same
2108
01:20:12,080 --> 01:20:15,719
message if you want to print additional
2109
01:20:14,159 --> 01:20:17,719
code in here you can literally do
2110
01:20:15,719 --> 01:20:19,679
anything you want you can even ask the
2111
01:20:17,719 --> 01:20:22,000
user what they want to print up here and
2112
01:20:19,679 --> 01:20:23,440
then you can replace this with a message
2113
01:20:22,000 --> 01:20:25,040
for example we could have a string
2114
01:20:23,440 --> 01:20:26,600
message
2115
01:20:25,040 --> 01:20:28,480
and we can just change this to hello
2116
01:20:26,600 --> 01:20:30,480
world and this could be something that
2117
01:20:28,480 --> 01:20:32,679
you reading from the console so we can
2118
01:20:30,480 --> 01:20:35,480
say message and we can put that in here
2119
01:20:32,679 --> 01:20:39,840
and if we print that there we
2120
01:20:35,480 --> 01:20:43,920
go so we can just say console.
2121
01:20:39,840 --> 01:20:45,639
right what do you want to repeat and
2122
01:20:43,920 --> 01:20:48,679
then we can read this
2123
01:20:45,639 --> 01:20:51,440
in and actually it might be easier if we
2124
01:20:48,679 --> 01:20:53,199
change this Loop counter to Second so if
2125
01:20:51,440 --> 01:20:55,199
you highlight the section hold alt and
2126
01:20:53,199 --> 01:20:59,120
press down arrow twice we can move it
2127
01:20:55,199 --> 01:21:03,960
down and say and how many times do you
2128
01:20:59,120 --> 01:21:05,560
want to repeat it there we go so now
2129
01:21:03,960 --> 01:21:07,920
what we can do is we can use this
2130
01:21:05,560 --> 01:21:09,440
message inside here and then Loop
2131
01:21:07,920 --> 01:21:11,400
depending on how many times the user
2132
01:21:09,440 --> 01:21:13,880
typed in whatever so we can type in
2133
01:21:11,400 --> 01:21:16,280
hello world get this exact value and we
2134
01:21:13,880 --> 01:21:18,480
can say 10 times there we go so now this
2135
01:21:16,280 --> 01:21:20,199
entire program is fully customized by
2136
01:21:18,480 --> 01:21:23,000
the users
2137
01:21:20,199 --> 01:21:25,080
input so let's just have a quick recap
2138
01:21:23,000 --> 01:21:26,880
so we started off this code I print in
2139
01:21:25,080 --> 01:21:29,679
the console right line five times to the
2140
01:21:26,880 --> 01:21:32,280
screen we wrote Our simple far Loop that
2141
01:21:29,679 --> 01:21:34,840
went from in I equals 0 to I is less
2142
01:21:32,280 --> 01:21:37,280
than five and we did I ++ because we
2143
01:21:34,840 --> 01:21:39,400
want to count up in ones you can place
2144
01:21:37,280 --> 01:21:41,480
the high inside here and it'll print it
2145
01:21:39,400 --> 01:21:43,840
five times for us so now we've made our
2146
01:21:41,480 --> 01:21:46,440
code from five lines down to just three
2147
01:21:43,840 --> 01:21:48,239
lines for the follow and then we allowed
2148
01:21:46,440 --> 01:21:50,400
the user to type in how many times that
2149
01:21:48,239 --> 01:21:52,960
they want to repeat it and we change the
2150
01:21:50,400 --> 01:21:55,280
condition from I is less than 5 to I is
2151
01:21:52,960 --> 01:21:57,840
less than whatever the user has typed in
2152
01:21:55,280 --> 01:21:59,400
installed within the loop counter and
2153
01:21:57,840 --> 01:22:01,600
then we hit a problem where if we
2154
01:21:59,400 --> 01:22:03,480
entered zero anything less than zero the
2155
01:22:01,600 --> 01:22:05,400
for Loop would just not run and it might
2156
01:22:03,480 --> 01:22:07,159
look like some things went wrong so we
2157
01:22:05,400 --> 01:22:09,040
added a little if statement in here to
2158
01:22:07,159 --> 01:22:11,199
make sure that the user has entered a
2159
01:22:09,040 --> 01:22:13,520
value above zero and if they haven't we
2160
01:22:11,199 --> 01:22:15,800
can let them know we don't need another
2161
01:22:13,520 --> 01:22:17,600
if statement in here because if this has
2162
01:22:15,800 --> 01:22:19,560
returned back false we can assume that
2163
01:22:17,600 --> 01:22:22,199
the value is something that we can work
2164
01:22:19,560 --> 01:22:24,239
with we later tailored the program so we
2165
01:22:22,199 --> 01:22:26,440
can change this message that was fixed
2166
01:22:24,239 --> 01:22:28,600
to I to something that the user can type
2167
01:22:26,440 --> 01:22:28,600
in
2168
01:22:28,880 --> 01:22:32,719
manually so now that we know how to use
2169
01:22:31,080 --> 01:22:35,040
for Loops let's have a look at while
2170
01:22:32,719 --> 01:22:37,239
Loops for loops and while Loops still
2171
01:22:35,040 --> 01:22:39,840
iterate through code but they're used in
2172
01:22:37,239 --> 01:22:41,679
slightly different contexts for Loops
2173
01:22:39,840 --> 01:22:43,679
are used mainly when you know how many
2174
01:22:41,679 --> 01:22:45,040
times you can iterate through as in this
2175
01:22:43,679 --> 01:22:47,960
example we're going to print to the
2176
01:22:45,040 --> 01:22:50,000
console exactly 10 times but in a while
2177
01:22:47,960 --> 01:22:52,600
loop you might print until a condition
2178
01:22:50,000 --> 01:22:54,760
is true for example if you asking the
2179
01:22:52,600 --> 01:22:56,920
user to type a number and they kept
2180
01:22:54,760 --> 01:22:58,440
typing in something else you don't know
2181
01:22:56,920 --> 01:23:01,040
how many times it's going to take the
2182
01:22:58,440 --> 01:23:03,520
user to type in correctly so in that
2183
01:23:01,040 --> 01:23:05,480
case you would use a y Loop if you
2184
01:23:03,520 --> 01:23:07,000
executing a certain amount of code and
2185
01:23:05,480 --> 01:23:09,199
you're only doing it a fixed amount of
2186
01:23:07,000 --> 01:23:11,800
times then it's always best to use a for
2187
01:23:09,199 --> 01:23:13,719
Loop so first let's try and convert this
2188
01:23:11,800 --> 01:23:15,600
simple for Loop that just prints I to
2189
01:23:13,719 --> 01:23:18,679
the console 10
2190
01:23:15,600 --> 01:23:21,000
times just like so from zero all the way
2191
01:23:18,679 --> 01:23:23,400
till 9 so let's comment out this for
2192
01:23:21,000 --> 01:23:26,000
Loop and have a look at this syntax for
2193
01:23:23,400 --> 01:23:28,040
the while loop so type the world while
2194
01:23:26,000 --> 01:23:30,320
and then in here we have our condition
2195
01:23:28,040 --> 01:23:32,239
and then we can open our curly braces
2196
01:23:30,320 --> 01:23:34,639
and while this condition returns back
2197
01:23:32,239 --> 01:23:36,719
true it will always go inside the curly
2198
01:23:34,639 --> 01:23:39,639
braces and when this condition returns
2199
01:23:36,719 --> 01:23:42,159
back false then we'll exit the while
2200
01:23:39,639 --> 01:23:44,480
loop so let's try and mimic exactly what
2201
01:23:42,159 --> 01:23:47,040
we're doing inside here there's three
2202
01:23:44,480 --> 01:23:48,480
sections to the for Loop we Define the
2203
01:23:47,040 --> 01:23:50,040
variable that we're going to use for the
2204
01:23:48,480 --> 01:23:52,920
for Loop counter and we give it an
2205
01:23:50,040 --> 01:23:54,600
initial value we give it the stopping
2206
01:23:52,920 --> 01:23:56,400
condition and we give it how many many
2207
01:23:54,600 --> 01:23:58,960
times the value needs to go up every
2208
01:23:56,400 --> 01:24:00,719
time the for Loop iterates in the while
2209
01:23:58,960 --> 01:24:02,880
loop situation we only have the
2210
01:24:00,719 --> 01:24:04,280
condition available so let's take our
2211
01:24:02,880 --> 01:24:06,560
condition and paste it straight into
2212
01:24:04,280 --> 01:24:09,080
here and now we need to store our
2213
01:24:06,560 --> 01:24:10,920
variable so just because it's a y Loop
2214
01:24:09,080 --> 01:24:12,880
and we can't store it inside here we
2215
01:24:10,920 --> 01:24:14,520
have to do it outside the while loop so
2216
01:24:12,880 --> 01:24:17,520
let's make it in I and we'll make it
2217
01:24:14,520 --> 01:24:19,520
equal to zero so we've got our variable
2218
01:24:17,520 --> 01:24:20,600
we've got our condition now we need to
2219
01:24:19,520 --> 01:24:24,440
set
2220
01:24:20,600 --> 01:24:26,199
i++ so inside the Y Loop we can say i++
2221
01:24:24,440 --> 01:24:28,840
and then now we've achieved this entire
2222
01:24:26,199 --> 01:24:30,520
line and successfully converted it let's
2223
01:24:28,840 --> 01:24:32,040
try and put the console right line in
2224
01:24:30,520 --> 01:24:34,639
and see what
2225
01:24:32,040 --> 01:24:37,440
happens and let's run the
2226
01:24:34,639 --> 01:24:39,639
code so it's going from 1 all the way
2227
01:24:37,440 --> 01:24:42,080
till 10 and the reason why it's going
2228
01:24:39,639 --> 01:24:44,719
from 1 till 10 not from 0 to 10 is
2229
01:24:42,080 --> 01:24:46,960
because the i++ is at the start let's
2230
01:24:44,719 --> 01:24:49,000
move it down online using alt and down
2231
01:24:46,960 --> 01:24:50,679
arrow key to move it down and let's run
2232
01:24:49,000 --> 01:24:53,639
it again and you can see it goes from
2233
01:24:50,679 --> 01:24:55,400
zero till 9 and if we just block comment
2234
01:24:53,639 --> 01:24:57,320
this out
2235
01:24:55,400 --> 01:24:59,560
and undo the for
2236
01:24:57,320 --> 01:25:01,360
Loop you can see that they're doing
2237
01:24:59,560 --> 01:25:03,920
exactly the same thing so let's bring
2238
01:25:01,360 --> 01:25:03,920
the Y loop
2239
01:25:04,000 --> 01:25:09,639
back and as you can see what we have is
2240
01:25:07,080 --> 01:25:12,880
we've defined our I variable made it
2241
01:25:09,639 --> 01:25:15,280
equals to zero while the I variable is
2242
01:25:12,880 --> 01:25:17,880
less than the value of 10 we print the I
2243
01:25:15,280 --> 01:25:20,280
to the console and increment I by one
2244
01:25:17,880 --> 01:25:22,480
and we keep looping this until this
2245
01:25:20,280 --> 01:25:24,320
statement returns back false and then we
2246
01:25:22,480 --> 01:25:26,080
exit the Y Loop and jump underneath the
2247
01:25:24,320 --> 01:25:28,040
the curly braces and hit the console
2248
01:25:26,080 --> 01:25:30,199
read line and this is the output that
2249
01:25:28,040 --> 01:25:31,760
we've got so now I've just copied the
2250
01:25:30,199 --> 01:25:33,960
code from the if statement video and
2251
01:25:31,760 --> 01:25:35,880
let's make this code a little bit better
2252
01:25:33,960 --> 01:25:37,360
so let's comment out our while loop and
2253
01:25:35,880 --> 01:25:39,440
our for Loop that was just used for
2254
01:25:37,360 --> 01:25:41,920
explanation purposes and this is the
2255
01:25:39,440 --> 01:25:43,960
code that you've saw earlier on in the C
2256
01:25:41,920 --> 01:25:46,320
we read in the first number Star as a
2257
01:25:43,960 --> 01:25:48,840
string use that string to convert to an
2258
01:25:46,320 --> 01:25:50,600
integer star number a and Then star
2259
01:25:48,840 --> 01:25:52,840
number B and these two blocks are
2260
01:25:50,600 --> 01:25:54,800
exactly the same except you have enter
2261
01:25:52,840 --> 01:25:57,400
first number and enter second number
2262
01:25:54,800 --> 01:25:59,159
and we store number a and store number B
2263
01:25:57,400 --> 01:26:02,119
then we store the value of them both
2264
01:25:59,159 --> 01:26:04,600
multiplied inside our int answer
2265
01:26:02,119 --> 01:26:07,080
variable then we ask the user what's the
2266
01:26:04,600 --> 01:26:09,159
value of both answers times together
2267
01:26:07,080 --> 01:26:11,239
give them a chance to input their value
2268
01:26:09,159 --> 01:26:13,719
convert it to an integer and then we
2269
01:26:11,239 --> 01:26:15,880
check the value and print well done of
2270
01:26:13,719 --> 01:26:19,239
Cl but it was wrong now if we run this
2271
01:26:15,880 --> 01:26:21,440
code again and we type in 10 and 10 and
2272
01:26:19,239 --> 01:26:23,600
we know the value is 100 but let's just
2273
01:26:21,440 --> 01:26:26,600
type in 90 now this is kind of a
2274
01:26:23,600 --> 01:26:28,400
problematic user case we've asked the
2275
01:26:26,600 --> 01:26:30,480
user to type in a value and they've got
2276
01:26:28,400 --> 01:26:32,440
it wrong and then when they press enter
2277
01:26:30,480 --> 01:26:35,280
the program just closes there's no
2278
01:26:32,440 --> 01:26:37,159
opportunity to Loop or try this again so
2279
01:26:35,280 --> 01:26:39,320
what we can do is add a y Loop in here
2280
01:26:37,159 --> 01:26:41,679
and we can say while the answer is
2281
01:26:39,320 --> 01:26:44,000
incorrect we can keep asking the user to
2282
01:26:41,679 --> 01:26:45,880
keep on guessing so instead of reading
2283
01:26:44,000 --> 01:26:50,080
the input here let's turn this little
2284
01:26:45,880 --> 01:26:52,880
section into a question what's the value
2285
01:26:50,080 --> 01:26:54,520
of number a * number B and instead of
2286
01:26:52,880 --> 01:26:57,159
allowing an input here let's just put a
2287
01:26:54,520 --> 01:26:58,800
question mark and then let's add a
2288
01:26:57,159 --> 01:27:01,679
console right line just to give a little
2289
01:26:58,800 --> 01:27:03,440
bit of a gap and then in order to do our
2290
01:27:01,679 --> 01:27:05,840
while loop we actually want to create a
2291
01:27:03,440 --> 01:27:07,639
boan variable just to track if the
2292
01:27:05,840 --> 01:27:10,159
answer has been correct or
2293
01:27:07,639 --> 01:27:12,159
not this is the section that we want to
2294
01:27:10,159 --> 01:27:14,920
loop around in the while loop we want to
2295
01:27:12,159 --> 01:27:16,600
read in the user's answer check it
2296
01:27:14,920 --> 01:27:18,719
output a message and then give them the
2297
01:27:16,600 --> 01:27:22,000
opportunity to read in another value so
2298
01:27:18,719 --> 01:27:24,639
they can guess again while the answer is
2299
01:27:22,000 --> 01:27:26,960
not equal to the actual answer
2300
01:27:24,639 --> 01:27:28,800
then we can loop around our code so we
2301
01:27:26,960 --> 01:27:30,880
have to modify a few things here so we
2302
01:27:28,800 --> 01:27:33,360
can't just take this code and place it
2303
01:27:30,880 --> 01:27:35,360
inside the problem with doing this is
2304
01:27:33,360 --> 01:27:37,760
the variables that we want to check
2305
01:27:35,360 --> 01:27:40,159
answer is outside of this y Loop but
2306
01:27:37,760 --> 01:27:42,000
actual answer is inside and the reason
2307
01:27:40,159 --> 01:27:44,159
why there's a red line here says it does
2308
01:27:42,000 --> 01:27:45,600
not exist in the current context because
2309
01:27:44,159 --> 01:27:47,760
we're trying to check a variable that
2310
01:27:45,600 --> 01:27:49,719
hasn't been defined yet so what we can
2311
01:27:47,760 --> 01:27:51,920
do is take the Declaration for the
2312
01:27:49,719 --> 01:27:54,199
actual answer and place it outside here
2313
01:27:51,920 --> 01:27:55,920
and make it equal to zero and instead of
2314
01:27:54,199 --> 01:27:58,320
redeclaring it inside the while loop we
2315
01:27:55,920 --> 01:28:00,440
can take this in and get rid of it so we
2316
01:27:58,320 --> 01:28:02,840
set the actual answers to zero set the
2317
01:28:00,440 --> 01:28:05,400
answer to the multiplication while the
2318
01:28:02,840 --> 01:28:07,760
answer is not equal to the actual answer
2319
01:28:05,400 --> 01:28:10,159
then we want to loop around so we don't
2320
01:28:07,760 --> 01:28:11,920
actually need another check in here this
2321
01:28:10,159 --> 01:28:14,080
while loop will keep on repeating if the
2322
01:28:11,920 --> 01:28:16,840
answer is incorrect so we don't need a
2323
01:28:14,080 --> 01:28:18,560
path for correct and incorrect if this
2324
01:28:16,840 --> 01:28:20,800
while loop still continues then the
2325
01:28:18,560 --> 01:28:23,600
answer will always be incorrect so what
2326
01:28:20,800 --> 01:28:25,400
we can do to double check is we can say
2327
01:28:23,600 --> 01:28:27,520
if the answer answer does not equal the
2328
01:28:25,400 --> 01:28:31,840
actual answer then we can place this
2329
01:28:27,520 --> 01:28:34,159
message inside here and get rid of this
2330
01:28:31,840 --> 01:28:37,440
branch and then after the while loop has
2331
01:28:34,159 --> 01:28:39,440
continued we can say Well done so let's
2332
01:28:37,440 --> 01:28:42,080
just read through the code while the
2333
01:28:39,440 --> 01:28:43,800
answer is not equal to the actual answer
2334
01:28:42,080 --> 01:28:45,800
and when this while loop first enters
2335
01:28:43,800 --> 01:28:48,600
it's going to be true because you're
2336
01:28:45,800 --> 01:28:50,719
going to have 10 * 10 which is 100 and
2337
01:28:48,600 --> 01:28:52,440
the value of answer is zero so this will
2338
01:28:50,719 --> 01:28:54,239
return back true because these two
2339
01:28:52,440 --> 01:28:55,800
values are not equal to each other then
2340
01:28:54,239 --> 01:28:57,719
we're going to ask the user to enter the
2341
01:28:55,800 --> 01:28:59,920
value then we're going to read into the
2342
01:28:57,719 --> 01:29:01,520
actual answer check if they're not equal
2343
01:28:59,920 --> 01:29:03,760
to each other and then we output a
2344
01:29:01,520 --> 01:29:05,840
message because we're asking a question
2345
01:29:03,760 --> 01:29:10,159
here we want to ask another one using a
2346
01:29:05,840 --> 01:29:12,760
console right Line enter your answer
2347
01:29:10,159 --> 01:29:14,480
callon here we go so let's have a look
2348
01:29:12,760 --> 01:29:19,080
and see how this
2349
01:29:14,480 --> 01:29:21,000
works 100 100 what's the value 100 * 100
2350
01:29:19,080 --> 01:29:23,440
and let's just get it wrong on purpose
2351
01:29:21,000 --> 01:29:24,600
90 CLA but is wrong and now we get to
2352
01:29:23,440 --> 01:29:26,440
answer again
2353
01:29:24,600 --> 01:29:28,880
and we can keep answering this as many
2354
01:29:26,440 --> 01:29:31,440
times as we want until we reach the
2355
01:29:28,880 --> 01:29:33,679
correct answer which in this case is
2356
01:29:31,440 --> 01:29:36,000
10,000 and we've got the answer well
2357
01:29:33,679 --> 01:29:37,600
done so to make the console look a
2358
01:29:36,000 --> 01:29:39,920
little bit better cuz this is all in one
2359
01:29:37,600 --> 01:29:42,119
block we can separate these with console
2360
01:29:39,920 --> 01:29:44,159
right lines maybe after this line we can
2361
01:29:42,119 --> 01:29:46,320
print out a new line and then after each
2362
01:29:44,159 --> 01:29:48,280
guess we can print out another new line
2363
01:29:46,320 --> 01:29:49,960
so after each guess we'll be down here
2364
01:29:48,280 --> 01:29:52,600
we can just print out a blank console
2365
01:29:49,960 --> 01:29:54,320
right line and just after you ask them
2366
01:29:52,600 --> 01:29:56,000
to enter the second number we can print
2367
01:29:54,320 --> 01:29:58,719
another one here and let's just see how
2368
01:29:56,000 --> 01:30:00,560
that looks 10 10 now it looks a lot
2369
01:29:58,719 --> 01:30:02,080
better because you ask the user to input
2370
01:30:00,560 --> 01:30:04,560
the numbers then you ask them the
2371
01:30:02,080 --> 01:30:06,800
question and then you get the answer and
2372
01:30:04,560 --> 01:30:09,040
then it keeps looping around like
2373
01:30:06,800 --> 01:30:10,920
this so there's a little gap between
2374
01:30:09,040 --> 01:30:12,400
each one so you can efficiently read it
2375
01:30:10,920 --> 01:30:14,840
instead of it all being closed off
2376
01:30:12,400 --> 01:30:17,080
together now if you answer it correctly
2377
01:30:14,840 --> 01:30:19,679
100 now it says well done and when we
2378
01:30:17,080 --> 01:30:21,719
press enter again the program now closes
2379
01:30:19,679 --> 01:30:24,560
so that was using a while loop and now
2380
01:30:21,719 --> 01:30:27,119
we have something else called a do while
2381
01:30:24,560 --> 01:30:29,080
and a do while is very close to a while
2382
01:30:27,119 --> 01:30:31,600
except that the while will only happen
2383
01:30:29,080 --> 01:30:34,320
if this condition returns back true and
2384
01:30:31,600 --> 01:30:36,639
a do while will always happen once and
2385
01:30:34,320 --> 01:30:38,239
it'll check the condition at the end so
2386
01:30:36,639 --> 01:30:40,360
let's have a look and see how this works
2387
01:30:38,239 --> 01:30:42,480
so we write the word do and then we have
2388
01:30:40,360 --> 01:30:44,440
the curly braces and then after it we
2389
01:30:42,480 --> 01:30:46,440
have our while condition and let's just
2390
01:30:44,440 --> 01:30:49,239
copy the condition from the old while
2391
01:30:46,440 --> 01:30:51,760
loop and we can say answer is not equal
2392
01:30:49,239 --> 01:30:53,520
to actual answer so as you can see with
2393
01:30:51,760 --> 01:30:55,360
a while loop it will always check the
2394
01:30:53,520 --> 01:30:58,600
condition first first and then it opens
2395
01:30:55,360 --> 01:31:00,560
the curly braces in the do there is no
2396
01:30:58,600 --> 01:31:02,880
condition it will always do everything
2397
01:31:00,560 --> 01:31:05,480
inside the curly braces once and then it
2398
01:31:02,880 --> 01:31:07,800
will check this while condition if this
2399
01:31:05,480 --> 01:31:09,800
condition is still true then it'll loop
2400
01:31:07,800 --> 01:31:11,840
around and check it every time but it
2401
01:31:09,800 --> 01:31:13,480
will only check it at the end while
2402
01:31:11,840 --> 01:31:15,159
Loops will check the condition as the
2403
01:31:13,480 --> 01:31:18,280
first thing it does and then check it
2404
01:31:15,159 --> 01:31:20,880
every iteration so let's move our cord
2405
01:31:18,280 --> 01:31:22,719
down into here and let's just comment
2406
01:31:20,880 --> 01:31:25,320
out our while loop just to see how this
2407
01:31:22,719 --> 01:31:30,320
works and if we run it again to make
2408
01:31:25,320 --> 01:31:32,440
sure it's working fine 10 10 90 90 50
2409
01:31:30,320 --> 01:31:34,600
and then finally 100 and it says well
2410
01:31:32,440 --> 01:31:36,320
done so you could be thinking what are
2411
01:31:34,600 --> 01:31:38,760
the differences between a while loop and
2412
01:31:36,320 --> 01:31:40,280
a do while in certain cases you want the
2413
01:31:38,760 --> 01:31:42,840
while loop to make sure that this
2414
01:31:40,280 --> 01:31:45,560
condition is true before even continuing
2415
01:31:42,840 --> 01:31:48,159
in this specific case we always want the
2416
01:31:45,560 --> 01:31:50,000
user to answer the question it doesn't
2417
01:31:48,159 --> 01:31:51,639
matter if it executes once because
2418
01:31:50,000 --> 01:31:54,280
actually that's the behavior that we
2419
01:31:51,639 --> 01:31:56,119
want we've asked the user a question
2420
01:31:54,280 --> 01:31:59,400
right here so we want to always be
2421
01:31:56,119 --> 01:32:01,600
looping at least one time the while loop
2422
01:31:59,400 --> 01:32:03,199
user case doesn't quite apply here
2423
01:32:01,600 --> 01:32:05,520
because we don't necessarily have to
2424
01:32:03,199 --> 01:32:08,280
check if they've answered correctly if
2425
01:32:05,520 --> 01:32:09,719
they haven't even inputed a value yet we
2426
01:32:08,280 --> 01:32:12,119
always want to be able to give them the
2427
01:32:09,719 --> 01:32:13,840
option to do it once and then we check
2428
01:32:12,119 --> 01:32:17,520
their answer at the end they could get
2429
01:32:13,840 --> 01:32:19,800
it on the first answer 10 10 100 and
2430
01:32:17,520 --> 01:32:22,239
it'll still be fine the same is true
2431
01:32:19,800 --> 01:32:24,000
with a while loop but it'll be easier
2432
01:32:22,239 --> 01:32:26,320
and it makes more sense to read your
2433
01:32:24,000 --> 01:32:28,840
code when you have a while loop you can
2434
01:32:26,320 --> 01:32:31,119
see that this bit of code depends on a
2435
01:32:28,840 --> 01:32:33,440
condition whereas in a do while the
2436
01:32:31,119 --> 01:32:35,920
first iteration does not depend on
2437
01:32:33,440 --> 01:32:38,600
anything but the second and third and so
2438
01:32:35,920 --> 01:32:40,840
forth depend on the condition so
2439
01:32:38,600 --> 01:32:42,719
depending on your user case and what
2440
01:32:40,840 --> 01:32:44,600
situation you want to use it you should
2441
01:32:42,719 --> 01:32:47,000
either pick a while loop which will only
2442
01:32:44,600 --> 01:32:49,520
occur through a condition or pick a do
2443
01:32:47,000 --> 01:32:51,400
while that will occur once or multiple
2444
01:32:49,520 --> 01:32:54,400
times after the first condition has
2445
01:32:51,400 --> 01:32:54,400
passed
2446
01:32:54,719 --> 01:32:58,320
so when you have if statements that are
2447
01:32:56,400 --> 01:33:00,080
pretty basic you can actually make them
2448
01:32:58,320 --> 01:33:02,080
quite easy to read through the
2449
01:33:00,080 --> 01:33:04,560
conditional operator also known as the
2450
01:33:02,080 --> 01:33:06,800
tary operator so let's see how this
2451
01:33:04,560 --> 01:33:09,440
works let's say we have an INT age and
2452
01:33:06,800 --> 01:33:11,239
we set it to a minus value and we know
2453
01:33:09,440 --> 01:33:13,600
that this value can never be someone's
2454
01:33:11,239 --> 01:33:15,400
age because it's negative but let's say
2455
01:33:13,600 --> 01:33:17,000
that we have a situation where you ask
2456
01:33:15,400 --> 01:33:19,360
the user for their age and they've
2457
01:33:17,000 --> 01:33:21,639
accidentally typed in -10 and we know
2458
01:33:19,360 --> 01:33:24,000
that that value is not actually true so
2459
01:33:21,639 --> 01:33:26,560
we could write something like this age
2460
01:33:24,000 --> 01:33:27,800
is greater than or equal to zero then we
2461
01:33:26,560 --> 01:33:31,119
can print out
2462
01:33:27,800 --> 01:33:33,040
valid and then in the else we can take
2463
01:33:31,119 --> 01:33:35,719
this console right line and we can print
2464
01:33:33,040 --> 01:33:38,480
out invalid so that means if we run our
2465
01:33:35,719 --> 01:33:40,800
code now you can see that it says
2466
01:33:38,480 --> 01:33:43,880
invalid and if we change it to positive
2467
01:33:40,800 --> 01:33:46,920
10 now it says valid so this seems a
2468
01:33:43,880 --> 01:33:49,119
little bit long we are just checking one
2469
01:33:46,920 --> 01:33:51,320
condition printing a value and printing
2470
01:33:49,119 --> 01:33:54,000
another value if that condition returns
2471
01:33:51,320 --> 01:33:56,719
back false so our conditional operator
2472
01:33:54,000 --> 01:33:59,040
works like this we have a condition
2473
01:33:56,719 --> 01:34:01,480
followed by question mark then the value
2474
01:33:59,040 --> 01:34:03,880
if it returns true then the value if it
2475
01:34:01,480 --> 01:34:05,880
returns false let's see how you can
2476
01:34:03,880 --> 01:34:09,040
rewrite this if statement into one
2477
01:34:05,880 --> 01:34:12,000
simple line so let's take this in
2478
01:34:09,040 --> 01:34:14,360
steps these two values are both strings
2479
01:34:12,000 --> 01:34:16,679
that we've typed in so let's make a
2480
01:34:14,360 --> 01:34:19,320
string and we can call it result and
2481
01:34:16,679 --> 01:34:21,800
let's make it equal to our Turner
2482
01:34:19,320 --> 01:34:23,760
operator so the first step is to get the
2483
01:34:21,800 --> 01:34:25,880
condition and the condition in this case
2484
01:34:23,760 --> 01:34:28,600
is if the age is greater than equal to
2485
01:34:25,880 --> 01:34:31,280
zero we can follow that by a question
2486
01:34:28,600 --> 01:34:33,920
mark and then if this returns back true
2487
01:34:31,280 --> 01:34:36,040
we want the string to say valid and we
2488
01:34:33,920 --> 01:34:38,119
can have a call on and if it returns
2489
01:34:36,040 --> 01:34:41,440
back false then we want the string to
2490
01:34:38,119 --> 01:34:42,880
say invalid and we can add a semicolon
2491
01:34:41,440 --> 01:34:45,080
and then straight after it we can just
2492
01:34:42,880 --> 01:34:47,480
have a console right line that just says
2493
01:34:45,080 --> 01:34:49,800
result as you can see now that we get it
2494
01:34:47,480 --> 01:34:52,040
twice we can verify one comes from the
2495
01:34:49,800 --> 01:34:53,960
if statement and one comes from the turn
2496
01:34:52,040 --> 01:34:55,719
operator and now if we make this say
2497
01:34:53,960 --> 01:34:58,600
positive 10 then you can see it both
2498
01:34:55,719 --> 01:35:00,040
says valid so if we comment out the if
2499
01:34:58,600 --> 01:35:03,159
statement and just see this running on
2500
01:35:00,040 --> 01:35:04,760
its own just to double check there we go
2501
01:35:03,159 --> 01:35:07,119
and because this is just returning back
2502
01:35:04,760 --> 01:35:09,520
a string we can actually just take all
2503
01:35:07,119 --> 01:35:11,159
of this code in the conditional operator
2504
01:35:09,520 --> 01:35:12,920
and paste it straight into the console
2505
01:35:11,159 --> 01:35:15,880
right line so you run the line of code
2506
01:35:12,920 --> 01:35:18,040
now we get exactly the same thing if
2507
01:35:15,880 --> 01:35:19,679
this hurts the readability of your code
2508
01:35:18,040 --> 01:35:21,960
then it's best to just store it and then
2509
01:35:19,679 --> 01:35:24,239
use it after if you can read this code
2510
01:35:21,960 --> 01:35:26,080
and it's better for you in the long run
2511
01:35:24,239 --> 01:35:28,000
later on you might want to dismiss this
2512
01:35:26,080 --> 01:35:30,119
variable and do it straight inside the
2513
01:35:28,000 --> 01:35:33,119
brackets just to save a variable and
2514
01:35:30,119 --> 01:35:34,840
then print itto the screen straight away
2515
01:35:33,119 --> 01:35:36,719
it'll be better for memory if you just
2516
01:35:34,840 --> 01:35:38,480
print it out straight away but if you're
2517
01:35:36,719 --> 01:35:39,880
not familiar with this notation yet and
2518
01:35:38,480 --> 01:35:42,800
you want to get used to it then that's
2519
01:35:39,880 --> 01:35:44,719
fine as well so a quick recap we had an
2520
01:35:42,800 --> 01:35:47,119
if statement with a condition and if
2521
01:35:44,719 --> 01:35:48,840
this returned back true we printed valid
2522
01:35:47,119 --> 01:35:51,000
and if it return back false we' print
2523
01:35:48,840 --> 01:35:53,159
invalid and this is pretty simple the
2524
01:35:51,000 --> 01:35:55,159
only difference between these two paths
2525
01:35:53,159 --> 01:35:57,119
is this string literal that we've typed
2526
01:35:55,159 --> 01:35:59,159
into here so we can pull the string
2527
01:35:57,119 --> 01:36:01,280
values out and place them in our
2528
01:35:59,159 --> 01:36:04,520
conditional operator we can say string
2529
01:36:01,280 --> 01:36:07,080
result is equal to if the age is greater
2530
01:36:04,520 --> 01:36:09,840
than or equal to zero then we store
2531
01:36:07,080 --> 01:36:11,639
valid inside result and if it's not
2532
01:36:09,840 --> 01:36:14,280
greater than equal to zero then we can
2533
01:36:11,639 --> 01:36:16,239
St invalid straight in our string after
2534
01:36:14,280 --> 01:36:18,119
we've stored this value we can print
2535
01:36:16,239 --> 01:36:22,119
results straight to the screen which
2536
01:36:18,119 --> 01:36:22,119
will either give us valid or
2537
01:36:22,360 --> 01:36:26,320
invalid when when you're dividing two
2538
01:36:24,400 --> 01:36:27,360
numbers you might get an ugly output
2539
01:36:26,320 --> 01:36:29,760
just like
2540
01:36:27,360 --> 01:36:32,320
this the user sometimes doesn't want to
2541
01:36:29,760 --> 01:36:34,880
see such long decimal places and maybe
2542
01:36:32,320 --> 01:36:37,199
you just want to prevent it to
2543
01:36:34,880 --> 01:36:39,000
81.3 in order to do this we need to use
2544
01:36:37,199 --> 01:36:40,840
a format pattern so the language
2545
01:36:39,000 --> 01:36:42,920
understands what we're trying to achieve
2546
01:36:40,840 --> 01:36:45,480
so if we make another console right line
2547
01:36:42,920 --> 01:36:47,040
what we can do is use string. format and
2548
01:36:45,480 --> 01:36:48,600
inside the brackets we can give it a
2549
01:36:47,040 --> 01:36:51,040
format and then the object that we want
2550
01:36:48,600 --> 01:36:53,760
to format so in this case the object in
2551
01:36:51,040 --> 01:36:56,320
the second parameter is our value so
2552
01:36:53,760 --> 01:36:58,239
let's open some speech marks go to the
2553
01:36:56,320 --> 01:37:01,040
next parameter using the comma and just
2554
01:36:58,239 --> 01:37:02,679
type in our value and then inside the
2555
01:37:01,040 --> 01:37:04,719
speech marks we want to type in the
2556
01:37:02,679 --> 01:37:07,199
pattern if you type in curly braces as
2557
01:37:04,719 --> 01:37:10,199
zero then zero represents the first
2558
01:37:07,199 --> 01:37:12,560
valid you in this list we can do a space
2559
01:37:10,199 --> 01:37:14,639
followed by curly braces one and then
2560
01:37:12,560 --> 01:37:17,159
add another comma and we can add a
2561
01:37:14,639 --> 01:37:19,639
thousand and what this means is value
2562
01:37:17,159 --> 01:37:23,719
will get place where zero is and 1,000
2563
01:37:19,639 --> 01:37:25,080
will get Place one is so let's run this
2564
01:37:23,719 --> 01:37:27,400
and you see the exact same thing's
2565
01:37:25,080 --> 01:37:29,840
happened plus dis place and now we have
2566
01:37:27,400 --> 01:37:32,320
the Thousand which follows this pattern
2567
01:37:29,840 --> 01:37:33,960
exactly if this is confusing don't worry
2568
01:37:32,320 --> 01:37:36,600
too much about it I have the next
2569
01:37:33,960 --> 01:37:38,639
section all about string functions so
2570
01:37:36,600 --> 01:37:40,480
let's get rid of the one followed by the
2571
01:37:38,639 --> 01:37:42,880
thousand and run it again and we should
2572
01:37:40,480 --> 01:37:45,119
see the output being exactly the same
2573
01:37:42,880 --> 01:37:47,000
there we go so now let's see how we can
2574
01:37:45,119 --> 01:37:49,840
make this better in terms of decimal
2575
01:37:47,000 --> 01:37:52,080
places so just after the zero and the
2576
01:37:49,840 --> 01:37:54,440
zero represents the first value we can
2577
01:37:52,080 --> 01:37:56,480
add a call on and this column will tell
2578
01:37:54,440 --> 01:37:59,639
us what format we want the value to be
2579
01:37:56,480 --> 01:38:01,960
displayed as we can type in
2580
01:37:59,639 --> 01:38:03,679
0. and what this means is all the
2581
01:38:01,960 --> 01:38:06,199
numbers before the decimal place will
2582
01:38:03,679 --> 01:38:08,360
get displayed and only two numbers after
2583
01:38:06,199 --> 01:38:09,960
the decimal place will get displayed so
2584
01:38:08,360 --> 01:38:12,080
let's try run this now and you can see
2585
01:38:09,960 --> 01:38:13,960
that the code output is a lot cleaner
2586
01:38:12,080 --> 01:38:17,840
one thing to note though by default this
2587
01:38:13,960 --> 01:38:20,320
will round it up 0.37 becomes
2588
01:38:17,840 --> 01:38:21,880
0.4 not to worry though cuz this is what
2589
01:38:20,320 --> 01:38:23,920
the user will see on the screen and it
2590
01:38:21,880 --> 01:38:26,440
won't affect the actual value of the
2591
01:38:23,920 --> 01:38:28,320
variable if we print out value straight
2592
01:38:26,440 --> 01:38:31,119
after you'll see that the difference
2593
01:38:28,320 --> 01:38:33,320
hasn't been changed it's still 0.3 in
2594
01:38:31,119 --> 01:38:35,360
the original variable so that's one
2595
01:38:33,320 --> 01:38:38,000
method of doing it what we can do now is
2596
01:38:35,360 --> 01:38:39,800
just use controll D and copy it down and
2597
01:38:38,000 --> 01:38:43,239
we can use different formats to show you
2598
01:38:39,800 --> 01:38:45,280
what it looks like if we remove the 0
2599
01:38:43,239 --> 01:38:47,920
and then one of the zeros and leave it
2600
01:38:45,280 --> 01:38:49,800
like this and it gains one decimal place
2601
01:38:47,920 --> 01:38:53,040
each time you'll see what the output
2602
01:38:49,800 --> 01:38:55,199
looks like we just have the 81 which
2603
01:38:53,040 --> 01:38:57,719
essentially makes it into an integer
2604
01:38:55,199 --> 01:39:00,480
because it's a whole number then we have
2605
01:38:57,719 --> 01:39:03,520
81.0 and the reason why it's 0 is cuz
2606
01:39:00,480 --> 01:39:06,679
0.3 will round down as it's under
2607
01:39:03,520 --> 01:39:10,000
0.5 the 03 is going to round down as
2608
01:39:06,679 --> 01:39:13,080
it's under 05 so it'll round down to
2609
01:39:10,000 --> 01:39:15,679
zero and then finally we have the
2610
01:39:13,080 --> 01:39:16,800
81.4 and just like before this will
2611
01:39:15,679 --> 01:39:19,400
round down
2612
01:39:16,800 --> 01:39:20,639
to4 and just like before this will round
2613
01:39:19,400 --> 01:39:24,400
down to
2614
01:39:20,639 --> 01:39:26,199
0.4 this will round down to 0 four let's
2615
01:39:24,400 --> 01:39:28,280
say you wanted to get rid of trailing
2616
01:39:26,199 --> 01:39:30,360
zeros because you don't want them then
2617
01:39:28,280 --> 01:39:32,679
you can actually change this zero to a
2618
01:39:30,360 --> 01:39:35,360
hashtag once you run the hashtag then it
2619
01:39:32,679 --> 01:39:37,119
will trim off any trailing zeros so what
2620
01:39:35,360 --> 01:39:39,440
this means is it will try and put it in
2621
01:39:37,119 --> 01:39:41,440
this format but if this format happens
2622
01:39:39,440 --> 01:39:44,239
to be a zero then it will just keep it
2623
01:39:41,440 --> 01:39:45,840
like this and if you just have 81 dot
2624
01:39:44,239 --> 01:39:47,920
then it'll also get rid of the dot for
2625
01:39:45,840 --> 01:39:50,199
you so then you just left with
2626
01:39:47,920 --> 01:39:51,760
81 it's really up to you as the
2627
01:39:50,199 --> 01:39:53,599
programmer what you want to display
2628
01:39:51,760 --> 01:39:56,000
depending on what context I'm just given
2629
01:39:53,599 --> 01:39:57,639
you the options that you have available
2630
01:39:56,000 --> 01:40:02,040
so let's say we want to work with money
2631
01:39:57,639 --> 01:40:04,280
now and we have a 10d / by a 3D and what
2632
01:40:02,040 --> 01:40:06,760
that value would be is somewhere between
2633
01:40:04,280 --> 01:40:09,560
3333 and this will be reoccurring cuz
2634
01:40:06,760 --> 01:40:11,159
it's exactly 1/3 the reason why I've got
2635
01:40:09,560 --> 01:40:13,480
D's in here is because these are
2636
01:40:11,159 --> 01:40:15,560
registering as doubles if you type the
2637
01:40:13,480 --> 01:40:18,599
value in normally it'll get registers at
2638
01:40:15,560 --> 01:40:21,000
in 32 and the problem with in32 is when
2639
01:40:18,599 --> 01:40:23,119
you do this division it will process the
2640
01:40:21,000 --> 01:40:25,679
division as an integer and then store it
2641
01:40:23,119 --> 01:40:28,000
as a a double the issue with that is in
2642
01:40:25,679 --> 01:40:30,800
this case and in this case both values
2643
01:40:28,000 --> 01:40:33,119
will get truncated let me demonstrate if
2644
01:40:30,800 --> 01:40:34,400
I remove the D from both sides and down
2645
01:40:33,119 --> 01:40:37,040
in here it will'll see what the
2646
01:40:34,400 --> 01:40:39,520
difference is and let's print out money
2647
01:40:37,040 --> 01:40:41,679
and let's see what's happened in the
2648
01:40:39,520 --> 01:40:43,360
first case this isn't actually an issue
2649
01:40:41,679 --> 01:40:45,320
because we have a decimal place here
2650
01:40:43,360 --> 01:40:47,560
which means this will be a double so an
2651
01:40:45,320 --> 01:40:49,719
integer divid by double will give us a
2652
01:40:47,560 --> 01:40:51,560
double but we just place the D here just
2653
01:40:49,719 --> 01:40:53,320
so we don't forget and it makes the
2654
01:40:51,560 --> 01:40:54,639
compiler's job a lot easier cuz it
2655
01:40:53,320 --> 01:40:57,400
doesn't have to convert it from an
2656
01:40:54,639 --> 01:41:00,440
integer to a double and in this case we
2657
01:40:57,400 --> 01:41:02,760
have an INT that's divided by an INT so
2658
01:41:00,440 --> 01:41:04,920
we'll get 3.3 reoccurring but because
2659
01:41:02,760 --> 01:41:07,040
it's processing as an integer then we
2660
01:41:04,920 --> 01:41:09,159
just get three being displayed and this
2661
01:41:07,040 --> 01:41:10,719
is not the intended result so let's just
2662
01:41:09,159 --> 01:41:14,599
replace these back to D's and we'll see
2663
01:41:10,719 --> 01:41:16,119
what we get now which is the 3.3 perfect
2664
01:41:14,599 --> 01:41:17,840
so let's say that we want to display
2665
01:41:16,119 --> 01:41:20,040
this as actual currency because you
2666
01:41:17,840 --> 01:41:22,320
never see it displayed like this it's
2667
01:41:20,040 --> 01:41:24,320
always to two decimal places just as
2668
01:41:22,320 --> 01:41:25,480
follows so we can follow the same
2669
01:41:24,320 --> 01:41:28,239
approach as
2670
01:41:25,480 --> 01:41:32,719
above we can do string.
2671
01:41:28,239 --> 01:41:34,119
format 0 callon 0.0 and the curly brace
2672
01:41:32,719 --> 01:41:36,239
and then common money cuz that's the
2673
01:41:34,119 --> 01:41:38,400
variable in reference now if we run that
2674
01:41:36,239 --> 01:41:41,040
let's see what happens
2675
01:41:38,400 --> 01:41:43,119
3.33 that's perfect and now what we can
2676
01:41:41,040 --> 01:41:45,280
do in here because this is the pattern
2677
01:41:43,119 --> 01:41:47,000
we can actually type in anything before
2678
01:41:45,280 --> 01:41:49,360
after this and it won't affect the
2679
01:41:47,000 --> 01:41:51,639
pattern just like so we've added some
2680
01:41:49,360 --> 01:41:53,360
Extra Spaces in here so if we get rid of
2681
01:41:51,639 --> 01:41:56,080
these spaces we can actually just play
2682
01:41:53,360 --> 01:41:58,320
is a pound sign in front of it and then
2683
01:41:56,080 --> 01:42:00,400
let's see what happens when we run it
2684
01:41:58,320 --> 01:42:05,320
perfect so now we actually have some
2685
01:42:00,400 --> 01:42:09,639
money and we can say in here £10
2686
01:42:05,320 --> 01:42:11,599
/3 equals this and now our output looks
2687
01:42:09,639 --> 01:42:13,840
a little bit better because we have some
2688
01:42:11,599 --> 01:42:15,599
formatting now the issue is when we
2689
01:42:13,840 --> 01:42:20,560
start getting into negatives let's see
2690
01:42:15,599 --> 01:42:23,520
what happens so we can say -10 IDE by 3
2691
01:42:20,560 --> 01:42:25,040
equals something and when we run we'll
2692
01:42:23,520 --> 01:42:28,360
see that the format is actually
2693
01:42:25,040 --> 01:42:30,960
incorrect now you never see the negative
2694
01:42:28,360 --> 01:42:33,920
symbol after the pound symbol it's
2695
01:42:30,960 --> 01:42:35,320
always this way around in this case what
2696
01:42:33,920 --> 01:42:37,960
we're doing is we're telling the
2697
01:42:35,320 --> 01:42:40,400
compiler hey we only want to see two
2698
01:42:37,960 --> 01:42:42,880
decimal places but always keep in this
2699
01:42:40,400 --> 01:42:46,119
format the pound sign followed by the
2700
01:42:42,880 --> 01:42:48,840
value and the value of money is -3 so
2701
01:42:46,119 --> 01:42:50,920
it'll Place -3 inside here and then add
2702
01:42:48,840 --> 01:42:53,199
the pound sign before it but we don't
2703
01:42:50,920 --> 01:42:55,320
exactly want that so we can add some
2704
01:42:53,199 --> 01:42:57,320
something called a culture info but we
2705
01:42:55,320 --> 01:42:59,360
can also give it some other formatting
2706
01:42:57,320 --> 01:43:00,440
some other way so let's explore some
2707
01:42:59,360 --> 01:43:03,560
other
2708
01:43:00,440 --> 01:43:05,719
options if you use money to string then
2709
01:43:03,560 --> 01:43:07,880
what we're actually doing here is using
2710
01:43:05,719 --> 01:43:10,560
the double variable and converting it to
2711
01:43:07,880 --> 01:43:13,280
a string I me get passed back a string
2712
01:43:10,560 --> 01:43:15,159
representative of this value and that's
2713
01:43:13,280 --> 01:43:17,840
fine and when we run the code you'll see
2714
01:43:15,159 --> 01:43:19,320
no difference between these two at this
2715
01:43:17,840 --> 01:43:22,040
point they're both being converted to
2716
01:43:19,320 --> 01:43:23,960
Strings but what we can do in this case
2717
01:43:22,040 --> 01:43:26,400
is when we open these brackets it tells
2718
01:43:23,960 --> 01:43:28,719
us there one of four what does this mean
2719
01:43:26,400 --> 01:43:30,599
exactly so in our two- string function
2720
01:43:28,719 --> 01:43:32,920
we have four ways that we can call this
2721
01:43:30,599 --> 01:43:35,159
function the first way you give nothing
2722
01:43:32,920 --> 01:43:35,159
in the
2723
01:43:35,679 --> 01:43:40,280
brackets and if we press the down arrow
2724
01:43:38,159 --> 01:43:42,920
the second way is to give it a format
2725
01:43:40,280 --> 01:43:45,040
provider the Third Way is just a string
2726
01:43:42,920 --> 01:43:47,599
format and the fourth way is both a
2727
01:43:45,040 --> 01:43:49,639
format and a provider so we'll use all
2728
01:43:47,599 --> 01:43:51,960
of these in a second but let's explore
2729
01:43:49,639 --> 01:43:53,920
the easiest one if you add a string in
2730
01:43:51,960 --> 01:43:55,880
there which gives us the third overload
2731
01:43:53,920 --> 01:43:58,719
string format and we can just type in a
2732
01:43:55,880 --> 01:44:01,400
Capital C let's just see what
2733
01:43:58,719 --> 01:44:03,880
happens perfect so now we have the best
2734
01:44:01,400 --> 01:44:06,480
format display to us and the reason why
2735
01:44:03,880 --> 01:44:08,480
this works is the C program will
2736
01:44:06,480 --> 01:44:10,119
recognize that the format of C is
2737
01:44:08,480 --> 01:44:12,280
telling us that we want to display
2738
01:44:10,119 --> 01:44:14,239
currency so then it's going to your
2739
01:44:12,280 --> 01:44:16,520
local machine and figuring out where you
2740
01:44:14,239 --> 01:44:18,760
are based on your date and time just
2741
01:44:16,520 --> 01:44:21,119
down here when you have adjust dat and
2742
01:44:18,760 --> 01:44:23,280
time it will figure it out based on this
2743
01:44:21,119 --> 01:44:26,119
we have UTC time and we have the date
2744
01:44:23,280 --> 01:44:27,800
and time listed up here based on that
2745
01:44:26,119 --> 01:44:29,760
information it can figure out that we
2746
01:44:27,800 --> 01:44:31,639
are indeed in the UK and we want to
2747
01:44:29,760 --> 01:44:34,760
display a pound sign and because the
2748
01:44:31,639 --> 01:44:37,239
program knows that it's - 3.33 it'll put
2749
01:44:34,760 --> 01:44:39,159
the pound sign in the right location so
2750
01:44:37,239 --> 01:44:40,159
now that we've explored C let's have a
2751
01:44:39,159 --> 01:44:42,719
look at the other ones that are
2752
01:44:40,159 --> 01:44:46,880
available so if we use contrl D and copy
2753
01:44:42,719 --> 01:44:48,880
this down three times we can use C 0 C1
2754
01:44:46,880 --> 01:44:51,760
and C2 and now if you run this again
2755
01:44:48,880 --> 01:44:53,719
you'll see what the output gets you c
2756
01:44:51,760 --> 01:44:55,880
will get you currency in it most native
2757
01:44:53,719 --> 01:44:58,920
form which is what we're used to using
2758
01:44:55,880 --> 01:45:01,199
in the UK c0 will only return you back
2759
01:44:58,920 --> 01:45:04,320
with the whole number C1 will give you
2760
01:45:01,199 --> 01:45:06,480
one decimal place and C2 mimic C on its
2761
01:45:04,320 --> 01:45:08,480
own as they're displaying the same
2762
01:45:06,480 --> 01:45:10,280
values now this is where it gets
2763
01:45:08,480 --> 01:45:11,560
interesting if you want to display
2764
01:45:10,280 --> 01:45:13,119
different currencies then you're going
2765
01:45:11,560 --> 01:45:15,040
to have to use this thing called a
2766
01:45:13,119 --> 01:45:17,199
culture info which will be able to
2767
01:45:15,040 --> 01:45:19,679
retrieve back different currency formats
2768
01:45:17,199 --> 01:45:20,760
based on a certain country code that you
2769
01:45:19,679 --> 01:45:22,679
give the
2770
01:45:20,760 --> 01:45:24,800
function so let's have a look and see
2771
01:45:22,679 --> 01:45:27,280
how that works if we scroll to the top
2772
01:45:24,800 --> 01:45:29,920
we can add a new library called system.
2773
01:45:27,280 --> 01:45:32,560
globalization inside globalization we
2774
01:45:29,920 --> 01:45:34,639
have a culture info so let's see how we
2775
01:45:32,560 --> 01:45:36,679
can tailor these to make it look a lot
2776
01:45:34,639 --> 01:45:40,159
better if you're worried about different
2777
01:45:36,679 --> 01:45:42,679
currencies so we us CW tab tab and we do
2778
01:45:40,159 --> 01:45:44,760
the same thing money. two string we add
2779
01:45:42,679 --> 01:45:47,560
our C and this is where our format
2780
01:45:44,760 --> 01:45:50,719
provider fits in so we can start type in
2781
01:45:47,560 --> 01:45:53,360
culture info it should come up like this
2782
01:45:50,719 --> 01:45:56,239
press enter and inside this is a class
2783
01:45:53,360 --> 01:45:58,480
that's why it went green if we press dot
2784
01:45:56,239 --> 01:46:00,520
then we get the current culture the
2785
01:45:58,480 --> 01:46:02,760
current culture will decide based on the
2786
01:46:00,520 --> 01:46:04,520
machine that the program is running on
2787
01:46:02,760 --> 01:46:06,400
if this program is running on a cloud
2788
01:46:04,520 --> 01:46:08,280
computer and the cloud computer is
2789
01:46:06,400 --> 01:46:10,800
stored in Europe then it might display
2790
01:46:08,280 --> 01:46:12,840
euros and if it's stored in America then
2791
01:46:10,800 --> 01:46:14,639
it'll display dollars so you want to
2792
01:46:12,840 --> 01:46:17,400
avoid using current culture if you're
2793
01:46:14,639 --> 01:46:19,040
going to scale your program worldwide in
2794
01:46:17,400 --> 01:46:21,040
the case of this example I'm going to
2795
01:46:19,040 --> 01:46:22,920
leave it because as you can see when I
2796
01:46:21,040 --> 01:46:25,040
run it it will display the same thing as
2797
01:46:22,920 --> 01:46:27,040
as the other ones because this one will
2798
01:46:25,040 --> 01:46:30,080
also recognize that my current culture
2799
01:46:27,040 --> 01:46:31,960
is UK and I'm also telling it to use the
2800
01:46:30,080 --> 01:46:33,960
current culture so in both of these
2801
01:46:31,960 --> 01:46:36,119
cases it's figured out that my current
2802
01:46:33,960 --> 01:46:38,239
culture is indeed the UK and it's
2803
01:46:36,119 --> 01:46:39,520
started using the pound sign but let's
2804
01:46:38,239 --> 01:46:41,719
say we want to give it a different
2805
01:46:39,520 --> 01:46:44,280
culture then what we can do if we
2806
01:46:41,719 --> 01:46:47,159
duplicate this line using contr D and
2807
01:46:44,280 --> 01:46:49,400
remove this section just after the dot
2808
01:46:47,159 --> 01:46:52,119
we can actually say create specific
2809
01:46:49,400 --> 01:46:54,119
culture and inside the parentheses we
2810
01:46:52,119 --> 01:46:56,320
can actually add a string and it says
2811
01:46:54,119 --> 01:46:58,920
here a predefined culture name or the
2812
01:46:56,320 --> 01:47:01,040
name of existing culture info object the
2813
01:46:58,920 --> 01:47:05,159
name is not case sensitive so in the
2814
01:47:01,040 --> 01:47:06,840
case of the UK you have English GB and
2815
01:47:05,159 --> 01:47:08,880
now we need another bracket at the end
2816
01:47:06,840 --> 01:47:12,960
and then if we use control D to copy
2817
01:47:08,880 --> 01:47:16,119
this down we can change GB to us let's
2818
01:47:12,960 --> 01:47:16,119
run this again and see what
2819
01:47:16,480 --> 01:47:20,320
happens these two don't actually output
2820
01:47:18,760 --> 01:47:22,159
anything different because we're just
2821
01:47:20,320 --> 01:47:24,080
doing the normal culture and then we're
2822
01:47:22,159 --> 01:47:26,400
telling it to find the GB culture which
2823
01:47:24,080 --> 01:47:28,880
is exactly the same thing for me so we
2824
01:47:26,400 --> 01:47:30,599
get these two values exactly the same
2825
01:47:28,880 --> 01:47:32,639
and apparently it's picking up that in
2826
01:47:30,599 --> 01:47:34,960
the US you have brackets around your
2827
01:47:32,639 --> 01:47:37,679
negative values which I can't personally
2828
01:47:34,960 --> 01:47:39,520
validate but this is how it's showing up
2829
01:47:37,679 --> 01:47:43,320
so let's try some other cultures and see
2830
01:47:39,520 --> 01:47:45,320
what happens so you can try Au for
2831
01:47:43,320 --> 01:47:47,599
Australian then you can see that they
2832
01:47:45,320 --> 01:47:50,040
actually use a negative followed by the
2833
01:47:47,599 --> 01:47:52,000
pound sign and then the value so even
2834
01:47:50,040 --> 01:47:54,119
though they're both in dollars one's US
2835
01:47:52,000 --> 01:47:56,199
Dollars and one is Australian dollars
2836
01:47:54,119 --> 01:47:58,639
and apparently the US uses brackets for
2837
01:47:56,199 --> 01:48:01,679
the negative values but Australians use
2838
01:47:58,639 --> 01:48:03,760
the negative value like we do so let's
2839
01:48:01,679 --> 01:48:05,520
just have a quick recap so we can
2840
01:48:03,760 --> 01:48:06,880
display the value just like this but
2841
01:48:05,520 --> 01:48:09,320
we'll have a problem with multiple
2842
01:48:06,880 --> 01:48:12,080
decimal places we can use these format
2843
01:48:09,320 --> 01:48:14,760
options to give us zero decimal places
2844
01:48:12,080 --> 01:48:17,040
one or two decimal places and then when
2845
01:48:14,760 --> 01:48:19,320
we use this format the problem with it
2846
01:48:17,040 --> 01:48:21,280
was when you display negative numbers
2847
01:48:19,320 --> 01:48:23,159
you get a nasty format where the
2848
01:48:21,280 --> 01:48:25,199
negative value actually comes after the
2849
01:48:23,159 --> 01:48:27,320
pound sign which is not correct and you
2850
01:48:25,199 --> 01:48:29,679
don't want to Output that to your users
2851
01:48:27,320 --> 01:48:31,119
instead you should use the C format to
2852
01:48:29,679 --> 01:48:33,639
tell the compiler that you want to
2853
01:48:31,119 --> 01:48:35,159
process this as a currency currency
2854
01:48:33,639 --> 01:48:37,159
processing is better because the
2855
01:48:35,159 --> 01:48:38,880
software will understand that the value
2856
01:48:37,159 --> 01:48:41,320
you're trying to represent depends on a
2857
01:48:38,880 --> 01:48:43,560
certain strict format just as follows
2858
01:48:41,320 --> 01:48:45,199
you don't want the negative to be here
2859
01:48:43,560 --> 01:48:47,840
instead of before it because that
2860
01:48:45,199 --> 01:48:50,440
doesn't look very nice to the end user
2861
01:48:47,840 --> 01:48:52,280
alternatively you could use this C info
2862
01:48:50,440 --> 01:48:54,080
if you're going to scale your program
2863
01:48:52,280 --> 01:48:57,080
scale your program means that it's going
2864
01:48:54,080 --> 01:48:59,080
to live on servers and this culture info
2865
01:48:57,080 --> 01:49:01,119
especially the current culture depends
2866
01:48:59,080 --> 01:49:03,880
on where the program is being run on
2867
01:49:01,119 --> 01:49:05,400
because it depends on the system time
2868
01:49:03,880 --> 01:49:07,280
different servers will have different
2869
01:49:05,400 --> 01:49:09,119
dat times set because they'll be in
2870
01:49:07,280 --> 01:49:10,880
different countries so you want to be
2871
01:49:09,119 --> 01:49:12,840
careful and if you want to display in a
2872
01:49:10,880 --> 01:49:15,360
certain currency you can create a
2873
01:49:12,840 --> 01:49:17,400
culture specification and pass the
2874
01:49:15,360 --> 01:49:19,000
country code inside the brackets and
2875
01:49:17,400 --> 01:49:20,960
then you'll get the correct output and
2876
01:49:19,000 --> 01:49:22,719
it doesn't matter where your program
2877
01:49:20,960 --> 01:49:24,719
lives if you always want to gener
2878
01:49:22,719 --> 01:49:27,400
generate in pound symbols then you
2879
01:49:24,719 --> 01:49:29,199
always use create specific culture and
2880
01:49:27,400 --> 01:49:31,960
always specify
2881
01:49:29,199 --> 01:49:33,560
NGB upon doing so you always get the
2882
01:49:31,960 --> 01:49:36,080
pound sign and it doesn't matter where
2883
01:49:33,560 --> 01:49:36,080
your program
2884
01:49:36,639 --> 01:49:41,480
lives so throughout this course you've
2885
01:49:38,960 --> 01:49:43,520
seen this notation a lot print out value
2886
01:49:41,480 --> 01:49:45,400
using console right to give the user an
2887
01:49:43,520 --> 01:49:47,639
opportunity to write on the same line
2888
01:49:45,400 --> 01:49:49,920
reading their value as a string and
2889
01:49:47,639 --> 01:49:52,719
store it in a variable and then use that
2890
01:49:49,920 --> 01:49:54,239
variable to convert it to an integer 32
2891
01:49:52,719 --> 01:49:56,440
and we're passing the string input
2892
01:49:54,239 --> 01:49:58,800
inside here and then we convert it to an
2893
01:49:56,440 --> 01:50:00,679
integer if we run the code you can see
2894
01:49:58,800 --> 01:50:02,440
that this actually works perfectly
2895
01:50:00,679 --> 01:50:05,080
assuming you actually enter a real
2896
01:50:02,440 --> 01:50:07,040
number like 10 we press enter and it
2897
01:50:05,080 --> 01:50:09,400
works and it outputs back to the screen
2898
01:50:07,040 --> 01:50:11,040
using this console right line and if you
2899
01:50:09,400 --> 01:50:12,920
press this restart button we can start
2900
01:50:11,040 --> 01:50:15,480
the program again and let's say we
2901
01:50:12,920 --> 01:50:17,360
accidentally type a character like L and
2902
01:50:15,480 --> 01:50:20,000
we press enter and we've get a format
2903
01:50:17,360 --> 01:50:22,199
exception now this is problematic
2904
01:50:20,000 --> 01:50:24,119
because our program will now close if we
2905
01:50:22,199 --> 01:50:26,440
press continue it doesn't even hit this
2906
01:50:24,119 --> 01:50:29,040
line or this as soon as it hits the
2907
01:50:26,440 --> 01:50:31,800
exception here our program just closes
2908
01:50:29,040 --> 01:50:33,159
if we use debug start with debugging and
2909
01:50:31,800 --> 01:50:35,400
now we're actually running the program
2910
01:50:33,159 --> 01:50:37,400
outside of visual studio so we won't get
2911
01:50:35,400 --> 01:50:39,520
that pop up when the error happens let's
2912
01:50:37,400 --> 01:50:42,040
see what happens if we type this in now
2913
01:50:39,520 --> 01:50:43,560
we can say 10p and press enter and now
2914
01:50:42,040 --> 01:50:45,679
we get that the program has stopped
2915
01:50:43,560 --> 01:50:47,239
working you've probably seen this pop up
2916
01:50:45,679 --> 01:50:49,159
a lot throughout Windows in different
2917
01:50:47,239 --> 01:50:50,920
versions of Windows because when a
2918
01:50:49,159 --> 01:50:53,040
program has a fatal error or an
2919
01:50:50,920 --> 01:50:54,800
exception you see there's pop up appear
2920
01:50:53,040 --> 01:50:57,199
here and the only thing you can do is
2921
01:50:54,800 --> 01:50:59,239
either debug or close it normally the
2922
01:50:57,199 --> 01:51:01,000
debug option isn't available but because
2923
01:50:59,239 --> 01:51:02,880
this is a c program and it's made
2924
01:51:01,000 --> 01:51:04,920
through Visual Studio and the visual
2925
01:51:02,880 --> 01:51:07,239
studio is being installed on the system
2926
01:51:04,920 --> 01:51:09,400
then it gives us the option but really
2927
01:51:07,239 --> 01:51:11,400
all we can do here is just terminate the
2928
01:51:09,400 --> 01:51:13,639
program it doesn't matter what happened
2929
01:51:11,400 --> 01:51:15,159
because we can't recover this if we
2930
01:51:13,639 --> 01:51:16,880
press close then it's literally just
2931
01:51:15,159 --> 01:51:18,760
going to close down it's going to tell
2932
01:51:16,880 --> 01:51:20,760
us the exception and then press any key
2933
01:51:18,760 --> 01:51:23,119
to continue and as soon as you press any
2934
01:51:20,760 --> 01:51:25,679
key then the program just closes so this
2935
01:51:23,119 --> 01:51:27,960
is very fatal for our code since if we
2936
01:51:25,679 --> 01:51:29,960
had some precious data stored in here or
2937
01:51:27,960 --> 01:51:32,480
anywhere else in the program it is now
2938
01:51:29,960 --> 01:51:34,639
lost before we can do anything this is
2939
01:51:32,480 --> 01:51:36,440
problematic and we want to avoid this
2940
01:51:34,639 --> 01:51:39,560
the best way to do this is to use
2941
01:51:36,440 --> 01:51:41,599
something called try pass right here the
2942
01:51:39,560 --> 01:51:43,920
program is assuming that your string
2943
01:51:41,599 --> 01:51:46,760
input is a perfect input and then you're
2944
01:51:43,920 --> 01:51:49,280
immediately converting it to an integer
2945
01:51:46,760 --> 01:51:51,280
now like I said what if this had 10 H in
2946
01:51:49,280 --> 01:51:52,960
it the program is going to cause an
2947
01:51:51,280 --> 01:51:54,960
exception and then the program is going
2948
01:51:52,960 --> 01:51:57,719
to close so let's see what we can do
2949
01:51:54,960 --> 01:51:59,480
with this trass function so the notation
2950
01:51:57,719 --> 01:52:02,880
is int.
2951
01:51:59,480 --> 01:52:05,119
trass the first parameter is a string s
2952
01:52:02,880 --> 01:52:07,400
string s is a string containing a number
2953
01:52:05,119 --> 01:52:09,320
to convert so what this trip pass
2954
01:52:07,400 --> 01:52:11,400
function will do as it says here
2955
01:52:09,320 --> 01:52:14,000
converts the string representation of a
2956
01:52:11,400 --> 01:52:16,440
number to its 32 bits signed integer
2957
01:52:14,000 --> 01:52:18,920
equivalent a return value indicates
2958
01:52:16,440 --> 01:52:20,199
whether the conversion succeeded okay
2959
01:52:18,920 --> 01:52:22,719
perfect so we'll get into that in a
2960
01:52:20,199 --> 01:52:25,000
second so our string s is a string
2961
01:52:22,719 --> 01:52:26,960
containing our number to convert and in
2962
01:52:25,000 --> 01:52:29,679
this case we want to be using the num
2963
01:52:26,960 --> 01:52:31,560
input so we copy and paste that down and
2964
01:52:29,679 --> 01:52:35,199
then we add a comma cuz we have more
2965
01:52:31,560 --> 01:52:38,679
than one parameter now we have an out in
2966
01:52:35,199 --> 01:52:41,040
result when this method returns result
2967
01:52:38,679 --> 01:52:43,400
which is our integer contains the 32bit
2968
01:52:41,040 --> 01:52:45,920
sign integer equivalent of the number
2969
01:52:43,400 --> 01:52:48,480
contained in S and S in this case is our
2970
01:52:45,920 --> 01:52:51,000
first parameter which is num input if
2971
01:52:48,480 --> 01:52:52,960
the conversion succeeded then we get the
2972
01:52:51,000 --> 01:52:55,760
value inside our result
2973
01:52:52,960 --> 01:52:57,960
or zero if the conversion failed the
2974
01:52:55,760 --> 01:53:00,520
conversion fails if the parameter s is
2975
01:52:57,960 --> 01:53:02,679
null or string empty is not of the
2976
01:53:00,520 --> 01:53:04,760
correct format or it represents a number
2977
01:53:02,679 --> 01:53:07,280
that's less or more than the maximum
2978
01:53:04,760 --> 01:53:09,639
value that the integer can store this
2979
01:53:07,280 --> 01:53:11,480
paramet is past uninitialize any value
2980
01:53:09,639 --> 01:53:13,880
originally supplies in result will be
2981
01:53:11,480 --> 01:53:17,040
overwritten let's dive in to see what
2982
01:53:13,880 --> 01:53:18,520
this means because it's an out in result
2983
01:53:17,040 --> 01:53:21,400
what this means is we can actually
2984
01:53:18,520 --> 01:53:24,079
process a variable inside here like intn
2985
01:53:21,400 --> 01:53:26,280
so we can say out num and what this will
2986
01:53:24,079 --> 01:53:28,880
do is when this line executes it will
2987
01:53:26,280 --> 01:53:31,320
try and convert num input and place the
2988
01:53:28,880 --> 01:53:33,520
value inside num so we don't need to run
2989
01:53:31,320 --> 01:53:36,719
this convert anymore we can just specify
2990
01:53:33,520 --> 01:53:39,800
that num is equal to zero so we've read
2991
01:53:36,719 --> 01:53:42,280
in the string that contains our 10 H and
2992
01:53:39,800 --> 01:53:44,599
we have our Nome that contains zero
2993
01:53:42,280 --> 01:53:47,480
let's just remove this console right
2994
01:53:44,599 --> 01:53:49,679
line so now we have our variable set up
2995
01:53:47,480 --> 01:53:51,599
let's see what happens if we do a
2996
01:53:49,679 --> 01:53:53,719
console right line and just print num
2997
01:53:51,599 --> 01:53:56,599
after this let's see what happens to the
2998
01:53:53,719 --> 01:53:59,199
code so if we type in 10 H and press
2999
01:53:56,599 --> 01:54:01,800
enter the value of num is zero but what
3000
01:53:59,199 --> 01:54:05,159
you notice now the program hasn't closed
3001
01:54:01,800 --> 01:54:06,960
because it's tried to pass it instead of
3002
01:54:05,159 --> 01:54:10,239
just passing it automatically and then
3003
01:54:06,960 --> 01:54:12,800
cause an exception so the reason why Try
3004
01:54:10,239 --> 01:54:15,239
pass is called try pass is first it
3005
01:54:12,800 --> 01:54:17,119
tries and then if it succeeds this will
3006
01:54:15,239 --> 01:54:19,800
contain the value and if it doesn't
3007
01:54:17,119 --> 01:54:21,199
succeed this will contain zero to know
3008
01:54:19,800 --> 01:54:23,560
if the number has actually been
3009
01:54:21,199 --> 01:54:26,239
converted we actually have to use the
3010
01:54:23,560 --> 01:54:28,320
bull return value so what this means is
3011
01:54:26,239 --> 01:54:30,320
because it returns us back a bull we can
3012
01:54:28,320 --> 01:54:33,239
store it inside a bull and we can give
3013
01:54:30,320 --> 01:54:36,800
it the name of success bu success equals
3014
01:54:33,239 --> 01:54:39,360
in. trass we try and pass the num input
3015
01:54:36,800 --> 01:54:41,639
and we place the value inside num the
3016
01:54:39,360 --> 01:54:43,880
reason why having this bu success is
3017
01:54:41,639 --> 01:54:46,440
very very essential let's say for
3018
01:54:43,880 --> 01:54:50,199
example the user actually enters
3019
01:54:46,440 --> 01:54:52,520
zero so technically as of this point num
3020
01:54:50,199 --> 01:54:54,719
zero is actually correct because we've '
3021
01:54:52,520 --> 01:54:57,040
got zero and we initialize it to zero
3022
01:54:54,719 --> 01:54:59,199
but of course after the trip pass runs
3023
01:54:57,040 --> 01:55:02,400
this is still going to be zero now we
3024
01:54:59,199 --> 01:55:05,280
don't know if the value zero is because
3025
01:55:02,400 --> 01:55:07,880
this has succeeded or the value is zero
3026
01:55:05,280 --> 01:55:11,159
because we initialized it to zero let me
3027
01:55:07,880 --> 01:55:13,520
demonstrate if I type in a zero here is
3028
01:55:11,159 --> 01:55:16,239
this zero being converted because this
3029
01:55:13,520 --> 01:55:18,199
was successful or is it presenting me
3030
01:55:16,239 --> 01:55:21,320
zero because this has been initialized
3031
01:55:18,199 --> 01:55:23,520
to zero and the only way we can know is
3032
01:55:21,320 --> 01:55:26,079
because our tripod pass will return us
3033
01:55:23,520 --> 01:55:29,280
back with true if s was converted
3034
01:55:26,079 --> 01:55:31,239
successfully otherwise false we can
3035
01:55:29,280 --> 01:55:34,400
actually write an if statement in here
3036
01:55:31,239 --> 01:55:36,639
if success open the curly braces and we
3037
01:55:34,400 --> 01:55:38,599
can start to type something in here so
3038
01:55:36,639 --> 01:55:41,360
we can take our console right line and
3039
01:55:38,599 --> 01:55:44,880
place it up here using alt and up Arrow
3040
01:55:41,360 --> 01:55:47,520
key and then in the lse we can say
3041
01:55:44,880 --> 01:55:51,440
failed to
3042
01:55:47,520 --> 01:55:53,159
convert so now we have a mini setup here
3043
01:55:51,440 --> 01:55:55,040
if it's been success uccessful we print
3044
01:55:53,159 --> 01:55:57,119
out the number if it hasn't been
3045
01:55:55,040 --> 01:55:59,800
successful then it'll go into this path
3046
01:55:57,119 --> 01:56:01,880
and it'll print fail to convert let's
3047
01:55:59,800 --> 01:56:04,760
have a look and see how this works now
3048
01:56:01,880 --> 01:56:06,639
if I press zero and I see Zero that
3049
01:56:04,760 --> 01:56:09,000
means it's followed down the success
3050
01:56:06,639 --> 01:56:11,480
path and this zero actually means it's
3051
01:56:09,000 --> 01:56:13,440
converted my input instead of just the
3052
01:56:11,480 --> 01:56:17,119
value that it's being initialized by if
3053
01:56:13,440 --> 01:56:19,360
you run the code again and type in 10 H
3054
01:56:17,119 --> 01:56:21,960
then we actually see it says fail to
3055
01:56:19,360 --> 01:56:24,679
convert and why that happened is we have
3056
01:56:21,960 --> 01:56:27,320
10 H appear and then this will return
3057
01:56:24,679 --> 01:56:29,960
back false when it tries to convert num
3058
01:56:27,320 --> 01:56:32,119
input and then after it hits this line
3059
01:56:29,960 --> 01:56:34,760
since success will be false it will
3060
01:56:32,119 --> 01:56:37,679
Dodge this path going into this path and
3061
01:56:34,760 --> 01:56:39,239
output fail to convert what you can do
3062
01:56:37,679 --> 01:56:41,520
if you really wanted to is you could
3063
01:56:39,239 --> 01:56:43,679
wrap this around in a while loop so
3064
01:56:41,520 --> 01:56:47,199
let's see what we can do so we can
3065
01:56:43,679 --> 01:56:49,320
specify a success and make that equal to
3066
01:56:47,199 --> 01:56:51,400
true and then we can add a while loop
3067
01:56:49,320 --> 01:56:53,719
with success in there and then open the
3068
01:56:51,400 --> 01:56:55,800
curly braces and put all of this code
3069
01:56:53,719 --> 01:56:58,400
inside here now we're going to have some
3070
01:56:55,800 --> 01:57:00,000
errors because we got success twice but
3071
01:56:58,400 --> 01:57:03,040
what we can do which will be a lot
3072
01:57:00,000 --> 01:57:06,560
easier is we can place this straight
3073
01:57:03,040 --> 01:57:08,560
inside our bracket and the reason why is
3074
01:57:06,560 --> 01:57:10,199
because this returns us back at booing
3075
01:57:08,560 --> 01:57:12,960
so we don't need to store it and then
3076
01:57:10,199 --> 01:57:15,639
use it another thing we can do is
3077
01:57:12,960 --> 01:57:17,679
instead of having Inn and then num right
3078
01:57:15,639 --> 01:57:20,079
here you can actually make this a
3079
01:57:17,679 --> 01:57:22,840
declaration as well so if we just remove
3080
01:57:20,079 --> 01:57:25,520
this value we can write out
3081
01:57:22,840 --> 01:57:28,760
in n and this will do exactly the same
3082
01:57:25,520 --> 01:57:31,199
thing it just uses less variables while
3083
01:57:28,760 --> 01:57:33,599
our success is true we come into here
3084
01:57:31,199 --> 01:57:35,960
and run this code if we failed to
3085
01:57:33,599 --> 01:57:38,639
convert then we still want it to be true
3086
01:57:35,960 --> 01:57:40,960
so we can loop around if we've converted
3087
01:57:38,639 --> 01:57:43,520
it and the value is successful then we
3088
01:57:40,960 --> 01:57:46,520
can actually make our success at this
3089
01:57:43,520 --> 01:57:48,239
point false cuz that will stop the W
3090
01:57:46,520 --> 01:57:53,599
loop from going around let's run the
3091
01:57:48,239 --> 01:57:55,679
code we can type in 10 H 10 a 10 a etc
3092
01:57:53,599 --> 01:57:57,639
etc and as soon as we type in a real
3093
01:57:55,679 --> 01:57:59,840
number then it will print it out and the
3094
01:57:57,639 --> 01:58:02,040
while loop will cancel and now right
3095
01:57:59,840 --> 01:58:03,840
here we're start on the console readline
3096
01:58:02,040 --> 01:58:06,480
now if we press enter the program will
3097
01:58:03,840 --> 01:58:10,040
close so let's have a
3098
01:58:06,480 --> 01:58:12,320
recap we use inter. trass so we can try
3099
01:58:10,040 --> 01:58:13,960
and pass a value before the program
3100
01:58:12,320 --> 01:58:16,480
assumes that the value is in the correct
3101
01:58:13,960 --> 01:58:18,440
format we give it a string variable and
3102
01:58:16,480 --> 01:58:20,560
it returns us back with a fresh new
3103
01:58:18,440 --> 01:58:22,560
integer called num or whatever you want
3104
01:58:20,560 --> 01:58:24,239
to call it inside here we we can place
3105
01:58:22,560 --> 01:58:26,239
this straight inside the condition of an
3106
01:58:24,239 --> 01:58:27,920
if statement because this function
3107
01:58:26,239 --> 01:58:30,679
returns us back with a true or false
3108
01:58:27,920 --> 01:58:32,760
value otherwise known as a bull if this
3109
01:58:30,679 --> 01:58:34,520
has been successful we stop the while
3110
01:58:32,760 --> 01:58:37,199
loop and print the value out to the
3111
01:58:34,520 --> 01:58:39,520
screen and if it hasn't been successful
3112
01:58:37,199 --> 01:58:41,960
then we print fail to convert and
3113
01:58:39,520 --> 01:58:43,679
because our success is still true it
3114
01:58:41,960 --> 01:58:46,760
will Lo back around to the start of the
3115
01:58:43,679 --> 01:58:49,199
while loop and ask the user to input
3116
01:58:46,760 --> 01:58:51,360
another value to try and convert this
3117
01:58:49,199 --> 01:58:53,639
while loop will only exit if it's indeed
3118
01:58:51,360 --> 01:58:56,480
been successful ful and the only way it
3119
01:58:53,639 --> 01:58:58,719
can exit is by having the success being
3120
01:58:56,480 --> 01:59:01,880
equal to false and the only way that can
3121
01:58:58,719 --> 01:59:04,400
happen is by having a successful
3122
01:59:01,880 --> 01:59:06,639
conversion the use of trass in every
3123
01:59:04,400 --> 01:59:08,599
program is very essential because if you
3124
01:59:06,639 --> 01:59:10,400
have a faat exception then your program
3125
01:59:08,599 --> 01:59:12,440
will close and you will lose all the
3126
01:59:10,400 --> 01:59:14,320
data the problem with that is you might
3127
01:59:12,440 --> 01:59:16,560
lose anything that you haven't processed
3128
01:59:14,320 --> 01:59:18,760
yet at that given point in your program
3129
01:59:16,560 --> 01:59:20,360
so you want to try and use triy pass
3130
01:59:18,760 --> 01:59:22,679
because you'll avoid any potential
3131
01:59:20,360 --> 01:59:24,440
exceptions and you can let the user know
3132
01:59:22,679 --> 01:59:26,520
if the conversion has been successful or
3133
01:59:24,440 --> 01:59:28,400
not and write yourself the same while
3134
01:59:26,520 --> 01:59:30,880
loop just like this so you can tell the
3135
01:59:28,400 --> 01:59:33,360
user hey you inputed it incorrectly but
3136
01:59:30,880 --> 01:59:35,239
this is how to do it right and make sure
3137
01:59:33,360 --> 01:59:37,320
that your program doesn't execute
3138
01:59:35,239 --> 01:59:39,560
incorrectly and make sure your program
3139
01:59:37,320 --> 01:59:42,280
doesn't just close unexpectedly and then
3140
01:59:39,560 --> 01:59:42,280
an error will be
3141
01:59:42,400 --> 01:59:47,239
caused in this exercise I want you to
3142
01:59:44,880 --> 01:59:48,880
print out some sort of times table and
3143
01:59:47,239 --> 01:59:51,560
what I mean by this is you want to ask
3144
01:59:48,880 --> 01:59:53,199
the user to input a number for the table
3145
01:59:51,560 --> 01:59:56,199
and then write WR a for Loop to print
3146
01:59:53,199 --> 01:59:58,360
out the x times table X is depend on the
3147
01:59:56,199 --> 02:00:00,520
value that they entered if they enter 10
3148
01:59:58,360 --> 02:00:04,000
in here then this will be the 10 times
3149
02:00:00,520 --> 02:00:06,000
table let's get started so we can have
3150
02:00:04,000 --> 02:00:08,520
our normal console.
3151
02:00:06,000 --> 02:00:11,520
write enter a
3152
02:00:08,520 --> 02:00:15,159
number and then use our int number
3153
02:00:11,520 --> 02:00:16,840
equals to convert to int32 and we can
3154
02:00:15,159 --> 02:00:19,000
say console. read line because we want
3155
02:00:16,840 --> 02:00:22,199
to read whatever the user types in then
3156
02:00:19,000 --> 02:00:24,599
we can have a four in I equals z i is is
3157
02:00:22,199 --> 02:00:28,079
less than 10 we're only going to print
3158
02:00:24,599 --> 02:00:30,320
up to the 10 * table so 1 * Something 2
3159
02:00:28,079 --> 02:00:32,079
* something all the way up to 10 times
3160
02:00:30,320 --> 02:00:34,400
the value that they
3161
02:00:32,079 --> 02:00:36,520
entered immediately we acknowledge an
3162
02:00:34,400 --> 02:00:40,599
error here when you do your 10 times
3163
02:00:36,520 --> 02:00:43,079
table for example you do 1 * 10 2 * 10
3164
02:00:40,599 --> 02:00:46,119
and so on and so forth the issue here is
3165
02:00:43,079 --> 02:00:47,560
we're going from 0 to 10 but because
3166
02:00:46,119 --> 02:00:50,679
it's less than 10 we're actually going
3167
02:00:47,560 --> 02:00:52,800
from 0 to 9 so let's change this to 1
3168
02:00:50,679 --> 02:00:55,400
and then less than or equal to 10 10 in
3169
02:00:52,800 --> 02:00:57,880
both cases we were actually looping the
3170
02:00:55,400 --> 02:01:00,040
same amount of times the issue is this
3171
02:00:57,880 --> 02:01:02,280
was starting on one and the other one
3172
02:01:00,040 --> 02:01:04,360
was starting on zero and the first one
3173
02:01:02,280 --> 02:01:06,960
was finishing on nine and this one's
3174
02:01:04,360 --> 02:01:10,199
finishing on 10 and we want them to go
3175
02:01:06,960 --> 02:01:11,679
from 1 till 10 instead of from 0 till 9
3176
02:01:10,199 --> 02:01:14,119
even though that's the same amount of
3177
02:01:11,679 --> 02:01:16,520
iterations it gives us different
3178
02:01:14,119 --> 02:01:18,760
values so in here we can just have a
3179
02:01:16,520 --> 02:01:20,480
console right line and this is how it's
3180
02:01:18,760 --> 02:01:22,719
going to work we're going to have our
3181
02:01:20,480 --> 02:01:24,880
first number times about our second
3182
02:01:22,719 --> 02:01:27,320
number and that's going to be equal to
3183
02:01:24,880 --> 02:01:29,880
something so our first number is our
3184
02:01:27,320 --> 02:01:33,320
value of I cuz that will say 1 *
3185
02:01:29,880 --> 02:01:35,400
Something 2 * something and so forth our
3186
02:01:33,320 --> 02:01:38,280
second number is whatever the user has
3187
02:01:35,400 --> 02:01:40,800
typed into the calculation which will be
3188
02:01:38,280 --> 02:01:43,320
number and our third number is going to
3189
02:01:40,800 --> 02:01:45,360
be the actual result of the calculation
3190
02:01:43,320 --> 02:01:48,760
which are these two times together so we
3191
02:01:45,360 --> 02:01:52,320
can have I star number and that should
3192
02:01:48,760 --> 02:01:55,000
be it let's run it and give it a shot
3193
02:01:52,320 --> 02:01:57,079
we can do our 10 times table and if we
3194
02:01:55,000 --> 02:01:59,400
press enter you can see that it's fully
3195
02:01:57,079 --> 02:02:01,920
working this will be our I variable
3196
02:01:59,400 --> 02:02:04,520
going down the side the second number is
3197
02:02:01,920 --> 02:02:06,880
what the user typed in and the third
3198
02:02:04,520 --> 02:02:09,360
number is this multiplied by this gives
3199
02:02:06,880 --> 02:02:12,280
you the output let's run it again with a
3200
02:02:09,360 --> 02:02:14,599
bigger number and it seems to handle
3201
02:02:12,280 --> 02:02:16,800
okay and because this is an integer
3202
02:02:14,599 --> 02:02:19,599
you'll expect to work okay up until
3203
02:02:16,800 --> 02:02:22,000
around 2.1 billion which is a big number
3204
02:02:19,599 --> 02:02:24,360
for some time tables anyway so just just
3205
02:02:22,000 --> 02:02:27,400
a quick recap we looped through from 1
3206
02:02:24,360 --> 02:02:29,280
till 10 instead of from 0 till 9 because
3207
02:02:27,400 --> 02:02:31,079
we made sure that when we do our
3208
02:02:29,280 --> 02:02:33,560
multiplications we don't want to start
3209
02:02:31,079 --> 02:02:35,880
the value at 0er * something we want to
3210
02:02:33,560 --> 02:02:37,520
start the first one at 1 * something and
3211
02:02:35,880 --> 02:02:40,360
we don't want to end at 9 we want to end
3212
02:02:37,520 --> 02:02:42,599
at 10 so using 1 and less than or equal
3213
02:02:40,360 --> 02:02:45,000
to 10 will get us the values that go
3214
02:02:42,599 --> 02:02:47,840
from 1 till 10 and that will make our
3215
02:02:45,000 --> 02:02:49,679
output look a lot more concise we're
3216
02:02:47,840 --> 02:02:51,880
using conditional format in here adding
3217
02:02:49,679 --> 02:02:54,040
the zero and curly braces and then the
3218
02:02:51,880 --> 02:02:55,760
one and then the two and in order to
3219
02:02:54,040 --> 02:02:58,400
insert the values in it's a comma
3220
02:02:55,760 --> 02:03:00,760
delimited list after the speech marks
3221
02:02:58,400 --> 02:03:04,719
the first one goes in the right order
3222
02:03:00,760 --> 02:03:07,360
zero is I one is number two is I times
3223
02:03:04,719 --> 02:03:11,040
the number running that code we'll get
3224
02:03:07,360 --> 02:03:11,040
this output there you
3225
02:03:11,079 --> 02:03:15,360
go so in this exercise I want you to
3226
02:03:13,760 --> 02:03:18,079
have a look and see if you can make the
3227
02:03:15,360 --> 02:03:19,679
Fizz bus game fiz buuz game is one of
3228
02:03:18,079 --> 02:03:21,920
the most popular interview questions
3229
02:03:19,679 --> 02:03:23,440
you'll come across
3230
02:03:21,920 --> 02:03:25,400
and the way to do this is to create a
3231
02:03:23,440 --> 02:03:27,840
for loop from one to something in this
3232
02:03:25,400 --> 02:03:29,560
case I'm just going to use 15 and inside
3233
02:03:27,840 --> 02:03:31,920
the for Loop you want to decide if the
3234
02:03:29,560 --> 02:03:33,760
numbers divisible by three and five then
3235
02:03:31,920 --> 02:03:36,400
you want to print Fizz buzz if it's
3236
02:03:33,760 --> 02:03:38,199
divisible by three then it's Fizz five
3237
02:03:36,400 --> 02:03:40,079
it's buzz and if it doesn't fall into
3238
02:03:38,199 --> 02:03:41,679
any of them categories then you want to
3239
02:03:40,079 --> 02:03:44,639
simply just print the number out to the
3240
02:03:41,679 --> 02:03:46,920
screen let's get started so we can have
3241
02:03:44,639 --> 02:03:50,040
a four and because we know how many we
3242
02:03:46,920 --> 02:03:53,320
want to do we want to do in I less than
3243
02:03:50,040 --> 02:03:55,280
15 i++
3244
02:03:53,320 --> 02:03:57,639
and then inside here we need to use the
3245
02:03:55,280 --> 02:03:59,480
modulus operator which is the percentage
3246
02:03:57,639 --> 02:04:01,280
sign so if you remember from the
3247
02:03:59,480 --> 02:04:03,719
tutorial in order to figure out if a
3248
02:04:01,280 --> 02:04:06,840
number is odd or even you could do the
3249
02:04:03,719 --> 02:04:09,199
number modulus 2 and if that value is
3250
02:04:06,840 --> 02:04:11,679
one then it must mean it's odd and if
3251
02:04:09,199 --> 02:04:14,159
that value is zero it must mean it's
3252
02:04:11,679 --> 02:04:17,559
even but what we can also differentiate
3253
02:04:14,159 --> 02:04:20,360
from here is if 10 modulus 2 equals 0
3254
02:04:17,559 --> 02:04:24,119
then that means that 10 is divisible by
3255
02:04:20,360 --> 02:04:26,480
2 but also it is even and if it's even
3256
02:04:24,119 --> 02:04:28,679
that means it's divisible by two if it's
3257
02:04:26,480 --> 02:04:32,199
not divisible by two that means it's not
3258
02:04:28,679 --> 02:04:34,880
even and therefore it's odd so in this
3259
02:04:32,199 --> 02:04:37,199
case if we try to divide three and five
3260
02:04:34,880 --> 02:04:40,840
we can have the same calculation but we
3261
02:04:37,199 --> 02:04:42,639
can work out differently so 10 modulus 3
3262
02:04:40,840 --> 02:04:44,800
actually gives us a value of one because
3263
02:04:42,639 --> 02:04:47,320
it's three remainder one so if this
3264
02:04:44,800 --> 02:04:48,679
value is anything other than zero then
3265
02:04:47,320 --> 02:04:51,679
we know that these two were not
3266
02:04:48,679 --> 02:04:54,480
divisible by each other 9 percentage 3
3267
02:04:51,679 --> 02:04:57,639
equal 0 which means it's divisible by it
3268
02:04:54,480 --> 02:04:58,880
so in our if statement we can say if I
3269
02:04:57,639 --> 02:05:00,520
which is the number that we're currently
3270
02:04:58,880 --> 02:05:04,599
up to
3271
02:05:00,520 --> 02:05:06,079
percentage 3 equals equals to zero and
3272
02:05:04,599 --> 02:05:09,760
because we want to check for five as
3273
02:05:06,079 --> 02:05:11,679
well we want to say and I percentage 5
3274
02:05:09,760 --> 02:05:14,520
equal equals to Zer and that means
3275
02:05:11,679 --> 02:05:15,960
they're both divisible by each other so
3276
02:05:14,520 --> 02:05:19,719
we can do a console right line and we
3277
02:05:15,960 --> 02:05:21,360
can say fiz buzz and then in our lse if
3278
02:05:19,719 --> 02:05:23,840
we're going to check if it's three and
3279
02:05:21,360 --> 02:05:25,119
we can repurpose this exact condition
3280
02:05:23,840 --> 02:05:28,119
because this just checks if it's
3281
02:05:25,119 --> 02:05:30,520
divisible by three and then we can print
3282
02:05:28,119 --> 02:05:32,719
Fizz and then if we copy this exact
3283
02:05:30,520 --> 02:05:34,960
structure down and change this to a five
3284
02:05:32,719 --> 02:05:37,960
to check if it's divisible by five then
3285
02:05:34,960 --> 02:05:39,760
we can print out buzz and in the worst
3286
02:05:37,960 --> 02:05:41,840
case scenario where none of them get
3287
02:05:39,760 --> 02:05:44,000
applied then we can simply print I to
3288
02:05:41,840 --> 02:05:46,040
the screen and actually there's no point
3289
02:05:44,000 --> 02:05:48,559
in starting at zero because normally
3290
02:05:46,040 --> 02:05:50,760
these start at one so we'll just do one
3291
02:05:48,559 --> 02:05:53,360
and we can make it 15 by making it less
3292
02:05:50,760 --> 02:05:55,960
than or equal to so instead of before it
3293
02:05:53,360 --> 02:05:58,520
was actually going from 0 to 14 now it's
3294
02:05:55,960 --> 02:06:00,599
going from 1 to 15 and they will tell
3295
02:05:58,520 --> 02:06:03,119
you this in the interview they might say
3296
02:06:00,599 --> 02:06:05,800
counting from 1 to 100 print out all of
3297
02:06:03,119 --> 02:06:07,280
the Fizz buzzers so let's run the code
3298
02:06:05,800 --> 02:06:11,520
and see what's going
3299
02:06:07,280 --> 02:06:13,960
on one two fizz fizz is actually three
3300
02:06:11,520 --> 02:06:16,280
so three is divisible by three then you
3301
02:06:13,960 --> 02:06:19,159
have four and then five is divisible by
3302
02:06:16,280 --> 02:06:22,000
five so you get buzz and six goes into
3303
02:06:19,159 --> 02:06:24,480
three so you get Fizz nine you get Fizz
3304
02:06:22,000 --> 02:06:28,719
because it's divisible by three then 10
3305
02:06:24,480 --> 02:06:30,800
/ 5 gives you buzz 11 12 goes into three
3306
02:06:28,719 --> 02:06:34,119
so you've got Fizz and finally when you
3307
02:06:30,800 --> 02:06:36,480
hit 15 it goes into three and goes into
3308
02:06:34,119 --> 02:06:38,280
five so we get Fizz buzz and the way to
3309
02:06:36,480 --> 02:06:41,040
increase this to 100 is by simply
3310
02:06:38,280 --> 02:06:42,719
changing this to 100 if you need to and
3311
02:06:41,040 --> 02:06:44,880
you can scroll through the entire input
3312
02:06:42,719 --> 02:06:46,599
and you can see all the history if you
3313
02:06:44,880 --> 02:06:48,559
wanted to make this slightly efficient
3314
02:06:46,599 --> 02:06:50,719
what you could actually do is store the
3315
02:06:48,559 --> 02:06:52,760
Boolean values for these calculations
3316
02:06:50,719 --> 02:06:56,679
before you can store the values for
3317
02:06:52,760 --> 02:07:01,960
these calculations before so we can say
3318
02:06:56,679 --> 02:07:04,119
bu 3 div equals false and bull 5 div
3319
02:07:01,960 --> 02:07:05,760
equals false and what we can do at the
3320
02:07:04,119 --> 02:07:09,719
start of each of the for Loop is we can
3321
02:07:05,760 --> 02:07:12,840
say three div equals this value and five
3322
02:07:09,719 --> 02:07:15,800
div also equals this
3323
02:07:12,840 --> 02:07:18,079
value and then every time we find a
3324
02:07:15,800 --> 02:07:20,280
calculation for three or calculation for
3325
02:07:18,079 --> 02:07:23,119
five we can replace it with either three
3326
02:07:20,280 --> 02:07:28,599
div or five div so let's change this one
3327
02:07:23,119 --> 02:07:31,239
here and this one here and our five here
3328
02:07:28,599 --> 02:07:33,199
and here and because these are values
3329
02:07:31,239 --> 02:07:35,320
now we want to actually store them as
3330
02:07:33,199 --> 02:07:37,360
true or falses so we don't need to check
3331
02:07:35,320 --> 02:07:40,400
if they're equal to zero so we can just
3332
02:07:37,360 --> 02:07:42,679
remove them so we can say if three div
3333
02:07:40,400 --> 02:07:45,639
and five div are both true and they'll
3334
02:07:42,679 --> 02:07:47,239
only be true if these two are both zero
3335
02:07:45,639 --> 02:07:50,040
now if we run the code and just make it
3336
02:07:47,239 --> 02:07:52,000
15 again just we have a shorter output
3337
02:07:50,040 --> 02:07:54,199
you can see that our code is exact the
3338
02:07:52,000 --> 02:07:55,679
same the reason why this is more
3339
02:07:54,199 --> 02:07:58,400
efficient is we're only doing this
3340
02:07:55,679 --> 02:08:00,880
calculation twice whereas before we were
3341
02:07:58,400 --> 02:08:04,119
doing it twice for three and twice for
3342
02:08:00,880 --> 02:08:06,159
five now we're doing it once storing it
3343
02:08:04,119 --> 02:08:07,760
and then making use of it this is a lot
3344
02:08:06,159 --> 02:08:09,719
more memory efficient than having to
3345
02:08:07,760 --> 02:08:11,639
calculate it every single time it
3346
02:08:09,719 --> 02:08:14,199
computes a lot faster and it's better
3347
02:08:11,639 --> 02:08:15,960
for readability if the rules of the game
3348
02:08:14,199 --> 02:08:18,400
happen to change and this was divisible
3349
02:08:15,960 --> 02:08:20,480
by four or six then you could just
3350
02:08:18,400 --> 02:08:22,000
change it very easily whereas in the
3351
02:08:20,480 --> 02:08:24,880
previous example you would have to
3352
02:08:22,000 --> 02:08:27,920
change in every single
3353
02:08:24,880 --> 02:08:30,280
case in C when you're constructing
3354
02:08:27,920 --> 02:08:32,719
strings that have backs slashes you're
3355
02:08:30,280 --> 02:08:34,559
going to need to use double backslash
3356
02:08:32,719 --> 02:08:36,880
and the reason for this is the first
3357
02:08:34,559 --> 02:08:38,960
backslash is an indication that it's
3358
02:08:36,880 --> 02:08:40,639
actually going to be an escape character
3359
02:08:38,960 --> 02:08:42,320
you're going to put an escape character
3360
02:08:40,639 --> 02:08:44,520
in and then put the character that you
3361
02:08:42,320 --> 02:08:46,599
want to display and the reason for that
3362
02:08:44,520 --> 02:08:49,119
is they've chose backslash as the
3363
02:08:46,599 --> 02:08:51,520
designated character so you can use
3364
02:08:49,119 --> 02:08:54,159
backslash and then a backs slash to to
3365
02:08:51,520 --> 02:08:56,880
show one backslash only and what this
3366
02:08:54,159 --> 02:08:59,119
means is it will use the first backslash
3367
02:08:56,880 --> 02:09:01,360
as a reference so it knows that it's
3368
02:08:59,119 --> 02:09:03,559
going to process the next character as
3369
02:09:01,360 --> 02:09:05,599
an escape character and what these
3370
02:09:03,559 --> 02:09:09,880
Escape characters are you can have
3371
02:09:05,599 --> 02:09:12,040
things like T for tab and for new line
3372
02:09:09,880 --> 02:09:14,320
and you can even use speech marks
3373
02:09:12,040 --> 02:09:16,119
because if you outputting some dialogue
3374
02:09:14,320 --> 02:09:18,880
and you wanted to say something like
3375
02:09:16,119 --> 02:09:21,800
this he
3376
02:09:18,880 --> 02:09:24,400
said and then you added your own quote
3377
02:09:21,800 --> 02:09:26,920
in there if you want to put something in
3378
02:09:24,400 --> 02:09:28,960
quotes then you can't because the speech
3379
02:09:26,920 --> 02:09:31,159
mark dictates that this is the end of
3380
02:09:28,960 --> 02:09:32,920
the string literal that's why it's cting
3381
02:09:31,159 --> 02:09:34,880
it off like this and then now it's not
3382
02:09:32,920 --> 02:09:37,000
recognizing that something is just WR
3383
02:09:34,880 --> 02:09:39,400
here and what it's actually doing is
3384
02:09:37,000 --> 02:09:42,280
you've got two strings specified one
3385
02:09:39,400 --> 02:09:44,360
that says he said space and one that's
3386
02:09:42,280 --> 02:09:46,440
just empty and then you just have this
3387
02:09:44,360 --> 02:09:48,920
something that's just in the middle of
3388
02:09:46,440 --> 02:09:50,880
course we actually want to insert the
3389
02:09:48,920 --> 02:09:52,760
speech marks within another pair of
3390
02:09:50,880 --> 02:09:54,960
speech marks marks this is where the
3391
02:09:52,760 --> 02:09:57,880
backslash comes in so you can insert a
3392
02:09:54,960 --> 02:09:59,960
backslash before here and this is now
3393
02:09:57,880 --> 02:10:02,400
acceptable and the same thing with the
3394
02:09:59,960 --> 02:10:04,599
normal backslash because the backslash
3395
02:10:02,400 --> 02:10:06,440
character is also going to be the Escape
3396
02:10:04,599 --> 02:10:08,920
character then you need a double
3397
02:10:06,440 --> 02:10:11,719
backslash so then that equals to a
3398
02:10:08,920 --> 02:10:14,679
single backslash when you print it out
3399
02:10:11,719 --> 02:10:16,920
let's Show an example so we have C colon
3400
02:10:14,679 --> 02:10:19,559
sl/ slash which will return us back a
3401
02:10:16,920 --> 02:10:21,559
single slash but visually right now it's
3402
02:10:19,559 --> 02:10:23,480
a double slash but when it gets
3403
02:10:21,559 --> 02:10:25,960
processed or outputed to the screen
3404
02:10:23,480 --> 02:10:27,440
you'll see that it's a single slash so
3405
02:10:25,960 --> 02:10:29,480
this common example is when you're
3406
02:10:27,440 --> 02:10:31,599
constructing a path together and you
3407
02:10:29,480 --> 02:10:34,000
want to use backslashes to denote the
3408
02:10:31,599 --> 02:10:36,079
different directories so let's output
3409
02:10:34,000 --> 02:10:38,520
this to the screen and see what
3410
02:10:36,079 --> 02:10:40,760
happens and as you can see we have a
3411
02:10:38,520 --> 02:10:43,400
single backslash because of the Escape
3412
02:10:40,760 --> 02:10:45,040
character we don't see both of them and
3413
02:10:43,400 --> 02:10:47,400
if we print the speech out to the screen
3414
02:10:45,040 --> 02:10:48,960
just to show you that as well then we
3415
02:10:47,400 --> 02:10:50,960
can see that there's only the speech
3416
02:10:48,960 --> 02:10:53,520
marks around the something and we don't
3417
02:10:50,960 --> 02:10:55,679
have the backslash you can tell when an
3418
02:10:53,520 --> 02:10:58,239
escape character is being used because
3419
02:10:55,679 --> 02:11:00,960
it goes a slight hint of yellow instead
3420
02:10:58,239 --> 02:11:02,800
of the normal color for a string so now
3421
02:11:00,960 --> 02:11:05,199
that we've worked out that this is how
3422
02:11:02,800 --> 02:11:08,199
this works let's see a way that we can
3423
02:11:05,199 --> 02:11:09,960
undo this to make it easier for us so
3424
02:11:08,199 --> 02:11:12,040
you've used the dollar sign for string
3425
02:11:09,960 --> 02:11:14,000
interpolation and you've used the plus
3426
02:11:12,040 --> 02:11:16,520
just to concatenate two variables
3427
02:11:14,000 --> 02:11:19,599
together and just as a quick example it
3428
02:11:16,520 --> 02:11:22,320
looked something like this your name is
3429
02:11:19,599 --> 02:11:25,119
and then the name variable and using the
3430
02:11:22,320 --> 02:11:27,719
plus you would say your name is followed
3431
02:11:25,119 --> 02:11:30,599
by a space and the quotation marks and
3432
02:11:27,719 --> 02:11:33,119
do plus name that's fine and now we have
3433
02:11:30,599 --> 02:11:35,320
another one so similar to how we place a
3434
02:11:33,119 --> 02:11:38,119
dollar sign before the speech marks we
3435
02:11:35,320 --> 02:11:40,400
have a verbum identifier and what this
3436
02:11:38,119 --> 02:11:42,239
means because it says verbatim it just
3437
02:11:40,400 --> 02:11:44,440
means whatever you see is whatever
3438
02:11:42,239 --> 02:11:47,000
happens so let's try and Define this
3439
02:11:44,440 --> 02:11:49,400
path a slightly different way but still
3440
02:11:47,000 --> 02:11:50,920
containing the exact same information
3441
02:11:49,400 --> 02:11:52,840
that you see when you print out to the
3442
02:11:50,920 --> 02:11:55,280
screen
3443
02:11:52,840 --> 02:11:57,480
so let's have a look we can reassign the
3444
02:11:55,280 --> 02:12:00,639
path value and just before the speech
3445
02:11:57,480 --> 02:12:03,119
marks we can put it at symbol and this
3446
02:12:00,639 --> 02:12:05,639
at symbol means it will actually negate
3447
02:12:03,119 --> 02:12:08,840
all of the Escape characters it will
3448
02:12:05,639 --> 02:12:11,440
process a single backlash as a single
3449
02:12:08,840 --> 02:12:14,199
backslash let's take this line of code
3450
02:12:11,440 --> 02:12:16,360
and place inside here now what you see
3451
02:12:14,199 --> 02:12:18,360
is that tint of yellow is no longer
3452
02:12:16,360 --> 02:12:20,159
there so if we print this past to the
3453
02:12:18,360 --> 02:12:22,280
screen let's see what
3454
02:12:20,159 --> 02:12:24,199
happens we actually get the double
3455
02:12:22,280 --> 02:12:26,760
backslash because it's ignoring all the
3456
02:12:24,199 --> 02:12:29,320
Escape characters which means if we take
3457
02:12:26,760 --> 02:12:31,480
them all out this actually looks exactly
3458
02:12:29,320 --> 02:12:33,599
how we want and you don't have to worry
3459
02:12:31,480 --> 02:12:36,920
about the Escape
3460
02:12:33,599 --> 02:12:36,920
characters there you
3461
02:12:37,559 --> 02:12:41,840
go these two are identical even though
3462
02:12:40,119 --> 02:12:44,719
one is being displayed using Escape
3463
02:12:41,840 --> 02:12:46,880
characters and one's using the verbatim
3464
02:12:44,719 --> 02:12:49,280
identifier please note that if you're
3465
02:12:46,880 --> 02:12:50,719
going to use the back sln for example
3466
02:12:49,280 --> 02:12:53,360
and I'll just demonstrate at the end of
3467
02:12:50,719 --> 02:12:55,119
this new line test and if we run that
3468
02:12:53,360 --> 02:12:58,599
code again you'll see that it's printed
3469
02:12:55,119 --> 02:13:00,520
the path on one line the back sln has
3470
02:12:58,599 --> 02:13:02,400
made it go on to the next line and now
3471
02:13:00,520 --> 02:13:06,320
it's printing new line test on a
3472
02:13:02,400 --> 02:13:09,559
dedicated line that's fine the problem
3473
02:13:06,320 --> 02:13:12,079
is if you use a back sln or a back SLT
3474
02:13:09,559 --> 02:13:14,199
or any of these Escape characters during
3475
02:13:12,079 --> 02:13:16,760
the at symbol then it's going to process
3476
02:13:14,199 --> 02:13:20,400
it as literal characters which means you
3477
02:13:16,760 --> 02:13:23,199
can't mix a back sln with an at symbol
3478
02:13:20,400 --> 02:13:24,840
AS this is just taking the text verbatim
3479
02:13:23,199 --> 02:13:27,400
so if we try and run the code now you'll
3480
02:13:24,840 --> 02:13:30,320
see the back sln will actually appear in
3481
02:13:27,400 --> 02:13:32,639
the code if you wanted to do this then
3482
02:13:30,320 --> 02:13:34,599
maybe you could remove the back sln and
3483
02:13:32,639 --> 02:13:36,880
add a plus at the end and then make
3484
02:13:34,599 --> 02:13:39,440
another string that just has a back
3485
02:13:36,880 --> 02:13:41,920
slash n and then this string will not be
3486
02:13:39,440 --> 02:13:44,320
processed as a Verbatim string because
3487
02:13:41,920 --> 02:13:46,960
we don't have the at symbol right before
3488
02:13:44,320 --> 02:13:49,480
it so then we can have the exact same
3489
02:13:46,960 --> 02:13:52,800
thing here just to show you
3490
02:13:49,480 --> 02:13:56,480
this so now the these two are identical
3491
02:13:52,800 --> 02:13:59,119
again just as shown here and as shown
3492
02:13:56,480 --> 02:14:01,840
here the difference being that we can't
3493
02:13:59,119 --> 02:14:03,360
specify a back sln in the same string we
3494
02:14:01,840 --> 02:14:05,800
actually have to concatenate them
3495
02:14:03,360 --> 02:14:07,880
together because of the verbatim
3496
02:14:05,800 --> 02:14:09,880
identifier so let's have a look at
3497
02:14:07,880 --> 02:14:12,320
speech
3498
02:14:09,880 --> 02:14:15,960
marks so if we have a string name and we
3499
02:14:12,320 --> 02:14:18,239
can make this equal to hello and then
3500
02:14:15,960 --> 02:14:20,520
someone and we said before that we have
3501
02:14:18,239 --> 02:14:22,800
to use the backslash so we can activate
3502
02:14:20,520 --> 02:14:24,239
the Escape characters and that will work
3503
02:14:22,800 --> 02:14:26,760
and if we just print that to the screen
3504
02:14:24,239 --> 02:14:29,360
to make sure then we get hello someone
3505
02:14:26,760 --> 02:14:31,520
in the speech marks if we add the verbum
3506
02:14:29,360 --> 02:14:35,000
identify at the start then we see we've
3507
02:14:31,520 --> 02:14:37,159
got an error it's trying to process it
3508
02:14:35,000 --> 02:14:39,280
differently this is a little Quirk with
3509
02:14:37,159 --> 02:14:41,520
the verbatim identifier if you want to
3510
02:14:39,280 --> 02:14:44,400
insert a speech marks you actually want
3511
02:14:41,520 --> 02:14:46,440
a double speech mark on either side the
3512
02:14:44,400 --> 02:14:48,800
double speech marks will get processed
3513
02:14:46,440 --> 02:14:51,920
as a single speech mark because it's
3514
02:14:48,800 --> 02:14:53,719
within a speech mark the verb identifier
3515
02:14:51,920 --> 02:14:56,199
needs to know that you're specifying a
3516
02:14:53,719 --> 02:14:58,719
speech mark and this acts as the same
3517
02:14:56,199 --> 02:15:00,559
reason why you have a backslash here
3518
02:14:58,719 --> 02:15:02,800
it's slightly quirky doing it this way
3519
02:15:00,559 --> 02:15:05,079
but it still works as per you
3520
02:15:02,800 --> 02:15:08,119
expect and when you're specifying
3521
02:15:05,079 --> 02:15:09,880
something like name and we can say hello
3522
02:15:08,119 --> 02:15:12,079
someone so let's print name to the
3523
02:15:09,880 --> 02:15:13,880
screen and make sure it works in this
3524
02:15:12,079 --> 02:15:16,559
case we don't actually need to specify
3525
02:15:13,880 --> 02:15:18,719
any sort of Escape character because the
3526
02:15:16,559 --> 02:15:20,480
quotation mark is a separate character
3527
02:15:18,719 --> 02:15:22,119
to the speech mark and it doesn't
3528
02:15:20,480 --> 02:15:24,480
interrupt anything by placing a
3529
02:15:22,119 --> 02:15:26,679
quotation mark inside the string just
3530
02:15:24,480 --> 02:15:28,960
represents that as its own character
3531
02:15:26,679 --> 02:15:32,119
let's run that and make sure it works
3532
02:15:28,960 --> 02:15:34,040
there you go so just as a quick recap
3533
02:15:32,119 --> 02:15:36,400
Escape characters are very essential
3534
02:15:34,040 --> 02:15:38,320
because we can use things like back SLT
3535
02:15:36,400 --> 02:15:40,760
to give us a tab character in our
3536
02:15:38,320 --> 02:15:42,840
console output this can be very helpful
3537
02:15:40,760 --> 02:15:45,960
cuz you can almost mimic the use of
3538
02:15:42,840 --> 02:15:48,119
columns inside our console output you'll
3539
02:15:45,960 --> 02:15:49,800
notice that back sln is used quite a lot
3540
02:15:48,119 --> 02:15:51,920
in the console output so you can
3541
02:15:49,800 --> 02:15:53,920
separate lines without having to use
3542
02:15:51,920 --> 02:15:56,760
multiple console right
3543
02:15:53,920 --> 02:15:58,800
lines if you have a single backslash you
3544
02:15:56,760 --> 02:16:01,079
can't have it on its own if the string
3545
02:15:58,800 --> 02:16:03,000
is not a Verbatim string you have to
3546
02:16:01,079 --> 02:16:05,480
pair it with another character otherwise
3547
02:16:03,000 --> 02:16:07,480
you will get a syntax error if you want
3548
02:16:05,480 --> 02:16:09,320
to use the verbatim string then you can
3549
02:16:07,480 --> 02:16:11,719
put an add symbol before the string
3550
02:16:09,320 --> 02:16:14,199
definition and then it will ignore all
3551
02:16:11,719 --> 02:16:16,480
of the Escape characters for example
3552
02:16:14,199 --> 02:16:18,520
using backs sln Within These speech
3553
02:16:16,480 --> 02:16:20,440
marks will mean that the back sln will
3554
02:16:18,520 --> 02:16:21,719
literally be printed to the screen and
3555
02:16:20,440 --> 02:16:24,440
you won't get get the new line
3556
02:16:21,719 --> 02:16:26,040
functionality that backlash M provides
3557
02:16:24,440 --> 02:16:28,679
when you want to print speech marks to
3558
02:16:26,040 --> 02:16:30,840
the screen using the verbatim identifier
3559
02:16:28,679 --> 02:16:32,719
you have to use a double speech marks in
3560
02:16:30,840 --> 02:16:34,800
order for it to show up and as you can
3561
02:16:32,719 --> 02:16:36,960
see it slightly goes yellow and that's
3562
02:16:34,800 --> 02:16:38,319
the similar notation as using Escape
3563
02:16:36,960 --> 02:16:40,960
character when you don't have an a
3564
02:16:38,319 --> 02:16:42,679
Verbatim string if you're using other
3565
02:16:40,960 --> 02:16:45,519
characters that are similar to a speech
3566
02:16:42,679 --> 02:16:47,439
mark like a quotation mark because this
3567
02:16:45,519 --> 02:16:49,639
doesn't interrupt the syntax of the
3568
02:16:47,439 --> 02:16:52,240
language then this is fine because a
3569
02:16:49,639 --> 02:16:54,160
character can just be in inside a string
3570
02:16:52,240 --> 02:16:55,679
and to the compiler this is just a
3571
02:16:54,160 --> 02:16:58,120
character even though they are very
3572
02:16:55,679 --> 02:17:01,000
similar the quotation mark doesn't
3573
02:16:58,120 --> 02:17:01,000
affect the speech
3574
02:17:01,040 --> 02:17:05,439
mark the most common approach for
3575
02:17:03,359 --> 02:17:08,200
outputting values to the console is by
3576
02:17:05,439 --> 02:17:10,240
having this format you type something in
3577
02:17:08,200 --> 02:17:12,319
as a static string and then you add a
3578
02:17:10,240 --> 02:17:15,120
variable to it but when you're doing
3579
02:17:12,319 --> 02:17:17,960
long sentences this format can get messy
3580
02:17:15,120 --> 02:17:20,519
quite quickly so this will just output
3581
02:17:17,960 --> 02:17:22,200
both of these on separate lines the name
3582
02:17:20,519 --> 02:17:25,040
follow follow by the name variable and
3583
02:17:22,200 --> 02:17:26,840
the age followed by the age variable I
3584
02:17:25,040 --> 02:17:28,800
just have a console right line to split
3585
02:17:26,840 --> 02:17:30,840
up these two sections just so you can
3586
02:17:28,800 --> 02:17:33,719
see it visually in the console so if we
3587
02:17:30,840 --> 02:17:36,240
run this code you can see that both the
3588
02:17:33,719 --> 02:17:39,280
outputs are exactly the same and the
3589
02:17:36,240 --> 02:17:41,439
reason why is we can use this back sln
3590
02:17:39,280 --> 02:17:43,679
what this back sln is doing is inserting
3591
02:17:41,439 --> 02:17:46,080
a new line character just where my
3592
02:17:43,679 --> 02:17:48,559
cursor is here which is forcing the age
3593
02:17:46,080 --> 02:17:50,399
to jump onto the next line the reason
3594
02:17:48,559 --> 02:17:52,840
why this is a slightly different shade
3595
02:17:50,399 --> 02:17:56,639
to the age text is because it's an
3596
02:17:52,840 --> 02:17:58,679
Escape character back sln means new line
3597
02:17:56,639 --> 02:18:00,760
we'll explain this in a further video in
3598
02:17:58,679 --> 02:18:02,719
this section so don't worry about that
3599
02:18:00,760 --> 02:18:04,840
just know that it means that it produces
3600
02:18:02,719 --> 02:18:06,840
a new line so as you can see since we're
3601
02:18:04,840 --> 02:18:08,960
adding the name followed by the name and
3602
02:18:06,840 --> 02:18:10,840
then the age followed by the age then
3603
02:18:08,960 --> 02:18:13,319
it's getting a bit long and a little bit
3604
02:18:10,840 --> 02:18:14,880
tedious it's not too bad in this case
3605
02:18:13,319 --> 02:18:16,760
because we're not really printing out a
3606
02:18:14,880 --> 02:18:18,880
sentence we're just going straight to
3607
02:18:16,760 --> 02:18:20,439
another line but let's see what this is
3608
02:18:18,880 --> 02:18:23,080
going to look like if we start dealing
3609
02:18:20,439 --> 02:18:26,719
with more multiple pits of data your
3610
02:18:23,080 --> 02:18:31,479
name is followed by the name and then we
3611
02:18:26,719 --> 02:18:33,479
can add a comma and your age is and then
3612
02:18:31,479 --> 02:18:35,240
add the age and as you can see while
3613
02:18:33,479 --> 02:18:37,519
we're structuring a sentence then it
3614
02:18:35,240 --> 02:18:40,040
gets a little bit long we have to
3615
02:18:37,519 --> 02:18:42,160
remember to put in the spaces otherwise
3616
02:18:40,040 --> 02:18:44,479
our code is going to look funky when we
3617
02:18:42,160 --> 02:18:46,519
print out to the screen and the reason
3618
02:18:44,479 --> 02:18:49,399
for that is there's no spaces between
3619
02:18:46,519 --> 02:18:51,000
the name and the age variables so let's
3620
02:18:49,399 --> 02:18:53,000
put them back in and make sure the
3621
02:18:51,000 --> 02:18:55,280
output looks all
3622
02:18:53,000 --> 02:18:57,679
right now that the output looks like
3623
02:18:55,280 --> 02:18:59,559
this let's explore composite formatting
3624
02:18:57,679 --> 02:19:02,000
and what this means is we can get rid of
3625
02:18:59,559 --> 02:19:04,439
all of these plus symbols and replace it
3626
02:19:02,000 --> 02:19:06,840
with curly braces so let's actually
3627
02:19:04,439 --> 02:19:09,920
output this string again but just output
3628
02:19:06,840 --> 02:19:12,880
it completely static your name is
3629
02:19:09,920 --> 02:19:15,359
abber and your age is
3630
02:19:12,880 --> 02:19:18,080
23 and now if we've run this you'll see
3631
02:19:15,359 --> 02:19:19,960
that both outputs are identical this is
3632
02:19:18,080 --> 02:19:22,200
because I know the values for the string
3633
02:19:19,960 --> 02:19:24,599
variables appear here and I can input it
3634
02:19:22,200 --> 02:19:26,439
straight into here so now that we know
3635
02:19:24,599 --> 02:19:28,719
what our sentence is going to be we
3636
02:19:26,439 --> 02:19:31,439
don't need to add any pluses what we do
3637
02:19:28,719 --> 02:19:33,439
need to do is remove our variables so in
3638
02:19:31,439 --> 02:19:35,960
the case of this sentence our name
3639
02:19:33,439 --> 02:19:38,439
variable is here and our age variable is
3640
02:19:35,960 --> 02:19:41,319
here so we can replace this with this
3641
02:19:38,439 --> 02:19:43,519
curly brace notation we can have curly
3642
02:19:41,319 --> 02:19:44,719
braces and then we want to start this
3643
02:19:43,519 --> 02:19:47,280
value at
3644
02:19:44,719 --> 02:19:50,080
zero throughout the string you want to
3645
02:19:47,280 --> 02:19:52,760
have these values starting from zero and
3646
02:19:50,080 --> 02:19:55,520
going up incrementally from left to
3647
02:19:52,760 --> 02:19:59,280
right so our next variable would be our
3648
02:19:55,520 --> 02:20:01,240
23 and we can store it like this now
3649
02:19:59,280 --> 02:20:03,359
that it's got your name zero and your
3650
02:20:01,240 --> 02:20:05,920
age one let's see what happens when you
3651
02:20:03,359 --> 02:20:08,240
output it then we just get this and this
3652
02:20:05,920 --> 02:20:10,760
is very confusing that's because we're
3653
02:20:08,240 --> 02:20:13,240
not done yet so now that we've got our
3654
02:20:10,760 --> 02:20:16,120
string typed in we can actually place a
3655
02:20:13,240 --> 02:20:18,439
comma in here and now it lets us put any
3656
02:20:16,120 --> 02:20:21,800
object in and any object could be a
3657
02:20:18,439 --> 02:20:24,000
string an in a double or any data type
3658
02:20:21,800 --> 02:20:26,319
so we want to insert the values inside
3659
02:20:24,000 --> 02:20:29,319
here going from left to right so our
3660
02:20:26,319 --> 02:20:32,160
name is first and then we do a comma and
3661
02:20:29,319 --> 02:20:34,200
we type in Age and what this now says is
3662
02:20:32,160 --> 02:20:37,000
the name will get replaced by the curly
3663
02:20:34,200 --> 02:20:40,000
brace zero and the age will get replaced
3664
02:20:37,000 --> 02:20:42,319
by the curly brace one so now if we run
3665
02:20:40,000 --> 02:20:44,280
the code let's see what happens and now
3666
02:20:42,319 --> 02:20:46,600
you'll see that the output is exactly
3667
02:20:44,280 --> 02:20:49,040
the same and now look at the difference
3668
02:20:46,600 --> 02:20:51,680
between these two lines you'll see that
3669
02:20:49,040 --> 02:20:53,600
these multiple amount of Plus es and not
3670
02:20:51,680 --> 02:20:55,399
confusing and let's say you forget this
3671
02:20:53,600 --> 02:20:57,200
one then you're going to get a syntax
3672
02:20:55,399 --> 02:20:59,080
error and it's a bit frustrating to keep
3673
02:20:57,200 --> 02:21:01,280
on typing when you're trying to join
3674
02:20:59,080 --> 02:21:03,640
things together now we can clearly see
3675
02:21:01,280 --> 02:21:06,319
that this Flo is like a sentence and
3676
02:21:03,640 --> 02:21:08,560
this space here looks very unnatural but
3677
02:21:06,319 --> 02:21:10,880
this space here looks like it's part of
3678
02:21:08,560 --> 02:21:11,720
the sentence which makes our code a bit
3679
02:21:10,880 --> 02:21:13,800
more
3680
02:21:11,720 --> 02:21:15,880
complete and the same goes if you want
3681
02:21:13,800 --> 02:21:18,399
to print something like this and let's
3682
02:21:15,880 --> 02:21:20,920
convert this one so if we copy this line
3683
02:21:18,399 --> 02:21:24,319
down into here and we just remove the
3684
02:21:20,920 --> 02:21:27,359
spaces then it can look something like
3685
02:21:24,319 --> 02:21:28,800
this name followed by the name we don't
3686
02:21:27,359 --> 02:21:30,160
actually need this space here because
3687
02:21:28,800 --> 02:21:32,640
we're going to be going into a brake
3688
02:21:30,160 --> 02:21:35,479
line we can replace our name with curly
3689
02:21:32,640 --> 02:21:38,160
braces zero and our age with curly
3690
02:21:35,479 --> 02:21:41,040
braces one and then straight after we
3691
02:21:38,160 --> 02:21:43,680
can type name and age and if we output
3692
02:21:41,040 --> 02:21:47,000
this we'll see it's exactly the same
3693
02:21:43,680 --> 02:21:48,960
there we go so just a quick recap
3694
02:21:47,000 --> 02:21:50,960
printing strings and any other variables
3695
02:21:48,960 --> 02:21:53,640
to the screen has been not normally
3696
02:21:50,960 --> 02:21:55,800
achieved by doing a string followed by a
3697
02:21:53,640 --> 02:21:58,680
plus and then adding another variable to
3698
02:21:55,800 --> 02:22:01,000
it adding more than one can be a little
3699
02:21:58,680 --> 02:22:02,760
bit complicated and hard to track of all
3700
02:22:01,000 --> 02:22:04,560
the spaces that you're going to put in
3701
02:22:02,760 --> 02:22:07,120
and you might accidentally miss one of
3702
02:22:04,560 --> 02:22:09,399
them like this one here or this one here
3703
02:22:07,120 --> 02:22:11,840
are very crucial to the output if you
3704
02:22:09,399 --> 02:22:14,080
miss this one then it's going to say is
3705
02:22:11,840 --> 02:22:16,880
followed by the name all together with
3706
02:22:14,080 --> 02:22:18,840
no space we then introduce composite
3707
02:22:16,880 --> 02:22:21,080
formatting which lets us use this
3708
02:22:18,840 --> 02:22:23,240
notation of curly bra Z
3709
02:22:21,080 --> 02:22:25,920
and curly brace one to give us the
3710
02:22:23,240 --> 02:22:29,080
ability to store some variables outside
3711
02:22:25,920 --> 02:22:31,280
of the string and then insert them in
3712
02:22:29,080 --> 02:22:34,000
dynamically we took both of these
3713
02:22:31,280 --> 02:22:36,280
examples and made them down here using
3714
02:22:34,000 --> 02:22:38,800
composite
3715
02:22:36,280 --> 02:22:40,920
formatting so let's explore a new method
3716
02:22:38,800 --> 02:22:42,920
of concatenation that makes this look a
3717
02:22:40,920 --> 02:22:45,600
lot better the reason why this is an
3718
02:22:42,920 --> 02:22:47,160
issue is you have spaces in here and
3719
02:22:45,600 --> 02:22:49,439
sometimes you might forget these spaces
3720
02:22:47,160 --> 02:22:51,359
and it could mess up your program this
3721
02:22:49,439 --> 02:22:53,439
string here looks very very unnatural
3722
02:22:51,359 --> 02:22:55,200
because you're forcing the space but
3723
02:22:53,439 --> 02:22:57,920
when you run it you'll see that it is
3724
02:22:55,200 --> 02:23:00,359
actually what you expect the spaces are
3725
02:22:57,920 --> 02:23:02,080
complete and it makes perfect sense but
3726
02:23:00,359 --> 02:23:04,000
when you're writing it it kind of looks
3727
02:23:02,080 --> 02:23:06,680
a little bit stupid because you're
3728
02:23:04,000 --> 02:23:08,600
forcing the space and then adding a plus
3729
02:23:06,680 --> 02:23:10,720
and then the name and then another plus
3730
02:23:08,600 --> 02:23:13,080
and then another static string and this
3731
02:23:10,720 --> 02:23:14,760
just get a little bit long to type it's
3732
02:23:13,080 --> 02:23:16,680
fine when you have a smaller sentence
3733
02:23:14,760 --> 02:23:18,200
like this but when the program starts to
3734
02:23:16,680 --> 02:23:19,960
grow then it could be a little bit
3735
02:23:18,200 --> 02:23:21,880
problematic and you'll have a lot of
3736
02:23:19,960 --> 02:23:24,800
syntax ER is when you forget to type in
3737
02:23:21,880 --> 02:23:26,640
all the pluses luckily this can be fixed
3738
02:23:24,800 --> 02:23:29,000
let's duplicate this line down and let's
3739
02:23:26,640 --> 02:23:31,600
see what we can do so we have this thing
3740
02:23:29,000 --> 02:23:34,279
called string interpolation and it uses
3741
02:23:31,600 --> 02:23:35,880
a dollar sign as a reference we can
3742
02:23:34,279 --> 02:23:38,439
place the dollar sign before the
3743
02:23:35,880 --> 02:23:40,319
quotation marks and then what you can do
3744
02:23:38,439 --> 02:23:43,120
is you can actually keep this as one big
3745
02:23:40,319 --> 02:23:46,080
sentence so let's remove all of the
3746
02:23:43,120 --> 02:23:47,560
values and have it like this so now of
3747
02:23:46,080 --> 02:23:49,319
course this actually doesn't print out
3748
02:23:47,560 --> 02:23:51,680
variables because we've just replaced it
3749
02:23:49,319 --> 02:23:53,640
with text so if we just run this then
3750
02:23:51,680 --> 02:23:56,240
it's completely messed up because now
3751
02:23:53,640 --> 02:23:58,399
we're not using our variables and this
3752
02:23:56,240 --> 02:24:00,439
can be easily fixed because we've used
3753
02:23:58,399 --> 02:24:03,240
our dollar sign now we can actually wrap
3754
02:24:00,439 --> 02:24:05,240
our variables in this curly brace and
3755
02:24:03,240 --> 02:24:06,880
now you can see it went white if I
3756
02:24:05,240 --> 02:24:08,080
double click on it you can see that it's
3757
02:24:06,880 --> 02:24:10,399
referencing the
3758
02:24:08,080 --> 02:24:12,160
variable wrapping the variables in the
3759
02:24:10,399 --> 02:24:14,200
curly brace means that they can actually
3760
02:24:12,160 --> 02:24:16,680
be processed variables even though they
3761
02:24:14,200 --> 02:24:18,520
are still within the speech marks please
3762
02:24:16,680 --> 02:24:20,960
do not remove the dollar sign because
3763
02:24:18,520 --> 02:24:23,960
now this will be interpreted as string
3764
02:24:20,960 --> 02:24:25,800
text as you can see here putting a
3765
02:24:23,960 --> 02:24:27,960
dollar sign at the start will make into
3766
02:24:25,800 --> 02:24:30,399
string interpolation and then we get the
3767
02:24:27,960 --> 02:24:32,760
ability to have our brackets this can
3768
02:24:30,399 --> 02:24:34,800
make string concatenation very easy to
3769
02:24:32,760 --> 02:24:37,160
do because you don't have to worry about
3770
02:24:34,800 --> 02:24:38,920
all the pluses and adding them together
3771
02:24:37,160 --> 02:24:41,200
and don't have to worry about the spaces
3772
02:24:38,920 --> 02:24:43,000
as well this looks more like a natural
3773
02:24:41,200 --> 02:24:44,760
sentence and what you can do for
3774
02:24:43,000 --> 02:24:48,160
practice is you can actually just type
3775
02:24:44,760 --> 02:24:51,760
out the sentence your name is Aba your
3776
02:24:48,160 --> 02:24:53,640
age is 23 and then from there you can
3777
02:24:51,760 --> 02:24:55,399
actually just break down the variables
3778
02:24:53,640 --> 02:24:57,279
and know that you'll need one here so
3779
02:24:55,399 --> 02:24:58,760
you can replace that with name and then
3780
02:24:57,279 --> 02:25:00,880
you'll know you'll need one here so you
3781
02:24:58,760 --> 02:25:02,840
can replace that one with age and then
3782
02:25:00,880 --> 02:25:04,880
you can place the dollar sign right at
3783
02:25:02,840 --> 02:25:07,279
the end and then there you go you have
3784
02:25:04,880 --> 02:25:09,680
your solution this method means that
3785
02:25:07,279 --> 02:25:12,000
it's a lot cleaner to read easier to
3786
02:25:09,680 --> 02:25:13,160
maintain and make changes in the future
3787
02:25:12,000 --> 02:25:15,840
and that's the whole point of
3788
02:25:13,160 --> 02:25:17,479
programming if your code base is very
3789
02:25:15,840 --> 02:25:19,160
difficult to go around and understand
3790
02:25:17,479 --> 02:25:20,800
what's going on and potentially make
3791
02:25:19,160 --> 02:25:22,680
changes in the future then you've
3792
02:25:20,800 --> 02:25:25,200
already made a bad step and you don't
3793
02:25:22,680 --> 02:25:26,359
want to get into these nasty habits it's
3794
02:25:25,200 --> 02:25:27,880
fine if you're just going to have
3795
02:25:26,359 --> 02:25:30,840
something like this where it's not
3796
02:25:27,880 --> 02:25:32,920
really complicated at all a string plus
3797
02:25:30,840 --> 02:25:34,720
another string that's quite simple and
3798
02:25:32,920 --> 02:25:36,439
that's fine you don't have to go the
3799
02:25:34,720 --> 02:25:38,600
extra effort to put in the dollar sign
3800
02:25:36,439 --> 02:25:40,640
and wrap it around in the curly braces
3801
02:25:38,600 --> 02:25:42,920
even though I personally still would
3802
02:25:40,640 --> 02:25:44,680
because in my personal opinion this
3803
02:25:42,920 --> 02:25:47,680
looks a lot
3804
02:25:44,680 --> 02:25:49,439
better than having it like that you can
3805
02:25:47,680 --> 02:25:51,840
see that it's a lot neater it takes up
3806
02:25:49,439 --> 02:25:54,160
less code and you can see visually that
3807
02:25:51,840 --> 02:25:55,760
you're using a specific variable and you
3808
02:25:54,160 --> 02:25:59,720
don't have to worry too much about the
3809
02:25:55,760 --> 02:25:59,720
spaces because this looks like a real
3810
02:26:00,000 --> 02:26:04,120
sentence another method of string
3811
02:26:02,200 --> 02:26:06,439
concatenation which is the idea of
3812
02:26:04,120 --> 02:26:08,880
taking strings or objects and add them
3813
02:26:06,439 --> 02:26:11,600
together to form a sentence of sorts we
3814
02:26:08,880 --> 02:26:14,279
can say your name is followed by space
3815
02:26:11,600 --> 02:26:16,319
and then add the name followed by space
3816
02:26:14,279 --> 02:26:18,800
add your ages and then add the actual
3817
02:26:16,319 --> 02:26:20,640
age variable and we can also write the
3818
02:26:18,800 --> 02:26:23,279
exact same line using string
3819
02:26:20,640 --> 02:26:26,080
interpolation or composite formatting
3820
02:26:23,279 --> 02:26:28,520
just like this and like this we can
3821
02:26:26,080 --> 02:26:31,120
specify the dollar symbol and put our
3822
02:26:28,520 --> 02:26:33,439
variable in the bracket which is shown
3823
02:26:31,120 --> 02:26:35,600
here and here and for the composite
3824
02:26:33,439 --> 02:26:37,560
formatting we can take the content where
3825
02:26:35,600 --> 02:26:40,279
our variable would go and replace it
3826
02:26:37,560 --> 02:26:42,560
with incremental numbers 0er and one
3827
02:26:40,279 --> 02:26:45,279
wrapped around in a curly brace and then
3828
02:26:42,560 --> 02:26:48,000
specify in the correct order zero takes
3829
02:26:45,279 --> 02:26:50,240
a value of name and one takes a value of
3830
02:26:48,000 --> 02:26:51,920
age if we run this code you'll see that
3831
02:26:50,240 --> 02:26:54,240
that the outputs are
3832
02:26:51,920 --> 02:26:57,399
identical so let's explore this other
3833
02:26:54,240 --> 02:26:59,080
method we have a string test for example
3834
02:26:57,399 --> 02:27:01,600
and then inside the string Library we
3835
02:26:59,080 --> 02:27:03,520
have this function called concat and it
3836
02:27:01,600 --> 02:27:06,439
can takeing any amount of parameters
3837
02:27:03,520 --> 02:27:09,800
that we want to give it so we could say
3838
02:27:06,439 --> 02:27:12,200
your name is space and the quotation
3839
02:27:09,800 --> 02:27:14,520
marks and then we can add a comma and
3840
02:27:12,200 --> 02:27:16,279
then we can say we want to add the name
3841
02:27:14,520 --> 02:27:17,920
and then another comma and we want to
3842
02:27:16,279 --> 02:27:20,399
add and your
3843
02:27:17,920 --> 02:27:23,120
ages and the speech marks add add a
3844
02:27:20,399 --> 02:27:25,479
comma add the age variable and that's it
3845
02:27:23,120 --> 02:27:27,760
and we could just use CW tab tab and
3846
02:27:25,479 --> 02:27:30,160
type in test and let's run it and see
3847
02:27:27,760 --> 02:27:32,560
what happens and now as you can see we
3848
02:27:30,160 --> 02:27:34,000
get the exact same output and because
3849
02:27:32,560 --> 02:27:36,359
this just returns us back with the
3850
02:27:34,000 --> 02:27:38,040
string we can also just take this entire
3851
02:27:36,359 --> 02:27:40,040
line and paste it straight into the
3852
02:27:38,040 --> 02:27:41,840
console right line you don't necessarily
3853
02:27:40,040 --> 02:27:44,359
have to store it but you can if you want
3854
02:27:41,840 --> 02:27:46,520
to and there you go all methods produce
3855
02:27:44,359 --> 02:27:48,399
the same output another way you can use
3856
02:27:46,520 --> 02:27:51,160
this concap function is by giving it
3857
02:27:48,399 --> 02:27:52,240
array we haven't covered array so far so
3858
02:27:51,160 --> 02:27:54,680
I'm just going to show you this for
3859
02:27:52,240 --> 02:27:56,399
demonstration purposes don't worry about
3860
02:27:54,680 --> 02:27:58,439
understanding it so just a quick
3861
02:27:56,399 --> 02:28:00,920
explanation arrays are just ways of
3862
02:27:58,439 --> 02:28:03,760
storing multiple of the same data type
3863
02:28:00,920 --> 02:28:05,600
so if we have these three names inside
3864
02:28:03,760 --> 02:28:08,200
our names we can actually use the
3865
02:28:05,600 --> 02:28:10,560
console to print them all out so we can
3866
02:28:08,200 --> 02:28:12,520
say string. concat and then we can
3867
02:28:10,560 --> 02:28:14,920
concat the names together which will
3868
02:28:12,520 --> 02:28:16,240
just add all of these three together now
3869
02:28:14,920 --> 02:28:18,160
the problem is when you run it you'll
3870
02:28:16,240 --> 02:28:20,279
see they're stuck together so what you
3871
02:28:18,160 --> 02:28:22,120
could do is just add a space between
3872
02:28:20,279 --> 02:28:23,920
them and as you can see this is getting
3873
02:28:22,120 --> 02:28:26,240
a little bit tedious
3874
02:28:23,920 --> 02:28:28,359
now there's other methods that we can
3875
02:28:26,240 --> 02:28:29,800
use to concat some arrays together and
3876
02:28:28,359 --> 02:28:31,960
I'll show you that in the next section
3877
02:28:29,800 --> 02:28:33,640
but for now don't worry about it this is
3878
02:28:31,960 --> 02:28:35,880
concentrating on the string concat
3879
02:28:33,640 --> 02:28:37,840
function and we'll just demonstrate that
3880
02:28:35,880 --> 02:28:39,760
again so if we run the line of code we
3881
02:28:37,840 --> 02:28:41,359
can see that they're all the same so now
3882
02:28:39,760 --> 02:28:42,840
that you have four different methods of
3883
02:28:41,359 --> 02:28:45,200
printing out exactly the same
3884
02:28:42,840 --> 02:28:47,080
information you might be asking yourself
3885
02:28:45,200 --> 02:28:49,399
what's the best method I personally
3886
02:28:47,080 --> 02:28:51,200
prefer to use the string interpolation
3887
02:28:49,399 --> 02:28:53,560
because with this one you can easily
3888
02:28:51,200 --> 02:28:55,279
miss the spaces and you could produce a
3889
02:28:53,560 --> 02:28:57,560
bad output and it might be stuck
3890
02:28:55,279 --> 02:28:59,520
together it's fine when you can see the
3891
02:28:57,560 --> 02:29:01,359
lines right in front of you and you can
3892
02:28:59,520 --> 02:29:03,120
easily identify the spaces but when
3893
02:29:01,359 --> 02:29:05,520
you're adding about 10 variables
3894
02:29:03,120 --> 02:29:07,399
together then it could be a problem the
3895
02:29:05,520 --> 02:29:09,439
reason why like string interpolation
3896
02:29:07,399 --> 02:29:11,200
using the dollar signs and the variables
3897
02:29:09,439 --> 02:29:13,520
in the curly braces is that you don't
3898
02:29:11,200 --> 02:29:15,520
have to rely on missing out spaces
3899
02:29:13,520 --> 02:29:17,240
because it actually forms a sentence and
3900
02:29:15,520 --> 02:29:19,439
you just replace the values with your
3901
02:29:17,240 --> 02:29:21,880
variable composite format in which is
3902
02:29:19,439 --> 02:29:24,080
this one is very efficient and very nice
3903
02:29:21,880 --> 02:29:26,040
to look at because it allows you to
3904
02:29:24,080 --> 02:29:28,080
specify the sentence and then plug in
3905
02:29:26,040 --> 02:29:29,760
the variables after the only problem
3906
02:29:28,080 --> 02:29:31,600
with this is if you were to change this
3907
02:29:29,760 --> 02:29:33,160
value in the future or to change a
3908
02:29:31,600 --> 02:29:35,600
sentence you have to make sure that the
3909
02:29:33,160 --> 02:29:37,200
numbers add up just like in the first
3910
02:29:35,600 --> 02:29:39,240
example if you only have a short
3911
02:29:37,200 --> 02:29:41,160
sentence then this isn't really an issue
3912
02:29:39,240 --> 02:29:42,840
since you can see it all visually and
3913
02:29:41,160 --> 02:29:44,479
you also got to realize that these need
3914
02:29:42,840 --> 02:29:46,760
to be in the right order because if you
3915
02:29:44,479 --> 02:29:48,800
put Age first then it's going to put age
3916
02:29:46,760 --> 02:29:50,240
in this one and name in the second one
3917
02:29:48,800 --> 02:29:52,560
and that's going to be problematic
3918
02:29:50,240 --> 02:29:54,399
because it now mess up the sentence
3919
02:29:52,560 --> 02:29:56,000
using string interpolation you don't
3920
02:29:54,399 --> 02:29:57,560
actually have to worry about anything
3921
02:29:56,000 --> 02:30:00,279
because if you wanted to change this to
3922
02:29:57,560 --> 02:30:02,880
say your age is then you simply just do
3923
02:30:00,279 --> 02:30:05,319
this and that's very easy to do because
3924
02:30:02,880 --> 02:30:07,200
in this case you'd say your age is and
3925
02:30:05,319 --> 02:30:09,200
then you'd have to change a string and
3926
02:30:07,200 --> 02:30:11,680
then now that this is zero we need to
3927
02:30:09,200 --> 02:30:14,640
change this from being age and this one
3928
02:30:11,680 --> 02:30:16,439
to be name whereas in the first example
3929
02:30:14,640 --> 02:30:18,600
you could just easily change the value
3930
02:30:16,439 --> 02:30:20,680
in the curly braces instead of changing
3931
02:30:18,600 --> 02:30:23,120
this reference and the end reference
3932
02:30:20,680 --> 02:30:25,120
conar is not widely used just because
3933
02:30:23,120 --> 02:30:27,160
you have all of these methods available
3934
02:30:25,120 --> 02:30:28,960
where you can easily add stuff together
3935
02:30:27,160 --> 02:30:31,399
but there could be user cases later on
3936
02:30:28,960 --> 02:30:32,840
down the line where uses just so it's a
3937
02:30:31,399 --> 02:30:35,200
bit more obvious and it helps the
3938
02:30:32,840 --> 02:30:37,240
readability of your code and maintain
3939
02:30:35,200 --> 02:30:38,760
within a big project or just on your
3940
02:30:37,240 --> 02:30:41,600
personal projects when you're looking
3941
02:30:38,760 --> 02:30:44,120
back at your previous
3942
02:30:41,600 --> 02:30:45,720
code when you create an initialized a
3943
02:30:44,120 --> 02:30:47,880
string variable you might want to give
3944
02:30:45,720 --> 02:30:49,319
it the value of an empty quot and this
3945
02:30:47,880 --> 02:30:51,200
will just default the value to
3946
02:30:49,319 --> 02:30:52,960
essentially nothing you're giving it a
3947
02:30:51,200 --> 02:30:54,600
value but at the same time the value
3948
02:30:52,960 --> 02:30:56,319
doesn't actually contain any sort of
3949
02:30:54,600 --> 02:30:57,520
characters if we print this out to the
3950
02:30:56,319 --> 02:30:59,680
screen then you won't really see
3951
02:30:57,520 --> 02:31:02,160
anything this video is going to focus on
3952
02:30:59,680 --> 02:31:03,960
string.empty and string. empty will
3953
02:31:02,160 --> 02:31:06,680
actually just represent these quotation
3954
02:31:03,960 --> 02:31:08,800
marks string.empty will represent these
3955
02:31:06,680 --> 02:31:11,600
speech marks so what we can do is we can
3956
02:31:08,800 --> 02:31:13,760
type in string using a DOT and then we
3957
02:31:11,600 --> 02:31:16,240
can type in empty just like that we've
3958
02:31:13,760 --> 02:31:18,880
initialize our string name to a value of
3959
02:31:16,240 --> 02:31:21,040
empty now this isn't necessarily used
3960
02:31:18,880 --> 02:31:23,000
when you're declaring an initializing it
3961
02:31:21,040 --> 02:31:25,279
because this is almost identical to just
3962
02:31:23,000 --> 02:31:27,200
defining a string on its own main reason
3963
02:31:25,279 --> 02:31:29,640
why this is used let's say we actually
3964
02:31:27,200 --> 02:31:32,640
ask them user to input their name so we
3965
02:31:29,640 --> 02:31:33,960
can say a console. right enter your name
3966
02:31:32,640 --> 02:31:36,960
and then we can take this string
3967
02:31:33,960 --> 02:31:39,200
variable move it down using alt and then
3968
02:31:36,960 --> 02:31:41,640
arrow keys and remove this and let's
3969
02:31:39,200 --> 02:31:43,399
initialize it to a console. readline to
3970
02:31:41,640 --> 02:31:45,960
read in the value from the
3971
02:31:43,399 --> 02:31:48,000
console and now what we can do is using
3972
02:31:45,960 --> 02:31:50,160
our is string interpolation method that
3973
02:31:48,000 --> 02:31:51,920
we just learned we can print to the user
3974
02:31:50,160 --> 02:31:54,439
your name is and then the name in The
3975
02:31:51,920 --> 02:31:56,720
Curly braces now let's just see what
3976
02:31:54,439 --> 02:31:59,439
happens so let's actually type our name
3977
02:31:56,720 --> 02:32:01,160
in abber and it says your name is ABBA
3978
02:31:59,439 --> 02:32:02,960
but what if the user just types nothing
3979
02:32:01,160 --> 02:32:05,000
and just presses enter Then it still
3980
02:32:02,960 --> 02:32:06,880
says your name is blank now let's see
3981
02:32:05,000 --> 02:32:08,800
what we can do here we don't necessarily
3982
02:32:06,880 --> 02:32:10,680
want to say your name is blank we want
3983
02:32:08,800 --> 02:32:13,319
to check what this value is so we can
3984
02:32:10,680 --> 02:32:15,680
say if the name is not equal to empty
3985
02:32:13,319 --> 02:32:18,760
quotation marks then we can output your
3986
02:32:15,680 --> 02:32:20,399
name is and then then else we can say
3987
02:32:18,760 --> 02:32:21,880
name is empty
3988
02:32:20,399 --> 02:32:24,600
and then if we run it now and press
3989
02:32:21,880 --> 02:32:26,439
enter it says name is empty so we can
3990
02:32:24,600 --> 02:32:28,040
use string. empty instead of these
3991
02:32:26,439 --> 02:32:30,000
quotation
3992
02:32:28,040 --> 02:32:32,160
marks this looks a lot better when
3993
02:32:30,000 --> 02:32:34,359
you're trying to compare it your
3994
02:32:32,160 --> 02:32:37,720
name is and then when we press enter
3995
02:32:34,359 --> 02:32:39,800
it says name is empty this just helps to
3996
02:32:37,720 --> 02:32:41,800
keep your code a lot tidier just so you
3997
02:32:39,800 --> 02:32:43,359
don't have to write the two quotes if
3998
02:32:41,800 --> 02:32:45,359
you accidentally put a space in there
3999
02:32:43,359 --> 02:32:47,600
now your if statement is incorrect if
4000
02:32:45,359 --> 02:32:49,240
you always use string. empty then it
4001
02:32:47,600 --> 02:32:50,920
just helps you to make sure that your
4002
02:32:49,240 --> 02:32:53,640
code is going to to be maintainable and
4003
02:32:50,920 --> 02:32:55,359
readable in the future reading string.
4004
02:32:53,640 --> 02:32:59,399
empty gives you a better representation
4005
02:32:55,359 --> 02:32:59,399
of what's going on instead of two open
4006
02:32:59,720 --> 02:33:03,920
quotes if there is a situation where you
4007
02:33:02,120 --> 02:33:06,720
want to check if one string equals
4008
02:33:03,920 --> 02:33:09,880
another string then you can do the
4009
02:33:06,720 --> 02:33:12,800
following if message equals equals
4010
02:33:09,880 --> 02:33:15,640
compare then we can print out same and
4011
02:33:12,800 --> 02:33:17,880
then else we can print out
4012
02:33:15,640 --> 02:33:19,920
different and as we can see here they
4013
02:33:17,880 --> 02:33:22,319
both have a capital H and they both say
4014
02:33:19,920 --> 02:33:24,439
hello so they should be identical let's
4015
02:33:22,319 --> 02:33:27,439
run the code and find out so they say
4016
02:33:24,439 --> 02:33:29,319
same which is exactly what we'd expect
4017
02:33:27,439 --> 02:33:31,479
so we have another method of being able
4018
02:33:29,319 --> 02:33:33,920
to do this so even though we can apply
4019
02:33:31,479 --> 02:33:36,399
equals equals to two strings it's
4020
02:33:33,920 --> 02:33:38,120
normally better practice to use equals
4021
02:33:36,399 --> 02:33:40,520
and then place the other string inside
4022
02:33:38,120 --> 02:33:43,120
the brackets so you're saying if message
4023
02:33:40,520 --> 02:33:44,920
equals compare now if you run the code
4024
02:33:43,120 --> 02:33:47,359
you'll see that it's the same
4025
02:33:44,920 --> 02:33:48,720
output in certain circumstances where
4026
02:33:47,359 --> 02:33:52,240
you want to ask what the user has
4027
02:33:48,720 --> 02:33:54,600
entered enter your name and then we can
4028
02:33:52,240 --> 02:33:58,120
use a string name equals console.
4029
02:33:54,600 --> 02:34:01,040
readline we can say if the name does not
4030
02:33:58,120 --> 02:34:05,479
equal to empty strings then from there
4031
02:34:01,040 --> 02:34:06,760
what we can do is type in your name is
4032
02:34:05,479 --> 02:34:09,600
plus
4033
02:34:06,760 --> 02:34:11,359
name and then we get some output
4034
02:34:09,600 --> 02:34:13,279
your name is but since we're
4035
02:34:11,359 --> 02:34:15,319
checking if it's not equal to that that
4036
02:34:13,279 --> 02:34:16,760
means they've entered something in let's
4037
02:34:15,319 --> 02:34:18,399
see if there's another way that we can
4038
02:34:16,760 --> 02:34:20,120
actually write this so if we just
4039
02:34:18,399 --> 02:34:22,040
duplicate it and comment it out so we
4040
02:34:20,120 --> 02:34:24,560
got it for reference so what we want to
4041
02:34:22,040 --> 02:34:28,279
do is we're checking if name is not
4042
02:34:24,560 --> 02:34:31,800
equal to empty quotes so we can say name
4043
02:34:28,279 --> 02:34:33,760
do equals empty quotes but what this is
4044
02:34:31,800 --> 02:34:35,760
telling us if the name equals empty
4045
02:34:33,760 --> 02:34:37,399
quotes then we should do this so then
4046
02:34:35,760 --> 02:34:39,439
what we can do when we learn about the
4047
02:34:37,399 --> 02:34:42,040
if statements is we can use the not
4048
02:34:39,439 --> 02:34:44,720
operator and we can say if the name not
4049
02:34:42,040 --> 02:34:48,399
equals to empty quotes then we proceed
4050
02:34:44,720 --> 02:34:51,439
with this line else we can just
4051
02:34:48,399 --> 02:34:53,399
say and valid name
4052
02:34:51,439 --> 02:34:55,439
input let's run that and see what
4053
02:34:53,399 --> 02:34:57,600
happens if you type in then it
4054
02:34:55,439 --> 02:34:59,560
passes fine and if we type in nothing
4055
02:34:57,600 --> 02:35:02,319
then we get an invalid name input the
4056
02:34:59,560 --> 02:35:04,720
reason why equals equals and the equals
4057
02:35:02,319 --> 02:35:07,240
function are different is because lower
4058
02:35:04,720 --> 02:35:09,720
level using equals equals will actually
4059
02:35:07,240 --> 02:35:11,399
compare the reference in the memory and
4060
02:35:09,720 --> 02:35:13,479
the reference in the memory is what's
4061
02:35:11,399 --> 02:35:16,000
used up when you actually create these
4062
02:35:13,479 --> 02:35:17,600
variables so what this is doing is it's
4063
02:35:16,000 --> 02:35:19,880
comparing the value that's actually
4064
02:35:17,600 --> 02:35:22,120
stored in memory to the other one that's
4065
02:35:19,880 --> 02:35:24,200
stored in memory now the problem is when
4066
02:35:22,120 --> 02:35:26,319
it comes to something like arrays where
4067
02:35:24,200 --> 02:35:27,960
you actually have the same value but
4068
02:35:26,319 --> 02:35:29,600
they're slightly different let me
4069
02:35:27,960 --> 02:35:31,279
demonstrate so I'm just going to use
4070
02:35:29,600 --> 02:35:33,040
this code because we haven't covered
4071
02:35:31,279 --> 02:35:35,439
arrays yet so I'm just showing you an
4072
02:35:33,040 --> 02:35:37,840
example if we have a chart array that
4073
02:35:35,439 --> 02:35:40,479
literally says hello and then we can use
4074
02:35:37,840 --> 02:35:42,479
this line of code to join them together
4075
02:35:40,479 --> 02:35:44,600
but this is actually an object instead
4076
02:35:42,479 --> 02:35:46,479
of a string but if you see what we can
4077
02:35:44,600 --> 02:35:49,680
do in the if statement we can say if the
4078
02:35:46,479 --> 02:35:51,880
message which is equal to hello we can
4079
02:35:49,680 --> 02:35:54,319
use message. equals and we got the new
4080
02:35:51,880 --> 02:35:56,840
compare and this is actually an object
4081
02:35:54,319 --> 02:35:59,080
so now we're comparing a string with an
4082
02:35:56,840 --> 02:36:01,319
object and generically speaking an
4083
02:35:59,080 --> 02:36:03,080
object can hold any sort of value
4084
02:36:01,319 --> 02:36:05,960
because all of these variables that are
4085
02:36:03,080 --> 02:36:07,760
strings in double Etc they're all deriv
4086
02:36:05,960 --> 02:36:09,800
from something called an object which is
4087
02:36:07,760 --> 02:36:12,080
just a very generic way of storing
4088
02:36:09,800 --> 02:36:13,960
information so since these two data
4089
02:36:12,080 --> 02:36:16,319
types are little bit different they
4090
02:36:13,960 --> 02:36:18,120
still evaluate the same thing what the
4091
02:36:16,319 --> 02:36:20,080
equals function is doing is actually
4092
02:36:18,120 --> 02:36:22,399
checking the literal values of the
4093
02:36:20,080 --> 02:36:24,080
variables instead of checking the values
4094
02:36:22,399 --> 02:36:26,720
that are held inside the memory address
4095
02:36:24,080 --> 02:36:28,880
location and the difference here is even
4096
02:36:26,720 --> 02:36:31,240
though we have a character array that is
4097
02:36:28,880 --> 02:36:33,200
giving us the values hello and our
4098
02:36:31,240 --> 02:36:35,680
string is also given us the value of
4099
02:36:33,200 --> 02:36:38,359
hello if we run this line of code that
4100
02:36:35,680 --> 02:36:40,680
uses do equals then you'll actually see
4101
02:36:38,359 --> 02:36:42,600
that the second same actually represents
4102
02:36:40,680 --> 02:36:45,000
the bottom if statement so what that's
4103
02:36:42,600 --> 02:36:47,359
doing is it's actually giving us back
4104
02:36:45,000 --> 02:36:49,720
the comparison saying that it's true and
4105
02:36:47,359 --> 02:36:52,040
it is indeed true because hello is is
4106
02:36:49,720 --> 02:36:54,200
literally equal to hello but if you were
4107
02:36:52,040 --> 02:36:56,319
to use the double
4108
02:36:54,200 --> 02:36:58,920
equals then what you'll realize when you
4109
02:36:56,319 --> 02:37:01,439
run the code is now it'll say it's
4110
02:36:58,920 --> 02:37:03,640
different because what it's doing now is
4111
02:37:01,439 --> 02:37:06,319
it's taking a string object and
4112
02:37:03,640 --> 02:37:08,920
comparing it to an object and they are
4113
02:37:06,319 --> 02:37:10,560
not different because of their memory
4114
02:37:08,920 --> 02:37:12,680
and that's what the equals equals is
4115
02:37:10,560 --> 02:37:14,640
comparing it's not only comparing the
4116
02:37:12,680 --> 02:37:16,560
value it's comparing the memory address
4117
02:37:14,640 --> 02:37:18,479
location and that's going to be vastly
4118
02:37:16,560 --> 02:37:20,600
confusing because you don't want to hit
4119
02:37:18,479 --> 02:37:22,640
these situations so it's always best to
4120
02:37:20,600 --> 02:37:24,920
use the equals function because the
4121
02:37:22,640 --> 02:37:27,040
equals function is concerned about the
4122
02:37:24,920 --> 02:37:29,520
values rather than where they are in
4123
02:37:27,040 --> 02:37:31,800
your memory I hope that made sense and
4124
02:37:29,520 --> 02:37:34,160
it wasn't just confusing but basically
4125
02:37:31,800 --> 02:37:35,760
when you use equals equals it checks the
4126
02:37:34,160 --> 02:37:38,279
values that are actually wrote in your
4127
02:37:35,760 --> 02:37:40,880
computer's memory instead of the value
4128
02:37:38,279 --> 02:37:43,479
itself it will do both it will check the
4129
02:37:40,880 --> 02:37:45,920
memory and check the value whereas when
4130
02:37:43,479 --> 02:37:48,040
use do equals it is only concerned about
4131
02:37:45,920 --> 02:37:49,600
the values and that's all that matters
4132
02:37:48,040 --> 02:37:51,520
when we're doing an if statement like
4133
02:37:49,600 --> 02:37:53,760
this we're checking if they're both the
4134
02:37:51,520 --> 02:37:55,600
same so it shouldn't really matter how
4135
02:37:53,760 --> 02:37:57,560
and where they are stored the only thing
4136
02:37:55,600 --> 02:37:59,520
that matters is the values contained
4137
02:37:57,560 --> 02:38:03,120
within the variable we're only caring
4138
02:37:59,520 --> 02:38:03,120
about the values and not where they're
4139
02:38:03,160 --> 02:38:08,840
stored so when we have a string variable
4140
02:38:05,800 --> 02:38:10,399
defined in C we have it like this and
4141
02:38:08,840 --> 02:38:12,479
when we print it out to the console we
4142
02:38:10,399 --> 02:38:14,600
get the entire string being
4143
02:38:12,479 --> 02:38:16,760
displayed but what if you want to return
4144
02:38:14,600 --> 02:38:18,600
the first element back and not the rest
4145
02:38:16,760 --> 02:38:20,240
then what are your options so in the
4146
02:38:18,600 --> 02:38:22,319
later video in the of course we covered
4147
02:38:20,240 --> 02:38:24,319
the substring function and technically
4148
02:38:22,319 --> 02:38:26,040
you could go from the first character
4149
02:38:24,319 --> 02:38:28,600
till the second character and you will
4150
02:38:26,040 --> 02:38:31,240
just get C back so that's index zero
4151
02:38:28,600 --> 02:38:32,479
till index one and you'll get the cback
4152
02:38:31,240 --> 02:38:34,120
now the problem with that is that's an
4153
02:38:32,479 --> 02:38:36,240
extra function call of something that
4154
02:38:34,120 --> 02:38:38,680
should be quite simple and it is a
4155
02:38:36,240 --> 02:38:41,359
message is a string and strings tend to
4156
02:38:38,680 --> 02:38:43,640
be an array of characters which is
4157
02:38:41,359 --> 02:38:45,279
similar to this we haven't learned about
4158
02:38:43,640 --> 02:38:46,880
array so far and we'll cover that in the
4159
02:38:45,279 --> 02:38:48,600
next section but what an array
4160
02:38:46,880 --> 02:38:51,279
essentially is is it lets you store
4161
02:38:48,600 --> 02:38:53,279
multiple of that data type so we could
4162
02:38:51,279 --> 02:38:55,640
have a chart array that holds exactly
4163
02:38:53,279 --> 02:38:57,640
the same value and each position will
4164
02:38:55,640 --> 02:38:59,200
hold each one of these characters and
4165
02:38:57,640 --> 02:39:00,880
the reason why I'm mentioning this is
4166
02:38:59,200 --> 02:39:02,840
because with strings you can use the
4167
02:39:00,880 --> 02:39:04,880
square bracket notation so if you add
4168
02:39:02,840 --> 02:39:07,279
some square brackets at the end it says
4169
02:39:04,880 --> 02:39:10,240
here it returns back a Char and it takes
4170
02:39:07,279 --> 02:39:12,080
in an index gets the CH object at the
4171
02:39:10,240 --> 02:39:14,800
specified position in the current string
4172
02:39:12,080 --> 02:39:17,640
object so if we use message zero you can
4173
02:39:14,800 --> 02:39:20,080
see that returns us back with a ch ch
4174
02:39:17,640 --> 02:39:22,399
right here gets the CH object so we can
4175
02:39:20,080 --> 02:39:24,560
call this on a string and it can return
4176
02:39:22,399 --> 02:39:26,960
us back with a character because like I
4177
02:39:24,560 --> 02:39:29,240
said with the array of characters that's
4178
02:39:26,960 --> 02:39:31,000
essentially what a string is each one of
4179
02:39:29,240 --> 02:39:33,120
these is just its own independent
4180
02:39:31,000 --> 02:39:35,359
character and a string lets you pair
4181
02:39:33,120 --> 02:39:37,399
them all together so if we print this
4182
02:39:35,359 --> 02:39:39,840
out then we should get C because it's
4183
02:39:37,399 --> 02:39:40,720
zerob based so if we copy this down and
4184
02:39:39,840 --> 02:39:44,760
use
4185
02:39:40,720 --> 02:39:45,800
one 2 and three then what should we get
4186
02:39:44,760 --> 02:39:48,319
we get
4187
02:39:45,800 --> 02:39:50,160
C then we get actually nothing
4188
02:39:48,319 --> 02:39:52,399
technically because it's a space
4189
02:39:50,160 --> 02:39:53,840
character and then the third character
4190
02:39:52,399 --> 02:39:57,040
which is the fourth in the list because
4191
02:39:53,840 --> 02:39:58,399
it's zero based is going to be our I so
4192
02:39:57,040 --> 02:40:00,640
let's just print this out to the screen
4193
02:39:58,399 --> 02:40:02,080
see what happens there we go so even
4194
02:40:00,640 --> 02:40:04,279
though we don't see it visually there's
4195
02:40:02,080 --> 02:40:05,680
actually a space character right there
4196
02:40:04,279 --> 02:40:08,439
and that's the third character in The
4197
02:40:05,680 --> 02:40:10,399
List which is right here so of course we
4198
02:40:08,439 --> 02:40:12,800
don't tend to know how long the string
4199
02:40:10,399 --> 02:40:14,319
is unless we can see it visually and if
4200
02:40:12,800 --> 02:40:16,200
you were to reference one of these
4201
02:40:14,319 --> 02:40:18,640
values and the string is not actually
4202
02:40:16,200 --> 02:40:20,680
that long then you have a problem let's
4203
02:40:18,640 --> 02:40:23,600
demonstrate if we have the string just
4204
02:40:20,680 --> 02:40:26,000
called C and let's comment out the rest
4205
02:40:23,600 --> 02:40:28,640
then that's only two characters so only
4206
02:40:26,000 --> 02:40:30,680
zero and one will be applicable these
4207
02:40:28,640 --> 02:40:32,319
two will return back something let's
4208
02:40:30,680 --> 02:40:34,160
have a look and see what happens so the
4209
02:40:32,319 --> 02:40:36,479
first character and the second character
4210
02:40:34,160 --> 02:40:38,279
will run fine and let's put a console
4211
02:40:36,479 --> 02:40:40,920
read line in there just to break it up
4212
02:40:38,279 --> 02:40:43,760
so the other code doesn't run yet if we
4213
02:40:40,920 --> 02:40:46,520
run the code we get our C and that's the
4214
02:40:43,760 --> 02:40:49,000
max length of our string so far it's
4215
02:40:46,520 --> 02:40:51,760
zero and one now if we press enter let's
4216
02:40:49,000 --> 02:40:53,920
see what happens we get an index out of
4217
02:40:51,760 --> 02:40:55,960
range exception index was outside the
4218
02:40:53,920 --> 02:40:57,920
bounds of the array and the reason for
4219
02:40:55,960 --> 02:40:59,880
that is we only have two characters
4220
02:40:57,920 --> 02:41:02,479
inside our message and we're trying to
4221
02:40:59,880 --> 02:41:04,200
display the third and the fourth this is
4222
02:41:02,479 --> 02:41:06,160
problematic because we literally don't
4223
02:41:04,200 --> 02:41:08,479
have any of them characters to
4224
02:41:06,160 --> 02:41:10,120
display so what we can do instead is
4225
02:41:08,479 --> 02:41:12,160
actually write ourselves a little Loop
4226
02:41:10,120 --> 02:41:13,720
to go through the entire string and
4227
02:41:12,160 --> 02:41:15,760
return us back all of these character
4228
02:41:13,720 --> 02:41:17,760
positions instead of us just guessing
4229
02:41:15,760 --> 02:41:19,800
how long the string is so if we return
4230
02:41:17,760 --> 02:41:21,600
this string back to normal and then just
4231
02:41:19,800 --> 02:41:25,000
after this little section we can write a
4232
02:41:21,600 --> 02:41:27,680
for Loop in I equals 0 and I is less
4233
02:41:25,000 --> 02:41:29,640
than message and then we have access to
4234
02:41:27,680 --> 02:41:31,359
dot length this gets the number of
4235
02:41:29,640 --> 02:41:32,920
characters in the current string so we
4236
02:41:31,359 --> 02:41:34,920
don't have to worry about the maximum
4237
02:41:32,920 --> 02:41:37,040
length in the square brackets we can
4238
02:41:34,920 --> 02:41:39,279
always rely on dot length because we
4239
02:41:37,040 --> 02:41:41,000
don't have to keep track of it and the
4240
02:41:39,279 --> 02:41:42,040
language can know that for us and give
4241
02:41:41,000 --> 02:41:44,479
us the
4242
02:41:42,040 --> 02:41:45,720
value in this case we're going from zero
4243
02:41:44,479 --> 02:41:48,479
to whatever the length of these
4244
02:41:45,720 --> 02:41:50,279
characters are and then in here we can
4245
02:41:48,479 --> 02:41:52,760
actually just conso right line and we
4246
02:41:50,279 --> 02:41:53,840
can do the same notation we did here
4247
02:41:52,760 --> 02:41:56,000
we're saying we want to print the
4248
02:41:53,840 --> 02:41:58,720
message but not only the message we want
4249
02:41:56,000 --> 02:42:00,760
to print the index and the index is
4250
02:41:58,720 --> 02:42:02,960
going to be I in this case because we
4251
02:42:00,760 --> 02:42:05,240
start at zero since it's zero based and
4252
02:42:02,960 --> 02:42:07,200
then we're going to the last character
4253
02:42:05,240 --> 02:42:10,720
so let's print this and see what
4254
02:42:07,200 --> 02:42:13,439
happens so now we get C Is Awesome from
4255
02:42:10,720 --> 02:42:15,479
here down to the bottom c is awesome
4256
02:42:13,439 --> 02:42:17,240
print on every single line and that's a
4257
02:42:15,479 --> 02:42:18,720
little bit annoying to read so what we
4258
02:42:17,240 --> 02:42:20,880
could do is just change this to a
4259
02:42:18,720 --> 02:42:22,760
console right and maybe we can just
4260
02:42:20,880 --> 02:42:25,120
comment this section out just so we can
4261
02:42:22,760 --> 02:42:27,600
see this
4262
02:42:25,120 --> 02:42:29,600
properly there we go so we get c is
4263
02:42:27,600 --> 02:42:31,000
awesome and because it's a console right
4264
02:42:29,600 --> 02:42:32,840
it's right in them all next to each
4265
02:42:31,000 --> 02:42:35,560
other and they're being printed out one
4266
02:42:32,840 --> 02:42:38,200
by one let's add a cool little effect I
4267
02:42:35,560 --> 02:42:40,120
can demonstrate you for now so inside
4268
02:42:38,200 --> 02:42:42,520
the system. threading Library we have
4269
02:42:40,120 --> 02:42:45,800
this sleep function so what we can do is
4270
02:42:42,520 --> 02:42:48,040
we can add another using command system.
4271
02:42:45,800 --> 02:42:49,960
threading we already have threading do
4272
02:42:48,040 --> 02:42:52,680
tasks in there but the function that we
4273
02:42:49,960 --> 02:42:54,080
need is inside the threading Library so
4274
02:42:52,680 --> 02:42:56,040
there's two ways we can write this we
4275
02:42:54,080 --> 02:42:59,479
can actually write everything by hand
4276
02:42:56,040 --> 02:43:01,279
like this dot sleep or you could just
4277
02:42:59,479 --> 02:43:03,000
remove the system. threading because
4278
02:43:01,279 --> 02:43:04,279
we've added it at the Top If you don't
4279
02:43:03,000 --> 02:43:06,160
want to add it at the top that's not
4280
02:43:04,279 --> 02:43:08,439
really a problem you can just include it
4281
02:43:06,160 --> 02:43:09,760
straight into here adding it at the top
4282
02:43:08,439 --> 02:43:11,319
will include it within the entire
4283
02:43:09,760 --> 02:43:13,160
project but if you're only going to use
4284
02:43:11,319 --> 02:43:14,720
it once or twice then sometimes it's
4285
02:43:13,160 --> 02:43:16,359
better to just include it straight into
4286
02:43:14,720 --> 02:43:18,920
here because it'll make your overall
4287
02:43:16,359 --> 02:43:20,479
program size a lot smaller so let's just
4288
02:43:18,920 --> 02:43:23,279
keep keep it in so we can make our Corde
4289
02:43:20,479 --> 02:43:25,160
a bit more readable so we have thread
4290
02:43:23,279 --> 02:43:26,880
dos sleep and what this will do is it
4291
02:43:25,160 --> 02:43:29,479
will suspend the current thread for a
4292
02:43:26,880 --> 02:43:32,000
specified number of milliseconds there's
4293
02:43:29,479 --> 02:43:33,680
a th000 milliseconds in 1 second so what
4294
02:43:32,000 --> 02:43:36,240
we can do in here and we're trying to
4295
02:43:33,680 --> 02:43:38,520
mimic a typewriter effect so we can say
4296
02:43:36,240 --> 02:43:40,720
something like 200 which will be roughly
4297
02:43:38,520 --> 02:43:42,560
a quarter of a second so if we've run
4298
02:43:40,720 --> 02:43:44,279
the code now let's see what happens so
4299
02:43:42,560 --> 02:43:45,960
we get this cool effect where after it
4300
02:43:44,279 --> 02:43:48,080
prints out each of the characters it
4301
02:43:45,960 --> 02:43:49,960
will print out the next character only
4302
02:43:48,080 --> 02:43:52,040
after it's being asleep for a quarter of
4303
02:43:49,960 --> 02:43:53,479
a second and that looks kind of cool if
4304
02:43:52,040 --> 02:43:55,840
you wanted to speed it up you could just
4305
02:43:53,479 --> 02:43:58,520
decrease this number there you
4306
02:43:55,840 --> 02:43:59,960
go of course if you make it too low then
4307
02:43:58,520 --> 02:44:02,439
it might be too fast for you to actually
4308
02:43:59,960 --> 02:44:04,359
acknowledge what's happening and if you
4309
02:44:02,439 --> 02:44:06,279
also make it too slow then it's just
4310
02:44:04,359 --> 02:44:08,279
going to take too long to turn off and
4311
02:44:06,279 --> 02:44:10,160
at that point you can use contrl C on
4312
02:44:08,279 --> 02:44:12,800
your keyboard or use the stop button at
4313
02:44:10,160 --> 02:44:14,080
the top to terminate the program okay so
4314
02:44:12,800 --> 02:44:16,000
let's have a look at another thing that
4315
02:44:14,080 --> 02:44:18,640
we can do with this so maybe we can make
4316
02:44:16,000 --> 02:44:20,680
this 100 for now in the previous videos
4317
02:44:18,640 --> 02:44:22,560
we've used use a function called
4318
02:44:20,680 --> 02:44:24,880
contains and let's say we're just
4319
02:44:22,560 --> 02:44:26,720
checking if it contains C and we know it
4320
02:44:24,880 --> 02:44:27,960
does because we can see it visually
4321
02:44:26,720 --> 02:44:30,319
let's run the
4322
02:44:27,960 --> 02:44:32,359
code and we see we have true being
4323
02:44:30,319 --> 02:44:34,200
printed and the reason why true is
4324
02:44:32,359 --> 02:44:37,040
actually on the same line as c is
4325
02:44:34,200 --> 02:44:39,000
awesome is because we're using console.
4326
02:44:37,040 --> 02:44:41,319
right here so it's actually not made a
4327
02:44:39,000 --> 02:44:43,359
new line character just after the E so
4328
02:44:41,319 --> 02:44:45,399
when it's printing true it prints it
4329
02:44:43,359 --> 02:44:47,040
straight after that so what we can do to
4330
02:44:45,399 --> 02:44:48,600
fix that is just place a new line
4331
02:44:47,040 --> 02:44:50,200
character in here using console.
4332
02:44:48,600 --> 02:44:53,560
readline and we can just drop this a
4333
02:44:50,200 --> 02:44:56,160
little bit lower to 50 there we go so we
4334
02:44:53,560 --> 02:44:57,760
have true and what we can do with this
4335
02:44:56,160 --> 02:45:00,240
is we can actually make our own little
4336
02:44:57,760 --> 02:45:02,680
version of the contains function let's
4337
02:45:00,240 --> 02:45:05,680
demonstrate so if we make this exact for
4338
02:45:02,680 --> 02:45:09,080
loop again in I equals z i is less than
4339
02:45:05,680 --> 02:45:10,520
the message. length and i++ so first
4340
02:45:09,080 --> 02:45:12,359
let's kind of understand what's going on
4341
02:45:10,520 --> 02:45:14,439
with this function returns a value
4342
02:45:12,359 --> 02:45:16,439
indicated whether a specified subring
4343
02:45:14,439 --> 02:45:18,720
occurs within this string and what that
4344
02:45:16,439 --> 02:45:21,000
means is it will return a Boolean for us
4345
02:45:18,720 --> 02:45:22,920
if C is inside our message then this
4346
02:45:21,000 --> 02:45:24,760
will return back true and we know this
4347
02:45:22,920 --> 02:45:26,640
is true because we just outputed it to
4348
02:45:24,760 --> 02:45:28,680
the screen and we can see this visually
4349
02:45:26,640 --> 02:45:30,640
so what we need to do is we need to Loop
4350
02:45:28,680 --> 02:45:33,479
through our message and try and figure
4351
02:45:30,640 --> 02:45:35,120
out if we can find C inside our message
4352
02:45:33,479 --> 02:45:38,000
and then we can give back a value is
4353
02:45:35,120 --> 02:45:40,000
true so maybe we can do a bu contains
4354
02:45:38,000 --> 02:45:41,800
equal false and the reason why we
4355
02:45:40,000 --> 02:45:43,760
initial it to false and the reason why
4356
02:45:41,800 --> 02:45:45,560
we initialize it to false is because we
4357
02:45:43,760 --> 02:45:47,880
want to set it up telling it that we
4358
02:45:45,560 --> 02:45:51,040
haven't found it and then inside the for
4359
02:45:47,880 --> 02:45:53,479
Loop if we find it then we can set it to
4360
02:45:51,040 --> 02:45:55,520
True let's have a look so after the for
4361
02:45:53,479 --> 02:45:57,720
Loop we can print contains of the screen
4362
02:45:55,520 --> 02:46:00,439
and then inside the for Loop what we can
4363
02:45:57,720 --> 02:46:02,040
do as we've seen here it returns back
4364
02:46:00,439 --> 02:46:05,560
every single character so you'll get
4365
02:46:02,040 --> 02:46:08,240
back C sharp then the space then I then
4366
02:46:05,560 --> 02:46:09,640
s and so on and so forth and what we
4367
02:46:08,240 --> 02:46:12,120
want to do is we want to go through each
4368
02:46:09,640 --> 02:46:15,760
of the characters and then just ask if
4369
02:46:12,120 --> 02:46:18,120
the variable is equal to C if C is equal
4370
02:46:15,760 --> 02:46:20,960
to C then we can assign this to true and
4371
02:46:18,120 --> 02:46:23,560
then go through #is equal to C if space
4372
02:46:20,960 --> 02:46:24,920
is equal to C and so on and so forth so
4373
02:46:23,560 --> 02:46:28,080
we can write a if statement here that
4374
02:46:24,920 --> 02:46:31,439
says message square bracket I is equal
4375
02:46:28,080 --> 02:46:33,200
to equal to apostrophe C and the reason
4376
02:46:31,439 --> 02:46:35,439
why I said apostrophe is because this is
4377
02:46:33,200 --> 02:46:37,000
a character and this also returns us
4378
02:46:35,439 --> 02:46:39,680
back with a character so we need to
4379
02:46:37,000 --> 02:46:41,279
compare them both like that if we use a
4380
02:46:39,680 --> 02:46:43,240
string in here then it might complain
4381
02:46:41,279 --> 02:46:45,319
that we're trying to convert between a
4382
02:46:43,240 --> 02:46:47,200
Char and a string and it won't let you
4383
02:46:45,319 --> 02:46:49,680
do the equals equals operator between
4384
02:46:47,200 --> 02:46:51,680
two different variables maybe what we
4385
02:46:49,680 --> 02:46:53,640
can do instead is to follow the approach
4386
02:46:51,680 --> 02:46:56,160
that we used before and use the equals
4387
02:46:53,640 --> 02:46:57,760
function and place our C inside here so
4388
02:46:56,160 --> 02:47:00,760
if the character that we're currently up
4389
02:46:57,760 --> 02:47:03,399
to inside the for Loop is equal to our C
4390
02:47:00,760 --> 02:47:05,160
which is what we want to search for then
4391
02:47:03,399 --> 02:47:07,920
we can set our contains variable equal
4392
02:47:05,160 --> 02:47:10,319
to true we don't need to write an L
4393
02:47:07,920 --> 02:47:12,600
statement in here to set our contains to
4394
02:47:10,319 --> 02:47:14,520
false because we've already gave a value
4395
02:47:12,600 --> 02:47:16,359
at the start if we didn't have this
4396
02:47:14,520 --> 02:47:18,920
value then it'll be beneficial to do
4397
02:47:16,359 --> 02:47:20,279
this but since we have it then we can
4398
02:47:18,920 --> 02:47:22,319
remove this other path and we don't
4399
02:47:20,279 --> 02:47:24,240
exactly need it so let's have a look and
4400
02:47:22,319 --> 02:47:27,279
see if this
4401
02:47:24,240 --> 02:47:28,880
works perfect it says true maybe we can
4402
02:47:27,279 --> 02:47:30,680
change this character to something like
4403
02:47:28,880 --> 02:47:33,560
a make sure this
4404
02:47:30,680 --> 02:47:35,080
works which it does as well so perfect
4405
02:47:33,560 --> 02:47:37,520
so now we've made our own mini little
4406
02:47:35,080 --> 02:47:40,200
contains function inside here and we can
4407
02:47:37,520 --> 02:47:41,880
also use the C library one but the
4408
02:47:40,200 --> 02:47:43,760
purpose of this exercise was just to
4409
02:47:41,880 --> 02:47:46,040
show you how we can iterate through a
4410
02:47:43,760 --> 02:47:48,120
string variable and then decide what
4411
02:47:46,040 --> 02:47:49,840
each of the characters can be equal to
4412
02:47:48,120 --> 02:47:51,840
and then we can decide if they're equal
4413
02:47:49,840 --> 02:47:53,920
to another one and then let the user
4414
02:47:51,840 --> 02:47:56,279
know that we found a match for their
4415
02:47:53,920 --> 02:47:56,279
search
4416
02:47:56,359 --> 02:48:01,000
term so far in the course we've covered
4417
02:47:58,840 --> 02:48:02,359
the equals function and the contains
4418
02:48:01,000 --> 02:48:04,520
function when you're searching through
4419
02:48:02,359 --> 02:48:06,240
strings or trying to find values let's
4420
02:48:04,520 --> 02:48:08,520
have a look at a method that makes sure
4421
02:48:06,240 --> 02:48:10,200
a string is valid before we can process
4422
02:48:08,520 --> 02:48:12,399
information on it so in this quick
4423
02:48:10,200 --> 02:48:14,319
example we have a name output we're
4424
02:48:12,399 --> 02:48:17,000
reading the value into a string name and
4425
02:48:14,319 --> 02:48:18,960
then we simply output it to the screen
4426
02:48:17,000 --> 02:48:21,080
if we run the code and just press enter
4427
02:48:18,960 --> 02:48:22,399
then it says your name is blank and we
4428
02:48:21,080 --> 02:48:24,040
don't really want that because we want
4429
02:48:22,399 --> 02:48:26,479
to check if your name is equal to
4430
02:48:24,040 --> 02:48:29,200
something before we continue so let's
4431
02:48:26,479 --> 02:48:31,920
look at some methods so we have if the
4432
02:48:29,200 --> 02:48:34,640
name is not equal to nothing and then
4433
02:48:31,920 --> 02:48:37,040
let's just print zero to the screen and
4434
02:48:34,640 --> 02:48:42,000
that's one of the methods and we can say
4435
02:48:37,040 --> 02:48:44,880
if name is not equals to quotation marks
4436
02:48:42,000 --> 02:48:46,640
then let's print out number one so what
4437
02:48:44,880 --> 02:48:49,120
this is doing is checking if the name is
4438
02:48:46,640 --> 02:48:51,279
not equal to empty quotes and then this
4439
02:48:49,120 --> 02:48:53,720
is doing exactly the same thing except
4440
02:48:51,279 --> 02:48:56,479
we're using the equals method and let's
4441
02:48:53,720 --> 02:48:58,240
just run the code and press enter and
4442
02:48:56,479 --> 02:49:00,279
you can see the zero or the one doesn't
4443
02:48:58,240 --> 02:49:02,760
appear because it's detecting that it
4444
02:49:00,279 --> 02:49:05,160
contains nothing so if we type in a real
4445
02:49:02,760 --> 02:49:07,359
value we can see Zero and one coming up
4446
02:49:05,160 --> 02:49:10,160
so these are both valid ways of checking
4447
02:49:07,359 --> 02:49:12,319
if the string is not empty so let's look
4448
02:49:10,160 --> 02:49:14,120
at a third one and this one is actually
4449
02:49:12,319 --> 02:49:16,560
very important for reasons I'll explain
4450
02:49:14,120 --> 02:49:20,160
in a second so inside our string Library
4451
02:49:16,560 --> 02:49:22,279
we have is null or empty so we place our
4452
02:49:20,160 --> 02:49:25,040
name inside here and let's see what this
4453
02:49:22,279 --> 02:49:28,760
says now indicates whether the specified
4454
02:49:25,040 --> 02:49:31,000
string is null or at an empty string so
4455
02:49:28,760 --> 02:49:33,640
this can be very effective so what this
4456
02:49:31,000 --> 02:49:36,279
reads now is if the string is null or
4457
02:49:33,640 --> 02:49:38,720
empty then we print a value but we want
4458
02:49:36,279 --> 02:49:41,160
it to be not no lower empty because it
4459
02:49:38,720 --> 02:49:43,120
wants to have some contents so if we
4460
02:49:41,160 --> 02:49:45,120
type a value in again you'll see now we
4461
02:49:43,120 --> 02:49:47,080
get all three numbers and what them
4462
02:49:45,120 --> 02:49:49,680
three numbers mean is that it's past all
4463
02:49:47,080 --> 02:49:51,760
of these checks and each of the checks
4464
02:49:49,680 --> 02:49:54,399
just ensures that the value has got
4465
02:49:51,760 --> 02:49:56,279
something inside and that's fine but it
4466
02:49:54,399 --> 02:49:59,279
can be slightly problematic for this
4467
02:49:56,279 --> 02:50:02,279
reason using this method and this method
4468
02:49:59,279 --> 02:50:05,080
are fine in some cases except as it says
4469
02:50:02,279 --> 02:50:07,359
here it checks if the string is null and
4470
02:50:05,080 --> 02:50:10,279
null means it doesn't have a value but
4471
02:50:07,359 --> 02:50:12,399
it just has nothing instead of something
4472
02:50:10,279 --> 02:50:15,120
let me demonstrate if we assign the
4473
02:50:12,399 --> 02:50:17,720
value of empty quotation marks this
4474
02:50:15,120 --> 02:50:20,439
actually has a value but the contents is
4475
02:50:17,720 --> 02:50:22,840
empty but it still a string as it comes
4476
02:50:20,439 --> 02:50:24,760
up in here if we assign this the value
4477
02:50:22,840 --> 02:50:26,840
of null then it's just a key word that
4478
02:50:24,760 --> 02:50:29,479
we can use to denote that this literally
4479
02:50:26,840 --> 02:50:31,840
has no contents it's not that it has an
4480
02:50:29,479 --> 02:50:34,239
empty string because an empty string is
4481
02:50:31,840 --> 02:50:36,920
still actually a string whereas a null
4482
02:50:34,239 --> 02:50:39,600
it just means the variable is blank now
4483
02:50:36,920 --> 02:50:41,479
this is where the issue arises if we try
4484
02:50:39,600 --> 02:50:43,319
and call a method on something that's
4485
02:50:41,479 --> 02:50:45,760
null Watch What
4486
02:50:43,319 --> 02:50:48,399
Happens we actually get an exception and
4487
02:50:45,760 --> 02:50:50,239
our program just crushes you can't run
4488
02:50:48,399 --> 02:50:52,560
any any code on something if that
4489
02:50:50,239 --> 02:50:55,160
variable is actually defined as
4490
02:50:52,560 --> 02:50:57,200
null it doesn't mind if you check the
4491
02:50:55,160 --> 02:50:59,479
value of it but if you try and run a
4492
02:50:57,200 --> 02:51:02,680
function then you've got a problem so if
4493
02:50:59,479 --> 02:51:05,680
we comment this line out this ensures
4494
02:51:02,680 --> 02:51:07,880
everything it ensures it's null or it's
4495
02:51:05,680 --> 02:51:09,960
empty and this is the belt and braces
4496
02:51:07,880 --> 02:51:11,439
option because if it's null we don't
4497
02:51:09,960 --> 02:51:14,359
actually get an exception when we try
4498
02:51:11,439 --> 02:51:16,840
and run this code as
4499
02:51:14,359 --> 02:51:19,040
follows we get the zero showing up
4500
02:51:16,840 --> 02:51:20,920
because our variable has something
4501
02:51:19,040 --> 02:51:23,319
that's not speech marks and it's
4502
02:51:20,920 --> 02:51:25,399
actually null so we want to avoid this
4503
02:51:23,319 --> 02:51:27,479
option as well and also avoid this
4504
02:51:25,399 --> 02:51:28,760
option in case our string is actually
4505
02:51:27,479 --> 02:51:32,080
genuinely
4506
02:51:28,760 --> 02:51:34,560
null using is null or empty we check
4507
02:51:32,080 --> 02:51:37,319
both for the value of null which is
4508
02:51:34,560 --> 02:51:40,040
right now and if it's got empty quotes
4509
02:51:37,319 --> 02:51:42,080
EG the value is empty so if we just run
4510
02:51:40,040 --> 02:51:44,960
this again and have these two paths
4511
02:51:42,080 --> 02:51:47,160
commented out we can see that two is not
4512
02:51:44,960 --> 02:51:50,680
appearing if we change this back to our
4513
02:51:47,160 --> 02:51:52,680
console read line and we try and do it
4514
02:51:50,680 --> 02:51:55,080
then we get our number two because it's
4515
02:51:52,680 --> 02:51:57,319
validated that this string variable has
4516
02:51:55,080 --> 02:52:00,279
got something inside it and the variable
4517
02:51:57,319 --> 02:52:02,279
is not actually no this is a very good
4518
02:52:00,279 --> 02:52:04,640
way of being able to make sure that your
4519
02:52:02,279 --> 02:52:07,120
program doesn't get exceptions as with
4520
02:52:04,640 --> 02:52:10,720
calling these equals if you want to call
4521
02:52:07,120 --> 02:52:13,160
equals then call it inside here you can
4522
02:52:10,720 --> 02:52:15,200
check if that variable equals something
4523
02:52:13,160 --> 02:52:17,880
by simply placing that if statement in
4524
02:52:15,200 --> 02:52:20,200
inside here after you validated if the
4525
02:52:17,880 --> 02:52:23,319
name variable able actually has some
4526
02:52:20,200 --> 02:52:25,479
contents if it's null or it's empty
4527
02:52:23,319 --> 02:52:27,479
running equals on it will just break the
4528
02:52:25,479 --> 02:52:29,439
code further and you don't need to check
4529
02:52:27,479 --> 02:52:31,160
if it's equal to Something in here maybe
4530
02:52:29,439 --> 02:52:33,920
you were checking if the value is equal
4531
02:52:31,160 --> 02:52:36,080
to abber or something along them lines
4532
02:52:33,920 --> 02:52:37,319
and now if we run it again we actually
4533
02:52:36,080 --> 02:52:40,160
get the output
4534
02:52:37,319 --> 02:52:42,279
correct it's best to Nest them like this
4535
02:52:40,160 --> 02:52:44,120
because if you run this function on
4536
02:52:42,279 --> 02:52:46,880
something that's null it's actually
4537
02:52:44,120 --> 02:52:48,600
really safe but as you saw previously if
4538
02:52:46,880 --> 02:52:50,200
you run a function on something that's
4539
02:52:48,600 --> 02:52:51,960
null then you're going to get a null
4540
02:52:50,200 --> 02:52:54,600
reference exception and your program
4541
02:52:51,960 --> 02:52:57,200
will close down and break you want to
4542
02:52:54,600 --> 02:52:59,960
avoid exceptions at all cost so nesting
4543
02:52:57,200 --> 02:53:02,239
it inside a is null or Remy check will
4544
02:52:59,960 --> 02:53:04,359
be perfect for your code it improves the
4545
02:53:02,239 --> 02:53:07,040
stab ability of your program to ensure
4546
02:53:04,359 --> 02:53:09,640
that exceptions don't
4547
02:53:07,040 --> 02:53:11,800
happen so in this exercise I want you to
4548
02:53:09,640 --> 02:53:13,479
ask the user to input any message and
4549
02:53:11,800 --> 02:53:15,760
then I want you to print the message in
4550
02:53:13,479 --> 02:53:17,880
Order character by character and then
4551
02:53:15,760 --> 02:53:20,600
also print it in reverse character by
4552
02:53:17,880 --> 02:53:22,319
character let's get started so we could
4553
02:53:20,600 --> 02:53:24,479
just have a console right line but let's
4554
02:53:22,319 --> 02:53:26,880
change it to a right and just say enter
4555
02:53:24,479 --> 02:53:28,920
your message call on and then speech
4556
02:53:26,880 --> 02:53:30,960
marks and then just have a string
4557
02:53:28,920 --> 02:53:34,960
message
4558
02:53:30,960 --> 02:53:36,200
equals console. readline perfect so in
4559
02:53:34,960 --> 02:53:38,000
order to be able to Loop through the
4560
02:53:36,200 --> 02:53:39,880
string message we need to have a follow
4561
02:53:38,000 --> 02:53:42,160
Loop if you remember from our previous
4562
02:53:39,880 --> 02:53:44,600
videos we can have this string variable
4563
02:53:42,160 --> 02:53:46,680
followed by a square bracket and then we
4564
02:53:44,600 --> 02:53:48,880
can have an index inside and that will
4565
02:53:46,680 --> 02:53:52,479
print us back any sort of index based on
4566
02:53:48,880 --> 02:53:55,359
that position so if we just have this is
4567
02:53:52,479 --> 02:53:57,600
a test then it's prob print as the T
4568
02:53:55,359 --> 02:53:59,880
because that's the first index and so on
4569
02:53:57,600 --> 02:54:02,080
and so forth so what we can do with this
4570
02:53:59,880 --> 02:54:04,560
information is take what we can do with
4571
02:54:02,080 --> 02:54:07,279
the string index and then also use the
4572
02:54:04,560 --> 02:54:09,720
property which is do length and then use
4573
02:54:07,279 --> 02:54:12,000
these two values to be able to process
4574
02:54:09,720 --> 02:54:14,479
this is a test so we have the first
4575
02:54:12,000 --> 02:54:17,680
character Square braet 0 and the last
4576
02:54:14,479 --> 02:54:20,160
character is at length 14 so our message
4577
02:54:17,680 --> 02:54:21,720
variable now contains 14 characters
4578
02:54:20,160 --> 02:54:23,439
obviously When I close the program then
4579
02:54:21,720 --> 02:54:25,600
that value is lost and I have to reenter
4580
02:54:23,439 --> 02:54:27,600
it but because of that exact reason we
4581
02:54:25,600 --> 02:54:29,640
would always use do length because we
4582
02:54:27,600 --> 02:54:31,880
never know how long the users input is
4583
02:54:29,640 --> 02:54:35,000
going to be so we can have a for Loop so
4584
02:54:31,880 --> 02:54:37,439
we can say 4 into I equal 0 I is less
4585
02:54:35,000 --> 02:54:39,439
than and we can use the message. length
4586
02:54:37,439 --> 02:54:42,239
here and then we can say I
4587
02:54:39,439 --> 02:54:45,239
++ so we want to start at zero because
4588
02:54:42,239 --> 02:54:47,359
our first index position is indeed zero
4589
02:54:45,239 --> 02:54:49,239
and we want to go from the index zero
4590
02:54:47,359 --> 02:54:51,080
all the way to the length
4591
02:54:49,239 --> 02:54:53,640
now in that example I just printed out
4592
02:54:51,080 --> 02:54:56,080
14 for the length but you got to realize
4593
02:54:53,640 --> 02:54:59,120
is there might be 14 positions but
4594
02:54:56,080 --> 02:55:02,200
there's only 13 indexes so it's not
4595
02:54:59,120 --> 02:55:04,600
actually 13 index it's just up to 13
4596
02:55:02,200 --> 02:55:07,840
when you start at zero going from 0 to
4597
02:55:04,600 --> 02:55:10,600
13 gives you 14 positions so when we're
4598
02:55:07,840 --> 02:55:12,680
saying 0 is less than the message length
4599
02:55:10,600 --> 02:55:16,160
then that'll actually take us from 0 to
4600
02:55:12,680 --> 02:55:18,080
13 instead of 0 to 14 if we made this
4601
02:55:16,160 --> 02:55:20,040
greater than or equal to and change this
4602
02:55:18,080 --> 02:55:22,760
to to one for example then that's going
4603
02:55:20,040 --> 02:55:24,680
to go from 1 to 14 and first we're going
4604
02:55:22,760 --> 02:55:26,640
to miss out the first character and
4605
02:55:24,680 --> 02:55:28,720
we're actually going to go one over the
4606
02:55:26,640 --> 02:55:30,319
message length so you always want to
4607
02:55:28,720 --> 02:55:32,800
start from zero if we're going through
4608
02:55:30,319 --> 02:55:35,479
the entire string so this will go from 0
4609
02:55:32,800 --> 02:55:37,319
to 13 and then here we can just have a
4610
02:55:35,479 --> 02:55:39,760
console right line and just print
4611
02:55:37,319 --> 02:55:41,279
message Square braet I and then just for
4612
02:55:39,760 --> 02:55:42,960
the time being we can comment out these
4613
02:55:41,279 --> 02:55:44,600
lines just so it doesn't saturate the
4614
02:55:42,960 --> 02:55:45,680
output so let's run this code and see
4615
02:55:44,600 --> 02:55:48,960
what
4616
02:55:45,680 --> 02:55:51,080
happens this is a test and then we get
4617
02:55:48,960 --> 02:55:53,279
them all on separate lines maybe we can
4618
02:55:51,080 --> 02:55:55,040
just use a console. right instead so we
4619
02:55:53,279 --> 02:55:56,760
get each character on the same line
4620
02:55:55,040 --> 02:55:59,080
instead of a new line character being
4621
02:55:56,760 --> 02:56:01,479
outputed at the end of each of the lines
4622
02:55:59,080 --> 02:56:03,040
so we can say this as a test again and
4623
02:56:01,479 --> 02:56:05,160
there we go we get this as a test
4624
02:56:03,040 --> 02:56:06,840
outputed as well so now that we've
4625
02:56:05,160 --> 02:56:09,600
printed that in order let's see how we
4626
02:56:06,840 --> 02:56:11,439
can print it in reverse so just a quick
4627
02:56:09,600 --> 02:56:13,560
little tip throughout this course I've
4628
02:56:11,439 --> 02:56:15,560
been quite hard on you I've made you Mar
4629
02:56:13,560 --> 02:56:18,000
the for Loop out exactly the way it
4630
02:56:15,560 --> 02:56:20,080
should be but all done manually this is
4631
02:56:18,000 --> 02:56:21,560
so you can get used to the syntax and
4632
02:56:20,080 --> 02:56:23,600
after a while once you understand
4633
02:56:21,560 --> 02:56:25,920
exactly what to do then what you can do
4634
02:56:23,600 --> 02:56:27,960
is when you type in the fall you can see
4635
02:56:25,920 --> 02:56:30,120
that it says as a force snippet tab
4636
02:56:27,960 --> 02:56:32,239
twice to insert the force snippet and we
4637
02:56:30,120 --> 02:56:34,000
can do that now and it actually gives
4638
02:56:32,239 --> 02:56:35,279
you a little bit of an output you can
4639
02:56:34,000 --> 02:56:36,840
see these sections that have been
4640
02:56:35,279 --> 02:56:38,560
highlighted the reason they're
4641
02:56:36,840 --> 02:56:40,680
highlighted is once you're done with one
4642
02:56:38,560 --> 02:56:42,680
section you can actually press Tab and
4643
02:56:40,680 --> 02:56:44,800
it'll jump to the next one so now my
4644
02:56:42,680 --> 02:56:47,399
cursor is jumped on the length and we
4645
02:56:44,800 --> 02:56:49,560
can type in message. length if I press
4646
02:56:47,399 --> 02:56:51,359
tab again then it'll just keep cycling
4647
02:56:49,560 --> 02:56:54,000
through them until we're done and we can
4648
02:56:51,359 --> 02:56:55,960
press Escape upon pressing escape then
4649
02:56:54,000 --> 02:56:58,080
it gets out of the little message and
4650
02:56:55,960 --> 02:57:00,279
there we go we've now just defined a
4651
02:56:58,080 --> 02:57:02,040
really quick for Loop please only use
4652
02:57:00,279 --> 02:57:04,439
this once you're comfortable writing for
4653
02:57:02,040 --> 02:57:06,120
loops and don't use them immediately
4654
02:57:04,439 --> 02:57:08,359
because you might get stuck on what to
4655
02:57:06,120 --> 02:57:10,000
do and how to do it so it's best to
4656
02:57:08,359 --> 02:57:11,800
always write the for Loop syntax out
4657
02:57:10,000 --> 02:57:13,920
from scratch so you can get used to the
4658
02:57:11,800 --> 02:57:15,960
three different sections so this is
4659
02:57:13,920 --> 02:57:18,760
actually a full loop that goes forwards
4660
02:57:15,960 --> 02:57:21,600
it's going from 0 to 13 but what we
4661
02:57:18,760 --> 02:57:23,960
actually want to do now is go from 13 to
4662
02:57:21,600 --> 02:57:25,920
zero so let's actually change this for
4663
02:57:23,960 --> 02:57:28,239
Loop completely and we can do our little
4664
02:57:25,920 --> 02:57:29,880
shocket again so if we do four and then
4665
02:57:28,239 --> 02:57:32,080
we don't actually want to start on the
4666
02:57:29,880 --> 02:57:34,479
value of zero we want to start on
4667
02:57:32,080 --> 02:57:37,200
message. length and then you want a
4668
02:57:34,479 --> 02:57:39,120
minus one and just as we said here we
4669
02:57:37,200 --> 02:57:41,880
never actually want to get to the length
4670
02:57:39,120 --> 02:57:43,760
because we want to go from 0 to 13 so if
4671
02:57:41,880 --> 02:57:46,479
we want to go backwards we want to go
4672
02:57:43,760 --> 02:57:49,080
from 13 to zero and the only way that we
4673
02:57:46,479 --> 02:57:51,040
can get this 13 is by getting the 14
4674
02:57:49,080 --> 02:57:53,439
value which is message. length and
4675
02:57:51,040 --> 02:57:55,560
simply minusing one so now that we've
4676
02:57:53,439 --> 02:57:57,840
got our first variable which is in I
4677
02:57:55,560 --> 02:58:00,840
equals message length minus one which
4678
02:57:57,840 --> 02:58:03,040
will give us the 13 we want to go from I
4679
02:58:00,840 --> 02:58:05,200
and instead of less than it's going to
4680
02:58:03,040 --> 02:58:07,200
be greater than or equal to and then we
4681
02:58:05,200 --> 02:58:08,880
actually want to change it to a zero
4682
02:58:07,200 --> 02:58:10,840
because we actually want to stop at zero
4683
02:58:08,880 --> 02:58:14,160
in this case and then we don't want to
4684
02:58:10,840 --> 02:58:16,640
use I ++ because if we start at 13 we
4685
02:58:14,160 --> 02:58:19,120
want to go down in one instead of up in
4686
02:58:16,640 --> 02:58:21,680
one so we can simply just just do minus
4687
02:58:19,120 --> 02:58:23,160
minus we can take this exact line and
4688
02:58:21,680 --> 02:58:25,560
print out to the
4689
02:58:23,160 --> 02:58:28,279
screen and if we just print something
4690
02:58:25,560 --> 02:58:30,200
small in between like a hyphen just to
4691
02:58:28,279 --> 02:58:31,239
separate the two outputs and let's see
4692
02:58:30,200 --> 02:58:35,439
what
4693
02:58:31,239 --> 02:58:39,960
happens test there we go so we have test
4694
02:58:35,439 --> 02:58:42,080
and then t s e and t is now in Reverse
4695
02:58:39,960 --> 02:58:44,479
but now you might be wondering if we had
4696
02:58:42,080 --> 02:58:47,040
a shortcut for the four what's this four
4697
02:58:44,479 --> 02:58:49,720
with an additional R cords n it for a
4698
02:58:47,040 --> 02:58:51,479
reverse for Loop so if we tap tab twice
4699
02:58:49,720 --> 02:58:54,960
you can see that we have exactly the
4700
02:58:51,479 --> 02:58:57,200
same format inti equals something minus
4701
02:58:54,960 --> 02:58:59,640
one which we have right here and then we
4702
02:58:57,200 --> 02:59:01,720
have I is greater than or equal to zero
4703
02:58:59,640 --> 02:59:04,479
as is we have here and then we have
4704
02:59:01,720 --> 02:59:08,120
iusus just like here so we can literally
4705
02:59:04,479 --> 02:59:10,359
just change this into message do length
4706
02:59:08,120 --> 02:59:12,840
and then these two now are exactly the
4707
02:59:10,359 --> 02:59:14,880
same so just copying this down we'll get
4708
02:59:12,840 --> 02:59:16,399
us exactly what we need but obviously we
4709
02:59:14,880 --> 02:59:18,479
just wrote this I just wanted to show
4710
02:59:16,399 --> 02:59:21,239
you the shortcut so let's try it with a
4711
02:59:18,479 --> 02:59:23,000
longer string this is a test there you
4712
02:59:21,239 --> 02:59:24,960
go and if you wanted to you could just
4713
02:59:23,000 --> 02:59:27,160
print these on separate lines so you can
4714
02:59:24,960 --> 02:59:29,520
just change this into a right line for
4715
02:59:27,160 --> 02:59:31,960
example and have it like that it doesn't
4716
02:59:29,520 --> 02:59:33,720
really matter the rest is up to you but
4717
02:59:31,960 --> 02:59:35,960
the task is just so you can print it one
4718
02:59:33,720 --> 02:59:38,000
way and then print it the other way I
4719
02:59:35,960 --> 02:59:40,560
realized that what you can also do is
4720
02:59:38,000 --> 02:59:42,399
just simply print a console right line
4721
02:59:40,560 --> 02:59:44,720
and have message in there and just get
4722
02:59:42,399 --> 02:59:47,200
rid of this completely and the output
4723
02:59:44,720 --> 02:59:49,000
will still look exactly the same there
4724
02:59:47,200 --> 02:59:51,160
you go and and the reason why I told you
4725
02:59:49,000 --> 02:59:53,319
to write the for Loop is not because
4726
02:59:51,160 --> 02:59:55,600
it'll be easier if you do this it's more
4727
02:59:53,319 --> 02:59:57,800
for your Educational Learning purposes
4728
02:59:55,600 --> 03:00:00,040
having more practice to how to iterate a
4729
02:59:57,800 --> 03:00:02,279
string variable is very helpful for the
4730
03:00:00,040 --> 03:00:04,239
future and you can understand it fully
4731
03:00:02,279 --> 03:00:05,760
if you just print out to the screen then
4732
03:00:04,239 --> 03:00:07,200
you're not learning anything extra
4733
03:00:05,760 --> 03:00:09,680
you're just applying what you already
4734
03:00:07,200 --> 03:00:11,720
know so let's just have a quick recap we
4735
03:00:09,680 --> 03:00:13,640
ask the user to enter the message we STW
4736
03:00:11,720 --> 03:00:14,960
it into a string variable and then I
4737
03:00:13,640 --> 03:00:16,520
demonstrated that you could use the
4738
03:00:14,960 --> 03:00:18,680
square brackets to Output the first
4739
03:00:16,520 --> 03:00:20,920
index then you can output the length of
4740
03:00:18,680 --> 03:00:23,200
the entire variable using a follow Lo we
4741
03:00:20,920 --> 03:00:26,000
can go from zero to message length which
4742
03:00:23,200 --> 03:00:28,200
will give us from indexes 0 up until 13
4743
03:00:26,000 --> 03:00:31,000
in this case and then we can go through
4744
03:00:28,200 --> 03:00:32,680
i++ increment in one and then we can
4745
03:00:31,000 --> 03:00:35,120
have a console right which will just
4746
03:00:32,680 --> 03:00:36,880
print out message index I for the index
4747
03:00:35,120 --> 03:00:38,760
that we're currently up to and then we
4748
03:00:36,880 --> 03:00:40,600
break it off with a console right line
4749
03:00:38,760 --> 03:00:42,479
just in the middle because these are
4750
03:00:40,600 --> 03:00:44,840
printing on the same line so we need to
4751
03:00:42,479 --> 03:00:46,680
insert a new line character at the end
4752
03:00:44,840 --> 03:00:49,000
after we've done that what we can do now
4753
03:00:46,680 --> 03:00:51,279
is print it in reverse and now we can do
4754
03:00:49,000 --> 03:00:54,439
exactly the same thing except from 0 to
4755
03:00:51,279 --> 03:00:56,560
13 we go from 13 to 0 so we start at the
4756
03:00:54,439 --> 03:00:58,640
message length minus one because the
4757
03:00:56,560 --> 03:01:00,840
length was 14 and now we minus one to
4758
03:00:58,640 --> 03:01:02,600
get to 13 and we're going from I is
4759
03:01:00,840 --> 03:01:04,479
greater than or equal to zero because
4760
03:01:02,600 --> 03:01:07,279
that's the value that we want to stop at
4761
03:01:04,479 --> 03:01:10,040
and then we can do IUS minus if we were
4762
03:01:07,279 --> 03:01:12,359
to take out this equals operator let's
4763
03:01:10,040 --> 03:01:14,560
see what happens test and now we're
4764
03:01:12,359 --> 03:01:16,000
actually missing the last character
4765
03:01:14,560 --> 03:01:18,840
because what we've done is we've
4766
03:01:16,000 --> 03:01:21,200
actually went from 13 to 1 instead of 13
4767
03:01:18,840 --> 03:01:24,040
to zero so let's put that back in and
4768
03:01:21,200 --> 03:01:26,880
make sure it works and then we have t
4769
03:01:24,040 --> 03:01:29,160
set for test and then we're outputting
4770
03:01:26,880 --> 03:01:30,640
the exact same thing in both for Loops
4771
03:01:29,160 --> 03:01:32,760
because we're simply just printing the
4772
03:01:30,640 --> 03:01:34,880
character that we're currently up to and
4773
03:01:32,760 --> 03:01:36,760
because this for Loop starts at zero and
4774
03:01:34,880 --> 03:01:39,479
this for Loop starts at the length minus
4775
03:01:36,760 --> 03:01:41,439
one then they output in the same values
4776
03:01:39,479 --> 03:01:44,760
or what it appears to be the same values
4777
03:01:41,439 --> 03:01:44,760
it's just starting at different
4778
03:01:44,920 --> 03:01:48,720
indexes in this exercise we're going to
4779
03:01:47,040 --> 03:01:50,600
be building some sort of a password
4780
03:01:48,720 --> 03:01:52,560
validor these can be present in
4781
03:01:50,600 --> 03:01:54,560
something like a register form where you
4782
03:01:52,560 --> 03:01:56,080
ask the user to enter their password and
4783
03:01:54,560 --> 03:01:57,960
then enter it again to make sure that
4784
03:01:56,080 --> 03:01:59,680
they've entered it correctly you want to
4785
03:01:57,960 --> 03:02:01,800
ask user to enter their password in
4786
03:01:59,680 --> 03:02:03,960
twice andall them both check if they
4787
03:02:01,800 --> 03:02:05,960
both contain something if they do we can
4788
03:02:03,960 --> 03:02:08,000
print out password match if they don't
4789
03:02:05,960 --> 03:02:09,920
passwords do not match and if they both
4790
03:02:08,000 --> 03:02:12,000
contain nothing then you want to Output
4791
03:02:09,920 --> 03:02:14,920
please enter a password so let's get
4792
03:02:12,000 --> 03:02:19,120
started first we can have a console
4793
03:02:14,920 --> 03:02:23,040
console right we can have enter password
4794
03:02:19,120 --> 03:02:25,200
string password equals console.
4795
03:02:23,040 --> 03:02:28,200
readline and then we can just copy these
4796
03:02:25,200 --> 03:02:30,160
two lines down using contrl D and then
4797
03:02:28,200 --> 03:02:32,160
we can have a password C for password
4798
03:02:30,160 --> 03:02:34,160
confirmation and we can say enter
4799
03:02:32,160 --> 03:02:35,640
password again and let just print these
4800
03:02:34,160 --> 03:02:38,800
out to the screen just to make sure we
4801
03:02:35,640 --> 03:02:40,920
know what's going on just like so so we
4802
03:02:38,800 --> 03:02:42,319
can have abber and then abber again so
4803
03:02:40,920 --> 03:02:43,319
now we have both abber started and
4804
03:02:42,319 --> 03:02:45,920
that's
4805
03:02:43,319 --> 03:02:47,439
fine so let's see what we can do now we
4806
03:02:45,920 --> 03:02:49,359
need to check if they both contain
4807
03:02:47,439 --> 03:02:51,120
something so what are the different ways
4808
03:02:49,359 --> 03:02:53,880
that we can do this so we can have if
4809
03:02:51,120 --> 03:02:56,479
password do equals and then we can make
4810
03:02:53,880 --> 03:02:58,479
it equals to string. empty and because
4811
03:02:56,479 --> 03:03:00,239
we want to check if it's not empty then
4812
03:02:58,479 --> 03:03:02,319
we can simply place the not operator
4813
03:03:00,239 --> 03:03:04,800
right at the start and then we can join
4814
03:03:02,319 --> 03:03:07,600
them with another one not password.
4815
03:03:04,800 --> 03:03:09,840
equals and then exactly the same thing
4816
03:03:07,600 --> 03:03:11,399
string. empty but in this case we don't
4817
03:03:09,840 --> 03:03:14,600
want to use password we want to use
4818
03:03:11,399 --> 03:03:16,479
password see there we go so if we've
4819
03:03:14,600 --> 03:03:18,479
came into here then that means they must
4820
03:03:16,479 --> 03:03:20,760
have entered something so let's just
4821
03:03:18,479 --> 03:03:23,160
quickly do our exit path which is if
4822
03:03:20,760 --> 03:03:25,600
they're empty please enter a password so
4823
03:03:23,160 --> 03:03:27,239
we can say else and we don't need a
4824
03:03:25,600 --> 03:03:29,279
condition here because if this were to
4825
03:03:27,239 --> 03:03:31,160
fail then it has to mean that their
4826
03:03:29,279 --> 03:03:33,439
password's empty so we can have a
4827
03:03:31,160 --> 03:03:35,680
console. right line and we can just do
4828
03:03:33,439 --> 03:03:37,800
please enter a
4829
03:03:35,680 --> 03:03:40,120
password and let's just test if this
4830
03:03:37,800 --> 03:03:43,040
path works first and we just press enter
4831
03:03:40,120 --> 03:03:44,680
twice and we get please enter a password
4832
03:03:43,040 --> 03:03:46,840
and maybe if we type something in for
4833
03:03:44,680 --> 03:03:50,000
both and then we get nothing because
4834
03:03:46,840 --> 03:03:51,399
it's actually came here now perfect so
4835
03:03:50,000 --> 03:03:54,960
if we come through and we can say
4836
03:03:51,399 --> 03:03:56,880
password equals equals password C or we
4837
03:03:54,960 --> 03:04:00,319
could use the equals function and we can
4838
03:03:56,880 --> 03:04:02,239
say password. equals and then password C
4839
03:04:00,319 --> 03:04:05,800
and if they both equal to each other
4840
03:04:02,239 --> 03:04:09,160
then we can print out passwords
4841
03:04:05,800 --> 03:04:12,399
match there we go and if that were to
4842
03:04:09,160 --> 03:04:14,000
fail then we can print out passwords do
4843
03:04:12,399 --> 03:04:16,359
not
4844
03:04:14,000 --> 03:04:19,000
match here we go let's run that let's
4845
03:04:16,359 --> 03:04:21,760
find out Aba and abber and we get
4846
03:04:19,000 --> 03:04:23,880
passwords match and if we try abber and
4847
03:04:21,760 --> 03:04:26,160
abber with two A's and then we get
4848
03:04:23,880 --> 03:04:28,000
passwords do not match so what are some
4849
03:04:26,160 --> 03:04:30,520
other ways that we can do this you can
4850
03:04:28,000 --> 03:04:32,399
structure this completely differently
4851
03:04:30,520 --> 03:04:34,000
right now we're doing both checks in one
4852
03:04:32,399 --> 03:04:36,160
single if statement and we can actually
4853
03:04:34,000 --> 03:04:38,319
split this up into two and then provide
4854
03:04:36,160 --> 03:04:40,760
a different error message so right now
4855
03:04:38,319 --> 03:04:42,399
this is please enter a password you
4856
03:04:40,760 --> 03:04:44,479
might have entered the second one but
4857
03:04:42,399 --> 03:04:46,439
not have ented the first one or vice
4858
03:04:44,479 --> 03:04:48,479
versa but it doesn't matter because you
4859
03:04:46,439 --> 03:04:50,560
have to enter both of them in order to
4860
03:04:48,479 --> 03:04:52,880
pass this validation check so what you
4861
03:04:50,560 --> 03:04:55,920
could do instead is actually put another
4862
03:04:52,880 --> 03:04:58,560
if statement inside and paste this one
4863
03:04:55,920 --> 03:05:00,720
inside here and then just keep these as
4864
03:04:58,560 --> 03:05:02,279
two separate if statements and then you
4865
03:05:00,720 --> 03:05:05,000
can actually provide different error
4866
03:05:02,279 --> 03:05:08,560
messages so let's Nest this in one
4867
03:05:05,000 --> 03:05:10,840
deeper so we can say if the password is
4868
03:05:08,560 --> 03:05:12,800
not equal to empty and if this returns
4869
03:05:10,840 --> 03:05:14,760
back true then this variable has
4870
03:05:12,800 --> 03:05:16,800
something inside it and then once we go
4871
03:05:14,760 --> 03:05:19,319
inside these curly braces we get another
4872
03:05:16,800 --> 03:05:21,200
check and if the password C is not equal
4873
03:05:19,319 --> 03:05:23,120
to nothing then that means if we've came
4874
03:05:21,200 --> 03:05:24,680
in here then we pass both of these
4875
03:05:23,120 --> 03:05:26,479
validation checks and both of them
4876
03:05:24,680 --> 03:05:28,720
contain something and then we can
4877
03:05:26,479 --> 03:05:31,000
proceed to check the variables so what
4878
03:05:28,720 --> 03:05:33,200
you can do in the first L statement is
4879
03:05:31,000 --> 03:05:35,000
you can say please enter a password but
4880
03:05:33,200 --> 03:05:37,399
on the second one so just after this
4881
03:05:35,000 --> 03:05:41,359
curly brace we can have another L
4882
03:05:37,399 --> 03:05:43,279
statement or we can say please enter a
4883
03:05:41,359 --> 03:05:45,239
password
4884
03:05:43,279 --> 03:05:47,160
confirmation so I know that was a bit
4885
03:05:45,239 --> 03:05:48,680
quick so let's just have a quick recap
4886
03:05:47,160 --> 03:05:50,560
so we can have have one if statement
4887
03:05:48,680 --> 03:05:53,040
that processes the first password that
4888
03:05:50,560 --> 03:05:55,239
you type in and if this is empty then it
4889
03:05:53,040 --> 03:05:56,720
says please enter a password and if it's
4890
03:05:55,239 --> 03:05:59,439
not empty then it'll go inside this
4891
03:05:56,720 --> 03:06:01,520
curly brace hit the first line and check
4892
03:05:59,439 --> 03:06:04,000
if the password confirmation is not
4893
03:06:01,520 --> 03:06:06,520
empty if it is empty then it'll say
4894
03:06:04,000 --> 03:06:08,560
please enter a password confirmation if
4895
03:06:06,520 --> 03:06:10,720
both of these return back true then it
4896
03:06:08,560 --> 03:06:12,520
will go inside the success path in which
4897
03:06:10,720 --> 03:06:14,920
it will actually check if password is
4898
03:06:12,520 --> 03:06:16,960
equal to the password confirmation and
4899
03:06:14,920 --> 03:06:18,640
we can say the passwords match or if
4900
03:06:16,960 --> 03:06:21,040
they don't equal to each other we can
4901
03:06:18,640 --> 03:06:23,080
say the passwords do not match let's run
4902
03:06:21,040 --> 03:06:24,880
that and see what we get so if we type
4903
03:06:23,080 --> 03:06:26,840
something in for the second one but not
4904
03:06:24,880 --> 03:06:28,640
the first one so for the first one we
4905
03:06:26,840 --> 03:06:30,399
just press enter and then let's type
4906
03:06:28,640 --> 03:06:32,640
something in for the second one and it
4907
03:06:30,399 --> 03:06:34,840
says please enter a password and then if
4908
03:06:32,640 --> 03:06:37,160
we run that and do the complete opposite
4909
03:06:34,840 --> 03:06:38,960
type in p and then press enter for the
4910
03:06:37,160 --> 03:06:40,920
second one then it says please enter a
4911
03:06:38,960 --> 03:06:42,800
password confirmation so you can see
4912
03:06:40,920 --> 03:06:44,680
immediately what we can do is provide a
4913
03:06:42,800 --> 03:06:47,160
different error message for different
4914
03:06:44,680 --> 03:06:48,120
situations if you include these both in
4915
03:06:47,160 --> 03:06:49,600
one
4916
03:06:48,120 --> 03:06:51,600
then you can only give out more of a
4917
03:06:49,600 --> 03:06:53,680
generic error message this is similar
4918
03:06:51,600 --> 03:06:55,960
for when you go to login and when you
4919
03:06:53,680 --> 03:06:57,600
get your password or email wrong it
4920
03:06:55,960 --> 03:06:59,560
doesn't say which one you've got wrong
4921
03:06:57,600 --> 03:07:01,399
it just says them generically which one
4922
03:06:59,560 --> 03:07:04,200
you've got wrong it says invalid
4923
03:07:01,399 --> 03:07:06,200
credentials or invalid username or email
4924
03:07:04,200 --> 03:07:08,279
it'll say them generically like invalid
4925
03:07:06,200 --> 03:07:10,239
username or password but it won't tell
4926
03:07:08,279 --> 03:07:11,560
you which one it is because hackers can
4927
03:07:10,239 --> 03:07:13,760
exploit this and you don't want to be
4928
03:07:11,560 --> 03:07:15,520
doing this but in the case of a password
4929
03:07:13,760 --> 03:07:17,520
confirmation and this is just for a
4930
03:07:15,520 --> 03:07:19,439
register form then it's not exactly the
4931
03:07:17,520 --> 03:07:20,800
the worst thing to do this because
4932
03:07:19,439 --> 03:07:22,840
sometimes what you'll see is the text
4933
03:07:20,800 --> 03:07:24,600
boxes might go red to indicate that
4934
03:07:22,840 --> 03:07:26,399
they're both not equal to each other and
4935
03:07:24,600 --> 03:07:28,399
this is fine on registration but it's
4936
03:07:26,399 --> 03:07:30,479
not fine on the login what we can also
4937
03:07:28,399 --> 03:07:32,359
do in here once we've checked if they're
4938
03:07:30,479 --> 03:07:33,920
both not equal to something we can
4939
03:07:32,359 --> 03:07:36,399
actually just validate how long the
4940
03:07:33,920 --> 03:07:38,760
string needs to be we can have if
4941
03:07:36,399 --> 03:07:41,080
password. length and let's say you had a
4942
03:07:38,760 --> 03:07:44,680
restriction and it can only be over six
4943
03:07:41,080 --> 03:07:45,880
characters and we can say six and
4944
03:07:44,680 --> 03:07:48,880
password
4945
03:07:45,880 --> 03:07:50,560
c.length is greater than equal to six
4946
03:07:48,880 --> 03:07:52,439
and once it passes all three of these
4947
03:07:50,560 --> 03:07:54,960
checks then we can check if they're
4948
03:07:52,439 --> 03:07:56,760
equal to each other if they got
4949
03:07:54,960 --> 03:07:59,880
something inside and if they've got
4950
03:07:56,760 --> 03:08:01,840
something inside and if they've got both
4951
03:07:59,880 --> 03:08:04,479
the length is greater than or equal to
4952
03:08:01,840 --> 03:08:06,200
six then we can check it so we can have
4953
03:08:04,479 --> 03:08:08,200
something like password and then
4954
03:08:06,200 --> 03:08:10,640
password and then this returns back with
4955
03:08:08,200 --> 03:08:12,760
password matched if we have something
4956
03:08:10,640 --> 03:08:14,640
like abber then it's actually returns
4957
03:08:12,760 --> 03:08:17,600
back nothing because we don't have an
4958
03:08:14,640 --> 03:08:22,239
else for this specific bracket so we can
4959
03:08:17,600 --> 03:08:25,520
say else conso right line please enter
4960
03:08:22,239 --> 03:08:28,399
six or more
4961
03:08:25,520 --> 03:08:30,319
characters so if we type in Alber twice
4962
03:08:28,399 --> 03:08:32,920
then you can see we get this message now
4963
03:08:30,319 --> 03:08:34,840
please enter six or more characters so
4964
03:08:32,920 --> 03:08:37,479
right now we have a validation for the
4965
03:08:34,840 --> 03:08:39,319
first password and the second password
4966
03:08:37,479 --> 03:08:41,880
and for the length and if all these
4967
03:08:39,319 --> 03:08:43,520
checks go by then you can process the
4968
03:08:41,880 --> 03:08:45,279
actual passwords that they've entered
4969
03:08:43,520 --> 03:08:46,960
and check if they're equal to each other
4970
03:08:45,279 --> 03:08:49,239
you can technically do all of these
4971
03:08:46,960 --> 03:08:51,239
checks in one line it'll just be a
4972
03:08:49,239 --> 03:08:53,359
pretty long line but what that means is
4973
03:08:51,239 --> 03:08:55,160
you can't have three separate error
4974
03:08:53,359 --> 03:08:57,080
messages like this if you have it
4975
03:08:55,160 --> 03:08:58,520
process in one line the only error
4976
03:08:57,080 --> 03:09:00,920
message that you can give is very
4977
03:08:58,520 --> 03:09:03,479
generic there's no one generic message
4978
03:09:00,920 --> 03:09:05,640
that can fit for password empty and the
4979
03:09:03,479 --> 03:09:07,680
confirmation password empty and for the
4980
03:09:05,640 --> 03:09:09,880
length so maybe what you could do is
4981
03:09:07,680 --> 03:09:11,239
chain the both of them to be check if
4982
03:09:09,880 --> 03:09:13,359
they're not empty so then you can
4983
03:09:11,239 --> 03:09:15,640
process a message with something like
4984
03:09:13,359 --> 03:09:16,880
please enter a password and then you can
4985
03:09:15,640 --> 03:09:18,520
have another check for the password
4986
03:09:16,880 --> 03:09:20,600
length just to let them know that they
4987
03:09:18,520 --> 03:09:22,319
need to enter six or more characters
4988
03:09:20,600 --> 03:09:24,239
splitting these off into two separate
4989
03:09:22,319 --> 03:09:25,800
checks is going to be very helpful
4990
03:09:24,239 --> 03:09:27,520
because it'll let the user know
4991
03:09:25,800 --> 03:09:29,479
something a bit better than a generic
4992
03:09:27,520 --> 03:09:31,200
error message so in the previous
4993
03:09:29,479 --> 03:09:32,800
examples throughout the course we've
4994
03:09:31,200 --> 03:09:35,520
actually talked about using the to low
4995
03:09:32,800 --> 03:09:36,920
function and I said in majority of cases
4996
03:09:35,520 --> 03:09:38,439
where you want actually compare two
4997
03:09:36,920 --> 03:09:40,120
different things you want to actually
4998
03:09:38,439 --> 03:09:42,000
use the two lower function or the two
4999
03:09:40,120 --> 03:09:45,800
upper function and you can achieve this
5000
03:09:42,000 --> 03:09:48,239
by having two lower just here and then
5001
03:09:45,800 --> 03:09:49,720
also two lower just here
5002
03:09:48,239 --> 03:09:51,239
now the problem with comparing if
5003
03:09:49,720 --> 03:09:53,560
they're both equal to each other but
5004
03:09:51,239 --> 03:09:55,680
checking the lower gue values is this is
5005
03:09:53,560 --> 03:09:57,840
indeed a password if you're checking if
5006
03:09:55,680 --> 03:10:00,479
the user has inputed any capitals
5007
03:09:57,840 --> 03:10:02,600
lowercase symbols Etc then there's no
5008
03:10:00,479 --> 03:10:04,200
point implying a too low function
5009
03:10:02,600 --> 03:10:06,520
because that will just ruin their actual
5010
03:10:04,200 --> 03:10:08,239
password and reduce the security so in
5011
03:10:06,520 --> 03:10:10,399
certain cases you don't always want to
5012
03:10:08,239 --> 03:10:12,239
use two lower and two upper for
5013
03:10:10,399 --> 03:10:14,720
comparison you want to just check the
5014
03:10:12,239 --> 03:10:16,319
value straight up and the same thing is
5015
03:10:14,720 --> 03:10:18,279
when you're doing a login you don't want
5016
03:10:16,319 --> 03:10:20,359
to be checking the values lowcase
5017
03:10:18,279 --> 03:10:22,160
because that's their login these are
5018
03:10:20,359 --> 03:10:23,560
some of the specific situations where
5019
03:10:22,160 --> 03:10:25,279
you don't want to be using the two low
5020
03:10:23,560 --> 03:10:27,319
function if the data that your
5021
03:10:25,279 --> 03:10:29,520
processing is pretty sensitive and will
5022
03:10:27,319 --> 03:10:31,279
be messed up if you use the to function
5023
03:10:29,520 --> 03:10:33,120
that you should always avoid it
5024
03:10:31,279 --> 03:10:35,040
passwords fit this perfectly and
5025
03:10:33,120 --> 03:10:37,040
usernames as well if someone's added
5026
03:10:35,040 --> 03:10:38,720
some capitals inside the username then
5027
03:10:37,040 --> 03:10:41,560
you don't want to ruin that by doing
5028
03:10:38,720 --> 03:10:44,080
lower case or upper
5029
03:10:41,560 --> 03:10:46,279
case if your username is spelled like
5030
03:10:44,080 --> 03:10:48,239
this and you try and log on like this
5031
03:10:46,279 --> 03:10:49,560
then they're not equal to each other
5032
03:10:48,239 --> 03:10:51,319
because this will probably be two
5033
03:10:49,560 --> 03:10:53,120
different accounts you can have an
5034
03:10:51,319 --> 03:10:54,920
account with a capital B and have
5035
03:10:53,120 --> 03:10:56,439
account with a lowercase b so you don't
5036
03:10:54,920 --> 03:10:58,319
want to be lowercasing the input and
5037
03:10:56,439 --> 03:11:00,200
then doing a comparison because what you
5038
03:10:58,319 --> 03:11:02,120
might actually retrieve is a different
5039
03:11:00,200 --> 03:11:04,520
account than you're looking
5040
03:11:02,120 --> 03:11:06,160
for so now that we talked about stuff
5041
03:11:04,520 --> 03:11:08,279
theoretically let's see how we can do
5042
03:11:06,160 --> 03:11:10,160
things practically so as you heard in
5043
03:11:08,279 --> 03:11:12,520
the last video arrays are a way that we
5044
03:11:10,160 --> 03:11:15,640
can store multiple variables of the same
5045
03:11:12,520 --> 03:11:18,439
data type in one little group so if you
5046
03:11:15,640 --> 03:11:22,399
had something like num one equals 5 in
5047
03:11:18,439 --> 03:11:23,920
number 2 = 10 in number 3 = 15 and we
5048
03:11:22,399 --> 03:11:26,160
had a similar case in one of the
5049
03:11:23,920 --> 03:11:27,720
exercises like this where we had to work
5050
03:11:26,160 --> 03:11:29,960
out if a triangle was actually a
5051
03:11:27,720 --> 03:11:31,520
triangle so we asked the user to input
5052
03:11:29,960 --> 03:11:33,439
three different angles and then we
5053
03:11:31,520 --> 03:11:35,399
stalled them all and then we added them
5054
03:11:33,439 --> 03:11:39,960
up after and it looked something like
5055
03:11:35,399 --> 03:11:42,120
this in total equals num 1 plus num 2
5056
03:11:39,960 --> 03:11:44,120
and then finally plus num3 and then we
5057
03:11:42,120 --> 03:11:45,439
had some sort of if statement after to
5058
03:11:44,120 --> 03:11:48,319
check if the
5059
03:11:45,439 --> 03:11:50,279
total was equal to
5060
03:11:48,319 --> 03:11:52,200
180 and then we did something in the
5061
03:11:50,279 --> 03:11:54,399
curly braces just to Output that it's
5062
03:11:52,200 --> 03:11:56,439
valid and then else output it's
5063
03:11:54,399 --> 03:11:58,120
invalid you can see how this gets a
5064
03:11:56,439 --> 03:12:00,479
little tedious we have to create a new
5065
03:11:58,120 --> 03:12:02,880
variable every single time we want some
5066
03:12:00,479 --> 03:12:04,800
more data to be stored and in this case
5067
03:12:02,880 --> 03:12:06,880
we just simply want another number that
5068
03:12:04,800 --> 03:12:09,120
we can store but we have to just keep a
5069
03:12:06,880 --> 03:12:11,960
pending one to it in that case we could
5070
03:12:09,120 --> 03:12:13,399
have angle one angle two and angle three
5071
03:12:11,960 --> 03:12:15,800
but how about if you had something that
5072
03:12:13,399 --> 03:12:17,600
just called angles for now let's just
5073
03:12:15,800 --> 03:12:19,239
talk about how arrays work how we can
5074
03:12:17,600 --> 03:12:20,800
use them and then after I've went
5075
03:12:19,239 --> 03:12:22,520
through this example about arrays and
5076
03:12:20,800 --> 03:12:24,239
taught you what to do with it then I'll
5077
03:12:22,520 --> 03:12:26,080
will go get the code from the exercise
5078
03:12:24,239 --> 03:12:28,239
from the triangle video and actually
5079
03:12:26,080 --> 03:12:29,800
transform it into using an array so you
5080
03:12:28,239 --> 03:12:32,359
can see what the difference is and how
5081
03:12:29,800 --> 03:12:34,239
the new code will look like so let's get
5082
03:12:32,359 --> 03:12:36,239
rid of this little section so now that
5083
03:12:34,239 --> 03:12:38,840
we've got three integer variables let's
5084
03:12:36,239 --> 03:12:40,960
see how we can do this another way so
5085
03:12:38,840 --> 03:12:43,040
the Syntax for arrays you first type in
5086
03:12:40,960 --> 03:12:44,960
the data type that you want just like
5087
03:12:43,040 --> 03:12:46,760
before and then you add on a square
5088
03:12:44,960 --> 03:12:47,920
bracket and a closed square bracket
5089
03:12:46,760 --> 03:12:50,359
there's not nothing that needs to be
5090
03:12:47,920 --> 03:12:52,279
inside as of right now then you press
5091
03:12:50,359 --> 03:12:54,200
space and type in the variable name as
5092
03:12:52,279 --> 03:12:57,359
per always and then we can make that
5093
03:12:54,200 --> 03:12:59,640
equal to new in three so what this side
5094
03:12:57,359 --> 03:13:02,080
of the equal sign means is we want a new
5095
03:12:59,640 --> 03:13:04,319
instance of an integer array and we want
5096
03:13:02,080 --> 03:13:05,760
to have three positions since we have
5097
03:13:04,319 --> 03:13:07,880
three variables here that we want to
5098
03:13:05,760 --> 03:13:10,160
store we need to have three positions to
5099
03:13:07,880 --> 03:13:12,040
store them so this line in total says
5100
03:13:10,160 --> 03:13:13,520
that we want to integer array we want to
5101
03:13:12,040 --> 03:13:16,000
call it numbers and we want to
5102
03:13:13,520 --> 03:13:18,520
instantiate our new integer array with
5103
03:13:16,000 --> 03:13:21,239
three positions instancia is just the
5104
03:13:18,520 --> 03:13:23,760
term that we use when you turn something
5105
03:13:21,239 --> 03:13:25,800
into a new class at this point if we
5106
03:13:23,760 --> 03:13:28,279
don't use new then we're going to get
5107
03:13:25,800 --> 03:13:29,880
compiler error because it's unsure what
5108
03:13:28,279 --> 03:13:32,560
this is trying to tell you it's trying
5109
03:13:29,880 --> 03:13:34,600
to start an integer inside integer array
5110
03:13:32,560 --> 03:13:36,120
and that makes no sense so it won't
5111
03:13:34,600 --> 03:13:38,000
recognize the square brackets in this
5112
03:13:36,120 --> 03:13:39,560
case because it has no idea what it's
5113
03:13:38,000 --> 03:13:41,399
trying to do and this syntax is
5114
03:13:39,560 --> 03:13:42,760
completely incorrect so the compiler
5115
03:13:41,399 --> 03:13:45,479
doesn't know what error message to give
5116
03:13:42,760 --> 03:13:47,680
you so we can have a new int and what we
5117
03:13:45,479 --> 03:13:49,439
can do now is by us using the square
5118
03:13:47,680 --> 03:13:51,680
bracket reference we can actually
5119
03:13:49,439 --> 03:13:54,800
specify these three variables inside our
5120
03:13:51,680 --> 03:13:58,040
numbers array so now we have access to
5121
03:13:54,800 --> 03:14:00,560
numbers 0 = 5 and then if we use
5122
03:13:58,040 --> 03:14:03,080
controll D to copy this down twice we
5123
03:14:00,560 --> 03:14:06,600
change this to one and two and then
5124
03:14:03,080 --> 03:14:09,359
print 10 here and 15 here so now this is
5125
03:14:06,600 --> 03:14:11,800
exactly the same as this we have three
5126
03:14:09,359 --> 03:14:14,359
integer variables defined separately as
5127
03:14:11,800 --> 03:14:16,720
5 10 15 and now that we've created an
5128
03:14:14,359 --> 03:14:18,840
array of numbers we can specify all
5129
03:14:16,720 --> 03:14:20,760
three these values inside our numbers
5130
03:14:18,840 --> 03:14:22,920
array so instead of having three
5131
03:14:20,760 --> 03:14:25,000
individual variables now we just have
5132
03:14:22,920 --> 03:14:26,720
one that holds everything like a group
5133
03:14:25,000 --> 03:14:28,920
and just to show you how this works if
5134
03:14:26,720 --> 03:14:33,720
we have a console right line we can say
5135
03:14:28,920 --> 03:14:35,680
num one space num two space num three
5136
03:14:33,720 --> 03:14:37,760
and if we use controll D to duplicate
5137
03:14:35,680 --> 03:14:39,840
that down then what we can have instead
5138
03:14:37,760 --> 03:14:41,040
of these num ones is simply just place
5139
03:14:39,840 --> 03:14:44,640
these values
5140
03:14:41,040 --> 03:14:46,040
inside changes to one and changes to two
5141
03:14:44,640 --> 03:14:47,479
and what you should notice is these two
5142
03:14:46,040 --> 03:14:49,120
lines are going to print the the exact
5143
03:14:47,479 --> 03:14:52,359
same
5144
03:14:49,120 --> 03:14:54,279
values there we go so in this example
5145
03:14:52,359 --> 03:14:56,040
we've took our sloppy code that will
5146
03:14:54,279 --> 03:14:58,040
roll us around appending one to this
5147
03:14:56,040 --> 03:15:00,200
number creating a new array and then
5148
03:14:58,040 --> 03:15:02,040
printing it out so let's say we want to
5149
03:15:00,200 --> 03:15:04,439
actually use these to read in values
5150
03:15:02,040 --> 03:15:06,520
from the console what can we do and
5151
03:15:04,439 --> 03:15:09,120
you'd use these exactly the same as how
5152
03:15:06,520 --> 03:15:11,000
You' use variables up here this is just
5153
03:15:09,120 --> 03:15:13,760
an integer variable at a specific
5154
03:15:11,000 --> 03:15:16,000
position held inside a group so we could
5155
03:15:13,760 --> 03:15:18,600
simply have a console right line delete
5156
03:15:16,000 --> 03:15:21,040
the right and just say enter a
5157
03:15:18,600 --> 03:15:23,920
number and then instead of starring five
5158
03:15:21,040 --> 03:15:26,600
we can say convert to in32 and then we
5159
03:15:23,920 --> 03:15:28,680
can have a console read
5160
03:15:26,600 --> 03:15:31,279
line and that will read in from the
5161
03:15:28,680 --> 03:15:33,520
console convert it to an integer 32 and
5162
03:15:31,279 --> 03:15:35,960
then store it into the first position of
5163
03:15:33,520 --> 03:15:37,600
our numbers array and now if we just
5164
03:15:35,960 --> 03:15:39,680
copy this down
5165
03:15:37,600 --> 03:15:41,199
twice and we can get rid of these two
5166
03:15:39,680 --> 03:15:44,439
lines cuz this is just staring it
5167
03:15:41,199 --> 03:15:46,000
statically and add one and two to here
5168
03:15:44,439 --> 03:15:49,399
so now what we're saying is enter a
5169
03:15:46,000 --> 03:15:52,160
number star into to 0 1 and 2 and let's
5170
03:15:49,399 --> 03:15:54,960
see what happens now so now we get asked
5171
03:15:52,160 --> 03:15:57,319
the numbers 10 20 and 30 and you can see
5172
03:15:54,960 --> 03:15:58,880
that they've been stored here of course
5173
03:15:57,319 --> 03:16:01,560
these haven't changed cuz this is still
5174
03:15:58,880 --> 03:16:03,239
referencing our static variables up here
5175
03:16:01,560 --> 03:16:05,279
so what you can see in this example is
5176
03:16:03,239 --> 03:16:07,560
now that we've made an array we can
5177
03:16:05,279 --> 03:16:10,279
specify each of the values very easily
5178
03:16:07,560 --> 03:16:12,640
inside here and if so we wanted to have
5179
03:16:10,279 --> 03:16:14,840
a number four instead of having to
5180
03:16:12,640 --> 03:16:17,040
duplicate it and rename it and change
5181
03:16:14,840 --> 03:16:19,399
the value for example we can just simply
5182
03:16:17,040 --> 03:16:21,760
turn this into four duplicate this
5183
03:16:19,399 --> 03:16:25,600
little section and then just change this
5184
03:16:21,760 --> 03:16:27,920
to a three one thing to note in C and
5185
03:16:25,600 --> 03:16:30,199
many other languages almost every single
5186
03:16:27,920 --> 03:16:32,359
index will start at zero in the last
5187
03:16:30,199 --> 03:16:34,520
section we covered the string functions
5188
03:16:32,359 --> 03:16:36,359
and they also started at zero when
5189
03:16:34,520 --> 03:16:38,520
you're dealing with strings and their
5190
03:16:36,359 --> 03:16:40,520
positions when you have a for loop with
5191
03:16:38,520 --> 03:16:43,399
the string then you always want to use
5192
03:16:40,520 --> 03:16:46,239
index Z as the first position the same
5193
03:16:43,399 --> 03:16:48,640
is true here we have four positions
5194
03:16:46,239 --> 03:16:51,800
which means it goes from zero all the
5195
03:16:48,640 --> 03:16:54,640
way till three it never goes from 1 to 4
5196
03:16:51,800 --> 03:16:56,920
or from 0 to 4 these are bad values it
5197
03:16:54,640 --> 03:17:00,160
always goes from 0 to
5198
03:16:56,920 --> 03:17:02,199
three you might be thinking this looks
5199
03:17:00,160 --> 03:17:03,600
really good but when we go to print it
5200
03:17:02,199 --> 03:17:05,640
we're going to have to copy and paste
5201
03:17:03,600 --> 03:17:07,840
this little section add another one and
5202
03:17:05,640 --> 03:17:09,319
rename this to three this gets a little
5203
03:17:07,840 --> 03:17:11,080
bit tedious if you just trying to print
5204
03:17:09,319 --> 03:17:13,279
them all to the screen so let's see how
5205
03:17:11,080 --> 03:17:16,199
we can fix that we can use a four Loop
5206
03:17:13,279 --> 03:17:17,359
for this so we can have four inti equals
5207
03:17:16,199 --> 03:17:19,680
z
5208
03:17:17,359 --> 03:17:22,439
I is less than and then we can have
5209
03:17:19,680 --> 03:17:24,600
numbers length and if you notice from
5210
03:17:22,439 --> 03:17:27,319
iterating through a string variable the
5211
03:17:24,600 --> 03:17:29,680
syntax is exactly the same we have a do
5212
03:17:27,319 --> 03:17:31,279
length property on the arrays as well as
5213
03:17:29,680 --> 03:17:33,640
we do with a string
5214
03:17:31,279 --> 03:17:36,800
variable so in order to print out this
5215
03:17:33,640 --> 03:17:38,520
line what we can do is print one of them
5216
03:17:36,800 --> 03:17:40,319
every time we go around the for Loop
5217
03:17:38,520 --> 03:17:42,199
instead of using a console right line we
5218
03:17:40,319 --> 03:17:45,279
can just use a console
5219
03:17:42,199 --> 03:17:47,640
right so we can have console.
5220
03:17:45,279 --> 03:17:50,120
right and then what we can simply do in
5221
03:17:47,640 --> 03:17:53,640
here is we can say numbers square
5222
03:17:50,120 --> 03:17:56,040
bracket I and then we can have a space
5223
03:17:53,640 --> 03:17:57,720
right after it just to M make what
5224
03:17:56,040 --> 03:18:00,000
happens here we have the number printed
5225
03:17:57,720 --> 03:18:02,120
out and then a space after it number
5226
03:18:00,000 --> 03:18:04,560
printed out a space after it and so on
5227
03:18:02,120 --> 03:18:10,040
and so forth so let's see what happens
5228
03:18:04,560 --> 03:18:11,359
now if we run the code 10 20 30 40 there
5229
03:18:10,040 --> 03:18:13,520
you go so you can see that these two
5230
03:18:11,359 --> 03:18:15,319
lines are exactly the same and now what
5231
03:18:13,520 --> 03:18:17,319
happens is let's just comment these two
5232
03:18:15,319 --> 03:18:20,319
lines out and if we were to change this
5233
03:18:17,319 --> 03:18:23,080
to five and we can duplicate the
5234
03:18:20,319 --> 03:18:25,080
section change this to a four and then
5235
03:18:23,080 --> 03:18:26,680
now we have five variables started and
5236
03:18:25,080 --> 03:18:28,520
we don't need to touch this for Loop
5237
03:18:26,680 --> 03:18:32,479
because the numbers do length will get
5238
03:18:28,520 --> 03:18:34,439
updated anyway so we can have 1 2 3 4 5
5239
03:18:32,479 --> 03:18:36,520
and you can see all five variables have
5240
03:18:34,439 --> 03:18:38,160
been printed automatically what also is
5241
03:18:36,520 --> 03:18:40,319
quite tedious here is we're having to
5242
03:18:38,160 --> 03:18:42,080
add another one to this just so we can
5243
03:18:40,319 --> 03:18:44,319
ask the user how many times that they
5244
03:18:42,080 --> 03:18:46,520
want to enter a number again this is
5245
03:18:44,319 --> 03:18:47,279
quite tedious but we also have a fix for
5246
03:18:46,520 --> 03:18:49,000
that
5247
03:18:47,279 --> 03:18:51,199
because we know how many times we want
5248
03:18:49,000 --> 03:18:53,680
to ask the user to enter a number then
5249
03:18:51,199 --> 03:18:56,080
we can use a for Loop to iterate through
5250
03:18:53,680 --> 03:18:58,720
and ask the user as you can see every
5251
03:18:56,080 --> 03:19:00,600
single line here is exactly the same
5252
03:18:58,720 --> 03:19:02,920
except for the index position that we're
5253
03:19:00,600 --> 03:19:05,199
dealing with and the same is true for
5254
03:19:02,920 --> 03:19:07,319
this for Loop this line will always be
5255
03:19:05,199 --> 03:19:09,000
the same as we're printing out here and
5256
03:19:07,319 --> 03:19:11,439
the only thing that changes between this
5257
03:19:09,000 --> 03:19:14,520
section and this section is the fact
5258
03:19:11,439 --> 03:19:17,199
that this says one and this says zero so
5259
03:19:14,520 --> 03:19:18,120
we can translate this into a f Loop to
5260
03:19:17,199 --> 03:19:20,800
make it
5261
03:19:18,120 --> 03:19:23,600
Dynamic and we can do exactly the same
5262
03:19:20,800 --> 03:19:25,120
thing above here so let's actually copy
5263
03:19:23,600 --> 03:19:27,479
and paste this for
5264
03:19:25,120 --> 03:19:29,279
Loop because we want to do exactly the
5265
03:19:27,479 --> 03:19:31,560
same
5266
03:19:29,279 --> 03:19:33,640
approach we want to go through all the
5267
03:19:31,560 --> 03:19:35,239
positions inside our numbers array and
5268
03:19:33,640 --> 03:19:37,600
then we can actually ask the user to
5269
03:19:35,239 --> 03:19:40,040
enter a number in so let's block comment
5270
03:19:37,600 --> 03:19:41,960
out this massive section and then we can
5271
03:19:40,040 --> 03:19:44,520
take one of them and place it inside the
5272
03:19:41,960 --> 03:19:46,080
for Loop so now we're asking the user
5273
03:19:44,520 --> 03:19:48,520
enter a number and then we're storing
5274
03:19:46,080 --> 03:19:50,399
inside index IND Z and of course if we
5275
03:19:48,520 --> 03:19:51,800
keep using index zero then it's just
5276
03:19:50,399 --> 03:19:53,720
going to override the first one and
5277
03:19:51,800 --> 03:19:55,880
we're not going to store any data so we
5278
03:19:53,720 --> 03:19:59,880
can change that to an i let's see what
5279
03:19:55,880 --> 03:20:01,279
happens when we run it now 1 2 3 4 5 and
5280
03:19:59,880 --> 03:20:03,439
then we get the five numbers being
5281
03:20:01,279 --> 03:20:05,920
printed this is awesome because what
5282
03:20:03,439 --> 03:20:07,680
we've got now is five output commands
5283
03:20:05,920 --> 03:20:10,080
coming straight to the console just from
5284
03:20:07,680 --> 03:20:11,960
a for Loop the for Loop is responsible
5285
03:20:10,080 --> 03:20:14,120
for printing them all out to the screen
5286
03:20:11,960 --> 03:20:15,760
reading in the value convert it to an
5287
03:20:14,120 --> 03:20:17,720
integer and then storing it inside our
5288
03:20:15,760 --> 03:20:19,800
array and then we have a separate for
5289
03:20:17,720 --> 03:20:21,560
Loop to print out to the screen what's
5290
03:20:19,800 --> 03:20:23,479
really really powerful about these two
5291
03:20:21,560 --> 03:20:25,640
for Loops that we've built is if we want
5292
03:20:23,479 --> 03:20:27,800
to have 10 numbers now we simply just
5293
03:20:25,640 --> 03:20:30,560
change this to 10 and then just rerun
5294
03:20:27,800 --> 03:20:33,120
the code as you can see we now have 10
5295
03:20:30,560 --> 03:20:35,640
numbers there you go so what we've just
5296
03:20:33,120 --> 03:20:37,600
done if we move this code down a lot now
5297
03:20:35,640 --> 03:20:40,160
just so we can have it close
5298
03:20:37,600 --> 03:20:42,600
by we've made our entire program that
5299
03:20:40,160 --> 03:20:45,279
was actually pretty lengthy into just a
5300
03:20:42,600 --> 03:20:47,680
couple of lines and two for Loops one
5301
03:20:45,279 --> 03:20:49,920
for Loop is responsible for looping x
5302
03:20:47,680 --> 03:20:51,800
amount of times x depends on whatever
5303
03:20:49,920 --> 03:20:54,600
that number is and then we go through
5304
03:20:51,800 --> 03:20:56,720
and ask the user to enter a number read
5305
03:20:54,600 --> 03:20:59,920
in from the console convert it to an
5306
03:20:56,720 --> 03:21:01,720
integer 32 and start it within the array
5307
03:20:59,920 --> 03:21:03,239
after this for Loop has finished running
5308
03:21:01,720 --> 03:21:05,120
then we have all the numers stored
5309
03:21:03,239 --> 03:21:06,840
inside our aray and then we can go
5310
03:21:05,120 --> 03:21:09,040
through and print them all out to the
5311
03:21:06,840 --> 03:21:11,120
console another way we can print this
5312
03:21:09,040 --> 03:21:14,560
out to the screen is by actually using a
5313
03:21:11,120 --> 03:21:17,960
for each so we can say int num and then
5314
03:21:14,560 --> 03:21:20,160
we want to be a number inside our number
5315
03:21:17,960 --> 03:21:21,720
so for each of the integers inside the
5316
03:21:20,160 --> 03:21:23,560
number we want to create a new local
5317
03:21:21,720 --> 03:21:25,960
variable called num and then we can use
5318
03:21:23,560 --> 03:21:28,040
it in here and if we take this line and
5319
03:21:25,960 --> 03:21:30,000
copy it down everything's exactly the
5320
03:21:28,040 --> 03:21:32,319
same except we don't need to specify the
5321
03:21:30,000 --> 03:21:34,520
main array numbers now and we don't need
5322
03:21:32,319 --> 03:21:36,399
to specify the square brackets we can
5323
03:21:34,520 --> 03:21:38,439
just change this completely and replace
5324
03:21:36,399 --> 03:21:39,680
it with n and if we just make this
5325
03:21:38,439 --> 03:21:41,560
number smaller so we don't have to
5326
03:21:39,680 --> 03:21:44,439
Output a lot of
5327
03:21:41,560 --> 03:21:46,279
values 1 2 and three and you can see
5328
03:21:44,439 --> 03:21:47,920
that they're exactly the same there's no
5329
03:21:46,279 --> 03:21:50,000
line between them CU I don't have a
5330
03:21:47,920 --> 03:21:51,080
console right line so let's quickly add
5331
03:21:50,000 --> 03:21:54,720
that
5332
03:21:51,080 --> 03:21:58,160
in and there we go 1 2 3 and you can see
5333
03:21:54,720 --> 03:21:58,160
they're outputting the exact same
5334
03:21:59,000 --> 03:22:02,920
values the reason why I needed a console
5335
03:22:01,319 --> 03:22:04,960
right line is because these fall Loops
5336
03:22:02,920 --> 03:22:06,800
are using console. right the problem
5337
03:22:04,960 --> 03:22:08,960
with console. right is even after
5338
03:22:06,800 --> 03:22:11,000
finishing writing all of the lines it
5339
03:22:08,960 --> 03:22:13,199
doesn't continue to the next line it
5340
03:22:11,000 --> 03:22:15,199
stays there so if I don't break up the
5341
03:22:13,199 --> 03:22:17,359
two outputs with the console right line
5342
03:22:15,199 --> 03:22:19,239
then all six outputs from the console
5343
03:22:17,359 --> 03:22:21,720
right and the other console right will
5344
03:22:19,239 --> 03:22:23,840
all go on one line this is just for
5345
03:22:21,720 --> 03:22:26,080
demonstration purpose anyway either one
5346
03:22:23,840 --> 03:22:28,160
of these Solutions are fine more people
5347
03:22:26,080 --> 03:22:29,880
tend to use this one the problem with
5348
03:22:28,160 --> 03:22:31,080
these two options is you can't always
5349
03:22:29,880 --> 03:22:33,640
use them
5350
03:22:31,080 --> 03:22:36,000
interchangeably this one will always go
5351
03:22:33,640 --> 03:22:37,880
through every single position it will
5352
03:22:36,000 --> 03:22:40,239
always go from zero and it will always
5353
03:22:37,880 --> 03:22:42,479
go to the length the problem with this
5354
03:22:40,239 --> 03:22:44,120
fall Loop is you can't choose how many
5355
03:22:42,479 --> 03:22:46,160
times you want to iterate through before
5356
03:22:44,120 --> 03:22:48,600
you stop there might be a very specific
5357
03:22:46,160 --> 03:22:50,359
case where you have a big array of data
5358
03:22:48,600 --> 03:22:52,600
but you only want to select a specific
5359
03:22:50,359 --> 03:22:56,239
range if you example where you had a
5360
03:22:52,600 --> 03:22:58,399
th000 array positions so from 0 to 999
5361
03:22:56,239 --> 03:23:01,720
and let's say you wanted to request and
5362
03:22:58,399 --> 03:23:04,720
run a for loop from 500 to 600 so you
5363
03:23:01,720 --> 03:23:07,680
would literally type in 500 in here and
5364
03:23:04,720 --> 03:23:09,720
then 600 inside here and this would be
5365
03:23:07,680 --> 03:23:12,080
fine as long as your array has that many
5366
03:23:09,720 --> 03:23:14,000
positions but with the 4 each Loop if
5367
03:23:12,080 --> 03:23:15,680
you run a 4 each loop on a thousand
5368
03:23:14,000 --> 03:23:17,960
length array then it's going to go
5369
03:23:15,680 --> 03:23:19,760
through every sing single position in
5370
03:23:17,960 --> 03:23:21,399
some cases you might not want that and
5371
03:23:19,760 --> 03:23:23,479
you only want to go through specific
5372
03:23:21,399 --> 03:23:25,160
cases so if you know you're going to go
5373
03:23:23,479 --> 03:23:27,239
through every single case then you can
5374
03:23:25,160 --> 03:23:28,680
use the for each but if you know you're
5375
03:23:27,239 --> 03:23:30,560
going to go through a certain amount of
5376
03:23:28,680 --> 03:23:32,359
cases then it's always best to use the
5377
03:23:30,560 --> 03:23:35,239
for Loop okay so we covered quite a lot
5378
03:23:32,359 --> 03:23:37,319
in this video so let's just have a recap
5379
03:23:35,239 --> 03:23:39,279
so initially we started off saying we
5380
03:23:37,319 --> 03:23:41,560
have our gome one gome two and gome
5381
03:23:39,279 --> 03:23:43,680
three then we had our static variables
5382
03:23:41,560 --> 03:23:46,080
stored inside there and then we created
5383
03:23:43,680 --> 03:23:47,479
our integer array which is now down here
5384
03:23:46,080 --> 03:23:49,760
and just just to make it a little bit
5385
03:23:47,479 --> 03:23:51,439
easier to store multiple variables
5386
03:23:49,760 --> 03:23:52,920
because if you wanted five variables
5387
03:23:51,439 --> 03:23:54,920
you'd now have to duplicate that and
5388
03:23:52,920 --> 03:23:56,560
change that to number five and this will
5389
03:23:54,920 --> 03:23:59,880
get very tedious if we had something
5390
03:23:56,560 --> 03:24:01,600
like 10 like before this is very long
5391
03:23:59,880 --> 03:24:03,960
and you don't need be specifying number
5392
03:24:01,600 --> 03:24:06,359
10 in your code because this also hurts
5393
03:24:03,960 --> 03:24:08,520
the readability of it using arrays can
5394
03:24:06,359 --> 03:24:11,880
solve this problem quite a lot you can
5395
03:24:08,520 --> 03:24:14,640
easily specify array using this notation
5396
03:24:11,880 --> 03:24:16,600
int square brackets followed by the name
5397
03:24:14,640 --> 03:24:19,000
and you make that equal to a new int
5398
03:24:16,600 --> 03:24:20,800
three the three can be any number that
5399
03:24:19,000 --> 03:24:22,359
you want as long as it's a whole number
5400
03:24:20,800 --> 03:24:24,600
cuz this tells us how many positions
5401
03:24:22,359 --> 03:24:26,640
that we've got we then outputed lots of
5402
03:24:24,600 --> 03:24:29,359
values to the console and read them into
5403
03:24:26,640 --> 03:24:31,520
our Ray but we had the same problem as
5404
03:24:29,359 --> 03:24:33,880
we did above where we just had to keep
5405
03:24:31,520 --> 03:24:35,760
duplicating the sections so what we did
5406
03:24:33,880 --> 03:24:37,720
to prevent that is by writing a little
5407
03:24:35,760 --> 03:24:39,800
for Loop that went through every single
5408
03:24:37,720 --> 03:24:41,640
position in numbers array outp puted
5409
03:24:39,800 --> 03:24:43,720
something to the screen read in the
5410
03:24:41,640 --> 03:24:45,760
value converted it and stored it into
5411
03:24:43,720 --> 03:24:48,439
the array and then after all that was
5412
03:24:45,760 --> 03:24:50,359
done we can use a for Loop or for each
5413
03:24:48,439 --> 03:24:51,920
Loop and print that back to the screen
5414
03:24:50,359 --> 03:24:53,680
now that we finished the base content of
5415
03:24:51,920 --> 03:24:54,800
this video and because I said at the
5416
03:24:53,680 --> 03:24:56,760
start that I was going to take the
5417
03:24:54,800 --> 03:24:59,520
triangle exercise and convert that into
5418
03:24:56,760 --> 03:25:01,239
an array so let's do that now so now
5419
03:24:59,520 --> 03:25:03,120
I've copy and paste a solution from the
5420
03:25:01,239 --> 03:25:05,160
triangle exercise straight into this
5421
03:25:03,120 --> 03:25:06,800
project I've just block commented
5422
03:25:05,160 --> 03:25:08,960
everything above just so it doesn't
5423
03:25:06,800 --> 03:25:10,840
interfere with our code so let's just
5424
03:25:08,960 --> 03:25:14,960
run this code to familiarizes with
5425
03:25:10,840 --> 03:25:17,160
what's going on so we enter 60 60 and 60
5426
03:25:14,960 --> 03:25:18,479
then it says valid
5427
03:25:17,160 --> 03:25:20,960
and if you run it again and enter
5428
03:25:18,479 --> 03:25:23,600
something else that doesn't equal to 180
5429
03:25:20,960 --> 03:25:25,199
then we get invalid so let's see how we
5430
03:25:23,600 --> 03:25:27,479
can make this code a lot better by the
5431
03:25:25,199 --> 03:25:29,199
use of arrays so as we said before we
5432
03:25:27,479 --> 03:25:32,120
don't want to be using this angle one
5433
03:25:29,199 --> 03:25:36,520
angle two and angle three let's make an
5434
03:25:32,120 --> 03:25:38,239
INT square bracket angles equal new int
5435
03:25:36,520 --> 03:25:40,720
and then in this case we have three
5436
03:25:38,239 --> 03:25:43,560
angles and what you could do as well is
5437
03:25:40,720 --> 03:25:44,520
you could say const int angle count
5438
03:25:43,560 --> 03:25:46,920
equals
5439
03:25:44,520 --> 03:25:49,439
3 and then you can simp L use that
5440
03:25:46,920 --> 03:25:51,199
inside here so later on in the code if
5441
03:25:49,439 --> 03:25:53,520
we wanted to specify this to a different
5442
03:25:51,199 --> 03:25:56,560
value we can just change that later
5443
03:25:53,520 --> 03:25:58,760
on so now that we've got three angles we
5444
03:25:56,560 --> 03:26:01,680
can get rid of these three lines and
5445
03:25:58,760 --> 03:26:03,439
write ourselves a little for Loop so I
5446
03:26:01,680 --> 03:26:06,120
goes from zero all the way to the
5447
03:26:03,439 --> 03:26:08,319
angles. length and then every single
5448
03:26:06,120 --> 03:26:10,120
time we go around in the for Loop we
5449
03:26:08,319 --> 03:26:15,920
want to
5450
03:26:10,120 --> 03:26:19,239
say console. right enter angle one and
5451
03:26:15,920 --> 03:26:21,199
then we can say angles square bracket
5452
03:26:19,239 --> 03:26:23,720
and not zero in this case cuz we don't
5453
03:26:21,199 --> 03:26:27,640
be specifying index as it will go from
5454
03:26:23,720 --> 03:26:29,600
zero to one to two we want to be using I
5455
03:26:27,640 --> 03:26:31,040
I because we're inside a for Loop that's
5456
03:26:29,600 --> 03:26:34,120
already going through all the indexes
5457
03:26:31,040 --> 03:26:37,080
for us and make that equal to convert to
5458
03:26:34,120 --> 03:26:39,199
in32 and then a console.
5459
03:26:37,080 --> 03:26:42,080
readline now we've got all the values
5460
03:26:39,199 --> 03:26:44,160
stored inside here let's deal with this
5461
03:26:42,080 --> 03:26:46,040
problem so if we just comment out the
5462
03:26:44,160 --> 03:26:48,239
total for the time being let's run the
5463
03:26:46,040 --> 03:26:48,239
code
5464
03:26:51,479 --> 03:26:55,840
oh we have one more error down here
5465
03:26:53,000 --> 03:26:57,960
sorry let's run the code again and we
5466
03:26:55,840 --> 03:27:00,120
can say enter angle one and we can type
5467
03:26:57,960 --> 03:27:03,040
the values in but as you can see we've
5468
03:27:00,120 --> 03:27:05,520
got angle one for each of the times what
5469
03:27:03,040 --> 03:27:08,000
we can do in here is use our I index to
5470
03:27:05,520 --> 03:27:10,680
print out the value so we can turn this
5471
03:27:08,000 --> 03:27:12,880
into interpolation and then wrap the one
5472
03:27:10,680 --> 03:27:15,239
inside curly braces and we can simply
5473
03:27:12,880 --> 03:27:18,680
change as one to an i and let's see what
5474
03:27:15,239 --> 03:27:20,880
happens now so we have enter angle zero
5475
03:27:18,680 --> 03:27:23,520
and then one and then two and this is
5476
03:27:20,880 --> 03:27:25,800
fine but humans don't like reading zero
5477
03:27:23,520 --> 03:27:28,720
they like reading 1 2 and three so what
5478
03:27:25,800 --> 03:27:30,640
we can simply do is say I + 1 and
5479
03:27:28,720 --> 03:27:32,239
because we're using I and then adding
5480
03:27:30,640 --> 03:27:34,199
one to it it's not going to affect the
5481
03:27:32,239 --> 03:27:36,640
variable inside the for Loop so there's
5482
03:27:34,199 --> 03:27:39,399
no problems by doing this this is simply
5483
03:27:36,640 --> 03:27:41,640
just for the output purposes so now we
5484
03:27:39,399 --> 03:27:44,920
can have one two and three just shown up
5485
03:27:41,640 --> 03:27:47,439
like that perfect so what we can do now
5486
03:27:44,920 --> 03:27:49,239
that we have all the angles stored now
5487
03:27:47,439 --> 03:27:50,760
we need to replace this line by writing
5488
03:27:49,239 --> 03:27:53,680
a for Loop that can add up all the
5489
03:27:50,760 --> 03:27:57,680
values for us so we can have four each
5490
03:27:53,680 --> 03:28:03,880
in angle inside our angles and we can
5491
03:27:57,680 --> 03:28:06,800
just say int angle sum plus equals
5492
03:28:03,880 --> 03:28:09,399
angle the problem with this is we can't
5493
03:28:06,800 --> 03:28:11,760
specify a new variable inside here and
5494
03:28:09,399 --> 03:28:13,880
then use it outside an if statement so
5495
03:28:11,760 --> 03:28:16,120
what do we do in this case you want to
5496
03:28:13,880 --> 03:28:18,000
take angle sum and initialize it out
5497
03:28:16,120 --> 03:28:20,600
outside the for Loop and then give it a
5498
03:28:18,000 --> 03:28:22,680
value of zero and then inside the for
5499
03:28:20,600 --> 03:28:25,960
Loop we can remove the int and we can
5500
03:28:22,680 --> 03:28:28,560
say angle sum plus equals the whatever
5501
03:28:25,960 --> 03:28:30,319
angle that we're currently up to so if
5502
03:28:28,560 --> 03:28:33,520
we had three values that's going to say
5503
03:28:30,319 --> 03:28:35,600
0 + 60 and then take that value and add
5504
03:28:33,520 --> 03:28:37,680
another 60 and then take that value and
5505
03:28:35,600 --> 03:28:39,720
add another 60 so it'll keep a running
5506
03:28:37,680 --> 03:28:41,720
total for us and if we just print out
5507
03:28:39,720 --> 03:28:45,880
angles some to the screen let's see what
5508
03:28:41,720 --> 03:28:47,680
happens 10 20 30 and now we have 60 and
5509
03:28:45,880 --> 03:28:50,160
that's the correct calculation if you
5510
03:28:47,680 --> 03:28:52,199
add them three together so now that we
5511
03:28:50,160 --> 03:28:54,239
have a running total for angle sum then
5512
03:28:52,199 --> 03:28:56,239
we're pretty much done if we get rid of
5513
03:28:54,239 --> 03:28:59,640
this line of code which is our previous
5514
03:28:56,239 --> 03:29:01,720
total and we actually bring back our if
5515
03:28:59,640 --> 03:29:03,720
statement there you go there's no error
5516
03:29:01,720 --> 03:29:06,199
now and all this is doing is checking if
5517
03:29:03,720 --> 03:29:08,560
the angle sum is equal to 180 and then
5518
03:29:06,199 --> 03:29:12,000
print valid and invalid so we can have
5519
03:29:08,560 --> 03:29:13,920
60 60 60 and it prints out valid and
5520
03:29:12,000 --> 03:29:17,359
let's just comment out our
5521
03:29:13,920 --> 03:29:19,080
output and then we can have 66 6059 and
5522
03:29:17,359 --> 03:29:20,920
then it prints out in valid which is the
5523
03:29:19,080 --> 03:29:23,960
exact same behavior that we saw at the
5524
03:29:20,920 --> 03:29:27,359
start so let's remove the extra cord in
5525
03:29:23,960 --> 03:29:29,199
here and keep it like this there you go
5526
03:29:27,359 --> 03:29:31,080
so now we've got our solution just like
5527
03:29:29,199 --> 03:29:33,279
this and it's a lot better because if
5528
03:29:31,080 --> 03:29:35,120
triangles were a transform in the future
5529
03:29:33,279 --> 03:29:37,239
and we wanted more than three sides then
5530
03:29:35,120 --> 03:29:38,840
we can easily update this value because
5531
03:29:37,239 --> 03:29:41,279
now that we're using AR raise or we're
5532
03:29:38,840 --> 03:29:43,080
using a for Loop to enter out the values
5533
03:29:41,279 --> 03:29:45,359
what we can do we don't actually need to
5534
03:29:43,080 --> 03:29:46,960
store it what I mean by that is we can
5535
03:29:45,359 --> 03:29:49,359
actually just get rid rid of this array
5536
03:29:46,960 --> 03:29:52,319
completely let me
5537
03:29:49,359 --> 03:29:54,399
demonstrate we can take this angle sum
5538
03:29:52,319 --> 03:29:56,120
and place it underneath here we can keep
5539
03:29:54,399 --> 03:29:58,279
the count so we can use that in the for
5540
03:29:56,120 --> 03:30:00,880
Loop so we're going from 0o to three
5541
03:29:58,279 --> 03:30:02,399
because that's our angle count and then
5542
03:30:00,880 --> 03:30:06,000
we can get rid of all
5543
03:30:02,399 --> 03:30:08,399
this and let's see what we can do now so
5544
03:30:06,000 --> 03:30:10,880
we have an angle count equals 3 so we go
5545
03:30:08,399 --> 03:30:13,520
from 0 to three which you'll just do as
5546
03:30:10,880 --> 03:30:16,239
we said before from 0 to two because
5547
03:30:13,520 --> 03:30:17,479
it's less than the angle count and let's
5548
03:30:16,239 --> 03:30:19,640
just print this to the screen see what
5549
03:30:17,479 --> 03:30:21,600
happens so the output's still exactly
5550
03:30:19,640 --> 03:30:23,439
the same but what's happening is these
5551
03:30:21,600 --> 03:30:25,120
values are not being stored they're just
5552
03:30:23,439 --> 03:30:28,120
being shown to the screen and then not
5553
03:30:25,120 --> 03:30:30,640
done anything there is no equal sign
5554
03:30:28,120 --> 03:30:33,120
here to assign it into anything so what
5555
03:30:30,640 --> 03:30:35,920
we can do is we can say angle sum plus
5556
03:30:33,120 --> 03:30:39,640
equals the value straight away and now
5557
03:30:35,920 --> 03:30:41,520
if we try it 60 60 60 then we get valid
5558
03:30:39,640 --> 03:30:44,279
the main differences between these two
5559
03:30:41,520 --> 03:30:46,239
situations is in the first one we used
5560
03:30:44,279 --> 03:30:49,560
an array to store all the different
5561
03:30:46,239 --> 03:30:51,439
values and then we added the total but
5562
03:30:49,560 --> 03:30:53,439
if we're not going to use these values
5563
03:30:51,439 --> 03:30:55,560
after then what's the point in storing
5564
03:30:53,439 --> 03:30:57,319
them we can just use a for Loop to ask
5565
03:30:55,560 --> 03:30:59,600
the user how many times they want to
5566
03:30:57,319 --> 03:31:01,399
input the values and then just add them
5567
03:30:59,600 --> 03:31:03,560
every single time that they add the
5568
03:31:01,399 --> 03:31:05,359
calculation in and at the end we could
5569
03:31:03,560 --> 03:31:06,800
just simply use a conditional operator
5570
03:31:05,359 --> 03:31:08,439
to be able to print at the screen
5571
03:31:06,800 --> 03:31:10,239
whether it's valid or not but let me
5572
03:31:08,439 --> 03:31:12,000
just undo that solution so I can show
5573
03:31:10,239 --> 03:31:13,920
you the array solution and we can have a
5574
03:31:12,000 --> 03:31:15,760
quick recap that was just an extra
5575
03:31:13,920 --> 03:31:17,080
little tip just in case you wanted to
5576
03:31:15,760 --> 03:31:19,560
know know what different ways you could
5577
03:31:17,080 --> 03:31:21,800
use to make this work there we go so
5578
03:31:19,560 --> 03:31:24,239
let's just have a quick recap here so we
5579
03:31:21,800 --> 03:31:26,640
have a Conant which all we'll do is let
5580
03:31:24,239 --> 03:31:28,720
us specify three inside here and then we
5581
03:31:26,640 --> 03:31:30,560
can use it here this isn't entirely
5582
03:31:28,720 --> 03:31:32,279
necessary but it does clean up your code
5583
03:31:30,560 --> 03:31:34,520
a little bit because it gives this
5584
03:31:32,279 --> 03:31:37,600
number three a bit of a meaning it says
5585
03:31:34,520 --> 03:31:39,359
the amount of angles is our angle count
5586
03:31:37,600 --> 03:31:41,279
having a three left over like this is
5587
03:31:39,359 --> 03:31:42,520
what we call a magic number they're
5588
03:31:41,279 --> 03:31:44,960
quite bad because it hurts the
5589
03:31:42,520 --> 03:31:47,359
readability of your code people want to
5590
03:31:44,960 --> 03:31:49,479
know what the three actually means so if
5591
03:31:47,359 --> 03:31:51,960
you bring in an angle count then you can
5592
03:31:49,479 --> 03:31:53,960
actually read we want a set of angles
5593
03:31:51,960 --> 03:31:56,640
and this is the amount that we need and
5594
03:31:53,960 --> 03:31:59,680
that's denoted by angle count it adds
5595
03:31:56,640 --> 03:32:02,000
text to a variable it gives you a way of
5596
03:31:59,680 --> 03:32:04,080
making sure that the text equivalent of
5597
03:32:02,000 --> 03:32:06,080
a variable is present so someone can
5598
03:32:04,080 --> 03:32:08,319
just read it after we've created our
5599
03:32:06,080 --> 03:32:10,439
integer we can actually make a for Loop
5600
03:32:08,319 --> 03:32:12,640
that goes from zero to the length and
5601
03:32:10,439 --> 03:32:15,120
then we ask the user to enter the angle
5602
03:32:12,640 --> 03:32:17,680
and in the previous example we had enter
5603
03:32:15,120 --> 03:32:20,160
angle one two and three so now we can
5604
03:32:17,680 --> 03:32:22,840
mimic that by using I but because I
5605
03:32:20,160 --> 03:32:24,439
starts at zero we want to use plus one
5606
03:32:22,840 --> 03:32:26,600
every time we loop around the for Loop
5607
03:32:24,439 --> 03:32:28,800
we ask the user to enter the number read
5608
03:32:26,600 --> 03:32:31,680
it in through console read line convert
5609
03:32:28,800 --> 03:32:33,840
it using convert 2 in 32 and then St
5610
03:32:31,680 --> 03:32:35,920
into the array after this for Loop is
5611
03:32:33,840 --> 03:32:37,880
finished we have all three values stored
5612
03:32:35,920 --> 03:32:39,880
in the angles array and then we have an
5613
03:32:37,880 --> 03:32:42,279
In Sum to count up all the values that
5614
03:32:39,880 --> 03:32:44,080
we need and then we use the fall Loop to
5615
03:32:42,279 --> 03:32:46,399
go through every single integer value
5616
03:32:44,080 --> 03:32:49,040
inside and make a running to total and
5617
03:32:46,399 --> 03:32:51,239
store it inside angle sum once angle sum
5618
03:32:49,040 --> 03:32:53,399
has the total count then what we can do
5619
03:32:51,239 --> 03:32:55,319
is use console right line along with a
5620
03:32:53,399 --> 03:32:59,040
conditional operator to print whether
5621
03:32:55,319 --> 03:32:59,040
it's valid or invalid to the
5622
03:32:59,359 --> 03:33:03,239
screen in this video we're going to look
5623
03:33:01,439 --> 03:33:05,720
at how we can use the built-in array
5624
03:33:03,239 --> 03:33:08,199
functions to be able to sort an array so
5625
03:33:05,720 --> 03:33:09,920
if you have an INT array called numbers
5626
03:33:08,199 --> 03:33:11,520
and then using the previous notation
5627
03:33:09,920 --> 03:33:13,479
that we looked at we can actually
5628
03:33:11,520 --> 03:33:16,160
specify any numbers that we want using a
5629
03:33:13,479 --> 03:33:18,319
common delimited list so if we just have
5630
03:33:16,160 --> 03:33:20,239
nine numbers inside here then we've
5631
03:33:18,319 --> 03:33:21,880
actually defined an integer array and
5632
03:33:20,239 --> 03:33:23,960
then the compiler can count the amount
5633
03:33:21,880 --> 03:33:25,720
of items that are inside here and give
5634
03:33:23,960 --> 03:33:27,760
it the length but we don't have to worry
5635
03:33:25,720 --> 03:33:30,680
about that ourselves so if we use a for
5636
03:33:27,760 --> 03:33:34,439
each and we can just print
5637
03:33:30,680 --> 03:33:36,319
Inn inside numbers we can do a console
5638
03:33:34,439 --> 03:33:38,720
right line on the new local variable
5639
03:33:36,319 --> 03:33:40,560
called num if print it out we can see
5640
03:33:38,720 --> 03:33:43,640
all of the values and maybe we can
5641
03:33:40,560 --> 03:33:46,199
actually just use a console
5642
03:33:43,640 --> 03:33:49,840
right followed by a space just to give
5643
03:33:46,199 --> 03:33:51,399
them all on the same line there we go so
5644
03:33:49,840 --> 03:33:53,160
as you saw before if you had a string
5645
03:33:51,399 --> 03:33:55,359
called test and we wanted to do
5646
03:33:53,160 --> 03:33:57,520
something to it for example if we wanted
5647
03:33:55,359 --> 03:33:59,399
to use the replace function if we wanted
5648
03:33:57,520 --> 03:34:01,520
to remove all spaces with an empty
5649
03:33:59,399 --> 03:34:03,239
string then if we check the replace tool
5650
03:34:01,520 --> 03:34:05,880
tie we can see that it returns a new
5651
03:34:03,239 --> 03:34:07,840
string most of the string functions will
5652
03:34:05,880 --> 03:34:10,319
return you back the new string after
5653
03:34:07,840 --> 03:34:13,560
doing something with it but in this case
5654
03:34:10,319 --> 03:34:15,359
if we type in Array followed by do sort
5655
03:34:13,560 --> 03:34:16,520
and we can place our array straight in
5656
03:34:15,359 --> 03:34:18,239
the bracket
5657
03:34:16,520 --> 03:34:20,279
what you realize is if you hover over
5658
03:34:18,239 --> 03:34:22,560
sord it will sort the elements in the
5659
03:34:20,279 --> 03:34:25,239
entire array but it will indeed return
5660
03:34:22,560 --> 03:34:27,640
back a void and vo just means it returns
5661
03:34:25,239 --> 03:34:29,920
back nothing so in this case what it's
5662
03:34:27,640 --> 03:34:32,000
doing is you're actually affecting the
5663
03:34:29,920 --> 03:34:34,800
numbers array straight away and you
5664
03:34:32,000 --> 03:34:36,960
don't have to do numbers equals array do
5665
03:34:34,800 --> 03:34:38,720
sort numbers for example if we were
5666
03:34:36,960 --> 03:34:41,000
doing the test replace we would have to
5667
03:34:38,720 --> 03:34:43,080
do this otherwise this would just be
5668
03:34:41,000 --> 03:34:45,880
returning back a string but we wouldn't
5669
03:34:43,080 --> 03:34:48,520
be using it the same is not true for the
5670
03:34:45,880 --> 03:34:50,600
arrays when you run the array functions
5671
03:34:48,520 --> 03:34:52,279
they tend to return back void and you
5672
03:34:50,600 --> 03:34:53,960
don't have to do anything other than
5673
03:34:52,279 --> 03:34:56,279
give it the array that you wanted to
5674
03:34:53,960 --> 03:34:57,920
sort so now if you run this code you
5675
03:34:56,279 --> 03:35:00,160
should just see the same output since
5676
03:34:57,920 --> 03:35:02,439
they already sorted anyway so let's
5677
03:35:00,160 --> 03:35:05,080
change some of these numbers
5678
03:35:02,439 --> 03:35:10,880
up and just see what happens
5679
03:35:05,080 --> 03:35:12,800
now there we go 02 55 6 7 8 999 and
5680
03:35:10,880 --> 03:35:14,319
that's it sorted just like that there
5681
03:35:12,800 --> 03:35:16,199
isn't a whole lot to this sorting
5682
03:35:14,319 --> 03:35:18,760
function but what you should note is you
5683
03:35:16,199 --> 03:35:20,720
can use this new array syntax to Define
5684
03:35:18,760 --> 03:35:22,359
how many items that you want in there
5685
03:35:20,720 --> 03:35:23,960
instead of having to change the amount
5686
03:35:22,359 --> 03:35:26,359
of positions that you have using the
5687
03:35:23,960 --> 03:35:28,680
square brackets you can just denote any
5688
03:35:26,359 --> 03:35:30,479
number of items that you want inside the
5689
03:35:28,680 --> 03:35:32,160
compiler will count them for you and PR
5690
03:35:30,479 --> 03:35:33,840
the length straight into the bracket for
5691
03:35:32,160 --> 03:35:36,080
you so you don't have to worry about it
5692
03:35:33,840 --> 03:35:38,199
using the array. sort function all you
5693
03:35:36,080 --> 03:35:40,239
have to do is give it the parameter of
5694
03:35:38,199 --> 03:35:42,199
the array that you want to sort and it
5695
03:35:40,239 --> 03:35:44,120
will affect the array straight away and
5696
03:35:42,199 --> 03:35:46,199
you don't have to restore it and then
5697
03:35:44,120 --> 03:35:48,960
we're just using a simple 4 each Loop to
5698
03:35:46,199 --> 03:35:48,960
print them out to the
5699
03:35:49,000 --> 03:35:53,479
screen in this video I want to discuss
5700
03:35:51,359 --> 03:35:57,120
the array reverse function so if you had
5701
03:35:53,479 --> 03:35:59,279
an INT numbers equal new int and if you
5702
03:35:57,120 --> 03:36:01,760
just had lots of numbers in here
5703
03:35:59,279 --> 03:36:04,000
defined just up till five then we can
5704
03:36:01,760 --> 03:36:06,479
add a semicolon and again using this
5705
03:36:04,000 --> 03:36:09,319
array function we can say array. reverse
5706
03:36:06,479 --> 03:36:11,960
and pass our array
5707
03:36:09,319 --> 03:36:14,000
inside and then we can just use a for H
5708
03:36:11,960 --> 03:36:16,239
of our item inside our
5709
03:36:14,000 --> 03:36:18,279
numbers and simply just print them out
5710
03:36:16,239 --> 03:36:19,800
using a console. write and we can use
5711
03:36:18,279 --> 03:36:22,600
string interpolation and just put a
5712
03:36:19,800 --> 03:36:25,160
space at the end there we go and if you
5713
03:36:22,600 --> 03:36:26,840
run the code then you can see our
5714
03:36:25,160 --> 03:36:28,560
numbers have just been reversed there
5715
03:36:26,840 --> 03:36:29,680
isn't a lot to this video I just wanted
5716
03:36:28,560 --> 03:36:31,359
to show you about the different
5717
03:36:29,680 --> 03:36:33,319
functions that you have available for
5718
03:36:31,359 --> 03:36:34,880
arrays these can come in handy when you
5719
03:36:33,319 --> 03:36:37,000
have something like a list returned from
5720
03:36:34,880 --> 03:36:38,760
a database for example and it's ordered
5721
03:36:37,000 --> 03:36:40,840
in a certain way let's say you're
5722
03:36:38,760 --> 03:36:42,800
returning back a list of lots of people
5723
03:36:40,840 --> 03:36:44,800
who are ordered back alphabetically and
5724
03:36:42,800 --> 03:36:46,479
it's going from a to zed and let's say
5725
03:36:44,800 --> 03:36:48,520
you have some sort of user interface
5726
03:36:46,479 --> 03:36:50,319
allows you to flip the order instead of
5727
03:36:48,520 --> 03:36:52,319
having to request the database to get
5728
03:36:50,319 --> 03:36:53,960
the data back in the opposite order you
5729
03:36:52,319 --> 03:36:55,800
can use this array function to just
5730
03:36:53,960 --> 03:36:57,279
reverse the list the same thing goes if
5731
03:36:55,800 --> 03:36:59,120
you're trying to order by lowest price
5732
03:36:57,279 --> 03:37:00,800
or highest price you can use this to
5733
03:36:59,120 --> 03:37:02,680
show the different values in different
5734
03:37:00,800 --> 03:37:04,239
orders that's where this array function
5735
03:37:02,680 --> 03:37:06,560
comes in handy when you have different
5736
03:37:04,239 --> 03:37:08,920
situations like the names or the price
5737
03:37:06,560 --> 03:37:11,199
by ascending or descending where you can
5738
03:37:08,920 --> 03:37:13,319
just flip the data instead of having to
5739
03:37:11,199 --> 03:37:15,600
request it back from the database again
5740
03:37:13,319 --> 03:37:17,439
flipping it locally is a lot easier than
5741
03:37:15,600 --> 03:37:19,439
going back to the database is that uses
5742
03:37:17,439 --> 03:37:21,199
more data and more computational power
5743
03:37:19,439 --> 03:37:22,920
on the back end and then return it to
5744
03:37:21,199 --> 03:37:24,600
the front end it's always best to be
5745
03:37:22,920 --> 03:37:26,760
able to use these functions instead of
5746
03:37:24,600 --> 03:37:29,279
having to use external things to be able
5747
03:37:26,760 --> 03:37:30,880
to return back the same data using it
5748
03:37:29,279 --> 03:37:32,920
locally like this will give us a
5749
03:37:30,880 --> 03:37:34,640
performance Advantage because it's done
5750
03:37:32,920 --> 03:37:36,920
directly on the data instead of having
5751
03:37:34,640 --> 03:37:38,800
to request everything again if you
5752
03:37:36,920 --> 03:37:41,040
wanted to do this manually let's have a
5753
03:37:38,800 --> 03:37:43,000
look and see what your options are you
5754
03:37:41,040 --> 03:37:45,199
can have your integer array just defined
5755
03:37:43,000 --> 03:37:47,319
like this with a dynamic list and then
5756
03:37:45,199 --> 03:37:49,960
we can have a sorted array using the
5757
03:37:47,319 --> 03:37:52,160
same length as the current array what
5758
03:37:49,960 --> 03:37:53,720
you can do instead of using reverse we
5759
03:37:52,160 --> 03:37:56,600
can actually just comment these lines
5760
03:37:53,720 --> 03:37:58,359
out you can bring back a normal for Loop
5761
03:37:56,600 --> 03:38:00,319
but if we use two RS it gives us a
5762
03:37:58,359 --> 03:38:03,080
reverse for Loop so the length is going
5763
03:38:00,319 --> 03:38:05,080
to be our numbers do length and the rest
5764
03:38:03,080 --> 03:38:07,800
of the paramet is exactly the same we
5765
03:38:05,080 --> 03:38:09,120
want to go from the last minus one all
5766
03:38:07,800 --> 03:38:11,399
the way till zero and we want to
5767
03:38:09,120 --> 03:38:15,760
decrement in one and we can say our
5768
03:38:11,399 --> 03:38:17,600
sorted number I is equal to numbers all
5769
03:38:15,760 --> 03:38:19,800
right but all this will do right now is
5770
03:38:17,600 --> 03:38:21,239
just give us back the same value stored
5771
03:38:19,800 --> 03:38:23,279
so what we need to do in a case like
5772
03:38:21,239 --> 03:38:26,399
this is make another variable something
5773
03:38:23,279 --> 03:38:28,439
like X and we can store that into zero
5774
03:38:26,399 --> 03:38:30,920
and what we can say is X is assigned the
5775
03:38:28,439 --> 03:38:32,600
value of whatever this numbers is
5776
03:38:30,920 --> 03:38:34,600
because this is going backwards but we
5777
03:38:32,600 --> 03:38:37,399
want this one to go forwards cuz we want
5778
03:38:34,600 --> 03:38:39,199
it to be reversed and we can say x++ and
5779
03:38:37,399 --> 03:38:41,239
see what happens when we run it
5780
03:38:39,199 --> 03:38:42,880
now so nothing's going to come up
5781
03:38:41,239 --> 03:38:44,439
because we don't have any output so
5782
03:38:42,880 --> 03:38:47,239
let's use a for
5783
03:38:44,439 --> 03:38:50,880
each we can have VAR
5784
03:38:47,239 --> 03:38:52,840
number inside our sorted numbers and we
5785
03:38:50,880 --> 03:38:54,880
can just take this and place it in here
5786
03:38:52,840 --> 03:38:56,640
and just print out the item and we can
5787
03:38:54,880 --> 03:38:59,960
use
5788
03:38:56,640 --> 03:39:01,520
num and there you go it's been flipped
5789
03:38:59,960 --> 03:39:03,560
the exact same value is just the other
5790
03:39:01,520 --> 03:39:05,120
way so this is obviously a little bit
5791
03:39:03,560 --> 03:39:07,319
more complicated because you have to use
5792
03:39:05,120 --> 03:39:09,399
an additional variable use a backwards
5793
03:39:07,319 --> 03:39:11,439
for Loop and then incent your own
5794
03:39:09,399 --> 03:39:13,560
separate variable and as well as
5795
03:39:11,439 --> 03:39:14,840
completely make a whole new array of
5796
03:39:13,560 --> 03:39:16,880
course you should only do this if you
5797
03:39:14,840 --> 03:39:18,239
need it as two separate arrays if not
5798
03:39:16,880 --> 03:39:20,800
you can always just affect the one that
5799
03:39:18,239 --> 03:39:23,359
you're up to right
5800
03:39:20,800 --> 03:39:24,840
now because I said before that arrays
5801
03:39:23,359 --> 03:39:27,199
are actually static data and they're
5802
03:39:24,840 --> 03:39:29,840
going to be fixed size then there's no
5803
03:39:27,199 --> 03:39:31,960
way to remove or add an item but you can
5804
03:39:29,840 --> 03:39:35,439
clear what you have already so let's
5805
03:39:31,960 --> 03:39:37,439
have a look array. clear sets a range of
5806
03:39:35,439 --> 03:39:39,720
elements in an array to the default
5807
03:39:37,439 --> 03:39:41,680
value of each element type so the first
5808
03:39:39,720 --> 03:39:43,600
parameter takes our array so let's use
5809
03:39:41,680 --> 03:39:45,040
numbers and we want to start off the
5810
03:39:43,600 --> 03:39:47,520
index that we want to clear from and
5811
03:39:45,040 --> 03:39:49,800
let's say zero and because we want to
5812
03:39:47,520 --> 03:39:52,120
clear the entire array let's go all the
5813
03:39:49,800 --> 03:39:55,199
way to numbers. length and now let's
5814
03:39:52,120 --> 03:39:57,439
just produce a for each our item inside
5815
03:39:55,199 --> 03:40:00,319
numbers and then just have a console
5816
03:39:57,439 --> 03:40:02,600
right string interpolation which just
5817
03:40:00,319 --> 03:40:04,040
prints out the item followed by space
5818
03:40:02,600 --> 03:40:06,640
and now let's run the code and see what
5819
03:40:04,040 --> 03:40:08,880
happens so we have lots of zeros in the
5820
03:40:06,640 --> 03:40:10,960
console and this is to be expected
5821
03:40:08,880 --> 03:40:13,040
because as we've talked about before the
5822
03:40:10,960 --> 03:40:15,359
default value for an integer is indeed
5823
03:40:13,040 --> 03:40:17,399
zero so all this is doing when it says
5824
03:40:15,359 --> 03:40:19,479
is it's clearing it's just actually
5825
03:40:17,399 --> 03:40:21,880
assigning all of the positions the value
5826
03:40:19,479 --> 03:40:24,520
of zero and we can mimic this quite
5827
03:40:21,880 --> 03:40:26,080
easily through a for Loop so if we
5828
03:40:24,520 --> 03:40:28,800
comment this out and write ourselves as
5829
03:40:26,080 --> 03:40:31,439
a for Loop in I equals z and we want to
5830
03:40:28,800 --> 03:40:33,640
go to numbers. length and then simply
5831
03:40:31,439 --> 03:40:37,000
inside we just want to say numbers
5832
03:40:33,640 --> 03:40:39,560
square braet I equals to 0 what you can
5833
03:40:37,000 --> 03:40:41,520
also write is the word default this will
5834
03:40:39,560 --> 03:40:43,800
pick up the default value for the data
5835
03:40:41,520 --> 03:40:46,279
type that you specified you can type in
5836
03:40:43,800 --> 03:40:48,399
zero or you can type in default it's up
5837
03:40:46,279 --> 03:40:49,720
to you but I think in this case if
5838
03:40:48,399 --> 03:40:51,439
you're trying to reset it back to
5839
03:40:49,720 --> 03:40:53,640
default then you don't have to worry
5840
03:40:51,439 --> 03:40:56,239
about remembering it or any typos you
5841
03:40:53,640 --> 03:40:57,920
can just use the keyword default so if
5842
03:40:56,239 --> 03:41:00,359
we run this code now let's see what
5843
03:40:57,920 --> 03:41:02,239
happens so effectively we've mimicked
5844
03:41:00,359 --> 03:41:03,319
what this function is doing but let's
5845
03:41:02,239 --> 03:41:06,279
have a look and see what other
5846
03:41:03,319 --> 03:41:08,239
parameters we have access to so if we
5847
03:41:06,279 --> 03:41:10,319
comment this out and drop the value then
5848
03:41:08,239 --> 03:41:12,479
you can still see we only have one
5849
03:41:10,319 --> 03:41:14,479
Constructor normally you'd see the open
5850
03:41:12,479 --> 03:41:16,840
down arrows on the left hand side to say
5851
03:41:14,479 --> 03:41:18,960
one of two in this case we only have
5852
03:41:16,840 --> 03:41:21,000
access to one Constructor it will only
5853
03:41:18,960 --> 03:41:23,439
give us a range of elements we want to
5854
03:41:21,000 --> 03:41:25,479
remove but really we just want to clear
5855
03:41:23,439 --> 03:41:28,239
it does say here in the tool tape the
5856
03:41:25,479 --> 03:41:29,800
array whose elements need to be cleared
5857
03:41:28,239 --> 03:41:31,680
and because it is a clear and not a
5858
03:41:29,800 --> 03:41:33,520
remove you got to remember when using
5859
03:41:31,680 --> 03:41:35,800
this function it doesn't actually remove
5860
03:41:33,520 --> 03:41:37,960
any of the positions it just resets the
5861
03:41:35,800 --> 03:41:40,680
values inside the array so what we can
5862
03:41:37,960 --> 03:41:44,479
do in this case is we can say numbers
5863
03:41:40,680 --> 03:41:46,239
comma 5 comma 5 and what that will do is
5864
03:41:44,479 --> 03:41:48,560
the first param arer is where you want
5865
03:41:46,239 --> 03:41:50,760
to start so if you want to start at the
5866
03:41:48,560 --> 03:41:53,000
fifth position and then we want to count
5867
03:41:50,760 --> 03:41:54,840
five forward and because we have 10
5868
03:41:53,000 --> 03:41:57,199
numbers then this should return back
5869
03:41:54,840 --> 03:41:59,159
okay let's comment out this for Loop so
5870
03:41:57,199 --> 03:42:01,199
it doesn't take an effect and let's run
5871
03:41:59,159 --> 03:42:03,239
the code and see what happens now so now
5872
03:42:01,199 --> 03:42:04,920
we took the position five which is
5873
03:42:03,239 --> 03:42:07,399
actually the sixth number because it's
5874
03:42:04,920 --> 03:42:10,159
zero base and said we want to remove
5875
03:42:07,399 --> 03:42:12,600
five forward so it's s and then four
5876
03:42:10,159 --> 03:42:14,760
more forward and there we go we have
5877
03:42:12,600 --> 03:42:16,920
five numbers being removed so instead of
5878
03:42:14,760 --> 03:42:18,760
removing all of the different values we
5879
03:42:16,920 --> 03:42:21,159
can actually clear a certain amount that
5880
03:42:18,760 --> 03:42:23,520
we want and to mimic this in a for Loop
5881
03:42:21,159 --> 03:42:26,920
we can do the following we can start it
5882
03:42:23,520 --> 03:42:29,239
at five and we want to go to 10 and
5883
03:42:26,920 --> 03:42:32,279
let's run the code and you can see the
5884
03:42:29,239 --> 03:42:33,640
output is exactly the same when doing in
5885
03:42:32,279 --> 03:42:35,840
a for Loop you need to actually
5886
03:42:33,640 --> 03:42:37,920
calculate what this is going to be so we
5887
03:42:35,840 --> 03:42:40,840
need to take both the numbers the fifth
5888
03:42:37,920 --> 03:42:42,960
index and the five length which is 5 + 5
5889
03:42:40,840 --> 03:42:45,600
and then we get 10 and we don't make
5890
03:42:42,960 --> 03:42:47,520
this less than or equal to 10 because we
5891
03:42:45,600 --> 03:42:50,159
have 10 numbers so we actually want to
5892
03:42:47,520 --> 03:42:52,520
go from position five to position 9
5893
03:42:50,159 --> 03:42:54,319
which will equate to less than 10 we
5894
03:42:52,520 --> 03:42:56,560
actually want to go from position five
5895
03:42:54,319 --> 03:43:01,600
all the way to position 9 which actually
5896
03:42:56,560 --> 03:43:02,800
covers five numbers 5 6 7 8 9 even
5897
03:43:01,600 --> 03:43:05,040
though it feels like the difference
5898
03:43:02,800 --> 03:43:07,960
between the two numbers is only four
5899
03:43:05,040 --> 03:43:11,880
because 9 - 5 is 4 but we actually have
5900
03:43:07,960 --> 03:43:14,359
five numbers between that 1 2 3 4 5
5901
03:43:11,880 --> 03:43:16,800
there we go so when we print the values
5902
03:43:14,359 --> 03:43:18,840
from 5 to till less than 10 we're going
5903
03:43:16,800 --> 03:43:21,439
from 5 to 9 which is exactly what this
5904
03:43:18,840 --> 03:43:23,239
is doing this is the index that you
5905
03:43:21,439 --> 03:43:25,680
start at and this is how many numbers
5906
03:43:23,239 --> 03:43:27,840
you want to count forward so let's just
5907
03:43:25,680 --> 03:43:30,880
have a quick recap of this function we
5908
03:43:27,840 --> 03:43:32,680
defined our numbers array using array.
5909
03:43:30,880 --> 03:43:34,880
clear it doesn't actually clear the
5910
03:43:32,680 --> 03:43:36,560
values it just gives us access to
5911
03:43:34,880 --> 03:43:38,359
removing them back to their default
5912
03:43:36,560 --> 03:43:40,720
values it doesn't actually clear the
5913
03:43:38,359 --> 03:43:43,239
array it just resets the values back to
5914
03:43:40,720 --> 03:43:45,840
their default type we can have numbers
5915
03:43:43,239 --> 03:43:47,640
parameter which will equal to our array
5916
03:43:45,840 --> 03:43:50,120
and we want to say we want to go from
5917
03:43:47,640 --> 03:43:52,720
index zero all the way to numbers.
5918
03:43:50,120 --> 03:43:54,960
length which will be every single value
5919
03:43:52,720 --> 03:43:56,920
inside the array and all that will do is
5920
03:43:54,960 --> 03:43:59,159
go through the array and set them all to
5921
03:43:56,920 --> 03:44:01,199
their default value which is similar to
5922
03:43:59,159 --> 03:44:04,199
what this for Loop is doing and before
5923
03:44:01,199 --> 03:44:06,040
we had in I equals 0 and I is less than
5924
03:44:04,199 --> 03:44:07,560
numbers. length and that will go through
5925
03:44:06,040 --> 03:44:10,080
every single position you can
5926
03:44:07,560 --> 03:44:11,439
technically use zero instead of default
5927
03:44:10,080 --> 03:44:13,479
but that would mean you have to remember
5928
03:44:11,439 --> 03:44:15,800
the default value of every single data
5929
03:44:13,479 --> 03:44:17,840
type it's easier to just the default
5930
03:44:15,800 --> 03:44:20,319
keyword and it doesn't promote any
5931
03:44:17,840 --> 03:44:22,199
typers using the default keyword is by
5932
03:44:20,319 --> 03:44:23,520
far the easiest and the most safest
5933
03:44:22,199 --> 03:44:26,319
option if you're going to have this
5934
03:44:23,520 --> 03:44:28,560
situation so making every number's index
5935
03:44:26,319 --> 03:44:30,120
equal to default in this case because
5936
03:44:28,560 --> 03:44:32,319
we're using integers it's going to
5937
03:44:30,120 --> 03:44:34,960
assign the value of zero and then after
5938
03:44:32,319 --> 03:44:37,000
this follow loes run with I is zero and
5939
03:44:34,960 --> 03:44:38,720
I is less than numbers. length it's
5940
03:44:37,000 --> 03:44:41,120
going to make all of these positions
5941
03:44:38,720 --> 03:44:43,479
equal to zero which is in essence what
5942
03:44:41,120 --> 03:44:45,239
this function is here for because arrays
5943
03:44:43,479 --> 03:44:46,720
can't be added or changed in terms terms
5944
03:44:45,239 --> 03:44:49,080
of the values that they have after
5945
03:44:46,720 --> 03:44:51,439
they've been created you can only really
5946
03:44:49,080 --> 03:44:53,840
modify what's already there or you can
5947
03:44:51,439 --> 03:44:56,239
reset them all back to zero using this
5948
03:44:53,840 --> 03:44:58,080
notation from zero to numbers length you
5949
03:44:56,239 --> 03:45:01,080
can get rid of all the values and make
5950
03:44:58,080 --> 03:45:03,600
them all zero using 5 to five means it
5951
03:45:01,080 --> 03:45:05,359
will start index 5 which is the sixth
5952
03:45:03,600 --> 03:45:07,479
position and then go five numbers
5953
03:45:05,359 --> 03:45:09,800
forward so it will delete these and
5954
03:45:07,479 --> 03:45:11,800
reset them back to zero you can mimic
5955
03:45:09,800 --> 03:45:14,319
this in a follow Loop just like this
5956
03:45:11,800 --> 03:45:16,159
start in I at five and then go to less
5957
03:45:14,319 --> 03:45:18,920
than 10 and the reason why it's less
5958
03:45:16,159 --> 03:45:21,640
than 10 is 5 + 5 gives you the 10th
5959
03:45:18,920 --> 03:45:24,040
index but because it's I is less than 10
5960
03:45:21,640 --> 03:45:26,399
you actually want to go to the 9th index
5961
03:45:24,040 --> 03:45:28,680
because from 5 to 9 that's actually
5962
03:45:26,399 --> 03:45:30,600
covering five different numbers and then
5963
03:45:28,680 --> 03:45:32,640
we can go through and assign the default
5964
03:45:30,600 --> 03:45:34,159
value and then when we print it out
5965
03:45:32,640 --> 03:45:38,520
you'll see that all of these numbers
5966
03:45:34,159 --> 03:45:38,520
have turned into zeros just like
5967
03:45:38,840 --> 03:45:43,520
so searching through arrays are very
5968
03:45:41,439 --> 03:45:45,520
helpful when you try to find some data
5969
03:45:43,520 --> 03:45:47,399
inside your array and you want to return
5970
03:45:45,520 --> 03:45:48,960
back the position to maybe do something
5971
03:45:47,399 --> 03:45:51,279
else with it so let's have a look at
5972
03:45:48,960 --> 03:45:53,120
this example so we have a numbers array
5973
03:45:51,279 --> 03:45:56,640
and let's say we want to search number
5974
03:45:53,120 --> 03:45:58,640
199 and find the index of it so first
5975
03:45:56,640 --> 03:46:01,840
let's ask the user what the number they
5976
03:45:58,640 --> 03:46:04,399
want to search enter number to search
5977
03:46:01,840 --> 03:46:07,439
then we can have in search is equal to
5978
03:46:04,399 --> 03:46:09,359
convert to in32 and we're converting the
5979
03:46:07,439 --> 03:46:11,319
console.
5980
03:46:09,359 --> 03:46:13,319
readline and then let's have a look at
5981
03:46:11,319 --> 03:46:16,080
the function so inside the array Library
5982
03:46:13,319 --> 03:46:18,239
we have an index of surges for the
5983
03:46:16,080 --> 03:46:19,720
specified object and Returns the index
5984
03:46:18,239 --> 03:46:21,800
of the first occurrence in a
5985
03:46:19,720 --> 03:46:23,720
one-dimensional array this is very
5986
03:46:21,800 --> 03:46:25,720
important as it says the first
5987
03:46:23,720 --> 03:46:28,439
occurrence which means if there was
5988
03:46:25,720 --> 03:46:30,080
another 199 at the end it's only going
5989
03:46:28,439 --> 03:46:32,199
to return you back this one and then
5990
03:46:30,080 --> 03:46:34,800
stop immediately and not give you the
5991
03:46:32,199 --> 03:46:36,720
value of this one so let's have a look
5992
03:46:34,800 --> 03:46:39,199
in the parameters and see what it takes
5993
03:46:36,720 --> 03:46:41,040
it takes our array and then a value so
5994
03:46:39,199 --> 03:46:42,560
our array is called numbers and the
5995
03:46:41,040 --> 03:46:44,920
value you want to search by is our
5996
03:46:42,560 --> 03:46:47,000
variable called search if you hover over
5997
03:46:44,920 --> 03:46:49,720
the index function again it says that it
5998
03:46:47,000 --> 03:46:51,720
returns back at integer zerob based
5999
03:46:49,720 --> 03:46:53,680
index of the first occurrence of the
6000
03:46:51,720 --> 03:46:55,840
value in the entire array if it was
6001
03:46:53,680 --> 03:46:58,279
found otherwise the value is going to be
6002
03:46:55,840 --> 03:47:01,439
minus one so we can say inter position
6003
03:46:58,279 --> 03:47:03,159
is equal to this and because it says if
6004
03:47:01,439 --> 03:47:05,520
it's going to be found then it's going
6005
03:47:03,159 --> 03:47:07,040
to have the value of a zero index and if
6006
03:47:05,520 --> 03:47:09,920
it's not going to be found it's going to
6007
03:47:07,040 --> 03:47:12,680
have the value of minus1 so we can say
6008
03:47:09,920 --> 03:47:14,600
if the position is greater than minus1
6009
03:47:12,680 --> 03:47:18,080
because if it's greater than minus1 that
6010
03:47:14,600 --> 03:47:19,800
means the value is zero onwards we can
6011
03:47:18,080 --> 03:47:21,800
also write this by saying if the
6012
03:47:19,800 --> 03:47:24,080
position is greater than or equal to
6013
03:47:21,800 --> 03:47:26,640
zero it's just up to you they both mean
6014
03:47:24,080 --> 03:47:29,120
the same thing and then we can say in
6015
03:47:26,640 --> 03:47:34,199
here number
6016
03:47:29,120 --> 03:47:36,439
search has been found at position
6017
03:47:34,199 --> 03:47:39,359
position there we go so let's run the
6018
03:47:36,439 --> 03:47:41,960
code and let's do a perfect situation so
6019
03:47:39,359 --> 03:47:43,960
we can say 50 and you can see it says
6020
03:47:41,960 --> 03:47:46,319
number 50 has been found at position
6021
03:47:43,960 --> 03:47:48,680
three and that's the fourth index since
6022
03:47:46,319 --> 03:47:50,479
it's zero based and what you could do if
6023
03:47:48,680 --> 03:47:52,319
you wanted to is actually add one to
6024
03:47:50,479 --> 03:47:54,680
this just so it looks like a bit more of
6025
03:47:52,319 --> 03:47:59,479
a readable number because four is going
6026
03:47:54,680 --> 03:48:01,319
to be the fourth number 1 2 3 4 and
6027
03:47:59,479 --> 03:48:03,840
let's try find a number that hasn't came
6028
03:48:01,319 --> 03:48:05,800
up like 60 it's going to return us back
6029
03:48:03,840 --> 03:48:07,800
with nothing this is really bad because
6030
03:48:05,800 --> 03:48:09,560
it looks like the search has failed
6031
03:48:07,800 --> 03:48:11,840
which it has but you need to Output a
6032
03:48:09,560 --> 03:48:15,120
message to let the user know so have
6033
03:48:11,840 --> 03:48:16,880
cons right line and say number search
6034
03:48:15,120 --> 03:48:20,800
has not been
6035
03:48:16,880 --> 03:48:22,840
found let's type that again 60 number 60
6036
03:48:20,800 --> 03:48:24,680
has not been found so let's see what we
6037
03:48:22,840 --> 03:48:26,880
can do to be able to write this function
6038
03:48:24,680 --> 03:48:29,840
ourselves if we just block comment this
6039
03:48:26,880 --> 03:48:31,040
for the time being we can actually use
6040
03:48:29,840 --> 03:48:34,399
in
6041
03:48:31,040 --> 03:48:36,479
position equals to minus1 and the reason
6042
03:48:34,399 --> 03:48:38,120
why I make equal to minus1 because when
6043
03:48:36,479 --> 03:48:39,880
we read the tool tip with this index
6044
03:48:38,120 --> 03:48:41,960
function it said if it's not going to be
6045
03:48:39,880 --> 03:48:43,920
found it's set to minus one so why don't
6046
03:48:41,960 --> 03:48:46,800
we set it to minus one first and then we
6047
03:48:43,920 --> 03:48:50,680
can try and find it so inti equals z and
6048
03:48:46,800 --> 03:48:52,439
we want to go to numbers. length and
6049
03:48:50,680 --> 03:48:54,720
inside here we want to scan each of the
6050
03:48:52,439 --> 03:48:57,080
positions so we can say numbers square
6051
03:48:54,720 --> 03:48:59,319
braet I and if that is equal to our
6052
03:48:57,080 --> 03:49:02,159
search then we can set the position
6053
03:48:59,319 --> 03:49:04,439
equal to I if the number we're up to is
6054
03:49:02,159 --> 03:49:06,640
equal to what the user has typed in then
6055
03:49:04,439 --> 03:49:08,279
we want to set our position to the index
6056
03:49:06,640 --> 03:49:09,920
we're currently up to and we don't need
6057
03:49:08,279 --> 03:49:12,960
to have an else because we've already
6058
03:49:09,920 --> 03:49:14,359
set it to minus one and after the for
6059
03:49:12,960 --> 03:49:16,279
Loop we can actually take this if
6060
03:49:14,359 --> 03:49:18,279
statement and put it back down here
6061
03:49:16,279 --> 03:49:20,720
because the code's going to be the same
6062
03:49:18,279 --> 03:49:23,000
if the position is greater than minus1
6063
03:49:20,720 --> 03:49:25,800
which in this case is our default value
6064
03:49:23,000 --> 03:49:28,439
so in essence if this value has changed
6065
03:49:25,800 --> 03:49:31,040
then this should always pass EG we've
6066
03:49:28,439 --> 03:49:34,680
found the number so let's run the code
6067
03:49:31,040 --> 03:49:37,520
and let's type a number in 30 now it's
6068
03:49:34,680 --> 03:49:41,399
found at position five now if we try 60
6069
03:49:37,520 --> 03:49:44,319
again number 60 has not been found there
6070
03:49:41,399 --> 03:49:46,439
we go so this function is exactly doing
6071
03:49:44,319 --> 03:49:48,479
some something similar to this it's
6072
03:49:46,439 --> 03:49:50,800
going through every single position
6073
03:49:48,479 --> 03:49:53,120
finding the variable and if they match
6074
03:49:50,800 --> 03:49:55,080
assigning the index and then after just
6075
03:49:53,120 --> 03:49:57,359
returning back to position back to the
6076
03:49:55,080 --> 03:49:59,000
console so let's comment our example and
6077
03:49:57,359 --> 03:50:00,920
let's see what other variants of the
6078
03:49:59,000 --> 03:50:02,120
function we can use and let's comment
6079
03:50:00,920 --> 03:50:05,399
back the first
6080
03:50:02,120 --> 03:50:07,880
one so using this line we can duplicate
6081
03:50:05,399 --> 03:50:10,080
it and just comment it so we have some
6082
03:50:07,880 --> 03:50:12,159
other parameters inside here if we add a
6083
03:50:10,080 --> 03:50:14,720
comma we can actually give it a start
6084
03:50:12,159 --> 03:50:17,000
Index this is the starting index of the
6085
03:50:14,720 --> 03:50:19,359
search and let's say you only want to
6086
03:50:17,000 --> 03:50:21,359
search from this second number onwards
6087
03:50:19,359 --> 03:50:22,960
then you can just type in the number two
6088
03:50:21,359 --> 03:50:25,800
so now if you've run the code and type
6089
03:50:22,960 --> 03:50:28,080
in 90 it's going to say 90 is not found
6090
03:50:25,800 --> 03:50:31,000
cuz 90 is the first one and let's try a
6091
03:50:28,080 --> 03:50:33,399
value like 50 now it says the number 50
6092
03:50:31,000 --> 03:50:35,239
is being found at position 4 because now
6093
03:50:33,399 --> 03:50:37,600
it's included in the range from two
6094
03:50:35,239 --> 03:50:39,640
onwards because this is two onwards
6095
03:50:37,600 --> 03:50:41,800
it'll be position two until the end of
6096
03:50:39,640 --> 03:50:43,439
the array if you wanted a very specific
6097
03:50:41,800 --> 03:50:45,359
range then you can actually give it
6098
03:50:43,439 --> 03:50:47,760
another value so so we can say we want
6099
03:50:45,359 --> 03:50:50,439
to go from zero and then we only want to
6100
03:50:47,760 --> 03:50:53,680
go two forward so if you run that now
6101
03:50:50,439 --> 03:50:57,040
and type in 22 it say is not being found
6102
03:50:53,680 --> 03:50:59,920
cuz we' started here and we said 1 2 so
6103
03:50:57,040 --> 03:51:02,080
now it's only searching between 90 and
6104
03:50:59,920 --> 03:51:04,000
199 and if you wanted to include the
6105
03:51:02,080 --> 03:51:06,439
middle ones you could say you want to
6106
03:51:04,000 --> 03:51:08,439
start a position one and go two forward
6107
03:51:06,439 --> 03:51:10,920
so now you get these two numbers and so
6108
03:51:08,439 --> 03:51:13,319
on and so forth so now if we try number
6109
03:51:10,920 --> 03:51:15,680
90 you'll see it's not being found
6110
03:51:13,319 --> 03:51:17,479
because 90 is not one of these two the
6111
03:51:15,680 --> 03:51:20,279
reason why you'd use different ones like
6112
03:51:17,479 --> 03:51:23,080
this if I revert back the first
6113
03:51:20,279 --> 03:51:25,080
one this will go from two till the end
6114
03:51:23,080 --> 03:51:27,120
this will go specific range and this
6115
03:51:25,080 --> 03:51:29,560
will go through everything the reason
6116
03:51:27,120 --> 03:51:31,960
why these exist is if you have a very
6117
03:51:29,560 --> 03:51:34,159
very big array let's say it's a th000
6118
03:51:31,960 --> 03:51:36,399
indexes then you don't want to search
6119
03:51:34,159 --> 03:51:37,920
everything unless you really need to
6120
03:51:36,399 --> 03:51:40,239
let's say for example you had a nam's
6121
03:51:37,920 --> 03:51:41,800
array and it was sorted alphabetically
6122
03:51:40,239 --> 03:51:44,120
and the person's name that you wanted to
6123
03:51:41,800 --> 03:51:45,840
search began with an a there is no point
6124
03:51:44,120 --> 03:51:48,479
in running the search all the way from a
6125
03:51:45,840 --> 03:51:50,560
to zed if you know it begins with a and
6126
03:51:48,479 --> 03:51:53,640
let's say for example you found the
6127
03:51:50,560 --> 03:51:56,640
first index of the user that has a name
6128
03:51:53,640 --> 03:51:59,439
B so you have now the indexes from the
6129
03:51:56,640 --> 03:52:02,120
start of the array up until the last
6130
03:51:59,439 --> 03:52:04,439
array position that has the value of
6131
03:52:02,120 --> 03:52:06,840
someone's name beginning with a so now
6132
03:52:04,439 --> 03:52:10,560
you have the two indexes of every single
6133
03:52:06,840 --> 03:52:12,920
a name inside this array if this was a
6134
03:52:10,560 --> 03:52:15,120
this was B and this was C and this was D
6135
03:52:12,920 --> 03:52:17,279
and you had these two positions then you
6136
03:52:15,120 --> 03:52:19,159
should only be searching all of the a
6137
03:52:17,279 --> 03:52:21,239
names instead of searching the entire
6138
03:52:19,159 --> 03:52:23,239
array and the reason for that is you'll
6139
03:52:21,239 --> 03:52:24,960
be searching lots more data for no
6140
03:52:23,239 --> 03:52:27,000
apparent reason and it's going to take
6141
03:52:24,960 --> 03:52:28,080
an extra long time for searching
6142
03:52:27,000 --> 03:52:29,880
something that you're never going to
6143
03:52:28,080 --> 03:52:31,640
find if you're searching for a name that
6144
03:52:29,880 --> 03:52:34,279
begins with a there is no point in
6145
03:52:31,640 --> 03:52:36,479
searching for anything A B C D and so on
6146
03:52:34,279 --> 03:52:38,600
and so forth that's where you'd use the
6147
03:52:36,479 --> 03:52:40,760
very specific range you can say you want
6148
03:52:38,600 --> 03:52:42,399
to go from zero all the way up to the
6149
03:52:40,760 --> 03:52:44,880
first B name so then you're only
6150
03:52:42,399 --> 03:52:47,319
searching the a names the this is just
6151
03:52:44,880 --> 03:52:49,800
one example and there is a lot more
6152
03:52:47,319 --> 03:52:52,000
every situation is very unique so use
6153
03:52:49,800 --> 03:52:54,560
the parameters that you want and always
6154
03:52:52,000 --> 03:52:56,720
use the ones that can get you the best
6155
03:52:54,560 --> 03:52:58,640
performance the least amount of things
6156
03:52:56,720 --> 03:53:00,600
that you can search is the best way
6157
03:52:58,640 --> 03:53:02,000
forward if you don't know what you're
6158
03:53:00,600 --> 03:53:03,920
going to be searching then you should
6159
03:53:02,000 --> 03:53:06,080
always use the top one which will just
6160
03:53:03,920 --> 03:53:08,199
search everything if you know where the
6161
03:53:06,080 --> 03:53:11,199
value is going to be or roughly going to
6162
03:53:08,199 --> 03:53:13,120
be then you can use this one or this one
6163
03:53:11,199 --> 03:53:14,960
to give you a better range and it's a
6164
03:53:13,120 --> 03:53:17,279
bit more focused instead of searching
6165
03:53:14,960 --> 03:53:19,279
for everything so a quick recap of this
6166
03:53:17,279 --> 03:53:21,279
index function what it will do is it
6167
03:53:19,279 --> 03:53:23,760
will return you back to position if it's
6168
03:53:21,279 --> 03:53:26,080
found it the position will be zero based
6169
03:53:23,760 --> 03:53:27,880
just as arrays are if it hasn't found
6170
03:53:26,080 --> 03:53:30,040
the position then it's going to return
6171
03:53:27,880 --> 03:53:32,000
this variable with minus one so
6172
03:53:30,040 --> 03:53:34,159
therefore after you've run the code you
6173
03:53:32,000 --> 03:53:36,439
can check if the value is greater than
6174
03:53:34,159 --> 03:53:38,960
minus one which will be from zero
6175
03:53:36,439 --> 03:53:40,920
onwards if the value position is zero
6176
03:53:38,960 --> 03:53:42,840
onwards then you know that that number
6177
03:53:40,920 --> 03:53:44,680
has been found and you can use the
6178
03:53:42,840 --> 03:53:47,479
position variable to Output that to the
6179
03:53:44,680 --> 03:53:50,080
screen if the value position is minus1
6180
03:53:47,479 --> 03:53:52,080
it means the array index function has
6181
03:53:50,080 --> 03:53:54,199
returned back negative and the value
6182
03:53:52,080 --> 03:53:56,319
position is now minus one so you can
6183
03:53:54,199 --> 03:53:58,560
output to the user that it has not been
6184
03:53:56,319 --> 03:54:00,800
found just in my little explanation down
6185
03:53:58,560 --> 03:54:02,560
below you can see that we can mimic this
6186
03:54:00,800 --> 03:54:05,520
function by setting the value of
6187
03:54:02,560 --> 03:54:07,800
position to minus one using a fall Loop
6188
03:54:05,520 --> 03:54:09,439
that goes from zero to numbers length
6189
03:54:07,800 --> 03:54:12,239
and if you want to two as well you could
6190
03:54:09,439 --> 03:54:14,760
use these inside here if you wanted to
6191
03:54:12,239 --> 03:54:17,239
mimic this this goes from two till the
6192
03:54:14,760 --> 03:54:19,439
end so you can say I equals 2 and you
6193
03:54:17,239 --> 03:54:22,040
still want to go to the length this one
6194
03:54:19,439 --> 03:54:24,840
goes from one and then two ahead so you
6195
03:54:22,040 --> 03:54:27,199
can actually put I equal 1 and then
6196
03:54:24,840 --> 03:54:29,720
because it's two ahead from one instead
6197
03:54:27,199 --> 03:54:32,960
of numbers. length it will just be I is
6198
03:54:29,720 --> 03:54:35,319
less than 3 because 1 + 2 gives you the
6199
03:54:32,960 --> 03:54:36,760
three so in order to do this manually
6200
03:54:35,319 --> 03:54:38,880
you can Lube through the elements that
6201
03:54:36,760 --> 03:54:41,760
you want check if the number you're up
6202
03:54:38,880 --> 03:54:43,439
to based off the index position of I is
6203
03:54:41,760 --> 03:54:45,960
equal to the search variable that the
6204
03:54:43,439 --> 03:54:48,000
user typed in of here and if you find a
6205
03:54:45,960 --> 03:54:50,279
match then you can assign the position
6206
03:54:48,000 --> 03:54:52,640
to the value of I after the for Loop is
6207
03:54:50,279 --> 03:54:54,920
run you can check the same if statement
6208
03:54:52,640 --> 03:54:56,600
as we discussed above these are copy and
6209
03:54:54,920 --> 03:54:59,640
pasted down below just to give you an
6210
03:54:56,600 --> 03:54:59,640
example in this little
6211
03:55:00,080 --> 03:55:04,159
section all these arrays have static
6212
03:55:02,520 --> 03:55:05,920
data which means that when you
6213
03:55:04,159 --> 03:55:07,520
initialize them with a certain length
6214
03:55:05,920 --> 03:55:09,760
you can't increase that length or
6215
03:55:07,520 --> 03:55:11,640
decrease the length you can only modify
6216
03:55:09,760 --> 03:55:14,319
the data inside it and you can't delete
6217
03:55:11,640 --> 03:55:16,560
any rows if you create an array of size
6218
03:55:14,319 --> 03:55:18,840
is five and you only stall three people
6219
03:55:16,560 --> 03:55:21,359
in then now you have two positions that
6220
03:55:18,840 --> 03:55:23,279
are being unused and that's unnecessary
6221
03:55:21,359 --> 03:55:25,359
amount of data stored in your memory if
6222
03:55:23,279 --> 03:55:27,000
you're not going to be using it up in
6223
03:55:25,359 --> 03:55:29,960
certain situations like when you're
6224
03:55:27,000 --> 03:55:31,159
making a game for example Tic Tac Toe
6225
03:55:29,960 --> 03:55:35,239
then you know you're always going to
6226
03:55:31,159 --> 03:55:37,399
need a 3X3 grid so a 2d array will be
6227
03:55:35,239 --> 03:55:39,920
perfect in this situation cuz you know
6228
03:55:37,399 --> 03:55:42,560
the exact width and height of your array
6229
03:55:39,920 --> 03:55:44,680
so you can do this perfectly whereas
6230
03:55:42,560 --> 03:55:46,080
lists are quite Dynamic if if you don't
6231
03:55:44,680 --> 03:55:48,120
know how many people are going to be
6232
03:55:46,080 --> 03:55:51,120
within a system for example if you're
6233
03:55:48,120 --> 03:55:52,880
signing up some sort of register for a
6234
03:55:51,120 --> 03:55:54,680
class or for a school or something of
6235
03:55:52,880 --> 03:55:56,560
the sort and you don't know how many
6236
03:55:54,680 --> 03:55:58,439
people are going to be in that class
6237
03:55:56,560 --> 03:56:00,479
then you can use something called a list
6238
03:55:58,439 --> 03:56:02,520
and list a dynamic cuz what you do when
6239
03:56:00,479 --> 03:56:05,279
you initialize it you just say that you
6240
03:56:02,520 --> 03:56:07,399
simply want a list and you can add items
6241
03:56:05,279 --> 03:56:10,439
and you can remove items and you can
6242
03:56:07,399 --> 03:56:12,479
also modify them if you wanted to expand
6243
03:56:10,439 --> 03:56:14,760
the size of an array it's slightly
6244
03:56:12,479 --> 03:56:17,880
complicated but it's not impossible
6245
03:56:14,760 --> 03:56:19,800
so if we had int numbers equals new int
6246
03:56:17,880 --> 03:56:21,840
and let's make that length four and
6247
03:56:19,800 --> 03:56:24,000
let's say we had a numbers array that we
6248
03:56:21,840 --> 03:56:26,880
wanted to increase to length
6249
03:56:24,000 --> 03:56:28,760
five we could have new numbers change
6250
03:56:26,880 --> 03:56:30,720
that position five and then what we're
6251
03:56:28,760 --> 03:56:33,120
going to have to do is from position
6252
03:56:30,720 --> 03:56:35,680
zero all the way to three which is our
6253
03:56:33,120 --> 03:56:38,359
fourth length from the numbers array
6254
03:56:35,680 --> 03:56:40,800
we're going to have to copy that into 0
6255
03:56:38,359 --> 03:56:43,159
to three of the new numbers array so now
6256
03:56:40,800 --> 03:56:45,439
they have the extra data plus the last
6257
03:56:43,159 --> 03:56:47,840
one which will give us our four so
6258
03:56:45,439 --> 03:56:50,279
that'll be five length 0 to three gives
6259
03:56:47,840 --> 03:56:52,720
us our four numbers and then we have one
6260
03:56:50,279 --> 03:56:54,199
empty one so we've just made a new array
6261
03:56:52,720 --> 03:56:56,399
and then copied all the data from
6262
03:56:54,199 --> 03:56:57,880
numbers into new numbers now of course
6263
03:56:56,399 --> 03:56:59,960
this is not really practical that if you
6264
03:56:57,880 --> 03:57:01,279
want to reduce the size or increase the
6265
03:56:59,960 --> 03:57:03,040
size you're going to have to do this
6266
03:57:01,279 --> 03:57:04,359
each time of course you don't want to do
6267
03:57:03,040 --> 03:57:06,080
this you just want to use something
6268
03:57:04,359 --> 03:57:08,199
called a list so let's stick with an
6269
03:57:06,080 --> 03:57:09,560
example and see what we can do so you
6270
03:57:08,199 --> 03:57:12,120
make this length
6271
03:57:09,560 --> 03:57:13,680
three and let's just put three numbers
6272
03:57:12,120 --> 03:57:15,920
inside here if you remember from the
6273
03:57:13,680 --> 03:57:20,239
array video we can also declare it like
6274
03:57:15,920 --> 03:57:22,560
this we can say number 0al 1 and then 1
6275
03:57:20,239 --> 03:57:24,199
and then two and change these values
6276
03:57:22,560 --> 03:57:26,439
these two are doing exactly the same
6277
03:57:24,199 --> 03:57:28,479
thing I'm initializing it with 1 2 and
6278
03:57:26,439 --> 03:57:30,479
three and also just saying that all the
6279
03:57:28,479 --> 03:57:32,319
positions in here are equal to 1 2 and
6280
03:57:30,479 --> 03:57:34,880
three so we don't need these twice but I
6281
03:57:32,319 --> 03:57:36,920
was just showing you as a quick recap so
6282
03:57:34,880 --> 03:57:38,560
we're declaring array we're calling it
6283
03:57:36,920 --> 03:57:41,159
numbers and we're giving it a length of
6284
03:57:38,560 --> 03:57:43,680
three and initializing it to 1 2 and
6285
03:57:41,159 --> 03:57:44,920
three now let's see the syntax to define
6286
03:57:43,680 --> 03:57:46,560
a list
6287
03:57:44,920 --> 03:57:48,239
you start off with a word list and then
6288
03:57:46,560 --> 03:57:50,000
you have a left and a right arrow which
6289
03:57:48,239 --> 03:57:51,439
makes a little section inside the
6290
03:57:50,000 --> 03:57:53,239
section you actually want to State the
6291
03:57:51,439 --> 03:57:55,040
data type that you want so if we're
6292
03:57:53,239 --> 03:57:57,640
copying the numbers array we can have an
6293
03:57:55,040 --> 03:57:59,720
INT inside here and that's it for the
6294
03:57:57,640 --> 03:58:02,880
Declaration of the data type and then we
6295
03:57:59,720 --> 03:58:06,080
can say new numbers or List numbers
6296
03:58:02,880 --> 03:58:08,159
equals to new and then there you go it
6297
03:58:06,080 --> 03:58:11,279
gets Auto filled for you but let's just
6298
03:58:08,159 --> 03:58:13,560
type it in we have list open the arrow
6299
03:58:11,279 --> 03:58:16,000
in in the middle and then close it so
6300
03:58:13,560 --> 03:58:17,600
now it's the same same on both sides but
6301
03:58:16,000 --> 03:58:19,520
because this is a new we need to add a
6302
03:58:17,600 --> 03:58:21,960
Constructor so we can open the curly
6303
03:58:19,520 --> 03:58:23,680
braces and the first Constructor says
6304
03:58:21,960 --> 03:58:25,600
initialize a new instance of the list
6305
03:58:23,680 --> 03:58:28,040
class that is empty and has the default
6306
03:58:25,600 --> 03:58:30,080
initial capacity press the down arrow we
6307
03:58:28,040 --> 03:58:31,800
can give it a capacity of the number of
6308
03:58:30,080 --> 03:58:34,159
elements that the new list can initially
6309
03:58:31,800 --> 03:58:36,520
store so we can start it off with five
6310
03:58:34,159 --> 03:58:38,239
length or three length or whatever it
6311
03:58:36,520 --> 03:58:39,800
doesn't actually matter it just means
6312
03:58:38,239 --> 03:58:42,000
that we actually have three positions
6313
03:58:39,800 --> 03:58:44,359
available immediately if we place three
6314
03:58:42,000 --> 03:58:46,399
in the brackets
6315
03:58:44,359 --> 03:58:48,600
and the last one we can actually give it
6316
03:58:46,399 --> 03:58:50,760
entire collection so let's say if we
6317
03:58:48,600 --> 03:58:53,439
loaded a list of numbers from a database
6318
03:58:50,760 --> 03:58:55,279
or a list of something from database and
6319
03:58:53,439 --> 03:58:57,760
we can actually insert it straight into
6320
03:58:55,279 --> 03:58:59,920
this list by putting a list inside these
6321
03:58:57,760 --> 03:59:01,199
brackets of course right now we don't
6322
03:58:59,920 --> 03:59:03,159
have a list that we're going to assign
6323
03:59:01,199 --> 03:59:04,960
it with so let's just leave this empty
6324
03:59:03,159 --> 03:59:07,840
and close the brackets and add a
6325
03:59:04,960 --> 03:59:10,319
semicolon so we could say list numbers
6326
03:59:07,840 --> 03:59:12,800
do add and then we can add one and then
6327
03:59:10,319 --> 03:59:15,040
duplicate this down and add two and add
6328
03:59:12,800 --> 03:59:17,840
three so now these do have exactly the
6329
03:59:15,040 --> 03:59:20,040
same contents instead of doing this you
6330
03:59:17,840 --> 03:59:22,319
can have the same notation like this so
6331
03:59:20,040 --> 03:59:25,319
you can have the curly braces in
6332
03:59:22,319 --> 03:59:27,640
here and we can put in 1 two and three
6333
03:59:25,319 --> 03:59:29,399
now that's doing exactly the same thing
6334
03:59:27,640 --> 03:59:31,159
and you see I can just keep adding more
6335
03:59:29,399 --> 03:59:33,399
numbers in here and the list simply
6336
03:59:31,159 --> 03:59:35,080
doesn't care but as soon as I add one
6337
03:59:33,399 --> 03:59:37,159
more into here then I'm going to get a
6338
03:59:35,080 --> 03:59:39,040
syntax error because it complains that
6339
03:59:37,159 --> 03:59:39,840
the length is not equal to the numbers
6340
03:59:39,040 --> 03:59:42,399
that we
6341
03:59:39,840 --> 03:59:43,720
have because the list are really Dynamic
6342
03:59:42,399 --> 03:59:45,840
it doesn't matter how many you put in
6343
03:59:43,720 --> 03:59:47,640
here because it can just go on forever
6344
03:59:45,840 --> 03:59:49,279
of course until you run out of memory so
6345
03:59:47,640 --> 03:59:51,600
let's have a little working example just
6346
03:59:49,279 --> 03:59:53,840
to see how this
6347
03:59:51,600 --> 03:59:56,399
works so we can have a for Loop and we
6348
03:59:53,840 --> 03:59:57,680
can go from 0 to 10 or maybe 0 to three
6349
03:59:56,399 --> 03:59:59,760
just so we don't have to enter as many
6350
03:59:57,680 --> 04:00:02,479
values we can have a console right that
6351
03:59:59,760 --> 04:00:04,199
kind of just says enter a number and
6352
04:00:02,479 --> 04:00:08,159
then let's read that in and we can say
6353
04:00:04,199 --> 04:00:11,560
int n equals convert to in32 and we can
6354
04:00:08,159 --> 04:00:13,399
place a console do readline inside
6355
04:00:11,560 --> 04:00:15,640
here and then after we've got the
6356
04:00:13,399 --> 04:00:17,560
console read line we've got an inn
6357
04:00:15,640 --> 04:00:20,080
that's being converted from the console
6358
04:00:17,560 --> 04:00:22,439
input inside our integer num and from
6359
04:00:20,080 --> 04:00:25,040
there we can take our list numbers do
6360
04:00:22,439 --> 04:00:26,760
adds and we can add the num inside there
6361
04:00:25,040 --> 04:00:28,800
or if you want to reduce a variable you
6362
04:00:26,760 --> 04:00:31,520
can just take this line and place it
6363
04:00:28,800 --> 04:00:33,680
straight into here now if you get rid of
6364
04:00:31,520 --> 04:00:35,840
that when this follow Loop runs we'll
6365
04:00:33,680 --> 04:00:39,239
have three iterations let's run the code
6366
04:00:35,840 --> 04:00:40,640
and find out 1 2 three so now we've
6367
04:00:39,239 --> 04:00:43,080
entered three numbers and now they've
6368
04:00:40,640 --> 04:00:44,960
been stored in the list so let's create
6369
04:00:43,080 --> 04:00:47,720
a follow loop after and we can go from
6370
04:00:44,960 --> 04:00:49,880
zero all the way to list numbers do
6371
04:00:47,720 --> 04:00:52,960
length but of course when we're using
6372
04:00:49,880 --> 04:00:55,359
list we don't have a length anymore when
6373
04:00:52,960 --> 04:00:57,479
we're using arrays we have length
6374
04:00:55,359 --> 04:00:59,800
because arrays have a fixed length
6375
04:00:57,479 --> 04:01:02,520
whereas lists have a count of the items
6376
04:00:59,800 --> 04:01:05,880
inside it so instead of length we simply
6377
04:01:02,520 --> 04:01:07,600
use do count and then inside here we
6378
04:01:05,880 --> 04:01:10,279
could just do what we normally do and
6379
04:01:07,600 --> 04:01:12,000
say list numbers square braet I and
6380
04:01:10,279 --> 04:01:13,920
let's run that and we should see the
6381
04:01:12,000 --> 04:01:16,560
numbers printed back out in the exam
6382
04:01:13,920 --> 04:01:19,080
exact same order 1 2 and 3 and the same
6383
04:01:16,560 --> 04:01:21,760
principle goes if you user for each Loop
6384
04:01:19,080 --> 04:01:23,199
we can say V item inside list numbers
6385
04:01:21,760 --> 04:01:25,359
and that's all you need to do and you
6386
04:01:23,199 --> 04:01:27,680
can just print item to the screen now we
6387
04:01:25,359 --> 04:01:29,399
should see the same output twice let's
6388
04:01:27,680 --> 04:01:31,800
have a look at other values that we have
6389
04:01:29,399 --> 04:01:33,239
inside here so we type in list numbers
6390
04:01:31,800 --> 04:01:35,560
Dot and let's have a look at the
6391
04:01:33,239 --> 04:01:37,720
function inside so we can add which you
6392
04:01:35,560 --> 04:01:40,640
just used above we can convert it to an
6393
04:01:37,720 --> 04:01:42,680
array we can remove a specific position
6394
04:01:40,640 --> 04:01:45,080
and we can clear it completely they have
6395
04:01:42,680 --> 04:01:46,560
other ones in here like B researchers we
6396
04:01:45,080 --> 04:01:48,159
can check if it contains a certain
6397
04:01:46,560 --> 04:01:49,439
element this is going to be very handy
6398
04:01:48,159 --> 04:01:51,720
when we do our
6399
04:01:49,439 --> 04:01:54,560
remove we can convert them all to a
6400
04:01:51,720 --> 04:01:56,840
different type we have a copy to we
6401
04:01:54,560 --> 04:01:59,800
check if a value exists
6402
04:01:56,840 --> 04:02:01,479
inside we have a find finding all of
6403
04:01:59,800 --> 04:02:04,120
them the difference between these two is
6404
04:02:01,479 --> 04:02:05,840
the find will return back the first one
6405
04:02:04,120 --> 04:02:08,640
whereas find all will retrieve all the
6406
04:02:05,840 --> 04:02:11,000
elements that match it finding the index
6407
04:02:08,640 --> 04:02:13,520
of a specific match finding the last
6408
04:02:11,000 --> 04:02:16,040
index iterating through them all you can
6409
04:02:13,520 --> 04:02:17,880
insert a specific position whereas the
6410
04:02:16,040 --> 04:02:20,199
add will just insert at the end of the
6411
04:02:17,880 --> 04:02:23,199
list adds an object to the end of the
6412
04:02:20,199 --> 04:02:26,800
list inserts an element to the list at a
6413
04:02:23,199 --> 04:02:28,640
specified index and then you have remove
6414
04:02:26,800 --> 04:02:30,359
which gives you the first occurrence so
6415
04:02:28,640 --> 04:02:32,520
you can type the number straight into
6416
04:02:30,359 --> 04:02:34,600
there so you can ask the user what
6417
04:02:32,520 --> 04:02:36,840
number do you want to delete and if they
6418
04:02:34,600 --> 04:02:39,120
type zero it will delete the first zero
6419
04:02:36,840 --> 04:02:41,120
in the list and you can remove all so if
6420
04:02:39,120 --> 04:02:43,279
you want to remove every single zero
6421
04:02:41,120 --> 04:02:45,720
inside the list you can do that as well
6422
04:02:43,279 --> 04:02:47,600
or you can move at a specific position
6423
04:02:45,720 --> 04:02:48,640
this is the most used function because
6424
04:02:47,600 --> 04:02:51,279
what you can
6425
04:02:48,640 --> 04:02:53,199
do and what you can do is use the find
6426
04:02:51,279 --> 04:02:55,600
index search for an element that matches
6427
04:02:53,199 --> 04:02:58,199
a condition and then returns to zerob
6428
04:02:55,600 --> 04:03:00,000
based index of the first occurrent so if
6429
04:02:58,199 --> 04:03:02,439
you want to delete the number zero from
6430
04:03:00,000 --> 04:03:04,800
the list you could use find index and
6431
04:03:02,439 --> 04:03:06,880
that returns you back with an INT and
6432
04:03:04,800 --> 04:03:09,040
then you can use the remove at and then
6433
04:03:06,880 --> 04:03:11,239
pass that index straight into there you
6434
04:03:09,040 --> 04:03:13,399
can also sort it so it goes up in number
6435
04:03:11,239 --> 04:03:15,520
order we did this with the array. sort
6436
04:03:13,399 --> 04:03:17,760
function as well and if you wanted to
6437
04:03:15,520 --> 04:03:19,520
you can also reverse the entire list
6438
04:03:17,760 --> 04:03:21,479
this can be very helpful as we did with
6439
04:03:19,520 --> 04:03:23,319
the array functions if you just wanted
6440
04:03:21,479 --> 04:03:25,080
to reverse them all in case someone had
6441
04:03:23,319 --> 04:03:27,439
something like a filter when you're
6442
04:03:25,080 --> 04:03:29,319
doing from Price high to low and low to
6443
04:03:27,439 --> 04:03:31,800
high the difference between them two
6444
04:03:29,319 --> 04:03:33,880
lists is simply the list flipped so
6445
04:03:31,800 --> 04:03:35,600
let's run a remove just inside here and
6446
04:03:33,880 --> 04:03:37,920
let's see what happens so we can say
6447
04:03:35,600 --> 04:03:41,199
remove that and let's say position
6448
04:03:37,920 --> 04:03:42,920
zero so we type in one two and three
6449
04:03:41,199 --> 04:03:44,920
you'll see that one two three gets
6450
04:03:42,920 --> 04:03:47,040
printed out and then I'll hit this
6451
04:03:44,920 --> 04:03:49,159
remove at and it will delete the first
6452
04:03:47,040 --> 04:03:51,199
index and in this case the first index
6453
04:03:49,159 --> 04:03:53,319
is going to be our number one so now
6454
04:03:51,199 --> 04:03:55,479
this has became our first index and this
6455
04:03:53,319 --> 04:03:57,720
is our second index and it's got rid of
6456
04:03:55,479 --> 04:04:00,319
the number
6457
04:03:57,720 --> 04:04:02,040
one so let's move on to different data
6458
04:04:00,319 --> 04:04:03,680
structure called dictionaries
6459
04:04:02,040 --> 04:04:05,760
dictionaries are generic collections
6460
04:04:03,680 --> 04:04:08,239
that store key value pairs in no
6461
04:04:05,760 --> 04:04:10,840
particular order they take in a t key
6462
04:04:08,239 --> 04:04:13,000
and a t value and these just mean the
6463
04:04:10,840 --> 04:04:14,680
key and the value can be any data type
6464
04:04:13,000 --> 04:04:16,479
that you want dictionaries come under
6465
04:04:14,680 --> 04:04:18,359
the system. collections. generic
6466
04:04:16,479 --> 04:04:20,239
namespace and if you don't include this
6467
04:04:18,359 --> 04:04:23,680
line at the top then you'll have to type
6468
04:04:20,239 --> 04:04:25,520
in system. collections do generic space
6469
04:04:23,680 --> 04:04:27,560
dictionary every single time you want to
6470
04:04:25,520 --> 04:04:29,080
specify dictionary putting the name
6471
04:04:27,560 --> 04:04:30,840
space at the top will include it within
6472
04:04:29,080 --> 04:04:33,040
the project and you can use dictionaries
6473
04:04:30,840 --> 04:04:34,920
freely so one thing to know is the keys
6474
04:04:33,040 --> 04:04:37,479
must be unique and they cannot either be
6475
04:04:34,920 --> 04:04:39,640
null values can be null or duplicate cuz
6476
04:04:37,479 --> 04:04:41,239
they're the values depending on the key
6477
04:04:39,640 --> 04:04:43,199
as long as the keys aren't the same then
6478
04:04:41,239 --> 04:04:45,120
the values can be similar so let's have
6479
04:04:43,199 --> 04:04:47,960
a look and see what we can do we can
6480
04:04:45,120 --> 04:04:50,000
type in dictionary and you can see it
6481
04:04:47,960 --> 04:04:52,800
has the arrows either side and you can
6482
04:04:50,000 --> 04:04:55,000
see here it says T key and T value and
6483
04:04:52,800 --> 04:04:56,960
what T key and T value is is it gives us
6484
04:04:55,000 --> 04:04:58,760
the flexibility to add any data types
6485
04:04:56,960 --> 04:05:00,800
that we want we can add in two strings
6486
04:04:58,760 --> 04:05:02,560
in here for example or we can do
6487
04:05:00,800 --> 04:05:04,359
anything that we please so in this
6488
04:05:02,560 --> 04:05:07,159
example I just want to start a list of
6489
04:05:04,359 --> 04:05:09,560
names and I want to give them a unique
6490
04:05:07,159 --> 04:05:11,399
identifier and this unique identifier
6491
04:05:09,560 --> 04:05:13,439
will just give them a number that their
6492
04:05:11,399 --> 04:05:15,199
profile is up against it's the same way
6493
04:05:13,439 --> 04:05:17,319
when whenever you make an account with
6494
04:05:15,199 --> 04:05:19,359
any sort of website when your data gets
6495
04:05:17,319 --> 04:05:21,760
stored in the database you have a unique
6496
04:05:19,359 --> 04:05:23,680
ID for your account and this is the same
6497
04:05:21,760 --> 04:05:26,159
thing that I'm trying to replicate here
6498
04:05:23,680 --> 04:05:29,680
we have a unique ID and with that unique
6499
04:05:26,159 --> 04:05:32,680
ID comes a name so we can say equals new
6500
04:05:29,680 --> 04:05:34,399
dictionary just like we do with the list
6501
04:05:32,680 --> 04:05:36,239
we can end the brackets and end the
6502
04:05:34,399 --> 04:05:38,640
semicolon there and now we've created
6503
04:05:36,239 --> 04:05:40,600
our dictionary and the exact same
6504
04:05:38,640 --> 04:05:42,640
principle we can use with the list we
6505
04:05:40,600 --> 04:05:45,199
can simply type in the name of the
6506
04:05:42,640 --> 04:05:47,960
variable of the dictionary and type in
6507
04:05:45,199 --> 04:05:50,640
add and then we have access to int key
6508
04:05:47,960 --> 04:05:52,560
and string value and this parameter is
6509
04:05:50,640 --> 04:05:54,840
going to change if this now becomes a
6510
04:05:52,560 --> 04:05:57,399
string as I'll show you
6511
04:05:54,840 --> 04:06:00,080
now and if we reopen the bracket you'll
6512
04:05:57,399 --> 04:06:01,600
see this has turned into a string key so
6513
04:06:00,080 --> 04:06:04,000
that will change depending on the data
6514
04:06:01,600 --> 04:06:06,199
types that you specified so let's say we
6515
04:06:04,000 --> 04:06:08,319
want to add one and we want to add
6516
04:06:06,199 --> 04:06:12,000
ourselves then we can duplicate this
6517
04:06:08,319 --> 04:06:14,199
down and add in two and three and we can
6518
04:06:12,000 --> 04:06:15,560
say test and test
6519
04:06:14,199 --> 04:06:17,319
and now if you run the code you'll see
6520
04:06:15,560 --> 04:06:18,920
that we have no errors and nothing's
6521
04:06:17,319 --> 04:06:21,560
going to show up I'm just demonstrate
6522
04:06:18,920 --> 04:06:23,920
that it compiles okay and the same thing
6523
04:06:21,560 --> 04:06:26,199
with the list we can also simplifies the
6524
04:06:23,920 --> 04:06:29,040
initialization process and it looks
6525
04:06:26,199 --> 04:06:31,439
something like this so because this is
6526
04:06:29,040 --> 04:06:33,479
in key value pair we can no longer just
6527
04:06:31,439 --> 04:06:35,399
type it in one line they need to be in a
6528
04:06:33,479 --> 04:06:37,239
little block so when we wrap these
6529
04:06:35,399 --> 04:06:39,319
around the curly brace we can have the
6530
04:06:37,239 --> 04:06:41,680
key on the left and the value on the
6531
04:06:39,319 --> 04:06:45,439
right and one of these is called a key
6532
04:06:41,680 --> 04:06:47,279
value pair key P value pair and we will
6533
04:06:45,439 --> 04:06:48,960
use these in the for each Loop in a
6534
04:06:47,279 --> 04:06:50,760
second when we print out all the values
6535
04:06:48,960 --> 04:06:52,399
to the console so let me just
6536
04:06:50,760 --> 04:06:54,640
demonstrate something about what I said
6537
04:06:52,399 --> 04:06:56,720
at the start I said that all keys must
6538
04:06:54,640 --> 04:06:58,640
be unique and there's a reason why I
6539
04:06:56,720 --> 04:07:01,359
said that if we duplicate the first
6540
04:06:58,640 --> 04:07:03,600
entry therefore giv us the same key as
6541
04:07:01,359 --> 04:07:05,560
one and we try run it and let's see what
6542
04:07:03,600 --> 04:07:08,319
happens we get something called a
6543
04:07:05,560 --> 04:07:10,080
runtime error this runtime error happens
6544
04:07:08,319 --> 04:07:12,080
when we run the program and it's got
6545
04:07:10,080 --> 04:07:14,279
nothing to do with compile time or our
6546
04:07:12,080 --> 04:07:16,159
logic an item with a same key has
6547
04:07:14,279 --> 04:07:18,399
already been added so we need to watch
6548
04:07:16,159 --> 04:07:19,920
out if we do this because our program is
6549
04:07:18,399 --> 04:07:21,800
going to compile but it's not going to
6550
04:07:19,920 --> 04:07:24,600
work at runtime and let me demonstrate
6551
04:07:21,800 --> 04:07:26,840
another fact if we make this number four
6552
04:07:24,600 --> 04:07:29,319
and our values are exactly the same once
6553
04:07:26,840 --> 04:07:31,479
we run this it will still work fine
6554
04:07:29,319 --> 04:07:33,840
values can be the same but the keys
6555
04:07:31,479 --> 04:07:35,800
cannot be the same the keys have to be
6556
04:07:33,840 --> 04:07:38,199
unique so just like with our lists and
6557
04:07:35,800 --> 04:07:40,720
our arrays we can use a for each or a
6558
04:07:38,199 --> 04:07:43,239
for Loop to access this directly so we
6559
04:07:40,720 --> 04:07:45,880
can say 4 in I equal 0 and we're want to
6560
04:07:43,239 --> 04:07:47,880
go names do count and the same reason
6561
04:07:45,880 --> 04:07:50,080
it's a do count for a dictionary as it
6562
04:07:47,880 --> 04:07:52,159
is for a list but it's not for an array
6563
04:07:50,080 --> 04:07:54,840
dictionaries don't have fixed length and
6564
04:07:52,159 --> 04:07:57,119
neither do lists they are very Dynamic
6565
04:07:54,840 --> 04:07:59,479
and basically are a list list can only
6566
04:07:57,119 --> 04:08:01,520
store one different data type whereas
6567
04:07:59,479 --> 04:08:03,600
dictionaries give you a key and a value
6568
04:08:01,520 --> 04:08:05,800
so as we said above we have a key value
6569
04:08:03,600 --> 04:08:07,600
pair so let's try and print this and see
6570
04:08:05,800 --> 04:08:10,000
what happens have a console right line
6571
04:08:07,600 --> 04:08:12,359
and we can say names square bracket I
6572
04:08:10,000 --> 04:08:13,680
and that will give us back the position
6573
04:08:12,359 --> 04:08:15,199
the issue with this is it will just give
6574
04:08:13,680 --> 04:08:17,520
us back the string and let's just see
6575
04:08:15,199 --> 04:08:20,159
what happens if we print
6576
04:08:17,520 --> 04:08:22,520
this then we get an exception because no
6577
04:08:20,159 --> 04:08:24,560
key was given and the problem with this
6578
04:08:22,520 --> 04:08:27,159
situation is we can't use this like a
6579
04:08:24,560 --> 04:08:29,239
list getso sets the value associated
6580
04:08:27,159 --> 04:08:31,720
with a specified key what this means is
6581
04:08:29,239 --> 04:08:33,439
when we give our square bracket I inside
6582
04:08:31,720 --> 04:08:35,359
here this is actually not talking about
6583
04:08:33,439 --> 04:08:38,920
the index this is actually trying to do
6584
04:08:35,359 --> 04:08:40,279
a search on these keys so if you
6585
04:08:38,920 --> 04:08:43,159
actually Chang this to one then we
6586
04:08:40,279 --> 04:08:45,000
should get abber return back this is not
6587
04:08:43,159 --> 04:08:47,640
actually doing an index lookup it's
6588
04:08:45,000 --> 04:08:49,040
doing a key lookup and the reason for
6589
04:08:47,640 --> 04:08:52,080
that is what we're going to have to do
6590
04:08:49,040 --> 04:08:53,680
instead is say names. element art and
6591
04:08:52,080 --> 04:08:56,640
then what we can do inside there is
6592
04:08:53,680 --> 04:08:58,880
place our index so this element art will
6593
04:08:56,640 --> 04:09:01,560
return us back with a key value pair as
6594
04:08:58,880 --> 04:09:04,159
I said above here so how this variable
6595
04:09:01,560 --> 04:09:06,159
works is we can do key value pair and
6596
04:09:04,159 --> 04:09:08,359
then we need to specify this variable
6597
04:09:06,159 --> 04:09:11,159
the same way we've got it up here so we
6598
04:09:08,359 --> 04:09:13,680
have an INT inside a string and then we
6599
04:09:11,159 --> 04:09:16,439
can say a pair and make that equal to
6600
04:09:13,680 --> 04:09:18,720
the function called element at as that
6601
04:09:16,439 --> 04:09:20,560
returns us back with a key value pair in
6602
04:09:18,720 --> 04:09:22,000
string and of course if you wanted to
6603
04:09:20,560 --> 04:09:24,239
make this a lot smaller you could just
6604
04:09:22,000 --> 04:09:25,840
simply use V but let's keep it as key
6605
04:09:24,239 --> 04:09:27,840
value pair just so you can understand
6606
04:09:25,840 --> 04:09:29,680
what's going on and the reason why we
6607
04:09:27,840 --> 04:09:31,479
need to get the key value pair is we
6608
04:09:29,680 --> 04:09:33,600
want to retrieve the integer and we want
6609
04:09:31,479 --> 04:09:37,000
to retrieve the string and now we can
6610
04:09:33,600 --> 04:09:39,600
use pair. key and pair. value and
6611
04:09:37,000 --> 04:09:41,760
because these are both integer and this
6612
04:09:39,600 --> 04:09:44,199
one is a string it matches our pair
6613
04:09:41,760 --> 04:09:46,279
exactly so we can take these values and
6614
04:09:44,199 --> 04:09:47,960
go to our console right line and we can
6615
04:09:46,279 --> 04:09:50,239
say something like this use string
6616
04:09:47,960 --> 04:09:53,159
interpolation and say pair. key and then
6617
04:09:50,239 --> 04:09:54,880
maybe add a hyphen and then say pair.
6618
04:09:53,159 --> 04:09:58,199
value and then just remove these two
6619
04:09:54,880 --> 04:10:00,000
lines and let's try and run it now there
6620
04:09:58,199 --> 04:10:01,960
we go so we get all the values and it
6621
04:10:00,000 --> 04:10:03,640
doesn't matter what this key is it can
6622
04:10:01,960 --> 04:10:05,640
be something random it doesn't really
6623
04:10:03,640 --> 04:10:08,359
make a difference like I said this
6624
04:10:05,640 --> 04:10:11,119
doesn't have to be in a specific order
6625
04:10:08,359 --> 04:10:12,840
so let's keep it as 1 2 3 there we go so
6626
04:10:11,119 --> 04:10:14,279
you can see using a for each Loop to
6627
04:10:12,840 --> 04:10:16,279
print this is is a little bit more
6628
04:10:14,279 --> 04:10:18,680
complicated let's have a look and see
6629
04:10:16,279 --> 04:10:23,159
what it takes when you use a for each
6630
04:10:18,680 --> 04:10:25,680
Loop for each VAR item inside our
6631
04:10:23,159 --> 04:10:28,720
names and as you can see here our item
6632
04:10:25,680 --> 04:10:31,520
is automatically a key value pair of int
6633
04:10:28,720 --> 04:10:33,800
and string and again what you can do is
6634
04:10:31,520 --> 04:10:35,520
type this straight into here just so you
6635
04:10:33,800 --> 04:10:38,000
can get used to the notation and there
6636
04:10:35,520 --> 04:10:40,319
you go it's still a key value pair and
6637
04:10:38,000 --> 04:10:42,479
then now what we can do is we can take
6638
04:10:40,319 --> 04:10:45,479
this exact line and paste into here and
6639
04:10:42,479 --> 04:10:47,040
instead of pair we have item and this
6640
04:10:45,479 --> 04:10:48,840
should return us back the exact same
6641
04:10:47,040 --> 04:10:51,880
values and if we just separate with a
6642
04:10:48,840 --> 04:10:53,080
console right line there we go okay so
6643
04:10:51,880 --> 04:10:55,399
now that we've understood this let's
6644
04:10:53,080 --> 04:10:57,399
take a look at an example let's comment
6645
04:10:55,399 --> 04:10:59,760
this out and let's create another
6646
04:10:57,399 --> 04:11:01,600
dictionary so we can say dictionary and
6647
04:10:59,760 --> 04:11:04,040
then we can up a string of
6648
04:11:01,600 --> 04:11:06,640
string and we can call this teachers
6649
04:11:04,040 --> 04:11:08,560
equals new dictionary string of string
6650
04:11:06,640 --> 04:11:10,080
again and we can add our curly brace
6651
04:11:08,560 --> 04:11:11,760
just so we can add our value straight to
6652
04:11:10,080 --> 04:11:14,080
here so as you said before we need to
6653
04:11:11,760 --> 04:11:15,880
open curly brace and close Cur
6654
04:11:14,080 --> 04:11:18,279
and then we can say the math teacher is
6655
04:11:15,880 --> 04:11:21,279
called abber and then we can add another
6656
04:11:18,279 --> 04:11:22,920
one and we say the science teacher is
6657
04:11:21,279 --> 04:11:24,720
called test there we go so we've
6658
04:11:22,920 --> 04:11:26,159
initialized our teachers and we said
6659
04:11:24,720 --> 04:11:28,359
that the value that we want to give it
6660
04:11:26,159 --> 04:11:31,040
is a unique identifier which is going to
6661
04:11:28,359 --> 04:11:32,520
be our string assigned to the subject
6662
04:11:31,040 --> 04:11:34,359
and this is going to be the teacher that
6663
04:11:32,520 --> 04:11:36,279
is assigned to the subject so what we
6664
04:11:34,359 --> 04:11:38,319
can do inside here is we can have a
6665
04:11:36,279 --> 04:11:40,359
console right line and we can do
6666
04:11:38,319 --> 04:11:41,760
something like this so remember when I
6667
04:11:40,359 --> 04:11:43,840
was saying in the for Loop we can't
6668
04:11:41,760 --> 04:11:46,000
actually use the square bracket notation
6669
04:11:43,840 --> 04:11:48,640
like this because this is referring to a
6670
04:11:46,000 --> 04:11:50,960
key rather than a position so we had to
6671
04:11:48,640 --> 04:11:52,920
use element at just to find out the key
6672
04:11:50,960 --> 04:11:54,880
value pair and then to print out to the
6673
04:11:52,920 --> 04:11:56,880
console so because the square brackets
6674
04:11:54,880 --> 04:11:58,520
is an actual surge let's try and make
6675
04:11:56,880 --> 04:12:00,319
use of that now so we know that there's
6676
04:11:58,520 --> 04:12:02,199
a math class available so we can type
6677
04:12:00,319 --> 04:12:04,520
math in here and what that should return
6678
04:12:02,199 --> 04:12:05,840
us back is abber there we go so what
6679
04:12:04,520 --> 04:12:08,479
that's doing is it's searching through
6680
04:12:05,840 --> 04:12:10,720
our dictionary finding this element
6681
04:12:08,479 --> 04:12:13,359
inside here and then return us back with
6682
04:12:10,720 --> 04:12:15,399
the value for that key gets all sets the
6683
04:12:13,359 --> 04:12:17,199
value associated with the specified key
6684
04:12:15,399 --> 04:12:19,600
the problem with using this approach is
6685
04:12:17,199 --> 04:12:22,119
it says here if the specified key is not
6686
04:12:19,600 --> 04:12:23,920
found a operation throws a key not found
6687
04:12:22,119 --> 04:12:25,720
exception let's just say for instance I
6688
04:12:23,920 --> 04:12:27,520
mistyped the math and now we're just
6689
04:12:25,720 --> 04:12:29,560
going to get a runtime error and it's
6690
04:12:27,520 --> 04:12:31,560
going to be a horrible exception and our
6691
04:12:29,560 --> 04:12:33,920
code is obviously going to break this is
6692
04:12:31,560 --> 04:12:36,560
similar to what we do when we have in.
6693
04:12:33,920 --> 04:12:38,800
trass we don't just trass it immediately
6694
04:12:36,560 --> 04:12:40,680
and convert it without actually checking
6695
04:12:38,800 --> 04:12:43,800
if it's a valid value so instead of
6696
04:12:40,680 --> 04:12:46,439
doing this line what we can do is we can
6697
04:12:43,800 --> 04:12:49,760
check if that key exists first before we
6698
04:12:46,439 --> 04:12:52,840
then use it so we can say if
6699
04:12:49,760 --> 04:12:54,600
teachers do try and get the value which
6700
04:12:52,840 --> 04:12:57,119
is similar to what we have with triy
6701
04:12:54,600 --> 04:12:58,920
pass and then the first parameter is the
6702
04:12:57,119 --> 04:13:01,840
key that we want which is going to be
6703
04:12:58,920 --> 04:13:03,600
our math and then out here is going to
6704
04:13:01,840 --> 04:13:06,319
be the variable that we want to Output
6705
04:13:03,600 --> 04:13:09,040
out so we can say out
6706
04:13:06,319 --> 04:13:11,159
teacher and then open the curly braces
6707
04:13:09,040 --> 04:13:13,359
and then inside here we can print out
6708
04:13:11,159 --> 04:13:15,840
the teacher so we try to get the value
6709
04:13:13,359 --> 04:13:17,840
for Math and if we found that key inside
6710
04:13:15,840 --> 04:13:20,040
these then we return back the value
6711
04:13:17,840 --> 04:13:22,800
inside our teacher the same way works
6712
04:13:20,040 --> 04:13:24,840
for our trip pass you take in a string
6713
04:13:22,800 --> 04:13:27,119
in here and the output is going to be an
6714
04:13:24,840 --> 04:13:30,920
integer if it's successfully passed it
6715
04:13:27,119 --> 04:13:34,000
and if it hasn't we can say cons output
6716
04:13:30,920 --> 04:13:36,199
math teacher not found so let's try and
6717
04:13:34,000 --> 04:13:38,359
run that now you see we get return back
6718
04:13:36,199 --> 04:13:40,439
Abba and if you actually drop the lower
6719
04:13:38,359 --> 04:13:41,359
case then it says the math teacher is
6720
04:13:40,439 --> 04:13:43,720
not
6721
04:13:41,359 --> 04:13:45,800
found alternatively what we can do
6722
04:13:43,720 --> 04:13:48,800
inside here let's assume that this is
6723
04:13:45,800 --> 04:13:50,600
correct we can take this value and place
6724
04:13:48,800 --> 04:13:52,119
it straight inside the square brackets
6725
04:13:50,600 --> 04:13:54,159
and do this again this is kind of
6726
04:13:52,119 --> 04:13:56,600
redundant because we've told it to get
6727
04:13:54,159 --> 04:13:57,920
the value for key place inside teacher
6728
04:13:56,600 --> 04:14:00,119
and then we're just going back to the
6729
04:13:57,920 --> 04:14:02,040
dictionary to get the value twice so
6730
04:14:00,119 --> 04:14:03,960
make sure if you have an output variable
6731
04:14:02,040 --> 04:14:05,520
that you actually use it inside here you
6732
04:14:03,960 --> 04:14:07,600
can see that you're not using it when it
6733
04:14:05,520 --> 04:14:09,199
goes a slightly dark color like this so
6734
04:14:07,600 --> 04:14:11,520
just like in the last examples when we
6735
04:14:09,199 --> 04:14:13,880
were placing values inside our list we
6736
04:14:11,520 --> 04:14:16,560
can also do that in dictionaries
6737
04:14:13,880 --> 04:14:18,040
we can say teachers square bracket math
6738
04:14:16,560 --> 04:14:20,000
and then we can make that equal to
6739
04:14:18,040 --> 04:14:22,279
something so if the math teacher were to
6740
04:14:20,000 --> 04:14:23,880
change and now the teachers called Joe
6741
04:14:22,279 --> 04:14:25,800
then we can run this line and print out
6742
04:14:23,880 --> 04:14:27,359
to the screen let's just take that for
6743
04:14:25,800 --> 04:14:29,399
each Loop and tailor it to the new
6744
04:14:27,359 --> 04:14:31,319
dictionary and see what happens when we
6745
04:14:29,399 --> 04:14:33,239
run it so let's just change this into a
6746
04:14:31,319 --> 04:14:35,439
VAR to make it simple and then we can
6747
04:14:33,239 --> 04:14:37,640
have teachers inside here there we go so
6748
04:14:35,439 --> 04:14:39,760
now let's just run this and you see now
6749
04:14:37,640 --> 04:14:42,439
it says abber is the first value that
6750
04:14:39,760 --> 04:14:44,840
abber is coming from console right line
6751
04:14:42,439 --> 04:14:46,800
here that says teacher this is one line
6752
04:14:44,840 --> 04:14:49,040
before we change it and we change the
6753
04:14:46,800 --> 04:14:51,479
value of the math teacher to Joe on the
6754
04:14:49,040 --> 04:14:53,159
next line and there we go it's committed
6755
04:14:51,479 --> 04:14:54,760
the change and just like in the last
6756
04:14:53,159 --> 04:14:56,239
video as well when we try to delete
6757
04:14:54,760 --> 04:14:58,199
something from the list let's see what
6758
04:14:56,239 --> 04:15:00,720
we can do to delete it from here so we
6759
04:14:58,199 --> 04:15:03,080
can have teachers do remove and you can
6760
04:15:00,720 --> 04:15:04,800
see that it says key in there and again
6761
04:15:03,080 --> 04:15:07,439
if we add the key in like this then it's
6762
04:15:04,800 --> 04:15:08,880
going to work just fine there you go we
6763
04:15:07,439 --> 04:15:11,319
just have science we don't have math
6764
04:15:08,880 --> 04:15:12,560
anymore but if I drop the lowercase then
6765
04:15:11,319 --> 04:15:14,199
it's going to cause us back with an
6766
04:15:12,560 --> 04:15:16,399
error but in this case instead of it
6767
04:15:14,199 --> 04:15:18,760
throwing an exception like it did when
6768
04:15:16,399 --> 04:15:21,080
we were trying to do up here and up here
6769
04:15:18,760 --> 04:15:23,000
as well the problem with using the
6770
04:15:21,080 --> 04:15:25,040
remove is you just simply won't remove
6771
04:15:23,000 --> 04:15:27,040
it but what it will do is it'll give you
6772
04:15:25,040 --> 04:15:29,040
back a booing if you remember from the
6773
04:15:27,040 --> 04:15:31,119
last video we actually had a look at a
6774
04:15:29,040 --> 04:15:32,680
section where in the list it gave you
6775
04:15:31,119 --> 04:15:34,800
back a booing whether the remove
6776
04:15:32,680 --> 04:15:36,239
function worked or not alternatively
6777
04:15:34,800 --> 04:15:39,000
what you could do instead of using the
6778
04:15:36,239 --> 04:15:41,199
remove function is to check if the item
6779
04:15:39,000 --> 04:15:43,560
exists in the first place we can say
6780
04:15:41,199 --> 04:15:45,800
contains key and we can Place math
6781
04:15:43,560 --> 04:15:47,680
inside here and if it does contain it
6782
04:15:45,800 --> 04:15:50,399
then we can run the same line of code
6783
04:15:47,680 --> 04:15:52,359
and we can say remove and then use math
6784
04:15:50,399 --> 04:15:54,840
just to make sure that this has been
6785
04:15:52,359 --> 04:15:58,399
actually removed and then in the else we
6786
04:15:54,840 --> 04:16:00,600
can say math not found retrieving values
6787
04:15:58,399 --> 04:16:02,359
back should be retrieve using try get
6788
04:16:00,600 --> 04:16:04,560
just so you don't cause an exception if
6789
04:16:02,359 --> 04:16:06,479
this is not found we can simply do an
6790
04:16:04,560 --> 04:16:08,279
update by placing the value of the key
6791
04:16:06,479 --> 04:16:10,040
inside the square brackets and then
6792
04:16:08,279 --> 04:16:12,399
simply just assigning it to something
6793
04:16:10,040 --> 04:16:14,920
else this assignment will change the
6794
04:16:12,399 --> 04:16:17,040
value of the key which is inside the
6795
04:16:14,920 --> 04:16:18,760
teacher dictionary and if you wanted to
6796
04:16:17,040 --> 04:16:20,520
remove something it's best to use the
6797
04:16:18,760 --> 04:16:22,040
contains key just to check if it
6798
04:16:20,520 --> 04:16:23,760
contains it first and then you can do
6799
04:16:22,040 --> 04:16:25,880
the remove and then if it doesn't
6800
04:16:23,760 --> 04:16:28,279
contain it you can tell the user that
6801
04:16:25,880 --> 04:16:30,880
math has not been found and then finally
6802
04:16:28,279 --> 04:16:33,359
we can just print out like
6803
04:16:30,880 --> 04:16:35,239
this so in this exercise I want you to
6804
04:16:33,359 --> 04:16:37,560
create two lists with the integer data
6805
04:16:35,239 --> 04:16:38,800
type one for even and one for odd and
6806
04:16:37,560 --> 04:16:41,000
then we're going to Loop through from
6807
04:16:38,800 --> 04:16:43,119
zero all the way till 20 and if the
6808
04:16:41,000 --> 04:16:45,399
number is even add it to the even and if
6809
04:16:43,119 --> 04:16:47,279
the number is odd add it to the odd list
6810
04:16:45,399 --> 04:16:49,800
and at the end print out all the values
6811
04:16:47,279 --> 04:16:51,399
for even and for odd I know you can just
6812
04:16:49,800 --> 04:16:53,399
print these out inside the for Loop we
6813
04:16:51,399 --> 04:16:55,239
don't have to have dedicated lists but
6814
04:16:53,399 --> 04:16:56,720
this is just for list practice and I
6815
04:16:55,239 --> 04:16:58,960
want to talk about different ways that
6816
04:16:56,720 --> 04:17:01,319
we can do this solution so let's start
6817
04:16:58,960 --> 04:17:05,319
we can have a list of int and we can say
6818
04:17:01,319 --> 04:17:06,720
odd new list int and we don't know the
6819
04:17:05,319 --> 04:17:09,040
size so we're just going to leave the
6820
04:17:06,720 --> 04:17:11,159
gaps empty and if we duplicate this down
6821
04:17:09,040 --> 04:17:13,080
we can simply just change this to even
6822
04:17:11,159 --> 04:17:15,760
now the reason why we're doing lists
6823
04:17:13,080 --> 04:17:17,680
we're going from 0 to 20 of course this
6824
04:17:15,760 --> 04:17:19,800
20 value could be anything it could be a
6825
04:17:17,680 --> 04:17:23,199
million it could be 100 but we don't
6826
04:17:19,800 --> 04:17:25,520
know and the point is lists have counts
6827
04:17:23,199 --> 04:17:26,920
whereas arrays have lengths if we set
6828
04:17:25,520 --> 04:17:28,720
the array length to something like a
6829
04:17:26,920 --> 04:17:30,760
thousand we know probably be able to
6830
04:17:28,720 --> 04:17:31,920
contain all the numbers inside there but
6831
04:17:30,760 --> 04:17:33,680
that's going to be a lot of positions
6832
04:17:31,920 --> 04:17:35,680
that we don't exactly need because
6833
04:17:33,680 --> 04:17:37,399
arrays are not really dynamic because
6834
04:17:35,680 --> 04:17:39,439
they're static then it's best to use
6835
04:17:37,399 --> 04:17:41,640
list in this case because we don't know
6836
04:17:39,439 --> 04:17:43,040
how many numbers are going to be odd or
6837
04:17:41,640 --> 04:17:45,359
are going to be even between between
6838
04:17:43,040 --> 04:17:47,439
this range if we knew the exact number
6839
04:17:45,359 --> 04:17:49,600
of positions that we needed in a given
6840
04:17:47,439 --> 04:17:51,319
solution then array would be better
6841
04:17:49,600 --> 04:17:54,040
since you don't want it to be dynamic if
6842
04:17:51,319 --> 04:17:56,800
it doesn't need to be so let's start can
6843
04:17:54,040 --> 04:17:58,399
have a for Loop that goes from 0 to 20
6844
04:17:56,800 --> 04:18:00,680
if you want to include the 20 as well we
6845
04:17:58,399 --> 04:18:02,800
can just place an equals the best way to
6846
04:18:00,680 --> 04:18:04,680
determine if a number is odd or even is
6847
04:18:02,800 --> 04:18:07,560
to use the modulus operator so we can
6848
04:18:04,680 --> 04:18:09,399
say I percentage 2 equals equal Z and
6849
04:18:07,560 --> 04:18:11,560
what this will do is it'll tell us if I
6850
04:18:09,399 --> 04:18:15,119
is divisible by two if the value is
6851
04:18:11,560 --> 04:18:17,080
equal to zero if you have two modulus 2
6852
04:18:15,119 --> 04:18:19,319
then the remainder is going to be zero
6853
04:18:17,080 --> 04:18:22,600
if you have three modulus 2 it's going
6854
04:18:19,319 --> 04:18:24,720
to be 1 * by two which gets you to two
6855
04:18:22,600 --> 04:18:27,439
and then to go from two to three you
6856
04:18:24,720 --> 04:18:29,680
actually have one so if you insert an
6857
04:18:27,439 --> 04:18:32,319
odd number and do a modulus two you're
6858
04:18:29,680 --> 04:18:34,159
going to get a one returned if you do an
6859
04:18:32,319 --> 04:18:35,840
odd number and you do modulus 2 then
6860
04:18:34,159 --> 04:18:38,080
you're going to get one returned if you
6861
04:18:35,840 --> 04:18:40,439
do an even number modulus 2 then you're
6862
04:18:38,080 --> 04:18:43,080
going to get zero returned so what we
6863
04:18:40,439 --> 04:18:45,199
can do in here is we can say even add
6864
04:18:43,080 --> 04:18:48,000
and then we can simply add I and then in
6865
04:18:45,199 --> 04:18:50,840
the else we can also simply just do odd
6866
04:18:48,000 --> 04:18:52,880
do add and then have I because the
6867
04:18:50,840 --> 04:18:54,840
values of the remainder can only be zero
6868
04:18:52,880 --> 04:18:56,520
or one we don't need to have an lse if
6869
04:18:54,840 --> 04:18:58,199
in here doing this is a little bit
6870
04:18:56,520 --> 04:19:00,600
redundant although it's still
6871
04:18:58,199 --> 04:19:03,080
technically correct the values can only
6872
04:19:00,600 --> 04:19:05,199
ever be zero or one so if you run this
6873
04:19:03,080 --> 04:19:06,840
calculation and the value is zero if you
6874
04:19:05,199 --> 04:19:09,080
run this calculation and the value is
6875
04:19:06,840 --> 04:19:11,199
zero the only other value can possibly
6876
04:19:09,080 --> 04:19:13,040
be is one so therefore we don't need to
6877
04:19:11,199 --> 04:19:14,600
additional condition so therefore we
6878
04:19:13,040 --> 04:19:17,239
don't need this additional condition and
6879
04:19:14,600 --> 04:19:19,840
we can simply get rid of it if I modulus
6880
04:19:17,239 --> 04:19:22,319
2 is equal to Z then we add the number
6881
04:19:19,840 --> 04:19:24,000
to even and else we add it to odd
6882
04:19:22,319 --> 04:19:26,520
normally when you have situations like
6883
04:19:24,000 --> 04:19:28,000
this we can use the conditional operator
6884
04:19:26,520 --> 04:19:30,040
where we have the question mark followed
6885
04:19:28,000 --> 04:19:31,960
by the colon but in this case we're
6886
04:19:30,040 --> 04:19:34,279
actually doing two different assignments
6887
04:19:31,960 --> 04:19:36,000
we're either assigning into the even or
6888
04:19:34,279 --> 04:19:38,080
assigning into the odd so there's no
6889
04:19:36,000 --> 04:19:39,720
room for the conditional operator so at
6890
04:19:38,080 --> 04:19:41,840
this point we should have all the even
6891
04:19:39,720 --> 04:19:44,840
and odd values from zero all the way to
6892
04:19:41,840 --> 04:19:47,479
20 inside our two lists so then we can
6893
04:19:44,840 --> 04:19:50,720
now just print both the lists out so we
6894
04:19:47,479 --> 04:19:52,520
can say printing even numbers and then
6895
04:19:50,720 --> 04:19:55,279
we can have a for each Loop that goes v
6896
04:19:52,520 --> 04:19:57,199
item inside out even and then we can do
6897
04:19:55,279 --> 04:19:59,239
a console Right add some instring
6898
04:19:57,199 --> 04:20:02,159
interpolation and then we can say item
6899
04:19:59,239 --> 04:20:03,800
followed by space then we can print
6900
04:20:02,159 --> 04:20:06,239
environment. new line just to give it
6901
04:20:03,800 --> 04:20:08,520
some space and we can say printing odd
6902
04:20:06,239 --> 04:20:10,880
numbers here we go and then we can just
6903
04:20:08,520 --> 04:20:13,040
take this exact for Loop and copy it
6904
04:20:10,880 --> 04:20:15,359
down instead of set in even we want to
6905
04:20:13,040 --> 04:20:17,199
do in odd and this is exactly the same
6906
04:20:15,359 --> 04:20:19,960
since the variable name is exactly the
6907
04:20:17,199 --> 04:20:22,600
same let's try run the
6908
04:20:19,960 --> 04:20:24,560
code there we go it's got zero all the
6909
04:20:22,600 --> 04:20:26,880
way till 20 and you can see it goes up
6910
04:20:24,560 --> 04:20:28,720
in twos hence all even numbers and we
6911
04:20:26,880 --> 04:20:30,680
also get these going up in twos except
6912
04:20:28,720 --> 04:20:32,520
it starts from one and this covers all
6913
04:20:30,680 --> 04:20:34,319
of the odd numbers and if we change this
6914
04:20:32,520 --> 04:20:36,000
number up to something like 50 then the
6915
04:20:34,319 --> 04:20:38,239
list will just grow and they Cod should
6916
04:20:36,000 --> 04:20:39,800
still work okay you can see that all of
6917
04:20:38,239 --> 04:20:41,880
these numbers are still consistently
6918
04:20:39,800 --> 04:20:44,000
even so now that our task is actually
6919
04:20:41,880 --> 04:20:45,560
complete let's just have a little recap
6920
04:20:44,000 --> 04:20:47,800
so the reason why we're using lists and
6921
04:20:45,560 --> 04:20:51,319
we're not using arrays is for this exact
6922
04:20:47,800 --> 04:20:53,199
nature if we have 0 to 10 then we can
6923
04:20:51,319 --> 04:20:55,399
almost work out how many even numbers
6924
04:20:53,199 --> 04:20:58,159
and how many odd numbers we can have we
6925
04:20:55,399 --> 04:21:01,479
can manually count we have zero and then
6926
04:20:58,159 --> 04:21:05,880
two 4 6 8 and 10 and then for odd we
6927
04:21:01,479 --> 04:21:08,359
have 1 3 5 7 and 9 so we know how many
6928
04:21:05,880 --> 04:21:10,319
numbers exist within here but what if we
6929
04:21:08,359 --> 04:21:12,159
just change that number to 50 then now
6930
04:21:10,319 --> 04:21:14,680
we have a lot more odd and a lot more
6931
04:21:12,159 --> 04:21:16,399
even it's impractical to type these
6932
04:21:14,680 --> 04:21:18,159
numbers in and then to Simply count them
6933
04:21:16,399 --> 04:21:19,960
in your head and then make an array
6934
04:21:18,159 --> 04:21:22,040
length based on that position that's a
6935
04:21:19,960 --> 04:21:24,040
bad solution because not all cases
6936
04:21:22,040 --> 04:21:25,720
you're going to have the flexibility of
6937
04:21:24,040 --> 04:21:27,720
Simply typing in the numbers like this
6938
04:21:25,720 --> 04:21:29,279
and Counting them yourself these numbers
6939
04:21:27,720 --> 04:21:31,399
could be something that typed in by a
6940
04:21:29,279 --> 04:21:33,640
user or something loaded for something
6941
04:21:31,399 --> 04:21:35,199
external like a text file you need to
6942
04:21:33,640 --> 04:21:37,199
understand that these values can be
6943
04:21:35,199 --> 04:21:38,680
generated from anywhere and if you just
6944
04:21:37,199 --> 04:21:40,960
type them in then it provides an
6945
04:21:38,680 --> 04:21:42,760
unrealistic expectation of how to write
6946
04:21:40,960 --> 04:21:45,119
some programs so what you have to bear
6947
04:21:42,760 --> 04:21:47,479
in mind in situations like this is you
6948
04:21:45,119 --> 04:21:49,080
simply don't know how many even and how
6949
04:21:47,479 --> 04:21:51,199
many odd numbers will be appearing
6950
04:21:49,080 --> 04:21:53,800
during this range so you want to use a
6951
04:21:51,199 --> 04:21:56,040
list so it's as dynamic as possible we
6952
04:21:53,800 --> 04:21:57,880
have two lists in this case one to start
6953
04:21:56,040 --> 04:21:59,680
all the odd numbers and one to start all
6954
04:21:57,880 --> 04:22:01,600
the even numbers we don't initialize
6955
04:21:59,680 --> 04:22:03,520
them with any sort of capacity we just
6956
04:22:01,600 --> 04:22:05,760
keep it empty for the exact reason we
6957
04:22:03,520 --> 04:22:07,880
don't use an array we Loop through from
6958
04:22:05,760 --> 04:22:09,680
zero to anything we don't even have to
6959
04:22:07,880 --> 04:22:11,680
loop from zero we can also just start
6960
04:22:09,680 --> 04:22:14,119
this at 100 and go all the way till
6961
04:22:11,680 --> 04:22:15,920
th000 it really doesn't matter what our
6962
04:22:14,119 --> 04:22:18,239
number range is the only thing that
6963
04:22:15,920 --> 04:22:19,960
matters is that we having a list that is
6964
04:22:18,239 --> 04:22:22,239
going to be dynamic and then we can
6965
04:22:19,960 --> 04:22:24,840
calculate whether the number is odd or
6966
04:22:22,239 --> 04:22:26,960
even and add it to the right list how
6967
04:22:24,840 --> 04:22:28,640
this works is we use I which is the
6968
04:22:26,960 --> 04:22:30,640
number we're currently up to and we
6969
04:22:28,640 --> 04:22:32,560
calculate the modulus of it to check the
6970
04:22:30,640 --> 04:22:35,040
remainder if the remainder is zero then
6971
04:22:32,560 --> 04:22:36,760
it must be even as it's divisible by two
6972
04:22:35,040 --> 04:22:38,479
and if it's anything else than zero
6973
04:22:36,760 --> 04:22:40,920
which you can only ever be one in this
6974
04:22:38,479 --> 04:22:43,199
case when using modulus 2 then we add it
6975
04:22:40,920 --> 04:22:45,479
to the odd list after this volume is run
6976
04:22:43,199 --> 04:22:47,239
now our odd and even lists have all the
6977
04:22:45,479 --> 04:22:49,359
values that they should have and we
6978
04:22:47,239 --> 04:22:51,319
simply just write these two blocks are
6979
04:22:49,359 --> 04:22:53,479
exactly the same just print the even
6980
04:22:51,319 --> 04:22:55,119
numbers and print them out print the odd
6981
04:22:53,479 --> 04:22:57,080
numbers and simply print them out to the
6982
04:22:55,119 --> 04:22:58,720
screen and that's it and if you run the
6983
04:22:57,080 --> 04:23:00,840
code it might get a little bit crazy in
6984
04:22:58,720 --> 04:23:02,760
here but as you can see these will
6985
04:23:00,840 --> 04:23:04,159
actually work if you pick any random
6986
04:23:02,760 --> 04:23:06,880
numbers you can see that they're all
6987
04:23:04,159 --> 04:23:08,319
divisible by two and the same thing goes
6988
04:23:06,880 --> 04:23:12,840
for odd you can see that they're not
6989
04:23:08,319 --> 04:23:14,399
divisible at two cuz they're all odd
6990
04:23:12,840 --> 04:23:16,439
so in this exercise we're going to cover
6991
04:23:14,399 --> 04:23:18,279
a little task I like this call this task
6992
04:23:16,439 --> 04:23:20,560
an array of multiples so we want to
6993
04:23:18,279 --> 04:23:22,199
Define initialize two integers and num
6994
04:23:20,560 --> 04:23:24,760
into length so in the case of this
6995
04:23:22,199 --> 04:23:26,720
example we have a number seven and the
6996
04:23:24,760 --> 04:23:28,720
length of five so what this will do is
6997
04:23:26,720 --> 04:23:32,359
it will essentially give you seven times
6998
04:23:28,720 --> 04:23:34,119
table but only the first five numbers 74
6999
04:23:32,359 --> 04:23:37,359
21 28 and
7000
04:23:34,119 --> 04:23:39,319
35 we take the first number which is num
7001
04:23:37,359 --> 04:23:40,800
which will give us the multiples and we
7002
04:23:39,319 --> 04:23:42,520
take the second number which is the
7003
04:23:40,800 --> 04:23:44,479
length and that gives us the size of the
7004
04:23:42,520 --> 04:23:46,600
array so we create the integer array
7005
04:23:44,479 --> 04:23:48,359
with the size of length Loop through the
7006
04:23:46,600 --> 04:23:52,040
array and insert the loop count times
7007
04:23:48,359 --> 04:23:54,159
byome CU this is 1 * 7 2 * 7 and so on
7008
04:23:52,040 --> 04:23:56,119
so forth until we reach the length and
7009
04:23:54,159 --> 04:23:58,080
we place into the array and then finally
7010
04:23:56,119 --> 04:23:59,840
we paint the array just to give us these
7011
04:23:58,080 --> 04:24:02,239
values out to the console all right so
7012
04:23:59,840 --> 04:24:04,800
let's get started we can have an in norm
7013
04:24:02,239 --> 04:24:07,319
and make it equal to 7 and we have it in
7014
04:24:04,800 --> 04:24:08,800
length and make that one equal to five
7015
04:24:07,319 --> 04:24:11,159
now that we defined initialize the two
7016
04:24:08,800 --> 04:24:14,279
integers let's create the int array with
7017
04:24:11,159 --> 04:24:16,760
the size length in Array result equals
7018
04:24:14,279 --> 04:24:18,279
new int and we want size length after
7019
04:24:16,760 --> 04:24:20,399
that we want to create our for Loop now
7020
04:24:18,279 --> 04:24:23,520
this is the tricky part if we try go
7021
04:24:20,399 --> 04:24:25,239
from zero all the way to result. length
7022
04:24:23,520 --> 04:24:27,439
then let's see what happens so we want
7023
04:24:25,239 --> 04:24:29,880
to start result I you want to make that
7024
04:24:27,439 --> 04:24:32,000
equal to number which is our array of
7025
04:24:29,880 --> 04:24:33,600
multiples Times by the value that we're
7026
04:24:32,000 --> 04:24:35,600
currently up to now let's print our
7027
04:24:33,600 --> 04:24:38,199
array using for each Loop to see the
7028
04:24:35,600 --> 04:24:40,720
value so we can say V item inside our
7029
04:24:38,199 --> 04:24:42,720
result then we can have a console right
7030
04:24:40,720 --> 04:24:45,399
that just prints one of them at a time
7031
04:24:42,720 --> 04:24:47,239
so we can say item followed by spits now
7032
04:24:45,399 --> 04:24:51,760
let's see what happens when we run
7033
04:24:47,239 --> 04:24:53,640
it so we get 0 14 21 28 as you can see
7034
04:24:51,760 --> 04:24:56,399
these are the first four numbers so
7035
04:24:53,640 --> 04:24:58,479
we're close but we're nearly there
7036
04:24:56,399 --> 04:25:00,399
because our array starts at zero and it
7037
04:24:58,479 --> 04:25:03,680
goes to the length which in this case is
7038
04:25:00,399 --> 04:25:06,520
going to be 0 to less than five which
7039
04:25:03,680 --> 04:25:09,119
will cover the positions of 0 to
7040
04:25:06,520 --> 04:25:13,399
4 with these positions it's going to do
7041
04:25:09,119 --> 04:25:14,960
0 * 7 1 * 7 and then get to 4 * 7 and
7042
04:25:13,399 --> 04:25:17,479
not actually get to the fifth because
7043
04:25:14,960 --> 04:25:19,720
we're starting from zero so
7044
04:25:17,479 --> 04:25:22,119
alternatively what we can do or what you
7045
04:25:19,720 --> 04:25:24,520
think you can do is changes to a one and
7046
04:25:22,119 --> 04:25:26,800
changes to equal to so now what it's
7047
04:25:24,520 --> 04:25:28,840
doing is it's going from one all the way
7048
04:25:26,800 --> 04:25:30,880
to five which is still covering the same
7049
04:25:28,840 --> 04:25:33,600
amount of positions it's just one index
7050
04:25:30,880 --> 04:25:35,600
forward so now if we try to run the code
7051
04:25:33,600 --> 04:25:37,159
then we going a runtime exception and
7052
04:25:35,600 --> 04:25:39,600
the reason for that is it's trying to
7053
04:25:37,159 --> 04:25:42,960
access position number five if we hover
7054
04:25:39,600 --> 04:25:45,199
over I we have five and if you hover
7055
04:25:42,960 --> 04:25:47,720
over the result we actually have some of
7056
04:25:45,199 --> 04:25:49,439
the right values except it's trying to
7057
04:25:47,720 --> 04:25:51,800
get to the fifth position and it's not
7058
04:25:49,439 --> 04:25:53,800
working the reason why the zeroth index
7059
04:25:51,800 --> 04:25:55,640
is actually zero is simply because
7060
04:25:53,800 --> 04:25:57,560
that's the same value as the default
7061
04:25:55,640 --> 04:25:59,680
value because our for Loop started at in
7062
04:25:57,560 --> 04:26:01,840
I it's actually only starting from here
7063
04:25:59,680 --> 04:26:04,000
and going down so this calculation is
7064
04:26:01,840 --> 04:26:06,600
actually correct but it needs to be back
7065
04:26:04,000 --> 04:26:09,880
one we need to start the multiplication
7066
04:26:06,600 --> 04:26:12,000
on IAL 0 but our for Loop needs to start
7067
04:26:09,880 --> 04:26:13,840
on IAL 1 so we can get all the way
7068
04:26:12,000 --> 04:26:15,920
through
7069
04:26:13,840 --> 04:26:18,439
so we have two ways of doing this we
7070
04:26:15,920 --> 04:26:20,960
could say I minus 1 so even though it
7071
04:26:18,439 --> 04:26:23,560
goes from 1 to 5 it's still assigning
7072
04:26:20,960 --> 04:26:25,040
into the array using 0 to four so if we
7073
04:26:23,560 --> 04:26:27,840
run that we can see that that will give
7074
04:26:25,040 --> 04:26:30,000
us the desired result alternatively what
7075
04:26:27,840 --> 04:26:32,359
we can actually do instead of doing IUS
7076
04:26:30,000 --> 04:26:35,279
one inside here we can define a variable
7077
04:26:32,359 --> 04:26:37,720
called counter and make it equal to zero
7078
04:26:35,279 --> 04:26:39,159
we can change this to counter and we
7079
04:26:37,720 --> 04:26:40,720
don't need to change the multiplication
7080
04:26:39,159 --> 04:26:43,319
because that's already going from one to
7081
04:26:40,720 --> 04:26:45,040
the length which is 1 to 5 in this case
7082
04:26:43,319 --> 04:26:47,840
after the for Loop is run we need to
7083
04:26:45,040 --> 04:26:49,920
remember to increment counter so there
7084
04:26:47,840 --> 04:26:52,119
we go we have the desired effect another
7085
04:26:49,920 --> 04:26:53,960
hack if you're interested if you have
7086
04:26:52,119 --> 04:26:55,800
two variables that you want to increment
7087
04:26:53,960 --> 04:26:57,840
inside the for Loop you can see that we
7088
04:26:55,800 --> 04:26:59,720
already have I ++ in here which will
7089
04:26:57,840 --> 04:27:01,760
increment it by one and we want to
7090
04:26:59,720 --> 04:27:04,520
increment counter by one as well every
7091
04:27:01,760 --> 04:27:06,840
single time it loops around what you can
7092
04:27:04,520 --> 04:27:09,080
actually do is remove this and you know
7093
04:27:06,840 --> 04:27:10,439
how these are broken up by semicolons
7094
04:27:09,080 --> 04:27:13,000
that indicates that the section has
7095
04:27:10,439 --> 04:27:14,640
ended the first sections for declaration
7096
04:27:13,000 --> 04:27:16,000
the second is for the range and the
7097
04:27:14,640 --> 04:27:17,960
third is so you can increment the
7098
04:27:16,000 --> 04:27:19,960
variable or decrement it when you trying
7099
04:27:17,960 --> 04:27:22,359
to increment two variables inside a for
7100
04:27:19,960 --> 04:27:24,000
Loop instead of placing a semicolon here
7101
04:27:22,359 --> 04:27:26,359
and type in the next one you can
7102
04:27:24,000 --> 04:27:28,560
actually place a comma and do counter
7103
04:27:26,359 --> 04:27:30,640
Plus+ what that will tell the fall Loop
7104
04:27:28,560 --> 04:27:33,000
is every single time you go through you
7105
04:27:30,640 --> 04:27:36,159
want to do a series of these actions and
7106
04:27:33,000 --> 04:27:37,720
because we have i++ and counter Plus+
7107
04:27:36,159 --> 04:27:40,000
then that's going to work exactly the
7108
04:27:37,720 --> 04:27:42,199
same way it just prevents us having
7109
04:27:40,000 --> 04:27:43,800
counter Plus+ down here and we can move
7110
04:27:42,199 --> 04:27:45,600
move it inside the for Loop just so it
7111
04:27:43,800 --> 04:27:47,800
doesn't for us and it looks a lot
7112
04:27:45,600 --> 04:27:49,239
cleaner cuz you can clearly see that
7113
04:27:47,800 --> 04:27:50,920
every single time we loop around this
7114
04:27:49,239 --> 04:27:54,000
for loop we're going to increment these
7115
04:27:50,920 --> 04:27:55,520
two variables there we go so let's try
7116
04:27:54,000 --> 04:27:57,720
to put some different values in here
7117
04:27:55,520 --> 04:28:00,199
maybe make this 10 and we can have 10 on
7118
04:27:57,720 --> 04:28:02,640
both to give us the first 10 numbers
7119
04:28:00,199 --> 04:28:04,399
there we go from 10 all the way to 100
7120
04:28:02,640 --> 04:28:06,199
let's have a little recap so we're
7121
04:28:04,399 --> 04:28:08,080
essentially just making an array that
7122
04:28:06,199 --> 04:28:10,279
will give us back all the multiples so
7123
04:28:08,080 --> 04:28:11,640
it's like a mini times table but the
7124
04:28:10,279 --> 04:28:14,159
fact that we're doing it dynamically
7125
04:28:11,640 --> 04:28:16,119
using our array is quite important what
7126
04:28:14,159 --> 04:28:18,119
you can do if you wanted to is to extend
7127
04:28:16,119 --> 04:28:20,080
this program you can ask the user to
7128
04:28:18,119 --> 04:28:22,040
type these into the console and then
7129
04:28:20,080 --> 04:28:23,720
convert them and use them right now
7130
04:28:22,040 --> 04:28:25,080
we're just statically typing them in and
7131
04:28:23,720 --> 04:28:27,640
that's not really an issue since the
7132
04:28:25,080 --> 04:28:29,399
main focus was doing this for Loop if
7133
04:28:27,640 --> 04:28:31,000
you wanted to you could ask the user and
7134
04:28:29,399 --> 04:28:32,439
you can type these values in so you
7135
04:28:31,000 --> 04:28:34,040
don't have to change them every single
7136
04:28:32,439 --> 04:28:35,520
time you compile so now we've got the
7137
04:28:34,040 --> 04:28:37,520
number and the length let's make our
7138
04:28:35,520 --> 04:28:39,920
interray call result and initialize it
7139
04:28:37,520 --> 04:28:42,520
to the length we set up this counter so
7140
04:28:39,920 --> 04:28:43,960
we simply didn't have to do I minus one
7141
04:28:42,520 --> 04:28:46,279
here so it looks a little bit more
7142
04:28:43,960 --> 04:28:48,640
intuitive we start the follow from one
7143
04:28:46,279 --> 04:28:50,760
and go to the length so we can access 1
7144
04:28:48,640 --> 04:28:52,359
to five and the reason why we want to do
7145
04:28:50,760 --> 04:28:55,399
one to five so when we do our
7146
04:28:52,359 --> 04:28:56,800
multiplication it doesn't start at zero
7147
04:28:55,399 --> 04:28:59,080
these two are going through the exact
7148
04:28:56,800 --> 04:29:01,279
same amount of indexes 1 to 5 is five
7149
04:28:59,080 --> 04:29:03,359
numbers and 0 to four is also five
7150
04:29:01,279 --> 04:29:05,319
numbers so when we do a multiplication
7151
04:29:03,359 --> 04:29:07,359
we want to start from one since the Tim
7152
04:29:05,319 --> 04:29:09,040
table normally start at one and we go to
7153
04:29:07,359 --> 04:29:10,479
the length the reason why we're using
7154
04:29:09,040 --> 04:29:12,600
the counter is so we don't have to do
7155
04:29:10,479 --> 04:29:14,199
IUS one in here and it breaks up the
7156
04:29:12,600 --> 04:29:16,399
logic a little bit and it looks nicer
7157
04:29:14,199 --> 04:29:18,720
without our counter we start our counter
7158
04:29:16,399 --> 04:29:21,119
at zero and instead of incrementing down
7159
04:29:18,720 --> 04:29:23,600
here we use this fancy comma and we can
7160
04:29:21,119 --> 04:29:25,439
say we want to increment I and counter
7161
04:29:23,600 --> 04:29:26,960
every time the for Loop goes around so
7162
04:29:25,439 --> 04:29:28,840
we don't need to include the counter
7163
04:29:26,960 --> 04:29:31,199
increment inside the for Loop and this
7164
04:29:28,840 --> 04:29:33,239
one line is pretty simple we say result
7165
04:29:31,199 --> 04:29:35,080
the index of counter which will start at
7166
04:29:33,239 --> 04:29:37,119
zero which is the start of the array
7167
04:29:35,080 --> 04:29:38,880
since it's zero base and we say that's
7168
04:29:37,119 --> 04:29:40,640
equal to the number and the number is
7169
04:29:38,880 --> 04:29:43,119
going to be a fixed number since this is
7170
04:29:40,640 --> 04:29:45,359
the multiple and we say number times by
7171
04:29:43,119 --> 04:29:47,040
I and in this case it goes from one all
7172
04:29:45,359 --> 04:29:49,119
the way to the length so we get
7173
04:29:47,040 --> 04:29:51,199
essentially the times table and at the
7174
04:29:49,119 --> 04:29:52,560
end we just use a simple for each Loop
7175
04:29:51,199 --> 04:29:54,439
that will go through all the items
7176
04:29:52,560 --> 04:29:56,159
inside the result array and just print
7177
04:29:54,439 --> 04:29:58,159
them out to the screen with a comma
7178
04:29:56,159 --> 04:30:00,720
after each other using console. right
7179
04:29:58,159 --> 04:30:02,520
just so they're all on one line this is
7180
04:30:00,720 --> 04:30:04,680
the start of the function section
7181
04:30:02,520 --> 04:30:06,720
functions can also be called method they
7182
04:30:04,680 --> 04:30:08,880
are used interchangeably in the
7183
04:30:06,720 --> 04:30:10,760
introduction we discussed dry which
7184
04:30:08,880 --> 04:30:12,880
means don't repeat yourself and the
7185
04:30:10,760 --> 04:30:14,880
whole point of dry is so you don't have
7186
04:30:12,880 --> 04:30:16,479
multiple sections of code that do
7187
04:30:14,880 --> 04:30:18,960
basically the same thing you want to
7188
04:30:16,479 --> 04:30:20,720
wrap it around in a function functions
7189
04:30:18,960 --> 04:30:22,920
are used to improve the readability of
7190
04:30:20,720 --> 04:30:25,359
your code and the reusability of the
7191
04:30:22,920 --> 04:30:27,479
code as well having multiple section
7192
04:30:25,359 --> 04:30:29,720
code that are basically exactly the same
7193
04:30:27,479 --> 04:30:31,640
can be more prone to errors if you wrap
7194
04:30:29,720 --> 04:30:33,359
them around and place in One Singular
7195
04:30:31,640 --> 04:30:35,600
function you can make sure that the
7196
04:30:33,359 --> 04:30:37,520
error prone and then wherever you use it
7197
04:30:35,600 --> 04:30:39,279
it will be error prone as well if you
7198
04:30:37,520 --> 04:30:41,359
have to copy and paste and tweak some
7199
04:30:39,279 --> 04:30:43,760
code then you're actually exaggerating
7200
04:30:41,359 --> 04:30:45,239
the amount of error that could occur and
7201
04:30:43,760 --> 04:30:47,159
plus it makes your code look a lot
7202
04:30:45,239 --> 04:30:48,920
cleaner and the structure a lot better
7203
04:30:47,159 --> 04:30:51,119
since you can name the functions so
7204
04:30:48,920 --> 04:30:54,199
let's take a little example we have a
7205
04:30:51,119 --> 04:30:56,600
static void main straight into here this
7206
04:30:54,199 --> 04:30:59,279
is a function or a method and these are
7207
04:30:56,600 --> 04:31:02,000
the parameters for the function The NET
7208
04:30:59,279 --> 04:31:04,760
Framework expects every C program to
7209
04:31:02,000 --> 04:31:07,040
have a main function with a capital m
7210
04:31:04,760 --> 04:31:08,800
and it needs to be a static and avoid
7211
04:31:07,040 --> 04:31:10,800
the parameters don't exactly matter we
7212
04:31:08,800 --> 04:31:12,760
can also remove them and it won't be a
7213
04:31:10,800 --> 04:31:15,199
problem the only thing that matters is
7214
04:31:12,760 --> 04:31:17,319
this needs to be called main if we drop
7215
04:31:15,199 --> 04:31:19,760
the lower case on it and try and run the
7216
04:31:17,319 --> 04:31:22,439
code then we're going to get a build
7217
04:31:19,760 --> 04:31:25,080
error program does not contain a static
7218
04:31:22,439 --> 04:31:26,800
main method suitable for an entry point
7219
04:31:25,080 --> 04:31:29,920
so if you change this back to M and run
7220
04:31:26,800 --> 04:31:31,520
it again and then it works fine so
7221
04:31:29,920 --> 04:31:33,600
that's one thing to note about The NET
7222
04:31:31,520 --> 04:31:35,560
Framework is you need to include the
7223
04:31:33,600 --> 04:31:37,960
main function otherwise the program
7224
04:31:35,560 --> 04:31:40,319
won't run and it won't even compile so
7225
04:31:37,960 --> 04:31:41,600
let's take a look at how we can use this
7226
04:31:40,319 --> 04:31:44,319
we can have something like a console
7227
04:31:41,600 --> 04:31:46,439
right line and we can say welcome to my
7228
04:31:44,319 --> 04:31:48,279
program this is a line that we're
7229
04:31:46,439 --> 04:31:49,880
probably only going to use once but
7230
04:31:48,279 --> 04:31:52,040
let's say this is welcome in someone's
7231
04:31:49,880 --> 04:31:53,760
name welcome abber let's say you had
7232
04:31:52,040 --> 04:31:55,840
this in your software in many different
7233
04:31:53,760 --> 04:31:58,040
places you don't want to be recalling
7234
04:31:55,840 --> 04:31:59,680
this or retyping out this message in
7235
04:31:58,040 --> 04:32:01,800
case you type it wrong the fifth time
7236
04:31:59,680 --> 04:32:04,319
and you get a error of course this won't
7237
04:32:01,800 --> 04:32:06,119
be a syntax or a runtime error it'll
7238
04:32:04,319 --> 04:32:07,880
just simply be a logic error because
7239
04:32:06,119 --> 04:32:09,880
it's a typo so in order to make sure
7240
04:32:07,880 --> 04:32:12,000
this typo never occurs we can actually
7241
04:32:09,880 --> 04:32:14,080
make another function so outside of
7242
04:32:12,000 --> 04:32:15,920
these cury braces and just still within
7243
04:32:14,080 --> 04:32:18,119
the class we want to create a new
7244
04:32:15,920 --> 04:32:21,359
function so we want to follow this exact
7245
04:32:18,119 --> 04:32:24,159
same approach we can say static then
7246
04:32:21,359 --> 04:32:25,319
void and we can call it welcome message
7247
04:32:24,159 --> 04:32:26,640
and we don't need to give it any
7248
04:32:25,319 --> 04:32:28,800
parameters cuz we're not really going to
7249
04:32:26,640 --> 04:32:30,680
give it any values just for now now that
7250
04:32:28,800 --> 04:32:32,880
we've made that we have no syntax errors
7251
04:32:30,680 --> 04:32:34,680
and we can run it like this except of
7252
04:32:32,880 --> 04:32:36,640
course there's no body so let's place
7253
04:32:34,680 --> 04:32:38,279
our console right line within here now
7254
04:32:36,640 --> 04:32:40,560
if you run the code nothing is going to
7255
04:32:38,279 --> 04:32:43,479
happen and the reason for that is the
7256
04:32:40,560 --> 04:32:45,479
code will still execute in the main we
7257
04:32:43,479 --> 04:32:47,439
haven't called this function inside the
7258
04:32:45,479 --> 04:32:49,279
main we've defined it and called it
7259
04:32:47,439 --> 04:32:50,920
welcome message and we've gave it the
7260
04:32:49,279 --> 04:32:53,000
contents but we haven't actually made
7261
04:32:50,920 --> 04:32:55,279
use of it and you can see that when it's
7262
04:32:53,000 --> 04:32:57,560
highlighted in a darker color that means
7263
04:32:55,279 --> 04:33:00,680
it's not being used as it says here
7264
04:32:57,560 --> 04:33:02,560
private member welcome message is unused
7265
04:33:00,680 --> 04:33:04,561
so now we can simply type in here
7266
04:33:02,560 --> 04:33:06,240
welcome message followed by an Open
7267
04:33:04,561 --> 04:33:07,600
Bracket and then a Clos bracket to
7268
04:33:06,240 --> 04:33:09,561
denote the parameters that we're going
7269
04:33:07,600 --> 04:33:11,840
to give it and of course in this case
7270
04:33:09,561 --> 04:33:13,879
there is no parameters and then we close
7271
04:33:11,840 --> 04:33:15,480
the brackets and add a semicolon now if
7272
04:33:13,879 --> 04:33:17,680
you run the code you can see it says
7273
04:33:15,480 --> 04:33:19,760
welcome AB so just to go over some of
7274
04:33:17,680 --> 04:33:22,279
these words that we're using static
7275
04:33:19,760 --> 04:33:24,719
means that we can access it anywhere and
7276
04:33:22,279 --> 04:33:27,160
everywhere what I mean by that is we
7277
04:33:24,719 --> 04:33:28,959
don't have the keyword new just before
7278
04:33:27,160 --> 04:33:31,080
this we don't need to make a new
7279
04:33:28,959 --> 04:33:33,160
instance of the program class in order
7280
04:33:31,080 --> 04:33:35,199
to access this so what that means is
7281
04:33:33,160 --> 04:33:37,920
when we make future programs we're going
7282
04:33:35,199 --> 04:33:40,039
to have some functions inside our class
7283
04:33:37,920 --> 04:33:42,279
inside the class you can actually denote
7284
04:33:40,039 --> 04:33:44,039
if a function is going to be static or
7285
04:33:42,279 --> 04:33:46,199
it's not going to be static you don't
7286
04:33:44,039 --> 04:33:48,119
have to specify is it static but this
7287
04:33:46,199 --> 04:33:49,680
won't let you call it from here because
7288
04:33:48,119 --> 04:33:51,799
it's unaware where this function's
7289
04:33:49,680 --> 04:33:53,879
coming from an object reference is
7290
04:33:51,799 --> 04:33:56,520
required for the non-static field method
7291
04:33:53,879 --> 04:33:58,359
or property because this is static you
7292
04:33:56,520 --> 04:34:00,680
need to reference something else from a
7293
04:33:58,359 --> 04:34:02,520
static nature all static means is you
7294
04:34:00,680 --> 04:34:04,639
don't have to create a new instance of
7295
04:34:02,520 --> 04:34:06,160
this thing in order to use it we'll see
7296
04:34:04,639 --> 04:34:08,039
different examples in the future where
7297
04:34:06,160 --> 04:34:10,199
you have to create an instance in order
7298
04:34:08,039 --> 04:34:13,000
to use something so let me just show you
7299
04:34:10,199 --> 04:34:15,320
quickly if you have a test and I say
7300
04:34:13,000 --> 04:34:17,639
static void
7301
04:34:15,320 --> 04:34:19,520
something and we can just print out
7302
04:34:17,639 --> 04:34:21,959
nothing to the screen and let's try and
7303
04:34:19,520 --> 04:34:24,000
use this so because this class is
7304
04:34:21,959 --> 04:34:28,240
outside of the program we have to use
7305
04:34:24,000 --> 04:34:30,240
the notation test Dot and then it should
7306
04:34:28,240 --> 04:34:31,719
be available within here and the reason
7307
04:34:30,240 --> 04:34:33,799
why it's not is because we need to
7308
04:34:31,719 --> 04:34:35,480
insert this public word just before it
7309
04:34:33,799 --> 04:34:37,480
and public means everything has access
7310
04:34:35,480 --> 04:34:39,320
to it and static means you don't need to
7311
04:34:37,480 --> 04:34:41,279
create an instance now if I type in the
7312
04:34:39,320 --> 04:34:43,320
dot again you can see we can now see
7313
04:34:41,279 --> 04:34:45,240
something
7314
04:34:43,320 --> 04:34:47,240
and the reason why we can see something
7315
04:34:45,240 --> 04:34:49,119
is because this is a static now if I
7316
04:34:47,240 --> 04:34:50,920
remove the static word then you see that
7317
04:34:49,119 --> 04:34:53,000
we'll get a compile error because it
7318
04:34:50,920 --> 04:34:55,480
doesn't know where this is now if you
7319
04:34:53,000 --> 04:34:58,840
don't use static you need to do this you
7320
04:34:55,480 --> 04:35:01,080
need to say test test equals new test so
7321
04:34:58,840 --> 04:35:03,000
you make an instance of the class and
7322
04:35:01,080 --> 04:35:05,039
then inside test then we get the
7323
04:35:03,000 --> 04:35:08,119
something so that's the difference
7324
04:35:05,039 --> 04:35:09,920
between static and not static
7325
04:35:08,119 --> 04:35:11,760
essentially it's accessible from
7326
04:35:09,920 --> 04:35:13,520
everywhere but if you don't want to be
7327
04:35:11,760 --> 04:35:15,480
accessible from everywhere then you can
7328
04:35:13,520 --> 04:35:17,000
just simply remove the static word and
7329
04:35:15,480 --> 04:35:19,160
the user will have to create a new
7330
04:35:17,000 --> 04:35:21,719
instance before they can use it so this
7331
04:35:19,160 --> 04:35:23,959
is what it's like without
7332
04:35:21,719 --> 04:35:25,959
static and this is what it's like with
7333
04:35:23,959 --> 04:35:27,240
static don't worry if you don't
7334
04:35:25,959 --> 04:35:29,199
understand this too much this was just a
7335
04:35:27,240 --> 04:35:31,039
quick overiew but I have a dedicated
7336
04:35:29,199 --> 04:35:32,959
video later on in the course so don't
7337
04:35:31,039 --> 04:35:34,480
worry too much about it so now that
7338
04:35:32,959 --> 04:35:36,240
we've made a function and we've called
7339
04:35:34,480 --> 04:35:39,000
it successfully in the domain and it's
7340
04:35:36,240 --> 04:35:39,000
prined out to the
7341
04:35:39,359 --> 04:35:43,799
console so when we're declaring function
7342
04:35:42,000 --> 04:35:45,480
we want to specify what return type
7343
04:35:43,799 --> 04:35:48,039
we're going to have we have a static
7344
04:35:45,480 --> 04:35:50,080
void Main in here if you hover over main
7345
04:35:48,039 --> 04:35:51,920
it says void which means at the end of
7346
04:35:50,080 --> 04:35:54,000
this function it doesn't return anything
7347
04:35:51,920 --> 04:35:56,600
hence its void if you have a look at
7348
04:35:54,000 --> 04:35:58,279
something like an in. trass when we open
7349
04:35:56,600 --> 04:36:00,680
the brackets you can see that it takes
7350
04:35:58,279 --> 04:36:03,359
in a string and it takes in an integer
7351
04:36:00,680 --> 04:36:04,959
out parameter it also returns us back to
7352
04:36:03,359 --> 04:36:08,320
the Boolean so we can have something
7353
04:36:04,959 --> 04:36:10,320
like 1 2 3 out int result and then it
7354
04:36:08,320 --> 04:36:12,199
doesn't actually complain about anything
7355
04:36:10,320 --> 04:36:14,799
but what we can do is also so just store
7356
04:36:12,199 --> 04:36:17,240
this in to a bu success and then print
7357
04:36:14,799 --> 04:36:19,719
it out to the
7358
04:36:17,240 --> 04:36:22,359
screen there we go so it says it was
7359
04:36:19,719 --> 04:36:24,719
successfully true if we remove this or
7360
04:36:22,359 --> 04:36:26,561
whether we keep it in or not trip pass
7361
04:36:24,719 --> 04:36:28,840
function doesn't actually complain but
7362
04:36:26,561 --> 04:36:30,561
the main point here is it defines back a
7363
04:36:28,840 --> 04:36:32,240
bull that means at the end of this
7364
04:36:30,561 --> 04:36:34,000
function it will always return you back
7365
04:36:32,240 --> 04:36:35,799
with a bull and it's up to you if you
7366
04:36:34,000 --> 04:36:37,400
want to make use of it and you can
7367
04:36:35,799 --> 04:36:39,561
alternatively you can place this
7368
04:36:37,400 --> 04:36:41,039
straight inside the console right line
7369
04:36:39,561 --> 04:36:43,080
and get the value printed straight to
7370
04:36:41,039 --> 04:36:44,959
the screen or you could wrap this around
7371
04:36:43,080 --> 04:36:47,240
inside an if statement like we've done
7372
04:36:44,959 --> 04:36:48,879
in the past Etc and make use of it that
7373
04:36:47,240 --> 04:36:50,879
way and have the result being printed
7374
04:36:48,879 --> 04:36:52,760
out inside here the point is you have
7375
04:36:50,879 --> 04:36:54,799
functions that return something and
7376
04:36:52,760 --> 04:36:57,199
functions that don't if you remember
7377
04:36:54,799 --> 04:36:59,039
that we had the array. copy for example
7378
04:36:57,199 --> 04:37:01,160
you'll see that they have void here
7379
04:36:59,039 --> 04:37:02,799
because they just apply a function to
7380
04:37:01,160 --> 04:37:04,719
something but they don't actually give
7381
04:37:02,799 --> 04:37:06,879
you something back this is the
7382
04:37:04,719 --> 04:37:09,799
difference of having something like this
7383
04:37:06,879 --> 04:37:12,240
a string test that says hello and then
7384
04:37:09,799 --> 04:37:14,240
you can do test. replace and then you
7385
04:37:12,240 --> 04:37:16,840
can return you back with the string and
7386
04:37:14,240 --> 04:37:19,920
it says here it returns a new string
7387
04:37:16,840 --> 04:37:22,359
whereas when we had a string builder for
7388
04:37:19,920 --> 04:37:24,320
example and we did string builder.
7389
04:37:22,359 --> 04:37:26,000
replace you can see that although it
7390
04:37:24,320 --> 04:37:28,199
does say it replaces a new string
7391
04:37:26,000 --> 04:37:30,000
Builder you can type anything inside
7392
04:37:28,199 --> 04:37:31,879
here and it will still affect the
7393
04:37:30,000 --> 04:37:34,799
current string Builder it won't have to
7394
04:37:31,879 --> 04:37:36,920
do SB equals s SP replace as you would
7395
04:37:34,799 --> 04:37:38,920
need to do here because this returns you
7396
04:37:36,920 --> 04:37:40,760
back with a new string and if you wanted
7397
04:37:38,920 --> 04:37:44,359
to replace test you would literally have
7398
04:37:40,760 --> 04:37:46,199
to say test equals test. replace with
7399
04:37:44,359 --> 04:37:48,199
something like a string Builder all the
7400
04:37:46,199 --> 04:37:50,879
array functions you don't actually have
7401
04:37:48,199 --> 04:37:52,840
to do any of that so you can say array.
7402
04:37:50,879 --> 04:37:54,799
empty and it will just return the empty
7403
04:37:52,840 --> 04:37:56,439
array and the ones that we used before
7404
04:37:54,799 --> 04:37:58,240
things like equals and that will give
7405
04:37:56,439 --> 04:38:00,520
you back a Bool because that's
7406
04:37:58,240 --> 04:38:02,320
determined whether it's equal or not but
7407
04:38:00,520 --> 04:38:04,799
if you're doing functions like sort for
7408
04:38:02,320 --> 04:38:07,000
example and doing things like reverse it
7409
04:38:04,799 --> 04:38:08,959
just simply applies the data but it
7410
04:38:07,000 --> 04:38:10,639
doesn't return you back with anything so
7411
04:38:08,959 --> 04:38:12,680
you can literally just call it inside
7412
04:38:10,639 --> 04:38:15,279
here put your array in the brackets and
7413
04:38:12,680 --> 04:38:17,320
it'll use it happily now depending on
7414
04:38:15,279 --> 04:38:19,680
your user case you need to define
7415
04:38:17,320 --> 04:38:22,119
whether or not you need to use if return
7416
04:38:19,680 --> 04:38:24,520
type or not for now let's just cover
7417
04:38:22,119 --> 04:38:27,039
void which means it won't do anything so
7418
04:38:24,520 --> 04:38:29,719
let's just Define a function static void
7419
04:38:27,039 --> 04:38:31,480
create and print an array open the
7420
04:38:29,719 --> 04:38:34,240
brackets close the brackets and we can
7421
04:38:31,480 --> 04:38:36,240
have it int numbers make it equal to new
7422
04:38:34,240 --> 04:38:39,760
int with length three and let's just
7423
04:38:36,240 --> 04:38:42,480
have 0 1 and two inside here as the
7424
04:38:39,760 --> 04:38:45,520
initializer let's just do a for each VAR
7425
04:38:42,480 --> 04:38:47,920
item inside numbers and then just do a
7426
04:38:45,520 --> 04:38:50,400
console right string interpolation and
7427
04:38:47,920 --> 04:38:51,959
just have item followed by space and now
7428
04:38:50,400 --> 04:38:53,920
that it's dark gray because we haven't
7429
04:38:51,959 --> 04:38:55,799
called the function so let's make use of
7430
04:38:53,920 --> 04:38:58,959
it at the top of our code and let's run
7431
04:38:55,799 --> 04:39:00,920
it now you can see we get 012 printed to
7432
04:38:58,959 --> 04:39:03,600
the screen and if we make this into a
7433
04:39:00,920 --> 04:39:05,480
five and simply just add more values in
7434
04:39:03,600 --> 04:39:07,359
here and then we run the code you'll see
7435
04:39:05,480 --> 04:39:09,199
that now it all ofd but because this
7436
04:39:07,359 --> 04:39:11,119
doesn't return us back or anything and
7437
04:39:09,199 --> 04:39:13,359
we don't actually use the integer array
7438
04:39:11,119 --> 04:39:15,639
I outside of these curly braces it's not
7439
04:39:13,359 --> 04:39:17,920
actually available up here numbers won't
7440
04:39:15,639 --> 04:39:20,959
be accessible inside here because it's
7441
04:39:17,920 --> 04:39:23,000
simply not within the scope numbers is
7442
04:39:20,959 --> 04:39:25,080
only available within this curly brace
7443
04:39:23,000 --> 04:39:27,359
and this curly brace which means when
7444
04:39:25,080 --> 04:39:29,240
this function finishes numbers has now
7445
04:39:27,359 --> 04:39:31,879
been deleted from memory so you can't
7446
04:39:29,240 --> 04:39:34,240
access it after this line void doesn't
7447
04:39:31,879 --> 04:39:36,080
do anything it simply just does the
7448
04:39:34,240 --> 04:39:37,798
contents of the function and as soon as
7449
04:39:36,080 --> 04:39:39,718
it reaches the bottom that's all that
7450
04:39:37,798 --> 04:39:41,958
happens you can have another function
7451
04:39:39,718 --> 04:39:44,638
defined that can return a value
7452
04:39:41,958 --> 04:39:46,520
like I said with in. trass for example
7453
04:39:44,638 --> 04:39:48,440
at the end of the function you'll see a
7454
04:39:46,520 --> 04:39:50,400
word that says something like this it
7455
04:39:48,440 --> 04:39:52,760
will have return and then return back
7456
04:39:50,400 --> 04:39:54,480
something followed by a semicolon and
7457
04:39:52,760 --> 04:39:56,440
that will dictate that as soon as that
7458
04:39:54,480 --> 04:39:58,520
function is done it's going to give you
7459
04:39:56,440 --> 04:40:00,440
something back now we're going to cover
7460
04:39:58,520 --> 04:40:02,320
that in the next video but for now we're
7461
04:40:00,440 --> 04:40:04,000
only going to cover void functions so we
7462
04:40:02,320 --> 04:40:06,000
can have something more obvious like a
7463
04:40:04,000 --> 04:40:08,400
static void welcome
7464
04:40:06,000 --> 04:40:11,480
message and simply just do a console
7465
04:40:08,400 --> 04:40:12,798
right line welcome to my program and we
7466
04:40:11,480 --> 04:40:14,760
don't need to return anything here
7467
04:40:12,798 --> 04:40:17,160
because all this function will do is
7468
04:40:14,760 --> 04:40:18,718
simply print out a welcome to the screen
7469
04:40:17,160 --> 04:40:20,718
and we can use that just before the
7470
04:40:18,718 --> 04:40:23,080
print array so we can say welcome
7471
04:40:20,718 --> 04:40:26,200
message Open Bracket close bracket and
7472
04:40:23,080 --> 04:40:28,480
then a semicolon and hit F5 to run and
7473
04:40:26,200 --> 04:40:30,480
there we go as you can see our main is
7474
04:40:28,480 --> 04:40:32,240
really tidy now it gives us function
7475
04:40:30,480 --> 04:40:34,560
names that tells us exactly what's going
7476
04:40:32,240 --> 04:40:36,200
on we're creating and printing an array
7477
04:40:34,560 --> 04:40:38,000
and then welcoming the user to the
7478
04:40:36,200 --> 04:40:40,080
console and then having a console read
7479
04:40:38,000 --> 04:40:42,280
line at the end there isn't an awful lot
7480
04:40:40,080 --> 04:40:43,718
of void functions all you need to know
7481
04:40:42,280 --> 04:40:45,798
is that when you create a function you
7482
04:40:43,718 --> 04:40:47,638
need to give it a return type it returns
7483
04:40:45,798 --> 04:40:49,560
back a certain data type or it simply
7484
04:40:47,638 --> 04:40:51,120
just doesn't and depending on what you
7485
04:40:49,560 --> 04:40:53,360
want to do and what you want to achieve
7486
04:40:51,120 --> 04:40:55,718
with that function really depends on if
7487
04:40:53,360 --> 04:40:57,440
you want to return a data type or not in
7488
04:40:55,718 --> 04:40:59,560
the case of these since they're just
7489
04:40:57,440 --> 04:41:00,920
doing something and then moving on you
7490
04:40:59,560 --> 04:41:02,958
don't actually need to return anything
7491
04:41:00,920 --> 04:41:05,360
by it with in. tripod you want to make
7492
04:41:02,958 --> 04:41:07,520
the function is executed successfully so
7493
04:41:05,360 --> 04:41:09,600
when you use int. tripas you can see
7494
04:41:07,520 --> 04:41:11,400
that returns back a bull and the bull
7495
04:41:09,600 --> 04:41:13,440
determines whether the conversion has
7496
04:41:11,400 --> 04:41:15,160
been successful and based on that return
7497
04:41:13,440 --> 04:41:16,798
value you can determine the next steps
7498
04:41:15,160 --> 04:41:18,920
of your program if all your function
7499
04:41:16,798 --> 04:41:21,280
needs to do is provide some sort of
7500
04:41:18,920 --> 04:41:23,520
functionality and then quit then that's
7501
04:41:21,280 --> 04:41:25,840
fine and avoid is fine but if you need
7502
04:41:23,520 --> 04:41:28,000
to provide some functionality and then
7503
04:41:25,840 --> 04:41:29,878
also return a value then you'll need to
7504
04:41:28,000 --> 04:41:32,958
specify the data type in here which
7505
04:41:29,878 --> 04:41:35,400
we'll cover in the next
7506
04:41:32,958 --> 04:41:37,080
video so far we've covered aoid
7507
04:41:35,400 --> 04:41:38,920
functions but we haven't covered return
7508
04:41:37,080 --> 04:41:40,638
types vo functions are where the
7509
04:41:38,920 --> 04:41:42,560
function just does something but doesn't
7510
04:41:40,638 --> 04:41:45,040
actually return anything hence the name
7511
04:41:42,560 --> 04:41:48,200
void let's start with an example if you
7512
04:41:45,040 --> 04:41:50,200
have a static void print
7513
04:41:48,200 --> 04:41:52,040
introduction might want to just return
7514
04:41:50,200 --> 04:41:53,360
back your name after you've printed this
7515
04:41:52,040 --> 04:41:55,560
line it's going to print it to the
7516
04:41:53,360 --> 04:41:58,040
console and that's all it's going to
7517
04:41:55,560 --> 04:41:59,440
do there we go let's say there's a
7518
04:41:58,040 --> 04:42:01,280
situation where you actually want to use
7519
04:41:59,440 --> 04:42:03,200
this inside your code you can actually
7520
04:42:01,280 --> 04:42:05,320
set the console's title by typing in
7521
04:42:03,200 --> 04:42:06,958
console. title and type in your name
7522
04:42:05,320 --> 04:42:08,798
inside the quotation marks or you can
7523
04:42:06,958 --> 04:42:10,280
type in anything inside here if you run
7524
04:42:08,798 --> 04:42:11,958
that you'll see Abra is in the title of
7525
04:42:10,280 --> 04:42:14,000
the console right now and that looks
7526
04:42:11,958 --> 04:42:15,958
pretty cool what you got to realize is
7527
04:42:14,000 --> 04:42:18,160
now we have two references to the same
7528
04:42:15,958 --> 04:42:20,320
value now at this point we can actually
7529
04:42:18,160 --> 04:42:23,760
do something better we can type in
7530
04:42:20,320 --> 04:42:25,280
static string name equals abber and then
7531
04:42:23,760 --> 04:42:27,320
instead of using abber in here we can
7532
04:42:25,280 --> 04:42:29,360
say name and instead of using abber in
7533
04:42:27,320 --> 04:42:30,798
here we can also say a name and they're
7534
04:42:29,360 --> 04:42:32,400
both using this variable and if you run
7535
04:42:30,798 --> 04:42:34,400
the code now you see that the same
7536
04:42:32,400 --> 04:42:35,958
output will happen you have abber twice
7537
04:42:34,400 --> 04:42:37,520
both in the title and both in the
7538
04:42:35,958 --> 04:42:38,958
console output now the problem with this
7539
04:42:37,520 --> 04:42:40,718
approach is you're actually making use
7540
04:42:38,958 --> 04:42:42,958
of a global variable which we do not
7541
04:42:40,718 --> 04:42:44,760
like code in situation like this you
7542
04:42:42,958 --> 04:42:46,080
shouldn't need to make a global variable
7543
04:42:44,760 --> 04:42:48,040
because there are ways around it so
7544
04:42:46,080 --> 04:42:49,958
instead of having this which has a print
7545
04:42:48,040 --> 04:42:51,520
introduction and we've got the name used
7546
04:42:49,958 --> 04:42:53,280
in there but the name is coming from
7547
04:42:51,520 --> 04:42:54,958
here maybe we can use this somewhere
7548
04:42:53,280 --> 04:42:57,320
else so let's say we make another
7549
04:42:54,958 --> 04:42:58,878
function called Static and instead of
7550
04:42:57,320 --> 04:43:01,560
void we want to actually return back a
7551
04:42:58,878 --> 04:43:03,440
string so we can say string and then we
7552
04:43:01,560 --> 04:43:06,440
can say my
7553
04:43:03,440 --> 04:43:08,320
name or something like return name or
7554
04:43:06,440 --> 04:43:10,040
anything you want just make sure you
7555
04:43:08,320 --> 04:43:11,878
give it a meaningful name so it dictates
7556
04:43:10,040 --> 04:43:13,600
what the function is trying to do so
7557
04:43:11,878 --> 04:43:15,160
this ISS to return the name and the
7558
04:43:13,600 --> 04:43:18,200
reason why there's a red underline is it
7559
04:43:15,160 --> 04:43:21,160
says not all called path return a value
7560
04:43:18,200 --> 04:43:22,760
since we need to use our keyword return
7561
04:43:21,160 --> 04:43:24,400
now that we had some empty codes on the
7562
04:43:22,760 --> 04:43:26,280
return it's going to execute fine and
7563
04:43:24,400 --> 04:43:27,680
the compile error is gone so obviously
7564
04:43:26,280 --> 04:43:29,718
we don't want to just return nothing we
7565
04:43:27,680 --> 04:43:32,160
actually want to take this ab and return
7566
04:43:29,718 --> 04:43:33,520
in here so let's get rid of our Global
7567
04:43:32,160 --> 04:43:35,638
variable and let's see what we're going
7568
04:43:33,520 --> 04:43:37,878
to break so we're going to break these
7569
04:43:35,638 --> 04:43:39,798
two references to the name variable and
7570
04:43:37,878 --> 04:43:42,840
simply inside here we can actually just
7571
04:43:39,798 --> 04:43:44,798
call return name name and then here as
7572
04:43:42,840 --> 04:43:47,320
well we can call return name and what
7573
04:43:44,798 --> 04:43:49,440
this will do is inside here it will
7574
04:43:47,320 --> 04:43:52,000
execute this function and then return
7575
04:43:49,440 --> 04:43:54,400
the value in here if this is too many
7576
04:43:52,000 --> 04:43:56,840
steps for you you can have a string name
7577
04:43:54,400 --> 04:43:59,798
equals return name and then simply use
7578
04:43:56,840 --> 04:44:01,320
the name inside the brackets these two
7579
04:43:59,798 --> 04:44:03,280
lines are doing exactly the same thing
7580
04:44:01,320 --> 04:44:04,798
except this is using a variable cuz
7581
04:44:03,280 --> 04:44:06,480
we're storing it first and then we're
7582
04:44:04,798 --> 04:44:08,200
using it whereas if you place it
7583
04:44:06,480 --> 04:44:09,958
straight inside the brackets that's just
7584
04:44:08,200 --> 04:44:11,798
going to store it and use it immediately
7585
04:44:09,958 --> 04:44:13,080
and there's no additional variable but
7586
04:44:11,798 --> 04:44:15,080
don't worry too much if you're going to
7587
04:44:13,080 --> 04:44:16,480
do this notation because an extra
7588
04:44:15,080 --> 04:44:18,320
variable in this case doesn't really
7589
04:44:16,480 --> 04:44:20,600
matter because as soon as this function
7590
04:44:18,320 --> 04:44:22,718
is executed the variable used to run
7591
04:44:20,600 --> 04:44:24,400
this function gets deleted since it's
7592
04:44:22,718 --> 04:44:27,080
inside the scope of these two curly
7593
04:44:24,400 --> 04:44:28,920
braces so now if you run the code you'll
7594
04:44:27,080 --> 04:44:30,240
see that again we have abber and abber
7595
04:44:28,920 --> 04:44:32,400
but now what we've done is actually
7596
04:44:30,240 --> 04:44:34,360
control everything throughout here so if
7597
04:44:32,400 --> 04:44:36,040
your name were to change or maybe this
7598
04:44:34,360 --> 04:44:38,120
changes in a different circumstance
7599
04:44:36,040 --> 04:44:40,360
based on the user that's logged in then
7600
04:44:38,120 --> 04:44:42,840
you can simply have the same thing now
7601
04:44:40,360 --> 04:44:45,280
printed in two different locations in
7602
04:44:42,840 --> 04:44:47,600
the title and in the console output and
7603
04:44:45,280 --> 04:44:49,280
it's actually coming from one function
7604
04:44:47,600 --> 04:44:52,240
so we can have a return name and we can
7605
04:44:49,280 --> 04:44:55,878
make a static in now and can say return
7606
04:44:52,240 --> 04:44:57,600
H and then we can have simply return 23
7607
04:44:55,878 --> 04:44:59,638
and that's our name and our age being
7608
04:44:57,600 --> 04:45:01,400
returned I Chang this back to abber and
7609
04:44:59,638 --> 04:45:02,878
then instead of the print introduction
7610
04:45:01,400 --> 04:45:05,080
maybe we actually want to print a little
7611
04:45:02,878 --> 04:45:07,520
bit more than the name and maybe in the
7612
04:45:05,080 --> 04:45:10,280
title what we can do is have string
7613
04:45:07,520 --> 04:45:12,000
interpolation wrap the return name and
7614
04:45:10,280 --> 04:45:14,560
maybe put a high in there and then we
7615
04:45:12,000 --> 04:45:18,120
can say return age so if we on the code
7616
04:45:14,560 --> 04:45:20,360
now you can see we have abber hyphen 23
7617
04:45:18,120 --> 04:45:22,040
so maybe that's the title of our console
7618
04:45:20,360 --> 04:45:24,040
but then in the introduction we can use
7619
04:45:22,040 --> 04:45:26,280
a bit more than that so we can have int
7620
04:45:24,040 --> 04:45:28,360
age equals return age and then in the
7621
04:45:26,280 --> 04:45:32,280
console output we can have something
7622
04:45:28,360 --> 04:45:35,680
like this hello my name is name and my
7623
04:45:32,280 --> 04:45:37,760
age is age and then after we've compiled
7624
04:45:35,680 --> 04:45:40,360
that together we can place the output
7625
04:45:37,760 --> 04:45:42,638
inside here there we go so we're using
7626
04:45:40,360 --> 04:45:44,560
the name and the age inside the console
7627
04:45:42,638 --> 04:45:46,840
title and then we have this function
7628
04:45:44,560 --> 04:45:48,958
that runs to get us back the name and
7629
04:45:46,840 --> 04:45:50,958
the age and then we're using it using
7630
04:45:48,958 --> 04:45:53,320
string interpolation of course these
7631
04:45:50,958 --> 04:45:55,840
four lines can be all squished into one
7632
04:45:53,320 --> 04:45:57,400
you can simply take this dollar sign let
7633
04:45:55,840 --> 04:46:00,040
me duplicate this
7634
04:45:57,400 --> 04:46:01,840
line you can take this and paste it
7635
04:46:00,040 --> 04:46:04,000
straight into here and where you have
7636
04:46:01,840 --> 04:46:05,718
name we can have our return name and
7637
04:46:04,000 --> 04:46:08,080
where you have age you can have return
7638
04:46:05,718 --> 04:46:10,040
age and that will actually rule out all
7639
04:46:08,080 --> 04:46:11,600
of these lines and you can wrap it all
7640
04:46:10,040 --> 04:46:13,040
into one
7641
04:46:11,600 --> 04:46:15,718
there you
7642
04:46:13,040 --> 04:46:17,560
go and if age were to change like I said
7643
04:46:15,718 --> 04:46:19,680
before you can simply type this number
7644
04:46:17,560 --> 04:46:21,798
in change it and you can see in both
7645
04:46:19,680 --> 04:46:23,798
cases in the title and in the console
7646
04:46:21,798 --> 04:46:25,718
output it has officially changed
7647
04:46:23,798 --> 04:46:27,638
functions can be very helpful to reduce
7648
04:46:25,718 --> 04:46:30,080
repetition in code let's show you an
7649
04:46:27,638 --> 04:46:32,200
example if you had three sections where
7650
04:46:30,080 --> 04:46:35,878
you said enter a
7651
04:46:32,200 --> 04:46:38,680
number and you had int num one equals
7652
04:46:35,878 --> 04:46:41,480
convert to n32 and then we add a
7653
04:46:38,680 --> 04:46:43,080
console. readline
7654
04:46:41,480 --> 04:46:44,440
and then you had this section and you
7655
04:46:43,080 --> 04:46:46,798
wanted to do num
7656
04:46:44,440 --> 04:46:49,000
two and you had this section again and
7657
04:46:46,798 --> 04:46:50,558
you actually wanted to do num three now
7658
04:46:49,000 --> 04:46:52,320
at this point even though these three
7659
04:46:50,558 --> 04:46:54,280
are actually identical you want to be
7660
04:46:52,320 --> 04:46:58,120
using array in this case so you could
7661
04:46:54,280 --> 04:47:00,558
have int numbers equals new in square
7662
04:46:58,120 --> 04:47:01,600
braet 3 and then here we could say
7663
04:47:00,558 --> 04:47:03,558
numers
7664
04:47:01,600 --> 04:47:06,160
zero numbers
7665
04:47:03,558 --> 04:47:08,360
one numbers two and then we don't need
7666
04:47:06,160 --> 04:47:10,000
the ins cuz it's already been defined
7667
04:47:08,360 --> 04:47:11,718
and then we have them like this but
7668
04:47:10,000 --> 04:47:13,680
because again all these three sections
7669
04:47:11,718 --> 04:47:16,600
are exactly identical we can actually
7670
04:47:13,680 --> 04:47:18,638
run a for Loop that goes from numbers do
7671
04:47:16,600 --> 04:47:20,600
length and then we can place one of
7672
04:47:18,638 --> 04:47:22,718
these straight inside here and then
7673
04:47:20,600 --> 04:47:24,320
change this to I and now we've achieved
7674
04:47:22,718 --> 04:47:26,320
exactly the same thing but we're not
7675
04:47:24,320 --> 04:47:28,680
actually done yet so if you run the code
7676
04:47:26,320 --> 04:47:31,040
and we do one 2 3 now we've stored all
7677
04:47:28,680 --> 04:47:32,718
three numbers so we have enter a number
7678
04:47:31,040 --> 04:47:34,280
and then we have the console read line
7679
04:47:32,718 --> 04:47:36,440
and maybe we can actually make these two
7680
04:47:34,280 --> 04:47:38,600
lines a little bit shorter so let's make
7681
04:47:36,440 --> 04:47:40,680
a function static and because we
7682
04:47:38,600 --> 04:47:45,000
essentially want to return an integer we
7683
04:47:40,680 --> 04:47:47,080
can say static in read number from
7684
04:47:45,000 --> 04:47:49,760
console and then if we take these two
7685
04:47:47,080 --> 04:47:51,600
lines place them inside here and because
7686
04:47:49,760 --> 04:47:52,680
we need to return back an integer this
7687
04:47:51,600 --> 04:47:54,760
is the value that we're going to be
7688
04:47:52,680 --> 04:47:56,360
returning so instead of assigning this
7689
04:47:54,760 --> 04:47:58,958
position to something we'll actually
7690
04:47:56,360 --> 04:48:01,000
just immediately return at this point so
7691
04:47:58,958 --> 04:48:03,000
instead of doing this line now and
7692
04:48:01,000 --> 04:48:05,280
instead of doing all of this we can
7693
04:48:03,000 --> 04:48:07,440
simply make this into one line of code
7694
04:48:05,280 --> 04:48:09,718
read number from console so every time
7695
04:48:07,440 --> 04:48:12,360
this will execute will jump into here
7696
04:48:09,718 --> 04:48:14,680
print out the value enter a number the
7697
04:48:12,360 --> 04:48:16,718
user will typee it in it will read it in
7698
04:48:14,680 --> 04:48:18,080
convert it to an integer 32 and this
7699
04:48:16,718 --> 04:48:19,680
will return back the integer that the
7700
04:48:18,080 --> 04:48:20,958
user's typed in and then that will
7701
04:48:19,680 --> 04:48:23,120
return it back to the console and
7702
04:48:20,958 --> 04:48:24,600
essentially replace this so if the user
7703
04:48:23,120 --> 04:48:26,760
is typed in five then it essentially
7704
04:48:24,600 --> 04:48:28,680
replace this with five store it loop
7705
04:48:26,760 --> 04:48:30,480
around and have to placed in 10 the next
7706
04:48:28,680 --> 04:48:32,080
time and then you replace this with 10
7707
04:48:30,480 --> 04:48:34,320
essentially and then that gets stored
7708
04:48:32,080 --> 04:48:36,680
inside and so on and so forth until all
7709
04:48:34,320 --> 04:48:40,160
the positions have been complete let's
7710
04:48:36,680 --> 04:48:41,360
test that 1 2 3 and it still works okay
7711
04:48:40,160 --> 04:48:42,878
of course I'm not actually printing the
7712
04:48:41,360 --> 04:48:46,000
values out to the screen so if you
7713
04:48:42,878 --> 04:48:48,360
wanted to see that quickly for each can
7714
04:48:46,000 --> 04:48:50,360
have V item inside our numbers and then
7715
04:48:48,360 --> 04:48:53,440
do a console right with some string
7716
04:48:50,360 --> 04:48:55,840
interpolation item followed by a space
7717
04:48:53,440 --> 04:48:57,558
and let's run that one two and three and
7718
04:48:55,840 --> 04:48:58,920
there we go we get one two and three of
7719
04:48:57,558 --> 04:49:00,520
course you can make these functions and
7720
04:48:58,920 --> 04:49:02,400
they can return any bits of data that
7721
04:49:00,520 --> 04:49:05,160
you want you can even have something
7722
04:49:02,400 --> 04:49:06,878
like an INT array and we can say create
7723
04:49:05,160 --> 04:49:09,878
random array and then we can create an
7724
04:49:06,878 --> 04:49:12,480
array call numbers make that equal to
7725
04:49:09,878 --> 04:49:14,440
new int square bracket 3 and we can
7726
04:49:12,480 --> 04:49:16,798
actually give it the values of 0 1 and
7727
04:49:14,440 --> 04:49:18,440
two and then just at the end here we can
7728
04:49:16,798 --> 04:49:20,798
actually just return numbers and then
7729
04:49:18,440 --> 04:49:22,280
just in here we can have an INT array or
7730
04:49:20,798 --> 04:49:24,558
we can call that new numbers since we
7731
04:49:22,280 --> 04:49:26,320
have numbers above just above here and
7732
04:49:24,558 --> 04:49:28,840
then we can make that equal to create
7733
04:49:26,320 --> 04:49:31,240
random array and because this returns us
7734
04:49:28,840 --> 04:49:33,120
back with an integer array there's no
7735
04:49:31,240 --> 04:49:35,120
compile array inside here and we can
7736
04:49:33,120 --> 04:49:37,718
have a four each follows the exact same
7737
04:49:35,120 --> 04:49:40,798
approach as the one above new numbers
7738
04:49:37,718 --> 04:49:43,798
console right line into a console right
7739
04:49:40,798 --> 04:49:45,320
and then have item followed by a space
7740
04:49:43,798 --> 04:49:47,480
now if we print these both out you can
7741
04:49:45,320 --> 04:49:49,120
see that the zero one and two actually
7742
04:49:47,480 --> 04:49:51,000
comes from the new one it's just we have
7743
04:49:49,120 --> 04:49:52,600
two console rights right next to each
7744
04:49:51,000 --> 04:49:54,400
other so there's no new line separating
7745
04:49:52,600 --> 04:49:58,160
them so if we just add a console right
7746
04:49:54,400 --> 04:49:59,638
line here 1 2 and three and there you go
7747
04:49:58,160 --> 04:50:02,480
1 2 and three comes from the three
7748
04:49:59,638 --> 04:50:03,958
inputs we just typed in and 0 1 and two
7749
04:50:02,480 --> 04:50:06,440
actually come from the function that
7750
04:50:03,958 --> 04:50:08,000
we've just created of course if you
7751
04:50:06,440 --> 04:50:09,920
don't like this notation then what you
7752
04:50:08,000 --> 04:50:11,360
can do is you can make it a lot easier
7753
04:50:09,920 --> 04:50:13,080
you don't even need to make a new
7754
04:50:11,360 --> 04:50:15,400
variable you can actually just return
7755
04:50:13,080 --> 04:50:16,680
the integer just like this so if we
7756
04:50:15,400 --> 04:50:20,000
place
7757
04:50:16,680 --> 04:50:21,480
this straight inside here actually just
7758
04:50:20,000 --> 04:50:23,920
make it on one line just so it looks a
7759
04:50:21,480 --> 04:50:26,440
bit neater then there you go we have our
7760
04:50:23,920 --> 04:50:28,320
entire function reduced in one line the
7761
04:50:26,440 --> 04:50:30,440
goal of functions is to make them do as
7762
04:50:28,320 --> 04:50:32,680
little as possible if your function has
7763
04:50:30,440 --> 04:50:34,000
two different things it's doing or more
7764
04:50:32,680 --> 04:50:35,600
then you should break that out into
7765
04:50:34,000 --> 04:50:37,200
another function for example if you had
7766
04:50:35,600 --> 04:50:39,440
a function that did two distinct
7767
04:50:37,200 --> 04:50:41,360
operations one was a conversion and one
7768
04:50:39,440 --> 04:50:43,120
was a calculation then you want to split
7769
04:50:41,360 --> 04:50:44,840
out into two the first function should
7770
04:50:43,120 --> 04:50:46,600
be the conversion and the second
7771
04:50:44,840 --> 04:50:48,000
function should be the calculation and
7772
04:50:46,600 --> 04:50:50,200
the reason why they're separate is
7773
04:50:48,000 --> 04:50:52,120
because if you want to reuse them as a
7774
04:50:50,200 --> 04:50:54,480
pair then you won't be able to reuse
7775
04:50:52,120 --> 04:50:56,920
each of the pairs if you wanted to have
7776
04:50:54,480 --> 04:50:58,798
a calculation on its own then you can't
7777
04:50:56,920 --> 04:51:02,040
do that if both functions do the same
7778
04:50:58,798 --> 04:51:03,360
thing for example you could have static
7779
04:51:02,040 --> 04:51:05,480
and it just could be something let's
7780
04:51:03,360 --> 04:51:07,798
just call it void and it could just be
7781
04:51:05,480 --> 04:51:09,878
test for example and if you had the
7782
04:51:07,798 --> 04:51:11,958
conversion here and then if you had the
7783
04:51:09,878 --> 04:51:13,958
calculation here then you need to split
7784
04:51:11,958 --> 04:51:16,160
this up into two different functions you
7785
04:51:13,958 --> 04:51:19,520
want have static valed
7786
04:51:16,160 --> 04:51:21,080
conversion and static vied calculation
7787
04:51:19,520 --> 04:51:23,080
if you wanted to use conversion or
7788
04:51:21,080 --> 04:51:24,920
calculation separately you can't because
7789
04:51:23,080 --> 04:51:26,558
now it's in this pair together so if you
7790
04:51:24,920 --> 04:51:28,400
wanted to use them separately you simply
7791
04:51:26,558 --> 04:51:30,440
can't it's best to keep functions on the
7792
04:51:28,400 --> 04:51:32,280
RW anywhere just so they have a single
7793
04:51:30,440 --> 04:51:34,080
purpose so you can give them a name cuz
7794
04:51:32,280 --> 04:51:35,760
as you can see here we can't give this
7795
04:51:34,080 --> 04:51:37,520
an appropriate name because it's doing
7796
04:51:35,760 --> 04:51:39,638
more than one task this is doing the
7797
04:51:37,520 --> 04:51:41,440
conversion this is doing the calculation
7798
04:51:39,638 --> 04:51:43,480
and this is doing mult multiple things
7799
04:51:41,440 --> 04:51:44,798
this could be doing something if you
7800
04:51:43,480 --> 04:51:46,558
have to think too hard about the
7801
04:51:44,798 --> 04:51:48,638
function title then you might be doing
7802
04:51:46,558 --> 04:51:50,400
too much within each of functions these
7803
04:51:48,638 --> 04:51:52,320
are very easy to be able to decide what
7804
04:51:50,400 --> 04:51:54,638
they do cuz this one converts and this
7805
04:51:52,320 --> 04:51:56,760
one's going to calculate reading number
7806
04:51:54,638 --> 04:51:58,200
from console if this was reading the
7807
04:51:56,760 --> 04:52:00,040
number from the console and doing
7808
04:51:58,200 --> 04:52:01,558
something else then it's going to be a
7809
04:52:00,040 --> 04:52:03,360
bit of a problem because now you've got
7810
04:52:01,558 --> 04:52:05,840
a bigger name you don't want to be doing
7811
04:52:03,360 --> 04:52:07,680
reading number from console and and
7812
04:52:05,840 --> 04:52:09,360
something else cuz then your function
7813
04:52:07,680 --> 04:52:11,200
name gets also too big and you got
7814
04:52:09,360 --> 04:52:13,320
multiple dependencies so the best thing
7815
04:52:11,200 --> 04:52:15,000
to do is to call them accordingly and
7816
04:52:13,320 --> 04:52:16,320
keep them as short as possible so let's
7817
04:52:15,000 --> 04:52:18,080
say we wanted to make a function that
7818
04:52:16,320 --> 04:52:21,040
added two values we can have a static
7819
04:52:18,080 --> 04:52:23,798
int call it add and then we can return 5
7820
04:52:21,040 --> 04:52:25,920
+ 5 now this function is a little bit
7821
04:52:23,798 --> 04:52:27,840
redundant although it will actually work
7822
04:52:25,920 --> 04:52:29,600
so if we go back to our main and we do a
7823
04:52:27,840 --> 04:52:31,440
console right line and actually print
7824
04:52:29,600 --> 04:52:33,840
the value of
7825
04:52:31,440 --> 04:52:35,798
ADD and we go through our function then
7826
04:52:33,840 --> 04:52:37,760
we see we get the value of 10 which is
7827
04:52:35,798 --> 04:52:40,520
indeed 5 +
7828
04:52:37,760 --> 04:52:43,000
5 but since it's 5 + 5 won't change
7829
04:52:40,520 --> 04:52:44,440
range it's always going to be 5 + 5 this
7830
04:52:43,000 --> 04:52:45,920
is where parameters will come in which
7831
04:52:44,440 --> 04:52:47,480
we'll cover in the next videos
7832
04:52:45,920 --> 04:52:49,718
parameters are a way of being able to
7833
04:52:47,480 --> 04:52:51,840
give the function value so these two can
7834
04:52:49,718 --> 04:52:53,680
be actually changed there's no point in
7835
04:52:51,840 --> 04:52:56,320
having an add function if the only
7836
04:52:53,680 --> 04:52:58,400
ability you can do is add 5 + 5 cuz you
7837
04:52:56,320 --> 04:52:59,958
may as well just return 10 at this point
7838
04:52:58,400 --> 04:53:01,480
so I covered a lot in this video and
7839
04:52:59,958 --> 04:53:03,200
there's lots of different functions that
7840
04:53:01,480 --> 04:53:05,440
do different things of course there is
7841
04:53:03,200 --> 04:53:06,760
quite a lot of lines we're up to 87 this
7842
04:53:05,440 --> 04:53:08,718
is probably the biggest project that
7843
04:53:06,760 --> 04:53:10,638
we've wrote so far and even though this
7844
04:53:08,718 --> 04:53:12,718
is not really a project it's just a buns
7845
04:53:10,638 --> 04:53:14,718
functions that do little things that are
7846
04:53:12,718 --> 04:53:16,760
made for demonstrations so let's have a
7847
04:53:14,718 --> 04:53:19,558
recap so first here we're setting the
7848
04:53:16,760 --> 04:53:22,200
title as a result of return name and
7849
04:53:19,558 --> 04:53:24,440
return age we can actually take this and
7850
04:53:22,200 --> 04:53:27,680
develop this further if you want to we
7851
04:53:24,440 --> 04:53:28,840
could actually say return name age pair
7852
04:53:27,680 --> 04:53:30,878
and if you wanted to collapse that
7853
04:53:28,840 --> 04:53:34,120
inside one function then that could be
7854
04:53:30,878 --> 04:53:37,080
really easy you could say static string
7855
04:53:34,120 --> 04:53:39,440
return name age pair and then you can
7856
04:53:37,080 --> 04:53:41,718
have return and literally the exact same
7857
04:53:39,440 --> 04:53:43,760
line above you can have return name
7858
04:53:41,718 --> 04:53:46,040
followed by a hyphen and then you can
7859
04:53:43,760 --> 04:53:48,120
have a return age and then end the line
7860
04:53:46,040 --> 04:53:49,600
like that so that just concatenates the
7861
04:53:48,120 --> 04:53:51,680
two values that are returned from these
7862
04:53:49,600 --> 04:53:53,798
two functions but the point is you keep
7863
04:53:51,680 --> 04:53:57,000
them all separate if you were to type in
7864
04:53:53,798 --> 04:53:58,680
very statically inside here AB 25 then
7865
04:53:57,000 --> 04:54:00,040
obviously like I've talked about above
7866
04:53:58,680 --> 04:54:01,798
then you're going to have duplication of
7867
04:54:00,040 --> 04:54:04,320
data and of course we're going to use
7868
04:54:01,798 --> 04:54:06,600
this name inside something like here and
7869
04:54:04,320 --> 04:54:07,958
as you can see this name age pair is
7870
04:54:06,600 --> 04:54:09,878
actually not applicable when it comes
7871
04:54:07,958 --> 04:54:12,000
back to this format so it's always best
7872
04:54:09,878 --> 04:54:13,320
to keep the these two separately so if
7873
04:54:12,000 --> 04:54:15,600
you bring it back and we can actually
7874
04:54:13,320 --> 04:54:18,200
keep it like this now instead of having
7875
04:54:15,600 --> 04:54:20,320
this code we can actually say return
7876
04:54:18,200 --> 04:54:22,120
name age pair that will give us the same
7877
04:54:20,320 --> 04:54:23,718
value at the top of the code it will
7878
04:54:22,120 --> 04:54:26,320
have ABA hyphen
7879
04:54:23,718 --> 04:54:28,760
25 so that's that bit in our print
7880
04:54:26,320 --> 04:54:30,600
introduction If We Hold control and we
7881
04:54:28,760 --> 04:54:32,480
actually press on this function it will
7882
04:54:30,600 --> 04:54:34,600
actually jump us right down and then if
7883
04:54:32,480 --> 04:54:36,680
we press the back arrow top left we can
7884
04:54:34,600 --> 04:54:38,680
go back to it there we go and then click
7885
04:54:36,680 --> 04:54:40,520
forward so in this function we can break
7886
04:54:38,680 --> 04:54:42,280
down all of the return values into to
7887
04:54:40,520 --> 04:54:44,480
string variables we have a string name
7888
04:54:42,280 --> 04:54:46,280
equals return name and inage equals
7889
04:54:44,480 --> 04:54:48,360
return age then we have a string input
7890
04:54:46,280 --> 04:54:51,080
that concatenates them both together
7891
04:54:48,360 --> 04:54:52,840
hello my name is name and my age is age
7892
04:54:51,080 --> 04:54:54,520
and then we print out to the screen or
7893
04:54:52,840 --> 04:54:57,240
if you wanted to we could wrap all four
7894
04:54:54,520 --> 04:54:58,558
of these lines straight up into one we
7895
04:54:57,240 --> 04:55:00,280
don't need to assign these into
7896
04:54:58,558 --> 04:55:02,200
variables although it does help the
7897
04:55:00,280 --> 04:55:03,958
readability if you keep them separate
7898
04:55:02,200 --> 04:55:06,240
but I prefer to keep it like this since
7899
04:55:03,958 --> 04:55:07,840
I'm used to this notation we can place
7900
04:55:06,240 --> 04:55:09,718
return name straight into there which
7901
04:55:07,840 --> 04:55:11,360
will get rid of our name variable and
7902
04:55:09,718 --> 04:55:13,320
the same thing with age which will get
7903
04:55:11,360 --> 04:55:15,400
rid of these two variables and of course
7904
04:55:13,320 --> 04:55:17,320
we're actually storing the string value
7905
04:55:15,400 --> 04:55:19,320
before we printing it so we could just
7906
04:55:17,320 --> 04:55:23,440
take all of this and place it straight
7907
04:55:19,320 --> 04:55:25,440
instead of here scrolling back to the
7908
04:55:23,440 --> 04:55:27,520
top this little section was
7909
04:55:25,440 --> 04:55:29,558
demonstrating how we can use integer
7910
04:55:27,520 --> 04:55:31,680
arrays to read in all the values from
7911
04:55:29,558 --> 04:55:33,520
the console but wrap it around in a
7912
04:55:31,680 --> 04:55:35,240
function so before in here you would
7913
04:55:33,520 --> 04:55:36,718
have something like a console right line
7914
04:55:35,240 --> 04:55:38,160
You' print out the value to the screen
7915
04:55:36,718 --> 04:55:39,958
that would say something like a ter
7916
04:55:38,160 --> 04:55:43,360
number and then you have on the next
7917
04:55:39,958 --> 04:55:45,240
line a convert to in32 and then after
7918
04:55:43,360 --> 04:55:47,360
you converted it in 32 you'll actually
7919
04:55:45,240 --> 04:55:49,120
start straight to numbers but because
7920
04:55:47,360 --> 04:55:50,440
this is going to be the same thing for
7921
04:55:49,120 --> 04:55:52,280
inserting all the numbers you can
7922
04:55:50,440 --> 04:55:54,040
actually wrap this around in a function
7923
04:55:52,280 --> 04:55:55,878
so if we hold control again and go to
7924
04:55:54,040 --> 04:55:58,280
this you'll see that them two lines are
7925
04:55:55,878 --> 04:56:00,958
very simply placed in here console right
7926
04:55:58,280 --> 04:56:03,280
Line enter a new number and then convert
7927
04:56:00,958 --> 04:56:05,320
in 32 based on the console read line and
7928
04:56:03,280 --> 04:56:07,160
then we simply just returning that back
7929
04:56:05,320 --> 04:56:09,120
ready to be stored in the index of the
7930
04:56:07,160 --> 04:56:10,360
numbers array and then straight after
7931
04:56:09,120 --> 04:56:12,200
that we're just printing out all of
7932
04:56:10,360 --> 04:56:14,160
values to make sure it's worked and that
7933
04:56:12,200 --> 04:56:15,280
all succeeded fine here I was just
7934
04:56:14,160 --> 04:56:16,638
demonstrating that you can have
7935
04:56:15,280 --> 04:56:18,600
different return values and it can
7936
04:56:16,638 --> 04:56:20,320
literally be anything so as you can see
7937
04:56:18,600 --> 04:56:23,040
here if you hover over create random
7938
04:56:20,320 --> 04:56:24,920
array it's inside the program class it's
7939
04:56:23,040 --> 04:56:26,878
called create random array and it
7940
04:56:24,920 --> 04:56:28,958
returns back an integer array so that's
7941
04:56:26,878 --> 04:56:30,878
what we're signed into and if we hold
7942
04:56:28,958 --> 04:56:33,000
control and click on this again you can
7943
04:56:30,878 --> 04:56:36,120
see that all this simply does is returns
7944
04:56:33,000 --> 04:56:38,400
us back a three length array with 0 1
7945
04:56:36,120 --> 04:56:40,638
and two as the array positions and then
7946
04:56:38,400 --> 04:56:42,000
if we scroll back up when we execute
7947
04:56:40,638 --> 04:56:45,558
this for each Loop then we're simply
7948
04:56:42,000 --> 04:56:47,440
going to get 0 1 and two just like that
7949
04:56:45,558 --> 04:56:51,480
since it's a console right and it'll be
7950
04:56:47,440 --> 04:56:54,040
on the same line with a space after each
7951
04:56:51,480 --> 04:56:56,320
other this demonstration was slightly
7952
04:56:54,040 --> 04:56:58,440
theoretical I was saying in one given
7953
04:56:56,320 --> 04:57:00,920
situation a function should only do one
7954
04:56:58,440 --> 04:57:02,798
task if your function converts and does
7955
04:57:00,920 --> 04:57:04,558
a calculation at the same time then
7956
04:57:02,798 --> 04:57:06,280
these are actually two different things
7957
04:57:04,558 --> 04:57:08,280
you should split them so you use the
7958
04:57:06,280 --> 04:57:10,400
conversion in this step and you use the
7959
04:57:08,280 --> 04:57:12,240
calculation in this step if you want to
7960
04:57:10,400 --> 04:57:14,440
convert something or calculate something
7961
04:57:12,240 --> 04:57:16,958
in the future then you can only do it
7962
04:57:14,440 --> 04:57:19,080
now because we've separated them whereas
7963
04:57:16,958 --> 04:57:21,280
before if you have them both inside here
7964
04:57:19,080 --> 04:57:23,000
then you simply can't do one operation
7965
04:57:21,280 --> 04:57:25,080
because you've wrapped it around in one
7966
04:57:23,000 --> 04:57:27,480
section the same is true when I was
7967
04:57:25,080 --> 04:57:29,320
discussing this earlier on if we kept
7968
04:57:27,480 --> 04:57:31,040
this name pair as the only way to access
7969
04:57:29,320 --> 04:57:32,760
the age in the name then it's going to
7970
04:57:31,040 --> 04:57:34,040
mess up when we use it in here and then
7971
04:57:32,760 --> 04:57:36,440
we're going to have a duplication of
7972
04:57:34,040 --> 04:57:38,520
data but because we separated the name
7973
04:57:36,440 --> 04:57:40,718
and we separated the age we can use it
7974
04:57:38,520 --> 04:57:42,320
in this function and we can in this
7975
04:57:40,718 --> 04:57:44,680
function and it doesn't interrupt
7976
04:57:42,320 --> 04:57:46,680
anything this simply Returns the name
7977
04:57:44,680 --> 04:57:49,040
this simply Returns the age and these
7978
04:57:46,680 --> 04:57:50,718
two functions make use of them if we had
7979
04:57:49,040 --> 04:57:52,240
something else underneath there were
7980
04:57:50,718 --> 04:57:54,080
another function that depended on the
7981
04:57:52,240 --> 04:57:56,600
name of the edge then again we can
7982
04:57:54,080 --> 04:57:58,360
simply use them if it was restricted to
7983
04:57:56,600 --> 04:58:00,718
this function then it's going to be a
7984
04:57:58,360 --> 04:58:01,600
problem using it in this function or any
7985
04:58:00,718 --> 04:58:04,480
future
7986
04:58:01,600 --> 04:58:06,480
ones and finally just to finish off add
7987
04:58:04,480 --> 04:58:08,920
function and all it simply does is
7988
04:58:06,480 --> 04:58:10,160
return 5 + 5 and I was saying as of
7989
04:58:08,920 --> 04:58:11,600
right now even though return return
7990
04:58:10,160 --> 04:58:13,600
returns back a value it's kind of
7991
04:58:11,600 --> 04:58:15,638
pointless CU you may as well just return
7992
04:58:13,600 --> 04:58:17,320
back 10 since it's doing the same thing
7993
04:58:15,638 --> 04:58:19,160
the reason I included this function is
7994
04:58:17,320 --> 04:58:21,480
that we can set it up and start planting
7995
04:58:19,160 --> 04:58:23,760
the seeds to understand why functions
7996
04:58:21,480 --> 04:58:25,558
exist and how to make use of parameters
7997
04:58:23,760 --> 04:58:27,440
what a parameter is is it lets you put
7998
04:58:25,558 --> 04:58:29,638
things Within These brackets so you can
7999
04:58:27,440 --> 04:58:32,160
give this function some data so if you
8000
04:58:29,638 --> 04:58:33,760
have an in. trass for example these
8001
04:58:32,160 --> 04:58:35,798
things that we place inside the brackets
8002
04:58:33,760 --> 04:58:38,958
are actually the parameters so we have a
8003
04:58:35,798 --> 04:58:40,878
string s and out in something and that's
8004
04:58:38,958 --> 04:58:43,200
how the parameters work on our trip pass
8005
04:58:40,878 --> 04:58:45,840
function and just to show you this is
8006
04:58:43,200 --> 04:58:48,400
how it's actually wrote It's a Bool and
8007
04:58:45,840 --> 04:58:48,400
then it's called
8008
04:58:53,480 --> 04:58:57,440
Triassic and I know it's give us an
8009
04:58:55,798 --> 04:58:59,200
error because we don't have the control
8010
04:58:57,440 --> 04:59:01,638
path if we just use result and assign it
8011
04:58:59,200 --> 04:59:03,040
to zero and then return back true you
8012
04:59:01,638 --> 04:59:04,878
can see that the compil error is now
8013
04:59:03,040 --> 04:59:07,760
gone and as you can see here it says
8014
04:59:04,878 --> 04:59:09,558
bull trip pass string s out in result
8015
04:59:07,760 --> 04:59:11,760
and if we actually try and use the trip
8016
04:59:09,558 --> 04:59:13,360
pass in here and press the bracket you
8017
04:59:11,760 --> 04:59:15,600
can see that these parameters are
8018
04:59:13,360 --> 04:59:17,760
exactly identical to parameters that
8019
04:59:15,600 --> 04:59:20,040
were inside of the tripar coming from
8020
04:59:17,760 --> 04:59:21,520
the integer Library so in the future
8021
04:59:20,040 --> 04:59:23,920
we're going to study how we can use
8022
04:59:21,520 --> 04:59:26,400
parameters to pass data inside our
8023
04:59:23,920 --> 04:59:28,240
function but just for now know that this
8024
04:59:26,400 --> 04:59:29,680
function is essentially pointless
8025
04:59:28,240 --> 04:59:31,638
because it's only going to return back
8026
04:59:29,680 --> 04:59:33,480
10 and this function will gain the
8027
04:59:31,638 --> 04:59:35,120
functionality it needs when we start
8028
04:59:33,480 --> 04:59:38,360
adding parameters this should
8029
04:59:35,120 --> 04:59:40,240
essentially be add five + 5 except this
8030
04:59:38,360 --> 04:59:41,760
is kind of a pointless function because
8031
04:59:40,240 --> 04:59:44,440
we already know the value is going to be
8032
04:59:41,760 --> 04:59:45,760
10 but again this is just helping you
8033
04:59:44,440 --> 04:59:48,440
understand why parameters are very
8034
04:59:45,760 --> 04:59:48,440
essential to
8035
04:59:48,558 --> 04:59:52,638
functions we've covered how to create a
8036
04:59:50,638 --> 04:59:54,600
VI function and a function that returns
8037
04:59:52,638 --> 04:59:56,558
a data type but now we want to create a
8038
04:59:54,600 --> 04:59:58,080
function that gives it some sort of data
8039
04:59:56,558 --> 04:59:59,638
you may not realize it but all these
8040
04:59:58,080 --> 05:00:01,200
functions that you normally use like
8041
04:59:59,638 --> 05:00:03,480
console right line you're actually
8042
05:00:01,200 --> 05:00:05,200
inserting data consistently into all of
8043
05:00:03,480 --> 05:00:07,480
these functions if you hover over
8044
05:00:05,200 --> 05:00:09,440
console right line you see his void
8045
05:00:07,480 --> 05:00:11,160
console right line and right now there's
8046
05:00:09,440 --> 05:00:12,718
Open Bracket and there's a Clos bracket
8047
05:00:11,160 --> 05:00:14,280
if we delete this bracket and reopen it
8048
05:00:12,718 --> 05:00:15,798
you see there's 19 different ways that
8049
05:00:14,280 --> 05:00:17,440
you can call this function and as soon
8050
05:00:15,798 --> 05:00:19,000
as you start to type a string you can
8051
05:00:17,440 --> 05:00:20,680
see we have a string format and an
8052
05:00:19,000 --> 05:00:22,120
object if you need to and if you type in
8053
05:00:20,680 --> 05:00:24,080
another string you can see it says
8054
05:00:22,120 --> 05:00:26,120
string value this string value is
8055
05:00:24,080 --> 05:00:28,040
actually a parameter If We Hold control
8056
05:00:26,120 --> 05:00:30,440
and press on right line you can see all
8057
05:00:28,040 --> 05:00:32,160
of the different functions right here
8058
05:00:30,440 --> 05:00:33,760
this is the most basic version that just
8059
05:00:32,160 --> 05:00:35,440
inserts a right line character to the
8060
05:00:33,760 --> 05:00:36,798
console and then these are all the
8061
05:00:35,440 --> 05:00:39,000
different parameters that you can give
8062
05:00:36,798 --> 05:00:40,680
the value of course you can't double
8063
05:00:39,000 --> 05:00:43,000
click and view the source code because
8064
05:00:40,680 --> 05:00:45,120
this is hidden inside Microsoft's
8065
05:00:43,000 --> 05:00:46,958
libraries the values that are placed
8066
05:00:45,120 --> 05:00:49,520
inside these parentheses are actually
8067
05:00:46,958 --> 05:00:51,360
called the parameters this is known as
8068
05:00:49,520 --> 05:00:53,200
functional overloading which we'll cover
8069
05:00:51,360 --> 05:00:54,600
later on in the course basically that
8070
05:00:53,200 --> 05:00:56,120
means you can have the same name
8071
05:00:54,600 --> 05:00:57,760
function but give it different
8072
05:00:56,120 --> 05:00:59,558
parameters and you'll see why this is
8073
05:00:57,760 --> 05:01:01,000
helpful later on down the line so let's
8074
05:00:59,558 --> 05:01:03,080
close this tab in the top right hand
8075
05:01:01,000 --> 05:01:04,680
corner and let's bring back out code so
8076
05:01:03,080 --> 05:01:06,920
now that we've passed hello into this
8077
05:01:04,680 --> 05:01:09,040
function that was actually given this a
8078
05:01:06,920 --> 05:01:10,440
string value parameter and that hello is
8079
05:01:09,040 --> 05:01:12,240
going to get past straight into the
8080
05:01:10,440 --> 05:01:13,680
console right line so then when we run
8081
05:01:12,240 --> 05:01:15,958
the code it's going to print into the
8082
05:01:13,680 --> 05:01:17,718
console so let's create our own function
8083
05:01:15,958 --> 05:01:19,160
with our own parameters so if you take
8084
05:01:17,718 --> 05:01:21,440
the function I had last time it was
8085
05:01:19,160 --> 05:01:24,480
static int add Open Bracket close
8086
05:01:21,440 --> 05:01:26,160
bracket and it simply returned 5 + 5
8087
05:01:24,480 --> 05:01:28,280
which we all know is going to be 10 so
8088
05:01:26,160 --> 05:01:30,040
if we place add inside these brackets
8089
05:01:28,280 --> 05:01:32,840
and we run the code we should get 10
8090
05:01:30,040 --> 05:01:34,160
return back there we go so of course
8091
05:01:32,840 --> 05:01:36,760
this is a little bit pointless since
8092
05:01:34,160 --> 05:01:38,240
it's always going to be adding 5 + 5
8093
05:01:36,760 --> 05:01:40,400
there is no way we can change this
8094
05:01:38,240 --> 05:01:42,400
without the use of parameters so in here
8095
05:01:40,400 --> 05:01:44,480
we can actually just do what we do when
8096
05:01:42,400 --> 05:01:46,520
we normally declare a variable we say
8097
05:01:44,480 --> 05:01:48,040
the data type int and then the variable
8098
05:01:46,520 --> 05:01:49,920
name and then if you want to add
8099
05:01:48,040 --> 05:01:53,120
multiple parameters you can simply add a
8100
05:01:49,920 --> 05:01:54,600
comma so now we can say int a and int B
8101
05:01:53,120 --> 05:01:56,718
and you can see now we've got a syntax
8102
05:01:54,600 --> 05:01:58,600
error because it expects us to have both
8103
05:01:56,718 --> 05:02:00,280
of these values inside if you hover over
8104
05:01:58,600 --> 05:02:01,840
it there is no argument given that
8105
05:02:00,280 --> 05:02:04,798
corresponds to the required formal
8106
05:02:01,840 --> 05:02:06,958
parameter a and then if we insert a by
8107
05:02:04,798 --> 05:02:09,240
having five for example and if we hover
8108
05:02:06,958 --> 05:02:11,200
over it it complains about not having B
8109
05:02:09,240 --> 05:02:13,280
so we can do comma and then do five
8110
05:02:11,200 --> 05:02:14,680
again so just this on its own is not
8111
05:02:13,280 --> 05:02:16,120
actually going to do anything we're
8112
05:02:14,680 --> 05:02:18,480
actually giving the add function the
8113
05:02:16,120 --> 05:02:21,240
values of five and five and place them
8114
05:02:18,480 --> 05:02:23,480
inside a and inside B variables these
8115
05:02:21,240 --> 05:02:25,400
are now known as local variables and
8116
05:02:23,480 --> 05:02:27,520
they only exist in memory between these
8117
05:02:25,400 --> 05:02:29,440
two curly braces as soon as the function
8118
05:02:27,520 --> 05:02:31,440
is done these are deleted from memory
8119
05:02:29,440 --> 05:02:33,480
and you can no longer access in a and in
8120
05:02:31,440 --> 05:02:35,760
B you can't just do something like a
8121
05:02:33,480 --> 05:02:38,040
inside here and think that this can be
8122
05:02:35,760 --> 05:02:39,920
used up here these are in two completely
8123
05:02:38,040 --> 05:02:41,840
different Scopes this is one for
8124
05:02:39,920 --> 05:02:43,120
function and this is another function
8125
05:02:41,840 --> 05:02:44,920
and they're completely separate from
8126
05:02:43,120 --> 05:02:47,558
each other when you type in five and
8127
05:02:44,920 --> 05:02:49,920
five it goes in order so int a is the
8128
05:02:47,558 --> 05:02:52,240
first one so five gets placed into here
8129
05:02:49,920 --> 05:02:53,920
and this five gets placed into B so now
8130
05:02:52,240 --> 05:02:56,440
that we need to make use of them we can
8131
05:02:53,920 --> 05:02:58,280
simply change this to a and change this
8132
05:02:56,440 --> 05:03:00,558
one to B now if you run the code we
8133
05:02:58,280 --> 05:03:02,440
should still get 10 but now our function
8134
05:03:00,558 --> 05:03:04,360
Works a bit better we can give it a set
8135
05:03:02,440 --> 05:03:06,360
of data so if you actually just put
8136
05:03:04,360 --> 05:03:09,040
zeros instead of here then we should get
8137
05:03:06,360 --> 05:03:10,520
a 100 returned perfect so now that we
8138
05:03:09,040 --> 05:03:11,798
figured out how to to do this let's have
8139
05:03:10,520 --> 05:03:13,600
a look at different ways that we can
8140
05:03:11,798 --> 05:03:15,600
make use of this function so let's say
8141
05:03:13,600 --> 05:03:18,120
we had a situation where we did console
8142
05:03:15,600 --> 05:03:23,200
right enter a
8143
05:03:18,120 --> 05:03:25,558
number and we add intn equals convert to
8144
05:03:23,200 --> 05:03:27,798
in32 and then you're converting the
8145
05:03:25,558 --> 05:03:30,160
console do
8146
05:03:27,798 --> 05:03:32,480
readline and let's wrap this inside a
8147
05:03:30,160 --> 05:03:34,638
function so before what I was doing is
8148
05:03:32,480 --> 05:03:37,160
simply just returning back in int it was
8149
05:03:34,638 --> 05:03:40,080
static int read
8150
05:03:37,160 --> 05:03:42,080
int and then we took these two lines and
8151
05:03:40,080 --> 05:03:43,920
place it straight inside here and remove
8152
05:03:42,080 --> 05:03:45,840
the fact that we have an integer and
8153
05:03:43,920 --> 05:03:47,958
simply just change this to a return and
8154
05:03:45,840 --> 05:03:49,798
what this now will say is let's output
8155
05:03:47,958 --> 05:03:52,120
enter a number to the console let the
8156
05:03:49,798 --> 05:03:54,120
user type it in inside the console read
8157
05:03:52,120 --> 05:03:55,558
line and then read it in convert it to
8158
05:03:54,120 --> 05:03:57,400
an integer and then give it back to
8159
05:03:55,558 --> 05:03:59,920
whatever calls it so now we can get rid
8160
05:03:57,400 --> 05:04:02,958
of this and get rid of this convert and
8161
05:03:59,920 --> 05:04:04,480
simply just type in read in inside here
8162
05:04:02,958 --> 05:04:06,440
and after that line let's just print it
8163
05:04:04,480 --> 05:04:09,200
out to make sure it's worked okay enter
8164
05:04:06,440 --> 05:04:11,520
a number 10 press enter and we have 10
8165
05:04:09,200 --> 05:04:13,480
again and that's fine except if we want
8166
05:04:11,520 --> 05:04:15,680
to use this in another case for example
8167
05:04:13,480 --> 05:04:17,558
if you have num one maybe we don't
8168
05:04:15,680 --> 05:04:19,000
actually want to enter a number maybe
8169
05:04:17,558 --> 05:04:21,558
this time it's going to be enter an
8170
05:04:19,000 --> 05:04:23,718
angle or something of this sort so what
8171
05:04:21,558 --> 05:04:25,878
we can do if we undo that we can
8172
05:04:23,718 --> 05:04:27,400
actually introduce a string message and
8173
05:04:25,878 --> 05:04:29,680
what this will do now is actually
8174
05:04:27,400 --> 05:04:31,120
specify the message that we put in here
8175
05:04:29,680 --> 05:04:33,160
maybe there's a situation where we
8176
05:04:31,120 --> 05:04:34,920
always want to enter something so maybe
8177
05:04:33,160 --> 05:04:36,718
we can just replace this and put a
8178
05:04:34,920 --> 05:04:38,600
string inter polation and wrap this
8179
05:04:36,718 --> 05:04:40,958
number around in the curly brace and
8180
05:04:38,600 --> 05:04:44,160
replace it to message so then in here we
8181
05:04:40,958 --> 05:04:46,160
can say enter a and then we can pass the
8182
05:04:44,160 --> 05:04:48,160
string variable which will get placed in
8183
05:04:46,160 --> 05:04:49,840
here so if this is a number then we can
8184
05:04:48,160 --> 05:04:51,440
type a number and maybe this is
8185
05:04:49,840 --> 05:04:54,400
something like an
8186
05:04:51,440 --> 05:04:56,638
angle and then here we can type an angle
8187
05:04:54,400 --> 05:04:58,520
so we have 10 enter a number and then
8188
05:04:56,638 --> 05:05:00,200
there you go it says enter an angle then
8189
05:04:58,520 --> 05:05:02,120
we can have 100 and that's working as
8190
05:05:00,200 --> 05:05:04,240
well alternatively if you want to
8191
05:05:02,120 --> 05:05:07,040
specify the entire message then you can
8192
05:05:04,240 --> 05:05:08,680
remove these and keep it like this and
8193
05:05:07,040 --> 05:05:11,558
then in both of these situations we can
8194
05:05:08,680 --> 05:05:13,240
say enter and number and copy that down
8195
05:05:11,558 --> 05:05:15,520
like this and make sure you put the call
8196
05:05:13,240 --> 05:05:18,480
on at the end followed by the space
8197
05:05:15,520 --> 05:05:21,840
there we go enter a number 10 and an
8198
05:05:18,480 --> 05:05:23,600
angle 20 maybe the situation where this
8199
05:05:21,840 --> 05:05:25,878
text actually changed and you just want
8200
05:05:23,600 --> 05:05:26,840
it to be angle and if you run that again
8201
05:05:25,878 --> 05:05:28,600
then you'll see that these are
8202
05:05:26,840 --> 05:05:30,878
completely different now one says enter
8203
05:05:28,600 --> 05:05:32,440
a number and one says angle but this
8204
05:05:30,878 --> 05:05:35,200
gives us a lot of flexibility in what we
8205
05:05:32,440 --> 05:05:36,878
pass in if we keep it as enter a and
8206
05:05:35,200 --> 05:05:38,558
then let the user decide on what this
8207
05:05:36,878 --> 05:05:40,760
section is then you rule out any
8208
05:05:38,558 --> 05:05:42,600
opportunity to be able to do that option
8209
05:05:40,760 --> 05:05:44,440
if you wanted to to prevent the colon in
8210
05:05:42,600 --> 05:05:46,240
the space always being at the end you
8211
05:05:44,440 --> 05:05:47,320
can actually just add that in manually
8212
05:05:46,240 --> 05:05:49,520
if you bring back the string
8213
05:05:47,320 --> 05:05:51,320
interpolation we can say colon followed
8214
05:05:49,520 --> 05:05:53,520
by space so you don't have to remember
8215
05:05:51,320 --> 05:05:54,760
to always put that in there we go so
8216
05:05:53,520 --> 05:05:56,200
that's doing the exact same thing and
8217
05:05:54,760 --> 05:05:58,320
we're actually just reducing the amount
8218
05:05:56,200 --> 05:06:00,240
of repetition because we have to
8219
05:05:58,320 --> 05:06:02,200
remember to put a call on Follow by
8220
05:06:00,240 --> 05:06:04,040
space inside here we might just forget
8221
05:06:02,200 --> 05:06:05,480
it and then when you leave it like this
8222
05:06:04,040 --> 05:06:07,240
then the cord's going to look ugly
8223
05:06:05,480 --> 05:06:09,400
because it looks like this and it's all
8224
05:06:07,240 --> 05:06:10,878
stuck together so let's make use of
8225
05:06:09,400 --> 05:06:13,040
these two functions and let's see if we
8226
05:06:10,878 --> 05:06:16,638
can write a really basic calculator we
8227
05:06:13,040 --> 05:06:19,520
can say int first num equals read int
8228
05:06:16,638 --> 05:06:21,760
and we can say enter first number and
8229
05:06:19,520 --> 05:06:23,480
then duplicate that down change first to
8230
05:06:21,760 --> 05:06:25,520
second and then change this also to
8231
05:06:23,480 --> 05:06:27,200
Second and then inside a console right
8232
05:06:25,520 --> 05:06:29,400
line we can actually have the add
8233
05:06:27,200 --> 05:06:31,000
function and we can type in first number
8234
05:06:29,400 --> 05:06:32,040
and then we can type in second number
8235
05:06:31,000 --> 05:06:33,798
and if you don't want to call the
8236
05:06:32,040 --> 05:06:35,480
function inside here you can actually
8237
05:06:33,798 --> 05:06:38,040
just make another int and we can say int
8238
05:06:35,480 --> 05:06:39,920
result equals add and just bring this
8239
05:06:38,040 --> 05:06:41,360
line over to here and then in the
8240
05:06:39,920 --> 05:06:42,280
console right line you can just print
8241
05:06:41,360 --> 05:06:44,718
the
8242
05:06:42,280 --> 05:06:47,400
result there we go so we read in both
8243
05:06:44,718 --> 05:06:49,160
the values execute the ad function and
8244
05:06:47,400 --> 05:06:51,240
then return it back to the user using a
8245
05:06:49,160 --> 05:06:53,080
console right line so these are the
8246
05:06:51,240 --> 05:06:55,558
other ones and then here they are so if
8247
05:06:53,080 --> 05:06:56,958
we do 50 and we do 50 again and you see
8248
05:06:55,558 --> 05:06:58,760
that I've messed up because we have
8249
05:06:56,958 --> 05:07:00,760
double colums now so although our
8250
05:06:58,760 --> 05:07:02,360
calculator did indeed work let's just go
8251
05:07:00,760 --> 05:07:06,320
and correct that so we can take these
8252
05:07:02,360 --> 05:07:06,320
off now because we added it inside
8253
05:07:06,400 --> 05:07:12,320
here 50 and 25 we should get set 75
8254
05:07:10,558 --> 05:07:14,360
perfect so now we've made a little basic
8255
05:07:12,320 --> 05:07:16,638
calculator by simply making use these
8256
05:07:14,360 --> 05:07:17,798
functions and if we just expand this
8257
05:07:16,638 --> 05:07:21,120
just to show you what it would have
8258
05:07:17,798 --> 05:07:24,000
looked like so we go in from this and we
8259
05:07:21,120 --> 05:07:27,160
place our message inside to here and
8260
05:07:24,000 --> 05:07:29,440
then we have our convert line just like
8261
05:07:27,160 --> 05:07:31,200
this and then if we duplicate that down
8262
05:07:29,440 --> 05:07:32,718
we can change this to Second n and then
8263
05:07:31,200 --> 05:07:35,520
to second
8264
05:07:32,718 --> 05:07:38,080
number and then as for the result we can
8265
05:07:35,520 --> 05:07:40,440
say first num plus second
8266
05:07:38,080 --> 05:07:41,798
n there we go you can see the code is
8267
05:07:40,440 --> 05:07:44,400
drastically longer and it's a bit
8268
05:07:41,798 --> 05:07:47,080
meaningless to have duplica code this is
8269
05:07:44,400 --> 05:07:48,718
literally the exact same line in both of
8270
05:07:47,080 --> 05:07:51,360
these sections except for the fact that
8271
05:07:48,718 --> 05:07:52,718
this ISS first and this ISS second and
8272
05:07:51,360 --> 05:07:54,878
we're starring in first and we're
8273
05:07:52,718 --> 05:07:56,958
starring in second so let's undo that
8274
05:07:54,878 --> 05:07:58,680
and get back to our more efficient code
8275
05:07:56,958 --> 05:08:00,360
there we go you can see that these three
8276
05:07:58,680 --> 05:08:02,160
lines are considerably shorter and
8277
05:08:00,360 --> 05:08:03,840
easier to read this is telling us we
8278
05:08:02,160 --> 05:08:05,558
want to read an integer here is the
8279
05:08:03,840 --> 05:08:07,840
value and we want to return it inside
8280
05:08:05,558 --> 05:08:09,080
our first Nome and so on and so forth
8281
05:08:07,840 --> 05:08:10,718
let's have a look at another example
8282
05:08:09,080 --> 05:08:12,878
where we can make use of this so we can
8283
05:08:10,718 --> 05:08:16,600
have a string called name and we can
8284
05:08:12,878 --> 05:08:18,920
read this in from the console console.
8285
05:08:16,600 --> 05:08:21,480
readline but we need to Output a message
8286
05:08:18,920 --> 05:08:23,840
so we can have another console right and
8287
05:08:21,480 --> 05:08:27,160
we can say enter your
8288
05:08:23,840 --> 05:08:29,080
name and then we can have this twice and
8289
05:08:27,160 --> 05:08:30,718
we can do enter your age and then have
8290
05:08:29,080 --> 05:08:32,760
an INT
8291
05:08:30,718 --> 05:08:34,160
age but of course we don't want to be
8292
05:08:32,760 --> 05:08:36,400
using this approach and putting a
8293
05:08:34,160 --> 05:08:38,840
console right line inside here with a
8294
05:08:36,400 --> 05:08:40,558
convert in32 because although this is
8295
05:08:38,840 --> 05:08:42,440
actually technically correct now what
8296
05:08:40,558 --> 05:08:44,558
UND doing the point Val function so
8297
05:08:42,440 --> 05:08:46,160
let's change this into our readint and
8298
05:08:44,558 --> 05:08:47,760
then as for our message well we have the
8299
05:08:46,160 --> 05:08:49,360
message right here and again we don't
8300
05:08:47,760 --> 05:08:51,320
need the space callon so let's remove
8301
05:08:49,360 --> 05:08:53,400
that and then as for the name we can
8302
05:08:51,320 --> 05:08:55,120
actually do this differently so this is
8303
05:08:53,400 --> 05:08:56,878
actually going to do a string variable
8304
05:08:55,120 --> 05:08:58,878
so what we want to do instead of having
8305
05:08:56,878 --> 05:09:01,240
read in we can have another function
8306
05:08:58,878 --> 05:09:03,160
called read string another string
8307
05:09:01,240 --> 05:09:06,000
message parameter and then do exactly
8308
05:09:03,160 --> 05:09:08,400
the same thing have a console right and
8309
05:09:06,000 --> 05:09:10,240
we can place string interpolation and
8310
05:09:08,400 --> 05:09:12,760
place a message inside here followed by
8311
05:09:10,240 --> 05:09:16,240
callon space and then we can just simply
8312
05:09:12,760 --> 05:09:18,360
return the console. readline there we go
8313
05:09:16,240 --> 05:09:20,120
and instead of doing all of this we can
8314
05:09:18,360 --> 05:09:22,760
actually just put our read string inside
8315
05:09:20,120 --> 05:09:25,040
here and then place our ENT your name
8316
05:09:22,760 --> 05:09:27,240
inside here just without the callon
8317
05:09:25,040 --> 05:09:29,200
space because we don't need that so now
8318
05:09:27,240 --> 05:09:31,280
we reduce this section to two lines as
8319
05:09:29,200 --> 05:09:33,558
well so now what we can do is have a
8320
05:09:31,280 --> 05:09:35,200
string details and make that equal to a
8321
05:09:33,558 --> 05:09:37,040
function that we're going to use now we
8322
05:09:35,200 --> 05:09:39,600
can say user details if we want to give
8323
05:09:37,040 --> 05:09:41,120
it the name and give it the age and that
8324
05:09:39,600 --> 05:09:43,040
will give us back a string that we can
8325
05:09:41,120 --> 05:09:45,120
print to the console so we can say
8326
05:09:43,040 --> 05:09:47,480
details so let's go ahead and make use
8327
05:09:45,120 --> 05:09:48,680
of this function so we can have a static
8328
05:09:47,480 --> 05:09:50,120
and it's going to return us back a
8329
05:09:48,680 --> 05:09:52,120
string cuz we're placing it inside the
8330
05:09:50,120 --> 05:09:55,040
string and then it's going to be user
8331
05:09:52,120 --> 05:09:58,000
details and then our name is a string
8332
05:09:55,040 --> 05:09:59,520
our age as an INT and then inside here
8333
05:09:58,000 --> 05:10:02,080
we just want to concatenate them two
8334
05:09:59,520 --> 05:10:07,718
values together so we can say hello my
8335
05:10:02,080 --> 05:10:09,080
name is name and my age is age and
8336
05:10:07,718 --> 05:10:10,958
because that's returning us back with a
8337
05:10:09,080 --> 05:10:13,200
string we can make use of it inside
8338
05:10:10,958 --> 05:10:15,920
string details or we can place this
8339
05:10:13,200 --> 05:10:17,840
straight inside the console right line
8340
05:10:15,920 --> 05:10:19,760
let's leave it as a function call assign
8341
05:10:17,840 --> 05:10:22,400
into string details and then print out
8342
05:10:19,760 --> 05:10:24,440
to the screen let's just skip over these
8343
05:10:22,400 --> 05:10:26,638
sections enter your name abber enter
8344
05:10:24,440 --> 05:10:29,120
your age 23 and then there you go hello
8345
05:10:26,638 --> 05:10:31,480
my name is abber and my age is
8346
05:10:29,120 --> 05:10:33,558
23 so this is how we can make use of
8347
05:10:31,480 --> 05:10:35,200
parameters inside our functions so we
8348
05:10:33,558 --> 05:10:36,798
can actually give it some sort of data
8349
05:10:35,200 --> 05:10:37,920
that's meaningful if you didn't use
8350
05:10:36,798 --> 05:10:39,638
parameters then what you're going to
8351
05:10:37,920 --> 05:10:41,240
have to face is for example example what
8352
05:10:39,638 --> 05:10:42,480
we showed you with this integer if you
8353
05:10:41,240 --> 05:10:44,160
don't use parameters then you're going
8354
05:10:42,480 --> 05:10:46,480
to have situations like I showed you
8355
05:10:44,160 --> 05:10:48,360
before with this integer function it
8356
05:10:46,480 --> 05:10:50,840
returns back an integer but it only gave
8357
05:10:48,360 --> 05:10:52,480
us the value of 5 + 5 I mean technically
8358
05:10:50,840 --> 05:10:54,320
you don't need to return anything you
8359
05:10:52,480 --> 05:10:55,878
can actually just keep this aoid and
8360
05:10:54,320 --> 05:10:57,718
just wrap this around in console right
8361
05:10:55,878 --> 05:10:59,718
line the only issue with this approach
8362
05:10:57,718 --> 05:11:01,760
is you don't have any option other than
8363
05:10:59,718 --> 05:11:03,320
to print out of the screen what if you
8364
05:11:01,760 --> 05:11:04,878
didn't want to print out of the screen
8365
05:11:03,320 --> 05:11:06,558
and you simply just wanted to return the
8366
05:11:04,878 --> 05:11:08,360
calculation and then store it inside
8367
05:11:06,558 --> 05:11:10,120
another variable this doesn't give you
8368
05:11:08,360 --> 05:11:11,520
the flexibility of that so we change
8369
05:11:10,120 --> 05:11:13,520
this into an INT and instead of
8370
05:11:11,520 --> 05:11:15,680
returning it back to the console we just
8371
05:11:13,520 --> 05:11:17,360
return back the pure value and then if
8372
05:11:15,680 --> 05:11:19,878
the user decides that they want to print
8373
05:11:17,360 --> 05:11:21,360
this out to the screen that's fine like
8374
05:11:19,878 --> 05:11:23,520
here we've made a decision that we want
8375
05:11:21,360 --> 05:11:25,760
to print it out let's just say we had a
8376
05:11:23,520 --> 05:11:27,798
variable that said in calculation then
8377
05:11:25,760 --> 05:11:29,798
we place that inside but we actually
8378
05:11:27,798 --> 05:11:31,878
never wanted to print it maybe we kept
8379
05:11:29,798 --> 05:11:33,638
it like this and then use Cal for
8380
05:11:31,878 --> 05:11:35,400
something else down below or maybe that
8381
05:11:33,638 --> 05:11:37,280
was something like storing inside a
8382
05:11:35,400 --> 05:11:39,280
database not everything will be printed
8383
05:11:37,280 --> 05:11:40,680
out to the screen for the user to see
8384
05:11:39,280 --> 05:11:42,520
the best thing to do is return the
8385
05:11:40,680 --> 05:11:44,040
values in their purus form because you
8386
05:11:42,520 --> 05:11:45,878
never know what the developer or the
8387
05:11:44,040 --> 05:11:47,680
user is going to make use of that data
8388
05:11:45,878 --> 05:11:49,760
later on down the line keeping that it's
8389
05:11:47,680 --> 05:11:51,160
purus form is the best way possible for
8390
05:11:49,760 --> 05:11:52,600
example in this case we're actually
8391
05:11:51,160 --> 05:11:54,958
reading back the integer and then print
8392
05:11:52,600 --> 05:11:56,638
it out to the screen but in this exact
8393
05:11:54,958 --> 05:11:58,160
case which is the best case scenario
8394
05:11:56,638 --> 05:12:00,958
that I just showed you we're actually
8395
05:11:58,160 --> 05:12:02,718
making use of first n and second n but
8396
05:12:00,958 --> 05:12:04,638
first n and second n are never actually
8397
05:12:02,718 --> 05:12:06,878
print to the screen it's always just
8398
05:12:04,638 --> 05:12:08,840
used for the calculation if we use
8399
05:12:06,878 --> 05:12:10,240
console right line to print out first n
8400
05:12:08,840 --> 05:12:12,360
and second them then we're not going to
8401
05:12:10,240 --> 05:12:14,440
be able to use it inside our calculation
8402
05:12:12,360 --> 05:12:16,480
and therefore our very basic calculator
8403
05:12:14,440 --> 05:12:19,480
is now being rendered pointless so read
8404
05:12:16,480 --> 05:12:21,600
in and add or return back values that we
8405
05:12:19,480 --> 05:12:24,400
can use in their purest form and that is
8406
05:12:21,600 --> 05:12:24,400
the whole goal of
8407
05:12:24,480 --> 05:12:27,878
parameters so now that we've covered
8408
05:12:26,400 --> 05:12:29,798
parameters that are always going to be
8409
05:12:27,878 --> 05:12:32,000
required let's have a look at another
8410
05:12:29,798 --> 05:12:35,080
concept called optional parameters so we
8411
05:12:32,000 --> 05:12:37,160
had a static in add and we add int a and
8412
05:12:35,080 --> 05:12:39,680
int B for example and then we simply
8413
05:12:37,160 --> 05:12:41,958
just returned a plus b like use in the
8414
05:12:39,680 --> 05:12:44,200
previous examples if we have an INT
8415
05:12:41,958 --> 05:12:46,280
result and we want to call that function
8416
05:12:44,200 --> 05:12:47,878
and we give it the value five and five
8417
05:12:46,280 --> 05:12:49,638
and then print out to the screen
8418
05:12:47,878 --> 05:12:52,040
everything runs okay because we've added
8419
05:12:49,638 --> 05:12:53,958
in exactly what we expect let's say we
8420
05:12:52,040 --> 05:12:55,400
don't actually have this additional five
8421
05:12:53,958 --> 05:12:57,080
of course this doesn't make any sense
8422
05:12:55,400 --> 05:12:59,360
cuz we're meant to be adding two numbers
8423
05:12:57,080 --> 05:13:01,160
but just hum me for a minute then what
8424
05:12:59,360 --> 05:13:03,000
this will do is says there's no argument
8425
05:13:01,160 --> 05:13:04,718
given that corresponds to the parameter
8426
05:13:03,000 --> 05:13:06,600
B now of course this is a problem
8427
05:13:04,718 --> 05:13:07,878
because it's a red underline which means
8428
05:13:06,600 --> 05:13:09,878
if we try and run the code we're going
8429
05:13:07,878 --> 05:13:12,040
to get an issue let's say you were
8430
05:13:09,878 --> 05:13:15,120
creating some sort of system that if you
8431
05:13:12,040 --> 05:13:16,920
entered both values it uses both values
8432
05:13:15,120 --> 05:13:19,480
but if you only entered one it
8433
05:13:16,920 --> 05:13:21,638
automatically set the second value to
8434
05:13:19,480 --> 05:13:24,000
something else and we can achieve this
8435
05:13:21,638 --> 05:13:26,440
Behavior using optional parameters so an
8436
05:13:24,000 --> 05:13:28,558
optional parameter doesn't exactly mean
8437
05:13:26,440 --> 05:13:30,120
quite optional it kind of just means
8438
05:13:28,558 --> 05:13:32,080
that it's going to be optional but you
8439
05:13:30,120 --> 05:13:35,520
can also give it a default value this
8440
05:13:32,080 --> 05:13:37,680
will always add two numbers A and B even
8441
05:13:35,520 --> 05:13:40,040
if we make a or b optional it's going to
8442
05:13:37,680 --> 05:13:41,920
still need two numbers to add in order
8443
05:13:40,040 --> 05:13:44,120
for the function to run so in order to
8444
05:13:41,920 --> 05:13:46,320
make a parameter optional let's just say
8445
05:13:44,120 --> 05:13:48,280
we want to make B optional we can type
8446
05:13:46,320 --> 05:13:49,878
in it equals straight after it and
8447
05:13:48,280 --> 05:13:52,040
actually give it a value so as you can
8448
05:13:49,878 --> 05:13:54,320
see our syntax error is now gone and if
8449
05:13:52,040 --> 05:13:57,440
you run the code we should get
8450
05:13:54,320 --> 05:13:59,878
55 and the reason why this works is a is
8451
05:13:57,440 --> 05:14:02,160
now five if you hover over here we can
8452
05:13:59,878 --> 05:14:04,320
see a is five and then the square
8453
05:14:02,160 --> 05:14:06,320
bracket around the int B actually
8454
05:14:04,320 --> 05:14:09,480
denotes that it's optional and it says
8455
05:14:06,320 --> 05:14:12,000
int b equals 50 cuz we have that h here
8456
05:14:09,480 --> 05:14:14,200
so it's actually using this as 50 and if
8457
05:14:12,000 --> 05:14:16,120
we don't specify the second parameter
8458
05:14:14,200 --> 05:14:18,040
then it's practically just doing this
8459
05:14:16,120 --> 05:14:20,160
which is completely fine so the reason
8460
05:14:18,040 --> 05:14:21,360
why I said it's not quite optional is
8461
05:14:20,160 --> 05:14:23,400
because you still need to give it a
8462
05:14:21,360 --> 05:14:25,400
value of course in here you can use the
8463
05:14:23,400 --> 05:14:27,240
default keyword in the case of an
8464
05:14:25,400 --> 05:14:29,200
integer the default value is actually
8465
05:14:27,240 --> 05:14:32,120
zero so if you just run that code now
8466
05:14:29,200 --> 05:14:34,680
it's going to say 5 + 0 which is just
8467
05:14:32,120 --> 05:14:36,718
five so it can be optional but you still
8468
05:14:34,680 --> 05:14:38,320
need to give it some sort of value of
8469
05:14:36,718 --> 05:14:40,360
course if you're doing a calculator and
8470
05:14:38,320 --> 05:14:42,680
you're adding to values if you only
8471
05:14:40,360 --> 05:14:44,558
specify the first one then adding zero
8472
05:14:42,680 --> 05:14:46,558
to it is simply not going to change it
8473
05:14:44,558 --> 05:14:48,638
so zero or default is going to be the
8474
05:14:46,558 --> 05:14:50,798
best user case when you're dealing with
8475
05:14:48,638 --> 05:14:52,680
default values with integers so another
8476
05:14:50,798 --> 05:14:54,638
way you can actually specify this is by
8477
05:14:52,680 --> 05:14:57,000
inserting this thing called an attribute
8478
05:14:54,638 --> 05:14:59,000
if you remove this equal zero and just
8479
05:14:57,000 --> 05:15:00,878
before the end we can type in Open
8480
05:14:59,000 --> 05:15:03,878
Bracket close bracket and type in
8481
05:15:00,878 --> 05:15:05,240
optional with the capital O and then
8482
05:15:03,878 --> 05:15:07,080
you're going to get a red underline if
8483
05:15:05,240 --> 05:15:09,080
you hover over it it says we need to
8484
05:15:07,080 --> 05:15:11,080
include this extra Library which which
8485
05:15:09,080 --> 05:15:13,240
will import it at the top of our code as
8486
05:15:11,080 --> 05:15:15,200
you can see here now that this is
8487
05:15:13,240 --> 05:15:17,200
available it says indicates that a
8488
05:15:15,200 --> 05:15:19,160
parameter is optional and what this will
8489
05:15:17,200 --> 05:15:21,638
do by default is it'll give you the
8490
05:15:19,160 --> 05:15:23,760
basic value for this number so what I
8491
05:15:21,638 --> 05:15:26,798
mean by that is we can type in here just
8492
05:15:23,760 --> 05:15:28,160
to show you this b equals and then B and
8493
05:15:26,798 --> 05:15:30,120
this will just give you the default
8494
05:15:28,160 --> 05:15:31,760
value based on the data type and as we
8495
05:15:30,120 --> 05:15:34,718
just said a minute ago that value is
8496
05:15:31,760 --> 05:15:37,360
actually going to be zero there you go B
8497
05:15:34,718 --> 05:15:40,360
is zero so all it's doing is saying 5 +
8498
05:15:37,360 --> 05:15:41,958
0 = 5
8499
05:15:40,360 --> 05:15:43,840
so that's another way of doing it or you
8500
05:15:41,958 --> 05:15:46,480
could just leave it as I said before you
8501
05:15:43,840 --> 05:15:48,760
can say into b equals default or into b
8502
05:15:46,480 --> 05:15:50,680
equals 0 it's completely up to you I
8503
05:15:48,760 --> 05:15:52,160
prefer using the default keyword so you
8504
05:15:50,680 --> 05:15:53,840
don't have to keep remembering which
8505
05:15:52,160 --> 05:15:56,320
different data types have the default
8506
05:15:53,840 --> 05:15:57,920
values in the inter just case it's zero
8507
05:15:56,320 --> 05:15:59,718
but I just like leaving the default and
8508
05:15:57,920 --> 05:16:01,120
we can get rid of this line now let's
8509
05:15:59,718 --> 05:16:02,638
have a look at different situations
8510
05:16:01,120 --> 05:16:05,600
where this will be helpful if you had a
8511
05:16:02,638 --> 05:16:07,680
function called print name and we add a
8512
05:16:05,600 --> 05:16:10,200
string name in here and then we can
8513
05:16:07,680 --> 05:16:13,798
simply just say console right line my
8514
05:16:10,200 --> 05:16:16,400
name is and then name this is quite easy
8515
05:16:13,798 --> 05:16:18,280
to use so we can say print name and then
8516
05:16:16,400 --> 05:16:20,520
Place AB inside the brackets and then
8517
05:16:18,280 --> 05:16:22,958
run the code and we get abber printed to
8518
05:16:20,520 --> 05:16:25,040
the screen and that's fine let's say if
8519
05:16:22,958 --> 05:16:26,958
we had someone else's name in there as
8520
05:16:25,040 --> 05:16:28,520
Joe for example then we get Joe to the
8521
05:16:26,958 --> 05:16:30,440
screen and let's just say as the
8522
05:16:28,520 --> 05:16:32,400
developer you only want to default this
8523
05:16:30,440 --> 05:16:34,040
to abber if you don't actually insert
8524
05:16:32,400 --> 05:16:35,240
anything so now if you don't insert
8525
05:16:34,040 --> 05:16:36,958
anything of course we're going to get a
8526
05:16:35,240 --> 05:16:39,160
compile error because we need a
8527
05:16:36,958 --> 05:16:41,160
parameter but if we assume what we did
8528
05:16:39,160 --> 05:16:43,040
here and just follow the same approach
8529
05:16:41,160 --> 05:16:45,320
if we use default then it's going to
8530
05:16:43,040 --> 05:16:47,760
give us nothing because the default
8531
05:16:45,320 --> 05:16:49,798
value for a string is just empty quotes
8532
05:16:47,760 --> 05:16:52,200
which is just nothing but instead of
8533
05:16:49,798 --> 05:16:54,520
default we can actually specify value so
8534
05:16:52,200 --> 05:16:57,280
we can say if no one types their name
8535
05:16:54,520 --> 05:17:00,360
inside the bracket then use
8536
05:16:57,280 --> 05:17:02,000
abber there we go so you can have this
8537
05:17:00,360 --> 05:17:04,200
optional parameter that defaults to
8538
05:17:02,000 --> 05:17:06,120
something and if someone types in jaw or
8539
05:17:04,200 --> 05:17:07,958
whatever inside the print name function
8540
05:17:06,120 --> 05:17:09,878
then the default optional parameter is
8541
05:17:07,958 --> 05:17:11,798
not going to be used since this will be
8542
05:17:09,878 --> 05:17:13,718
overridden if you have a parameter
8543
05:17:11,798 --> 05:17:15,680
placed inside here so now the ab is
8544
05:17:13,718 --> 05:17:17,200
going to get overridden by the jaw
8545
05:17:15,680 --> 05:17:19,680
because this is actually the parameter
8546
05:17:17,200 --> 05:17:21,798
that's been used in the
8547
05:17:19,680 --> 05:17:23,320
function so you've looked at functions
8548
05:17:21,798 --> 05:17:25,000
with normal parameters optional
8549
05:17:23,320 --> 05:17:26,638
parameters and now let's take a look at
8550
05:17:25,000 --> 05:17:28,520
name parameters so let's say you had a
8551
05:17:26,638 --> 05:17:32,558
situation where you had string name
8552
05:17:28,520 --> 05:17:35,638
abber in age equal 23 and string address
8553
05:17:32,558 --> 05:17:37,120
equals one something rad and then you
8554
05:17:35,638 --> 05:17:39,240
had a function that would simply print
8555
05:17:37,120 --> 05:17:42,120
these details out to the console so
8556
05:17:39,240 --> 05:17:45,798
static void print details there string
8557
05:17:42,120 --> 05:17:47,840
name inage String
8558
05:17:45,798 --> 05:17:49,558
address so you have three console right
8559
05:17:47,840 --> 05:17:51,680
lines that just print out a name value
8560
05:17:49,558 --> 05:17:53,878
pair just like
8561
05:17:51,680 --> 05:17:55,558
this and then when you go to call this
8562
05:17:53,878 --> 05:17:57,878
function what you're going to have to do
8563
05:17:55,558 --> 05:18:00,200
is specify the name then the age then
8564
05:17:57,878 --> 05:18:02,080
the address in that specific order as
8565
05:18:00,200 --> 05:18:03,878
per the parameter show if you try and
8566
05:18:02,080 --> 05:18:05,080
run the code now we should get the
8567
05:18:03,878 --> 05:18:07,798
desired
8568
05:18:05,080 --> 05:18:09,400
output perfect let's say for example you
8569
05:18:07,798 --> 05:18:11,680
wanted to rearrange the parameters and
8570
05:18:09,400 --> 05:18:13,760
put Age first you'll see we have a
8571
05:18:11,680 --> 05:18:15,480
compiler error now because age and name
8572
05:18:13,760 --> 05:18:17,920
are in the wrong order string name
8573
05:18:15,480 --> 05:18:19,440
should be first and int age to be second
8574
05:18:17,920 --> 05:18:21,240
we can still achieve this by using
8575
05:18:19,440 --> 05:18:22,920
something called name parameters we
8576
05:18:21,240 --> 05:18:24,920
don't have to stick to this order if we
8577
05:18:22,920 --> 05:18:27,840
tell the compiler which parameter we're
8578
05:18:24,920 --> 05:18:29,878
directly talking about so we can say age
8579
05:18:27,840 --> 05:18:31,440
callon because our parameter is now
8580
05:18:29,878 --> 05:18:33,320
called age as you can see it's been
8581
05:18:31,440 --> 05:18:34,680
highlighted and then after the colon
8582
05:18:33,320 --> 05:18:37,120
It's the variables that we're talking
8583
05:18:34,680 --> 05:18:39,440
about so it just happens to be that age
8584
05:18:37,120 --> 05:18:42,240
is the same value as the edge
8585
05:18:39,440 --> 05:18:44,240
so maybe we can change to age input and
8586
05:18:42,240 --> 05:18:47,040
place input the end of these so then we
8587
05:18:44,240 --> 05:18:49,440
can have age equals age input and name
8588
05:18:47,040 --> 05:18:51,240
parameter equals the name input and the
8589
05:18:49,440 --> 05:18:53,840
address parameter equals the address
8590
05:18:51,240 --> 05:18:55,480
input now we can have it like this and
8591
05:18:53,840 --> 05:18:57,680
to make it look even better you can
8592
05:18:55,480 --> 05:18:59,320
actually add new lines in here just to
8593
05:18:57,680 --> 05:19:00,680
break it off like this and this is a
8594
05:18:59,320 --> 05:19:02,680
similar pattern that you'll see in
8595
05:19:00,680 --> 05:19:04,440
normal production code putting a space
8596
05:19:02,680 --> 05:19:06,040
here can separate them out a little bit
8597
05:19:04,440 --> 05:19:07,798
and there we go it doesn't matter which
8598
05:19:06,040 --> 05:19:10,280
order that we put these in right now we
8599
05:19:07,798 --> 05:19:12,200
can even put address first first and
8600
05:19:10,280 --> 05:19:13,840
it'll still work okay and if we run it
8601
05:19:12,200 --> 05:19:15,520
you should still get exactly the same
8602
05:19:13,840 --> 05:19:17,160
output it's just specified in a
8603
05:19:15,520 --> 05:19:18,958
different way so let's just have a
8604
05:19:17,160 --> 05:19:20,320
little recap to make use of name
8605
05:19:18,958 --> 05:19:22,080
parameters you don't need to do anything
8606
05:19:20,320 --> 05:19:23,920
to the original function you just write
8607
05:19:22,080 --> 05:19:25,760
it exactly the way you're used to doing
8608
05:19:23,920 --> 05:19:27,718
you specify the parameters in the order
8609
05:19:25,760 --> 05:19:29,480
that you think is sensible and when you
8610
05:19:27,718 --> 05:19:31,600
go to call it you can simply just type
8611
05:19:29,480 --> 05:19:33,558
in the parameter's name first followed
8612
05:19:31,600 --> 05:19:35,080
by callon and then the variable name
8613
05:19:33,558 --> 05:19:36,280
that you want to address once you've
8614
05:19:35,080 --> 05:19:38,120
done that then you can use name
8615
05:19:36,280 --> 05:19:39,680
parameters you can go back to using the
8616
05:19:38,120 --> 05:19:41,520
normal function Fun by just getting rid
8617
05:19:39,680 --> 05:19:43,400
of the name followed by the colon and
8618
05:19:41,520 --> 05:19:45,320
just replacing all of the parameters
8619
05:19:43,400 --> 05:19:47,520
using the correct order and the original
8620
05:19:45,320 --> 05:19:51,320
variables instead of the new notation
8621
05:19:47,520 --> 05:19:53,760
having Name colon followed by name
8622
05:19:51,320 --> 05:19:56,160
input so far we've covered functions
8623
05:19:53,760 --> 05:19:58,558
with no parameters normal parameters
8624
05:19:56,160 --> 05:20:00,200
optional parameters named parameters and
8625
05:19:58,558 --> 05:20:01,958
let's look at a concept called out
8626
05:20:00,200 --> 05:20:03,760
parameters let's demonstrate a quick
8627
05:20:01,958 --> 05:20:05,760
example so we call a function called
8628
05:20:03,760 --> 05:20:07,440
test and we pass in a value and let's
8629
05:20:05,760 --> 05:20:09,878
just make this work so we can have an
8630
05:20:07,440 --> 05:20:12,878
INT Norm equal zero and then we have a
8631
05:20:09,878 --> 05:20:16,360
test function so we can say static void
8632
05:20:12,878 --> 05:20:18,558
test and we can say in n and inside here
8633
05:20:16,360 --> 05:20:20,558
let's make num the value of five now in
8634
05:20:18,558 --> 05:20:22,040
this example if we print out num to the
8635
05:20:20,558 --> 05:20:24,040
screen what do you think is going to
8636
05:20:22,040 --> 05:20:25,680
happen is num going to take the value of
8637
05:20:24,040 --> 05:20:27,480
five or is you're going to keep the
8638
05:20:25,680 --> 05:20:29,680
value zero I'll give you a second to
8639
05:20:27,480 --> 05:20:33,840
think and let's run the
8640
05:20:29,680 --> 05:20:33,840
code so let's run it and see what
8641
05:20:33,920 --> 05:20:38,120
happens and if you guess five you're
8642
05:20:36,120 --> 05:20:40,040
indeed Incorrect and if you guess zero
8643
05:20:38,120 --> 05:20:42,360
you are correct correct and the reason
8644
05:20:40,040 --> 05:20:45,520
why it's not five is because when you
8645
05:20:42,360 --> 05:20:47,320
pass a num inside this parameter it just
8646
05:20:45,520 --> 05:20:49,878
becomes a parameter and therefore it
8647
05:20:47,320 --> 05:20:51,718
becomes a local variable even though
8648
05:20:49,878 --> 05:20:53,718
these two variables are actually called
8649
05:20:51,718 --> 05:20:55,958
exactly the same thing you'll see that
8650
05:20:53,718 --> 05:20:58,240
when I highlight it it only highlights
8651
05:20:55,958 --> 05:20:59,840
this one and doesn't highlight this one
8652
05:20:58,240 --> 05:21:01,440
and the same is true for this it
8653
05:20:59,840 --> 05:21:03,440
highlights these three references but
8654
05:21:01,440 --> 05:21:06,040
not the ones down here the num variable
8655
05:21:03,440 --> 05:21:08,040
is only accessible in each other scope
8656
05:21:06,040 --> 05:21:10,280
this one is available through here and
8657
05:21:08,040 --> 05:21:13,000
here and this one is available through
8658
05:21:10,280 --> 05:21:14,558
here and here therefore if we use this
8659
05:21:13,000 --> 05:21:16,840
assignment then it's actually doing
8660
05:21:14,558 --> 05:21:20,958
nothing if we print out gnome inside
8661
05:21:16,840 --> 05:21:23,000
here num equals num and then we print
8662
05:21:20,958 --> 05:21:24,958
that we can see that it is assigned the
8663
05:21:23,000 --> 05:21:26,480
value five but because it doesn't do
8664
05:21:24,958 --> 05:21:28,240
anything at the end of this function
8665
05:21:26,480 --> 05:21:30,520
then it simply doesn't get returned back
8666
05:21:28,240 --> 05:21:32,638
or anything happens to it so let's see
8667
05:21:30,520 --> 05:21:34,718
how we can make this in to five when it
8668
05:21:32,638 --> 05:21:36,120
gets printed out here so the options
8669
05:21:34,718 --> 05:21:39,040
that we have is we can actually make
8670
05:21:36,120 --> 05:21:41,480
this viid an integer and we can say num
8671
05:21:39,040 --> 05:21:43,040
equals 5 and then we can return num or
8672
05:21:41,480 --> 05:21:44,958
alternatively we could simply just
8673
05:21:43,040 --> 05:21:47,480
return five and not do both of these
8674
05:21:44,958 --> 05:21:49,798
lines so if we run that now then we
8675
05:21:47,480 --> 05:21:51,840
still get zero and the reason why we get
8676
05:21:49,798 --> 05:21:54,040
zero is the test function is being
8677
05:21:51,840 --> 05:21:55,680
called and it returns back an integer
8678
05:21:54,040 --> 05:21:58,798
but we're simply not doing anything with
8679
05:21:55,680 --> 05:22:00,638
it so we can say num equals the return
8680
05:21:58,798 --> 05:22:02,440
value of this function which will be
8681
05:22:00,638 --> 05:22:05,558
five and now if we run it we should see
8682
05:22:02,440 --> 05:22:07,000
how five perfect now this is a little
8683
05:22:05,558 --> 05:22:09,160
bit cumbersome and let's say you wanted
8684
05:22:07,000 --> 05:22:10,958
to return a value and return back
8685
05:22:09,160 --> 05:22:12,680
another value at the same time than what
8686
05:22:10,958 --> 05:22:14,878
are your options so let me show you a
8687
05:22:12,680 --> 05:22:18,480
quick example when you're using int.
8688
05:22:14,878 --> 05:22:20,638
trass we have a string s an out int
8689
05:22:18,480 --> 05:22:23,000
result and a Bool so what this function
8690
05:22:20,638 --> 05:22:25,558
is effectively doing is return you back
8691
05:22:23,000 --> 05:22:27,120
a true or false value to dictate whether
8692
05:22:25,558 --> 05:22:29,280
or not the conversion has been
8693
05:22:27,120 --> 05:22:31,958
successful and it's also returned you an
8694
05:22:29,280 --> 05:22:34,000
out in result and what this means is you
8695
05:22:31,958 --> 05:22:35,878
can actually specify integer variable
8696
05:22:34,000 --> 05:22:38,000
straight inside the brackets so in
8697
05:22:35,878 --> 05:22:40,400
essence this function has gave you back
8698
05:22:38,000 --> 05:22:42,558
two values it's give you back something
8699
05:22:40,400 --> 05:22:44,400
from the integer parameter and it's also
8700
05:22:42,558 --> 05:22:46,480
returned back a Boolean variable so
8701
05:22:44,400 --> 05:22:48,200
let's convert this to code and see what
8702
05:22:46,480 --> 05:22:51,040
the third option is of trying to return
8703
05:22:48,200 --> 05:22:53,240
back five over to this so if we just
8704
05:22:51,040 --> 05:22:55,520
comment out this line and as you saw in
8705
05:22:53,240 --> 05:22:57,840
the trip pass it simply just need to put
8706
05:22:55,520 --> 05:23:02,160
the word out just before
8707
05:22:57,840 --> 05:23:03,958
here out int num now we don't actually
8708
05:23:02,160 --> 05:23:05,798
need to return anything in this specific
8709
05:23:03,958 --> 05:23:09,040
example because we want to just change a
8710
05:23:05,798 --> 05:23:10,440
num variable using the out parameter but
8711
05:23:09,040 --> 05:23:12,360
if we wanted to return something
8712
05:23:10,440 --> 05:23:14,080
separate to the num like a boing like
8713
05:23:12,360 --> 05:23:16,600
the tri pass did then we have that
8714
05:23:14,080 --> 05:23:18,320
option so we can return back true but
8715
05:23:16,600 --> 05:23:20,280
just before we return back true as
8716
05:23:18,320 --> 05:23:22,040
there's a red underline here it will say
8717
05:23:20,280 --> 05:23:24,680
that the out parameter num must be
8718
05:23:22,040 --> 05:23:27,480
assigned so we need to give num a
8719
05:23:24,680 --> 05:23:29,400
value we give it the value five because
8720
05:23:27,480 --> 05:23:31,760
what we're actually doing here even if
8721
05:23:29,400 --> 05:23:34,080
we double click on this we'll still get
8722
05:23:31,760 --> 05:23:36,440
num being selected here cuz the scope is
8723
05:23:34,080 --> 05:23:38,200
still exactly the same now except what's
8724
05:23:36,440 --> 05:23:39,920
happening is when this variable gets
8725
05:23:38,200 --> 05:23:41,360
returned it actually gets put into the
8726
05:23:39,920 --> 05:23:43,440
variable that it was initially called
8727
05:23:41,360 --> 05:23:45,480
with so let's fix these compiler errors
8728
05:23:43,440 --> 05:23:46,840
first so the reason why it's complaining
8729
05:23:45,480 --> 05:23:49,120
is because we need to give it the out
8730
05:23:46,840 --> 05:23:51,440
keyword so when we're specifying in Tri
8731
05:23:49,120 --> 05:23:53,638
pass our first parameter is a string so
8732
05:23:51,440 --> 05:23:56,080
if the user types in 1 2 3 we put in the
8733
05:23:53,638 --> 05:23:58,400
first parameter and then we'd say out in
8734
05:23:56,080 --> 05:24:00,718
result or something along them lines you
8735
05:23:58,400 --> 05:24:02,160
have an out and then you say you want to
8736
05:24:00,718 --> 05:24:04,240
make a new integer and you want to call
8737
05:24:02,160 --> 05:24:06,920
it result see in our case we don't need
8738
05:24:04,240 --> 05:24:08,638
to put int just like in here because
8739
05:24:06,920 --> 05:24:10,958
we've already created our num so the
8740
05:24:08,638 --> 05:24:12,878
only keyword we need is how to follow by
8741
05:24:10,958 --> 05:24:15,040
space and now of course we're trying to
8742
05:24:12,878 --> 05:24:16,600
assign this back into an integer which
8743
05:24:15,040 --> 05:24:19,240
we don't need anymore because this is a
8744
05:24:16,600 --> 05:24:22,840
booing so we can say Bo
8745
05:24:19,240 --> 05:24:24,120
success equals test out n so we print
8746
05:24:22,840 --> 05:24:26,558
out
8747
05:24:24,120 --> 05:24:28,798
Success Print them both the screen now
8748
05:24:26,558 --> 05:24:30,760
we should have five and true the true
8749
05:24:28,798 --> 05:24:33,040
comes from the return true and the five
8750
05:24:30,760 --> 05:24:34,160
comes from num equals 5 so what's
8751
05:24:33,040 --> 05:24:36,360
actually happening here is we're
8752
05:24:34,160 --> 05:24:38,480
creating our new integer we're passing
8753
05:24:36,360 --> 05:24:40,200
it into this function we're also telling
8754
05:24:38,480 --> 05:24:42,840
it's going to be an out parameter so
8755
05:24:40,200 --> 05:24:44,680
this function can still output things to
8756
05:24:42,840 --> 05:24:46,798
our number variable and then when it
8757
05:24:44,680 --> 05:24:48,718
gets placed inside here we assign num
8758
05:24:46,798 --> 05:24:50,958
equal to five and then just because we
8759
05:24:48,718 --> 05:24:53,320
have a bull we have to return true or we
8760
05:24:50,958 --> 05:24:55,400
can return false of course we can just
8761
05:24:53,320 --> 05:24:57,878
take this out completely take away the
8762
05:24:55,400 --> 05:25:00,320
return true and just keep this as
8763
05:24:57,878 --> 05:25:01,760
test of course in both scenarios these
8764
05:25:00,320 --> 05:25:03,760
aren't actually do anything they're just
8765
05:25:01,760 --> 05:25:06,200
completely random values it's just made
8766
05:25:03,760 --> 05:25:07,878
for demonstration so bu is returned back
8767
05:25:06,200 --> 05:25:09,480
true or false doesn't actually mean
8768
05:25:07,878 --> 05:25:11,680
anything I was just demonstrating that
8769
05:25:09,480 --> 05:25:13,760
you can have two return values from one
8770
05:25:11,680 --> 05:25:15,680
function there we go we get five when we
8771
05:25:13,760 --> 05:25:17,240
run it just like this so now I've
8772
05:25:15,680 --> 05:25:18,558
completed this little example let's have
8773
05:25:17,240 --> 05:25:20,958
a look at how it's used in the real
8774
05:25:18,558 --> 05:25:23,040
world so before we continue let's see if
8775
05:25:20,958 --> 05:25:25,200
we can rewrite the tripal function in
8776
05:25:23,040 --> 05:25:27,360
our code so it's going to be a static
8777
05:25:25,200 --> 05:25:29,600
returns back a bu we have a triy pass
8778
05:25:27,360 --> 05:25:32,480
our first parameter is a string s and
8779
05:25:29,600 --> 05:25:35,280
our second parameter is out into result
8780
05:25:32,480 --> 05:25:37,320
and inside there we can just do result
8781
05:25:35,280 --> 05:25:38,958
equals to zero and then we can return
8782
05:25:37,320 --> 05:25:40,958
back true of course we actually need to
8783
05:25:38,958 --> 05:25:42,080
do conversions inside here if you want
8784
05:25:40,958 --> 05:25:44,440
this function to work but I'm just
8785
05:25:42,080 --> 05:25:46,000
joining you for demonstration purpose if
8786
05:25:44,440 --> 05:25:47,638
we uncomment this line out and have a
8787
05:25:46,000 --> 05:25:50,040
look at the parameters again you'll see
8788
05:25:47,638 --> 05:25:52,600
that now they line up perfectly string s
8789
05:25:50,040 --> 05:25:54,440
out in result and it returns back a Bo
8790
05:25:52,600 --> 05:25:57,520
which we have exactly the same here
8791
05:25:54,440 --> 05:25:59,480
there you go Bo in. trass string s out
8792
05:25:57,520 --> 05:26:01,160
in result so that's how this function
8793
05:25:59,480 --> 05:26:03,120
has been declared of course they're
8794
05:26:01,160 --> 05:26:04,958
doing something inside the body that can
8795
05:26:03,120 --> 05:26:07,240
take in the string convert it to an
8796
05:26:04,958 --> 05:26:08,920
integer place it inside result and then
8797
05:26:07,240 --> 05:26:11,040
return whether or not the function has
8798
05:26:08,920 --> 05:26:13,080
been executed successfully so now that
8799
05:26:11,040 --> 05:26:14,520
we understand how to make an example and
8800
05:26:13,080 --> 05:26:15,958
how to make the trip pass function
8801
05:26:14,520 --> 05:26:17,760
ourselves and to give you the
8802
05:26:15,958 --> 05:26:20,400
Declaration let's have a look at a real
8803
05:26:17,760 --> 05:26:22,520
world example so if we had a list of
8804
05:26:20,400 --> 05:26:24,958
string and we had our shopping list that
8805
05:26:22,520 --> 05:26:27,120
was used from the examples we can have
8806
05:26:24,958 --> 05:26:28,760
list string and let's just create our
8807
05:26:27,120 --> 05:26:29,600
own initializer just with coffee and
8808
05:26:28,760 --> 05:26:31,760
milk
8809
05:26:29,600 --> 05:26:33,638
inside there we go so now that we've
8810
05:26:31,760 --> 05:26:35,080
created that we've got our shopping list
8811
05:26:33,638 --> 05:26:36,600
if you wanted to find something inside
8812
05:26:35,080 --> 05:26:38,840
the shopping list we have the function
8813
05:26:36,600 --> 05:26:40,958
index of and what that will do as it
8814
05:26:38,840 --> 05:26:43,200
says here searches for the specified
8815
05:26:40,958 --> 05:26:45,280
object and Returns the zero based index
8816
05:26:43,200 --> 05:26:47,638
of the first occurrence so if we were
8817
05:26:45,280 --> 05:26:50,400
search for coffee then we should get
8818
05:26:47,638 --> 05:26:51,638
return back zero so let's try and run
8819
05:26:50,400 --> 05:26:53,840
this and put it in the console right
8820
05:26:51,638 --> 05:26:56,320
line and see what the value is there we
8821
05:26:53,840 --> 05:26:57,920
go we get zero since it's the first item
8822
05:26:56,320 --> 05:27:01,600
and if we place milk inside here we
8823
05:26:57,920 --> 05:27:03,558
should get back one there we go so let's
8824
05:27:01,600 --> 05:27:06,240
try and rewrite this function into our
8825
05:27:03,558 --> 05:27:07,878
own code so all this will do is loop
8826
05:27:06,240 --> 05:27:10,080
around all of the positions until it
8827
05:27:07,878 --> 05:27:12,480
finds it and it'll find the first one
8828
05:27:10,080 --> 05:27:14,480
and it'll return us back the index so in
8829
05:27:12,480 --> 05:27:16,878
this case there's no out parameters but
8830
05:27:14,480 --> 05:27:19,200
there is a return value so we can also
8831
05:27:16,878 --> 05:27:20,958
include an out parameter and then
8832
05:27:19,200 --> 05:27:22,840
instead of assigning the value of minus
8833
05:27:20,958 --> 05:27:25,080
one we can actually assign the value of
8834
05:27:22,840 --> 05:27:26,878
true or false to dictate whether or not
8835
05:27:25,080 --> 05:27:28,520
that we found it so let's write the code
8836
05:27:26,878 --> 05:27:30,558
normally in here and then let's wrap it
8837
05:27:28,520 --> 05:27:32,718
around in a function so we can say int
8838
05:27:30,558 --> 05:27:35,280
index equal minus1 because we want it to
8839
05:27:32,718 --> 05:27:37,760
start a value that we know is incorrect
8840
05:27:35,280 --> 05:27:40,040
since the lists are from zero onwards
8841
05:27:37,760 --> 05:27:41,798
the value is only ever minus one then
8842
05:27:40,040 --> 05:27:43,760
that means it's not in the list then we
8843
05:27:41,798 --> 05:27:46,558
can then create a for Loop intire equal
8844
05:27:43,760 --> 05:27:48,240
Z shopping list do count because we want
8845
05:27:46,558 --> 05:27:50,320
to go through every single item inside
8846
05:27:48,240 --> 05:27:52,958
the shopping list and inside here we
8847
05:27:50,320 --> 05:27:54,638
want to do a lowercase comparison so we
8848
05:27:52,958 --> 05:27:56,958
can do if the shopping list square
8849
05:27:54,638 --> 05:27:59,600
bracket I do to
8850
05:27:56,958 --> 05:28:01,320
lower do equals and then we want to make
8851
05:27:59,600 --> 05:28:03,160
that equal to just something that we're
8852
05:28:01,320 --> 05:28:06,040
going to type in statically so we can
8853
05:28:03,160 --> 05:28:07,520
just say coffee we can make this into a
8854
05:28:06,040 --> 05:28:09,520
parameter after but this is just for
8855
05:28:07,520 --> 05:28:11,558
demonstration purposes so we have a
8856
05:28:09,520 --> 05:28:14,080
bracket and if we found that match then
8857
05:28:11,558 --> 05:28:15,558
we can say index equals to I cuz the
8858
05:28:14,080 --> 05:28:17,920
index of the position that we want to
8859
05:28:15,558 --> 05:28:19,400
find must be up to I cuz that's the one
8860
05:28:17,920 --> 05:28:22,200
that we've made a match with and we
8861
05:28:19,400 --> 05:28:24,080
don't need an else inside here we don't
8862
05:28:22,200 --> 05:28:25,958
need to set an else inside here to make
8863
05:28:24,080 --> 05:28:27,920
it equal to minus one because if it
8864
05:28:25,958 --> 05:28:29,680
simply doesn't find it then the index
8865
05:28:27,920 --> 05:28:31,680
variable won't change its value so it
8866
05:28:29,680 --> 05:28:33,400
will always stay minus one if it doesn't
8867
05:28:31,680 --> 05:28:36,080
find anything so after the followup is
8868
05:28:33,400 --> 05:28:39,240
run let's just print index out to the
8869
05:28:36,080 --> 05:28:41,878
screen there we go we have zero and if
8870
05:28:39,240 --> 05:28:44,040
we change this over to milk run it again
8871
05:28:41,878 --> 05:28:47,000
then we actually have one placed inside
8872
05:28:44,040 --> 05:28:49,320
here perfect so now what our function is
8873
05:28:47,000 --> 05:28:51,638
doing is giving us the values of the
8874
05:28:49,320 --> 05:28:53,000
index if it finds it now if you type in
8875
05:28:51,638 --> 05:28:55,240
something else if you just add another
8876
05:28:53,000 --> 05:28:57,760
can let's see what happens then we get
8877
05:28:55,240 --> 05:29:00,200
minus one and minus one means that it's
8878
05:28:57,760 --> 05:29:02,280
incorrect and it hasn't found it and
8879
05:29:00,200 --> 05:29:05,638
what we can do inside here we can say if
8880
05:29:02,280 --> 05:29:08,480
the index is greater than minus1 then we
8881
05:29:05,638 --> 05:29:10,520
can say found or else we can say not
8882
05:29:08,480 --> 05:29:13,120
found so let's run the code now and see
8883
05:29:10,520 --> 05:29:15,718
if we get a visual representation not
8884
05:29:13,120 --> 05:29:19,638
found and if we changes back to milk run
8885
05:29:15,718 --> 05:29:20,958
it again then we get found perfect so as
8886
05:29:19,638 --> 05:29:22,920
you can see here we're actually checking
8887
05:29:20,958 --> 05:29:24,878
if the value is greater than minus one
8888
05:29:22,920 --> 05:29:26,920
but it'd be easier if you had a variable
8889
05:29:24,878 --> 05:29:28,798
called found and it actually a booing
8890
05:29:26,920 --> 05:29:31,000
variable so we can make a boo found and
8891
05:29:28,798 --> 05:29:32,920
make that equal to an if statement
8892
05:29:31,000 --> 05:29:35,958
condition so we can say something like
8893
05:29:32,920 --> 05:29:38,400
index is greater than minus one which is
8894
05:29:35,958 --> 05:29:39,920
exactly what we had here a second ago
8895
05:29:38,400 --> 05:29:41,440
and because this is a condition it's
8896
05:29:39,920 --> 05:29:43,240
either going to return back true or
8897
05:29:41,440 --> 05:29:45,040
false and it's going to place the value
8898
05:29:43,240 --> 05:29:47,600
inside found and then we're going to use
8899
05:29:45,040 --> 05:29:50,200
found inside here this is easier logic
8900
05:29:47,600 --> 05:29:52,080
to understand if this is true then it's
8901
05:29:50,200 --> 05:29:56,040
found if this is not true then it's not
8902
05:29:52,080 --> 05:29:58,000
found true true false false it's quite
8903
05:29:56,040 --> 05:30:00,040
really straightforward and very easy and
8904
05:29:58,000 --> 05:30:02,160
clear you have an index and you have
8905
05:30:00,040 --> 05:30:03,760
whether or not it's been found so let's
8906
05:30:02,160 --> 05:30:06,280
try and wrap this around inside a
8907
05:30:03,760 --> 05:30:08,280
function so just as the trip pass does
8908
05:30:06,280 --> 05:30:10,200
it returns us back with a Bool which in
8909
05:30:08,280 --> 05:30:12,280
this case will be our found and it also
8910
05:30:10,200 --> 05:30:14,798
gives us back an integer result but in
8911
05:30:12,280 --> 05:30:16,120
this case it's going to be our index so
8912
05:30:14,798 --> 05:30:17,878
let's see what we can do to write this
8913
05:30:16,120 --> 05:30:20,878
so we can have a static we want to
8914
05:30:17,878 --> 05:30:23,558
return back the Bo and we can say find
8915
05:30:20,878 --> 05:30:25,520
in list we need to give it the item
8916
05:30:23,558 --> 05:30:27,958
we're going to find so we're going to
8917
05:30:25,520 --> 05:30:29,480
need a string s we need a string s for
8918
05:30:27,958 --> 05:30:31,360
the item that we're going to find we
8919
05:30:29,480 --> 05:30:34,320
need to give it the list as well and
8920
05:30:31,360 --> 05:30:37,080
then finally we need our out in
8921
05:30:34,320 --> 05:30:39,920
index there we go so let's comment out
8922
05:30:37,080 --> 05:30:39,920
this code here
8923
05:30:40,280 --> 05:30:44,958
and let's copy this down into our
8924
05:30:42,120 --> 05:30:46,760
function and see what we need to do so
8925
05:30:44,958 --> 05:30:48,000
index has the value of minus one we
8926
05:30:46,760 --> 05:30:49,878
don't need the integer variable
8927
05:30:48,000 --> 05:30:51,760
declaration here because we've already
8928
05:30:49,878 --> 05:30:53,638
declared it inside the parameter so
8929
05:30:51,760 --> 05:30:55,798
after declaring inside here we give it
8930
05:30:53,638 --> 05:30:57,600
the value of minus one minus one
8931
05:30:55,798 --> 05:31:00,280
indicates it hasn't been found since the
8932
05:30:57,600 --> 05:31:03,080
index are zero based so instead of
8933
05:31:00,280 --> 05:31:05,520
shopping list we now just have list and
8934
05:31:03,080 --> 05:31:07,320
list here as well and then instead of
8935
05:31:05,520 --> 05:31:09,400
having it being equal to milk we
8936
05:31:07,320 --> 05:31:10,840
actually want to use our s variable and
8937
05:31:09,400 --> 05:31:12,400
to make it better we can convert it to
8938
05:31:10,840 --> 05:31:15,120
lower case just so we have lowercase
8939
05:31:12,400 --> 05:31:16,840
conversions on both sides of the equals
8940
05:31:15,120 --> 05:31:18,840
if we find a match between the list that
8941
05:31:16,840 --> 05:31:20,760
we've passed in and the S variable which
8942
05:31:18,840 --> 05:31:23,920
is our surge term that we've passed in
8943
05:31:20,760 --> 05:31:25,320
then we assign I to the value of index
8944
05:31:23,920 --> 05:31:27,200
and then it's still got a red underline
8945
05:31:25,320 --> 05:31:29,400
because we haven't returned back
8946
05:31:27,200 --> 05:31:30,958
bu so right now we actually want to
8947
05:31:29,400 --> 05:31:32,958
return back the found variable so we
8948
05:31:30,958 --> 05:31:35,360
have two options here we can return back
8949
05:31:32,958 --> 05:31:37,080
found or we can simply take this and
8950
05:31:35,360 --> 05:31:38,798
return it back in here we don't need to
8951
05:31:37,080 --> 05:31:40,558
run this code to see if it's found or
8952
05:31:38,798 --> 05:31:42,120
not and then store it and then return it
8953
05:31:40,558 --> 05:31:43,480
after since we're not doing anything
8954
05:31:42,120 --> 05:31:44,920
else with it we could just return it
8955
05:31:43,480 --> 05:31:46,718
like this this makes the code
8956
05:31:44,920 --> 05:31:49,080
considerably shorter because this will
8957
05:31:46,718 --> 05:31:50,958
just return back a true or false if the
8958
05:31:49,080 --> 05:31:52,878
index is greater than minus1 then it
8959
05:31:50,958 --> 05:31:55,120
will turn back true if the index is less
8960
05:31:52,878 --> 05:31:56,320
than minus1 then it will turn back false
8961
05:31:55,120 --> 05:31:58,440
so let's see how we can use this
8962
05:31:56,320 --> 05:32:00,200
function inside our code now so we
8963
05:31:58,440 --> 05:32:01,760
wanted to search for milk so let's just
8964
05:32:00,200 --> 05:32:03,200
copy and paste this line and let's see
8965
05:32:01,760 --> 05:32:05,878
if we get the same output as the
8966
05:32:03,200 --> 05:32:08,200
shopping list index of so now our
8967
05:32:05,878 --> 05:32:09,958
function is called finding list you have
8968
05:32:08,200 --> 05:32:12,360
a bracket and then our string s is going
8969
05:32:09,958 --> 05:32:15,600
to be our milk our list is our shopping
8970
05:32:12,360 --> 05:32:18,280
list and our out in
8971
05:32:15,600 --> 05:32:19,680
index so this is going to return us back
8972
05:32:18,280 --> 05:32:21,680
with a booing so we can print that
8973
05:32:19,680 --> 05:32:23,480
booing to the screen and then if we just
8974
05:32:21,680 --> 05:32:25,480
duplicate that down and we can print
8975
05:32:23,480 --> 05:32:27,718
index to the screen and maybe using a
8976
05:32:25,480 --> 05:32:29,160
console right line and just place a
8977
05:32:27,718 --> 05:32:31,240
space right at the
8978
05:32:29,160 --> 05:32:33,040
start so you print out whether it's
8979
05:32:31,240 --> 05:32:35,840
successfully converted or not and we get
8980
05:32:33,040 --> 05:32:35,840
the value back from
8981
05:32:35,920 --> 05:32:42,440
it let's run the code
8982
05:32:39,080 --> 05:32:43,958
so we get true plus the value of one so
8983
05:32:42,440 --> 05:32:46,000
that means it's found it and it's found
8984
05:32:43,958 --> 05:32:47,878
it at one index and the previous line
8985
05:32:46,000 --> 05:32:50,040
before it which just run the normal
8986
05:32:47,878 --> 05:32:51,840
index of function also return back one
8987
05:32:50,040 --> 05:32:54,240
so they're both the same let's just get
8988
05:32:51,840 --> 05:32:54,240
rid of that
8989
05:32:56,000 --> 05:33:01,280
space so if we change these both to
8990
05:32:59,200 --> 05:33:03,558
coffee then hopefully we should see the
8991
05:33:01,280 --> 05:33:05,520
same result but with index zero there we
8992
05:33:03,558 --> 05:33:07,760
go this zero comes from the index
8993
05:33:05,520 --> 05:33:09,600
function this true is from our function
8994
05:33:07,760 --> 05:33:11,840
tell us has been successful and this
8995
05:33:09,600 --> 05:33:13,558
zero is the index so we can actually
8996
05:33:11,840 --> 05:33:14,320
take this and put this straight into an
8997
05:33:13,558 --> 05:33:16,798
if
8998
05:33:14,320 --> 05:33:18,840
statement so just like we had down here
8999
05:33:16,798 --> 05:33:21,760
with the found and not found we can say
9000
05:33:18,840 --> 05:33:24,120
if and then this function call and then
9001
05:33:21,760 --> 05:33:25,760
we can say found and then the item what
9002
05:33:24,120 --> 05:33:27,638
we passed in so in this case it's going
9003
05:33:25,760 --> 05:33:31,440
to be coffee found
9004
05:33:27,638 --> 05:33:33,000
coffee found coffee at index index and
9005
05:33:31,440 --> 05:33:35,280
if it hasn't been found then we can just
9006
05:33:33,000 --> 05:33:37,280
print not
9007
05:33:35,280 --> 05:33:39,200
found and this is getting an error
9008
05:33:37,280 --> 05:33:41,360
because we have two indexes up here so
9009
05:33:39,200 --> 05:33:42,320
let's comment out this line and let's
9010
05:33:41,360 --> 05:33:45,080
try running
9011
05:33:42,320 --> 05:33:47,320
out found coffee index zero and you can
9012
05:33:45,080 --> 05:33:48,878
see these two are the same again one
9013
05:33:47,320 --> 05:33:52,120
comes from here and one comes from that
9014
05:33:48,878 --> 05:33:55,798
index we change these both to milk now
9015
05:33:52,120 --> 05:33:58,040
milk milk and milk then run the code and
9016
05:33:55,798 --> 05:34:00,280
you see found milk index one if you
9017
05:33:58,040 --> 05:34:02,400
wanted to just before you pass in here
9018
05:34:00,280 --> 05:34:06,718
you can ask the
9019
05:34:02,400 --> 05:34:09,878
user enter an item to
9020
05:34:06,718 --> 05:34:12,120
search store it inside string search
9021
05:34:09,878 --> 05:34:14,400
equals console.
9022
05:34:12,120 --> 05:34:16,718
readline and then instead of passing mil
9023
05:34:14,400 --> 05:34:18,280
here we can just pass in Search and then
9024
05:34:16,718 --> 05:34:20,080
instead of passing milk in here we can
9025
05:34:18,280 --> 05:34:22,080
also use the search variable so now if
9026
05:34:20,080 --> 05:34:24,400
we try run it again we can type in milk
9027
05:34:22,080 --> 05:34:26,958
and then we go file milk index one and
9028
05:34:24,400 --> 05:34:28,760
then coffee found coffee index zero and
9029
05:34:26,958 --> 05:34:31,760
just to give this a test let's just try
9030
05:34:28,760 --> 05:34:33,680
and find m not
9031
05:34:31,760 --> 05:34:35,920
found
9032
05:34:33,680 --> 05:34:37,718
perfect so let's have a little recap
9033
05:34:35,920 --> 05:34:39,718
based on this function so initially we
9034
05:34:37,718 --> 05:34:41,558
try to assign the value of zero and then
9035
05:34:39,718 --> 05:34:45,160
print out a normal function and inside
9036
05:34:41,558 --> 05:34:46,958
test we just had a simple num equals 5
9037
05:34:45,160 --> 05:34:48,760
and what this was doing inside is just
9038
05:34:46,958 --> 05:34:50,480
locally assigning it but not actually
9039
05:34:48,760 --> 05:34:52,520
return anything and then we said we
9040
05:34:50,480 --> 05:34:53,840
could assign it and then return it back
9041
05:34:52,520 --> 05:34:55,760
and that was another option of being
9042
05:34:53,840 --> 05:34:57,200
able to assign the value to five and
9043
05:34:55,760 --> 05:34:59,120
then we use the out parameter which
9044
05:34:57,200 --> 05:35:01,000
allowed you to pass in an integer when
9045
05:34:59,120 --> 05:35:03,120
you assign in here it will pass it back
9046
05:35:01,000 --> 05:35:05,240
into the original call and place it in
9047
05:35:03,120 --> 05:35:07,160
this variable here so when we print out
9048
05:35:05,240 --> 05:35:09,240
on the next line it says it's equal to
9049
05:35:07,160 --> 05:35:11,280
five we wrote our own little shopping
9050
05:35:09,240 --> 05:35:13,240
list for the list of strings and then we
9051
05:35:11,280 --> 05:35:15,320
called the shopping list function index
9052
05:35:13,240 --> 05:35:18,000
of and then we actually created our own
9053
05:35:15,320 --> 05:35:20,280
index of function and made it into a
9054
05:35:18,000 --> 05:35:22,480
function so just a little recap what's
9055
05:35:20,280 --> 05:35:24,558
going on here we have a string s which
9056
05:35:22,480 --> 05:35:26,080
is our search term the list full of the
9057
05:35:24,558 --> 05:35:27,680
items in the index that we're going to
9058
05:35:26,080 --> 05:35:29,558
pass back to the user as well as
9059
05:35:27,680 --> 05:35:32,520
returning back a bu to indicate whether
9060
05:35:29,558 --> 05:35:34,480
we found it or not we set the index to
9061
05:35:32,520 --> 05:35:37,080
minus one so then that completes our
9062
05:35:34,480 --> 05:35:38,638
logic at the bottom minus one means it
9063
05:35:37,080 --> 05:35:41,878
hasn't been found found since the list
9064
05:35:38,638 --> 05:35:44,240
is all zero based if this value is
9065
05:35:41,878 --> 05:35:46,280
greater than minus1 then that means that
9066
05:35:44,240 --> 05:35:48,760
we found it inside the list since the
9067
05:35:46,280 --> 05:35:50,600
list is zero based and if it's not that
9068
05:35:48,760 --> 05:35:52,600
and this returns back false then we
9069
05:35:50,600 --> 05:35:54,520
mustn't have found it because this line
9070
05:35:52,600 --> 05:35:57,000
would have executed so we Loop through
9071
05:35:54,520 --> 05:35:58,920
the list we do a lowercase comparison on
9072
05:35:57,000 --> 05:36:01,638
both sides of the equation on the list
9073
05:35:58,920 --> 05:36:04,120
item and on the surge term if they both
9074
05:36:01,638 --> 05:36:06,120
match we run the index and store the I
9075
05:36:04,120 --> 05:36:07,638
variable inside the index and then we
9076
05:36:06,120 --> 05:36:09,680
return whether index is greater than
9077
05:36:07,638 --> 05:36:11,360
minus one so after we return this back
9078
05:36:09,680 --> 05:36:13,040
from the end of the function we can see
9079
05:36:11,360 --> 05:36:14,760
once we run it inside the console right
9080
05:36:13,040 --> 05:36:16,840
Line This is just going to return us
9081
05:36:14,760 --> 05:36:18,878
back with a true or false value as well
9082
05:36:16,840 --> 05:36:20,920
as creating our new integer variable
9083
05:36:18,878 --> 05:36:22,840
called index so on the next line we can
9084
05:36:20,920 --> 05:36:24,600
use index and print out of the screen
9085
05:36:22,840 --> 05:36:26,680
this line should return true or false
9086
05:36:24,600 --> 05:36:28,480
since it's a Boolean return and this
9087
05:36:26,680 --> 05:36:30,480
variable will either give us minus one
9088
05:36:28,480 --> 05:36:31,920
or something above minus one but in this
9089
05:36:30,480 --> 05:36:33,958
case when we actually make use of the
9090
05:36:31,920 --> 05:36:36,280
function we can ask a user to enter
9091
05:36:33,958 --> 05:36:38,000
something get them to type it in and
9092
05:36:36,280 --> 05:36:40,360
pass that into the function but more
9093
05:36:38,000 --> 05:36:42,920
importantly when this function returns
9094
05:36:40,360 --> 05:36:45,440
if it's true we output it's been found
9095
05:36:42,920 --> 05:36:47,160
and if it's false we output not found
9096
05:36:45,440 --> 05:36:49,958
and we only make use of this integer
9097
05:36:47,160 --> 05:36:52,280
variable should this return back true
9098
05:36:49,958 --> 05:36:53,920
there is no point in printing minus one
9099
05:36:52,280 --> 05:36:55,600
to this screen because that's what
9100
05:36:53,920 --> 05:36:57,760
you're going to get if it's not
9101
05:36:55,600 --> 05:36:59,440
found it's going to look something like
9102
05:36:57,760 --> 05:37:01,320
this which is kind of redundant because
9103
05:36:59,440 --> 05:37:02,480
the user doesn't care about this so we
9104
05:37:01,320 --> 05:37:04,480
don't need to include it when we're
9105
05:37:02,480 --> 05:37:06,240
printing not found but we should include
9106
05:37:04,480 --> 05:37:07,798
it when we're printing out the found of
9107
05:37:06,240 --> 05:37:09,120
course this is just a random example Le
9108
05:37:07,798 --> 05:37:10,760
and you might not need to print it out
9109
05:37:09,120 --> 05:37:12,798
to the screen or you might just do
9110
05:37:10,760 --> 05:37:14,200
something else inside here you might be
9111
05:37:12,798 --> 05:37:15,958
searching for something and then you
9112
05:37:14,200 --> 05:37:17,878
might want to edit it or do something
9113
05:37:15,958 --> 05:37:19,760
else with it and then you can make use
9114
05:37:17,878 --> 05:37:21,840
of that index if you wanted to ask the
9115
05:37:19,760 --> 05:37:23,320
user to search for an item and then tell
9116
05:37:21,840 --> 05:37:25,120
them what they want to replace it with
9117
05:37:23,320 --> 05:37:27,878
then you can also do that just as we've
9118
05:37:25,120 --> 05:37:27,878
covered in the last
9119
05:37:28,360 --> 05:37:32,520
examples so after we've covered out
9120
05:37:30,520 --> 05:37:34,200
parameters and normal parameters let's
9121
05:37:32,520 --> 05:37:36,760
have a look at a concept called passing
9122
05:37:34,200 --> 05:37:38,680
by reference so we created inome equals
9123
05:37:36,760 --> 05:37:41,000
10 and then let's call it assign and we
9124
05:37:38,680 --> 05:37:45,440
passed it straight into here and we can
9125
05:37:41,000 --> 05:37:48,798
have a static void assign in
9126
05:37:45,440 --> 05:37:50,680
N if we say n equals 20 if we use a
9127
05:37:48,798 --> 05:37:53,120
console right line up here then num is
9128
05:37:50,680 --> 05:37:54,440
still going to be 10 because we're not
9129
05:37:53,120 --> 05:37:57,080
returning it and we're not really
9130
05:37:54,440 --> 05:37:59,958
affecting it now other ways we can do
9131
05:37:57,080 --> 05:38:02,400
this is by changing this into an INT and
9132
05:37:59,958 --> 05:38:04,480
then we can say return 20 and then we
9133
05:38:02,400 --> 05:38:05,638
don't really need this anymore so we
9134
05:38:04,480 --> 05:38:08,520
could just say
9135
05:38:05,638 --> 05:38:11,638
num num equals a sign and then we'll get
9136
05:38:08,520 --> 05:38:14,600
20 assign back in there we go all we can
9137
05:38:11,638 --> 05:38:17,520
actually make this an out into num and
9138
05:38:14,600 --> 05:38:20,440
then we can say num equals to
9139
05:38:17,520 --> 05:38:22,520
20 and then inside here we can say out
9140
05:38:20,440 --> 05:38:24,320
num we don't need to return back in
9141
05:38:22,520 --> 05:38:26,400
anymore so we can make this valued and
9142
05:38:24,320 --> 05:38:27,680
you see the value 20 so there are
9143
05:38:26,400 --> 05:38:29,638
different ways that we can assign a
9144
05:38:27,680 --> 05:38:31,320
variable straight from here so let's
9145
05:38:29,638 --> 05:38:33,320
have a look at another method so instead
9146
05:38:31,320 --> 05:38:35,638
of having out we can actually use a new
9147
05:38:33,320 --> 05:38:37,120
keyw called ref and the reason why it's
9148
05:38:35,638 --> 05:38:38,840
ref it means it's passing it by
9149
05:38:37,120 --> 05:38:40,320
reference so when you pass a value in
9150
05:38:38,840 --> 05:38:42,440
through a parameter normally you're
9151
05:38:40,320 --> 05:38:44,878
actually passing by value and what that
9152
05:38:42,440 --> 05:38:47,200
means is you're copying this value that
9153
05:38:44,878 --> 05:38:48,840
gets passed inside the brackets into
9154
05:38:47,200 --> 05:38:51,200
here and it's making a whole new
9155
05:38:48,840 --> 05:38:53,440
variable what I mean by that is this
9156
05:38:51,200 --> 05:38:55,040
might have memory dress location one and
9157
05:38:53,440 --> 05:38:57,320
then when this variable gets created it
9158
05:38:55,040 --> 05:38:59,160
has memory dress location 2 for example
9159
05:38:57,320 --> 05:39:01,360
and then it's used within here and then
9160
05:38:59,160 --> 05:39:02,798
two is now deleted because you've got
9161
05:39:01,360 --> 05:39:05,000
rid of the variable because of this
9162
05:39:02,798 --> 05:39:06,840
function but one is still alive since in
9163
05:39:05,000 --> 05:39:08,200
the main function when you pass by
9164
05:39:06,840 --> 05:39:10,000
reference what you're doing is you're
9165
05:39:08,200 --> 05:39:11,638
saying give me the memory address
9166
05:39:10,000 --> 05:39:14,120
location of the variable inside the
9167
05:39:11,638 --> 05:39:15,958
brackets and give it into this function
9168
05:39:14,120 --> 05:39:17,920
rather than copying the value so if this
9169
05:39:15,958 --> 05:39:19,520
has memory just location one and you
9170
05:39:17,920 --> 05:39:21,320
pass it into here then it's also going
9171
05:39:19,520 --> 05:39:23,360
to have one so what this actually means
9172
05:39:21,320 --> 05:39:25,200
is if we put this ref keyword just
9173
05:39:23,360 --> 05:39:27,558
before the int just like we do with the
9174
05:39:25,200 --> 05:39:29,600
out and we can place it here as well
9175
05:39:27,558 --> 05:39:31,840
we're now saying we want to pass this by
9176
05:39:29,600 --> 05:39:34,718
reference so when this 10 gets pass in
9177
05:39:31,840 --> 05:39:37,520
here we actually have a link if you will
9178
05:39:34,718 --> 05:39:39,680
to this gome is now linked to this gome
9179
05:39:37,520 --> 05:39:41,878
which means if we simply do nor equals
9180
05:39:39,680 --> 05:39:44,200
20 and run the code then we're going to
9181
05:39:41,878 --> 05:39:46,478
see 20 the main differences between
9182
05:39:44,200 --> 05:39:48,600
reference and using the out keyword is
9183
05:39:46,478 --> 05:39:50,320
when you're using out within a function
9184
05:39:48,600 --> 05:39:52,558
you actually need to make use of the
9185
05:39:50,320 --> 05:39:54,360
value if I don't include anything within
9186
05:39:52,558 --> 05:39:56,798
here then I'm going to get exception and
9187
05:39:54,360 --> 05:39:59,320
it says the out parameter num must be
9188
05:39:56,798 --> 05:40:02,000
assigned so when using the out keyword
9189
05:39:59,320 --> 05:40:03,878
num has to be assigned when using the
9190
05:40:02,000 --> 05:40:05,638
ref keyword you don't have to make any
9191
05:40:03,878 --> 05:40:07,600
assignment because we're just passing a
9192
05:40:05,638 --> 05:40:09,520
reference in with the out keyword word
9193
05:40:07,600 --> 05:40:11,638
you literally tell the function I want
9194
05:40:09,520 --> 05:40:13,478
to Output this value but if you haven't
9195
05:40:11,638 --> 05:40:15,360
gave it a value to Output then of course
9196
05:40:13,478 --> 05:40:17,120
it's going to be an error so we can
9197
05:40:15,360 --> 05:40:19,120
assign in the reference if we really
9198
05:40:17,120 --> 05:40:20,840
want to but you don't have to and it's
9199
05:40:19,120 --> 05:40:22,920
not required so there are the two main
9200
05:40:20,840 --> 05:40:25,360
differences between passing by reference
9201
05:40:22,920 --> 05:40:27,400
and passing by value and also using the
9202
05:40:25,360 --> 05:40:28,718
out parameter passing by reference is
9203
05:40:27,400 --> 05:40:30,638
technically quicker because of
9204
05:40:28,718 --> 05:40:32,638
efficiency when you don't pass it by
9205
05:40:30,638 --> 05:40:34,638
reference you pass it by value which
9206
05:40:32,638 --> 05:40:35,760
means it'll essentially place that 10
9207
05:40:34,638 --> 05:40:38,040
inside these
9208
05:40:35,760 --> 05:40:40,240
brackets instead of passing the variable
9209
05:40:38,040 --> 05:40:42,080
in it actually just passes the value and
9210
05:40:40,240 --> 05:40:44,360
then it makes a whole new variable down
9211
05:40:42,080 --> 05:40:46,240
here when you pass it in by reference
9212
05:40:44,360 --> 05:40:48,600
you're essentially actually giving it a
9213
05:40:46,240 --> 05:40:50,638
link to this memory address location
9214
05:40:48,600 --> 05:40:52,320
from inside this function which means
9215
05:40:50,638 --> 05:40:54,638
that you don't actually have another
9216
05:40:52,320 --> 05:40:57,320
local variable inside here because this
9217
05:40:54,638 --> 05:40:58,600
one is affecting this one directly you
9218
05:40:57,320 --> 05:41:00,440
can do this with any amount of
9219
05:40:58,600 --> 05:41:02,920
references you want by simply just
9220
05:41:00,440 --> 05:41:04,718
chaining them and adding another one in
9221
05:41:02,920 --> 05:41:07,478
like that for example and then we can
9222
05:41:04,718 --> 05:41:10,718
have a string name and then we can pass
9223
05:41:07,478 --> 05:41:13,478
our ref name inside here and inside here
9224
05:41:10,718 --> 05:41:15,400
we can say name equals hour and this
9225
05:41:13,478 --> 05:41:17,840
doesn't like it if it's unassigned so we
9226
05:41:15,400 --> 05:41:19,200
just give it empty quotes or we can sign
9227
05:41:17,840 --> 05:41:21,360
it another name it doesn't really matter
9228
05:41:19,200 --> 05:41:23,520
it's just a demonstration and if you run
9229
05:41:21,360 --> 05:41:24,878
that but let's just print out the name
9230
05:41:23,520 --> 05:41:27,240
as well to the
9231
05:41:24,878 --> 05:41:28,280
screen there we go we get abber assigned
9232
05:41:27,240 --> 05:41:30,798
into
9233
05:41:28,280 --> 05:41:32,600
there and same thing with this variable
9234
05:41:30,798 --> 05:41:34,840
is we're actually just passing in the
9235
05:41:32,600 --> 05:41:36,680
variable as a reference rather than as a
9236
05:41:34,840 --> 05:41:38,280
value it doesn't matter what the value
9237
05:41:36,680 --> 05:41:40,440
is going to be B cuz it's just given
9238
05:41:38,280 --> 05:41:42,440
this variable inside this function so
9239
05:41:40,440 --> 05:41:44,040
we're actually saying name equals we
9240
05:41:42,440 --> 05:41:45,680
basically just assign it back into the
9241
05:41:44,040 --> 05:41:47,600
original variable so let's just have a
9242
05:41:45,680 --> 05:41:49,600
look at another example so we can have
9243
05:41:47,600 --> 05:41:51,320
some sort of like change name function
9244
05:41:49,600 --> 05:41:53,160
and we can give it the name variable as
9245
05:41:51,320 --> 05:41:54,440
a reference and then we can give it the
9246
05:41:53,160 --> 05:41:56,320
new name that we're going to pass in in
9247
05:41:54,440 --> 05:41:58,680
a second so if we make a new function
9248
05:41:56,320 --> 05:42:01,760
static void change name you're going to
9249
05:41:58,680 --> 05:42:04,638
need a ref string name and a string new
9250
05:42:01,760 --> 05:42:05,878
name and we can simply say in here name
9251
05:42:04,638 --> 05:42:07,638
equals new
9252
05:42:05,878 --> 05:42:09,440
name perfect
9253
05:42:07,638 --> 05:42:11,520
so let's give this a shot so if we just
9254
05:42:09,440 --> 05:42:14,958
comment out this little section and we
9255
05:42:11,520 --> 05:42:17,958
can say console right enter your new
9256
05:42:14,958 --> 05:42:20,320
name then we have a string new name
9257
05:42:17,958 --> 05:42:22,478
equals console.
9258
05:42:20,320 --> 05:42:23,958
readline and then just up here as we've
9259
05:42:22,478 --> 05:42:26,120
done before we could just declare the
9260
05:42:23,958 --> 05:42:28,200
name as being Joe and then we enter the
9261
05:42:26,120 --> 05:42:32,360
new name call this function and then
9262
05:42:28,200 --> 05:42:34,160
after we can say your new name is and
9263
05:42:32,360 --> 05:42:35,280
then we can pass in name perfect let's
9264
05:42:34,160 --> 05:42:37,440
run
9265
05:42:35,280 --> 05:42:39,638
that so at this point the name name is
9266
05:42:37,440 --> 05:42:40,958
actually Joe cuz we've made it appear
9267
05:42:39,638 --> 05:42:43,000
and then we'll enter a new name so we
9268
05:42:40,958 --> 05:42:45,798
can type in abber now it says your new
9269
05:42:43,000 --> 05:42:47,040
name is abber perfect so all this
9270
05:42:45,798 --> 05:42:48,400
function is doing I know it's pretty
9271
05:42:47,040 --> 05:42:51,080
pointless right now cuz it's just doing
9272
05:42:48,400 --> 05:42:52,958
one line but all it's doing is assigning
9273
05:42:51,080 --> 05:42:54,760
the reference to the variable that we
9274
05:42:52,958 --> 05:42:56,440
pass in so what we can do now is make
9275
05:42:54,760 --> 05:42:58,718
sure that we can actually assign this
9276
05:42:56,440 --> 05:43:00,440
variable in and we can also pass in a
9277
05:42:58,718 --> 05:43:02,160
Boolean variable so what we should do
9278
05:43:00,440 --> 05:43:03,798
now is actually make sure that we can do
9279
05:43:02,160 --> 05:43:05,920
the assignment properly cuz you don't
9280
05:43:03,798 --> 05:43:08,120
want to assign it if the value is empty
9281
05:43:05,920 --> 05:43:09,718
and then return back a Bo Lan type just
9282
05:43:08,120 --> 05:43:11,520
to indicate whether or not it's being
9283
05:43:09,718 --> 05:43:13,478
assigned so what we can do in here is
9284
05:43:11,520 --> 05:43:15,600
just on the change name we can wrap this
9285
05:43:13,478 --> 05:43:17,360
around an if statement and if this
9286
05:43:15,600 --> 05:43:20,280
returns back true then your name has
9287
05:43:17,360 --> 05:43:22,878
been changed and if not we can say
9288
05:43:20,280 --> 05:43:24,520
console right Line new name cannot be
9289
05:43:22,878 --> 05:43:27,440
empty or
9290
05:43:24,520 --> 05:43:30,478
no in order to be able to do this check
9291
05:43:27,440 --> 05:43:32,120
as we've used before we could say if new
9292
05:43:30,478 --> 05:43:34,558
name and then just around that we can
9293
05:43:32,120 --> 05:43:36,080
say string do is n or empty and wrap
9294
05:43:34,558 --> 05:43:38,600
that inside the brackets and we want to
9295
05:43:36,080 --> 05:43:40,400
say if it's not and the L empty then we
9296
05:43:38,600 --> 05:43:42,080
want to do our
9297
05:43:40,400 --> 05:43:43,520
assignment after we've done our
9298
05:43:42,080 --> 05:43:46,200
assignment we can return back true
9299
05:43:43,520 --> 05:43:48,240
inside here and then just outside we can
9300
05:43:46,200 --> 05:43:49,600
return back false notice how in this
9301
05:43:48,240 --> 05:43:52,638
little bit of code we actually don't
9302
05:43:49,600 --> 05:43:55,280
have an else we don't need to write else
9303
05:43:52,638 --> 05:43:57,558
inside here and place return false
9304
05:43:55,280 --> 05:43:59,600
inside here because if this returns back
9305
05:43:57,558 --> 05:44:02,160
true then it's going to execute this one
9306
05:43:59,600 --> 05:44:03,798
line return back true and since it's a
9307
05:44:02,160 --> 05:44:05,600
return it's going to jump to the end of
9308
05:44:03,798 --> 05:44:07,440
the function so we don't need the else
9309
05:44:05,600 --> 05:44:09,920
in there because the only way that this
9310
05:44:07,440 --> 05:44:12,400
line will ever execute is if we actually
9311
05:44:09,920 --> 05:44:13,958
had this function returning back false
9312
05:44:12,400 --> 05:44:16,080
so if this returns back true it's going
9313
05:44:13,958 --> 05:44:18,120
to hit this line hit the return jump to
9314
05:44:16,080 --> 05:44:19,680
the end if this returns back false it's
9315
05:44:18,120 --> 05:44:21,520
going to skip over the if statement and
9316
05:44:19,680 --> 05:44:22,878
then just return back false to the user
9317
05:44:21,520 --> 05:44:25,558
perfect so now we've got no compile
9318
05:44:22,878 --> 05:44:27,160
errors let's give it a run so if we want
9319
05:44:25,558 --> 05:44:29,160
to change your name to nothing if we
9320
05:44:27,160 --> 05:44:31,680
just press enter then we can see it says
9321
05:44:29,160 --> 05:44:33,520
new name cannot be empty or no and if we
9322
05:44:31,680 --> 05:44:35,798
type something in then we get our name
9323
05:44:33,520 --> 05:44:37,240
changed so just to round up this video
9324
05:44:35,798 --> 05:44:38,798
let's talk about the main differences
9325
05:44:37,240 --> 05:44:40,840
between the ref parameter and the out
9326
05:44:38,798 --> 05:44:42,360
parameter when you sign in by reference
9327
05:44:40,840 --> 05:44:44,360
you actually have to make the variable
9328
05:44:42,360 --> 05:44:46,638
before you pass it in for example you
9329
05:44:44,360 --> 05:44:48,718
can't just write ref string in here and
9330
05:44:46,638 --> 05:44:50,280
that actually works whereas you can't
9331
05:44:48,718 --> 05:44:53,320
actually do that with the in let me
9332
05:44:50,280 --> 05:44:55,120
demonstrate so if you had the in. trass
9333
05:44:53,320 --> 05:44:58,558
and that has an out parameter you can
9334
05:44:55,120 --> 05:45:00,558
actually type out int index inside here
9335
05:44:58,558 --> 05:45:03,360
you don't actually have to make the int
9336
05:45:00,558 --> 05:45:05,638
index inside here and then specify it
9337
05:45:03,360 --> 05:45:07,320
outside you could just initialize and
9338
05:45:05,638 --> 05:45:09,200
declare the variable straight inside the
9339
05:45:07,320 --> 05:45:11,120
brackets whereas because you're passing
9340
05:45:09,200 --> 05:45:12,840
a reference with the string you actually
9341
05:45:11,120 --> 05:45:14,600
need to make the string first and then
9342
05:45:12,840 --> 05:45:16,680
pass it in so that's a big difference
9343
05:45:14,600 --> 05:45:18,440
between the out and the ref when you
9344
05:45:16,680 --> 05:45:20,320
make news of the actually inside the
9345
05:45:18,440 --> 05:45:22,280
function if you're using a reference
9346
05:45:20,320 --> 05:45:24,240
then you never have to assign it or at
9347
05:45:22,280 --> 05:45:26,400
least it's not required you could have a
9348
05:45:24,240 --> 05:45:27,680
series of if statements inside this body
9349
05:45:26,400 --> 05:45:29,280
and then you might determine that you
9350
05:45:27,680 --> 05:45:31,200
might not need to assign it which is
9351
05:45:29,280 --> 05:45:33,280
exactly what we've got here the only way
9352
05:45:31,200 --> 05:45:35,400
we assign name is if this returns back
9353
05:45:33,280 --> 05:45:37,240
true if it doesn't return back true then
9354
05:45:35,400 --> 05:45:39,240
we simply don't assign it and move to
9355
05:45:37,240 --> 05:45:41,400
here this is a problem if the out
9356
05:45:39,240 --> 05:45:43,000
variable if we change this to an out
9357
05:45:41,400 --> 05:45:45,040
then we're going to get a compiler error
9358
05:45:43,000 --> 05:45:46,440
because it doesn't return back a value
9359
05:45:45,040 --> 05:45:48,878
so we'd have to write something like
9360
05:45:46,440 --> 05:45:50,520
this at the top name equals something
9361
05:45:48,878 --> 05:45:52,798
and then potentially sign it here and
9362
05:45:50,520 --> 05:45:54,440
then return back false so with reference
9363
05:45:52,798 --> 05:45:55,958
you don't have to worry about the fact
9364
05:45:54,440 --> 05:45:58,120
that you might not assign in every
9365
05:45:55,958 --> 05:45:59,680
single path of your code and finally the
9366
05:45:58,120 --> 05:46:02,000
main difference is the fact that out
9367
05:45:59,680 --> 05:46:04,040
parameters are still used by value and
9368
05:46:02,000 --> 05:46:05,520
this is by reference reference will be
9369
05:46:04,040 --> 05:46:07,600
slightly quicker since you'll get the
9370
05:46:05,520 --> 05:46:09,680
memory just location being passed in
9371
05:46:07,600 --> 05:46:12,600
instead of the Val we won't have two
9372
05:46:09,680 --> 05:46:12,600
copies of the same
9373
05:46:12,840 --> 05:46:16,718
variable the focus of this task is to
9374
05:46:15,240 --> 05:46:18,360
create a function that calculates the
9375
05:46:16,718 --> 05:46:20,040
area of a triangle so we're going to ask
9376
05:46:18,360 --> 05:46:21,840
the user for width and height install
9377
05:46:20,040 --> 05:46:23,558
them both create a function to calculate
9378
05:46:21,840 --> 05:46:26,200
the area the function is going to
9379
05:46:23,558 --> 05:46:28,000
calculate it using width times by height
9380
05:46:26,200 --> 05:46:29,878
taking that calculation and dividing the
9381
05:46:28,000 --> 05:46:31,200
value by two call it in the main and
9382
05:46:29,878 --> 05:46:33,000
print out the area of the triangle to
9383
05:46:31,200 --> 05:46:35,160
the screen so let's start with entering
9384
05:46:33,000 --> 05:46:37,440
the width and the height so we can say
9385
05:46:35,160 --> 05:46:42,200
enter width colon
9386
05:46:37,440 --> 05:46:42,200
and have int width equals convert to
9387
05:46:42,240 --> 05:46:47,958
in32 console.
9388
05:46:45,160 --> 05:46:50,840
readline and if you copy these
9389
05:46:47,958 --> 05:46:52,878
down change width to height and change
9390
05:46:50,840 --> 05:46:54,760
WID here to height and just before we
9391
05:46:52,878 --> 05:46:56,280
continue because we did copy and paste
9392
05:46:54,760 --> 05:46:58,000
down then this is quite bad and we
9393
05:46:56,280 --> 05:46:59,160
should use a function here so in both of
9394
05:46:58,000 --> 05:47:00,878
the cases we're going to output
9395
05:46:59,160 --> 05:47:02,840
something to the console and then read
9396
05:47:00,878 --> 05:47:04,718
it in and convert it back as an integer
9397
05:47:02,840 --> 05:47:06,360
so we can say static int because we
9398
05:47:04,718 --> 05:47:09,320
return back an integer so we can store
9399
05:47:06,360 --> 05:47:11,160
it we can say read int and we want a
9400
05:47:09,320 --> 05:47:12,798
string message so we can pass in this
9401
05:47:11,160 --> 05:47:15,200
value inside
9402
05:47:12,798 --> 05:47:18,080
here so we take these two lines and
9403
05:47:15,200 --> 05:47:19,958
place inside here instead of enter
9404
05:47:18,080 --> 05:47:21,478
height we're now going to have message
9405
05:47:19,958 --> 05:47:23,440
instead of int height we're just going
9406
05:47:21,478 --> 05:47:24,718
to have a return value and then we'll
9407
05:47:23,440 --> 05:47:26,840
return in the conversion from the
9408
05:47:24,718 --> 05:47:29,320
console read line and now we can use it
9409
05:47:26,840 --> 05:47:30,718
inside here read in inside the brackets
9410
05:47:29,320 --> 05:47:32,718
you want to put the string output which
9411
05:47:30,718 --> 05:47:35,000
is going to be enter height if we paste
9412
05:47:32,718 --> 05:47:36,878
that inside here and then do the same
9413
05:47:35,000 --> 05:47:38,718
thing for the previous section and and
9414
05:47:36,878 --> 05:47:41,680
change height to width and now get rid
9415
05:47:38,718 --> 05:47:43,520
of these so we just have these two lines
9416
05:47:41,680 --> 05:47:45,320
perfect so let's just print these out to
9417
05:47:43,520 --> 05:47:47,558
the screen on separate lines just to
9418
05:47:45,320 --> 05:47:50,200
make sure it's
9419
05:47:47,558 --> 05:47:53,160
working so the width of 10 and height of
9420
05:47:50,200 --> 05:47:54,558
15 we get 10 and 15 Perfect Since that's
9421
05:47:53,160 --> 05:47:56,240
working okay we can now create a
9422
05:47:54,558 --> 05:48:00,478
function that can calculate the area for
9423
05:47:56,240 --> 05:48:04,600
us so we can get static int calc area
9424
05:48:00,478 --> 05:48:04,600
and we can have int width and int
9425
05:48:05,000 --> 05:48:10,558
height and then taking this formula we
9426
05:48:07,520 --> 05:48:13,878
can simply just do return bracket width
9427
05:48:10,558 --> 05:48:15,400
times by height / two as you can see
9428
05:48:13,878 --> 05:48:17,798
this is literally exactly the same as
9429
05:48:15,400 --> 05:48:19,240
what we've got here it's crucial to
9430
05:48:17,798 --> 05:48:21,478
include the brackets because we want to
9431
05:48:19,240 --> 05:48:23,638
do this calculation first take the value
9432
05:48:21,478 --> 05:48:25,718
of that and divide it by two if this is
9433
05:48:23,638 --> 05:48:28,600
too many steps you can actually break it
9434
05:48:25,718 --> 05:48:30,878
down we can say int calculation is equal
9435
05:48:28,600 --> 05:48:33,160
to the width times by the height and
9436
05:48:30,878 --> 05:48:35,638
then we can simply return calculation /
9437
05:48:33,160 --> 05:48:37,080
two back to the function it's slightly
9438
05:48:35,638 --> 05:48:38,200
gred out because we haven't used use it
9439
05:48:37,080 --> 05:48:40,600
but that's not a problem we're going to
9440
05:48:38,200 --> 05:48:42,440
use it now I personally just to prefer
9441
05:48:40,600 --> 05:48:43,760
is to keep that inside here and just
9442
05:48:42,440 --> 05:48:45,600
have the brackets since a bit more
9443
05:48:43,760 --> 05:48:47,680
obvious to me what's going on but it's
9444
05:48:45,600 --> 05:48:49,600
up to you as the programmer either way
9445
05:48:47,680 --> 05:48:51,400
it makes no difference it's just more
9446
05:48:49,600 --> 05:48:53,040
for the programmer's convenience so now
9447
05:48:51,400 --> 05:48:55,680
we need to call the function above so we
9448
05:48:53,040 --> 05:48:57,520
can say int result equals calc area and
9449
05:48:55,680 --> 05:48:59,040
then pass in the width and pass in the
9450
05:48:57,520 --> 05:49:01,760
height and then we can have console
9451
05:48:59,040 --> 05:49:04,160
right line that just says the area is
9452
05:49:01,760 --> 05:49:06,280
and then result and then let's run it
9453
05:49:04,160 --> 05:49:07,638
you have 10 and 15 and there we go we
9454
05:49:06,280 --> 05:49:10,080
get the area area is
9455
05:49:07,638 --> 05:49:11,920
75 perfect if you didn't want to call
9456
05:49:10,080 --> 05:49:13,478
the function inside here and stall it
9457
05:49:11,920 --> 05:49:15,120
inside a variable you could just output
9458
05:49:13,478 --> 05:49:16,638
this straight to the screen by placing
9459
05:49:15,120 --> 05:49:18,520
the function call straight in the cly
9460
05:49:16,638 --> 05:49:22,280
braces and if you run that again just to
9461
05:49:18,520 --> 05:49:24,638
make sure 105 and we get 75 again
9462
05:49:22,280 --> 05:49:26,360
perfect so just a quick little recap we
9463
05:49:24,638 --> 05:49:28,320
have the console right plus the convert
9464
05:49:26,360 --> 05:49:30,120
inside this function which will let us
9465
05:49:28,320 --> 05:49:31,558
return as back an integer and we can
9466
05:49:30,120 --> 05:49:32,958
pass in the message straight in the
9467
05:49:31,558 --> 05:49:34,240
variable to make this a little bit
9468
05:49:32,958 --> 05:49:35,760
quicker and a bit easier you can
9469
05:49:34,240 --> 05:49:38,440
actually just change this one word
9470
05:49:35,760 --> 05:49:41,680
inside here so you could actually say
9471
05:49:38,440 --> 05:49:43,958
enter message call on space and then
9472
05:49:41,680 --> 05:49:45,798
just inside here you can say height and
9473
05:49:43,958 --> 05:49:47,638
just inside this one you can say width
9474
05:49:45,798 --> 05:49:49,200
just to reduce on code because in this
9475
05:49:47,638 --> 05:49:50,600
specific case the only thing that
9476
05:49:49,200 --> 05:49:52,280
changes between them two string
9477
05:49:50,600 --> 05:49:53,958
variables was the fact that one was
9478
05:49:52,280 --> 05:49:55,958
width and one's
9479
05:49:53,958 --> 05:49:58,638
height there we go you can see it's
9480
05:49:55,958 --> 05:50:00,320
exactly the same now so after we've read
9481
05:49:58,638 --> 05:50:01,958
in these two variables we pass it into
9482
05:50:00,320 --> 05:50:04,080
the function and all the function will
9483
05:50:01,958 --> 05:50:06,000
do is take width and height times them
9484
05:50:04,080 --> 05:50:08,400
both together take that value and divide
9485
05:50:06,000 --> 05:50:09,840
it by two and return it back to the user
9486
05:50:08,400 --> 05:50:11,920
and then we can call it straight in here
9487
05:50:09,840 --> 05:50:14,120
the area is calc area and pass in width
9488
05:50:11,920 --> 05:50:14,120
and
9489
05:50:15,080 --> 05:50:18,520
height in this task we're going to
9490
05:50:16,920 --> 05:50:20,400
create a function that will return us
9491
05:50:18,520 --> 05:50:23,160
back with the sum of numbers inside an
9492
05:50:20,400 --> 05:50:25,200
integer array so first step is to create
9493
05:50:23,160 --> 05:50:27,000
an initialize an array of numbers create
9494
05:50:25,200 --> 05:50:28,798
the function with the int return type
9495
05:50:27,000 --> 05:50:30,920
and it also takes a int array as a
9496
05:50:28,798 --> 05:50:32,400
parameter the function should return a
9497
05:50:30,920 --> 05:50:35,040
total of all the numbers inside the
9498
05:50:32,400 --> 05:50:37,798
interray call it in the main output the
9499
05:50:35,040 --> 05:50:39,558
total and just for extra bonus points
9500
05:50:37,798 --> 05:50:41,520
inside the function instead of using a
9501
05:50:39,558 --> 05:50:43,718
for Loop immediately you can check if
9502
05:50:41,520 --> 05:50:45,878
the array length is greater than zero
9503
05:50:43,718 --> 05:50:47,478
and then return minus one if it's empty
9504
05:50:45,878 --> 05:50:49,680
and then check that inside the main and
9505
05:50:47,478 --> 05:50:51,000
then output a specific message and also
9506
05:50:49,680 --> 05:50:53,120
see if you can think of any other
9507
05:50:51,000 --> 05:50:55,320
implementations you can use instead of
9508
05:50:53,120 --> 05:50:56,878
having to do return minus one how else
9509
05:50:55,320 --> 05:50:58,240
can you make this function whether that
9510
05:50:56,878 --> 05:51:00,080
means you have to change the parameters
9511
05:50:58,240 --> 05:51:01,718
or return type or anything like that
9512
05:51:00,080 --> 05:51:03,478
just have a think about the situation
9513
05:51:01,718 --> 05:51:04,958
and see what other options you can make
9514
05:51:03,478 --> 05:51:06,360
of course the output at the end is still
9515
05:51:04,958 --> 05:51:08,000
going to be the same but it's the way
9516
05:51:06,360 --> 05:51:09,320
you do it and the problem solving skills
9517
05:51:08,000 --> 05:51:11,200
that I'm trying to get you to get used
9518
05:51:09,320 --> 05:51:13,478
to let's get started so the create
9519
05:51:11,200 --> 05:51:16,320
initialize an array of numbers so you
9520
05:51:13,478 --> 05:51:18,400
have int numbers equals new int and
9521
05:51:16,320 --> 05:51:21,798
let's just allocate them manually 1 2 3
9522
05:51:18,400 --> 05:51:24,200
4 5 and add a semicolon there we go now
9523
05:51:21,798 --> 05:51:26,160
let's create the function sum of
9524
05:51:24,200 --> 05:51:27,878
numbers and we want it to return an
9525
05:51:26,160 --> 05:51:30,600
integer back with the total and we want
9526
05:51:27,878 --> 05:51:32,920
to take an interray called
9527
05:51:30,600 --> 05:51:34,638
numbers and then inside here we need to
9528
05:51:32,920 --> 05:51:37,400
actually return back an INT so we can
9529
05:51:34,638 --> 05:51:39,558
create an in total make that equal to Z
9530
05:51:37,400 --> 05:51:40,760
and at the end we can return total and
9531
05:51:39,558 --> 05:51:42,320
then during the middle we're going to
9532
05:51:40,760 --> 05:51:43,680
have to write our for Loop so we can go
9533
05:51:42,320 --> 05:51:45,360
through the integer array and count
9534
05:51:43,680 --> 05:51:48,600
through everything so we can use a for
9535
05:51:45,360 --> 05:51:51,558
each V item inside our numbers and just
9536
05:51:48,600 --> 05:51:54,320
inside here we can say total plus equals
9537
05:51:51,558 --> 05:51:56,000
item perfect so if we can call it in
9538
05:51:54,320 --> 05:51:57,718
here we can have a console right line
9539
05:51:56,000 --> 05:52:00,080
and we can just say sum of numbers and
9540
05:51:57,718 --> 05:52:01,240
pass our numbers array in the parameters
9541
05:52:00,080 --> 05:52:02,920
so we're just going to Output it to the
9542
05:52:01,240 --> 05:52:05,638
screen just for the time
9543
05:52:02,920 --> 05:52:08,600
being so sum of values from zero all the
9544
05:52:05,638 --> 05:52:10,400
way till 5 is 15
9545
05:52:08,600 --> 05:52:11,600
perfect so now that we've done that
9546
05:52:10,400 --> 05:52:13,280
we've created a function that Returns
9547
05:52:11,600 --> 05:52:15,080
the total numbers call it in the main
9548
05:52:13,280 --> 05:52:15,840
I'll put the total so we can just say
9549
05:52:15,080 --> 05:52:19,240
the
9550
05:52:15,840 --> 05:52:20,680
total the total is and then wrap this
9551
05:52:19,240 --> 05:52:23,718
around in the C
9552
05:52:20,680 --> 05:52:25,920
brace there we go the total is
9553
05:52:23,718 --> 05:52:27,920
15 now with the extra points we can
9554
05:52:25,920 --> 05:52:30,200
check the array length and return minus
9555
05:52:27,920 --> 05:52:31,840
one if the array is empty so let's see
9556
05:52:30,200 --> 05:52:33,520
what we can do in here before we even
9557
05:52:31,840 --> 05:52:35,680
end up going inside anything to do with
9558
05:52:33,520 --> 05:52:37,600
the total we can wrap this around in if
9559
05:52:35,680 --> 05:52:39,760
statement that says if the numbers do
9560
05:52:37,600 --> 05:52:41,200
length is greater than zero then we can
9561
05:52:39,760 --> 05:52:43,080
proceed with this little section so if
9562
05:52:41,200 --> 05:52:45,120
we highlight all of this hold alt and
9563
05:52:43,080 --> 05:52:47,280
press upar key then because it's one
9564
05:52:45,120 --> 05:52:48,798
back just press tab once and just press
9565
05:52:47,280 --> 05:52:50,760
the delete key here and let's get rid of
9566
05:52:48,798 --> 05:52:52,478
the empty Line This is going to return
9567
05:52:50,760 --> 05:52:54,920
us back with an error because not all
9568
05:52:52,478 --> 05:52:56,638
code path to return a value if this
9569
05:52:54,920 --> 05:52:58,600
returns back true then we'll eventually
9570
05:52:56,638 --> 05:53:00,360
hit a return but if this returns back
9571
05:52:58,600 --> 05:53:01,958
false then we not do anything inside
9572
05:53:00,360 --> 05:53:04,040
here so let's have a look at our
9573
05:53:01,958 --> 05:53:06,840
requirements return minus one if the
9574
05:53:04,040 --> 05:53:08,878
array is empty this dictates whether the
9575
05:53:06,840 --> 05:53:10,600
ray is empty so if this returns back
9576
05:53:08,878 --> 05:53:13,958
false then it's going to come into here
9577
05:53:10,600 --> 05:53:15,878
and we can say return minus one so we've
9578
05:53:13,958 --> 05:53:17,600
got this function call in here we don't
9579
05:53:15,878 --> 05:53:18,958
want to print the total right now
9580
05:53:17,600 --> 05:53:21,760
because we want to make sure that this
9581
05:53:18,958 --> 05:53:23,040
total is actually greater than zero
9582
05:53:21,760 --> 05:53:25,000
because we don't want to make a sum of
9583
05:53:23,040 --> 05:53:27,400
the numbers if the array is empty so if
9584
05:53:25,000 --> 05:53:29,558
you run this right now going to get the
9585
05:53:27,400 --> 05:53:31,520
total is 15 and if we get rid of all
9586
05:53:29,558 --> 05:53:34,040
these variables and just have the length
9587
05:53:31,520 --> 05:53:35,920
of five but not give it any value so all
9588
05:53:34,040 --> 05:53:37,200
the values are going to be zero then if
9589
05:53:35,920 --> 05:53:38,798
we still run that then the total is
9590
05:53:37,200 --> 05:53:40,600
going to be zero but that's still an
9591
05:53:38,798 --> 05:53:41,718
acceptable value because that's actually
9592
05:53:40,600 --> 05:53:44,040
got values
9593
05:53:41,718 --> 05:53:46,840
inside but if you said the array length
9594
05:53:44,040 --> 05:53:48,280
is zero then the total is minus one of
9595
05:53:46,840 --> 05:53:51,478
course we don't want the user to see
9596
05:53:48,280 --> 05:53:52,840
minus one so we want to bring this back
9597
05:53:51,478 --> 05:53:55,240
and instead of calling the function
9598
05:53:52,840 --> 05:53:58,520
immediately down here let's call it here
9599
05:53:55,240 --> 05:54:00,440
we can say int result is equal to this
9600
05:53:58,520 --> 05:54:02,120
and then we can say if the result is
9601
05:54:00,440 --> 05:54:04,080
greater than minus one then we can
9602
05:54:02,120 --> 05:54:07,478
output the total and again press into
9603
05:54:04,080 --> 05:54:09,280
this line hold alt and press Arrow key
9604
05:54:07,478 --> 05:54:12,160
and then we can place the result inside
9605
05:54:09,280 --> 05:54:15,520
here and the L's to this will be canot
9606
05:54:12,160 --> 05:54:18,718
add up an empty array there we go so now
9607
05:54:15,520 --> 05:54:20,920
if you run it we should still see 15 but
9608
05:54:18,718 --> 05:54:24,080
if we get rid of this and put it back to
9609
05:54:20,920 --> 05:54:27,558
zero there we go we cannot add an empty
9610
05:54:24,080 --> 05:54:28,958
array so we bring that back perfect so
9611
05:54:27,558 --> 05:54:31,000
now if we check this we've got check in
9612
05:54:28,958 --> 05:54:33,000
the main output the message and then now
9613
05:54:31,000 --> 05:54:35,280
we've got this little solution do we
9614
05:54:33,000 --> 05:54:37,558
need to return minus one how else can we
9615
05:54:35,280 --> 05:54:39,520
make this so right now we're going to
9616
05:54:37,558 --> 05:54:41,760
have to return back minus one cuz that's
9617
05:54:39,520 --> 05:54:43,798
the only way that we can tell the user
9618
05:54:41,760 --> 05:54:46,080
whether or not it's been successful if
9619
05:54:43,798 --> 05:54:47,878
the r is empty we return minus one and
9620
05:54:46,080 --> 05:54:49,798
if the ray is not empty then we return
9621
05:54:47,878 --> 05:54:51,958
something greater than minus one which
9622
05:54:49,798 --> 05:54:53,600
will be something above zero or zero
9623
05:54:51,958 --> 05:54:54,958
itself but as long as it's greater than
9624
05:54:53,600 --> 05:54:56,798
minus one that's how we know it's
9625
05:54:54,958 --> 05:54:59,080
complete but we don't actually want to
9626
05:54:56,798 --> 05:55:01,160
do this we want to be ideally returning
9627
05:54:59,080 --> 05:55:03,280
back a Boolean so let's duplicate this
9628
05:55:01,160 --> 05:55:05,320
function and show you both
9629
05:55:03,280 --> 05:55:07,080
implementations let's change this into a
9630
05:55:05,320 --> 05:55:08,080
Bo
9631
05:55:07,080 --> 05:55:09,798
and then because we're still going to
9632
05:55:08,080 --> 05:55:12,558
need to reference the total we're going
9633
05:55:09,798 --> 05:55:14,400
to have an out in total so we need to
9634
05:55:12,558 --> 05:55:16,160
change up a few things inside here
9635
05:55:14,400 --> 05:55:18,160
instead of this return minus one we can
9636
05:55:16,160 --> 05:55:20,240
do return false to indicate that it
9637
05:55:18,160 --> 05:55:21,920
hasn't been successful instead of
9638
05:55:20,240 --> 05:55:23,920
returning back total we can actually
9639
05:55:21,920 --> 05:55:25,558
just return back true now we haven't
9640
05:55:23,920 --> 05:55:27,600
done everything yet we don't need to
9641
05:55:25,558 --> 05:55:29,638
redeclare in total inside here because
9642
05:55:27,600 --> 05:55:31,478
we already declared it inside here so we
9643
05:55:29,638 --> 05:55:34,120
could just initialize total to equal to
9644
05:55:31,478 --> 05:55:36,040
zero but the problem here is if we make
9645
05:55:34,120 --> 05:55:36,760
total equal to zero just within this
9646
05:55:36,040 --> 05:55:38,000
path
9647
05:55:36,760 --> 05:55:40,160
then it's still going to complain that
9648
05:55:38,000 --> 05:55:41,680
total must be assigned so we can take
9649
05:55:40,160 --> 05:55:44,400
this and just assign it right at the
9650
05:55:41,680 --> 05:55:46,320
start of the function just like that
9651
05:55:44,400 --> 05:55:48,440
there we go so we make total equal to
9652
05:55:46,320 --> 05:55:50,040
zero that gets rid of the compil errors
9653
05:55:48,440 --> 05:55:51,958
and gives it a value the reason why we
9654
05:55:50,040 --> 05:55:54,240
have to give it a value is because this
9655
05:55:51,958 --> 05:55:55,920
out in parameter doesn't like it if you
9656
05:55:54,240 --> 05:55:57,718
don't assign the value that is a
9657
05:55:55,920 --> 05:55:59,798
requirement for using the out parameter
9658
05:55:57,718 --> 05:56:01,840
so we make total equal to zero check the
9659
05:55:59,798 --> 05:56:03,920
numbers length If This Were to return
9660
05:56:01,840 --> 05:56:06,360
back false then we return false to
9661
05:56:03,920 --> 05:56:07,878
indicate that we can't do anything if it
9662
05:56:06,360 --> 05:56:10,360
this returns back true it will go
9663
05:56:07,878 --> 05:56:13,240
through and count all of the items place
9664
05:56:10,360 --> 05:56:15,000
inside total and then return back true
9665
05:56:13,240 --> 05:56:16,878
so let's have a look and see what else
9666
05:56:15,000 --> 05:56:18,958
we can write this little section of code
9667
05:56:16,878 --> 05:56:20,798
so instead of having to call inside here
9668
05:56:18,958 --> 05:56:22,360
and use it within there then we can
9669
05:56:20,798 --> 05:56:23,760
actually do this another way the reason
9670
05:56:22,360 --> 05:56:26,000
why we actually calling this straight
9671
05:56:23,760 --> 05:56:27,958
inside here is because we can't place
9672
05:56:26,000 --> 05:56:29,878
this straight into here since it returns
9673
05:56:27,958 --> 05:56:31,360
us back with an integer and now that is
9674
05:56:29,878 --> 05:56:33,120
actually fine if that returns back of
9675
05:56:31,360 --> 05:56:35,360
the integer cuz as you can see there's
9676
05:56:33,120 --> 05:56:37,200
no problems here the issue arises is
9677
05:56:35,360 --> 05:56:39,280
when we have our result variable we can
9678
05:56:37,200 --> 05:56:41,200
use it inside the bracket and down here
9679
05:56:39,280 --> 05:56:42,958
there's no point in calling this twice
9680
05:56:41,200 --> 05:56:44,680
to get the same value although this
9681
05:56:42,958 --> 05:56:45,878
would still technically work but this is
9682
05:56:44,680 --> 05:56:48,080
not really efficient since you're
9683
05:56:45,878 --> 05:56:49,040
calling the function twice it's best to
9684
05:56:48,080 --> 05:56:51,718
call it
9685
05:56:49,040 --> 05:56:54,040
once store the value and then make use
9686
05:56:51,718 --> 05:56:55,440
of it twice when we use our out
9687
05:56:54,040 --> 05:56:57,280
parameter you'll see that this gets a
9688
05:56:55,440 --> 05:56:59,400
bit easier so straight inside the if
9689
05:56:57,280 --> 05:57:01,200
statement we can say sum of numbers and
9690
05:56:59,400 --> 05:57:04,160
then we can pass in our numbers and then
9691
05:57:01,200 --> 05:57:06,040
space out into total and what that'll do
9692
05:57:04,160 --> 05:57:08,000
now is because this returns us back with
9693
05:57:06,040 --> 05:57:09,878
the fol we don't need any additional
9694
05:57:08,000 --> 05:57:11,520
conditions like we have one here cuz
9695
05:57:09,878 --> 05:57:14,440
this will turn us back with a true or
9696
05:57:11,520 --> 05:57:16,040
false true if it succeeds false if it
9697
05:57:14,440 --> 05:57:17,798
doesn't so we don't need to say if it's
9698
05:57:16,040 --> 05:57:19,040
greater than minus one because we don't
9699
05:57:17,798 --> 05:57:20,958
actually care about the value of the
9700
05:57:19,040 --> 05:57:23,000
total right now we just care if this
9701
05:57:20,958 --> 05:57:24,760
returns back with true or false so
9702
05:57:23,000 --> 05:57:26,638
inside here we can take this same output
9703
05:57:24,760 --> 05:57:27,920
message and instead of having result
9704
05:57:26,638 --> 05:57:30,520
here we can have
9705
05:57:27,920 --> 05:57:32,360
total and then we can take this exact L
9706
05:57:30,520 --> 05:57:35,520
statement and place it here and let's
9707
05:57:32,360 --> 05:57:37,478
see what happens now the total is 15 and
9708
05:57:35,520 --> 05:57:39,360
the total is 15 so they both turning
9709
05:57:37,478 --> 05:57:41,760
back the same values let's add something
9710
05:57:39,360 --> 05:57:43,718
else on here just to make sure it works
9711
05:57:41,760 --> 05:57:45,878
there we go so you can see that both
9712
05:57:43,718 --> 05:57:47,760
these Solutions look very identical but
9713
05:57:45,878 --> 05:57:50,200
one is considerably different we have to
9714
05:57:47,760 --> 05:57:52,280
make a variable outside use inside here
9715
05:57:50,200 --> 05:57:53,760
and use inside here whereas in this
9716
05:57:52,280 --> 05:57:55,638
function we can actually just call the
9717
05:57:53,760 --> 05:57:58,080
function straight inside that will
9718
05:57:55,638 --> 05:58:00,360
process our new out into total and then
9719
05:57:58,080 --> 05:58:01,718
we can simply use total inside here and
9720
05:58:00,360 --> 05:58:04,240
we don't have to require another
9721
05:58:01,718 --> 05:58:06,478
additional variable just outside here as
9722
05:58:04,240 --> 05:58:08,360
this one will and you can see that the
9723
05:58:06,478 --> 05:58:10,200
functions is a little bit better because
9724
05:58:08,360 --> 05:58:11,798
we're returning true and returning false
9725
05:58:10,200 --> 05:58:13,718
to indicate whether or not it's been
9726
05:58:11,798 --> 05:58:15,878
successful whereas in this solution we
9727
05:58:13,718 --> 05:58:18,280
have to return back an arbitrary value
9728
05:58:15,878 --> 05:58:20,440
and then check that in the console the
9729
05:58:18,280 --> 05:58:22,600
reason with returning minus one is kind
9730
05:58:20,440 --> 05:58:24,958
of a problem is what if these numbers
9731
05:58:22,600 --> 05:58:27,280
are actually all negative then this
9732
05:58:24,958 --> 05:58:29,520
value is going to be very bad this can
9733
05:58:27,280 --> 05:58:32,080
only really work if all of these values
9734
05:58:29,520 --> 05:58:34,400
are positive if you had negatives for
9735
05:58:32,080 --> 05:58:37,680
all of these and we tried running the
9736
05:58:34,400 --> 05:58:39,680
code there you go this is a major issue
9737
05:58:37,680 --> 05:58:41,400
cannot add up an empty array well it's
9738
05:58:39,680 --> 05:58:43,440
not the fact that it is empty it's the
9739
05:58:41,400 --> 05:58:45,320
fact that these values are smaller than
9740
05:58:43,440 --> 05:58:46,680
minus one so you don't actually want to
9741
05:58:45,320 --> 05:58:48,600
be going with something like this
9742
05:58:46,680 --> 05:58:50,280
approach where you're comparing the
9743
05:58:48,600 --> 05:58:51,958
value because you don't know what
9744
05:58:50,280 --> 05:58:53,638
numbers are going to be typed into here
9745
05:58:51,958 --> 05:58:55,718
if the user were type in positive or
9746
05:58:53,638 --> 05:58:57,718
negative you want to provide a solution
9747
05:58:55,718 --> 05:58:59,558
just like this one that works for either
9748
05:58:57,718 --> 05:59:01,240
positive or negative you don't want to
9749
05:58:59,558 --> 05:59:03,520
restrict that by checking if it's
9750
05:59:01,240 --> 05:59:05,638
greater than minus one will only works
9751
05:59:03,520 --> 05:59:07,520
if all of these are positive values all
9752
05:59:05,638 --> 05:59:09,840
this is an index for the array since
9753
05:59:07,520 --> 05:59:11,878
arrays start at zero and go upwards you
9754
05:59:09,840 --> 05:59:13,080
can't have a negative -1 array position
9755
05:59:11,878 --> 05:59:14,958
but in terms of numbers that are
9756
05:59:13,080 --> 05:59:16,638
randomly typed inside of a ray you
9757
05:59:14,958 --> 05:59:18,320
should always be wary of using this
9758
05:59:16,638 --> 05:59:20,120
option because if some of them are
9759
05:59:18,320 --> 05:59:23,040
negative then this could return back
9760
05:59:20,120 --> 05:59:25,160
false so let's just have a quick recap
9761
05:59:23,040 --> 05:59:27,200
so we have an integer array we have our
9762
05:59:25,160 --> 05:59:29,478
values initialized down here and then
9763
05:59:27,200 --> 05:59:31,558
the first solution we call the function
9764
05:59:29,478 --> 05:59:33,280
passing the numbers array get our result
9765
05:59:31,558 --> 05:59:35,160
back check if it's greater than minus
9766
05:59:33,280 --> 05:59:37,400
one then that means it's been successful
9767
05:59:35,160 --> 05:59:40,040
so we print it out and else we tell it
9768
05:59:37,400 --> 05:59:41,400
it's an empty array now second option we
9769
05:59:40,040 --> 05:59:43,200
actually call the function because it
9770
05:59:41,400 --> 05:59:44,638
returns back a boly in this case so we
9771
05:59:43,200 --> 05:59:46,080
can place it straight inside the if
9772
05:59:44,638 --> 05:59:48,120
statement and then we can pass in the
9773
05:59:46,080 --> 05:59:50,040
numbers AR and then we have an out in
9774
05:59:48,120 --> 05:59:51,520
total which will give us the total and
9775
05:59:50,040 --> 05:59:53,440
in this case we don't need to check the
9776
05:59:51,520 --> 05:59:55,920
value because this returns us back with
9777
05:59:53,440 --> 05:59:57,600
the true or false and in this case true
9778
05:59:55,920 --> 05:59:59,760
determines whether it's been successful
9779
05:59:57,600 --> 06:00:01,798
and false it doesn't if it returns back
9780
05:59:59,760 --> 06:00:03,280
true then it has some values inside the
9781
06:00:01,798 --> 06:00:05,040
numbers array that we can actually add
9782
06:00:03,280 --> 06:00:07,360
up and if it returns back false then
9783
06:00:05,040 --> 06:00:09,240
that means the array is empty so let's
9784
06:00:07,360 --> 06:00:10,958
have a look at the implementations so
9785
06:00:09,240 --> 06:00:13,760
without the out parameters this is the
9786
06:00:10,958 --> 06:00:15,160
first solution up here we get the
9787
06:00:13,760 --> 06:00:17,160
numbers length check if it's greater
9788
06:00:15,160 --> 06:00:19,200
than zero if it's not we return minus
9789
06:00:17,160 --> 06:00:21,638
one make a new variable called total
9790
06:00:19,200 --> 06:00:24,000
make equal to zero and we run a for each
9791
06:00:21,638 --> 06:00:26,600
Loop V item inside the numbers and we
9792
06:00:24,000 --> 06:00:28,520
just say total plus equals item add them
9793
06:00:26,600 --> 06:00:31,360
all up in the for Loop and simply return
9794
06:00:28,520 --> 06:00:33,320
the total and then in the out solution
9795
06:00:31,360 --> 06:00:35,000
we have a total equals zero which just
9796
06:00:33,320 --> 06:00:37,080
initializes this variable otherwise
9797
06:00:35,000 --> 06:00:39,120
we'll get a compiler then we can say if
9798
06:00:37,080 --> 06:00:40,920
the numbers is greater than zero then we
9799
06:00:39,120 --> 06:00:43,040
can say if numbers. length is greater
9800
06:00:40,920 --> 06:00:45,240
than zero then we can do the for Loop
9801
06:00:43,040 --> 06:00:47,718
and return back true and if it turn back
9802
06:00:45,240 --> 06:00:49,080
false then we can say false inside here
9803
06:00:47,718 --> 06:00:50,798
and then when we use it straight in the
9804
06:00:49,080 --> 06:00:53,080
brackets we have access to it straight
9805
06:00:50,798 --> 06:00:54,718
away since it uses a Boolean variable
9806
06:00:53,080 --> 06:00:57,200
and then our value will be placed inside
9807
06:00:54,718 --> 06:00:59,080
our out into total parameter which is
9808
06:00:57,200 --> 06:01:01,160
used down here for the output to the
9809
06:00:59,080 --> 06:01:03,040
screen and again if this returns back
9810
06:01:01,160 --> 06:01:04,600
false then it will say I cannot add up
9811
06:01:03,040 --> 06:01:06,520
an empty rray and then we hit the
9812
06:01:04,600 --> 06:01:07,878
console read line and we at the the end
9813
06:01:06,520 --> 06:01:09,360
there we go that covers everything
9814
06:01:07,878 --> 06:01:11,878
inside
9815
06:01:09,360 --> 06:01:13,638
here exceptions are used to prevent your
9816
06:01:11,878 --> 06:01:16,400
code from crashing and then the program
9817
06:01:13,638 --> 06:01:18,878
closes down let's have a look at a very
9818
06:01:16,400 --> 06:01:21,440
very common example let's have a console
9819
06:01:18,878 --> 06:01:24,160
right and we can say enter a number and
9820
06:01:21,440 --> 06:01:27,040
then we can use int Norm equals convert
9821
06:01:24,160 --> 06:01:29,638
to int32 and then we can have a console.
9822
06:01:27,040 --> 06:01:31,478
readline inside here and let's just
9823
06:01:29,638 --> 06:01:33,120
print out number to the screen now if we
9824
06:01:31,478 --> 06:01:35,600
assume everything goes well and we
9825
06:01:33,120 --> 06:01:38,240
actually enter an integer like 15 and we
9826
06:01:35,600 --> 06:01:40,400
press enter then we simply get 15
9827
06:01:38,240 --> 06:01:42,520
returned and as we saw in the past when
9828
06:01:40,400 --> 06:01:44,320
we covered the trip pass function if you
9829
06:01:42,520 --> 06:01:47,240
enter anything else that's not exactly a
9830
06:01:44,320 --> 06:01:49,840
number then you get again exception
9831
06:01:47,240 --> 06:01:51,920
exception unhandled unhandled means that
9832
06:01:49,840 --> 06:01:53,638
we haven't wrote anything specific that
9833
06:01:51,920 --> 06:01:55,760
will handle this exception so it's
9834
06:01:53,638 --> 06:01:58,040
causing the program to close it's a
9835
06:01:55,760 --> 06:02:00,080
system. format exception it says the
9836
06:01:58,040 --> 06:02:01,958
input string was not in a correct format
9837
06:02:00,080 --> 06:02:04,080
so this is a format exception because
9838
06:02:01,958 --> 06:02:05,878
we've entered in a very weird set of
9839
06:02:04,080 --> 06:02:08,080
characters that can't be converted over
9840
06:02:05,878 --> 06:02:10,280
to an integer so now our program is
9841
06:02:08,080 --> 06:02:11,958
forced to crash and now rest of our code
9842
06:02:10,280 --> 06:02:14,040
is going to be deleted now because it
9843
06:02:11,958 --> 06:02:15,638
won't be able to run so if you had any
9844
06:02:14,040 --> 06:02:17,760
important data that was stored just
9845
06:02:15,638 --> 06:02:19,400
before this point then it is now lost
9846
06:02:17,760 --> 06:02:21,558
this is very crucial because if you have
9847
06:02:19,400 --> 06:02:23,120
a system that rides to a database or it
9848
06:02:21,558 --> 06:02:24,760
stores some very important information
9849
06:02:23,120 --> 06:02:26,558
that someone's typed in you want to
9850
06:02:24,760 --> 06:02:28,520
handle the exceptions so if something
9851
06:02:26,558 --> 06:02:30,120
were to arise it doesn't actually close
9852
06:02:28,520 --> 06:02:31,920
the program down and they lose all of
9853
06:02:30,120 --> 06:02:33,638
their data I'm sure you've had it in the
9854
06:02:31,920 --> 06:02:35,840
past where you've tried to do something
9855
06:02:33,638 --> 06:02:37,878
and the program comes up Windows and it
9856
06:02:35,840 --> 06:02:39,638
says this program had to stop responding
9857
06:02:37,878 --> 06:02:41,120
when that normally comes up it means
9858
06:02:39,638 --> 06:02:43,000
that Something's Happened that's so
9859
06:02:41,120 --> 06:02:45,320
fatal that the program cannot continue
9860
06:02:43,000 --> 06:02:47,000
it has to crash down and in most cases
9861
06:02:45,320 --> 06:02:48,798
this is what's called an exception so if
9862
06:02:47,000 --> 06:02:50,600
we run this code again let's have a look
9863
06:02:48,798 --> 06:02:52,798
and see what else we can do to make this
9864
06:02:50,600 --> 06:02:55,478
code break so of course as you heard
9865
06:02:52,798 --> 06:02:57,920
before a gnome integer can only survive
9866
06:02:55,478 --> 06:02:59,680
up to about 2 billion numbers so if you
9867
06:02:57,920 --> 06:03:01,920
enter a number bigger than that let's
9868
06:02:59,680 --> 06:03:04,440
see what happens then we get an overflow
9869
06:03:01,920 --> 06:03:06,878
exception value is either too large or
9870
06:03:04,440 --> 06:03:09,320
too small for an in3 too of course this
9871
06:03:06,878 --> 06:03:11,600
number is very big and only a long would
9872
06:03:09,320 --> 06:03:13,440
handle something like this an integer
9873
06:03:11,600 --> 06:03:15,440
definitely won't so these are the kind
9874
06:03:13,440 --> 06:03:17,440
of exceptions where you'd have to handle
9875
06:03:15,440 --> 06:03:19,400
it yourself manually otherwise your
9876
06:03:17,440 --> 06:03:20,760
program's going to crash coming up next
9877
06:03:19,400 --> 06:03:22,638
we're going to go through the ways that
9878
06:03:20,760 --> 06:03:24,718
we can handle these and make sure
9879
06:03:22,638 --> 06:03:27,160
programs don't come to a halt exactly
9880
06:03:24,718 --> 06:03:27,160
like these
9881
06:03:27,400 --> 06:03:32,360
examples I want to explore a technique
9882
06:03:29,840 --> 06:03:35,200
called trycatch this is very similar to
9883
06:03:32,360 --> 06:03:37,440
when we use the int. trass what this
9884
06:03:35,200 --> 06:03:39,040
essentially does does is it tries to do
9885
06:03:37,440 --> 06:03:41,320
something and it lets you know if
9886
06:03:39,040 --> 06:03:43,280
something went wrong this is exactly the
9887
06:03:41,320 --> 06:03:45,558
same when we're dealing with exceptions
9888
06:03:43,280 --> 06:03:47,160
except when we're doing a try catch what
9889
06:03:45,558 --> 06:03:49,040
the catch means is it's catching the
9890
06:03:47,160 --> 06:03:51,360
exceptions that happen and therefore it
9891
06:03:49,040 --> 06:03:53,478
is now a handled exception instead of
9892
06:03:51,360 --> 06:03:55,760
unhandled exception so before we try and
9893
06:03:53,478 --> 06:03:58,478
fix how this works let's just explore
9894
06:03:55,760 --> 06:04:00,798
the syntax so we can trpe in try Follow
9895
06:03:58,478 --> 06:04:03,120
by curly brace and just after this curly
9896
06:04:00,798 --> 06:04:04,360
brace we can say catch and then we're
9897
06:04:03,120 --> 06:04:06,320
going to put something inside the
9898
06:04:04,360 --> 06:04:08,600
brackets and then we have it opens
9899
06:04:06,320 --> 06:04:10,400
brackets if you wanted a quick shortcut
9900
06:04:08,600 --> 06:04:12,200
to this just like we've done the for
9901
06:04:10,400 --> 06:04:13,958
Loop and we double tap tab we can
9902
06:04:12,200 --> 06:04:16,160
actually delete this so you can type in
9903
06:04:13,958 --> 06:04:17,920
try and press tab twice and then you get
9904
06:04:16,160 --> 06:04:20,160
try the Open brackets you get the
9905
06:04:17,920 --> 06:04:22,200
exception and then we get a throw so
9906
06:04:20,160 --> 06:04:24,000
let's get rid of this and what we can do
9907
06:04:22,200 --> 06:04:26,478
right now is we can actually place the
9908
06:04:24,000 --> 06:04:29,000
code that we want inside here inside
9909
06:04:26,478 --> 06:04:30,920
this Tri bit because we're telling the
9910
06:04:29,000 --> 06:04:32,878
compiler that we want to try to do the
9911
06:04:30,920 --> 06:04:34,840
things inside here and then we want to
9912
06:04:32,878 --> 06:04:37,360
catch these things so inside these
9913
06:04:34,840 --> 06:04:39,878
brackets what you want to actually do is
9914
06:04:37,360 --> 06:04:42,320
decide what thing that you want to catch
9915
06:04:39,878 --> 06:04:44,600
if you type in exception this will catch
9916
06:04:42,320 --> 06:04:46,638
every single exception now the problem
9917
06:04:44,600 --> 06:04:48,240
with that is you'll actually face some
9918
06:04:46,638 --> 06:04:51,200
difficulties when you're trying to
9919
06:04:48,240 --> 06:04:53,000
Output very very specific messages so as
9920
06:04:51,200 --> 06:04:54,878
we've said before when you type in a
9921
06:04:53,000 --> 06:04:57,120
value too large you get an overflow
9922
06:04:54,878 --> 06:04:59,280
exception but when you type a value that
9923
06:04:57,120 --> 06:05:01,520
can't be converted like a square bracket
9924
06:04:59,280 --> 06:05:03,760
then you get an input format exception
9925
06:05:01,520 --> 06:05:06,200
so first what you actually want to do is
9926
06:05:03,760 --> 06:05:09,000
run this code outside the try and let's
9927
06:05:06,200 --> 06:05:10,920
run it now and let's get them bad values
9928
06:05:09,000 --> 06:05:12,840
so we had the square brackets and we can
9929
06:05:10,920 --> 06:05:15,440
get this and let's copy it down and
9930
06:05:12,840 --> 06:05:17,160
place it inside a comment just like this
9931
06:05:15,440 --> 06:05:21,200
and then let's rerun
9932
06:05:17,160 --> 06:05:24,160
it and if you type a number too big take
9933
06:05:21,200 --> 06:05:25,520
this stop the program and put it inside
9934
06:05:24,160 --> 06:05:27,000
here so now that you know what the
9935
06:05:25,520 --> 06:05:29,160
exceptions are called we can actually
9936
06:05:27,000 --> 06:05:31,718
write these inside the code so we have a
9937
06:05:29,160 --> 06:05:33,400
form exception and an overflow exception
9938
06:05:31,718 --> 06:05:35,958
but before we get into these specific
9939
06:05:33,400 --> 06:05:38,040
exceptions let's just catch everything
9940
06:05:35,958 --> 06:05:39,798
this is just a catch all and then see
9941
06:05:38,040 --> 06:05:42,200
how this
9942
06:05:39,798 --> 06:05:44,680
works so as we've put it back inside
9943
06:05:42,200 --> 06:05:46,280
here let's see what actually happens so
9944
06:05:44,680 --> 06:05:49,840
we can just put a console right on here
9945
06:05:46,280 --> 06:05:52,240
and say something has went
9946
06:05:49,840 --> 06:05:54,120
wrong the reason why it's something has
9947
06:05:52,240 --> 06:05:57,040
went wrong is because this is a catch
9948
06:05:54,120 --> 06:05:59,360
all any exception appears during this
9949
06:05:57,040 --> 06:06:01,160
code it will always come into here
9950
06:05:59,360 --> 06:06:03,360
regardless of what the exception is
9951
06:06:01,160 --> 06:06:05,040
whether it's a format or an overflow it
9952
06:06:03,360 --> 06:06:06,958
doesn't matter it will always come into
9953
06:06:05,040 --> 06:06:09,440
here so it's going to be something
9954
06:06:06,958 --> 06:06:12,040
because this is not specific at all so
9955
06:06:09,440 --> 06:06:14,360
let's run the code enter a very big
9956
06:06:12,040 --> 06:06:15,798
number something has went wrong but if
9957
06:06:14,360 --> 06:06:17,718
you notice how the program doesn't
9958
06:06:15,798 --> 06:06:19,920
actually clash it's still very much
9959
06:06:17,718 --> 06:06:21,760
alive and we can test this by putting a
9960
06:06:19,920 --> 06:06:24,718
simple message here at the end that says
9961
06:06:21,760 --> 06:06:26,240
goodbye and we run it again enter a big
9962
06:06:24,718 --> 06:06:28,558
number and there you go we get the
9963
06:06:26,240 --> 06:06:30,600
message as goodbye so it successfully
9964
06:06:28,558 --> 06:06:31,958
skipped over this section without
9965
06:06:30,600 --> 06:06:34,120
actually causing an exception that
9966
06:06:31,958 --> 06:06:36,160
breaks the program so even though we've
9967
06:06:34,120 --> 06:06:38,200
entered something very fertile because
9968
06:06:36,160 --> 06:06:40,440
we've handled it then there's no
9969
06:06:38,200 --> 06:06:42,120
problems so if we now type the other one
9970
06:06:40,440 --> 06:06:43,718
where we have the square brackets then
9971
06:06:42,120 --> 06:06:44,958
you'll see we'll also get something has
9972
06:06:43,718 --> 06:06:47,478
went
9973
06:06:44,958 --> 06:06:49,840
wrong so if you wanted to catch a very
9974
06:06:47,478 --> 06:06:52,400
specific exception what we can do is say
9975
06:06:49,840 --> 06:06:54,760
catch open the brackets and we can say
9976
06:06:52,400 --> 06:06:57,320
format exception just like it says above
9977
06:06:54,760 --> 06:06:58,920
here and then place the curry braces and
9978
06:06:57,320 --> 06:07:01,120
what you can do now is you can actually
9979
06:06:58,920 --> 06:07:04,160
chain these and these go from top to
9980
06:07:01,120 --> 06:07:05,958
bottom when an exception occurs up here
9981
06:07:04,160 --> 06:07:08,040
it will match this one if if it doesn't
9982
06:07:05,958 --> 06:07:09,840
match it will match this one and you
9983
06:07:08,040 --> 06:07:11,760
want to put the catch all right at the
9984
06:07:09,840 --> 06:07:13,558
end so if it doesn't match any of the
9985
06:07:11,760 --> 06:07:15,360
ones above it it will always match the
9986
06:07:13,558 --> 06:07:17,600
last one so you can have a generic
9987
06:07:15,360 --> 06:07:19,798
message inside here but you can have a
9988
06:07:17,600 --> 06:07:21,680
very specific message inside here
9989
06:07:19,798 --> 06:07:24,040
because it's a format exception you can
9990
06:07:21,680 --> 06:07:25,600
say please only enter numbers so now if
9991
06:07:24,040 --> 06:07:27,760
you run the code and if we type in
9992
06:07:25,600 --> 06:07:29,958
square brackets it'll say please only
9993
06:07:27,760 --> 06:07:32,520
enter numbers there we go so this is a
9994
06:07:29,958 --> 06:07:34,280
very specific example and if we copy and
9995
06:07:32,520 --> 06:07:36,920
paste this down again and then we can
9996
06:07:34,280 --> 06:07:40,760
say overflow exception
9997
06:07:36,920 --> 06:07:43,718
and we can say please enter a number
9998
06:07:40,760 --> 06:07:46,000
less than 2
9999
06:07:43,718 --> 06:07:48,160
billion if you enter a very big number
10000
06:07:46,000 --> 06:07:49,958
again please enter a number that's less
10001
06:07:48,160 --> 06:07:52,558
than 2 billion
10002
06:07:49,958 --> 06:07:54,440
perfect so this will get checked like if
10003
06:07:52,558 --> 06:07:56,638
statement it will start on the first one
10004
06:07:54,440 --> 06:07:58,638
and go down the list imagine this is a
10005
06:07:56,638 --> 06:08:01,000
if and then we have an else if and then
10006
06:07:58,638 --> 06:08:03,478
else if as well if you place this one
10007
06:08:01,000 --> 06:08:05,240
right at the start then you're actually
10008
06:08:03,478 --> 06:08:07,360
going to get compiled errors and you
10009
06:08:05,240 --> 06:08:09,400
can't actually have the catch all right
10010
06:08:07,360 --> 06:08:11,760
at the start so even if you wanted to
10011
06:08:09,400 --> 06:08:13,200
the compiler literally doesn't let you
10012
06:08:11,760 --> 06:08:14,798
and anyway you don't really want to be
10013
06:08:13,200 --> 06:08:16,638
doing that because then you'll be ruling
10014
06:08:14,798 --> 06:08:18,280
out these two and it's kind of pointless
10015
06:08:16,638 --> 06:08:20,320
writing this code if it's never going to
10016
06:08:18,280 --> 06:08:22,240
get used anyway so now when we run the
10017
06:08:20,320 --> 06:08:24,478
code you can see even if we typ in a bad
10018
06:08:22,240 --> 06:08:26,680
value our program will still run and
10019
06:08:24,478 --> 06:08:28,600
everything works okay so now what we can
10020
06:08:26,680 --> 06:08:29,360
do is actually make this into a real
10021
06:08:28,600 --> 06:08:32,520
life
10022
06:08:29,360 --> 06:08:34,400
situation so if we actually type in 10.2
10023
06:08:32,520 --> 06:08:35,520
then it says please only enter numbers
10024
06:08:34,400 --> 06:08:37,200
then we're going to have an issue here
10025
06:08:35,520 --> 06:08:38,958
here because if we try and print the
10026
06:08:37,200 --> 06:08:41,360
value down here then it's going to cause
10027
06:08:38,958 --> 06:08:43,320
a problem let's try and print out num
10028
06:08:41,360 --> 06:08:45,680
and you can see that num does not exist
10029
06:08:43,320 --> 06:08:47,680
now we have an issue of scope in is only
10030
06:08:45,680 --> 06:08:49,440
declared within here and here so if you
10031
06:08:47,680 --> 06:08:51,440
wanted to print out the value of gome
10032
06:08:49,440 --> 06:08:53,680
we'd have to print it out up here so now
10033
06:08:51,440 --> 06:08:56,798
let's run it again and if we enter 20 20
10034
06:08:53,680 --> 06:08:58,400
comes up if we've entered 15.5 then it
10035
06:08:56,798 --> 06:09:00,478
actually skips over this console right
10036
06:08:58,400 --> 06:09:02,200
line because the exception occurs right
10037
06:09:00,478 --> 06:09:04,040
here and then it'll jump into one of
10038
06:09:02,200 --> 06:09:06,360
these paths so in order to store the
10039
06:09:04,040 --> 06:09:08,958
value of num you can either the move num
10040
06:09:06,360 --> 06:09:11,040
outside here and make it equal to zero
10041
06:09:08,958 --> 06:09:13,040
and then assign num inside here and then
10042
06:09:11,040 --> 06:09:14,958
you can take this console right line and
10043
06:09:13,040 --> 06:09:16,638
actually put it right after the triy
10044
06:09:14,958 --> 06:09:19,798
pass but I don't really suggest this
10045
06:09:16,638 --> 06:09:21,600
option so if you said 15.5 then you're
10046
06:09:19,798 --> 06:09:23,478
just going to get zero returned but we
10047
06:09:21,600 --> 06:09:24,798
don't really care about the zero because
10048
06:09:23,478 --> 06:09:26,840
we're not going to get the value
10049
06:09:24,798 --> 06:09:29,400
converted so why do we need to print the
10050
06:09:26,840 --> 06:09:33,400
zero and the answer is we don't so what
10051
06:09:29,400 --> 06:09:33,400
we should do is keep it all inside here
10052
06:09:33,958 --> 06:09:39,280
again and then what we should do instead
10053
06:09:36,680 --> 06:09:41,400
is wrap this around in a while loop so
10054
06:09:39,280 --> 06:09:43,160
this will try to do the code this
10055
06:09:41,400 --> 06:09:45,160
section will catch them all and then we
10056
06:09:43,160 --> 06:09:46,760
simply have a good buy at the end but
10057
06:09:45,160 --> 06:09:49,280
let's just make sure that this actually
10058
06:09:46,760 --> 06:09:51,878
runs because if we type in
10059
06:09:49,280 --> 06:09:54,000
15.5 then the user still hasn't entered
10060
06:09:51,878 --> 06:09:56,320
the number but our program was closed so
10061
06:09:54,000 --> 06:09:58,400
we want to loop around until the user
10062
06:09:56,320 --> 06:10:01,600
has successfully entered a correct
10063
06:09:58,400 --> 06:10:04,080
number so we can say bull looping equals
10064
06:10:01,600 --> 06:10:06,958
true and we can have while the cord is
10065
06:10:04,080 --> 06:10:09,638
looping that off the curly braces let's
10066
06:10:06,958 --> 06:10:11,638
place our entire Tri cat inside here if
10067
06:10:09,638 --> 06:10:14,120
we highlight it all hold alt and press
10068
06:10:11,638 --> 06:10:16,160
the op power key and then tab it once
10069
06:10:14,120 --> 06:10:18,680
and then get rid of this empty line so
10070
06:10:16,160 --> 06:10:20,958
now we'll keep looping around here until
10071
06:10:18,680 --> 06:10:22,478
this is going to be a different value so
10072
06:10:20,958 --> 06:10:24,520
because the while loop will only keep
10073
06:10:22,478 --> 06:10:26,440
looping until this is true and the
10074
06:10:24,520 --> 06:10:28,920
moment when it's false is when it will
10075
06:10:26,440 --> 06:10:31,120
cancel the W Loop so as I said before
10076
06:10:28,920 --> 06:10:33,000
when we run this code and then just at
10077
06:10:31,120 --> 06:10:34,958
this specific line if the exception
10078
06:10:33,000 --> 06:10:37,478
occurs it will actually skip this line
10079
06:10:34,958 --> 06:10:39,920
and jumped below here so if it skip this
10080
06:10:37,478 --> 06:10:41,958
line then what we can actually do is we
10081
06:10:39,920 --> 06:10:44,000
know if it reaches this line then the
10082
06:10:41,958 --> 06:10:45,760
conversion has been successful so we can
10083
06:10:44,000 --> 06:10:48,040
print out to the screen and we can say
10084
06:10:45,760 --> 06:10:50,718
looping equals false and we don't need
10085
06:10:48,040 --> 06:10:52,958
to place looping is equal to True inside
10086
06:10:50,718 --> 06:10:55,160
all of these three paths because if we
10087
06:10:52,958 --> 06:10:56,760
don't assign it to true then it's still
10088
06:10:55,160 --> 06:10:58,920
going to be the value false no matter
10089
06:10:56,760 --> 06:11:01,080
how many times you loop around so let's
10090
06:10:58,920 --> 06:11:04,920
try and run that
10091
06:11:01,080 --> 06:11:06,760
now enter a number 15.5 enter a number
10092
06:11:04,920 --> 06:11:08,760
15
10093
06:11:06,760 --> 06:11:10,240
and we can keep doing this forever as
10094
06:11:08,760 --> 06:11:12,440
it's a while loop and as soon as we
10095
06:11:10,240 --> 06:11:14,120
enter a valid character 20 and now it
10096
06:11:12,440 --> 06:11:15,360
says goodbye because we hit the end of
10097
06:11:14,120 --> 06:11:17,638
the
10098
06:11:15,360 --> 06:11:20,040
program so that's it for this video but
10099
06:11:17,638 --> 06:11:22,120
let's just have a little recap so we've
10100
06:11:20,040 --> 06:11:24,520
determined when we run our code just
10101
06:11:22,120 --> 06:11:26,440
these two lines that in both situations
10102
06:11:24,520 --> 06:11:28,760
we can have a format exception and an
10103
06:11:26,440 --> 06:11:30,840
overflow exception we can use these to
10104
06:11:28,760 --> 06:11:33,000
catch them specific exceptions and then
10105
06:11:30,840 --> 06:11:34,798
have a catch all at the end the reason
10106
06:11:33,000 --> 06:11:36,638
why you want to catch multiple is you
10107
06:11:34,798 --> 06:11:38,440
can have a very specific message
10108
06:11:36,638 --> 06:11:40,798
overflow dictates we have a number
10109
06:11:38,440 --> 06:11:42,680
that's too big or too small to fit
10110
06:11:40,798 --> 06:11:45,000
inside of an integer it can be less than
10111
06:11:42,680 --> 06:11:46,760
negative2 billion or can be greater than
10112
06:11:45,000 --> 06:11:49,000
2 billion and that's how you get the
10113
06:11:46,760 --> 06:11:50,400
Overflow exception we get him a format
10114
06:11:49,000 --> 06:11:52,040
exception if you entered something
10115
06:11:50,400 --> 06:11:54,000
that's literally not a number or
10116
06:11:52,040 --> 06:11:55,840
something that has decimal places and
10117
06:11:54,000 --> 06:11:57,920
then our catch all can be if anything
10118
06:11:55,840 --> 06:12:00,638
else goes wrong we can output a generic
10119
06:11:57,920 --> 06:12:03,080
message by 99% of the cases you'll
10120
06:12:00,638 --> 06:12:05,160
either enter number too big or too small
10121
06:12:03,080 --> 06:12:06,878
or it in the wrong format and this AC
10122
06:12:05,160 --> 06:12:09,120
ction is just so it catches everything
10123
06:12:06,878 --> 06:12:10,360
else should anything else happen so at
10124
06:12:09,120 --> 06:12:11,958
this point even though our program
10125
06:12:10,360 --> 06:12:13,920
doesn't actually close and there's no
10126
06:12:11,958 --> 06:12:15,878
exceptions the user hasn't had a chance
10127
06:12:13,920 --> 06:12:17,958
to enter the number again so we use this
10128
06:12:15,878 --> 06:12:20,120
inside a while loop and we can say while
10129
06:12:17,958 --> 06:12:21,920
looping is true when we hit a conversion
10130
06:12:20,120 --> 06:12:23,718
that's successful we say looping is
10131
06:12:21,920 --> 06:12:25,798
equal to false and once it hits this
10132
06:12:23,718 --> 06:12:27,680
line the Y Loop will then exit and then
10133
06:12:25,798 --> 06:12:30,000
we'll get our goodbye message and we
10134
06:12:27,680 --> 06:12:32,478
wrap the entire try catch inside the W
10135
06:12:30,000 --> 06:12:35,280
Loop just so this keeps on looping until
10136
06:12:32,478 --> 06:12:37,440
the user has typed in a successful value
10137
06:12:35,280 --> 06:12:41,798
as demonstrated
10138
06:12:37,440 --> 06:12:45,120
above we can type all this and 15.2 15
10139
06:12:41,798 --> 06:12:47,320
Point 15 and then finally says 15 and
10140
06:12:45,120 --> 06:12:47,320
then
10141
06:12:47,520 --> 06:12:51,718
goodbye so I want to explore different
10142
06:12:49,760 --> 06:12:53,558
ways you can output messages inside this
10143
06:12:51,718 --> 06:12:55,360
catch block when you have a catch
10144
06:12:53,558 --> 06:12:57,440
exception this is actually a parameter
10145
06:12:55,360 --> 06:12:59,360
that we're passing in this parameter can
10146
06:12:57,440 --> 06:13:01,638
actually have a variable as well
10147
06:12:59,360 --> 06:13:06,360
normally when you specify parameters in
10148
06:13:01,638 --> 06:13:08,320
a function form you say in and then some
10149
06:13:06,360 --> 06:13:10,558
something whereas right now we're kind
10150
06:13:08,320 --> 06:13:13,080
of defining the data type which is our
10151
06:13:10,558 --> 06:13:14,360
exception in the same case as int here
10152
06:13:13,080 --> 06:13:16,000
but we're actually not giving it a name
10153
06:13:14,360 --> 06:13:17,600
so it looks something like this of
10154
06:13:16,000 --> 06:13:19,400
course when we do this inside a function
10155
06:13:17,600 --> 06:13:21,080
we're going to get a syntax error but
10156
06:13:19,400 --> 06:13:22,798
for a catch we're not going to get any
10157
06:13:21,080 --> 06:13:24,878
errors at all and the reason is because
10158
06:13:22,798 --> 06:13:26,878
we can catch without having to use a
10159
06:13:24,878 --> 06:13:29,280
local variable so if we actually add a
10160
06:13:26,878 --> 06:13:31,080
local variable make you call it e for
10161
06:13:29,280 --> 06:13:33,400
exception then we actually have lots of
10162
06:13:31,080 --> 06:13:35,400
options inside here Type e then we have
10163
06:13:33,400 --> 06:13:37,120
lots of data and the stack traces very
10164
06:13:35,400 --> 06:13:38,798
helpful when you debuging but we'll
10165
06:13:37,120 --> 06:13:40,400
cover that at later date the thing I
10166
06:13:38,798 --> 06:13:42,798
want to focus on right now is the
10167
06:13:40,400 --> 06:13:46,320
message so we can use this and print out
10168
06:13:42,798 --> 06:13:49,200
to the screen so we can say error callon
10169
06:13:46,320 --> 06:13:51,718
and then type in E message and let let
10170
06:13:49,200 --> 06:13:54,120
run that now if you enter something
10171
06:13:51,718 --> 06:13:56,638
invalid input string was not in a
10172
06:13:54,120 --> 06:13:59,160
correct format if you enter a very big
10173
06:13:56,638 --> 06:14:01,440
number then values either too large or
10174
06:13:59,160 --> 06:14:02,760
too small for an in32 as you can see
10175
06:14:01,440 --> 06:14:04,798
these error messag are pretty
10176
06:14:02,760 --> 06:14:06,958
descriptive and normal users should be
10177
06:14:04,798 --> 06:14:08,440
able to read this and understand it of
10178
06:14:06,958 --> 06:14:10,600
course if you want this error message to
10179
06:14:08,440 --> 06:14:12,798
be more specific then you can add some
10180
06:14:10,600 --> 06:14:14,040
more catches inside here and we can have
10181
06:14:12,798 --> 06:14:16,160
format
10182
06:14:14,040 --> 06:14:17,920
exception this will be if you type in
10183
06:14:16,160 --> 06:14:20,040
something other than number but if you
10184
06:14:17,920 --> 06:14:21,440
do exactly the same thing in here then
10185
06:14:20,040 --> 06:14:23,760
it's kind of redundant because you're
10186
06:14:21,440 --> 06:14:25,160
just outputting exactly the same value
10187
06:14:23,760 --> 06:14:26,400
if we try and put the square brackets
10188
06:14:25,160 --> 06:14:28,520
again then we're going to get this
10189
06:14:26,400 --> 06:14:30,798
string which is actually outputed from
10190
06:14:28,520 --> 06:14:32,760
this line but even if we include the
10191
06:14:30,798 --> 06:14:35,520
catch all exception you get exactly the
10192
06:14:32,760 --> 06:14:37,798
same error message outputed from here
10193
06:14:35,520 --> 06:14:39,638
the moral of the story is you should
10194
06:14:37,798 --> 06:14:41,920
always have a catch all right at the end
10195
06:14:39,638 --> 06:14:43,798
of your chain but if the only thing
10196
06:14:41,920 --> 06:14:45,638
you're going to do inside here is simply
10197
06:14:43,798 --> 06:14:47,958
output this message to the screen then
10198
06:14:45,638 --> 06:14:49,400
you don't need any multiple catches what
10199
06:14:47,958 --> 06:14:50,680
you should do is catch multiple
10200
06:14:49,400 --> 06:14:52,478
exceptions that you know are going to
10201
06:14:50,680 --> 06:14:54,478
get fired in your code and then you want
10202
06:14:52,478 --> 06:14:55,878
to handle them and then have a catch all
10203
06:14:54,478 --> 06:14:57,478
at the bottom that will display the
10204
06:14:55,878 --> 06:14:59,040
message generically instead of
10205
06:14:57,478 --> 06:15:00,520
displaying the message right here you
10206
06:14:59,040 --> 06:15:01,958
can actually have a more specific
10207
06:15:00,520 --> 06:15:05,478
message like we covered in the last
10208
06:15:01,958 --> 06:15:07,520
video so this could be please only enter
10209
06:15:05,478 --> 06:15:09,400
numbers and we don't actually need the
10210
06:15:07,520 --> 06:15:10,798
message in this case because we know
10211
06:15:09,400 --> 06:15:13,320
what exception we're going to catch
10212
06:15:10,798 --> 06:15:15,160
because we've specified formal exception
10213
06:15:13,320 --> 06:15:17,280
another little tip if you hover over the
10214
06:15:15,160 --> 06:15:19,360
function call it says here format
10215
06:15:17,280 --> 06:15:20,760
exception and overfall exception which
10216
06:15:19,360 --> 06:15:23,120
are the two that we handled in the
10217
06:15:20,760 --> 06:15:24,600
previous video this is very easy and
10218
06:15:23,120 --> 06:15:26,600
understandable if you can just read the
10219
06:15:24,600 --> 06:15:28,440
values from here and to see what
10220
06:15:26,600 --> 06:15:30,360
exceptions can get raised and the same
10221
06:15:28,440 --> 06:15:32,600
thing here you get an IO exception for
10222
06:15:30,360 --> 06:15:34,478
the console right line the console right
10223
06:15:32,600 --> 06:15:36,718
can also give back an IO exception as
10224
06:15:34,478 --> 06:15:38,798
well generally speaking these console
10225
06:15:36,718 --> 06:15:41,120
functions almost never return back
10226
06:15:38,798 --> 06:15:42,920
exceptions I don't think I've ever hit a
10227
06:15:41,120 --> 06:15:44,680
situation where this exception has been
10228
06:15:42,920 --> 06:15:46,798
returned unless you do something
10229
06:15:44,680 --> 06:15:48,520
ridiculous and maybe you type in way too
10230
06:15:46,798 --> 06:15:51,080
many values that the console right line
10231
06:15:48,520 --> 06:15:52,920
can handle let's try an example and we
10232
06:15:51,080 --> 06:15:54,400
can do something like this if you enter
10233
06:15:52,920 --> 06:15:56,120
a very big number that can't be
10234
06:15:54,400 --> 06:15:58,520
specified inside the integer then
10235
06:15:56,120 --> 06:16:00,478
console right line has so many overloads
10236
06:15:58,520 --> 06:16:02,718
that actually processes it as a long
10237
06:16:00,478 --> 06:16:04,400
right now so you almost can't break this
10238
06:16:02,718 --> 06:16:06,840
unless something fundamental to your
10239
06:16:04,400 --> 06:16:09,240
computer actually happens so you have
10240
06:16:06,840 --> 06:16:10,760
exception e and with exception e you can
10241
06:16:09,240 --> 06:16:12,478
actually take this local variable and
10242
06:16:10,760 --> 06:16:14,320
print out the message this is the
10243
06:16:12,478 --> 06:16:16,400
preferred pattern because instead of
10244
06:16:14,320 --> 06:16:18,280
outputting here something went wrong
10245
06:16:16,400 --> 06:16:19,718
just very generically you can actually
10246
06:16:18,280 --> 06:16:21,400
give them the error message that comes
10247
06:16:19,718 --> 06:16:23,558
back from the exception the reason why
10248
06:16:21,400 --> 06:16:25,840
this is helpful is because if it doesn't
10249
06:16:23,558 --> 06:16:27,718
hit any of these exceptions and it just
10250
06:16:25,840 --> 06:16:29,520
happens to fall into the catch all at
10251
06:16:27,718 --> 06:16:31,080
least the user has an idea of what the
10252
06:16:29,520 --> 06:16:32,638
error message is going to be the
10253
06:16:31,080 --> 06:16:34,120
preferred option is to try get to a
10254
06:16:32,638 --> 06:16:36,920
point where you never have to reach the
10255
06:16:34,120 --> 06:16:39,040
catch all and try chain as many catches
10256
06:16:36,920 --> 06:16:40,638
as you can in order to handle as many
10257
06:16:39,040 --> 06:16:42,878
exceptions based on the code that you're
10258
06:16:40,638 --> 06:16:44,280
writing which can be found if you hover
10259
06:16:42,878 --> 06:16:46,400
over the function and read the
10260
06:16:44,280 --> 06:16:47,920
exceptions once you've covered as many
10261
06:16:46,400 --> 06:16:49,798
options as you can throughout the
10262
06:16:47,920 --> 06:16:51,400
catches then you almost never have to
10263
06:16:49,798 --> 06:16:53,600
worry about the code entering this
10264
06:16:51,400 --> 06:16:55,718
section because we've covered both the
10265
06:16:53,600 --> 06:16:58,160
exceptions in format and overflow and we
10266
06:16:55,718 --> 06:17:00,878
can do the Overflow right now please
10267
06:16:58,160 --> 06:17:02,878
enter a value under 2
10268
06:17:00,878 --> 06:17:04,680
billion then that means we've actually
10269
06:17:02,878 --> 06:17:07,440
covered all of the catches that are to
10270
06:17:04,680 --> 06:17:09,440
do with converting an integer 32 covered
10271
06:17:07,440 --> 06:17:11,160
format and covered overflow so when we
10272
06:17:09,440 --> 06:17:12,878
run this code the only two things that
10273
06:17:11,160 --> 06:17:15,200
can possibly go wrong are these two
10274
06:17:12,878 --> 06:17:17,240
functions the format exception overflow
10275
06:17:15,200 --> 06:17:19,400
exception and we won't ever hit the
10276
06:17:17,240 --> 06:17:21,280
catch all in this case but if anything
10277
06:17:19,400 --> 06:17:23,320
goes wrong at least we have a belt and
10278
06:17:21,280 --> 06:17:25,160
Brace option to catch it and we can tell
10279
06:17:23,320 --> 06:17:27,160
the user what happened and because it's
10280
06:17:25,160 --> 06:17:28,878
e. message then the value is going to be
10281
06:17:27,160 --> 06:17:31,680
quite descriptive so the user can read
10282
06:17:28,878 --> 06:17:31,680
it without any
10283
06:17:31,878 --> 06:17:36,160
problems so in this task we want to be
10284
06:17:34,000 --> 06:17:37,600
able to convert a string to an INT
10285
06:17:36,160 --> 06:17:39,360
successfully without any errors
10286
06:17:37,600 --> 06:17:41,840
occurring and without any exceptions
10287
06:17:39,360 --> 06:17:43,240
that could break our code so in steps we
10288
06:17:41,840 --> 06:17:45,240
want to create an INT and try and
10289
06:17:43,240 --> 06:17:47,320
convert any string from the console to
10290
06:17:45,240 --> 06:17:49,160
an integer notice the error that appears
10291
06:17:47,320 --> 06:17:51,040
when you try to do that write a try
10292
06:17:49,160 --> 06:17:52,958
catch handle around it to successfully
10293
06:17:51,040 --> 06:17:55,120
operate that and output the message to
10294
06:17:52,958 --> 06:17:57,280
the screen and you won't need to change
10295
06:17:55,120 --> 06:17:59,920
the console out print and the convert
10296
06:17:57,280 --> 06:18:01,600
in32 to be able to make this work so you
10297
06:17:59,920 --> 06:18:03,478
don't want to change the current code
10298
06:18:01,600 --> 06:18:06,120
you just want to add the triun Handler
10299
06:18:03,478 --> 06:18:08,120
around it so why is a bad situation how
10300
06:18:06,120 --> 06:18:09,558
can we know if it's been converted this
10301
06:18:08,120 --> 06:18:10,878
is just a question that you should keep
10302
06:18:09,558 --> 06:18:13,120
at the back of your head and we'll
10303
06:18:10,878 --> 06:18:14,798
answer this shortly and then finally we
10304
06:18:13,120 --> 06:18:16,840
want to create a custom tri pass
10305
06:18:14,798 --> 06:18:18,440
function find the real trip pass
10306
06:18:16,840 --> 06:18:20,400
function and copy the return Titan
10307
06:18:18,440 --> 06:18:22,440
parameters and see if you can mimic Its
10308
06:18:20,400 --> 06:18:24,558
Behavior read the tool tip it gives you
10309
06:18:22,440 --> 06:18:26,478
to give you ideas on what to do so let's
10310
06:18:24,558 --> 06:18:29,080
take these in steps first we can have a
10311
06:18:26,478 --> 06:18:33,280
console right and say enter a number and
10312
06:18:29,080 --> 06:18:36,280
then Inn equals convert to in32 and then
10313
06:18:33,280 --> 06:18:38,798
we need a console read line
10314
06:18:36,280 --> 06:18:41,160
and then if you run the
10315
06:18:38,798 --> 06:18:44,718
code if we try and just convert some
10316
06:18:41,160 --> 06:18:46,600
square brackets then we get a format
10317
06:18:44,718 --> 06:18:48,718
exception so that's the exception that
10318
06:18:46,600 --> 06:18:50,360
we're going to be handling right now so
10319
06:18:48,718 --> 06:18:51,520
we can take this code and we can wrap it
10320
06:18:50,360 --> 06:18:55,000
around and a
10321
06:18:51,520 --> 06:18:56,638
try use alt and up Arrow key and then
10322
06:18:55,000 --> 06:18:58,920
just after do a catch and then we want
10323
06:18:56,638 --> 06:19:00,958
to do a format exception and then space
10324
06:18:58,920 --> 06:19:03,080
e to give it a variable and then we do
10325
06:19:00,958 --> 06:19:06,160
e. message out to the screen and now
10326
06:19:03,080 --> 06:19:08,120
let's run the code square brackets input
10327
06:19:06,160 --> 06:19:09,920
string was not in a correct format so we
10328
06:19:08,120 --> 06:19:11,360
get our message and our exception has
10329
06:19:09,920 --> 06:19:13,440
been successfully handled and our
10330
06:19:11,360 --> 06:19:15,680
program hasn't crutched but the problem
10331
06:19:13,440 --> 06:19:17,160
is is has got to this console read line
10332
06:19:15,680 --> 06:19:18,760
and the user hasn't had a chance to
10333
06:19:17,160 --> 06:19:21,040
actually enter a number so what we can
10334
06:19:18,760 --> 06:19:23,160
do in here to ensure the user always has
10335
06:19:21,040 --> 06:19:26,320
access to writing a number is we can say
10336
06:19:23,160 --> 06:19:28,400
something like this bu success equals
10337
06:19:26,320 --> 06:19:30,600
false and then just after here we can
10338
06:19:28,400 --> 06:19:33,040
say success equals the
10339
06:19:30,600 --> 06:19:35,680
true and just down here we can say
10340
06:19:33,040 --> 06:19:40,160
console right line so success question
10341
06:19:35,680 --> 06:19:43,040
mark yay and then call on or
10342
06:19:40,160 --> 06:19:45,760
no so if it converts it 15 we should get
10343
06:19:43,040 --> 06:19:47,558
a yay and if we convert it and it fails
10344
06:19:45,760 --> 06:19:48,200
then we should get an o no along with
10345
06:19:47,558 --> 06:19:50,240
the
10346
06:19:48,200 --> 06:19:52,360
message of course what you can also do
10347
06:19:50,240 --> 06:19:53,760
is wrap this entire section inside a
10348
06:19:52,360 --> 06:19:55,680
while loop what we've already covered
10349
06:19:53,760 --> 06:19:57,718
that inside the tutorial and that means
10350
06:19:55,680 --> 06:19:59,878
it'll keep repeating this until this is
10351
06:19:57,718 --> 06:20:01,360
set to true and therefore exit the while
10352
06:19:59,878 --> 06:20:03,400
loop and then will the users finally
10353
06:20:01,360 --> 06:20:04,878
typed in a number so let's go back so
10354
06:20:03,400 --> 06:20:06,760
we've noticed an error and we've brought
10355
06:20:04,878 --> 06:20:08,120
a TR catch handle around it now we want
10356
06:20:06,760 --> 06:20:09,680
to catch the error and output the error
10357
06:20:08,120 --> 06:20:11,280
message with through done and then we
10358
06:20:09,680 --> 06:20:13,718
haven't changed the current code which
10359
06:20:11,280 --> 06:20:15,478
was these two lines we've added a booing
10360
06:20:13,718 --> 06:20:17,360
and set it to True here just so we can
10361
06:20:15,478 --> 06:20:19,280
have a bit of an output message before
10362
06:20:17,360 --> 06:20:21,160
so why is this a bad situation how can
10363
06:20:19,280 --> 06:20:23,320
we know if it's being converted so this
10364
06:20:21,160 --> 06:20:25,320
code is a bad situation because we just
10365
06:20:23,320 --> 06:20:27,000
have these two lines outputed and then
10366
06:20:25,320 --> 06:20:28,520
now that we've added our booing to tell
10367
06:20:27,000 --> 06:20:30,120
the user whether it's been successful or
10368
06:20:28,520 --> 06:20:32,000
not is actually making it a bit more
10369
06:20:30,120 --> 06:20:33,878
meaningful the problem is even if we
10370
06:20:32,000 --> 06:20:35,520
print or no then the user doesn't really
10371
06:20:33,878 --> 06:20:37,080
have a chance to enter it again so like
10372
06:20:35,520 --> 06:20:38,878
I said before you could loop around and
10373
06:20:37,080 --> 06:20:41,000
use a while loop to make sure that this
10374
06:20:38,878 --> 06:20:43,200
situation can go from something that
10375
06:20:41,000 --> 06:20:44,958
gets handled into a situation where the
10376
06:20:43,200 --> 06:20:46,958
user can actually type it in there's no
10377
06:20:44,958 --> 06:20:48,240
point hand on an exception and realizing
10378
06:20:46,958 --> 06:20:50,680
there's an error if you don't give the
10379
06:20:48,240 --> 06:20:53,240
user a chance to enter another value if
10380
06:20:50,680 --> 06:20:55,280
they enter out 15 followed by full stop
10381
06:20:53,240 --> 06:20:56,878
when they only meant to type 15 then you
10382
06:20:55,280 --> 06:20:59,080
need to give them another chance to type
10383
06:20:56,878 --> 06:21:00,520
in that 15 value otherwise the program
10384
06:20:59,080 --> 06:21:03,280
is simply just going to end and
10385
06:21:00,520 --> 06:21:05,920
nothing's going to happen for example 15
10386
06:21:03,280 --> 06:21:07,320
DOT it says or no or you can have
10387
06:21:05,920 --> 06:21:09,320
another message that says something went
10388
06:21:07,320 --> 06:21:11,080
wrong or you can have more specific
10389
06:21:09,320 --> 06:21:12,718
message about the conversion and then
10390
06:21:11,080 --> 06:21:14,558
once you press enter the program simply
10391
06:21:12,718 --> 06:21:16,240
closes this is not an interaction that
10392
06:21:14,558 --> 06:21:18,440
you want you want the US it to be able
10393
06:21:16,240 --> 06:21:20,280
to reselect another value so that's why
10394
06:21:18,440 --> 06:21:22,200
it's a bad situation because you know
10395
06:21:20,280 --> 06:21:23,958
it's being converted or not heuse now
10396
06:21:22,200 --> 06:21:26,200
build success but the problem is we
10397
06:21:23,958 --> 06:21:28,280
don't have any sort of looping so let's
10398
06:21:26,200 --> 06:21:29,920
have a look so now let's comment out
10399
06:21:28,280 --> 06:21:32,718
this and let's see how we can make our
10400
06:21:29,920 --> 06:21:35,718
custom trip pass so you type it in. trip
10401
06:21:32,718 --> 06:21:38,160
pass you can see they have a bu inpass
10402
06:21:35,718 --> 06:21:39,920
string s and out in result so let's try
10403
06:21:38,160 --> 06:21:42,320
and copy that and make our own custom
10404
06:21:39,920 --> 06:21:46,080
function so we say static Bull and it's
10405
06:21:42,320 --> 06:21:49,160
Tri pass and then we have a string input
10406
06:21:46,080 --> 06:21:52,000
and out in
10407
06:21:49,160 --> 06:21:53,080
result perfect so now we can get rid of
10408
06:21:52,000 --> 06:21:54,798
this line just so we use it for
10409
06:21:53,080 --> 06:21:55,718
reference and let's use it straight in
10410
06:21:54,798 --> 06:21:57,760
here and then we can write the
10411
06:21:55,718 --> 06:22:00,400
implementation in a second so we can say
10412
06:21:57,760 --> 06:22:03,478
if trass the thing that we want to pass
10413
06:22:00,400 --> 06:22:06,520
is actually a console read line and then
10414
06:22:03,478 --> 06:22:08,600
we just say out into
10415
06:22:06,520 --> 06:22:10,240
result and the console read line is
10416
06:22:08,600 --> 06:22:12,200
going to be answered to this question
10417
06:22:10,240 --> 06:22:13,920
which is enter a number you place that
10418
06:22:12,200 --> 06:22:16,320
straight inside here and then we say out
10419
06:22:13,920 --> 06:22:19,120
in result so if this has been successful
10420
06:22:16,320 --> 06:22:20,920
we can print out yay again and else if
10421
06:22:19,120 --> 06:22:22,680
it's not successful we can print out or
10422
06:22:20,920 --> 06:22:24,478
not of course you can do other things
10423
06:22:22,680 --> 06:22:26,320
inside here or you can even print out
10424
06:22:24,478 --> 06:22:29,200
the value straight from here so we can
10425
06:22:26,320 --> 06:22:30,760
say yay plus a space and then result
10426
06:22:29,200 --> 06:22:32,400
just so we can get that back to the user
10427
06:22:30,760 --> 06:22:33,878
and then oh know if something goes wrong
10428
06:22:32,400 --> 06:22:36,280
of course this is just so we can test
10429
06:22:33,878 --> 06:22:38,600
things and see how they work so this Tri
10430
06:22:36,280 --> 06:22:40,520
pass function it will take in a string
10431
06:22:38,600 --> 06:22:42,240
input and it'll try and give us back the
10432
06:22:40,520 --> 06:22:44,280
result so in order to be able to make
10433
06:22:42,240 --> 06:22:45,798
this successful function what we need to
10434
06:22:44,280 --> 06:22:49,280
do is essentially do the conversion
10435
06:22:45,798 --> 06:22:51,360
ourself which will be this convert in 32
10436
06:22:49,280 --> 06:22:53,958
so first we need to assign this out int
10437
06:22:51,360 --> 06:22:55,080
so we can say a result equals minus1 so
10438
06:22:53,958 --> 06:22:56,920
now that we've used that we actually
10439
06:22:55,080 --> 06:22:58,600
need to have a return type as well but
10440
06:22:56,920 --> 06:23:00,360
let's worry about that in a second so
10441
06:22:58,600 --> 06:23:03,000
essentially what we actually want to do
10442
06:23:00,360 --> 06:23:05,240
is say that the result is equal to
10443
06:23:03,000 --> 06:23:07,360
convert to in 32 and then we want to
10444
06:23:05,240 --> 06:23:09,440
convert the input store it inside the
10445
06:23:07,360 --> 06:23:11,080
result and if this goes successful then
10446
06:23:09,440 --> 06:23:13,160
we want to return true and if it doesn't
10447
06:23:11,080 --> 06:23:15,000
go successful then we want to return
10448
06:23:13,160 --> 06:23:17,160
false but of course we need to be able
10449
06:23:15,000 --> 06:23:19,280
to do these things separately so like we
10450
06:23:17,160 --> 06:23:20,798
said with the try you only wrap around
10451
06:23:19,280 --> 06:23:22,878
the section of which is going to be
10452
06:23:20,798 --> 06:23:24,920
problematic so in this case as we've
10453
06:23:22,878 --> 06:23:27,160
done here we've wrapped the convert in
10454
06:23:24,920 --> 06:23:29,120
32 inside the try so we're going to need
10455
06:23:27,160 --> 06:23:31,320
to do that down here as well so we can
10456
06:23:29,120 --> 06:23:32,798
open the try right here and place these
10457
06:23:31,320 --> 06:23:35,400
two lines
10458
06:23:32,798 --> 06:23:37,080
inside and then have a catch have a
10459
06:23:35,400 --> 06:23:39,400
little think about what sort of catch do
10460
06:23:37,080 --> 06:23:41,878
we need the whole point in this function
10461
06:23:39,400 --> 06:23:43,878
is to Simply convert value and return
10462
06:23:41,878 --> 06:23:46,400
back whether it's been successful or not
10463
06:23:43,878 --> 06:23:49,160
there is no messages here we just need
10464
06:23:46,400 --> 06:23:50,798
has it been successful yes or no and
10465
06:23:49,160 --> 06:23:52,760
because we only need to know if it's a
10466
06:23:50,798 --> 06:23:54,638
yes or a no then we can just have a
10467
06:23:52,760 --> 06:23:56,160
generic exception and we don't need the
10468
06:23:54,638 --> 06:23:57,638
E variable because we're not going to
10469
06:23:56,160 --> 06:23:59,160
access the message we're going to keep
10470
06:23:57,638 --> 06:24:00,920
it as an exception and instead of
10471
06:23:59,160 --> 06:24:02,200
returning false down here we actually
10472
06:24:00,920 --> 06:24:04,958
want to do this straight inside the
10473
06:24:02,200 --> 06:24:06,840
catch so now the code reads like this
10474
06:24:04,958 --> 06:24:08,478
we set the result to minus one and the
10475
06:24:06,840 --> 06:24:10,120
reason why we do that is because if we
10476
06:24:08,478 --> 06:24:12,080
don't then we're going to get a compiler
10477
06:24:10,120 --> 06:24:13,440
error and it says that not all paths
10478
06:24:12,080 --> 06:24:15,360
will going to assign result
10479
06:24:13,440 --> 06:24:17,400
alternatively we can also assign result
10480
06:24:15,360 --> 06:24:19,200
straight inside here just so we have an
10481
06:24:17,400 --> 06:24:21,160
assignment in both paths or you could
10482
06:24:19,200 --> 06:24:23,040
just leave it outside at the top so we
10483
06:24:21,160 --> 06:24:24,718
assign result to minus one and then we
10484
06:24:23,040 --> 06:24:26,718
place our problematic code straight
10485
06:24:24,718 --> 06:24:28,718
inside here we try and convert the
10486
06:24:26,718 --> 06:24:30,558
result and if this line returns back
10487
06:24:28,718 --> 06:24:32,680
successful then it's going to execute
10488
06:24:30,558 --> 06:24:34,680
the second line and return back true if
10489
06:24:32,680 --> 06:24:36,718
this line returns back an exception it's
10490
06:24:34,680 --> 06:24:38,120
going to miss this line completely and
10491
06:24:36,718 --> 06:24:40,478
jump straight into the exception and
10492
06:24:38,120 --> 06:24:42,558
then return us back with false so let's
10493
06:24:40,478 --> 06:24:48,360
actually try this and see what happens
10494
06:24:42,558 --> 06:24:49,600
15 DOT or no and then 15 y 15 so the
10495
06:24:48,360 --> 06:24:51,320
reason why we don't care about the
10496
06:24:49,600 --> 06:24:53,040
exception in here we can add in
10497
06:24:51,320 --> 06:24:55,320
different values as we've used before
10498
06:24:53,040 --> 06:24:57,878
with format exception overflow exception
10499
06:24:55,320 --> 06:24:59,760
but as we saw with trass it simply just
10500
06:24:57,878 --> 06:25:01,760
returns us back with the bull the bull
10501
06:24:59,760 --> 06:25:04,120
simply tells us if it's been successful
10502
06:25:01,760 --> 06:25:06,240
or not so we could stack these multiple
10503
06:25:04,120 --> 06:25:08,320
times times we can say catch and then we
10504
06:25:06,240 --> 06:25:10,200
have the format exception and then we
10505
06:25:08,320 --> 06:25:11,440
can place a return false in there and
10506
06:25:10,200 --> 06:25:13,440
then we have another catch for the
10507
06:25:11,440 --> 06:25:15,360
Overflow exception and we can place
10508
06:25:13,440 --> 06:25:16,798
another return false in there but as you
10509
06:25:15,360 --> 06:25:18,718
can see we're just returning false
10510
06:25:16,798 --> 06:25:20,440
inside here so now that we've changed
10511
06:25:18,718 --> 06:25:22,400
these it's kind of pointless each of
10512
06:25:20,440 --> 06:25:24,360
them return back false and because we're
10513
06:25:22,400 --> 06:25:25,920
not doing anything very specific we
10514
06:25:24,360 --> 06:25:28,478
don't actually need to chain them all
10515
06:25:25,920 --> 06:25:30,558
together we don't care about the message
10516
06:25:28,478 --> 06:25:32,400
we don't care about exception we only
10517
06:25:30,558 --> 06:25:34,040
care about the fact that something has
10518
06:25:32,400 --> 06:25:36,360
went wrong and if something has went
10519
06:25:34,040 --> 06:25:37,958
wrong then we return back false if
10520
06:25:36,360 --> 06:25:39,878
everything goes okay then we return back
10521
06:25:37,958 --> 06:25:41,760
true and that's all this Tri pass
10522
06:25:39,878 --> 06:25:43,798
function is going to need to do it's
10523
06:25:41,760 --> 06:25:46,798
simply convert it or it doesn't convert
10524
06:25:43,798 --> 06:25:49,840
it using an input and outputting a
10525
06:25:46,798 --> 06:25:52,320
result so if you run it retype in a real
10526
06:25:49,840 --> 06:25:54,798
number we get a number back and then 15
10527
06:25:52,320 --> 06:25:56,920
DOT Z then we get all no because it
10528
06:25:54,798 --> 06:25:59,718
can't convert it there we go we've met
10529
06:25:56,920 --> 06:26:01,920
all our requirements for this
10530
06:25:59,718 --> 06:26:04,440
program let's have a look at how we can
10531
06:26:01,920 --> 06:26:06,400
do debugging using visual studio to to
10532
06:26:04,440 --> 06:26:08,520
step through our code manually so this
10533
06:26:06,400 --> 06:26:11,040
is the example I used last time int age
10534
06:26:08,520 --> 06:26:12,638
equal 35 and ideally in this case you'd
10535
06:26:11,040 --> 06:26:15,160
want something like 30 plus to be
10536
06:26:12,638 --> 06:26:17,280
printed instead of 18 plus even though
10537
06:26:15,160 --> 06:26:19,920
18 plus is still technically true we'd
10538
06:26:17,280 --> 06:26:22,638
still like 30 to be printed in this case
10539
06:26:19,920 --> 06:26:25,120
because this is if it's over 18 do 18 if
10540
06:26:22,638 --> 06:26:26,920
it's over 30 do 30 plus so in order to
10541
06:26:25,120 --> 06:26:28,920
start the debugging process you
10542
06:26:26,920 --> 06:26:31,200
shouldn't run the code first if you run
10543
06:26:28,920 --> 06:26:32,840
the code first it's going to execute so
10544
06:26:31,200 --> 06:26:34,638
fast that you won't get a chance to
10545
06:26:32,840 --> 06:26:36,718
actually put any code in and and try and
10546
06:26:34,638 --> 06:26:38,760
debug it there we go we've already hit
10547
06:26:36,718 --> 06:26:41,520
the console read line and it's executed
10548
06:26:38,760 --> 06:26:44,080
for a spit second so the best thing to
10549
06:26:41,520 --> 06:26:46,400
do just to the left of all the line
10550
06:26:44,080 --> 06:26:48,200
numbers you want to press in the Gap and
10551
06:26:46,400 --> 06:26:50,040
that will create a red dot and it'll
10552
06:26:48,200 --> 06:26:52,080
tell you that this is a debuging red dot
10553
06:26:50,040 --> 06:26:56,320
just in this little tool tip debugging
10554
06:26:52,080 --> 06:26:57,840
do program. men debuging program. men so
10555
06:26:56,320 --> 06:26:59,520
it tells us we're in the program class
10556
06:26:57,840 --> 06:27:01,878
and we'll going inside the main you can
10557
06:26:59,520 --> 06:27:03,840
also address the debug menu up here and
10558
06:27:01,878 --> 06:27:05,600
it says we can have a toggle breakpoint
10559
06:27:03,840 --> 06:27:08,160
we can add new one we can delete them
10560
06:27:05,600 --> 06:27:10,280
all or we can actually disable deleting
10561
06:27:08,160 --> 06:27:13,520
them will just simply get rid of them as
10562
06:27:10,280 --> 06:27:15,280
you can see here if I set a couple more
10563
06:27:13,520 --> 06:27:17,240
and press disable then it's going to
10564
06:27:15,280 --> 06:27:18,760
hide them so when I run our code it
10565
06:27:17,240 --> 06:27:20,558
doesn't actually apply the debugging but
10566
06:27:18,760 --> 06:27:22,680
it doesn't delete them and then we can
10567
06:27:20,558 --> 06:27:24,120
enable them and then we can also delete
10568
06:27:22,680 --> 06:27:25,360
them all and it gives you a little
10569
06:27:24,120 --> 06:27:27,200
prompt because if you're doing this
10570
06:27:25,360 --> 06:27:28,680
inside a big project and you just spent
10571
06:27:27,200 --> 06:27:30,520
a lot of time going through and
10572
06:27:28,680 --> 06:27:32,120
debugging certain functions then you
10573
06:27:30,520 --> 06:27:33,840
don't want to be able to delete them all
10574
06:27:32,120 --> 06:27:35,920
without any confirmation so to delete
10575
06:27:33,840 --> 06:27:38,080
them all and put it back on line 13
10576
06:27:35,920 --> 06:27:40,040
which is our int Edge so let's run the
10577
06:27:38,080 --> 06:27:42,240
code and see what happens so as you can
10578
06:27:40,040 --> 06:27:43,680
see it minimizes our console for us and
10579
06:27:42,240 --> 06:27:45,718
nothing's actually been printed yet
10580
06:27:43,680 --> 06:27:47,638
because we're only up to this line and
10581
06:27:45,718 --> 06:27:51,040
then we can use these buttons up here
10582
06:27:47,638 --> 06:27:53,320
step into goes inside a function step
10583
06:27:51,040 --> 06:27:56,080
over goes over the line so it will go
10584
06:27:53,320 --> 06:27:58,200
from this line to this line step out is
10585
06:27:56,080 --> 06:28:00,000
used after you've stepped in if you
10586
06:27:58,200 --> 06:28:01,160
stepped into a function and then you
10587
06:28:00,000 --> 06:28:03,440
don't want to see the rest of the
10588
06:28:01,160 --> 06:28:04,840
function executing you can step out to
10589
06:28:03,440 --> 06:28:06,718
jump back out of it which I'll
10590
06:28:04,840 --> 06:28:08,280
demonstrate later on in this video so
10591
06:28:06,718 --> 06:28:09,878
now that we've hit this line you can
10592
06:28:08,280 --> 06:28:12,400
actually see what's going on so we have
10593
06:28:09,878 --> 06:28:14,680
int age equals z because when this line
10594
06:28:12,400 --> 06:28:17,680
is highlighted it means this line hasn't
10595
06:28:14,680 --> 06:28:20,478
executed just yet so we can press this
10596
06:28:17,680 --> 06:28:22,958
or we can press F10 on our keyboard if I
10597
06:28:20,478 --> 06:28:25,240
press F10 you can now see int age has
10598
06:28:22,958 --> 06:28:26,920
the value 35 and what you can actually
10599
06:28:25,240 --> 06:28:28,958
do is double click on it and just change
10600
06:28:26,920 --> 06:28:30,878
the value to 30 for example now if you
10601
06:28:28,958 --> 06:28:32,440
highlight you can see the value is 30 so
10602
06:28:30,878 --> 06:28:34,440
you need to double click change the
10603
06:28:32,440 --> 06:28:36,718
value to something and then simply press
10604
06:28:34,440 --> 06:28:38,478
enter if you change the value but leave
10605
06:28:36,718 --> 06:28:40,000
the box like this then it won't change
10606
06:28:38,478 --> 06:28:42,200
the value so you have to remember to
10607
06:28:40,000 --> 06:28:43,920
press enter so now I've made the age 40
10608
06:28:42,200 --> 06:28:45,840
but let's just change it back to 35 to
10609
06:28:43,920 --> 06:28:47,600
keep it the same and then now what we
10610
06:28:45,840 --> 06:28:51,478
can do we can explore what's going on
10611
06:28:47,600 --> 06:28:52,878
here if age is greater than 18 then
10612
06:28:51,478 --> 06:28:54,840
we're going to check if this condition
10613
06:28:52,878 --> 06:28:56,798
is true or false so in order to check if
10614
06:28:54,840 --> 06:28:58,478
this returns back true or false you can
10615
06:28:56,798 --> 06:29:00,878
actually hover over the operator and it
10616
06:28:58,478 --> 06:29:03,680
says here AG is greater than 18 is equal
10617
06:29:00,878 --> 06:29:05,600
to true and if we hover over this one it
10618
06:29:03,680 --> 06:29:08,040
says AG is greater than 30 is equal to
10619
06:29:05,600 --> 06:29:10,080
two notice what we actually did there we
10620
06:29:08,040 --> 06:29:12,000
essentially ran this condition through
10621
06:29:10,080 --> 06:29:14,520
the debugger without actually reaching
10622
06:29:12,000 --> 06:29:16,478
this line we're only up to line 15 as of
10623
06:29:14,520 --> 06:29:18,680
right now but we've managed to execute
10624
06:29:16,478 --> 06:29:21,558
this and give us back a function so as
10625
06:29:18,680 --> 06:29:23,920
you can see age greater than 30 is true
10626
06:29:21,558 --> 06:29:26,200
and age greater than 18 is true and
10627
06:29:23,920 --> 06:29:28,440
because this is lsif chain it only cares
10628
06:29:26,200 --> 06:29:29,760
about the first one that turns back true
10629
06:29:28,440 --> 06:29:32,120
and because this one has turned back
10630
06:29:29,760 --> 06:29:33,798
true then if you press F10 again you'll
10631
06:29:32,120 --> 06:29:35,558
see that now it's went inside this curly
10632
06:29:33,798 --> 06:29:38,040
bra is which will print out the console
10633
06:29:35,558 --> 06:29:40,280
right line if I press F10 again you can
10634
06:29:38,040 --> 06:29:42,478
see we're now waiting on here if I use
10635
06:29:40,280 --> 06:29:44,240
alt tab I can get back to the console
10636
06:29:42,478 --> 06:29:46,638
you see that 18 plus has now been
10637
06:29:44,240 --> 06:29:48,680
printed now that we're waiting here we
10638
06:29:46,638 --> 06:29:50,440
actually press F10 one more time and
10639
06:29:48,680 --> 06:29:52,638
then it jump straight to this read line
10640
06:29:50,440 --> 06:29:54,798
of course because this is L if chain as
10641
06:29:52,638 --> 06:29:56,718
soon as one of these become true and it
10642
06:29:54,798 --> 06:29:59,040
jumps over the else if because of course
10643
06:29:56,718 --> 06:30:01,520
this is a chain the first one it hits
10644
06:29:59,040 --> 06:30:03,240
that's true it skips over the rest if we
10645
06:30:01,520 --> 06:30:05,120
didn't want this to be a chain then we
10646
06:30:03,240 --> 06:30:08,200
could keep them as both independent if
10647
06:30:05,120 --> 06:30:10,240
statements if age is greater than 18 and
10648
06:30:08,200 --> 06:30:12,440
if age is greater than 30 and don't use
10649
06:30:10,240 --> 06:30:13,920
the lse keyword but in this case because
10650
06:30:12,440 --> 06:30:15,958
you do want to Output a different
10651
06:30:13,920 --> 06:30:17,958
message for every single age range you
10652
06:30:15,958 --> 06:30:19,400
do want to place the L's word in there
10653
06:30:17,958 --> 06:30:20,120
and now that we've hit this line we can
10654
06:30:19,400 --> 06:30:22,160
press
10655
06:30:20,120 --> 06:30:25,040
F10 and then now we jump back in the
10656
06:30:22,160 --> 06:30:26,440
console and now we're waiting right here
10657
06:30:25,040 --> 06:30:28,280
and just a little tip when it comes to
10658
06:30:26,440 --> 06:30:30,200
debugging if at any point you just want
10659
06:30:28,280 --> 06:30:32,840
to exit the debuging you can either
10660
06:30:30,200 --> 06:30:35,680
press continue or just press F5 and the
10661
06:30:32,840 --> 06:30:37,760
program will run for example if we
10662
06:30:35,680 --> 06:30:40,478
investigate this variable and we can see
10663
06:30:37,760 --> 06:30:42,080
the value is 35 and then now we're happy
10664
06:30:40,478 --> 06:30:45,000
with the rest of the Coe we can just
10665
06:30:42,080 --> 06:30:46,798
press F5 and our code will run as normal
10666
06:30:45,000 --> 06:30:49,600
we'll get the out print and we'll get a
10667
06:30:46,798 --> 06:30:51,520
read line waiting here so we know our
10668
06:30:49,600 --> 06:30:53,680
problem here is both conditions return
10669
06:30:51,520 --> 06:30:55,680
back true so just like in the last video
10670
06:30:53,680 --> 06:30:57,360
we actually change this condition so to
10671
06:30:55,680 --> 06:30:59,160
make sure that it only goes between this
10672
06:30:57,360 --> 06:31:00,760
range but let's not repeat ourselves
10673
06:30:59,160 --> 06:31:03,718
let's have a look at a different example
10674
06:31:00,760 --> 06:31:05,798
so we can have string name equals Abu
10675
06:31:03,718 --> 06:31:07,878
and and then int age and then age equal
10676
06:31:05,798 --> 06:31:09,638
23 which I'll demonstrate why I've done
10677
06:31:07,878 --> 06:31:11,200
it like this in a second and then let's
10678
06:31:09,638 --> 06:31:13,558
have a function just so I can show you
10679
06:31:11,200 --> 06:31:17,920
how to step in so we can have a static
10680
06:31:13,558 --> 06:31:20,320
string print details string name in
10681
06:31:17,920 --> 06:31:22,040
Edge and here actually we want to just
10682
06:31:20,320 --> 06:31:23,840
return back a void because we just want
10683
06:31:22,040 --> 06:31:27,320
to print out to the screen and we can
10684
06:31:23,840 --> 06:31:28,878
say name space edge and maybe just put a
10685
06:31:27,320 --> 06:31:31,360
dash between them and let's run that
10686
06:31:28,878 --> 06:31:33,440
code now we can see
10687
06:31:31,360 --> 06:31:35,360
aa-23 of course making that function
10688
06:31:33,440 --> 06:31:37,240
wasn't the point I actually want to show
10689
06:31:35,360 --> 06:31:38,600
you what's going on so if we just print
10690
06:31:37,240 --> 06:31:40,558
this a couple of times just so we can
10691
06:31:38,600 --> 06:31:42,478
show you the step out and step in so if
10692
06:31:40,558 --> 06:31:45,000
you run the code now we can see our
10693
06:31:42,478 --> 06:31:46,840
string name if we go over that you can
10694
06:31:45,000 --> 06:31:48,360
see it's called abber and then now that
10695
06:31:46,840 --> 06:31:50,280
it's stepped over this line cuz there's
10696
06:31:48,360 --> 06:31:52,080
nothing really to execute here it's just
10697
06:31:50,280 --> 06:31:53,920
creating back a variable so you can see
10698
06:31:52,080 --> 06:31:56,080
that age is zero cuz that's the default
10699
06:31:53,920 --> 06:31:58,200
value for an integer and when we skip
10700
06:31:56,080 --> 06:32:00,000
over that now it's got the value of 23
10701
06:31:58,200 --> 06:32:02,600
so one line creates the integer with the
10702
06:32:00,000 --> 06:32:05,200
default value equal to zero and then the
10703
06:32:02,600 --> 06:32:06,878
next line will make it equal to 23 and
10704
06:32:05,200 --> 06:32:08,280
then we're passing on the variables here
10705
06:32:06,878 --> 06:32:09,920
and if you wanted to investigate it you
10706
06:32:08,280 --> 06:32:11,558
could simply just hover over them like
10707
06:32:09,920 --> 06:32:13,440
this so you can see what the values
10708
06:32:11,558 --> 06:32:16,080
contain and then now that we've hit this
10709
06:32:13,440 --> 06:32:17,558
function if we press F10 then it's just
10710
06:32:16,080 --> 06:32:20,200
going to skip over it but what we
10711
06:32:17,558 --> 06:32:22,478
actually want to do is be able to go
10712
06:32:20,200 --> 06:32:25,120
inside the function so this is where our
10713
06:32:22,478 --> 06:32:27,200
step in goes step over doesn't care if
10714
06:32:25,120 --> 06:32:29,360
it's a function or a normal line it will
10715
06:32:27,200 --> 06:32:31,680
just step over it regardless so we want
10716
06:32:29,360 --> 06:32:34,520
to in here we want to use step into or
10717
06:32:31,680 --> 06:32:36,638
we can press f11 once we press F 11 you
10718
06:32:34,520 --> 06:32:38,680
can see that now it's jumped from print
10719
06:32:36,638 --> 06:32:41,398
details up here to print details down
10720
06:32:38,680 --> 06:32:43,080
here and then now look if I step through
10721
06:32:41,398 --> 06:32:45,240
it's going to actually execute these one
10722
06:32:43,080 --> 06:32:47,280
by one so right now it hasn't executed
10723
06:32:45,240 --> 06:32:49,638
anything so I step over this once using
10724
06:32:47,280 --> 06:32:51,040
F10 you'll see it now appears and then
10725
06:32:49,638 --> 06:32:53,320
the same thing with this one and this
10726
06:32:51,040 --> 06:32:55,718
one and now that I've executed three we
10727
06:32:53,320 --> 06:32:57,120
should see three in the console perfect
10728
06:32:55,718 --> 06:32:58,440
so now what I want to demonstrate is
10729
06:32:57,120 --> 06:33:00,440
let's say what happy with the rest of
10730
06:32:58,440 --> 06:33:02,840
this function and then now we want to
10731
06:33:00,440 --> 06:33:04,200
just step out so I press this button and
10732
06:33:02,840 --> 06:33:07,080
you can see it jumps back back up to
10733
06:33:04,200 --> 06:33:09,600
print details f11 to go in and then
10734
06:33:07,080 --> 06:33:11,478
shift f11 to go out so now that we're
10735
06:33:09,600 --> 06:33:13,440
back in here the only thing we can do
10736
06:33:11,478 --> 06:33:16,080
really from this point is either press
10737
06:33:13,440 --> 06:33:17,840
F5 to stop debugging or press F10 to
10738
06:33:16,080 --> 06:33:19,840
jump to this line and at this point
10739
06:33:17,840 --> 06:33:21,320
we've already hit the last line now code
10740
06:33:19,840 --> 06:33:23,360
so we don't really need to use F10
10741
06:33:21,320 --> 06:33:25,558
anymore cuz we nothing to step over so
10742
06:33:23,360 --> 06:33:27,600
we can just press F5 and let our program
10743
06:33:25,558 --> 06:33:29,520
finish and there we go so let's have a
10744
06:33:27,600 --> 06:33:32,600
look another example can have a string
10745
06:33:29,520 --> 06:33:34,398
message equals read from console then we
10746
06:33:32,600 --> 06:33:36,360
can pass this in and
10747
06:33:34,398 --> 06:33:37,680
anything and then we can have a console
10748
06:33:36,360 --> 06:33:39,280
right line and we can just print the
10749
06:33:37,680 --> 06:33:41,600
message and then let's create this
10750
06:33:39,280 --> 06:33:43,200
function so because it's a return value
10751
06:33:41,600 --> 06:33:47,200
and start inside a string we're going to
10752
06:33:43,200 --> 06:33:49,558
need a static string read from console
10753
06:33:47,200 --> 06:33:52,558
and then this is one string parameter so
10754
06:33:49,558 --> 06:33:52,558
we have a string
10755
06:33:54,798 --> 06:33:57,798
message and then we can just do a
10756
06:33:56,760 --> 06:33:59,798
console
10757
06:33:57,798 --> 06:34:01,680
right and then print the message to the
10758
06:33:59,798 --> 06:34:05,760
screen and then we just want to return
10759
06:34:01,680 --> 06:34:07,680
the console. readline there we go and
10760
06:34:05,760 --> 06:34:09,320
again let's put a break point here and
10761
06:34:07,680 --> 06:34:11,398
we can remove this one so it doesn't
10762
06:34:09,320 --> 06:34:12,920
always have to be the first line if
10763
06:34:11,398 --> 06:34:15,200
we're perfectly happy with this amount
10764
06:34:12,920 --> 06:34:18,200
of code then we can simply take that off
10765
06:34:15,200 --> 06:34:20,240
and it will stop when it gets after this
10766
06:34:18,200 --> 06:34:22,320
line now if you look at the console
10767
06:34:20,240 --> 06:34:24,200
you'll see all of this is executed that
10768
06:34:22,320 --> 06:34:25,920
function is executed four times to print
10769
06:34:24,200 --> 06:34:27,958
the name and age of the screen and now
10770
06:34:25,920 --> 06:34:30,160
we're waiting here so we have our read
10771
06:34:27,958 --> 06:34:32,760
from console and now we can step into
10772
06:34:30,160 --> 06:34:34,600
using f11 and you can see that if you
10773
06:34:32,760 --> 06:34:36,600
hover over string m message has enter
10774
06:34:34,600 --> 06:34:38,718
anything which is our parameter that we
10775
06:34:36,600 --> 06:34:40,120
specified up here and then there you go
10776
06:34:38,718 --> 06:34:41,680
and then that will print into the screen
10777
06:34:40,120 --> 06:34:43,680
so you tab back in you'll see it says
10778
06:34:41,680 --> 06:34:45,520
enter anything and then just at the read
10779
06:34:43,680 --> 06:34:47,440
line we need to actually enter it
10780
06:34:45,520 --> 06:34:49,680
now
10781
06:34:47,440 --> 06:34:51,638
something and then press enter and then
10782
06:34:49,680 --> 06:34:54,240
just right here it's going to return it
10783
06:34:51,638 --> 06:34:56,160
back so if we use F10 it jumps back in
10784
06:34:54,240 --> 06:34:58,638
here and then actually we need to
10785
06:34:56,160 --> 06:35:00,200
execute this line so F10 again and then
10786
06:34:58,638 --> 06:35:02,120
if you hover over a message you see it
10787
06:35:00,200 --> 06:35:04,440
says something which is exactly what we
10788
06:35:02,120 --> 06:35:06,520
typed in the console here and then we
10789
06:35:04,440 --> 06:35:08,478
just print it back to the screen there
10790
06:35:06,520 --> 06:35:10,520
you go and now we just have the console
10791
06:35:08,478 --> 06:35:13,000
read line and we can press F5 to finish
10792
06:35:10,520 --> 06:35:15,440
up there we go so debugging is mainly
10793
06:35:13,000 --> 06:35:17,440
used by pressing this button on the side
10794
06:35:15,440 --> 06:35:19,558
so debugging is used by pressing on the
10795
06:35:17,440 --> 06:35:21,680
empty Gap just to the left of the line
10796
06:35:19,558 --> 06:35:23,080
number and it'll create a red dot once
10797
06:35:21,680 --> 06:35:24,600
you create a red dot it'll highlight
10798
06:35:23,080 --> 06:35:26,360
this for you once you've clicked on it
10799
06:35:24,600 --> 06:35:28,080
it should show up a red dot you can also
10800
06:35:26,360 --> 06:35:29,958
disable or delete all the bra points
10801
06:35:28,080 --> 06:35:31,920
from the debug window at the top of the
10802
06:35:29,958 --> 06:35:34,040
screen when you run the code it will
10803
06:35:31,920 --> 06:35:35,840
stop here and then you can either use F9
10804
06:35:34,040 --> 06:35:37,920
as a shortcut as well click into the
10805
06:35:35,840 --> 06:35:39,840
line that you want to debug and press F9
10806
06:35:37,920 --> 06:35:41,280
please note there is specific cases
10807
06:35:39,840 --> 06:35:43,440
where if you try and press this one
10808
06:35:41,280 --> 06:35:45,360
right here it's not going to let you if
10809
06:35:43,440 --> 06:35:47,680
the line doesn't really do anything for
10810
06:35:45,360 --> 06:35:49,478
example this is creating an integer
10811
06:35:47,680 --> 06:35:52,080
there's no real action that's happening
10812
06:35:49,478 --> 06:35:53,878
here that you should or want to debug
10813
06:35:52,080 --> 06:35:55,840
this is an assignment this is an
10814
06:35:53,878 --> 06:35:57,718
assignment this is a function call and
10815
06:35:55,840 --> 06:35:59,520
so on and so forth and this is also a
10816
06:35:57,718 --> 06:36:02,000
function call when it comes to just
10817
06:35:59,520 --> 06:36:04,398
declaring variables then a compiler does
10818
06:36:02,000 --> 06:36:06,080
not let you put in a break point because
10819
06:36:04,398 --> 06:36:08,040
there's nothing really to investigate
10820
06:36:06,080 --> 06:36:09,320
when you're just creating a variable so
10821
06:36:08,040 --> 06:36:10,878
don't get thrown off or think
10822
06:36:09,320 --> 06:36:12,920
something's wrong if you click on the
10823
06:36:10,878 --> 06:36:14,440
empty line and nothing appears so that's
10824
06:36:12,920 --> 06:36:18,398
it for debugging but let's just have a
10825
06:36:14,440 --> 06:36:21,878
little recap we can use F10 to step over
10826
06:36:18,398 --> 06:36:23,920
f11 to step into functions shift f11 to
10827
06:36:21,878 --> 06:36:25,718
step out or you can use the buttons that
10828
06:36:23,920 --> 06:36:28,240
appear at the top of the screen when you
10829
06:36:25,718 --> 06:36:30,920
run the code just right
10830
06:36:28,240 --> 06:36:32,840
here hover over variables and you get
10831
06:36:30,920 --> 06:36:34,600
their values as I showed you in the if
10832
06:36:32,840 --> 06:36:36,718
statements if you hover over the
10833
06:36:34,600 --> 06:36:38,240
operator inside the if statement it will
10834
06:36:36,718 --> 06:36:39,760
actually tell you what the condition is
10835
06:36:38,240 --> 06:36:42,240
and whether it's true or false so you
10836
06:36:39,760 --> 06:36:44,160
say if 1 equals equals to one and let's
10837
06:36:42,240 --> 06:36:46,160
just print out true to the screen just
10838
06:36:44,160 --> 06:36:48,760
so I can demonstrate
10839
06:36:46,160 --> 06:36:53,000
this if I hover over the equals equals
10840
06:36:48,760 --> 06:36:53,000
it says 1 equals equals 1 is
10841
06:36:53,798 --> 06:36:57,638
true so that's how you check if a
10842
06:36:56,040 --> 06:36:58,478
condition is going to be true or false
10843
06:36:57,638 --> 06:37:00,600
when you're
10844
06:36:58,478 --> 06:37:02,680
debugging you can check the parameter
10845
06:37:00,600 --> 06:37:05,958
values by simply hovering over it and
10846
06:37:02,680 --> 06:37:05,958
the tool tip should include the
10847
06:37:06,000 --> 06:37:10,558
value just like so and if you wanted to
10848
06:37:08,520 --> 06:37:14,440
step into this function you can use step
10849
06:37:10,558 --> 06:37:16,160
into and then use step over which is F10
10850
06:37:14,440 --> 06:37:18,040
just here and then you can actually
10851
06:37:16,160 --> 06:37:19,558
investigate these values as well or you
10852
06:37:18,040 --> 06:37:20,958
can look at the parameters and once
10853
06:37:19,558 --> 06:37:24,520
you're done with the function you can do
10854
06:37:20,958 --> 06:37:26,120
shift f11 or you can use step out and
10855
06:37:24,520 --> 06:37:27,680
then you can skip over this and the same
10856
06:37:26,120 --> 06:37:29,160
thing with this you can have a look at
10857
06:37:27,680 --> 06:37:32,878
the string variable once it gets
10858
06:37:29,160 --> 06:37:34,398
returned something in here and now it
10859
06:37:32,878 --> 06:37:36,080
says something thing in here and string
10860
06:37:34,398 --> 06:37:38,120
message and then we can print out to the
10861
06:37:36,080 --> 06:37:40,160
screen Etc so you can have a little bit
10862
06:37:38,120 --> 06:37:42,360
of Freedom with what you can do in terms
10863
06:37:40,160 --> 06:37:44,040
of debugging play around to debuging in
10864
06:37:42,360 --> 06:37:45,558
the past exercises that we've made
10865
06:37:44,040 --> 06:37:48,000
together and then what you can actually
10866
06:37:45,558 --> 06:37:49,878
do is gain experience in debugging so it
10867
06:37:48,000 --> 06:37:51,878
comes to more complicated projects you
10868
06:37:49,878 --> 06:37:55,638
can also apply that learning there and
10869
06:37:51,878 --> 06:37:55,638
make sure that your code is error
10870
06:37:55,798 --> 06:38:00,160
free so we covered how to debug by
10871
06:37:58,558 --> 06:38:02,360
pressing the white space just to the
10872
06:38:00,160 --> 06:38:04,280
left of the line number if we run the
10873
06:38:02,360 --> 06:38:05,520
code
10874
06:38:04,280 --> 06:38:07,440
the program will stop where our
10875
06:38:05,520 --> 06:38:09,120
breakpoint is and it won't execute the
10876
06:38:07,440 --> 06:38:10,958
line that we're up to we have to step
10877
06:38:09,120 --> 06:38:13,080
over it so we've actually used this
10878
06:38:10,958 --> 06:38:15,040
before to analyze our functions and see
10879
06:38:13,080 --> 06:38:17,160
what values are inside things so we have
10880
06:38:15,040 --> 06:38:19,160
these extra windows called the local and
10881
06:38:17,160 --> 06:38:21,440
the auto so let's take a look at them
10882
06:38:19,160 --> 06:38:23,080
and see what they're about so again if
10883
06:38:21,440 --> 06:38:25,280
you don't have this layout you can use
10884
06:38:23,080 --> 06:38:27,520
window reset layout and it should be on
10885
06:38:25,280 --> 06:38:29,840
the left panel not the right one this
10886
06:38:27,520 --> 06:38:31,638
time so we've got watch which we'll
10887
06:38:29,840 --> 06:38:34,920
explore later and we're going to have
10888
06:38:31,638 --> 06:38:37,000
locals and autos so the autos because
10889
06:38:34,920 --> 06:38:39,200
we're up to the age then it shows us the
10890
06:38:37,000 --> 06:38:41,120
age has value zero and then that
10891
06:38:39,200 --> 06:38:43,000
prevents us from hovering over it and if
10892
06:38:41,120 --> 06:38:46,320
we step over it you'll see that now it's
10893
06:38:43,000 --> 06:38:48,760
got age 23 and now name has appeared the
10894
06:38:46,320 --> 06:38:51,080
auto tab will try and figure out what
10895
06:38:48,760 --> 06:38:53,120
functions of what parameters you need to
10896
06:38:51,080 --> 06:38:54,320
be displayed down here based on the
10897
06:38:53,120 --> 06:38:56,680
section of Corde that you're currently
10898
06:38:54,320 --> 06:38:59,120
up to so my guess is because we're up to
10899
06:38:56,680 --> 06:39:00,958
this function and it takes in a name and
10900
06:38:59,120 --> 06:39:02,760
age then we're getting name and age
10901
06:39:00,958 --> 06:39:04,878
appearing before when we were on this
10902
06:39:02,760 --> 06:39:06,760
line it was only showing back the age
10903
06:39:04,878 --> 06:39:09,000
and because that section only used the
10904
06:39:06,760 --> 06:39:10,558
age we only got age here but if you
10905
06:39:09,000 --> 06:39:12,360
press locals you can see all the
10906
06:39:10,558 --> 06:39:14,398
variables that are local to this main
10907
06:39:12,360 --> 06:39:16,160
function are actually showing up we have
10908
06:39:14,398 --> 06:39:17,878
a parameter called ARs and technically
10909
06:39:16,160 --> 06:39:20,320
this is a parameter and it's a string
10910
06:39:17,878 --> 06:39:22,280
array type but it's currently unused but
10911
06:39:20,320 --> 06:39:24,280
it's still a parameter so you can see it
10912
06:39:22,280 --> 06:39:26,360
down here and then in order we have the
10913
06:39:24,280 --> 06:39:28,200
name variable down here then we've got
10914
06:39:26,360 --> 06:39:30,040
the age and then we've got the message
10915
06:39:28,200 --> 06:39:31,958
which is currently said to know because
10916
06:39:30,040 --> 06:39:34,040
we haven't created this in memory yet
10917
06:39:31,958 --> 06:39:36,040
but as you can see this changes depend
10918
06:39:34,040 --> 06:39:37,760
on the scope of our variables so now
10919
06:39:36,040 --> 06:39:40,120
that we're about to hit this function
10920
06:39:37,760 --> 06:39:42,200
let's use the step into and you'll see
10921
06:39:40,120 --> 06:39:44,920
that the locals have now changed we've
10922
06:39:42,200 --> 06:39:46,798
got rid of name age argument and string
10923
06:39:44,920 --> 06:39:48,240
message and it's replaced with name and
10924
06:39:46,798 --> 06:39:49,840
age because remember what I was saying
10925
06:39:48,240 --> 06:39:51,760
about the scope of the variables
10926
06:39:49,840 --> 06:39:53,558
parameters inside a function are always
10927
06:39:51,760 --> 06:39:54,958
going to be local to that function
10928
06:39:53,558 --> 06:39:57,440
around the curly braces that they're
10929
06:39:54,958 --> 06:39:59,280
surrounded by name and age is only
10930
06:39:57,440 --> 06:40:01,680
within here so when it's trying to shows
10931
06:39:59,280 --> 06:40:03,558
the local variables it's only local to
10932
06:40:01,680 --> 06:40:06,120
what's inside these curly braces which
10933
06:40:03,558 --> 06:40:07,760
in this case is name and age so now if
10934
06:40:06,120 --> 06:40:10,160
we step over
10935
06:40:07,760 --> 06:40:12,120
this and then if we step into this read
10936
06:40:10,160 --> 06:40:13,840
from console you'll see that just before
10937
06:40:12,120 --> 06:40:15,920
we step into this function that these
10938
06:40:13,840 --> 06:40:18,040
have all came back cuz again the scope
10939
06:40:15,920 --> 06:40:20,240
has changed back to this function which
10940
06:40:18,040 --> 06:40:22,520
is our main function which has all these
10941
06:40:20,240 --> 06:40:24,120
variables inside so our read from
10942
06:40:22,520 --> 06:40:26,600
console will only have our string
10943
06:40:24,120 --> 06:40:28,320
message so hopefully when we press f11
10944
06:40:26,600 --> 06:40:30,280
or press this step into button at the
10945
06:40:28,320 --> 06:40:33,320
top we should only see the message
10946
06:40:30,280 --> 06:40:34,958
variable inside here there we go we get
10947
06:40:33,320 --> 06:40:37,840
into anything cuz that's what was passed
10948
06:40:34,958 --> 06:40:40,478
in over here and then as soon as we
10949
06:40:37,840 --> 06:40:43,320
return back to
10950
06:40:40,478 --> 06:40:45,280
that let we type something in and then
10951
06:40:43,320 --> 06:40:47,638
we get all of the values again and you
10952
06:40:45,280 --> 06:40:50,040
can see that system. console. rine had
10953
06:40:47,638 --> 06:40:51,958
returned hello which is this line and
10954
06:40:50,040 --> 06:40:53,718
then if we press F10 again you'll see
10955
06:40:51,958 --> 06:40:55,718
that even though we've got it back here
10956
06:40:53,718 --> 06:40:57,958
our message is about to be assigned once
10957
06:40:55,718 --> 06:41:00,398
we step over this there you go it says
10958
06:40:57,958 --> 06:41:02,280
hello and as you can see this changes in
10959
06:41:00,398 --> 06:41:04,440
real time so as soon as we step over
10960
06:41:02,280 --> 06:41:06,360
lines hello got play straight into there
10961
06:41:04,440 --> 06:41:07,638
this helps us tremendously because we
10962
06:41:06,360 --> 06:41:09,398
don't have to keep hovering over
10963
06:41:07,638 --> 06:41:11,440
variables like this to keep checking
10964
06:41:09,398 --> 06:41:13,600
over their values you can simply use
10965
06:41:11,440 --> 06:41:15,200
this menu and it will always update
10966
06:41:13,600 --> 06:41:16,718
because whenever you change Scopes
10967
06:41:15,200 --> 06:41:18,520
between functions that will
10968
06:41:16,718 --> 06:41:20,440
automatically update as you just saw in
10969
06:41:18,520 --> 06:41:22,120
them examples there we go and then when
10970
06:41:20,440 --> 06:41:23,718
the program ends obviously this window
10971
06:41:22,120 --> 06:41:25,920
is going to close down so that's a very
10972
06:41:23,718 --> 06:41:27,878
helpful method of being able to debug
10973
06:41:25,920 --> 06:41:29,320
without having to hover over anything in
10974
06:41:27,878 --> 06:41:31,360
the next video we're going to cover how
10975
06:41:29,320 --> 06:41:32,958
to watch variables and this can be very
10976
06:41:31,360 --> 06:41:35,240
handy when you're looking for specific
10977
06:41:32,958 --> 06:41:35,240
areas
10978
06:41:37,040 --> 06:41:40,760
another really good debuging technique
10979
06:41:38,638 --> 06:41:43,240
is the watch window let's press a break
10980
06:41:40,760 --> 06:41:44,840
point just of left of line 13 and let's
10981
06:41:43,240 --> 06:41:47,080
run the code and let's see what this is
10982
06:41:44,840 --> 06:41:49,200
about so we covered that we have the
10983
06:41:47,080 --> 06:41:50,920
locals window and we have the auto
10984
06:41:49,200 --> 06:41:52,160
window that will automatically give us
10985
06:41:50,920 --> 06:41:54,200
the variables that we're currently in
10986
06:41:52,160 --> 06:41:55,840
the scope of but let's forget about that
10987
06:41:54,200 --> 06:41:59,120
right now let's have a look at the watch
10988
06:41:55,840 --> 06:42:00,600
window it says here add an item to watch
10989
06:41:59,120 --> 06:42:02,280
so there's a couple ways that we can do
10990
06:42:00,600 --> 06:42:04,478
this you can actually double click on a
10991
06:42:02,280 --> 06:42:06,000
variable and write click or you can just
10992
06:42:04,478 --> 06:42:08,558
highlight it like this and then right
10993
06:42:06,000 --> 06:42:10,958
click and it says here add watch and you
10994
06:42:08,558 --> 06:42:12,798
can press it it says name value null
10995
06:42:10,958 --> 06:42:14,798
because we haven't created it yet so the
10996
06:42:12,798 --> 06:42:16,600
default value is null as of right now
10997
06:42:14,798 --> 06:42:18,440
cuz it's not actually assigned anything
10998
06:42:16,600 --> 06:42:20,040
so if you press F10 and step over it
10999
06:42:18,440 --> 06:42:22,000
you'll see that immediately changes to
11000
06:42:20,040 --> 06:42:24,160
abber without was having to do anything
11001
06:42:22,000 --> 06:42:26,200
except simply press F10 which is the
11002
06:42:24,160 --> 06:42:28,120
shortcut for our step over button right
11003
06:42:26,200 --> 06:42:29,798
at the top of the screen this prevents
11004
06:42:28,120 --> 06:42:31,760
us from having to hold over the
11005
06:42:29,798 --> 06:42:33,958
variables to get the little tool tip so
11006
06:42:31,760 --> 06:42:35,958
we can see the value the of this is we
11007
06:42:33,958 --> 06:42:37,680
can watch variables and we don't have to
11008
06:42:35,958 --> 06:42:39,718
worry about showing in the locals or the
11009
06:42:37,680 --> 06:42:41,798
auto because it's always there and if
11010
06:42:39,718 --> 06:42:43,280
you have lots of variables like this
11011
06:42:41,798 --> 06:42:45,440
then you're only concerned about the one
11012
06:42:43,280 --> 06:42:46,958
that you're watching locals and aluto
11013
06:42:45,440 --> 06:42:48,638
will always change depending on the
11014
06:42:46,958 --> 06:42:50,558
scope that you currently at whereas
11015
06:42:48,638 --> 06:42:53,040
watch will only do what you want it to
11016
06:42:50,558 --> 06:42:54,878
do so because we don't put age inside
11017
06:42:53,040 --> 06:42:56,520
there then it simply won't come up just
11018
06:42:54,878 --> 06:42:59,000
because it likes it if we check the
11019
06:42:56,520 --> 06:43:00,878
locals we have agent there and the Autos
11020
06:42:59,000 --> 06:43:02,958
we have agent there as well but we don't
11021
06:43:00,878 --> 06:43:04,240
have it in the watch so what else you
11022
06:43:02,958 --> 06:43:07,080
can you do is actually just type
11023
06:43:04,240 --> 06:43:09,320
straight into here age
11024
06:43:07,080 --> 06:43:11,440
23 if you really wanted to you can
11025
06:43:09,320 --> 06:43:14,520
actually also type in conditions age
11026
06:43:11,440 --> 06:43:15,680
equals equals 23 it returns back true so
11027
06:43:14,520 --> 06:43:17,440
if you had an if statement that you
11028
06:43:15,680 --> 06:43:20,120
wanted to track then you could do that
11029
06:43:17,440 --> 06:43:23,000
as well so let's have a
11030
06:43:20,120 --> 06:43:25,000
look so had this statement that said if
11031
06:43:23,000 --> 06:43:27,080
age is greater than 18 then we can just
11032
06:43:25,000 --> 06:43:29,638
print out 18 plus to the screen so let's
11033
06:43:27,080 --> 06:43:31,200
run the code now and see what happens so
11034
06:43:29,638 --> 06:43:32,718
as you know we have this condition right
11035
06:43:31,200 --> 06:43:34,600
here so if you actually highlight this
11036
06:43:32,718 --> 06:43:36,160
and and drop it you can see that the
11037
06:43:34,600 --> 06:43:38,638
cursor changes and you can drop it
11038
06:43:36,160 --> 06:43:41,558
inside here let's click on the age equal
11039
06:43:38,638 --> 06:43:43,040
equal 23 and click delete so of course
11040
06:43:41,558 --> 06:43:44,558
we can't really render this function
11041
06:43:43,040 --> 06:43:46,558
right now because we haven't even
11042
06:43:44,558 --> 06:43:48,520
created the integer variable so let's
11043
06:43:46,558 --> 06:43:51,120
run over the integer variable and as you
11044
06:43:48,520 --> 06:43:53,558
can see here it says AG is zero which is
11045
06:43:51,120 --> 06:43:55,878
the default value for integer and that
11046
06:43:53,558 --> 06:43:58,080
is indeed zero right now and then AG is
11047
06:43:55,878 --> 06:44:00,200
greater than 18 is false but as soon as
11048
06:43:58,080 --> 06:44:02,160
I step over this line and it makes age
11049
06:44:00,200 --> 06:44:03,920
the value of 23 you'll see that these
11050
06:44:02,160 --> 06:44:06,000
two immediately change you know they
11051
06:44:03,920 --> 06:44:07,280
change because the values have went red
11052
06:44:06,000 --> 06:44:08,920
red means that the values have just
11053
06:44:07,280 --> 06:44:10,520
changed in the last button that you
11054
06:44:08,920 --> 06:44:12,798
pressed whether that means you stepped
11055
06:44:10,520 --> 06:44:14,718
into over or stepped out it doesn't
11056
06:44:12,798 --> 06:44:16,520
matter red always dictates that the
11057
06:44:14,718 --> 06:44:18,638
value is just changed so now it's
11058
06:44:16,520 --> 06:44:20,798
telling us that age is greater than 18
11059
06:44:18,638 --> 06:44:22,600
is returned back true so when I step
11060
06:44:20,798 --> 06:44:25,398
over this line because this is returned
11061
06:44:22,600 --> 06:44:28,398
back true I should render 18 plus let's
11062
06:44:25,398 --> 06:44:29,958
have a look F10 F10 and there we go
11063
06:44:28,398 --> 06:44:31,840
we've reached the middle which means
11064
06:44:29,958 --> 06:44:34,520
that's going to get printed out
11065
06:44:31,840 --> 06:44:36,600
now so so moving on to another example
11066
06:44:34,520 --> 06:44:38,160
we can have print details if we go into
11067
06:44:36,600 --> 06:44:39,878
this function and let's say we're only
11068
06:44:38,160 --> 06:44:41,798
concerned about the name then we can
11069
06:44:39,878 --> 06:44:43,600
print out the name down here but as you
11070
06:44:41,798 --> 06:44:45,000
can see we already have it twice so
11071
06:44:43,600 --> 06:44:46,600
that's just going to update depending on
11072
06:44:45,000 --> 06:44:48,520
the parameter name and even if you
11073
06:44:46,600 --> 06:44:50,360
wanted to show something like this you
11074
06:44:48,520 --> 06:44:52,478
can drag this down here and it actually
11075
06:44:50,360 --> 06:44:54,160
shows you the output this is very handy
11076
06:44:52,478 --> 06:44:55,760
so you can see the output because
11077
06:44:54,160 --> 06:44:58,040
sometimes when you have it like this it
11078
06:44:55,760 --> 06:44:59,878
doesn't look very clear you can also
11079
06:44:58,040 --> 06:45:02,398
hover over it right there and you can
11080
06:44:59,878 --> 06:45:04,398
see the value placed here but again it's
11081
06:45:02,398 --> 06:45:06,600
very handy to have it here so you don't
11082
06:45:04,398 --> 06:45:08,920
have to keep hovering over the tool tape
11083
06:45:06,600 --> 06:45:10,760
just to see the values and from here you
11084
06:45:08,920 --> 06:45:12,920
can actually change the values so we can
11085
06:45:10,760 --> 06:45:16,520
just say Harry is now the value for the
11086
06:45:12,920 --> 06:45:18,478
name and Harry is going to be age 35 so
11087
06:45:16,520 --> 06:45:20,320
when we press F5 and let the program run
11088
06:45:18,478 --> 06:45:22,718
naturally you'll see that Harry is age
11089
06:45:20,320 --> 06:45:24,760
35 now and that's all done through the
11090
06:45:22,718 --> 06:45:27,200
watch list down here and it doesn't even
11091
06:45:24,760 --> 06:45:28,920
matter because we have full control over
11092
06:45:27,200 --> 06:45:30,638
the watch list variables and we can
11093
06:45:28,920 --> 06:45:33,280
change their contents to see how our
11094
06:45:30,638 --> 06:45:34,798
code acts in different situations so
11095
06:45:33,280 --> 06:45:36,680
please use this watch list as much as
11096
06:45:34,798 --> 06:45:38,878
you can and don't forget that once you
11097
06:45:36,680 --> 06:45:41,080
can double click on variables and add it
11098
06:45:38,878 --> 06:45:43,718
to the watch just from here you can also
11099
06:45:41,080 --> 06:45:45,798
say quick watch and what that'll let you
11100
06:45:43,718 --> 06:45:47,558
do is it'll give you the value and then
11101
06:45:45,798 --> 06:45:49,840
if you want to add it you can press add
11102
06:45:47,558 --> 06:45:51,760
watch here or you can just keep pressing
11103
06:45:49,840 --> 06:45:53,920
re-evaluate once you've skipped over
11104
06:45:51,760 --> 06:45:56,320
some lines so let's say we skip over it
11105
06:45:53,920 --> 06:45:58,920
now and then go back onto it and because
11106
06:45:56,320 --> 06:46:00,760
it has a shortcut shift F9 we can just
11107
06:45:58,920 --> 06:46:02,638
quickly press it like this and it says
11108
06:46:00,760 --> 06:46:03,760
Alber inside here and we can change the
11109
06:46:02,638 --> 06:46:05,398
expression
11110
06:46:03,760 --> 06:46:07,080
maybe do something like age and then
11111
06:46:05,398 --> 06:46:09,440
click re-eval it and it changes the
11112
06:46:07,080 --> 06:46:11,478
value to zero and then we can even
11113
06:46:09,440 --> 06:46:13,240
change this to like 50 for example as
11114
06:46:11,478 --> 06:46:16,080
you can see down here it's changed to 50
11115
06:46:13,240 --> 06:46:18,200
as well so you can drag and drop it into
11116
06:46:16,080 --> 06:46:20,040
here or you can double click and press
11117
06:46:18,200 --> 06:46:22,000
add watch it's up to you please make use
11118
06:46:20,040 --> 06:46:24,000
of this watch variable and don't rely
11119
06:46:22,000 --> 06:46:25,320
too much on the autos and the local just
11120
06:46:24,000 --> 06:46:27,398
so you can keep your own variables
11121
06:46:25,320 --> 06:46:29,320
inside here and you can have complicate
11122
06:46:27,398 --> 06:46:31,558
outputs like these you can check
11123
06:46:29,320 --> 06:46:32,920
conditions you can check console outputs
11124
06:46:31,558 --> 06:46:35,280
as well as just checking normal
11125
06:46:32,920 --> 06:46:37,080
variables so using the watch window you
11126
06:46:35,280 --> 06:46:39,878
have lots of different opportunities to
11127
06:46:37,080 --> 06:46:39,878
keep track of your
11128
06:46:40,200 --> 06:46:45,040
code so we've got another debugging task
11129
06:46:42,920 --> 06:46:47,320
let's try and run the code and see what
11130
06:46:45,040 --> 06:46:50,840
happens we're going to get printed over
11131
06:46:47,320 --> 06:46:52,840
18 so our value is agal 35 and we're
11132
06:46:50,840 --> 06:46:55,000
already getting over 18 printed to the
11133
06:46:52,840 --> 06:46:56,878
screen so if we place a Brak line inside
11134
06:46:55,000 --> 06:46:58,638
here and actually run the code we can
11135
06:46:56,878 --> 06:47:01,120
see what's going on we can add them
11136
06:46:58,638 --> 06:47:04,478
inside the watch just like this we can
11137
06:47:01,120 --> 06:47:05,760
add in all the conditions inside
11138
06:47:04,478 --> 06:47:08,840
just like
11139
06:47:05,760 --> 06:47:11,200
this perfect so we can actually see here
11140
06:47:08,840 --> 06:47:13,280
that because our age is over 35 it's
11141
06:47:11,200 --> 06:47:15,000
saying greater than 18 is true greater
11142
06:47:13,280 --> 06:47:17,478
than 30 is true but greater than 50
11143
06:47:15,000 --> 06:47:19,638
isn't true and we can easily see that
11144
06:47:17,478 --> 06:47:21,760
except because this is an if and an LSI
11145
06:47:19,638 --> 06:47:23,520
chain as soon as the first one is true
11146
06:47:21,760 --> 06:47:26,320
it's going to enter that one you press
11147
06:47:23,520 --> 06:47:28,000
F10 then we get over 18 and then as soon
11148
06:47:26,320 --> 06:47:30,718
as it finishes that path it actually
11149
06:47:28,000 --> 06:47:32,240
skips all the way to the end the problem
11150
06:47:30,718 --> 06:47:35,080
with this is we only want it to print
11151
06:47:32,240 --> 06:47:37,160
over 30 since it's over 30 so we have to
11152
06:47:35,080 --> 06:47:40,240
introduce ranges inside here and we can
11153
06:47:37,160 --> 06:47:42,520
say age is less than 30 and then you can
11154
06:47:40,240 --> 06:47:44,478
have AG is greater than 30 here and then
11155
06:47:42,520 --> 06:47:47,920
you can also insert an and and say age
11156
06:47:44,478 --> 06:47:49,638
is less than 50 and there we go and now
11157
06:47:47,920 --> 06:47:52,398
it says over
11158
06:47:49,638 --> 06:47:54,840
30 and if we change this to 55 then we
11159
06:47:52,398 --> 06:47:57,638
should get over 50 and then finally if
11160
06:47:54,840 --> 06:48:00,760
we change this to something like 23 then
11161
06:47:57,638 --> 06:48:02,440
we should also get over 18 perfect so
11162
06:48:00,760 --> 06:48:04,680
what was going on here is because we had
11163
06:48:02,440 --> 06:48:06,798
if El if chain then you're only going to
11164
06:48:04,680 --> 06:48:09,520
get one of them printed and because a
11165
06:48:06,798 --> 06:48:11,520
value over 18 satisfied both the first
11166
06:48:09,520 --> 06:48:13,240
path and the second path then it's only
11167
06:48:11,520 --> 06:48:15,240
going to print the first path if we
11168
06:48:13,240 --> 06:48:17,360
actually undo our changes all the way
11169
06:48:15,240 --> 06:48:19,958
till the start and then we actually flip
11170
06:48:17,360 --> 06:48:22,520
the order of this if we put our this one
11171
06:48:19,958 --> 06:48:24,958
first and delete the else and change it
11172
06:48:22,520 --> 06:48:26,958
to lse if inside here so now we have a
11173
06:48:24,958 --> 06:48:28,680
new chain except the first one in the
11174
06:48:26,958 --> 06:48:31,160
chain is actually if age is greater than
11175
06:48:28,680 --> 06:48:32,798
30 now if you're on the code you'll see
11176
06:48:31,160 --> 06:48:35,360
that the first one actually says over 30
11177
06:48:32,798 --> 06:48:37,760
30 and it says over 30 and it doesn't
11178
06:48:35,360 --> 06:48:39,680
say over 18 anymore but simply because
11179
06:48:37,760 --> 06:48:41,240
this is the first one in the list as
11180
06:48:39,680 --> 06:48:43,398
soon as it hits the first one as I'll
11181
06:48:41,240 --> 06:48:45,360
demonstrate now it will go inside this
11182
06:48:43,398 --> 06:48:46,920
bracket hit this curly brace and then
11183
06:48:45,360 --> 06:48:49,798
jump straight to the
11184
06:48:46,920 --> 06:48:51,600
end you should always order them from
11185
06:48:49,798 --> 06:48:53,398
smallest to biggest because that will
11186
06:48:51,600 --> 06:48:55,440
probably make the most logical sense
11187
06:48:53,398 --> 06:48:57,280
when you're reviewing your code and in
11188
06:48:55,440 --> 06:48:59,280
doing so you can actually have a more
11189
06:48:57,280 --> 06:49:01,360
formatted code but you need to watch out
11190
06:48:59,280 --> 06:49:03,040
that the ranges need to make sense if
11191
06:49:01,360 --> 06:49:04,600
you have something like this then you
11192
06:49:03,040 --> 06:49:06,478
need to restrict this one because it's
11193
06:49:04,600 --> 06:49:08,558
going to be blocking the second one and
11194
06:49:06,478 --> 06:49:10,558
you don't want to make these all ifs
11195
06:49:08,558 --> 06:49:12,718
because then you get multiple outputs
11196
06:49:10,558 --> 06:49:14,398
now we should get 18 and 30 being
11197
06:49:12,718 --> 06:49:16,040
printed to the screen this is not
11198
06:49:14,398 --> 06:49:18,000
something you want so make sure that you
11199
06:49:16,040 --> 06:49:20,160
place your ranges inside here by adding
11200
06:49:18,000 --> 06:49:22,040
an and and then placing the other end of
11201
06:49:20,160 --> 06:49:24,040
it which will be 30 and then this one
11202
06:49:22,040 --> 06:49:25,520
will continue from it so just to sum up
11203
06:49:24,040 --> 06:49:28,120
what you need to do is make sure that
11204
06:49:25,520 --> 06:49:31,040
you use l ifs and make sure that you
11205
06:49:28,120 --> 06:49:32,320
have ranges just so when one exits it's
11206
06:49:31,040 --> 06:49:34,120
going to check the other one and the
11207
06:49:32,320 --> 06:49:36,120
other one you don't want the first
11208
06:49:34,120 --> 06:49:39,000
condition to beat the second one if
11209
06:49:36,120 --> 06:49:41,600
that's for the right path age 35 goes
11210
06:49:39,000 --> 06:49:43,320
into this path or at least we want it to
11211
06:49:41,600 --> 06:49:45,398
but it's been restricted by the fact
11212
06:49:43,320 --> 06:49:46,398
that age is greater than 18 when we had
11213
06:49:45,398 --> 06:49:49,040
it like
11214
06:49:46,398 --> 06:49:51,760
this this is restricting everything over
11215
06:49:49,040 --> 06:49:54,440
18 to go inside this path if this was
11216
06:49:51,760 --> 06:49:56,160
even like 65 we should only fall into
11217
06:49:54,440 --> 06:49:58,160
this one and we run the code then we're
11218
06:49:56,160 --> 06:50:00,000
still going to get over 18 which is
11219
06:49:58,160 --> 06:50:02,160
still technically true it is still over
11220
06:50:00,000 --> 06:50:03,958
18 but we have different outputs for
11221
06:50:02,160 --> 06:50:05,638
this sort of age range make sure that
11222
06:50:03,958 --> 06:50:07,558
you use El ifs and you keep in an
11223
06:50:05,638 --> 06:50:09,080
appropriate range and keep them in the
11224
06:50:07,558 --> 06:50:11,040
order of the if statements going from
11225
06:50:09,080 --> 06:50:12,478
lowest to highest or whatever makes
11226
06:50:11,040 --> 06:50:13,558
sense in that situation that you're
11227
06:50:12,478 --> 06:50:16,160
using
11228
06:50:13,558 --> 06:50:17,878
it this is a start of objectoriented
11229
06:50:16,160 --> 06:50:20,120
programming let's start with a concept
11230
06:50:17,878 --> 06:50:22,840
called structures structures allow us to
11231
06:50:20,120 --> 06:50:25,040
store different data types inside one
11232
06:50:22,840 --> 06:50:26,878
data type so it's almost like a group of
11233
06:50:25,040 --> 06:50:28,320
data types we can demonstrate this if
11234
06:50:26,878 --> 06:50:29,920
you wanted to stall something like a
11235
06:50:28,320 --> 06:50:32,360
person you would normally have something
11236
06:50:29,920 --> 06:50:35,440
like a string name equals ABA ear it in
11237
06:50:32,360 --> 06:50:38,160
age equal 23 so in here we have a string
11238
06:50:35,440 --> 06:50:39,920
name that represents the name and inage
11239
06:50:38,160 --> 06:50:41,520
which represents their age and you can
11240
06:50:39,920 --> 06:50:43,478
see that because these are two different
11241
06:50:41,520 --> 06:50:45,638
data types you can't wrap them around
11242
06:50:43,478 --> 06:50:47,840
inside an array you can't have a string
11243
06:50:45,638 --> 06:50:49,958
array called person and then say we want
11244
06:50:47,840 --> 06:50:51,718
to have two different values although
11245
06:50:49,958 --> 06:50:53,798
this will still compile we can't store
11246
06:50:51,718 --> 06:50:55,840
both these data types inside our array
11247
06:50:53,798 --> 06:50:58,320
unless you make the age and store it as
11248
06:50:55,840 --> 06:51:00,160
a string which will be 23 inside the
11249
06:50:58,320 --> 06:51:01,760
quotation marks and then you'll have to
11250
06:51:00,160 --> 06:51:04,398
worry about converting it later on in
11251
06:51:01,760 --> 06:51:06,600
your program so this is not really ideal
11252
06:51:04,398 --> 06:51:09,600
so we have something called a structure
11253
06:51:06,600 --> 06:51:11,280
so just outside here we can say struct
11254
06:51:09,600 --> 06:51:13,240
which stands for structure and then we
11255
06:51:11,280 --> 06:51:15,040
can have our name straight after so we
11256
06:51:13,240 --> 06:51:16,840
can say person and the next time we just
11257
06:51:15,040 --> 06:51:18,638
open the curly braces and let's see what
11258
06:51:16,840 --> 06:51:20,398
we've just created so now in order to
11259
06:51:18,638 --> 06:51:22,320
make an instance of this we can actually
11260
06:51:20,398 --> 06:51:24,440
use that like a data type so we can say
11261
06:51:22,320 --> 06:51:26,760
person person there we go so now we've
11262
06:51:24,440 --> 06:51:28,680
created our person variable now we see
11263
06:51:26,760 --> 06:51:30,120
we've got our own data type before we
11264
06:51:28,680 --> 06:51:32,040
were just using string and ins and
11265
06:51:30,120 --> 06:51:34,478
arrays and things like that but now we
11266
06:51:32,040 --> 06:51:36,440
actually have a new person so because
11267
06:51:34,478 --> 06:51:39,718
our person is made up of a name and an
11268
06:51:36,440 --> 06:51:41,478
age let's place that in here string name
11269
06:51:39,718 --> 06:51:43,600
and then int age of course you don't
11270
06:51:41,478 --> 06:51:45,120
want to give them default values because
11271
06:51:43,600 --> 06:51:47,798
like all variables you don't know what
11272
06:51:45,120 --> 06:51:50,760
the value is going to be to begin with
11273
06:51:47,798 --> 06:51:52,240
so now what we can do person Dot and we
11274
06:51:50,760 --> 06:51:53,520
don't actually have access to using
11275
06:51:52,240 --> 06:51:55,478
these values yet so what we're going to
11276
06:51:53,520 --> 06:51:57,760
have to do there is a concept called
11277
06:51:55,478 --> 06:52:00,000
access modifiers which we'll cover on
11278
06:51:57,760 --> 06:52:01,638
later on in this course but just for now
11279
06:52:00,000 --> 06:52:03,200
what this means is you either have
11280
06:52:01,638 --> 06:52:04,878
private or public public and they're the
11281
06:52:03,200 --> 06:52:06,240
main two words there's other words that
11282
06:52:04,878 --> 06:52:08,200
you can have but they're the main two
11283
06:52:06,240 --> 06:52:10,080
for now if you have it private it means
11284
06:52:08,200 --> 06:52:12,360
you can't access it down here through
11285
06:52:10,080 --> 06:52:13,160
person. name and but as soon as you make
11286
06:52:12,360 --> 06:52:17,120
this
11287
06:52:13,160 --> 06:52:17,120
public then now it becomes
11288
06:52:17,638 --> 06:52:23,558
accessible there we go we can make that
11289
06:52:19,718 --> 06:52:25,840
equal to something and if we take off
11290
06:52:23,558 --> 06:52:28,080
public then you see we don't have access
11291
06:52:25,840 --> 06:52:29,920
to it it's inaccessible due to its
11292
06:52:28,080 --> 06:52:31,680
protection level because the protection
11293
06:52:29,920 --> 06:52:33,478
level is private which means that you
11294
06:52:31,680 --> 06:52:35,360
can only access name Within These two
11295
06:52:33,478 --> 06:52:37,280
curly braces the second you make it
11296
06:52:35,360 --> 06:52:39,798
public it means that anyone outside of
11297
06:52:37,280 --> 06:52:41,958
the structure EG down here can also have
11298
06:52:39,798 --> 06:52:44,638
access to name so if we do that to our
11299
06:52:41,958 --> 06:52:46,840
public on in age and string name then we
11300
06:52:44,638 --> 06:52:49,440
actually can access both now we can say
11301
06:52:46,840 --> 06:52:51,840
person. Ag and it comes up like this the
11302
06:52:49,440 --> 06:52:53,878
purple cube is a function and the blue
11303
06:52:51,840 --> 06:52:55,920
is Fields you can actually fil them out
11304
06:52:53,878 --> 06:52:58,638
by pressing them down here show only
11305
06:52:55,920 --> 06:53:00,878
methods and show only Fields so we can
11306
06:52:58,638 --> 06:53:03,000
do age equals 23 and we can actually
11307
06:53:00,878 --> 06:53:04,638
just place abber inside here
11308
06:53:03,000 --> 06:53:06,398
and now that this data is essentially
11309
06:53:04,638 --> 06:53:08,360
the same as this the benefit that we
11310
06:53:06,398 --> 06:53:11,558
have now is when we print to the screen
11311
06:53:08,360 --> 06:53:15,840
we can say person. name and then hyphen
11312
06:53:11,558 --> 06:53:17,958
person. age and if you run that
11313
06:53:15,840 --> 06:53:20,160
now we get Abid
11314
06:53:17,958 --> 06:53:22,478
23 if you actually copy and paste is
11315
06:53:20,160 --> 06:53:24,000
lined out and just remove person dot
11316
06:53:22,478 --> 06:53:26,280
just so we're using these two variables
11317
06:53:24,000 --> 06:53:27,760
at the top of here we in the code you
11318
06:53:26,280 --> 06:53:30,638
should see exactly the same values
11319
06:53:27,760 --> 06:53:32,920
coming up there we go but now what's
11320
06:53:30,638 --> 06:53:34,600
really handy about this situation is we
11321
06:53:32,920 --> 06:53:36,680
have two data types that are wrapped
11322
06:53:34,600 --> 06:53:39,080
inside of one data type instead of
11323
06:53:36,680 --> 06:53:40,920
having a name and age we have a person
11324
06:53:39,080 --> 06:53:42,558
because what gets really easy now is if
11325
06:53:40,920 --> 06:53:45,638
you wanted to add something else inside
11326
06:53:42,558 --> 06:53:47,798
here like for example birth month which
11327
06:53:45,638 --> 06:53:50,638
for me is five so in here you'd have to
11328
06:53:47,798 --> 06:53:52,600
make another one birth month equals 5
11329
06:53:50,638 --> 06:53:55,040
and then all we have to do inside our
11330
06:53:52,600 --> 06:53:56,638
person is just say person. birth month
11331
06:53:55,040 --> 06:53:58,120
and we can just assign it right here
11332
06:53:56,638 --> 06:54:00,040
instead of having to create a whole new
11333
06:53:58,120 --> 06:54:01,840
variable we add it up here and then we
11334
06:54:00,040 --> 06:54:05,760
can use it down here and then we can add
11335
06:54:01,840 --> 06:54:05,760
that into here person. birthmonth
11336
06:54:06,080 --> 06:54:10,360
make add this to this one and then get
11337
06:54:08,360 --> 06:54:12,360
rid of birth month and as you can see
11338
06:54:10,360 --> 06:54:14,878
it's exactly the
11339
06:54:12,360 --> 06:54:16,760
same so we've taken these data types and
11340
06:54:14,878 --> 06:54:19,360
placed it inside our structure so then
11341
06:54:16,760 --> 06:54:21,520
we can just reference it by the person
11342
06:54:19,360 --> 06:54:22,958
this becomes very very handy for this
11343
06:54:21,520 --> 06:54:24,718
exact reason that I'm going to show you
11344
06:54:22,958 --> 06:54:27,440
now let's say you wanted to read this
11345
06:54:24,718 --> 06:54:28,920
data in from the user so you have static
11346
06:54:27,440 --> 06:54:30,680
and then you're a bit stuck on what data
11347
06:54:28,920 --> 06:54:32,398
type you want to return if you forget
11348
06:54:30,680 --> 06:54:34,760
about the birth month then we have a
11349
06:54:32,398 --> 06:54:36,958
name and age is a very basic person but
11350
06:54:34,760 --> 06:54:38,718
our return type only lets us have one
11351
06:54:36,958 --> 06:54:40,000
type so that's going to be a problem now
11352
06:54:38,718 --> 06:54:42,360
so you could do a string return that
11353
06:54:40,000 --> 06:54:45,440
will give us back the name and then we
11354
06:54:42,360 --> 06:54:47,160
can say return person and inside the
11355
06:54:45,440 --> 06:54:49,558
brackets what you can actually do is do
11356
06:54:47,160 --> 06:54:52,160
out in Edge so you can have two output
11357
06:54:49,558 --> 06:54:55,080
variables an out age and then a return
11358
06:54:52,160 --> 06:54:59,120
string so inside here we could
11359
06:54:55,080 --> 06:55:02,680
say enter your name and then string name
11360
06:54:59,120 --> 06:55:04,520
equals console. readline
11361
06:55:02,680 --> 06:55:06,520
if we copy and paste this down and then
11362
06:55:04,520 --> 06:55:10,718
we can say enter your age and then we
11363
06:55:06,520 --> 06:55:12,040
can say int age equals convert to in 32
11364
06:55:10,718 --> 06:55:14,878
and then put the console read line
11365
06:55:12,040 --> 06:55:16,638
inside there there we go now that we've
11366
06:55:14,878 --> 06:55:17,760
got that we don't actually need in age
11367
06:55:16,638 --> 06:55:20,520
because we've already got output
11368
06:55:17,760 --> 06:55:21,958
parameter so we just need to be age and
11369
06:55:20,520 --> 06:55:25,760
then after we've retrieved them two
11370
06:55:21,958 --> 06:55:28,240
values we can return name there we go so
11371
06:55:25,760 --> 06:55:32,478
let's actually try that up here so we
11372
06:55:28,240 --> 06:55:35,200
can say new name is equal to return
11373
06:55:32,478 --> 06:55:38,000
person and then out into new age and you
11374
06:55:35,200 --> 06:55:40,478
can take this exact line paste it down
11375
06:55:38,000 --> 06:55:42,718
here get rid of the birth month and then
11376
06:55:40,478 --> 06:55:46,558
we can use new age in here and new name
11377
06:55:42,718 --> 06:55:48,120
inside here so let's run that now ask us
11378
06:55:46,558 --> 06:55:50,520
what is your name Abba what is your age
11379
06:55:48,120 --> 06:55:52,280
23 and then we got Abid 23 which is the
11380
06:55:50,520 --> 06:55:54,240
same as what we've got before but you
11381
06:55:52,280 --> 06:55:56,280
see that this situation is pretty messy
11382
06:55:54,240 --> 06:55:58,558
we have to return a string variable and
11383
06:55:56,280 --> 06:56:00,958
also use an output in order to be able
11384
06:55:58,558 --> 06:56:02,478
to give back two values alternatively
11385
06:56:00,958 --> 06:56:04,878
what we could do is actually just turn
11386
06:56:02,478 --> 06:56:07,520
back void and then we can say string
11387
06:56:04,878 --> 06:56:10,200
name and then inage but we're going to
11388
06:56:07,520 --> 06:56:13,958
have to use references inside here and
11389
06:56:10,200 --> 06:56:17,240
then have string new name equals nothing
11390
06:56:13,958 --> 06:56:19,200
and then new inage equals zero so we
11391
06:56:17,240 --> 06:56:21,718
have string new name equals nothing and
11392
06:56:19,200 --> 06:56:23,680
then into New Age also equal to nothing
11393
06:56:21,718 --> 06:56:25,360
and then inside our function instead of
11394
06:56:23,680 --> 06:56:27,840
having out parameters we can actually
11395
06:56:25,360 --> 06:56:30,000
use a reference and we can say new name
11396
06:56:27,840 --> 06:56:32,240
and then ref New Age and what that will
11397
06:56:30,000 --> 06:56:34,000
do is we can just return it like this
11398
06:56:32,240 --> 06:56:35,680
because it's void instead of declaring a
11399
06:56:34,000 --> 06:56:37,120
new string name we can actually just
11400
06:56:35,680 --> 06:56:39,040
assign it to the reference that just
11401
06:56:37,120 --> 06:56:40,798
passed and because it's a void parameter
11402
06:56:39,040 --> 06:56:42,398
now and for the return type we don't
11403
06:56:40,798 --> 06:56:44,478
actually need to return anything so we
11404
06:56:42,398 --> 06:56:47,558
can actually assign these by reference
11405
06:56:44,478 --> 06:56:49,638
enter your name abber and 23 so as you
11406
06:56:47,558 --> 06:56:51,398
can see it's still works fine but let's
11407
06:56:49,638 --> 06:56:52,760
say we wanted to add another birth month
11408
06:56:51,398 --> 06:56:55,638
so we' have to take the birth month
11409
06:56:52,760 --> 06:56:58,160
variable new birth month make it equal
11410
06:56:55,638 --> 06:57:02,200
to zero and then add yet another
11411
06:56:58,160 --> 06:57:04,520
parameter ref new birth month and then
11412
06:57:02,200 --> 06:57:09,638
we need a new birth month inside
11413
06:57:04,520 --> 06:57:12,000
here ref int birth month and then we
11414
06:57:09,638 --> 06:57:12,878
need another one from here enter your
11415
06:57:12,000 --> 06:57:15,080
birth
11416
06:57:12,878 --> 06:57:18,200
month and then we'd store it inside
11417
06:57:15,080 --> 06:57:20,080
birth month and then finally we can also
11418
06:57:18,200 --> 06:57:22,520
return it up
11419
06:57:20,080 --> 06:57:26,360
here as new birth
11420
06:57:22,520 --> 06:57:28,040
month Here We Go abber 23 and then five
11421
06:57:26,360 --> 06:57:29,360
so we get them values being returned so
11422
06:57:28,040 --> 06:57:30,878
in order to keep adding different
11423
06:57:29,360 --> 06:57:33,398
properties to your code you have to make
11424
06:57:30,878 --> 06:57:35,200
a new parameter and reference variable
11425
06:57:33,398 --> 06:57:37,120
and then append the parameters inside
11426
06:57:35,200 --> 06:57:38,398
the function now the problem that is
11427
06:57:37,120 --> 06:57:40,398
even though you can have an infinite
11428
06:57:38,398 --> 06:57:42,240
amount of parameters then this gets very
11429
06:57:40,398 --> 06:57:44,280
tedious very quickly and you don't want
11430
06:57:42,240 --> 06:57:46,958
this so let's have a look at how we can
11431
06:57:44,280 --> 06:57:50,080
make this function exactly the same way
11432
06:57:46,958 --> 06:57:51,680
just using our new structure you can see
11433
06:57:50,080 --> 06:57:53,760
that instead of having to pass all of
11434
06:57:51,680 --> 06:57:55,798
these different things in or return back
11435
06:57:53,760 --> 06:57:58,600
a certain value we can actually just
11436
06:57:55,798 --> 06:58:01,478
return back a complete person so all of
11437
06:57:58,600 --> 06:58:04,200
this can be squished pretty quietly so
11438
06:58:01,478 --> 06:58:06,760
actually let's just comment all of this
11439
06:58:04,200 --> 06:58:08,280
out and let's comment out this function
11440
06:58:06,760 --> 06:58:10,840
and let's have another
11441
06:58:08,280 --> 06:58:12,638
rewrite so now that everything's gone we
11442
06:58:10,840 --> 06:58:15,558
only want to be returning and dealing
11443
06:58:12,638 --> 06:58:17,320
with the person so we can say static and
11444
06:58:15,558 --> 06:58:19,840
instead of void we actually want to say
11445
06:58:17,320 --> 06:58:21,680
we want to give the user back a person
11446
06:58:19,840 --> 06:58:24,240
return person and we don't need any
11447
06:58:21,680 --> 06:58:26,478
parameters in this case this is only
11448
06:58:24,240 --> 06:58:28,398
passed in because it's a reference so we
11449
06:58:26,478 --> 06:58:30,160
can assign these values back to the
11450
06:58:28,398 --> 06:58:31,878
original variables but because we're
11451
06:58:30,160 --> 06:58:33,920
going to be returning a person then we
11452
06:58:31,878 --> 06:58:35,680
don't need to do that so we can
11453
06:58:33,920 --> 06:58:38,080
essentially take this code and put it in
11454
06:58:35,680 --> 06:58:40,760
here and we can say string name now we
11455
06:58:38,080 --> 06:58:43,160
have an INT here and then also an in
11456
06:58:40,760 --> 06:58:45,040
here there we go so now that we've
11457
06:58:43,160 --> 06:58:47,440
constructed all our variables that we
11458
06:58:45,040 --> 06:58:50,360
need for the person what we can do is we
11459
06:58:47,440 --> 06:58:53,320
can make a new person person person and
11460
06:58:50,360 --> 06:58:56,000
then person. name equals name and then
11461
06:58:53,320 --> 06:58:58,878
duplicate this down we say person. AG is
11462
06:58:56,000 --> 06:59:00,680
equal to age and finally the birth month
11463
06:58:58,878 --> 06:59:02,440
is equal to the birth
11464
06:59:00,680 --> 06:59:04,280
month and then now that we've
11465
06:59:02,440 --> 06:59:05,798
constructed our person we still got a
11466
06:59:04,280 --> 06:59:07,638
red underline because we haven't
11467
06:59:05,798 --> 06:59:09,760
returned anything yet and after all of
11468
06:59:07,638 --> 06:59:11,878
that we can say return
11469
06:59:09,760 --> 06:59:14,638
person there we
11470
06:59:11,878 --> 06:59:19,718
go and now simply in here we could just
11471
06:59:14,638 --> 06:59:22,280
say person person equals return person
11472
06:59:19,718 --> 06:59:24,120
there we go and then we can take this
11473
06:59:22,280 --> 06:59:25,798
console right line here and place it
11474
06:59:24,120 --> 06:59:28,680
straight underneath since it uses the
11475
06:59:25,798 --> 06:59:30,200
same properties now let's try and run it
11476
06:59:28,680 --> 06:59:32,958
h
11477
06:59:30,200 --> 06:59:35,080
235 there you go so now instead of
11478
06:59:32,958 --> 06:59:36,680
having to make a whole new variable all
11479
06:59:35,080 --> 06:59:38,638
you have to do is simply just add
11480
06:59:36,680 --> 06:59:40,160
another one down here so we can just say
11481
06:59:38,638 --> 06:59:42,160
number this could be your favorite
11482
06:59:40,160 --> 06:59:44,520
number or anything and we can add it
11483
06:59:42,160 --> 06:59:44,520
onto
11484
06:59:45,080 --> 06:59:50,240
here person.
11485
06:59:47,558 --> 06:59:53,680
number and then here we can paste this
11486
06:59:50,240 --> 06:59:56,200
down we can say enter your number and
11487
06:59:53,680 --> 06:59:58,600
then it'll be number in here we can
11488
06:59:56,200 --> 07:00:00,638
paste it down here and change this to
11489
06:59:58,600 --> 07:00:02,718
number and there we go that's all we
11490
07:00:00,638 --> 07:00:04,040
need to change and this C doesn't change
11491
07:00:02,718 --> 07:00:05,718
cuz it's simply calling back the
11492
07:00:04,040 --> 07:00:08,760
function and we're doing all the hard
11493
07:00:05,718 --> 07:00:13,600
work inside here so now if you run this
11494
07:00:08,760 --> 07:00:16,040
enter your name AB 23 5 and finally four
11495
07:00:13,600 --> 07:00:18,040
and there you go we get the values being
11496
07:00:16,040 --> 07:00:20,160
returned so this looks a little bit
11497
07:00:18,040 --> 07:00:22,478
tedious we have to assign each one
11498
07:00:20,160 --> 07:00:24,120
individually every single time we do it
11499
07:00:22,478 --> 07:00:26,240
so we can actually use something called
11500
07:00:24,120 --> 07:00:28,600
a Constructor in order to achieve this
11501
07:00:26,240 --> 07:00:30,558
result we make the person we set them
11502
07:00:28,600 --> 07:00:32,320
and then we return it it'd be easier if
11503
07:00:30,558 --> 07:00:34,240
we could have the Constructor that we
11504
07:00:32,320 --> 07:00:36,000
can assign straight inside here and then
11505
07:00:34,240 --> 07:00:37,920
just return it immediately instead of
11506
07:00:36,000 --> 07:00:40,360
having to do all of these lines and then
11507
07:00:37,920 --> 07:00:42,160
reference each one individually so let's
11508
07:00:40,360 --> 07:00:44,520
have a look and see what we can do
11509
07:00:42,160 --> 07:00:46,120
inside here we can have a new function
11510
07:00:44,520 --> 07:00:47,958
that'll just be called whatever the
11511
07:00:46,120 --> 07:00:49,920
structure is called then you open the
11512
07:00:47,958 --> 07:00:51,878
brackets close the brackets for now and
11513
07:00:49,920 --> 07:00:53,718
this is called a Constructor in order to
11514
07:00:51,878 --> 07:00:55,680
be able to use it down here we need to
11515
07:00:53,718 --> 07:00:57,878
make it public in order for someone else
11516
07:00:55,680 --> 07:01:00,000
to access it so this says that it needs
11517
07:00:57,878 --> 07:01:02,080
explicit parameters in order to be able
11518
07:01:00,000 --> 07:01:04,680
to compile it says that it needs to
11519
07:01:02,080 --> 07:01:06,638
assign name age birth month and number
11520
07:01:04,680 --> 07:01:09,280
so let's see how we can do that as we've
11521
07:01:06,638 --> 07:01:11,280
used Constructors in the past for
11522
07:01:09,280 --> 07:01:13,760
example when we had things like the
11523
07:01:11,280 --> 07:01:15,798
string Builder we had s SP equals new
11524
07:01:13,760 --> 07:01:17,360
string Builder and when you make a new
11525
07:01:15,798 --> 07:01:20,120
something that means you make a new
11526
07:01:17,360 --> 07:01:22,200
Constructor so all of these sixes are
11527
07:01:20,120 --> 07:01:23,718
the parameters that go inside of this
11528
07:01:22,200 --> 07:01:25,958
but each one of these is a separate
11529
07:01:23,718 --> 07:01:27,680
Constructor if we control click this we
11530
07:01:25,958 --> 07:01:29,360
should be able to see them all there we
11531
07:01:27,680 --> 07:01:31,240
go you see that the class is called
11532
07:01:29,360 --> 07:01:33,240
string Builder and this is called string
11533
07:01:31,240 --> 07:01:35,600
Builder and these two names match
11534
07:01:33,240 --> 07:01:37,878
exactly so to make a string Builder you
11535
07:01:35,600 --> 07:01:40,638
can give it any one of these as long as
11536
07:01:37,878 --> 07:01:42,160
your parameters match one of these six
11537
07:01:40,638 --> 07:01:44,558
you can give it nothing or you can give
11538
07:01:42,160 --> 07:01:46,600
it some other values and that's the same
11539
07:01:44,558 --> 07:01:48,920
is true with our current
11540
07:01:46,600 --> 07:01:51,360
person so what we need to do in this
11541
07:01:48,920 --> 07:01:53,520
case is we don't have multiple ways that
11542
07:01:51,360 --> 07:01:55,638
we can construct this in the case of our
11543
07:01:53,520 --> 07:01:57,558
person we have to provide all of these
11544
07:01:55,638 --> 07:01:59,200
details otherwise the compiler is not
11545
07:01:57,558 --> 07:02:02,878
going to like it so we can have a string
11546
07:01:59,200 --> 07:02:05,478
name int age int birth mon
11547
07:02:02,878 --> 07:02:06,878
and in number now we got our parameters
11548
07:02:05,478 --> 07:02:07,798
set up we actually need to make use of
11549
07:02:06,878 --> 07:02:11,160
them in
11550
07:02:07,798 --> 07:02:13,558
here so because we have name and name
11551
07:02:11,160 --> 07:02:15,680
and they're exactly the same string by
11552
07:02:13,558 --> 07:02:18,280
string then here we can't say name
11553
07:02:15,680 --> 07:02:20,440
equals name in terms of global and local
11554
07:02:18,280 --> 07:02:22,760
variables this is global within the
11555
07:02:20,440 --> 07:02:24,958
structure as it's within these two Co
11556
07:02:22,760 --> 07:02:27,878
braces and all of these variables are
11557
07:02:24,958 --> 07:02:29,878
only Within These two curly braces local
11558
07:02:27,878 --> 07:02:31,920
variables always take presidence over
11559
07:02:29,878 --> 07:02:33,638
the main one if I double click on this
11560
07:02:31,920 --> 07:02:35,878
you'll see that all three are
11561
07:02:33,638 --> 07:02:37,638
referencing this Constructor if I double
11562
07:02:35,878 --> 07:02:40,558
click on this you'll see that both name
11563
07:02:37,638 --> 07:02:42,718
references on line 20 down here is all
11564
07:02:40,558 --> 07:02:45,080
referencing this one in the parameter
11565
07:02:42,718 --> 07:02:46,798
and not the one up top we want it to be
11566
07:02:45,080 --> 07:02:48,638
assigned into here so you have two
11567
07:02:46,798 --> 07:02:50,760
options in this case you could
11568
07:02:48,638 --> 07:02:52,398
capitalize all of these so then you
11569
07:02:50,760 --> 07:02:54,520
don't run into this issue and then
11570
07:02:52,398 --> 07:02:56,840
instead of name equals name it's capital
11571
07:02:54,520 --> 07:02:58,280
N equals name and that fixes it
11572
07:02:56,840 --> 07:03:00,360
alternatively if you want it to be a
11573
07:02:58,280 --> 07:03:03,120
lowercase then what you can simply do is
11574
07:03:00,360 --> 07:03:04,840
you can place this Dot
11575
07:03:03,120 --> 07:03:06,920
if you use this it goes back to the
11576
07:03:04,840 --> 07:03:09,360
current scope and the current scope of
11577
07:03:06,920 --> 07:03:10,320
name is going to be this which is within
11578
07:03:09,360 --> 07:03:12,638
the
11579
07:03:10,320 --> 07:03:14,478
structure when you use this it doesn't
11580
07:03:12,638 --> 07:03:16,440
reference the local variables it
11581
07:03:14,478 --> 07:03:17,878
references the global variables up here
11582
07:03:16,440 --> 07:03:19,638
so when I double click on this now
11583
07:03:17,878 --> 07:03:22,360
you'll see that that gets highlighted so
11584
07:03:19,638 --> 07:03:24,798
we can say this. age equals age this.
11585
07:03:22,360 --> 07:03:27,878
birth month equals birth month and
11586
07:03:24,798 --> 07:03:29,558
finally this do number equals number so
11587
07:03:27,878 --> 07:03:31,280
we're just getting all the parameters
11588
07:03:29,558 --> 07:03:33,000
from here and putting them into these
11589
07:03:31,280 --> 07:03:34,638
values this might look a little bit
11590
07:03:33,000 --> 07:03:35,398
redundant but I'll show you why this is
11591
07:03:34,638 --> 07:03:37,958
a lot
11592
07:03:35,398 --> 07:03:40,240
cleaner so back in our return person
11593
07:03:37,958 --> 07:03:42,478
function we have the variable and then
11594
07:03:40,240 --> 07:03:44,600
we assign them all manually but look how
11595
07:03:42,478 --> 07:03:46,558
easy this now has became if I comment
11596
07:03:44,600 --> 07:03:48,478
out this return we can say a return
11597
07:03:46,558 --> 07:03:50,478
person so now we can reference person
11598
07:03:48,478 --> 07:03:52,398
with a capital P this time cuz I want to
11599
07:03:50,478 --> 07:03:53,840
reference the structure directly so
11600
07:03:52,398 --> 07:03:56,000
after the person we can open the
11601
07:03:53,840 --> 07:03:57,840
brackets and then start to type in so
11602
07:03:56,000 --> 07:04:01,040
our first one is name age and the birth
11603
07:03:57,840 --> 07:04:02,958
month name comma age comma birth month
11604
07:04:01,040 --> 07:04:04,440
comma and then number comma and then
11605
07:04:02,958 --> 07:04:07,798
this is not going to like it because we
11606
07:04:04,440 --> 07:04:07,798
also need to place new
11607
07:04:09,600 --> 07:04:14,440
inside as I said before when you making
11608
07:04:11,840 --> 07:04:15,680
a string Builder you have a new keyword
11609
07:04:14,440 --> 07:04:18,398
and this new means that you want to
11610
07:04:15,680 --> 07:04:20,520
create a new instance of this you could
11611
07:04:18,398 --> 07:04:22,718
think of this structure as a template
11612
07:04:20,520 --> 07:04:24,718
this is the template for something and
11613
07:04:22,718 --> 07:04:26,320
until you start using that something
11614
07:04:24,718 --> 07:04:28,120
it's actually not going to do anything
11615
07:04:26,320 --> 07:04:30,120
so we have to make a new instance of it
11616
07:04:28,120 --> 07:04:31,798
because it's not already available so we
11617
07:04:30,120 --> 07:04:33,958
need to make it available through our
11618
07:04:31,798 --> 07:04:35,520
data type so when we say we want a new
11619
07:04:33,958 --> 07:04:37,440
person that gives us access to the
11620
07:04:35,520 --> 07:04:39,320
Constructor if I remove that you'll see
11621
07:04:37,440 --> 07:04:41,320
we've got one of two now we can create
11622
07:04:39,320 --> 07:04:43,398
an empty person and that will just make
11623
07:04:41,320 --> 07:04:44,920
us assign all the values individually
11624
07:04:43,398 --> 07:04:46,718
but we don't want that we want to use
11625
07:04:44,920 --> 07:04:47,878
the second path and if you drop the
11626
07:04:46,718 --> 07:04:49,600
arrow you'll be able to see the
11627
07:04:47,878 --> 07:04:52,478
different parameters which is the ones
11628
07:04:49,600 --> 07:04:54,558
we just typed in name age birth month
11629
07:04:52,478 --> 07:04:55,958
and then number and now that we've
11630
07:04:54,558 --> 07:04:58,440
returned back this we actually don't
11631
07:04:55,958 --> 07:05:00,040
even need to do any of these lines right
11632
07:04:58,440 --> 07:05:02,280
here we can actually just return it
11633
07:05:00,040 --> 07:05:03,920
exactly like this so now if you it again
11634
07:05:02,280 --> 07:05:07,478
you should see exactly the same output
11635
07:05:03,920 --> 07:05:09,840
23 5 and four perfect so let's just have
11636
07:05:07,478 --> 07:05:11,680
a little recap so normally when we're
11637
07:05:09,840 --> 07:05:13,320
creating variables we use common
11638
07:05:11,680 --> 07:05:16,680
variables that we've used in the past
11639
07:05:13,320 --> 07:05:19,398
string in double Flo bu Etc but with the
11640
07:05:16,680 --> 07:05:21,240
use of structures we can create our own
11641
07:05:19,398 --> 07:05:23,360
group of variables and then we can use
11642
07:05:21,240 --> 07:05:25,040
them variables inside functions and
11643
07:05:23,360 --> 07:05:27,638
inside our code to make things a lot
11644
07:05:25,040 --> 07:05:29,798
cleaner and a lot easier so inside
11645
07:05:27,638 --> 07:05:31,798
person we actually store four different
11646
07:05:29,798 --> 07:05:33,680
variables and they can be an infin
11647
07:05:31,798 --> 07:05:35,240
amount of variables should you will and
11648
07:05:33,680 --> 07:05:37,120
you can easily remove them and then
11649
07:05:35,240 --> 07:05:39,120
remove them from the parameters and from
11650
07:05:37,120 --> 07:05:40,840
the assignment and then that's it if we
11651
07:05:39,120 --> 07:05:42,160
use this without our structure then
11652
07:05:40,840 --> 07:05:44,240
we're going to have to declare every
11653
07:05:42,160 --> 07:05:46,080
single variable separately and I know
11654
07:05:44,240 --> 07:05:47,478
this is the same as doing it up here
11655
07:05:46,080 --> 07:05:49,240
it's actually a little bit more work cuz
11656
07:05:47,478 --> 07:05:51,240
you need to create a new parameter and
11657
07:05:49,240 --> 07:05:52,440
create all the the instances inside here
11658
07:05:51,240 --> 07:05:54,600
and assign the
11659
07:05:52,440 --> 07:05:56,520
values now the benefit of doing it
11660
07:05:54,600 --> 07:05:59,080
within a structure is that when you go
11661
07:05:56,520 --> 07:06:01,798
to do a function look how easy this line
11662
07:05:59,080 --> 07:06:04,080
is before you'd have to have name a age
11663
07:06:01,798 --> 07:06:05,798
birth month all as references or have
11664
07:06:04,080 --> 07:06:07,478
them as out parameters and it gets a
11665
07:06:05,798 --> 07:06:09,080
little bit long and a bit sloppy in
11666
07:06:07,478 --> 07:06:12,200
order to include the month in here we'd
11667
07:06:09,080 --> 07:06:13,718
have to have another in number and you
11668
07:06:12,200 --> 07:06:15,398
can see that the parameters are growing
11669
07:06:13,718 --> 07:06:17,558
if you wanted to store a user from a
11670
07:06:15,398 --> 07:06:19,200
registration form you might have name
11671
07:06:17,558 --> 07:06:21,920
address phone number country and the
11672
07:06:19,200 --> 07:06:23,320
list goes on it can be a very long form
11673
07:06:21,920 --> 07:06:25,440
and you therefore you'll have a lot of
11674
07:06:23,320 --> 07:06:27,280
parameters so it's very easy to wrap it
11675
07:06:25,440 --> 07:06:28,920
around inside a structure and then
11676
07:06:27,280 --> 07:06:31,040
simply reference it like that and you'll
11677
07:06:28,920 --> 07:06:33,120
need zero parameters because everything
11678
07:06:31,040 --> 07:06:34,520
is contained within the data type and
11679
07:06:33,120 --> 07:06:36,680
then inside here we can ask for the
11680
07:06:34,520 --> 07:06:39,000
users's details individually store them
11681
07:06:36,680 --> 07:06:40,798
as their integers and string variables
11682
07:06:39,000 --> 07:06:43,040
and then instead of using this notation
11683
07:06:40,798 --> 07:06:44,878
we can now use our fancy Constructor
11684
07:06:43,040 --> 07:06:47,878
where we can pass it in in this exact
11685
07:06:44,878 --> 07:06:50,120
order right here name age birth month
11686
07:06:47,878 --> 07:06:52,280
and then the number perfect and then we
11687
07:06:50,120 --> 07:06:53,798
can use inside here we can call each of
11688
07:06:52,280 --> 07:06:56,440
these positions separately and
11689
07:06:53,798 --> 07:06:58,320
concatenate them
11690
07:06:56,440 --> 07:07:00,320
together so now that we covered
11691
07:06:58,320 --> 07:07:01,798
structures known by struct as the
11692
07:07:00,320 --> 07:07:03,280
keyword let's have a look look at
11693
07:07:01,798 --> 07:07:05,478
classes and how to define them
11694
07:07:03,280 --> 07:07:07,360
syntactically so we can make a struct
11695
07:07:05,478 --> 07:07:10,000
called person and we can give them a
11696
07:07:07,360 --> 07:07:12,840
string name and inage and to make them
11697
07:07:10,000 --> 07:07:15,958
accessible we'll make them public and we
11698
07:07:12,840 --> 07:07:18,080
can say public person string name and
11699
07:07:15,958 --> 07:07:20,120
int Agee and that means we can assign
11700
07:07:18,080 --> 07:07:23,600
these values using the Constructor so we
11701
07:07:20,120 --> 07:07:26,920
can say this.name equals name and this.
11702
07:07:23,600 --> 07:07:28,680
age equals age perfect so's get rid of
11703
07:07:26,920 --> 07:07:30,398
our compiler error since we're assigning
11704
07:07:28,680 --> 07:07:32,600
both the values and then down here we
11705
07:07:30,398 --> 07:07:35,638
can use it we can say person person
11706
07:07:32,600 --> 07:07:38,200
equals new person and then have abber
11707
07:07:35,638 --> 07:07:40,878
and then 23 in the brackets and we can
11708
07:07:38,200 --> 07:07:42,878
print to the screen person. name and
11709
07:07:40,878 --> 07:07:45,240
then if use contrl D to duplicate that
11710
07:07:42,878 --> 07:07:48,000
down and we can do age and if we run
11711
07:07:45,240 --> 07:07:50,080
that right now we should see abber in 23
11712
07:07:48,000 --> 07:07:52,558
being printed to the
11713
07:07:50,080 --> 07:07:54,840
console perfect so as we talked about
11714
07:07:52,558 --> 07:07:56,840
introduction syntactically the struct
11715
07:07:54,840 --> 07:07:58,240
and the classes are very similar so
11716
07:07:56,840 --> 07:08:00,040
let's comment this out so we don't get
11717
07:07:58,240 --> 07:08:01,760
any compile errors and let's see how
11718
07:08:00,040 --> 07:08:03,200
this is going to work so of course we're
11719
07:08:01,760 --> 07:08:04,920
going to get error on this line but once
11720
07:08:03,200 --> 07:08:07,760
we make our class the error should
11721
07:08:04,920 --> 07:08:09,920
disappear from here so instead of struct
11722
07:08:07,760 --> 07:08:12,680
person we're simply just going to have
11723
07:08:09,920 --> 07:08:14,280
class person exactly the same and
11724
07:08:12,680 --> 07:08:16,120
because these are fields we can also
11725
07:08:14,280 --> 07:08:18,680
copy these down so we can have public
11726
07:08:16,120 --> 07:08:22,120
string name and public int
11727
07:08:18,680 --> 07:08:25,718
Edge and then we can have public person
11728
07:08:22,120 --> 07:08:29,000
and then string name in Edge this. name
11729
07:08:25,718 --> 07:08:30,878
equals name and this. age equals age so
11730
07:08:29,000 --> 07:08:32,840
as you can see this is exactly the same
11731
07:08:30,878 --> 07:08:35,398
as this except for the fact we have
11732
07:08:32,840 --> 07:08:36,840
structure and we have class inside here
11733
07:08:35,398 --> 07:08:38,798
now you see how compile error is gone
11734
07:08:36,840 --> 07:08:40,360
and if youve won the code we should see
11735
07:08:38,798 --> 07:08:42,600
exactly the same
11736
07:08:40,360 --> 07:08:43,958
output there we go now the main
11737
07:08:42,600 --> 07:08:45,638
difference about classes and about
11738
07:08:43,958 --> 07:08:47,638
structures is that you don't actually
11739
07:08:45,638 --> 07:08:48,878
have to give these a default value
11740
07:08:47,638 --> 07:08:50,878
inside the
11741
07:08:48,878 --> 07:08:52,398
Constructor if we just comment this one
11742
07:08:50,878 --> 07:08:54,638
out for a
11743
07:08:52,398 --> 07:08:56,398
second and if we bring the structure
11744
07:08:54,638 --> 07:08:58,160
back and we actually remove the
11745
07:08:56,398 --> 07:09:00,200
parameters from here then we're actually
11746
07:08:58,160 --> 07:09:02,558
going to get compile errors and it says
11747
07:09:00,200 --> 07:09:04,840
that the name and age must be assigned
11748
07:09:02,558 --> 07:09:06,958
before it's returned and stroks cannot
11749
07:09:04,840 --> 07:09:08,760
have parameterless Constructors now
11750
07:09:06,958 --> 07:09:10,440
that's a bit of a problem in certain
11751
07:09:08,760 --> 07:09:12,080
cases you might want to create an
11752
07:09:10,440 --> 07:09:14,398
instance of something but not actually
11753
07:09:12,080 --> 07:09:16,520
derive any values so you could actually
11754
07:09:14,398 --> 07:09:18,478
make these into default values you can
11755
07:09:16,520 --> 07:09:21,240
have string name equals nothing and int
11756
07:09:18,478 --> 07:09:23,398
age equals z and therefore once we place
11757
07:09:21,240 --> 07:09:25,200
inside here then there's no problems if
11758
07:09:23,398 --> 07:09:27,240
you run the code then you will get the
11759
07:09:25,200 --> 07:09:30,040
output this will be nothing this will be
11760
07:09:27,240 --> 07:09:31,878
zero and you'll get errors and you won't
11761
07:09:30,040 --> 07:09:33,718
get any compile errors doing this
11762
07:09:31,878 --> 07:09:35,280
but the point is you have to include
11763
07:09:33,718 --> 07:09:37,520
them and you don't have a
11764
07:09:35,280 --> 07:09:39,600
choice so if we just comment this back
11765
07:09:37,520 --> 07:09:40,600
out now what you'll notice about the
11766
07:09:39,600 --> 07:09:43,440
class
11767
07:09:40,600 --> 07:09:45,160
person is even if we get rid of it then
11768
07:09:43,440 --> 07:09:46,558
it still lets us have it of course we
11769
07:09:45,160 --> 07:09:48,478
can't actually give it parameters since
11770
07:09:46,558 --> 07:09:50,320
we don't have a Constructor that lets us
11771
07:09:48,478 --> 07:09:52,160
have parameters but what you can do as
11772
07:09:50,320 --> 07:09:54,320
well is you can overload this and
11773
07:09:52,160 --> 07:09:56,200
actually include it as well so you can
11774
07:09:54,320 --> 07:09:58,040
have another set that literally let you
11775
07:09:56,200 --> 07:10:00,398
define both of
11776
07:09:58,040 --> 07:10:02,840
them and then you can even have another
11777
07:10:00,398 --> 07:10:05,280
overload that lets you just include the
11778
07:10:02,840 --> 07:10:06,680
name without the ede and then also
11779
07:10:05,280 --> 07:10:09,840
another one that lets you include the
11780
07:10:06,680 --> 07:10:12,080
int age without the name or you include
11781
07:10:09,840 --> 07:10:14,000
both the details so in these cases what
11782
07:10:12,080 --> 07:10:16,478
you can do is you can store the values
11783
07:10:14,000 --> 07:10:18,798
up here as the fields let them use a
11784
07:10:16,478 --> 07:10:20,878
default Constructor or only give it the
11785
07:10:18,798 --> 07:10:23,440
name or the age or you can actually give
11786
07:10:20,878 --> 07:10:24,958
it both so when we come down into here
11787
07:10:23,440 --> 07:10:27,680
we actually now have four different
11788
07:10:24,958 --> 07:10:29,920
Constructors we can make an empty person
11789
07:10:27,680 --> 07:10:32,040
we can make it with an edge with a name
11790
07:10:29,920 --> 07:10:33,320
or with both
11791
07:10:32,040 --> 07:10:36,558
and what you can do in here is you can
11792
07:10:33,320 --> 07:10:38,638
say this.name is equal to nothing and
11793
07:10:36,558 --> 07:10:40,958
then here you can say this. AG is equal
11794
07:10:38,638 --> 07:10:42,798
to minus one just to default it to a
11795
07:10:40,958 --> 07:10:44,000
value that's incorrect so after we've
11796
07:10:42,798 --> 07:10:45,718
got this let's have a look at the
11797
07:10:44,000 --> 07:10:47,760
different ways so we can give it a
11798
07:10:45,718 --> 07:10:50,160
person which gives us an empty value so
11799
07:10:47,760 --> 07:10:52,520
if you run that now we get blank and
11800
07:10:50,160 --> 07:10:54,600
zero and if we just give it the name
11801
07:10:52,520 --> 07:10:57,320
which is Aba then we should get ABA and
11802
07:10:54,600 --> 07:11:00,398
minus one yep and then if you just give
11803
07:10:57,320 --> 07:11:02,040
it the ede you should get nothing in 23
11804
07:11:00,398 --> 07:11:03,920
and if you actually give them both then
11805
07:11:02,040 --> 07:11:06,000
you should get both printed out to the
11806
07:11:03,920 --> 07:11:08,240
screen and there we
11807
07:11:06,000 --> 07:11:10,440
go so now we have four different ways
11808
07:11:08,240 --> 07:11:12,120
that we can create our person with our
11809
07:11:10,440 --> 07:11:14,478
structure we're only really limited to
11810
07:11:12,120 --> 07:11:16,638
one since all of these values need to be
11811
07:11:14,478 --> 07:11:18,320
assigned as the structure is created
11812
07:11:16,638 --> 07:11:20,478
structures are meant to be used as an
11813
07:11:18,320 --> 07:11:23,000
extension to a data type rather than
11814
07:11:20,478 --> 07:11:25,000
almost like a mini management system
11815
07:11:23,000 --> 07:11:26,638
when we make a class we have so many
11816
07:11:25,000 --> 07:11:28,600
options of different ways that we can do
11817
07:11:26,638 --> 07:11:30,398
this inside here and later on in the
11818
07:11:28,600 --> 07:11:32,478
course we're going to show you how to
11819
07:11:30,398 --> 07:11:34,200
include functions inside here and then
11820
07:11:32,478 --> 07:11:36,080
eventually do other Advanced Techniques
11821
07:11:34,200 --> 07:11:38,920
with classes that aren't directly
11822
07:11:36,080 --> 07:11:40,558
available structures if you have a basic
11823
07:11:38,920 --> 07:11:43,200
example where you want to extend
11824
07:11:40,558 --> 07:11:45,638
something to use inside a function or a
11825
07:11:43,200 --> 07:11:47,760
data type like storing name and age
11826
07:11:45,638 --> 07:11:49,520
instead of having to do two return types
11827
07:11:47,760 --> 07:11:51,360
inside a function then a struct is
11828
07:11:49,520 --> 07:11:53,558
perfect the second you want to do more
11829
07:11:51,360 --> 07:11:55,718
complicated things or make it a bit more
11830
07:11:53,558 --> 07:11:57,798
specific and flexible then classes of
11831
07:11:55,718 --> 07:12:00,080
the way forward you'll see it very often
11832
07:11:57,798 --> 07:12:01,558
in games where they have struct position
11833
07:12:00,080 --> 07:12:03,240
you'll see in video games where they
11834
07:12:01,558 --> 07:12:05,440
have something like a struct position
11835
07:12:03,240 --> 07:12:07,520
and they might have int X int Y and int
11836
07:12:05,440 --> 07:12:09,920
Zed and that's for their 3D position or
11837
07:12:07,520 --> 07:12:11,840
they might even just have an X and Y or
11838
07:12:09,920 --> 07:12:15,120
you could have something simple like a
11839
07:12:11,840 --> 07:12:17,240
rectangle and you can give it a width
11840
07:12:15,120 --> 07:12:19,200
and a length so now when you use that
11841
07:12:17,240 --> 07:12:20,878
inside the code you're going to have
11842
07:12:19,200 --> 07:12:22,520
different flexibilities of the amount of
11843
07:12:20,878 --> 07:12:23,718
data types that you can store and then
11844
07:12:22,520 --> 07:12:25,958
when you return that back from a
11845
07:12:23,718 --> 07:12:28,280
function that'll be okay as well since
11846
07:12:25,958 --> 07:12:29,600
you can use this as a data type so if
11847
07:12:28,280 --> 07:12:31,638
you want something simple and
11848
07:12:29,600 --> 07:12:33,080
straightforward then go for the
11849
07:12:31,638 --> 07:12:35,040
the second you want something a bit more
11850
07:12:33,080 --> 07:12:37,000
complicated and a bit flexible then you
11851
07:12:35,040 --> 07:12:38,958
probably want a class we'll go into more
11852
07:12:37,000 --> 07:12:40,840
different situations inside this course
11853
07:12:38,958 --> 07:12:42,638
so you understand in which cases to use
11854
07:12:40,840 --> 07:12:44,520
what and in most cases you'll notice
11855
07:12:42,638 --> 07:12:45,958
that the class is more replicable so if
11856
07:12:44,520 --> 07:12:47,680
you wanted to you could ask the user to
11857
07:12:45,958 --> 07:12:49,718
enter these details in and then you can
11858
07:12:47,680 --> 07:12:52,398
pass them inside the Constructor so we
11859
07:12:49,718 --> 07:12:54,878
could say console right line change it
11860
07:12:52,398 --> 07:12:59,040
to a right enter your
11861
07:12:54,878 --> 07:13:01,160
name String name console. readline and
11862
07:12:59,040 --> 07:13:03,920
then copy and pastees down changes into
11863
07:13:01,160 --> 07:13:06,478
into a in Edge and then wrap this around
11864
07:13:03,920 --> 07:13:08,440
inside of convert in 32 instead of enter
11865
07:13:06,478 --> 07:13:10,000
your name it'll be ENT your Edge and
11866
07:13:08,440 --> 07:13:11,798
then C these are grade out it means we
11867
07:13:10,000 --> 07:13:14,958
haven't used them so we can change abber
11868
07:13:11,798 --> 07:13:17,320
to your name and then change 23 to age
11869
07:13:14,958 --> 07:13:20,398
perfect so now if you run the code now
11870
07:13:17,320 --> 07:13:22,520
enter your name ABA enter your age 23
11871
07:13:20,398 --> 07:13:25,120
then we get ABA 23 printed out to the
11872
07:13:22,520 --> 07:13:26,760
screen so just a quick recap
11873
07:13:25,120 --> 07:13:29,160
Constructors are basically defined the
11874
07:13:26,760 --> 07:13:30,840
same way you have public followed by the
11875
07:13:29,160 --> 07:13:33,040
class or the structure name and then the
11876
07:13:30,840 --> 07:13:35,440
pr inside the brackets and then you can
11877
07:13:33,040 --> 07:13:37,200
assign it after without structure you
11878
07:13:35,440 --> 07:13:39,120
have to declare all of the variables
11879
07:13:37,200 --> 07:13:40,878
inside the parameter and assign them all
11880
07:13:39,120 --> 07:13:43,200
down here otherwise you won't be able to
11881
07:13:40,878 --> 07:13:45,478
run your code with classes we have the
11882
07:13:43,200 --> 07:13:47,280
flexibility of just including one
11883
07:13:45,478 --> 07:13:49,478
default function which will just be our
11884
07:13:47,280 --> 07:13:51,600
Constructor and even if it does nothing
11885
07:13:49,478 --> 07:13:53,520
this is still acceptable and then we
11886
07:13:51,600 --> 07:13:55,120
broke it down to multiple sections where
11887
07:13:53,520 --> 07:13:57,040
you might have situations where you only
11888
07:13:55,120 --> 07:13:59,080
want their name or their age or you
11889
07:13:57,040 --> 07:14:00,600
might even collect both to store it if
11890
07:13:59,080 --> 07:14:02,920
you collect their name then we'll
11891
07:14:00,600 --> 07:14:04,958
initialize age to minus one just so you
11892
07:14:02,920 --> 07:14:06,558
know that this is a bad value and if you
11893
07:14:04,958 --> 07:14:08,798
declare only the age then will'll
11894
07:14:06,558 --> 07:14:11,000
initialize aim to nothing just so you
11895
07:14:08,798 --> 07:14:13,798
know that it's an empty value and if you
11896
07:14:11,000 --> 07:14:16,280
accept both then we simply assign both
11897
07:14:13,798 --> 07:14:17,840
and down here we have access to it we
11898
07:14:16,280 --> 07:14:20,120
can even make this a little bit better
11899
07:14:17,840 --> 07:14:23,638
by checking these values so we can say
11900
07:14:20,120 --> 07:14:26,000
if string is not null or empty of
11901
07:14:23,638 --> 07:14:31,280
person. name and we can put a knot
11902
07:14:26,000 --> 07:14:32,840
inside here then we can print out this
11903
07:14:31,280 --> 07:14:36,280
and then the same thing for age we can
11904
07:14:32,840 --> 07:14:38,760
say if person. AG we can say greater
11905
07:14:36,280 --> 07:14:40,920
than minus one since we set minus one as
11906
07:14:38,760 --> 07:14:43,000
the bad value and then we can move this
11907
07:14:40,920 --> 07:14:44,080
line up and let's try run it now so
11908
07:14:43,000 --> 07:14:46,120
these two if statements are
11909
07:14:44,080 --> 07:14:47,638
fundamentally separate from each other
11910
07:14:46,120 --> 07:14:49,798
one is checking the name and one is
11911
07:14:47,638 --> 07:14:51,440
checking the age so let's try and
11912
07:14:49,798 --> 07:14:54,040
manually type these in and see what
11913
07:14:51,440 --> 07:14:56,040
happens so enter your name let's make
11914
07:14:54,040 --> 07:14:58,958
this blank and only include the age and
11915
07:14:56,040 --> 07:15:01,240
let's see if this runs okay so you had
11916
07:14:58,958 --> 07:15:03,478
no name and the age is 23 then you see
11917
07:15:01,240 --> 07:15:05,920
we only get printed 23 because it'll
11918
07:15:03,478 --> 07:15:07,280
skip over this line as you can see if we
11919
07:15:05,920 --> 07:15:10,160
put a break point in now and just
11920
07:15:07,280 --> 07:15:13,120
refresh the code we get empty name plus
11921
07:15:10,160 --> 07:15:15,000
23 and when we press F10 over this line
11922
07:15:13,120 --> 07:15:16,798
it jumps the next one because if we
11923
07:15:15,000 --> 07:15:19,000
hover over this you'll see that it
11924
07:15:16,798 --> 07:15:20,958
should return us back with false there
11925
07:15:19,000 --> 07:15:23,080
we go we get strings null or empty is
11926
07:15:20,958 --> 07:15:25,040
false so that we skip over that and
11927
07:15:23,080 --> 07:15:26,958
because the age is 23 then this line
11928
07:15:25,040 --> 07:15:30,120
going to execute let's run it again and
11929
07:15:26,958 --> 07:15:32,040
do the opposite we could say name is Aba
11930
07:15:30,120 --> 07:15:34,280
and then age is min one and then we get
11931
07:15:32,040 --> 07:15:37,000
ABA printed be not the
11932
07:15:34,280 --> 07:15:39,080
edge there we go so if you wanted to fix
11933
07:15:37,000 --> 07:15:40,840
these in and type them manually so we
11934
07:15:39,080 --> 07:15:43,798
can say
11935
07:15:40,840 --> 07:15:45,638
23 there we go we get 23 and if we only
11936
07:15:43,798 --> 07:15:47,240
include the
11937
07:15:45,638 --> 07:15:49,958
name
11938
07:15:47,240 --> 07:15:52,320
AB then we only get Al printed out and
11939
07:15:49,958 --> 07:15:56,798
if we include both of
11940
07:15:52,320 --> 07:15:58,798
them then we get them both printed out
11941
07:15:56,798 --> 07:16:00,440
perfect so now that we've created our
11942
07:15:58,798 --> 07:16:02,080
class we've gave it some fields and
11943
07:16:00,440 --> 07:16:03,920
we've made our Constructor that lets us
11944
07:16:02,080 --> 07:16:05,958
initialize it let's see how we can print
11945
07:16:03,920 --> 07:16:08,718
the details out to the console so we can
11946
07:16:05,958 --> 07:16:11,040
have a CW tab tab a console right line
11947
07:16:08,718 --> 07:16:13,440
and then we can say string interpolation
11948
07:16:11,040 --> 07:16:16,360
name callon space and then we can have
11949
07:16:13,440 --> 07:16:18,120
our curly braces and we can say person.
11950
07:16:16,360 --> 07:16:20,240
name we can access these fields using
11951
07:16:18,120 --> 07:16:22,360
the dot operator and then we can say
11952
07:16:20,240 --> 07:16:25,478
back sln for the new line and then say
11953
07:16:22,360 --> 07:16:27,398
age and the same thing person. AG let's
11954
07:16:25,478 --> 07:16:30,040
try run that and see what we've
11955
07:16:27,398 --> 07:16:31,798
got so now we've outputed name and age
11956
07:16:30,040 --> 07:16:33,558
out to the screen let's see how we can
11957
07:16:31,798 --> 07:16:35,040
do this inside a function if you wanted
11958
07:16:33,558 --> 07:16:36,520
to place this in different part of your
11959
07:16:35,040 --> 07:16:38,320
code then you're going to have to copy
11960
07:16:36,520 --> 07:16:39,878
and paste this and as we know this is
11961
07:16:38,320 --> 07:16:41,718
bad for our code because it's j a
11962
07:16:39,878 --> 07:16:43,440
duplication we want to be able to wrap
11963
07:16:41,718 --> 07:16:44,920
this inside a function somewhere so
11964
07:16:43,440 --> 07:16:47,200
let's see what we can do we can have a
11965
07:16:44,920 --> 07:16:48,520
static string so it can return as back
11966
07:16:47,200 --> 07:16:51,558
with this that we can output straight to
11967
07:16:48,520 --> 07:16:53,920
the console and we can say return
11968
07:16:51,558 --> 07:16:56,080
details and for our parameter we can
11969
07:16:53,920 --> 07:16:57,680
have something like string name and int
11970
07:16:56,080 --> 07:17:00,440
age because that's what our class is
11971
07:16:57,680 --> 07:17:01,638
made up of and inside here we can simply
11972
07:17:00,440 --> 07:17:03,558
take this
11973
07:17:01,638 --> 07:17:05,718
put it inside here and put a return
11974
07:17:03,558 --> 07:17:07,958
before it and then get rid of the person
11975
07:17:05,718 --> 07:17:10,200
dot since we have an aim and age
11976
07:17:07,958 --> 07:17:11,878
variables as local to the function so
11977
07:17:10,200 --> 07:17:13,718
now instead of this line we can
11978
07:17:11,878 --> 07:17:16,718
duplicate this down take out the
11979
07:17:13,718 --> 07:17:18,718
contents and we can say return details
11980
07:17:16,718 --> 07:17:21,958
and then we can place person. name and
11981
07:17:18,718 --> 07:17:24,760
person. AG inside here let's run the
11982
07:17:21,958 --> 07:17:26,360
code so we get exactly the same output
11983
07:17:24,760 --> 07:17:28,200
but the problem with this function is
11984
07:17:26,360 --> 07:17:30,398
we're having to type out the parameters
11985
07:17:28,200 --> 07:17:31,718
manually we have them here and we're
11986
07:17:30,398 --> 07:17:33,840
going to have to to type them again in
11987
07:17:31,718 --> 07:17:35,320
here and again for the parameters this
11988
07:17:33,840 --> 07:17:37,040
is a bit of a problem cuz we've just
11989
07:17:35,320 --> 07:17:38,840
made our class but we haven't really
11990
07:17:37,040 --> 07:17:40,360
used it when we're passing it in so
11991
07:17:38,840 --> 07:17:42,120
instead of having string name and int
11992
07:17:40,360 --> 07:17:43,920
age we can actually duplicate this
11993
07:17:42,120 --> 07:17:45,958
method and just comment it out just so
11994
07:17:43,920 --> 07:17:47,638
you can see it for reference and instead
11995
07:17:45,958 --> 07:17:50,160
of doing this you can actually just pass
11996
07:17:47,638 --> 07:17:52,200
in an entire person so now we have
11997
07:17:50,160 --> 07:17:54,600
access to person and we can bring this
11998
07:17:52,200 --> 07:17:57,080
back and we can say person. name and we
11999
07:17:54,600 --> 07:17:59,398
can also say person. age and the reason
12000
07:17:57,080 --> 07:18:01,440
why this method is preferred is if you
12001
07:17:59,398 --> 07:18:03,240
actually have something else public int
12002
07:18:01,440 --> 07:18:05,600
number and then you added another one
12003
07:18:03,240 --> 07:18:08,280
inside here in number and then you said
12004
07:18:05,600 --> 07:18:10,798
this do number equals number the
12005
07:18:08,280 --> 07:18:12,958
parameters won't have to change inside
12006
07:18:10,798 --> 07:18:15,120
the function all you have to do is
12007
07:18:12,958 --> 07:18:17,200
simply pass in the person and then now
12008
07:18:15,120 --> 07:18:19,638
we actually have access to doing person.
12009
07:18:17,200 --> 07:18:21,558
number inside here should we need to if
12010
07:18:19,638 --> 07:18:24,000
you're passing them all in one by one
12011
07:18:21,558 --> 07:18:25,558
like this then you have to append into
12012
07:18:24,000 --> 07:18:27,680
number on the end and then add it
12013
07:18:25,558 --> 07:18:29,958
straight to here and then also add it
12014
07:18:27,680 --> 07:18:31,760
inside here as you have like this it's
12015
07:18:29,958 --> 07:18:33,478
easy to pass the class in because all of
12016
07:18:31,760 --> 07:18:35,478
these properties above are just simply
12017
07:18:33,478 --> 07:18:38,200
used throughout the function so now if
12018
07:18:35,478 --> 07:18:38,200
we just bring this
12019
07:18:39,878 --> 07:18:43,160
back and let's have a run and see if we
12020
07:18:42,120 --> 07:18:46,958
get the same
12021
07:18:43,160 --> 07:18:49,080
output which we do name ABA age 23
12022
07:18:46,958 --> 07:18:51,040
perfect so now that we've thought of two
12023
07:18:49,080 --> 07:18:52,958
functions how to do this we can manually
12024
07:18:51,040 --> 07:18:54,798
type in the strings and the integer and
12025
07:18:52,958 --> 07:18:56,718
any other parameters that we need all we
12026
07:18:54,798 --> 07:18:58,398
can actually pass in the person the
12027
07:18:56,718 --> 07:19:00,080
problem with these two approaches is the
12028
07:18:58,398 --> 07:19:02,000
whole point of having classes and
12029
07:19:00,080 --> 07:19:03,798
structures of of the like is so we can
12030
07:19:02,000 --> 07:19:05,520
wrap around lots of functionality inside
12031
07:19:03,798 --> 07:19:07,040
it if you want to send this to another
12032
07:19:05,520 --> 07:19:08,878
developer for them to use in their
12033
07:19:07,040 --> 07:19:10,680
project or maybe this is a file that is
12034
07:19:08,878 --> 07:19:12,878
shared within the project then you don't
12035
07:19:10,680 --> 07:19:14,920
want them to include a separate function
12036
07:19:12,878 --> 07:19:16,760
that is simply outside of the class we
12037
07:19:14,920 --> 07:19:19,040
want to include the functions inside the
12038
07:19:16,760 --> 07:19:22,080
class so of course as you know these are
12039
07:19:19,040 --> 07:19:24,280
functions so inside here we can also
12040
07:19:22,080 --> 07:19:27,760
make functions so let's do it so we can
12041
07:19:24,280 --> 07:19:29,920
have public and we want a string return
12042
07:19:27,760 --> 07:19:32,120
details and then we don't need to pass
12043
07:19:29,920 --> 07:19:33,920
anything in the the values that we're
12044
07:19:32,120 --> 07:19:35,920
going to be returning are these two
12045
07:19:33,920 --> 07:19:37,798
right here which is technically local to
12046
07:19:35,920 --> 07:19:40,200
our class so therefore we're not going
12047
07:19:37,798 --> 07:19:42,398
to have any parameters I'll explain that
12048
07:19:40,200 --> 07:19:44,840
further in a second so we can take this
12049
07:19:42,398 --> 07:19:46,878
exact line paste it straight into here
12050
07:19:44,840 --> 07:19:48,558
and get rid of the person Dot and get
12051
07:19:46,878 --> 07:19:50,000
rid of the person do on this side so
12052
07:19:48,558 --> 07:19:52,000
then we have the name variable and the
12053
07:19:50,000 --> 07:19:54,000
age variable if you double click on them
12054
07:19:52,000 --> 07:19:56,160
you see it references these variables
12055
07:19:54,000 --> 07:19:58,000
and the same thing for age the reason
12056
07:19:56,160 --> 07:19:59,718
why we don't have parameters is when you
12057
07:19:58,000 --> 07:20:01,638
call this function as I'll show you in a
12058
07:19:59,718 --> 07:20:03,398
second you call it directly on the
12059
07:20:01,638 --> 07:20:05,520
person so the person that you're trying
12060
07:20:03,398 --> 07:20:07,680
to print has already got these contents
12061
07:20:05,520 --> 07:20:09,440
inside of them so let's have a look we
12062
07:20:07,680 --> 07:20:11,440
can duplicate this down and instead of
12063
07:20:09,440 --> 07:20:13,120
calling return details and passing the
12064
07:20:11,440 --> 07:20:16,280
person you actually do it the other way
12065
07:20:13,120 --> 07:20:18,440
around you say the person object dot
12066
07:20:16,280 --> 07:20:21,320
return details so now we're calling
12067
07:20:18,440 --> 07:20:24,240
return details inside our person
12068
07:20:21,320 --> 07:20:26,040
object now let's run the code you see
12069
07:20:24,240 --> 07:20:27,958
that the output is exactly the same
12070
07:20:26,040 --> 07:20:30,080
except now this functionality is a lot
12071
07:20:27,958 --> 07:20:32,320
better our return details is wrapped
12072
07:20:30,080 --> 07:20:34,558
inside our class which is the preferred
12073
07:20:32,320 --> 07:20:36,920
way now we can actually get rid of these
12074
07:20:34,558 --> 07:20:38,760
inside here and then we shouldn't really
12075
07:20:36,920 --> 07:20:40,520
use this notation even though it is
12076
07:20:38,760 --> 07:20:42,798
technically correct but you want to wrap
12077
07:20:40,520 --> 07:20:44,520
all your functionality inside here and
12078
07:20:42,798 --> 07:20:46,958
just bear in mind that scope is also
12079
07:20:44,520 --> 07:20:49,040
applicable here return details is within
12080
07:20:46,958 --> 07:20:50,878
this class which means if you try to run
12081
07:20:49,040 --> 07:20:53,160
this function inside the main then it's
12082
07:20:50,878 --> 07:20:55,080
not going to node exist this return
12083
07:20:53,160 --> 07:20:57,200
details function was able to be used
12084
07:20:55,080 --> 07:20:59,440
inside here because this was in the
12085
07:20:57,200 --> 07:21:02,478
scope of the class program which is this
12086
07:20:59,440 --> 07:21:04,398
curly brace and this curly bra but this
12087
07:21:02,478 --> 07:21:06,280
return details is only within the scope
12088
07:21:04,398 --> 07:21:08,360
of this class which means you can't just
12089
07:21:06,280 --> 07:21:09,520
suddenly use it inside the main so
12090
07:21:08,360 --> 07:21:11,200
please bear that in mind when you
12091
07:21:09,520 --> 07:21:13,360
produce in these classes and you trying
12092
07:21:11,200 --> 07:21:15,080
to understand where everything goes
12093
07:21:13,360 --> 07:21:16,878
every function that you place inside
12094
07:21:15,080 --> 07:21:18,878
here is accessible through the dot
12095
07:21:16,878 --> 07:21:21,080
notation after the object once you've
12096
07:21:18,878 --> 07:21:23,718
created the class say person equals new
12097
07:21:21,080 --> 07:21:24,958
person and then after we type person dot
12098
07:21:23,718 --> 07:21:26,878
then we'll get a list of all the
12099
07:21:24,958 --> 07:21:30,120
functions the same way if you have
12100
07:21:26,878 --> 07:21:32,478
something else just like this and then
12101
07:21:30,120 --> 07:21:34,160
if you do person Dot and then you see
12102
07:21:32,478 --> 07:21:36,160
that something has appeared there so all
12103
07:21:34,160 --> 07:21:38,200
the functions that you create up inside
12104
07:21:36,160 --> 07:21:40,280
in this class for every person that you
12105
07:21:38,200 --> 07:21:42,558
make it'll be accessible through the do
12106
07:21:40,280 --> 07:21:44,160
notation so just to round up this video
12107
07:21:42,558 --> 07:21:46,280
you should always make your functions
12108
07:21:44,160 --> 07:21:48,240
inside your class having them inside the
12109
07:21:46,280 --> 07:21:50,080
class is the whole point of making a big
12110
07:21:48,240 --> 07:21:52,200
class and have lots of functionality
12111
07:21:50,080 --> 07:21:54,320
inside if you keep the function that
12112
07:21:52,200 --> 07:21:56,200
does exactly the same thing outside of
12113
07:21:54,320 --> 07:21:58,200
the class then if you were to share this
12114
07:21:56,200 --> 07:21:59,600
file or come back to it in the future
12115
07:21:58,200 --> 07:22:01,160
then you're not going to understand why
12116
07:21:59,600 --> 07:22:02,920
you've done this and and it's outside
12117
07:22:01,160 --> 07:22:04,878
the class keeping everything wrapped
12118
07:22:02,920 --> 07:22:07,558
inside the class is the best way forward
12119
07:22:04,878 --> 07:22:07,558
in most case
12120
07:22:07,600 --> 07:22:11,638
scenarios so now we have our class
12121
07:22:09,680 --> 07:22:13,760
person it's got a name and age a
12122
07:22:11,638 --> 07:22:15,760
Constructor and a function to return
12123
07:22:13,760 --> 07:22:17,760
back the details and we can simply use
12124
07:22:15,760 --> 07:22:19,840
it like this and return back the details
12125
07:22:17,760 --> 07:22:22,398
like this but so far we've covered how
12126
07:22:19,840 --> 07:22:24,478
to make classes Constructors Etc and
12127
07:22:22,398 --> 07:22:26,000
assign the values but the values are
12128
07:22:24,478 --> 07:22:28,120
only get assigned throughout the
12129
07:22:26,000 --> 07:22:30,080
Constructor but what if a different part
12130
07:22:28,120 --> 07:22:32,558
of our program the user wants to change
12131
07:22:30,080 --> 07:22:34,398
their name or update their age Etc then
12132
07:22:32,558 --> 07:22:37,040
what options do we have so you know we
12133
07:22:34,398 --> 07:22:39,000
can access our Fields using person. name
12134
07:22:37,040 --> 07:22:41,200
and then you can simply type equals and
12135
07:22:39,000 --> 07:22:44,520
then type in another name and we can say
12136
07:22:41,200 --> 07:22:46,478
person. AG equals 25 now we've changed
12137
07:22:44,520 --> 07:22:48,638
these properties if we use the console
12138
07:22:46,478 --> 07:22:50,398
right line and return details you'll see
12139
07:22:48,638 --> 07:22:52,478
that the values have indeed
12140
07:22:50,398 --> 07:22:54,478
changed as you can see there it's
12141
07:22:52,478 --> 07:22:57,398
changed the hurry and
12142
07:22:54,478 --> 07:22:59,000
25 this is okay except for the fact that
12143
07:22:57,398 --> 07:23:00,520
this shouldn't really be possible we
12144
07:22:59,000 --> 07:23:03,160
should try and restrict this Direct
12145
07:23:00,520 --> 07:23:04,920
corly by the use of gets and sets so for
12146
07:23:03,160 --> 07:23:07,120
now we're going to cover fields and how
12147
07:23:04,920 --> 07:23:08,760
to restrict certain values and later on
12148
07:23:07,120 --> 07:23:10,920
in the course we're going to go through
12149
07:23:08,760 --> 07:23:13,120
what properties are so before alter
12150
07:23:10,920 --> 07:23:15,360
properties got invented we had to use
12151
07:23:13,120 --> 07:23:17,680
gets and sets gets and sets are
12152
07:23:15,360 --> 07:23:20,200
essentially functions that will prevent
12153
07:23:17,680 --> 07:23:22,680
this from happening so the reason why we
12154
07:23:20,200 --> 07:23:25,240
can do this is simply because this
12155
07:23:22,680 --> 07:23:27,680
public word is placed right here if we
12156
07:23:25,240 --> 07:23:30,080
change it to private it means that name
12157
07:23:27,680 --> 07:23:32,398
is still accessible within here and here
12158
07:23:30,080 --> 07:23:35,200
which is only the limit of the class but
12159
07:23:32,398 --> 07:23:37,440
as you can see it's not applicable here
12160
07:23:35,200 --> 07:23:39,318
this is why you need the public Getters
12161
07:23:37,440 --> 07:23:41,920
and Setters so I'll explain that what
12162
07:23:39,318 --> 07:23:44,000
that means now so we prevent these from
12163
07:23:41,920 --> 07:23:46,160
accessing outside the class by making
12164
07:23:44,000 --> 07:23:48,318
them private so this functionality no
12165
07:23:46,160 --> 07:23:50,840
longer works so what you have to do
12166
07:23:48,318 --> 07:23:53,040
instead is for every field that you have
12167
07:23:50,840 --> 07:23:55,520
you need to create a GTO and a seter
12168
07:23:53,040 --> 07:23:57,200
function so in order to set a value we
12169
07:23:55,520 --> 07:23:58,680
can have public and because we're
12170
07:23:57,200 --> 07:24:00,878
setting it we don't need to return
12171
07:23:58,680 --> 07:24:04,080
anything so it'll be void and we can say
12172
07:24:00,878 --> 07:24:06,120
set name and then we pass in string name
12173
07:24:04,080 --> 07:24:09,040
and then we simply just say this. name
12174
07:24:06,120 --> 07:24:11,160
equals name so when someone call set
12175
07:24:09,040 --> 07:24:13,398
name and pass in the name like this we
12176
07:24:11,160 --> 07:24:15,360
just override it so let's see how that
12177
07:24:13,398 --> 07:24:18,718
looks so if we comment these two lines
12178
07:24:15,360 --> 07:24:21,160
out we can simply say person do set name
12179
07:24:18,718 --> 07:24:23,840
and then we can say Harry so this line
12180
07:24:21,160 --> 07:24:25,600
is directly replaced by this line and
12181
07:24:23,840 --> 07:24:28,200
now if you run this we'll see that Harry
12182
07:24:25,600 --> 07:24:31,080
should be changed there you go abber is
12183
07:24:28,200 --> 07:24:33,200
changed to Harry this is powerful
12184
07:24:31,080 --> 07:24:35,160
because what we can do now is restrict
12185
07:24:33,200 --> 07:24:36,840
the access inside here and make sure
12186
07:24:35,160 --> 07:24:41,200
that the value contains something
12187
07:24:36,840 --> 07:24:43,360
genuine we can say if string is null or
12188
07:24:41,200 --> 07:24:46,680
empty and then pass the name inside
12189
07:24:43,360 --> 07:24:48,680
there if the string isn't null or empty
12190
07:24:46,680 --> 07:24:51,040
then we assign it and if it is null or
12191
07:24:48,680 --> 07:24:53,520
empty then what we can do inside here is
12192
07:24:51,040 --> 07:24:55,798
we can say else this do name equals
12193
07:24:53,520 --> 07:24:58,080
nothing and we can keep it blank or we
12194
07:24:55,798 --> 07:24:59,958
can use string. empty just to make sure
12195
07:24:58,080 --> 07:25:02,000
that we know that the name has been
12196
07:24:59,958 --> 07:25:03,558
blank out if they've entered something
12197
07:25:02,000 --> 07:25:05,520
invalid if they've entered something
12198
07:25:03,558 --> 07:25:07,558
that's valid we assign it if it's
12199
07:25:05,520 --> 07:25:09,398
invalid then we don't assign it and we
12200
07:25:07,558 --> 07:25:10,878
just initialize it back to nothing and
12201
07:25:09,398 --> 07:25:12,318
because our options are endless with
12202
07:25:10,878 --> 07:25:14,360
this we can actually make this into
12203
07:25:12,318 --> 07:25:17,000
something else we can simply just say
12204
07:25:14,360 --> 07:25:19,120
invalid name so then what happens is if
12205
07:25:17,000 --> 07:25:20,360
we try and change this to nothing and we
12206
07:25:19,120 --> 07:25:22,840
run the
12207
07:25:20,360 --> 07:25:24,920
code we can see that we get invalid name
12208
07:25:22,840 --> 07:25:27,600
being printed to the screen and that's a
12209
07:25:24,920 --> 07:25:30,080
lot better than if we had string.
12210
07:25:27,600 --> 07:25:31,718
empty because if you had string. empty
12211
07:25:30,080 --> 07:25:33,558
then it would kind of say nothing then
12212
07:25:31,718 --> 07:25:35,240
it looks like something's went wrong if
12213
07:25:33,558 --> 07:25:37,878
you have something like invalid then
12214
07:25:35,240 --> 07:25:40,000
that gives you a bit of a Barr message
12215
07:25:37,878 --> 07:25:41,878
so we can have invalid name there so
12216
07:25:40,000 --> 07:25:44,318
using our Setter Let's us change our
12217
07:25:41,878 --> 07:25:46,398
name from down here without directly
12218
07:25:44,318 --> 07:25:48,600
exposing the public if he expose the
12219
07:25:46,398 --> 07:25:50,280
public then everyone who uses classes
12220
07:25:48,600 --> 07:25:52,798
can simply change the name to whatever
12221
07:25:50,280 --> 07:25:54,638
they want having it private and only
12222
07:25:52,798 --> 07:25:57,040
changing the name within the set name
12223
07:25:54,638 --> 07:25:58,878
function is very preferable because you
12224
07:25:57,040 --> 07:26:01,240
can restrict the access and what people
12225
07:25:58,878 --> 07:26:03,520
can set the values to if they try to set
12226
07:26:01,240 --> 07:26:06,240
it to something that's not valid then we
12227
07:26:03,520 --> 07:26:08,958
set it to invalid name whereas before
12228
07:26:06,240 --> 07:26:10,080
when we did this and if we make this
12229
07:26:08,958 --> 07:26:12,360
public
12230
07:26:10,080 --> 07:26:14,920
again we can simply set this to
12231
07:26:12,360 --> 07:26:17,200
something bad like null null is a very
12232
07:26:14,920 --> 07:26:19,718
bad value but it still lets us have it
12233
07:26:17,200 --> 07:26:21,840
and the reason why it's very bad is if
12234
07:26:19,718 --> 07:26:24,680
you try to do something like this
12235
07:26:21,840 --> 07:26:26,798
person. equals and just some empty
12236
07:26:24,680 --> 07:26:29,798
brackets now even if I just leave this
12237
07:26:26,798 --> 07:26:32,398
empty the fact that you try call equals
12238
07:26:29,798 --> 07:26:33,478
on S inside person. name is going to be
12239
07:26:32,398 --> 07:26:36,440
a bit of a
12240
07:26:33,478 --> 07:26:38,520
problem so we can say person object the
12241
07:26:36,440 --> 07:26:40,638
name property inside it and we checking
12242
07:26:38,520 --> 07:26:42,398
if it equals to Aba for example but
12243
07:26:40,638 --> 07:26:43,398
because we've just said it to null let's
12244
07:26:42,398 --> 07:26:45,558
see what
12245
07:26:43,398 --> 07:26:47,240
happens you see that we actually get an
12246
07:26:45,558 --> 07:26:49,240
exception it's a null reference
12247
07:26:47,240 --> 07:26:51,600
exception because if you hover over name
12248
07:26:49,240 --> 07:26:53,920
you see that returns back null nulls are
12249
07:26:51,600 --> 07:26:56,080
very bad in this case whereas if you
12250
07:26:53,920 --> 07:26:59,000
actually bring our function back and we
12251
07:26:56,080 --> 07:27:01,638
make this into a private and then if we
12252
07:26:59,000 --> 07:27:03,558
try and take this value our null value
12253
07:27:01,638 --> 07:27:06,440
and place it straight inside our set
12254
07:27:03,558 --> 07:27:08,280
name if you uncomment it get rid of the
12255
07:27:06,440 --> 07:27:10,440
quotation marks and if you actually try
12256
07:27:08,280 --> 07:27:12,558
and assign it to null let's see what
12257
07:27:10,440 --> 07:27:14,680
happens then we're going to get invalid
12258
07:27:12,558 --> 07:27:17,080
name and this prevents us from breaking
12259
07:27:14,680 --> 07:27:18,798
our code if a function like equals were
12260
07:27:17,080 --> 07:27:20,878
to be called on any of our properties
12261
07:27:18,798 --> 07:27:22,878
inside of name so now that we've done it
12262
07:27:20,878 --> 07:27:25,318
for our name we can also do it for our
12263
07:27:22,878 --> 07:27:28,440
age but before we do that we can also
12264
07:27:25,318 --> 07:27:30,638
call a function called get name return
12265
07:27:28,440 --> 07:27:32,798
details gives you back these details
12266
07:27:30,638 --> 07:27:34,958
in a very specific fashion but what if
12267
07:27:32,798 --> 07:27:37,280
you wanted to do something like this
12268
07:27:34,958 --> 07:27:39,718
your name is and then you wanted to do
12269
07:27:37,280 --> 07:27:42,000
person. name but of course you can't do
12270
07:27:39,718 --> 07:27:44,520
that because name is no longer public so
12271
07:27:42,000 --> 07:27:46,398
you can't reference it directly so we've
12272
07:27:44,520 --> 07:27:49,920
got a set name and then now we need to
12273
07:27:46,398 --> 07:27:52,120
do a get name so we can have public and
12274
07:27:49,920 --> 07:27:54,120
because name is a string the return type
12275
07:27:52,120 --> 07:27:56,878
is going to be string and we can say get
12276
07:27:54,120 --> 07:27:59,760
name and this function is very simple it
12277
07:27:56,878 --> 07:28:01,520
just returns back name and that's it
12278
07:27:59,760 --> 07:28:03,798
there's no no parameters because all it
12279
07:28:01,520 --> 07:28:06,120
does is return back a value and in this
12280
07:28:03,798 --> 07:28:08,280
case it's just returning back name so
12281
07:28:06,120 --> 07:28:11,040
instead of using person. name in here we
12282
07:28:08,280 --> 07:28:12,920
can say person do get name open close
12283
07:28:11,040 --> 07:28:14,840
the brackets to indicate it's a function
12284
07:28:12,920 --> 07:28:16,798
but there's no parameters now you see
12285
07:28:14,840 --> 07:28:18,920
that we have no compile errors so now if
12286
07:28:16,798 --> 07:28:21,040
you run that you'll see that it says
12287
07:28:18,920 --> 07:28:22,200
your name is invalid name so let's
12288
07:28:21,040 --> 07:28:25,440
actually give it a
12289
07:28:22,200 --> 07:28:28,280
value and we can say Harry again run the
12290
07:28:25,440 --> 07:28:30,318
code there you go your name is Harry so
12291
07:28:28,280 --> 07:28:31,360
it's officially change it now let's work
12292
07:28:30,318 --> 07:28:33,718
on the
12293
07:28:31,360 --> 07:28:37,160
edge so just following the exact same
12294
07:28:33,718 --> 07:28:41,240
approach we can have a public void set
12295
07:28:37,160 --> 07:28:43,958
Edge in age and instead of just doing
12296
07:28:41,240 --> 07:28:46,440
this. age equals age let's try set some
12297
07:28:43,958 --> 07:28:48,760
boundaries on this we can wrap this
12298
07:28:46,440 --> 07:28:50,360
inside an if statement so as for the
12299
07:28:48,760 --> 07:28:52,680
condition we want to validate if the age
12300
07:28:50,360 --> 07:28:54,958
is successful so we want to do if age is
12301
07:28:52,680 --> 07:28:57,600
greater than or equal to zero and age is
12302
07:28:54,958 --> 07:28:59,878
less than or equal to 150 so this means
12303
07:28:57,600 --> 07:29:02,160
if the age is less than zero or greater
12304
07:28:59,878 --> 07:29:03,840
than 150 then this is going to return
12305
07:29:02,160 --> 07:29:05,718
back false and then it's not going to
12306
07:29:03,840 --> 07:29:08,638
assign the value and we can simply
12307
07:29:05,718 --> 07:29:10,398
assign it to minus1 -1 is just so we
12308
07:29:08,638 --> 07:29:12,398
know it's a bad value so now that we've
12309
07:29:10,398 --> 07:29:14,920
created our set age we can actually make
12310
07:29:12,398 --> 07:29:17,080
our get age so we want to return back in
12311
07:29:14,920 --> 07:29:20,200
int because that's going to be the data
12312
07:29:17,080 --> 07:29:22,318
type for age so we do public int get age
12313
07:29:20,200 --> 07:29:24,280
we don't need any parameters cuz as like
12314
07:29:22,318 --> 07:29:27,040
the name we're just simply returning
12315
07:29:24,280 --> 07:29:28,478
something so we can say return age so
12316
07:29:27,040 --> 07:29:31,120
now that you've made the set age and the
12317
07:29:28,478 --> 07:29:33,080
get age let's actually use it quote so
12318
07:29:31,120 --> 07:29:36,878
we've made our set name and let's say
12319
07:29:33,080 --> 07:29:39,318
person do set age and we have 25 inside
12320
07:29:36,878 --> 07:29:40,878
here just like above and then now that
12321
07:29:39,318 --> 07:29:43,798
we've set the age let's appin this
12322
07:29:40,878 --> 07:29:47,120
sentence we can say and your age is and
12323
07:29:43,798 --> 07:29:49,200
then person. get age perfect so let's
12324
07:29:47,120 --> 07:29:53,040
run the code and see what happens there
12325
07:29:49,200 --> 07:29:55,318
we go we have 25 and Harry being
12326
07:29:53,040 --> 07:29:58,360
changed so another little tip when we do
12327
07:29:55,318 --> 07:30:00,398
in this code I get age as one line and
12328
07:29:58,360 --> 07:30:02,520
I'll set age as a couple of lines but
12329
07:30:00,398 --> 07:30:04,360
fundamentally only assigning Edge so
12330
07:30:02,520 --> 07:30:06,958
what we can do is we can make these into
12331
07:30:04,360 --> 07:30:09,360
the tertiary operator so as we can see
12332
07:30:06,958 --> 07:30:11,718
here this is the condition if this
12333
07:30:09,360 --> 07:30:13,840
returns back true then we assign age our
12334
07:30:11,718 --> 07:30:16,080
local parameter and if this returns back
12335
07:30:13,840 --> 07:30:19,000
false we assign minus one so we can
12336
07:30:16,080 --> 07:30:20,600
actually make this into this. AG equals
12337
07:30:19,000 --> 07:30:23,240
if you remember the format of our
12338
07:30:20,600 --> 07:30:26,240
conditional operator use condition true
12339
07:30:23,240 --> 07:30:28,000
and then condition false so this is our
12340
07:30:26,240 --> 07:30:30,638
condition that is leading our if
12341
07:30:28,000 --> 07:30:32,760
statement and if this returns back true
12342
07:30:30,638 --> 07:30:34,680
then we do question mark and put age in
12343
07:30:32,760 --> 07:30:36,920
there and then call on if it returns
12344
07:30:34,680 --> 07:30:38,558
back false then it's simply minus one
12345
07:30:36,920 --> 07:30:41,200
there we go so this is actually being
12346
07:30:38,558 --> 07:30:43,360
entirely replaced by this one simple
12347
07:30:41,200 --> 07:30:46,440
line and we can do the same thing for
12348
07:30:43,360 --> 07:30:48,958
our string so we can say this.name is
12349
07:30:46,440 --> 07:30:50,840
equal to string that is null or empty
12350
07:30:48,958 --> 07:30:53,318
pass the name inside there and if that
12351
07:30:50,840 --> 07:30:55,958
is true then we assign it to name and if
12352
07:30:53,318 --> 07:30:57,200
that is false we say invalid name let's
12353
07:30:55,958 --> 07:31:00,040
comment this one out and let's see if
12354
07:30:57,200 --> 07:31:03,440
our code runs okay still
12355
07:31:00,040 --> 07:31:05,760
hurry 25 hurry 25 and now let's try our
12356
07:31:03,440 --> 07:31:08,680
bad values so we can try to set the age
12357
07:31:05,760 --> 07:31:11,120
to 200 and the value to nothing and
12358
07:31:08,680 --> 07:31:13,440
let's see what happens we get invalid
12359
07:31:11,120 --> 07:31:15,958
name minus one and there you
12360
07:31:13,440 --> 07:31:18,360
go perfect so that's working okay let's
12361
07:31:15,958 --> 07:31:20,080
undo them values and you can see that
12362
07:31:18,360 --> 07:31:22,120
this has been shortened completely
12363
07:31:20,080 --> 07:31:23,398
another top tip for functions like this
12364
07:31:22,120 --> 07:31:25,520
you can actually use something called
12365
07:31:23,398 --> 07:31:27,760
Arrow functions and if you've done any
12366
07:31:25,520 --> 07:31:29,878
JavaScript this might be quite familiar
12367
07:31:27,760 --> 07:31:31,600
so we can have our get Edge and in
12368
07:31:29,878 --> 07:31:33,558
instead of open the curly brace at this
12369
07:31:31,600 --> 07:31:36,318
point we can actually place this Arrow
12370
07:31:33,558 --> 07:31:38,398
inside so if we comment out this one
12371
07:31:36,318 --> 07:31:41,080
with arrow functions you can only return
12372
07:31:38,398 --> 07:31:43,760
back one simple line so because this is
12373
07:31:41,080 --> 07:31:46,080
only one line we can have return Edge
12374
07:31:43,760 --> 07:31:48,200
but our functions don't like the keyword
12375
07:31:46,080 --> 07:31:50,200
return any variable that you put
12376
07:31:48,200 --> 07:31:52,080
straight inside here is automatically
12377
07:31:50,200 --> 07:31:55,120
applied that it's a return type so we
12378
07:31:52,080 --> 07:31:57,798
literally just put age get age returns
12379
07:31:55,120 --> 07:32:00,318
us back with age if this is confusing
12380
07:31:57,798 --> 07:32:02,440
then stick to this approach but this can
12381
07:32:00,318 --> 07:32:04,160
might tidy up your code because instead
12382
07:32:02,440 --> 07:32:06,680
of having all of this and all the curly
12383
07:32:04,160 --> 07:32:08,920
braces for one simple line you can use
12384
07:32:06,680 --> 07:32:11,360
Arrow function so if you actually copy
12385
07:32:08,920 --> 07:32:13,638
and paste but first comment out all of
12386
07:32:11,360 --> 07:32:16,558
these functions you can see how small
12387
07:32:13,638 --> 07:32:18,600
the code can actually get there we go
12388
07:32:16,558 --> 07:32:22,520
and then let's actually have a public
12389
07:32:18,600 --> 07:32:24,398
void set age let's put our R function in
12390
07:32:22,520 --> 07:32:26,360
and then we can take this one line and
12391
07:32:24,398 --> 07:32:28,280
paste it straight into here so you can
12392
07:32:26,360 --> 07:32:30,718
see that these two lines are now being
12393
07:32:28,280 --> 07:32:32,958
replaced by all of this
12394
07:32:30,718 --> 07:32:35,040
and of course we need our int age inside
12395
07:32:32,958 --> 07:32:37,638
here there we
12396
07:32:35,040 --> 07:32:40,398
go so now we can do the same thing with
12397
07:32:37,638 --> 07:32:44,760
our set name we can take
12398
07:32:40,398 --> 07:32:46,680
this we can have public viid set name
12399
07:32:44,760 --> 07:32:48,638
String name and then have our Arrow
12400
07:32:46,680 --> 07:32:49,958
function and then simply paste that in
12401
07:32:48,638 --> 07:32:52,638
you can see that this will get a little
12402
07:32:49,958 --> 07:32:54,280
bit long now but it's still ideal I've
12403
07:32:52,638 --> 07:32:56,558
got the code zoomed in but if you put
12404
07:32:54,280 --> 07:32:58,520
that to 100% you'll see that this code
12405
07:32:56,558 --> 07:33:00,240
is actually pretty small it's just I've
12406
07:32:58,520 --> 07:33:01,798
zoomed in just so you can see the code a
12407
07:33:00,240 --> 07:33:05,160
lot
12408
07:33:01,798 --> 07:33:07,558
clearer and then finally for public
12409
07:33:05,160 --> 07:33:10,600
string and we can get name and then we
12410
07:33:07,558 --> 07:33:11,878
simply just do arrow and then name and
12411
07:33:10,600 --> 07:33:13,718
you can see that with all this other
12412
07:33:11,878 --> 07:33:15,840
code commented we're just relying on
12413
07:33:13,718 --> 07:33:17,478
these functions as we just created and
12414
07:33:15,840 --> 07:33:19,520
you'll see there's no console errors or
12415
07:33:17,478 --> 07:33:21,920
anything else so if you try and run it
12416
07:33:19,520 --> 07:33:23,000
you'll see that everything is going okay
12417
07:33:21,920 --> 07:33:24,558
of course if you don't like this
12418
07:33:23,000 --> 07:33:27,000
notation and you don't have to include
12419
07:33:24,558 --> 07:33:28,840
it you can stick with the curly braces I
12420
07:33:27,000 --> 07:33:30,520
don't use the arrow functions a lot in
12421
07:33:28,840 --> 07:33:32,478
practice because there isn't many
12422
07:33:30,520 --> 07:33:34,360
situations where you'd actually use them
12423
07:33:32,478 --> 07:33:36,040
and even if you do some people might not
12424
07:33:34,360 --> 07:33:37,520
like them or there might be certain
12425
07:33:36,040 --> 07:33:38,638
guidelines that you might be following
12426
07:33:37,520 --> 07:33:41,240
inside the
12427
07:33:38,638 --> 07:33:44,240
company what I do suggest you stick with
12428
07:33:41,240 --> 07:33:46,360
is the tertiary operator these are a lot
12429
07:33:44,240 --> 07:33:48,600
good and a lot better to use since it
12430
07:33:46,360 --> 07:33:51,440
reduces the code drastically if we
12431
07:33:48,600 --> 07:33:52,680
actually remove this and I'll paste it
12432
07:33:51,440 --> 07:33:54,718
back in just so you can see it for
12433
07:33:52,680 --> 07:33:56,718
reference inside the resources but you
12434
07:33:54,718 --> 07:33:59,200
can see that these four functions are
12435
07:33:56,718 --> 07:34:00,680
now reduced to on one pitch you can see
12436
07:33:59,200 --> 07:34:02,920
that they're all got one lines and
12437
07:34:00,680 --> 07:34:04,318
they're pretty short so let me undo that
12438
07:34:02,920 --> 07:34:05,440
just so you can see the original code
12439
07:34:04,318 --> 07:34:07,520
and have it for
12440
07:34:05,440 --> 07:34:09,718
reference so there's a lot covered
12441
07:34:07,520 --> 07:34:12,760
inside this video so let's just have a
12442
07:34:09,718 --> 07:34:14,680
little recap before we finish up so just
12443
07:34:12,760 --> 07:34:16,318
to start at the beginning when we
12444
07:34:14,680 --> 07:34:18,200
initialize our person with their name
12445
07:34:16,318 --> 07:34:19,760
and their age we don't actually have any
12446
07:34:18,200 --> 07:34:22,080
other opportunities to change their
12447
07:34:19,760 --> 07:34:24,160
value which is where this comes in you
12448
07:34:22,080 --> 07:34:26,398
can actually just say a person. name and
12449
07:34:24,160 --> 07:34:28,120
assign any sort of value you can put
12450
07:34:26,398 --> 07:34:30,478
anything inside there and it'll assign
12451
07:34:28,120 --> 07:34:32,600
it directly unconditionally the problems
12452
07:34:30,478 --> 07:34:34,680
of doing this is someone might expose it
12453
07:34:32,600 --> 07:34:36,478
to null by accident and somewhere in
12454
07:34:34,680 --> 07:34:38,360
your code if you're using something like
12455
07:34:36,478 --> 07:34:40,000
equals this will actually prevent an
12456
07:34:38,360 --> 07:34:41,600
exception of course you can wrap this
12457
07:34:40,000 --> 07:34:43,360
inside a try catch but this is
12458
07:34:41,600 --> 07:34:45,360
unnecessary because we can prevent this
12459
07:34:43,360 --> 07:34:47,120
in the first place it's always best to
12460
07:34:45,360 --> 07:34:49,160
prevent an exception then to put
12461
07:34:47,120 --> 07:34:50,718
additional cord in like a try catch in
12462
07:34:49,160 --> 07:34:52,878
order to catch something that you can
12463
07:34:50,718 --> 07:34:54,760
prevent in the first place prevention is
12464
07:34:52,878 --> 07:34:57,200
definitely better than the Cure in this
12465
07:34:54,760 --> 07:34:59,840
case so having something like a person.
12466
07:34:57,200 --> 07:35:02,360
name being exposed it's not so much for
12467
07:34:59,840 --> 07:35:04,718
the integer because integers will always
12468
07:35:02,360 --> 07:35:06,760
have number values whereas strings they
12469
07:35:04,718 --> 07:35:09,280
can be initialized to null there is no
12470
07:35:06,760 --> 07:35:12,120
such thing as null when our age that our
12471
07:35:09,280 --> 07:35:13,958
default value for an integer is zero so
12472
07:35:12,120 --> 07:35:15,680
we don't really have the same issue we
12473
07:35:13,958 --> 07:35:17,638
started introducing the name function
12474
07:35:15,680 --> 07:35:19,798
and the age function these are used so
12475
07:35:17,638 --> 07:35:22,040
we can control what values get assigned
12476
07:35:19,798 --> 07:35:24,558
to our local variables and we also have
12477
07:35:22,040 --> 07:35:26,120
our get functions get name and get age
12478
07:35:24,558 --> 07:35:28,318
so let's explore how these are made
12479
07:35:26,120 --> 07:35:30,398
inside our Cass and expand on that and
12480
07:35:28,318 --> 07:35:32,040
see how they work so first we need to
12481
07:35:30,398 --> 07:35:34,200
make sure that our variables are
12482
07:35:32,040 --> 07:35:36,360
actually private they're private so we
12483
07:35:34,200 --> 07:35:37,958
can't access them just like this if I
12484
07:35:36,360 --> 07:35:39,718
copy and paste this line You'll see
12485
07:35:37,958 --> 07:35:41,558
we're going to get a compile error and
12486
07:35:39,718 --> 07:35:44,040
this error is because we can't actually
12487
07:35:41,558 --> 07:35:46,760
use it because as it says here it's
12488
07:35:44,040 --> 07:35:48,520
inaccessible due to its protection level
12489
07:35:46,760 --> 07:35:51,240
and the protection level comes from the
12490
07:35:48,520 --> 07:35:53,280
fact that this is indeed private it's
12491
07:35:51,240 --> 07:35:56,000
private so it's only accessible within
12492
07:35:53,280 --> 07:35:58,760
class person so every reference and name
12493
07:35:56,000 --> 07:36:00,360
can be used inside here just not outside
12494
07:35:58,760 --> 07:36:02,280
so the only way to be able to change the
12495
07:36:00,360 --> 07:36:04,478
name now you've made it private is by
12496
07:36:02,280 --> 07:36:06,080
enforcing this function the good thing
12497
07:36:04,478 --> 07:36:08,318
about this function is we can check if
12498
07:36:06,080 --> 07:36:10,280
the value is invalid and then we can
12499
07:36:08,318 --> 07:36:12,360
star it if it's valid and then change it
12500
07:36:10,280 --> 07:36:13,760
to something else if it's invalid and
12501
07:36:12,360 --> 07:36:15,920
then we can wrap this if statement
12502
07:36:13,760 --> 07:36:17,840
around in a conditional Operator just to
12503
07:36:15,920 --> 07:36:19,680
make it a little bit shorter and in
12504
07:36:17,840 --> 07:36:21,318
order to retrieve a name because now
12505
07:36:19,680 --> 07:36:24,280
that it made it public we can neither
12506
07:36:21,318 --> 07:36:26,000
set or retrieve it so we have our get
12507
07:36:24,280 --> 07:36:28,160
name function that simply just returns
12508
07:36:26,000 --> 07:36:30,520
it back to the screen nothing special
12509
07:36:28,160 --> 07:36:32,360
here and the same thing for our set age
12510
07:36:30,520 --> 07:36:34,920
we can check if the age is between 0 and
12511
07:36:32,360 --> 07:36:37,040
150 and assign it and if it's not assign
12512
07:36:34,920 --> 07:36:39,120
it to minus1 and then again wrap it
12513
07:36:37,040 --> 07:36:41,478
around in our conditional operator and
12514
07:36:39,120 --> 07:36:44,120
then for get age we can just return age
12515
07:36:41,478 --> 07:36:45,558
I demonstrated our functions inside here
12516
07:36:44,120 --> 07:36:47,398
you can take all of these on line
12517
07:36:45,558 --> 07:36:49,120
functions and actually add them all in
12518
07:36:47,398 --> 07:36:51,760
one line without the need of curly
12519
07:36:49,120 --> 07:36:53,718
braces if you want to return back in age
12520
07:36:51,760 --> 07:36:55,920
then you simply just type in age just
12521
07:36:53,718 --> 07:36:58,080
like you can see here and here when you
12522
07:36:55,920 --> 07:37:00,280
do assignment you want to specify this.
12523
07:36:58,080 --> 07:37:01,718
name equals but when you doing returns
12524
07:37:00,280 --> 07:37:03,680
you just need to type in the variable
12525
07:37:01,718 --> 07:37:05,958
that you need of course this looks a
12526
07:37:03,680 --> 07:37:07,760
little bit cleaner and a bit shorter but
12527
07:37:05,958 --> 07:37:09,600
not Everyone likes this notation so it's
12528
07:37:07,760 --> 07:37:11,318
completely up to the developer all the
12529
07:37:09,600 --> 07:37:13,680
environment that you're working in and
12530
07:37:11,318 --> 07:37:15,160
finally we just have our return details
12531
07:37:13,680 --> 07:37:17,600
and then we can make use of them just
12532
07:37:15,160 --> 07:37:19,280
like this by using person which is the
12533
07:37:17,600 --> 07:37:21,718
name of our local variable for our new
12534
07:37:19,280 --> 07:37:24,478
instance followed by a DOT and then set
12535
07:37:21,718 --> 07:37:26,718
name set age and then get name and then
12536
07:37:24,478 --> 07:37:26,718
get
12537
07:37:27,080 --> 07:37:31,200
age so now that we're dealing with
12538
07:37:29,080 --> 07:37:33,478
classes it's important you focus on the
12539
07:37:31,200 --> 07:37:35,280
scope we have a person person here
12540
07:37:33,478 --> 07:37:37,080
that's defined within the main which
12541
07:37:35,280 --> 07:37:39,878
means it's only Within These two curly
12542
07:37:37,080 --> 07:37:42,080
braces so if we have a static void and a
12543
07:37:39,878 --> 07:37:44,680
function that did something inside here
12544
07:37:42,080 --> 07:37:46,680
we can't access person inside here if we
12545
07:37:44,680 --> 07:37:49,360
take out the comment and we try it the
12546
07:37:46,680 --> 07:37:52,638
only reference we have is accessing the
12547
07:37:49,360 --> 07:37:54,360
class directly not this local variable
12548
07:37:52,638 --> 07:37:56,200
if you want to access local variable
12549
07:37:54,360 --> 07:37:58,360
from inside this function then you'd
12550
07:37:56,200 --> 07:38:00,398
simply have to pass it in like this and
12551
07:37:58,360 --> 07:38:02,360
then it'll finally become available as
12552
07:38:00,398 --> 07:38:04,638
you can see now there's two and it says
12553
07:38:02,360 --> 07:38:07,240
as a parameter and the same goes for
12554
07:38:04,638 --> 07:38:09,120
this class above just because we have
12555
07:38:07,240 --> 07:38:10,680
name and age up here it doesn't
12556
07:38:09,120 --> 07:38:13,840
necessarily mean that we can magically
12557
07:38:10,680 --> 07:38:16,600
use name down here if we have something
12558
07:38:13,840 --> 07:38:18,478
like a static int defined right here
12559
07:38:16,600 --> 07:38:20,520
then we can use this integer inside
12560
07:38:18,478 --> 07:38:23,240
these functions as it says there it's
12561
07:38:20,520 --> 07:38:25,000
available we can sign that to 10 and
12562
07:38:23,240 --> 07:38:26,958
it's also down here available and we can
12563
07:38:25,000 --> 07:38:29,160
sign that one to 20 and you see we have
12564
07:38:26,958 --> 07:38:30,920
no compil erors but if we take this and
12565
07:38:29,160 --> 07:38:32,200
put it inside our return details
12566
07:38:30,920 --> 07:38:34,040
function then you're going to get an
12567
07:38:32,200 --> 07:38:35,680
exception because X is not available
12568
07:38:34,040 --> 07:38:38,360
within that scope and the functional
12569
07:38:35,680 --> 07:38:40,360
scope matters inside here as well if you
12570
07:38:38,360 --> 07:38:42,920
have a string test inside
12571
07:38:40,360 --> 07:38:45,718
here we can just access it in another
12572
07:38:42,920 --> 07:38:48,160
function for example The Constructor we
12573
07:38:45,718 --> 07:38:49,958
can't just print out test as the screen
12574
07:38:48,160 --> 07:38:51,638
the handy things or classes if you want
12575
07:38:49,958 --> 07:38:53,840
variables to be available throughout the
12576
07:38:51,638 --> 07:38:56,160
class then you put them at the top we
12577
07:38:53,840 --> 07:38:57,478
can have that private string test and
12578
07:38:56,160 --> 07:38:59,798
there you go you see that the compiler
12579
07:38:57,478 --> 07:39:01,638
error is now gone and we give that
12580
07:38:59,798 --> 07:39:04,360
initial value and then you can use that
12581
07:39:01,638 --> 07:39:06,360
to print to the screen Etc and just to
12582
07:39:04,360 --> 07:39:09,120
note when you have a variable that's
12583
07:39:06,360 --> 07:39:11,440
called exactly the same thing we have a
12584
07:39:09,120 --> 07:39:13,958
private string test up here and we can
12585
07:39:11,440 --> 07:39:16,000
say hello then we also have a string
12586
07:39:13,958 --> 07:39:17,878
test down here so let me demonstrate
12587
07:39:16,000 --> 07:39:20,318
what happens when two variables are the
12588
07:39:17,878 --> 07:39:22,840
same so let's move this into our
12589
07:39:20,318 --> 07:39:24,798
Constructor so if we run this as it is
12590
07:39:22,840 --> 07:39:26,958
the test equals hello and then we print
12591
07:39:24,798 --> 07:39:29,878
out test and then run the
12592
07:39:26,958 --> 07:39:31,520
code there you go we get hello printed
12593
07:39:29,878 --> 07:39:33,760
and just ignore this this is from the
12594
07:39:31,520 --> 07:39:37,160
return details function as we've got in
12595
07:39:33,760 --> 07:39:39,080
the main down here return details so
12596
07:39:37,160 --> 07:39:41,600
we've made our new Constructor and then
12597
07:39:39,080 --> 07:39:43,160
assigned it the value with abber and 23
12598
07:39:41,600 --> 07:39:44,878
and then we can get hello printed to the
12599
07:39:43,160 --> 07:39:47,160
screen but let's say we actually made a
12600
07:39:44,878 --> 07:39:48,840
string test inside here and we said hi
12601
07:39:47,160 --> 07:39:51,120
instead of hello then which one do you
12602
07:39:48,840 --> 07:39:54,040
think it's used the global one or the
12603
07:39:51,120 --> 07:39:56,478
local one let's run it and find out it's
12604
07:39:54,040 --> 07:39:58,798
actually the local one the compiler will
12605
07:39:56,478 --> 07:40:00,638
always prioritize the local variables
12606
07:39:58,798 --> 07:40:02,600
over the glob Global variables and this
12607
07:40:00,638 --> 07:40:05,000
is the reason why you need to type this
12608
07:40:02,600 --> 07:40:06,760
dot because when you use name it's
12609
07:40:05,000 --> 07:40:09,520
always going to reference the parameter
12610
07:40:06,760 --> 07:40:11,120
when it's name on its own use console
12611
07:40:09,520 --> 07:40:12,958
right line and say name and then if we
12612
07:40:11,120 --> 07:40:14,958
do after this line and another console
12613
07:40:12,958 --> 07:40:16,600
right line and we say this. name then
12614
07:40:14,958 --> 07:40:18,558
these are completely different this one
12615
07:40:16,600 --> 07:40:20,718
is the parameter and this one is talking
12616
07:40:18,558 --> 07:40:23,478
about the variables from the class and
12617
07:40:20,718 --> 07:40:26,080
these are known as our Fields local
12618
07:40:23,478 --> 07:40:28,080
variables take presidence overall Fields
12619
07:40:26,080 --> 07:40:30,360
as long as they're called the same thing
12620
07:40:28,080 --> 07:40:32,280
as you can see now says it's assigned
12621
07:40:30,360 --> 07:40:33,840
but the value is never used if you take
12622
07:40:32,280 --> 07:40:36,520
the string test out then you can see
12623
07:40:33,840 --> 07:40:36,520
it's now being
12624
07:40:36,878 --> 07:40:41,920
used there you go hello gets printed out
12625
07:40:39,520 --> 07:40:43,760
to the screen and there you go the green
12626
07:40:41,920 --> 07:40:45,920
line has went because we're using it
12627
07:40:43,760 --> 07:40:47,798
down here scope is very important when
12628
07:40:45,920 --> 07:40:49,638
you're designing these classes and just
12629
07:40:47,798 --> 07:40:52,200
to note that the variables that are
12630
07:40:49,638 --> 07:40:54,080
available inside here are not going to
12631
07:40:52,200 --> 07:40:56,718
be necessarily available inside here
12632
07:40:54,080 --> 07:40:58,718
unless you make them public and even
12633
07:40:56,718 --> 07:41:00,318
when you make them public that doesn't
12634
07:40:58,718 --> 07:41:04,040
necessarily mean mean that this is still
12635
07:41:00,318 --> 07:41:06,120
available magically over here name will
12636
07:41:04,040 --> 07:41:08,440
only be available through an instance of
12637
07:41:06,120 --> 07:41:10,120
the person class so if we do person.
12638
07:41:08,440 --> 07:41:13,200
name now we can see that that's
12639
07:41:10,120 --> 07:41:14,478
available as our field so that's why
12640
07:41:13,200 --> 07:41:16,840
that makes it
12641
07:41:14,478 --> 07:41:19,160
public if we try and make this private
12642
07:41:16,840 --> 07:41:21,760
again just to return that value
12643
07:41:19,160 --> 07:41:23,718
back and we scroll down you'll see now
12644
07:41:21,760 --> 07:41:25,760
we get a compile error because it's
12645
07:41:23,718 --> 07:41:28,160
inaccessible because it's outside the
12646
07:41:25,760 --> 07:41:31,160
scope because it's now a private
12647
07:41:28,160 --> 07:41:31,160
variable
12648
07:41:31,638 --> 07:41:35,558
so we've covered what it means to have a
12649
07:41:33,200 --> 07:41:37,040
public or a private field if you have a
12650
07:41:35,558 --> 07:41:39,280
private field then you're going to have
12651
07:41:37,040 --> 07:41:41,280
to use Setters and Getters in order to
12652
07:41:39,280 --> 07:41:43,120
work with this field if you make it
12653
07:41:41,280 --> 07:41:45,200
directly public then you won't need any
12654
07:41:43,120 --> 07:41:48,000
Setters and Getters because the user can
12655
07:41:45,200 --> 07:41:50,398
just type in do name after their public
12656
07:41:48,000 --> 07:41:52,638
instance so you can have person followed
12657
07:41:50,398 --> 07:41:55,638
by the dot and right now you can't see
12658
07:41:52,638 --> 07:41:57,558
name tied here and the blue slittle icon
12659
07:41:55,638 --> 07:41:59,200
is no longer there the reason why it's
12660
07:41:57,558 --> 07:42:02,318
not there is cuz there's no public
12661
07:41:59,200 --> 07:42:04,718
Fields if we then change this to
12662
07:42:02,318 --> 07:42:07,200
public remove the dot and read it you'll
12663
07:42:04,718 --> 07:42:09,600
see that our blue square is now back
12664
07:42:07,200 --> 07:42:11,080
because our field is now accessible and
12665
07:42:09,600 --> 07:42:12,958
we can assign that to anything that we
12666
07:42:11,080 --> 07:42:15,240
want the whole point of making this
12667
07:42:12,958 --> 07:42:17,600
private is you only restrict the access
12668
07:42:15,240 --> 07:42:20,080
so you know what you're doing and the
12669
07:42:17,600 --> 07:42:21,638
reason why I say that is if they enter
12670
07:42:20,080 --> 07:42:23,840
something that's invalid then we want to
12671
07:42:21,638 --> 07:42:25,638
specify something like invalid if we
12672
07:42:23,840 --> 07:42:27,080
give them Public Access then we're not
12673
07:42:25,638 --> 07:42:29,280
going to know and we won't be able to
12674
07:42:27,080 --> 07:42:31,240
validate what they've entered so in
12675
07:42:29,280 --> 07:42:33,798
order to do it through a Setter we can
12676
07:42:31,240 --> 07:42:35,600
make it private create our set function
12677
07:42:33,798 --> 07:42:37,958
with a return type it's going to take in
12678
07:42:35,600 --> 07:42:39,878
a string parameter and this string has
12679
07:42:37,958 --> 07:42:42,440
to be the same data type as the prety
12680
07:42:39,878 --> 07:42:44,080
you're talking about call it name or
12681
07:42:42,440 --> 07:42:45,520
whatever you want it to be and because
12682
07:42:44,080 --> 07:42:47,520
these two are called exactly the same
12683
07:42:45,520 --> 07:42:49,520
thing we have to use this dot so we're
12684
07:42:47,520 --> 07:42:51,000
referencing this name instead of this
12685
07:42:49,520 --> 07:42:52,760
name in the parameters and here we're
12686
07:42:51,000 --> 07:42:54,440
just using a conditional operator and
12687
07:42:52,760 --> 07:42:56,840
we're saying if the string is not null
12688
07:42:54,440 --> 07:42:59,680
or empty hence the exclamation point
12689
07:42:56,840 --> 07:43:02,280
then we assign it to name name gets put
12690
07:42:59,680 --> 07:43:04,520
in this.name and if this were to return
12691
07:43:02,280 --> 07:43:07,478
back false then we place invalid name
12692
07:43:04,520 --> 07:43:09,478
straight into this. name and for the get
12693
07:43:07,478 --> 07:43:11,240
we simply just return a string since
12694
07:43:09,478 --> 07:43:13,360
that's a data type for name and then
12695
07:43:11,240 --> 07:43:15,240
just return it back and it's that simple
12696
07:43:13,360 --> 07:43:17,558
and the same thing for age we validate
12697
07:43:15,240 --> 07:43:19,520
the age range set it and if it's outside
12698
07:43:17,558 --> 07:43:21,680
this range we can set it to minus one so
12699
07:43:19,520 --> 07:43:24,360
as you can see this is a bit long we
12700
07:43:21,680 --> 07:43:27,240
have our private variables then we have
12701
07:43:24,360 --> 07:43:29,478
two functions one for set one for get
12702
07:43:27,240 --> 07:43:32,280
and the same thing for age so these are
12703
07:43:29,478 --> 07:43:34,120
called fields and gets and Setters so
12704
07:43:32,280 --> 07:43:36,280
I'm going to introduce a notion called
12705
07:43:34,120 --> 07:43:38,760
properties and properties make it easier
12706
07:43:36,280 --> 07:43:41,240
to do this exact notation so let's now
12707
07:43:38,760 --> 07:43:43,000
see how property is created since these
12708
07:43:41,240 --> 07:43:45,280
fields are private we need to make
12709
07:43:43,000 --> 07:43:47,120
something that's actually public so we
12710
07:43:45,280 --> 07:43:49,958
can do something like this we can say
12711
07:43:47,120 --> 07:43:53,360
public string and we can say name with a
12712
07:43:49,958 --> 07:43:55,360
capital N so now you can see that name
12713
07:43:53,360 --> 07:43:56,878
and name are not actually clashing and
12714
07:43:55,360 --> 07:43:58,840
we can assign that in here should we
12715
07:43:56,878 --> 07:44:01,798
want to and you see we don't need this
12716
07:43:58,840 --> 07:44:04,120
do anymore because this is now different
12717
07:44:01,798 --> 07:44:07,478
it's a capital N so this one always
12718
07:44:04,120 --> 07:44:09,280
references this and this references this
12719
07:44:07,478 --> 07:44:11,360
so we need to add something else onto
12720
07:44:09,280 --> 07:44:14,000
here so if you create them
12721
07:44:11,360 --> 07:44:16,000
both now you can see we can do age
12722
07:44:14,000 --> 07:44:18,120
equals age and there'll be no problems
12723
07:44:16,000 --> 07:44:20,638
with this so now that we've made our two
12724
07:44:18,120 --> 07:44:22,798
new Fields so now we have our two new
12725
07:44:20,638 --> 07:44:24,840
public variables which are known as our
12726
07:44:22,798 --> 07:44:26,798
Fields let's see how we can turn them
12727
07:44:24,840 --> 07:44:28,798
into properties so if you remove the
12728
07:44:26,798 --> 07:44:31,440
semicolon you can actually add in some
12729
07:44:28,798 --> 07:44:34,000
curve curly braces so inside here we
12730
07:44:31,440 --> 07:44:35,958
need a getter and Setter just like we
12731
07:44:34,000 --> 07:44:38,200
had down there but we have a quicker
12732
07:44:35,958 --> 07:44:39,878
notation for it instead of making two
12733
07:44:38,200 --> 07:44:41,680
distinct functions you can actually
12734
07:44:39,878 --> 07:44:44,558
create a little section inside these
12735
07:44:41,680 --> 07:44:46,600
curly braces to get and set the name so
12736
07:44:44,558 --> 07:44:49,360
you got to think of this as a middleman
12737
07:44:46,600 --> 07:44:51,360
the same way this is a middleman we call
12738
07:44:49,360 --> 07:44:53,520
this function and this function will
12739
07:44:51,360 --> 07:44:56,120
affect the name and this is going to be
12740
07:44:53,520 --> 07:44:58,280
the same principle we make use of name
12741
07:44:56,120 --> 07:45:00,840
with a capital N but it will actually be
12742
07:44:58,280 --> 07:45:02,520
affecting the lowercase end variable
12743
07:45:00,840 --> 07:45:04,520
these are going to be our new middle BS
12744
07:45:02,520 --> 07:45:06,840
and they will directly replace the gets
12745
07:45:04,520 --> 07:45:09,840
and sets here so what we can do in here
12746
07:45:06,840 --> 07:45:11,920
is we can say get press enter go on the
12747
07:45:09,840 --> 07:45:13,958
next line and make some C braces and
12748
07:45:11,920 --> 07:45:16,680
then we can also have a set so this is
12749
07:45:13,958 --> 07:45:18,360
like our mini get a mini set function
12750
07:45:16,680 --> 07:45:20,200
just like we've got here but as you can
12751
07:45:18,360 --> 07:45:21,798
see it's a lot shorter we don't need to
12752
07:45:20,200 --> 07:45:24,240
give it a distinct name or set the
12753
07:45:21,798 --> 07:45:26,878
public or anything else we just give it
12754
07:45:24,240 --> 07:45:29,200
a simple set and get so if you have a
12755
07:45:26,878 --> 07:45:31,398
look at the compiler error it says not
12756
07:45:29,200 --> 07:45:33,760
all cause path return of value because
12757
07:45:31,398 --> 07:45:36,040
as it says here this get function is
12758
07:45:33,760 --> 07:45:38,958
actually a mini function built into this
12759
07:45:36,040 --> 07:45:41,318
property person. name which is our name
12760
07:45:38,958 --> 07:45:43,240
with a capital N and then it's do get
12761
07:45:41,318 --> 07:45:45,040
and this returns back a string so if we
12762
07:45:43,240 --> 07:45:47,040
actually just type in return this then
12763
07:45:45,040 --> 07:45:48,718
it gets rid of the compiler error but of
12764
07:45:47,040 --> 07:45:50,760
course we don't want to return nothing
12765
07:45:48,718 --> 07:45:52,558
we actually want to return name because
12766
07:45:50,760 --> 07:45:55,160
remember what I said string name and
12767
07:45:52,558 --> 07:45:57,080
intage up here as our private field they
12768
07:45:55,160 --> 07:45:59,318
actually start the data the public
12769
07:45:57,080 --> 07:46:01,318
string name and the public inage is the
12770
07:45:59,318 --> 07:46:03,280
middle mans that coordinates all of the
12771
07:46:01,318 --> 07:46:05,920
gets and sets to do with the private
12772
07:46:03,280 --> 07:46:08,920
variables so for the set just for now we
12773
07:46:05,920 --> 07:46:11,160
can say name equals and then what do we
12774
07:46:08,920 --> 07:46:13,160
give it the value of so if we hover over
12775
07:46:11,160 --> 07:46:15,440
set it doesn't actually give you any
12776
07:46:13,160 --> 07:46:17,200
indication of what it's going to be so
12777
07:46:15,440 --> 07:46:19,798
let's have a look if we press control
12778
07:46:17,200 --> 07:46:21,680
space then we don't also get the list of
12779
07:46:19,798 --> 07:46:23,798
everything that we can add inside here
12780
07:46:21,680 --> 07:46:26,280
now it's very difficult to understand
12781
07:46:23,798 --> 07:46:28,080
what to assign this to when we have set
12782
07:46:26,280 --> 07:46:30,318
name we've gave it a string name
12783
07:46:28,080 --> 07:46:32,000
parameter so we can use name down here
12784
07:46:30,318 --> 07:46:33,360
so what's our parameter in this case
12785
07:46:32,000 --> 07:46:34,958
well the function itself doesn't
12786
07:46:33,360 --> 07:46:36,920
actually tell us but I'm telling you
12787
07:46:34,958 --> 07:46:38,878
right now that the parameter is actually
12788
07:46:36,920 --> 07:46:40,680
just called value and this is a very
12789
07:46:38,878 --> 07:46:42,360
default parameter that you'll see
12790
07:46:40,680 --> 07:46:44,638
everywhere when you're doing gets and
12791
07:46:42,360 --> 07:46:47,280
sets inside a property you don't have to
12792
07:46:44,638 --> 07:46:49,958
specify value or anything it just gets
12793
07:46:47,280 --> 07:46:52,040
simply assigned in just like here where
12794
07:46:49,958 --> 07:46:53,920
we have string name this will be a
12795
07:46:52,040 --> 07:46:55,558
string value so it will look something
12796
07:46:53,920 --> 07:46:57,280
like this behind the scenes but of
12797
07:46:55,558 --> 07:46:59,200
course you can't actually type this in
12798
07:46:57,280 --> 07:47:00,878
cuz remember these properties they
12799
07:46:59,200 --> 07:47:02,920
trying to give you a short hand on how
12800
07:47:00,878 --> 07:47:04,280
to do gets and Setters so you just got
12801
07:47:02,920 --> 07:47:06,878
to remember that when you're trying to
12802
07:47:04,280 --> 07:47:09,520
set you get the property inserted as
12803
07:47:06,878 --> 07:47:11,520
value so you can start that into name so
12804
07:47:09,520 --> 07:47:13,398
that's it the name is actually sorted
12805
07:47:11,520 --> 07:47:15,558
now so before we do the same thing for
12806
07:47:13,398 --> 07:47:18,000
age let's see how we can use this inside
12807
07:47:15,558 --> 07:47:19,958
the code so we scroll down to our main
12808
07:47:18,000 --> 07:47:21,360
we have a person person and then we have
12809
07:47:19,958 --> 07:47:24,878
a console right line that prints the
12810
07:47:21,360 --> 07:47:26,680
details so we can actually do person Dot
12811
07:47:24,878 --> 07:47:28,878
and you can see now that it's slightly
12812
07:47:26,680 --> 07:47:30,558
different if you ignore the edge since
12813
07:47:28,878 --> 07:47:32,398
start to return back as a field since we
12814
07:47:30,558 --> 07:47:34,600
haven't done that yet you'll see that
12815
07:47:32,398 --> 07:47:37,200
name now has a little Cog next to it
12816
07:47:34,600 --> 07:47:39,200
sure only properties this is a new icon
12817
07:47:37,200 --> 07:47:41,478
that we haven't saw yet so this is our
12818
07:47:39,200 --> 07:47:43,160
new icon it says it's going to return us
12819
07:47:41,478 --> 07:47:46,000
back with a string and we can actually
12820
07:47:43,160 --> 07:47:48,120
execute a get and execute a set straight
12821
07:47:46,000 --> 07:47:50,040
on this name then we can say name equals
12822
07:47:48,120 --> 07:47:52,318
Harry and we don't get any compiler
12823
07:47:50,040 --> 07:47:54,120
errors whatsoever this lets us do it
12824
07:47:52,318 --> 07:47:56,398
completely so now if we do another
12825
07:47:54,120 --> 07:47:58,558
console right line and we say person do
12826
07:47:56,398 --> 07:48:01,080
return details and let's run that and
12827
07:47:58,558 --> 07:48:04,398
you should see that the name gets
12828
07:48:01,080 --> 07:48:07,958
changed there we go ABA gets changed to
12829
07:48:04,398 --> 07:48:10,478
hurry perfect so after we've got person.
12830
07:48:07,958 --> 07:48:12,878
name let's see how we can also get the
12831
07:48:10,478 --> 07:48:15,318
value so we can just say string
12832
07:48:12,878 --> 07:48:18,200
interpolation your name is and then we
12833
07:48:15,318 --> 07:48:20,200
do person and followed by a DOT and as
12834
07:48:18,200 --> 07:48:22,478
you can see the exact same list that we
12835
07:48:20,200 --> 07:48:25,240
got given over here is given right here
12836
07:48:22,478 --> 07:48:27,520
as well name also says get and set and
12837
07:48:25,240 --> 07:48:30,160
it returns back a property which is a
12838
07:48:27,520 --> 07:48:31,360
string type so we can open like this and
12839
07:48:30,160 --> 07:48:33,440
you'll notice that we don't have the
12840
07:48:31,360 --> 07:48:35,558
parentheses after it and the reason for
12841
07:48:33,440 --> 07:48:37,360
that is because the brackets indicate
12842
07:48:35,558 --> 07:48:39,638
that you're calling a function but a
12843
07:48:37,360 --> 07:48:41,360
property is just a property it's almost
12844
07:48:39,638 --> 07:48:43,318
like a variable so you're not actually
12845
07:48:41,360 --> 07:48:45,120
needing to put any brackets after it
12846
07:48:43,318 --> 07:48:47,478
because there's no function to call so
12847
07:48:45,120 --> 07:48:49,798
as you can see this is exactly the same
12848
07:48:47,478 --> 07:48:52,000
as this in this case we're actually
12849
07:48:49,798 --> 07:48:54,160
setting the value and in this case we're
12850
07:48:52,000 --> 07:48:56,520
actually getting the value so let's
12851
07:48:54,160 --> 07:48:59,280
comment this line out and let's see what
12852
07:48:56,520 --> 07:49:01,318
happens your name is Harry so that's
12853
07:48:59,280 --> 07:49:02,600
worked okay so now let's do the same
12854
07:49:01,318 --> 07:49:05,318
thing for
12855
07:49:02,600 --> 07:49:09,600
age remove the semicolon add the curly
12856
07:49:05,318 --> 07:49:12,240
brace we can say get and then return age
12857
07:49:09,600 --> 07:49:15,160
and then simply do set and then do age
12858
07:49:12,240 --> 07:49:17,478
equals value there we go so now we got
12859
07:49:15,160 --> 07:49:19,440
that set up if you return back down here
12860
07:49:17,478 --> 07:49:21,558
and we see a person dot so as you can
12861
07:49:19,440 --> 07:49:24,120
see now the age is being removed as
12862
07:49:21,558 --> 07:49:26,000
being a field as we saw in the blue icon
12863
07:49:24,120 --> 07:49:27,840
it's now been turned into a spanner
12864
07:49:26,000 --> 07:49:30,000
which means it's a property as shown
12865
07:49:27,840 --> 07:49:32,000
here show only properties so now you can
12866
07:49:30,000 --> 07:49:34,520
see it returns back an integer and we've
12867
07:49:32,000 --> 07:49:36,760
got a get in a set so we can set that to
12868
07:49:34,520 --> 07:49:40,240
25 for example and then we can append
12869
07:49:36,760 --> 07:49:42,200
onto here and your AG is person. AG you
12870
07:49:40,240 --> 07:49:44,360
see that it's a capital A just as this
12871
07:49:42,200 --> 07:49:46,600
one is a capital N and now we have
12872
07:49:44,360 --> 07:49:48,600
getting set available and again we don't
12873
07:49:46,600 --> 07:49:50,878
need the brackets inside here since this
12874
07:49:48,600 --> 07:49:53,680
is a property not a function now let's
12875
07:49:50,878 --> 07:49:56,280
run it we should see Harry 25 there you
12876
07:49:53,680 --> 07:49:58,798
go your name is Harry and your age is
12877
07:49:56,280 --> 07:50:00,638
25 perfect
12878
07:49:58,798 --> 07:50:02,558
so as you can see now is instead of
12879
07:50:00,638 --> 07:50:04,558
having our set name and get name which
12880
07:50:02,558 --> 07:50:06,360
are these two functions we actually have
12881
07:50:04,558 --> 07:50:09,318
them contained within a
12882
07:50:06,360 --> 07:50:11,240
property we call this property and it
12883
07:50:09,318 --> 07:50:14,000
affects these variables but as you can
12884
07:50:11,240 --> 07:50:16,080
see just as these functions below were a
12885
07:50:14,000 --> 07:50:18,958
middleman we've now moved the middleman
12886
07:50:16,080 --> 07:50:21,240
into these properties so as these are
12887
07:50:18,958 --> 07:50:22,680
only one line you're probably thinking
12888
07:50:21,240 --> 07:50:24,680
there's probably a better way of doing
12889
07:50:22,680 --> 07:50:26,558
this and it's a lot quicker for syntax
12890
07:50:24,680 --> 07:50:28,638
and you're right there is so let's
12891
07:50:26,558 --> 07:50:30,360
explore that so I said before about
12892
07:50:28,638 --> 07:50:32,120
about Arrow functions when you're doing
12893
07:50:30,360 --> 07:50:35,440
something like this get name you can
12894
07:50:32,120 --> 07:50:37,760
have public string get name and then you
12895
07:50:35,440 --> 07:50:40,000
can simply do Arrow name this is a very
12896
07:50:37,760 --> 07:50:41,840
common string function get a name that
12897
07:50:40,000 --> 07:50:43,280
just returns back a value and you can
12898
07:50:41,840 --> 07:50:45,280
make that into an arrow function by
12899
07:50:43,280 --> 07:50:46,920
literally just putting back name so we
12900
07:50:45,280 --> 07:50:49,638
can actually do the same thing but for
12901
07:50:46,920 --> 07:50:52,360
our properties so these both contain one
12902
07:50:49,638 --> 07:50:54,318
line so we can actually minimize this
12903
07:50:52,360 --> 07:50:58,718
and if we just block comment this
12904
07:50:54,318 --> 07:51:00,440
out we can say public string name and
12905
07:50:58,718 --> 07:51:02,160
then we can have get and then the same
12906
07:51:00,440 --> 07:51:04,160
thing with the arrow function when we
12907
07:51:02,160 --> 07:51:06,478
doing Arrow function down here you
12908
07:51:04,160 --> 07:51:07,920
simply won't use the return keyword you
12909
07:51:06,478 --> 07:51:10,798
would just type in the variable you want
12910
07:51:07,920 --> 07:51:12,760
to return so we can say get arrow and
12911
07:51:10,798 --> 07:51:14,558
then simply lowcase name and then add a
12912
07:51:12,760 --> 07:51:16,760
semicolon just so you know that that
12913
07:51:14,558 --> 07:51:19,360
section is ended and then straight after
12914
07:51:16,760 --> 07:51:22,600
we can have set and then arrow and then
12915
07:51:19,360 --> 07:51:24,600
name equals value just like that so as
12916
07:51:22,600 --> 07:51:26,318
you can see these contents are exactly
12917
07:51:24,600 --> 07:51:28,680
the same we've actually returned them
12918
07:51:26,318 --> 07:51:30,520
back in a short space and of course here
12919
07:51:28,680 --> 07:51:32,280
as we got a compile error it just
12920
07:51:30,520 --> 07:51:34,440
expects a semicolon to be right at the
12921
07:51:32,280 --> 07:51:37,080
end here there you go and now we do the
12922
07:51:34,440 --> 07:51:42,240
same thing for our
12923
07:51:37,080 --> 07:51:46,840
age we can do public int ede get arrow
12924
07:51:42,240 --> 07:51:48,680
ede and set Arrow age equals value
12925
07:51:46,840 --> 07:51:50,920
perfect so now you can see that this has
12926
07:51:48,680 --> 07:51:52,958
been drastically reduced if I cut this
12927
07:51:50,920 --> 07:51:54,558
just underneath you'll see that almost
12928
07:51:52,958 --> 07:51:56,120
all of these lines are just replaced
12929
07:51:54,558 --> 07:51:57,798
into a two and you'll see that our
12930
07:51:56,120 --> 07:52:00,040
functionality will still work exactly
12931
07:51:57,798 --> 07:52:02,240
the same if we just run the code Harry
12932
07:52:00,040 --> 07:52:05,478
and 25 is being reassigned over our
12933
07:52:02,240 --> 07:52:06,638
default variables perfect of course now
12934
07:52:05,478 --> 07:52:09,000
if you want to add some more
12935
07:52:06,638 --> 07:52:11,280
functionality inside here for example in
12936
07:52:09,000 --> 07:52:12,840
our set name we had a little check that
12937
07:52:11,280 --> 07:52:16,478
makees sure that it's a valid name
12938
07:52:12,840 --> 07:52:19,798
before we assign it so we actually take
12939
07:52:16,478 --> 07:52:21,840
this and paste it over here then you see
12940
07:52:19,798 --> 07:52:23,600
the lines grown quite a lot so what's
12941
07:52:21,840 --> 07:52:25,920
pred to do is if you're going to have
12942
07:52:23,600 --> 07:52:27,878
any data inside here that does a little
12943
07:52:25,920 --> 07:52:30,040
bit of logic then it's best to keep the
12944
07:52:27,878 --> 07:52:32,360
curly bra is since you can have a bigger
12945
07:52:30,040 --> 07:52:34,160
output and it looks a bit more sensible
12946
07:52:32,360 --> 07:52:35,840
if you're just simply getting and you're
12947
07:52:34,160 --> 07:52:38,600
setting the variables then you can leave
12948
07:52:35,840 --> 07:52:42,240
this notation so let's undo this and
12949
07:52:38,600 --> 07:52:42,240
bring back the original ones in this
12950
07:52:43,318 --> 07:52:47,718
case there we go and then we can take
12951
07:52:45,920 --> 07:52:49,520
that line and in the set we can actually
12952
07:52:47,718 --> 07:52:51,360
replace it here so as you can see this
12953
07:52:49,520 --> 07:52:53,360
is a little bit grayed out so let's put
12954
07:52:51,360 --> 07:52:55,120
the semicon at the end and let's see why
12955
07:52:53,360 --> 07:52:57,600
this is grayed out name can be
12956
07:52:55,120 --> 07:52:59,920
simplified so what we're doing now is
12957
07:52:57,600 --> 07:53:02,160
name and all of these variables are
12958
07:52:59,920 --> 07:53:03,718
actually looking at the private variable
12959
07:53:02,160 --> 07:53:06,520
whereas if you actually look down here
12960
07:53:03,718 --> 07:53:08,920
and we uncomment this you'll see that
12961
07:53:06,520 --> 07:53:11,040
set name if we double click on this one
12962
07:53:08,920 --> 07:53:13,600
this is the only reference to it which
12963
07:53:11,040 --> 07:53:15,638
is our private variable up and the
12964
07:53:13,600 --> 07:53:18,240
reason why this doesn't need to be used
12965
07:53:15,638 --> 07:53:21,200
in this case is because our parameter is
12966
07:53:18,240 --> 07:53:21,200
now called
12967
07:53:21,718 --> 07:53:26,878
value so it's saying the name is equal
12968
07:53:24,360 --> 07:53:29,318
to checking if the value is null or
12969
07:53:26,878 --> 07:53:32,040
empty assigning the value into name if
12970
07:53:29,318 --> 07:53:34,760
not and then if it is empty then we hide
12971
07:53:32,040 --> 07:53:37,040
invalid straight into name in the case
12972
07:53:34,760 --> 07:53:39,200
of our set name our name was our
12973
07:53:37,040 --> 07:53:41,318
parameter the same goes here if we
12974
07:53:39,200 --> 07:53:43,680
change this to Value we can change these
12975
07:53:41,318 --> 07:53:45,680
two for Value as you can see now this
12976
07:53:43,680 --> 07:53:47,718
dot is actually grade out and it can be
12977
07:53:45,680 --> 07:53:50,000
simplified to just simply name and the
12978
07:53:47,718 --> 07:53:52,000
same principle applies above so let's
12979
07:53:50,000 --> 07:53:54,280
comment these back out and scroll back
12980
07:53:52,000 --> 07:53:57,000
to the top so now that we've assigned
12981
07:53:54,280 --> 07:53:58,798
name there we go we've got our check
12982
07:53:57,000 --> 07:54:00,760
inside there and you can also leave this
12983
07:53:58,798 --> 07:54:02,680
on one line if you want you can even
12984
07:54:00,760 --> 07:54:04,520
drop the curly brace in just so it looks
12985
07:54:02,680 --> 07:54:07,920
like this instead of having it all on
12986
07:54:04,520 --> 07:54:09,600
one line so you could do this and this
12987
07:54:07,920 --> 07:54:11,160
would be applicable as well but the
12988
07:54:09,600 --> 07:54:13,440
second you start to put it all on one
12989
07:54:11,160 --> 07:54:15,558
line then it gets a little bit messy
12990
07:54:13,440 --> 07:54:17,558
quickly so if you leave the outer
12991
07:54:15,558 --> 07:54:19,240
bracket just like that then it can be
12992
07:54:17,558 --> 07:54:21,558
pretty short since you don't need more
12993
07:54:19,240 --> 07:54:23,440
curly braces so now that we've done that
12994
07:54:21,558 --> 07:54:26,000
for the name let's do it for the age as
12995
07:54:23,440 --> 07:54:28,360
well so we had this. age we check the
12996
07:54:26,000 --> 07:54:32,280
range and we assign the relevant values
12997
07:54:28,360 --> 07:54:34,798
so let's take that paste it over this we
12998
07:54:32,280 --> 07:54:36,040
don't need this dot anymore and age in
12999
07:54:34,798 --> 07:54:37,798
this case is actually going to be the
13000
07:54:36,040 --> 07:54:41,558
value gets passed
13001
07:54:37,798 --> 07:54:43,718
in to replace all the ages with value
13002
07:54:41,558 --> 07:54:46,478
there we go so if the value passes the
13003
07:54:43,718 --> 07:54:49,280
range check we put the value inside age
13004
07:54:46,478 --> 07:54:51,080
or not we can put minus one inside age
13005
07:54:49,280 --> 07:54:54,000
and again we can have the same thing we
13006
07:54:51,080 --> 07:54:57,280
can use our arrows inside here there we
13007
07:54:54,000 --> 07:54:59,440
go so now we've got our Cod drastically
13008
07:54:57,280 --> 07:55:01,318
reduced so we went from these big
13009
07:54:59,440 --> 07:55:04,000
functions that have to be defined with
13010
07:55:01,318 --> 07:55:05,920
specific names over to our properties
13011
07:55:04,000 --> 07:55:08,080
and they went from really small to just
13012
07:55:05,920 --> 07:55:10,120
a little bit bigger but now they have
13013
07:55:08,080 --> 07:55:12,000
advanced connections just to make sure
13014
07:55:10,120 --> 07:55:13,840
that the variables are okay before you
13015
07:55:12,000 --> 07:55:15,958
assign it in so now that we've got our
13016
07:55:13,840 --> 07:55:18,120
properties let's have a look so inside
13017
07:55:15,958 --> 07:55:20,520
our Constructor we don't also need to
13018
07:55:18,120 --> 07:55:23,040
use our local variables which is our
13019
07:55:20,520 --> 07:55:25,040
private ones directly we should actually
13020
07:55:23,040 --> 07:55:27,160
get rid of this now and we should always
13021
07:55:25,040 --> 07:55:29,240
do everything through our properties
13022
07:55:27,160 --> 07:55:31,360
name equals name and because this is a
13023
07:55:29,240 --> 07:55:33,760
capital and this is a lower case we
13024
07:55:31,360 --> 07:55:35,638
don't need this dot in
13025
07:55:33,760 --> 07:55:38,000
there there we
13026
07:55:35,638 --> 07:55:40,280
go so what you should do in the return
13027
07:55:38,000 --> 07:55:42,718
details function is also use the capital
13028
07:55:40,280 --> 07:55:44,440
n and the capital age so when you hover
13029
07:55:42,718 --> 07:55:47,120
over these you're actually referring to
13030
07:55:44,440 --> 07:55:48,958
the property because like I said before
13031
07:55:47,120 --> 07:55:51,200
these are the middleman to communicate
13032
07:55:48,958 --> 07:55:53,000
with the private variables you shouldn't
13033
07:55:51,200 --> 07:55:54,920
need to access the private variables
13034
07:55:53,000 --> 07:55:56,920
directly you should use the middle man
13035
07:55:54,920 --> 07:55:58,638
as much as possible so now that we've
13036
07:55:56,920 --> 07:56:00,718
done that I have one last thing to
13037
07:55:58,638 --> 07:56:02,080
introduce so if we actually abandon all
13038
07:56:00,718 --> 07:56:03,878
of this code just to keep it for
13039
07:56:02,080 --> 07:56:06,558
reference we have something called an
13040
07:56:03,878 --> 07:56:08,520
auto property and you briefly saw it
13041
07:56:06,558 --> 07:56:11,120
when we saw it down here so if we
13042
07:56:08,520 --> 07:56:13,638
actually just bring them back for a
13043
07:56:11,120 --> 07:56:16,760
second and if you hover over a name down
13044
07:56:13,638 --> 07:56:18,638
here it says string name get and set in
13045
07:56:16,760 --> 07:56:21,160
the curly brace but you see how the get
13046
07:56:18,638 --> 07:56:23,000
and set is empty it just has two values
13047
07:56:21,160 --> 07:56:25,478
inside there and it just ends a curly
13048
07:56:23,000 --> 07:56:28,000
brace with no actual logic you see in
13049
07:56:25,478 --> 07:56:30,360
our get we're saying age and our set
13050
07:56:28,000 --> 07:56:32,280
with saying age equals this so when
13051
07:56:30,360 --> 07:56:36,558
we're doing Auto properties what you can
13052
07:56:32,280 --> 07:56:36,558
do is you can actually make these public
13053
07:56:37,160 --> 07:56:42,080
again change these to a capital N
13054
07:56:39,718 --> 07:56:47,318
changes to a capital A and just straight
13055
07:56:42,080 --> 07:56:49,798
after it you can say Get Set just like
13056
07:56:47,318 --> 07:56:51,638
that and what that will do is although
13057
07:56:49,798 --> 07:56:53,680
they're actually made public what it's
13058
07:56:51,638 --> 07:56:56,398
doing behind the scenes is actually
13059
07:56:53,680 --> 07:56:59,120
doing this for you it actually has a
13060
07:56:56,398 --> 07:57:01,680
private and a public for you behind the
13061
07:56:59,120 --> 07:57:04,080
scenes this is just a Shand these are
13062
07:57:01,680 --> 07:57:05,840
what's called a auto property what it
13063
07:57:04,080 --> 07:57:07,878
does is it makes an anonymous property
13064
07:57:05,840 --> 07:57:10,200
behind the scenes as I'll show you in
13065
07:57:07,878 --> 07:57:12,840
Microsoft documentation right now as it
13066
07:57:10,200 --> 07:57:14,840
says here in c three or later Auto
13067
07:57:12,840 --> 07:57:16,760
implemented properties make property
13068
07:57:14,840 --> 07:57:18,878
declaration more concise when no
13069
07:57:16,760 --> 07:57:22,398
additional logic is required and what
13070
07:57:18,878 --> 07:57:24,798
that means is no additional logic all of
13071
07:57:22,398 --> 07:57:26,878
these are additional logic if you're
13072
07:57:24,798 --> 07:57:29,080
just simply assigning a value and
13073
07:57:26,878 --> 07:57:32,080
reading it back just like here then this
13074
07:57:29,080 --> 07:57:33,680
is no need to write get name and set
13075
07:57:32,080 --> 07:57:35,558
name if all you're doing is getting and
13076
07:57:33,680 --> 07:57:36,840
setting it if you're getting and setting
13077
07:57:35,558 --> 07:57:38,958
it but there is a certain amount of
13078
07:57:36,840 --> 07:57:40,718
logic as we have here we essentially
13079
07:57:38,958 --> 07:57:42,958
have a conditional operator that's
13080
07:57:40,718 --> 07:57:44,920
making a subconscious decision about
13081
07:57:42,958 --> 07:57:46,478
what to assign the name to but if you
13082
07:57:44,920 --> 07:57:49,760
just simply assign in something like
13083
07:57:46,478 --> 07:57:51,440
this then an auto property is perfect
13084
07:57:49,760 --> 07:57:53,240
they also enable client code to create
13085
07:57:51,440 --> 07:57:54,840
objects when you declare a property as
13086
07:57:53,240 --> 07:57:57,040
shown in the following example the
13087
07:57:54,840 --> 07:57:58,478
compiler creates a private Anonymous
13088
07:57:57,040 --> 07:58:01,280
backing field that can only only be
13089
07:57:58,478 --> 07:58:03,798
accessed to the properties get and
13090
07:58:01,280 --> 07:58:06,760
sets as you can see here just like in
13091
07:58:03,798 --> 07:58:09,200
our example we have public int something
13092
07:58:06,760 --> 07:58:10,958
and then get in set and then they have a
13093
07:58:09,200 --> 07:58:13,200
public in something with the get in set
13094
07:58:10,958 --> 07:58:14,398
as well and they can use it just here to
13095
07:58:13,200 --> 07:58:16,638
access it
13096
07:58:14,398 --> 07:58:18,520
directly and because they're simply just
13097
07:58:16,638 --> 07:58:20,280
assigning and getting the values and
13098
07:58:18,520 --> 07:58:22,080
there isn't any additional logic then
13099
07:58:20,280 --> 07:58:24,958
this is the best way to do it and you
13100
07:58:22,080 --> 07:58:27,558
get as it says here an anonymous private
13101
07:58:24,958 --> 07:58:31,680
backing field so this would be the same
13102
07:58:27,558 --> 07:58:34,760
thing as doing this private string name
13103
07:58:31,680 --> 07:58:36,760
and private int age so once you do that
13104
07:58:34,760 --> 07:58:40,558
and you make this into the get and set
13105
07:58:36,760 --> 07:58:40,558
properties which are these two right
13106
07:58:41,080 --> 07:58:47,080
here this and this line together make a
13107
07:58:44,958 --> 07:58:49,040
property what the auto implemented
13108
07:58:47,080 --> 07:58:51,478
property does is actually combine this
13109
07:58:49,040 --> 07:58:53,160
line and this line together and make the
13110
07:58:51,478 --> 07:58:55,160
private one behind the scenes so the
13111
07:58:53,160 --> 07:58:56,840
programmer doesn't see it but it will be
13112
07:58:55,160 --> 07:58:59,318
applicable when the program runs in the
13113
07:58:56,840 --> 07:59:01,558
compiler and then you only get publicly
13114
07:58:59,318 --> 07:59:04,440
exposed with this parameter which will
13115
07:59:01,558 --> 07:59:06,798
be our name property so it will actually
13116
07:59:04,440 --> 07:59:08,878
make the private for you if you just
13117
07:59:06,798 --> 07:59:11,638
keep it like we had it a second
13118
07:59:08,878 --> 07:59:13,240
ago just like this so if you leave it
13119
07:59:11,638 --> 07:59:15,280
like this it will actually create the
13120
07:59:13,240 --> 07:59:17,958
private variable behind the scenes as it
13121
07:59:15,280 --> 07:59:20,160
says here it's Anonymous backing field
13122
07:59:17,958 --> 07:59:22,600
and it can only be accessed through name
13123
07:59:20,160 --> 07:59:25,160
and Edge and as you can see none of our
13124
07:59:22,600 --> 07:59:26,760
code has actually changed all of these
13125
07:59:25,160 --> 07:59:29,280
still function okay and if we run the
13126
07:59:26,760 --> 07:59:31,920
code we should still get Harry and
13127
07:59:29,280 --> 07:59:33,920
25 so just to sum this video up since
13128
07:59:31,920 --> 07:59:36,040
there was a lot covered the bottom line
13129
07:59:33,920 --> 07:59:38,318
is if you're going to have any sort of
13130
07:59:36,040 --> 07:59:40,558
values that require any logic then you
13131
07:59:38,318 --> 07:59:42,440
should use normal properties if you're
13132
07:59:40,558 --> 07:59:44,360
simply just going to assign and get the
13133
07:59:42,440 --> 07:59:46,520
value and it doesn't matter and you have
13134
07:59:44,360 --> 07:59:48,478
no control over it then you can use the
13135
07:59:46,520 --> 07:59:50,520
auto implemented properties since that
13136
07:59:48,478 --> 07:59:52,478
will do the same thing with less code
13137
07:59:50,520 --> 07:59:54,318
using the gets and sets is the old way
13138
07:59:52,478 --> 07:59:56,360
of doing this and you should always use
13139
07:59:54,318 --> 07:59:58,520
the properties going forward gets and
13140
07:59:56,360 --> 08:00:00,040
Setters are very helpful to know just so
13141
07:59:58,520 --> 08:00:02,000
you understand what they are and why
13142
08:00:00,040 --> 08:00:04,080
they exist but you should note that
13143
08:00:02,000 --> 08:00:05,760
properties are the best way forward
13144
08:00:04,080 --> 08:00:08,318
properties give you access to doing the
13145
08:00:05,760 --> 08:00:10,440
same logic as it says inside here if you
13146
08:00:08,318 --> 08:00:12,080
do an auto implemented properties then
13147
08:00:10,440 --> 08:00:14,398
you're not allowed to use the logic
13148
08:00:12,080 --> 08:00:16,120
inside the set or the get so if you
13149
08:00:14,398 --> 08:00:18,318
don't want to do any logic then don't
13150
08:00:16,120 --> 08:00:20,760
use the auto implemented properties only
13151
08:00:18,318 --> 08:00:22,958
use the normal properties to make a get
13152
08:00:20,760 --> 08:00:25,120
and Setter you just do set name get the
13153
08:00:22,958 --> 08:00:27,440
parameter and assign the local variable
13154
08:00:25,120 --> 08:00:29,440
and the get just returns it getting back
13155
08:00:27,440 --> 08:00:31,240
a property you want to capitalize your
13156
08:00:29,440 --> 08:00:33,520
private variable if you had private
13157
08:00:31,240 --> 08:00:36,200
string name with a lowercase n you want
13158
08:00:33,520 --> 08:00:37,958
public string name with a capital N you
13159
08:00:36,200 --> 08:00:39,718
get the name and then you can set the
13160
08:00:37,958 --> 08:00:41,840
name with some sort of conditional
13161
08:00:39,718 --> 08:00:43,558
operator or any if statement or anything
13162
08:00:41,840 --> 08:00:45,478
inside here and the same thing goes with
13163
08:00:43,558 --> 08:00:47,718
the age you can have a public int age
13164
08:00:45,478 --> 08:00:50,040
with a capital A and A Private in age
13165
08:00:47,718 --> 08:00:51,558
with a lowas A you can get the age and
13166
08:00:50,040 --> 08:00:54,318
then set the age with a conditional
13167
08:00:51,558 --> 08:00:56,040
operator again or any other code if you
13168
08:00:54,318 --> 08:00:57,958
don't require any logic like these two
13169
08:00:56,040 --> 08:00:59,798
lines indicate then you can simply just
13170
08:00:57,958 --> 08:01:01,600
just use the auto properties as
13171
08:00:59,798 --> 08:01:03,040
demonstrated above here and they can be
13172
08:01:01,600 --> 08:01:05,878
used quite easily throughout the
13173
08:01:03,040 --> 08:01:07,440
Constructor and our functions and even
13174
08:01:05,878 --> 08:01:09,520
outside the function when the user is
13175
08:01:07,440 --> 08:01:11,798
trying to call
13176
08:01:09,520 --> 08:01:13,520
them but one thing that we haven't been
13177
08:01:11,798 --> 08:01:15,478
able to solve and you may not have
13178
08:01:13,520 --> 08:01:17,478
noticed this as a nuisance the problem
13179
08:01:15,478 --> 08:01:19,120
is when you try to return details about
13180
08:01:17,478 --> 08:01:21,280
something we're having to call this
13181
08:01:19,120 --> 08:01:24,000
function every single time if we have an
13182
08:01:21,280 --> 08:01:25,840
in age for example made that equal to 23
13183
08:01:24,000 --> 08:01:27,718
and we did console right line and simply
13184
08:01:25,840 --> 08:01:29,798
placed age inside the brackets we don't
13185
08:01:27,718 --> 08:01:31,958
have to do anything like this because it
13186
08:01:29,798 --> 08:01:34,398
simply just prints the value out if we
13187
08:01:31,958 --> 08:01:36,958
run the code you'll see that it simply
13188
08:01:34,398 --> 08:01:39,080
just says 23 this should be pretty
13189
08:01:36,958 --> 08:01:41,440
simple for our person as well but it's
13190
08:01:39,080 --> 08:01:43,360
not we need to call return details every
13191
08:01:41,440 --> 08:01:45,440
single time we want to print the
13192
08:01:43,360 --> 08:01:47,318
properties and if we don't do this the
13193
08:01:45,440 --> 08:01:49,280
only way to do it is by making some
13194
08:01:47,318 --> 08:01:51,680
string interpolation and we can say
13195
08:01:49,280 --> 08:01:54,600
person.
13196
08:01:51,680 --> 08:01:56,440
name and then also include person. AG
13197
08:01:54,600 --> 08:01:59,398
Etc and then that can print out to the
13198
08:01:56,440 --> 08:02:00,558
screen just like that
13199
08:01:59,398 --> 08:02:03,200
of course it doesn't have the same
13200
08:02:00,558 --> 08:02:05,120
format in but we can also just take this
13201
08:02:03,200 --> 08:02:08,638
place it straight inside here add our
13202
08:02:05,120 --> 08:02:10,638
person dot before them person Dot and
13203
08:02:08,638 --> 08:02:13,120
there you go then you get exactly the
13204
08:02:10,638 --> 08:02:14,360
same output as Callum return details but
13205
08:02:13,120 --> 08:02:15,958
of course you don't want to do this
13206
08:02:14,360 --> 08:02:17,680
every time and you certainly don't want
13207
08:02:15,958 --> 08:02:19,718
to have to type in return details
13208
08:02:17,680 --> 08:02:21,878
function every single time as well you
13209
08:02:19,718 --> 08:02:23,040
want to keep it as just person now of
13210
08:02:21,878 --> 08:02:24,840
course this is not going to work
13211
08:02:23,040 --> 08:02:26,798
Straight Out of the Box let's try run
13212
08:02:24,840 --> 08:02:29,360
that and see what happens so we get
13213
08:02:26,798 --> 08:02:31,200
string override person and what's going
13214
08:02:29,360 --> 08:02:32,920
on here is it's saying names space is
13215
08:02:31,200 --> 08:02:34,680
string override and then it's a class
13216
08:02:32,920 --> 08:02:36,718
person so it's just printing out this
13217
08:02:34,680 --> 08:02:38,318
person object but it's not really doing
13218
08:02:36,718 --> 08:02:40,760
anything because we haven't really told
13219
08:02:38,318 --> 08:02:42,920
it to do something so what we can do
13220
08:02:40,760 --> 08:02:44,958
here is we can actually override the two
13221
08:02:42,920 --> 08:02:47,160
string method so let me explain so let
13222
08:02:44,958 --> 08:02:48,360
me set up a couple of examples just to
13223
08:02:47,160 --> 08:02:50,600
show you what's going to happen at the
13224
08:02:48,360 --> 08:02:52,878
end so right now we call return details
13225
08:02:50,600 --> 08:02:55,680
to return these details what we can also
13226
08:02:52,878 --> 08:02:58,160
do is say dot two string and then we can
13227
08:02:55,680 --> 08:03:00,520
also do these three options return the
13228
08:02:58,160 --> 08:03:02,440
details convert it to a string or just
13229
08:03:00,520 --> 08:03:04,080
print the person straight to the console
13230
08:03:02,440 --> 08:03:05,920
at the end of this little section we're
13231
08:03:04,080 --> 08:03:07,920
going to make all three of these print
13232
08:03:05,920 --> 08:03:11,558
out the same values so let's have a look
13233
08:03:07,920 --> 08:03:13,080
now two string and printing out normally
13234
08:03:11,558 --> 08:03:15,318
these two lines right here are actually
13235
08:03:13,080 --> 08:03:17,240
doing exactly the same thing behind the
13236
08:03:15,318 --> 08:03:18,920
scenes when you place anything inside
13237
08:03:17,240 --> 08:03:20,600
these brackets inside a console right
13238
08:03:18,920 --> 08:03:22,398
line it will try and convert it to a
13239
08:03:20,600 --> 08:03:24,280
string anywhere so whether you put two
13240
08:03:22,398 --> 08:03:26,080
string or you leave it as person the
13241
08:03:24,280 --> 08:03:28,478
console right line function will still
13242
08:03:26,080 --> 08:03:30,398
accept it so just coming out the age for
13243
08:03:28,478 --> 08:03:32,240
the time being in order to make the two
13244
08:03:30,398 --> 08:03:34,360
string function work as well as just
13245
08:03:32,240 --> 08:03:36,398
placing person inside the brackets we
13246
08:03:34,360 --> 08:03:38,398
need to make a special function which is
13247
08:03:36,398 --> 08:03:40,160
an override function so let's have a
13248
08:03:38,398 --> 08:03:42,040
look so just underneath our return
13249
08:03:40,160 --> 08:03:45,080
details still within the scope of the
13250
08:03:42,040 --> 08:03:47,360
class we say public and then override
13251
08:03:45,080 --> 08:03:49,280
after we press tab we can follow it by a
13252
08:03:47,360 --> 08:03:51,240
space and we can see the functions that
13253
08:03:49,280 --> 08:03:52,760
we can override so in this video we're
13254
08:03:51,240 --> 08:03:55,360
actually going to cover two string and
13255
08:03:52,760 --> 08:03:57,318
do equals get hash code will actually
13256
08:03:55,360 --> 08:03:59,360
serve as a default hash function if I
13257
08:03:57,318 --> 08:04:01,240
show you the the documentation it says
13258
08:03:59,360 --> 08:04:03,600
here a hash code is numeric value that
13259
08:04:01,240 --> 08:04:05,680
is used to insert and identify an object
13260
08:04:03,600 --> 08:04:07,080
in a hash based collection this is
13261
08:04:05,680 --> 08:04:09,318
normally use when you're doing hash
13262
08:04:07,080 --> 08:04:10,600
functions as it says here and for the
13263
08:04:09,318 --> 08:04:13,080
time being we're not actually going to
13264
08:04:10,600 --> 08:04:15,840
be using this right now so the two we
13265
08:04:13,080 --> 08:04:17,840
can do a two string and equals but as
13266
08:04:15,840 --> 08:04:19,520
you can see our two string is available
13267
08:04:17,840 --> 08:04:21,680
right now we'll cover equals as an
13268
08:04:19,520 --> 08:04:23,318
extension in a minute so let's double
13269
08:04:21,680 --> 08:04:25,318
click on our two string and we get this
13270
08:04:23,318 --> 08:04:26,680
little template placed in if you don't
13271
08:04:25,318 --> 08:04:28,958
want the template then it's just an
13272
08:04:26,680 --> 08:04:31,240
override if returns back a string since
13273
08:04:28,958 --> 08:04:33,920
it's converting to string it needs to be
13274
08:04:31,240 --> 08:04:36,160
this exact notation if you drop the T
13275
08:04:33,920 --> 08:04:37,798
then it's got nothing to override now
13276
08:04:36,160 --> 08:04:40,558
you go be a compile error because
13277
08:04:37,798 --> 08:04:42,240
there's nothing to override itself so
13278
08:04:40,558 --> 08:04:43,920
play two string in the capitals there's
13279
08:04:42,240 --> 08:04:46,080
no parameters passed in and then we're
13280
08:04:43,920 --> 08:04:47,240
going to be returning something so the
13281
08:04:46,080 --> 08:04:48,600
thing that we're going to be returning
13282
08:04:47,240 --> 08:04:50,558
is exactly the same as our return
13283
08:04:48,600 --> 08:04:52,840
details function we actually have two
13284
08:04:50,558 --> 08:04:55,440
options here we could say return and
13285
08:04:52,840 --> 08:04:57,718
then return details so what that will do
13286
08:04:55,440 --> 08:04:59,718
is it will go to this function it will
13287
08:04:57,718 --> 08:05:01,600
call this function grab that and
13288
08:04:59,718 --> 08:05:03,120
essentially place inside here and then
13289
08:05:01,600 --> 08:05:04,840
return it back to the console in the
13290
08:05:03,120 --> 08:05:06,840
ideal world we don't actually need this
13291
08:05:04,840 --> 08:05:08,160
return details function so we could
13292
08:05:06,840 --> 08:05:09,680
comment it out but I'm going to leave it
13293
08:05:08,160 --> 08:05:12,360
in just so the demonstration doesn't
13294
08:05:09,680 --> 08:05:14,398
break down here so we can take this and
13295
08:05:12,360 --> 08:05:16,000
place it straight inside here now we
13296
08:05:14,398 --> 08:05:18,520
have two functions that essentially do
13297
08:05:16,000 --> 08:05:19,958
the same thing but the advantage is this
13298
08:05:18,520 --> 08:05:22,958
function is something that needs to be
13299
08:05:19,958 --> 08:05:24,798
called explicitly do return details
13300
08:05:22,958 --> 08:05:26,638
whereas this function the two string
13301
08:05:24,798 --> 08:05:28,638
override actually just gets called
13302
08:05:26,638 --> 08:05:31,160
naturally without having to add any
13303
08:05:28,638 --> 08:05:32,760
extensions so if we run the code now
13304
08:05:31,160 --> 08:05:34,840
you'll see that now they all equal
13305
08:05:32,760 --> 08:05:37,398
exactly the same value we get three
13306
08:05:34,840 --> 08:05:39,798
sections that have this maybe we can
13307
08:05:37,398 --> 08:05:41,280
place back SLT instead just so we can
13308
08:05:39,798 --> 08:05:44,200
have them all in the same
13309
08:05:41,280 --> 08:05:47,000
line there we go so you can see all
13310
08:05:44,200 --> 08:05:49,318
three are exactly the same so what we
13311
08:05:47,000 --> 08:05:51,360
can do now is if we comment this option
13312
08:05:49,318 --> 08:05:52,958
and we comment out this function we
13313
08:05:51,360 --> 08:05:54,398
don't have to include two string if we
13314
08:05:52,958 --> 08:05:56,120
don't want to we can also comment this
13315
08:05:54,398 --> 08:05:58,520
one out and we can keep it just as
13316
08:05:56,120 --> 08:06:00,920
person so as we do when we declare the
13317
08:05:58,520 --> 08:06:03,040
integer we say integ equal 23 and then
13318
08:06:00,920 --> 08:06:05,200
we simply print out and this is exactly
13319
08:06:03,040 --> 08:06:07,798
the notation that we want it should
13320
08:06:05,200 --> 08:06:10,000
really be this simple there you go one
13321
08:06:07,798 --> 08:06:12,080
line creates the person and one line
13322
08:06:10,000 --> 08:06:13,920
with just a variable name goes inside
13323
08:06:12,080 --> 08:06:16,920
the brackets to print them out the same
13324
08:06:13,920 --> 08:06:18,600
thing here person person Edge and Edge
13325
08:06:16,920 --> 08:06:20,520
and let's run that code now and there
13326
08:06:18,600 --> 08:06:22,520
you go you get the person printed and
13327
08:06:20,520 --> 08:06:24,160
the edge printed exactly and
13328
08:06:22,520 --> 08:06:25,760
straightforward as you may think it
13329
08:06:24,160 --> 08:06:27,600
should be so now that we've done that
13330
08:06:25,760 --> 08:06:29,600
let's explore what the equals function
13331
08:06:27,600 --> 08:06:32,000
needs to be done let's make another
13332
08:06:29,600 --> 08:06:34,840
person we can say person test equals new
13333
08:06:32,000 --> 08:06:36,920
person and we can have hurry age
13334
08:06:34,840 --> 08:06:38,160
23 then we don't need to print them out
13335
08:06:36,920 --> 08:06:40,040
in this example we just need to
13336
08:06:38,160 --> 08:06:41,558
demonstrate this equals function so if
13337
08:06:40,040 --> 08:06:43,760
you remember with strings we can have a
13338
08:06:41,558 --> 08:06:46,000
string called abber for example and we
13339
08:06:43,760 --> 08:06:48,398
can say do equals straight after it we
13340
08:06:46,000 --> 08:06:50,520
can say if ABA is equal to hurry then we
13341
08:06:48,398 --> 08:06:52,000
can do something inside here what I'm
13342
08:06:50,520 --> 08:06:54,240
trying to demonstrate is once you've got
13343
08:06:52,000 --> 08:06:56,520
an object you can place do equals and
13344
08:06:54,240 --> 08:06:58,558
then you can compare it like that so you
13345
08:06:56,520 --> 08:07:01,280
can also do that without classes so we
13346
08:06:58,558 --> 08:07:04,000
can say person do equals and then equals
13347
08:07:01,280 --> 08:07:06,638
to test and in here we could just say
13348
08:07:04,000 --> 08:07:08,958
same let's try and make these as same as
13349
08:07:06,638 --> 08:07:11,520
possible and see what happens so let's
13350
08:07:08,958 --> 08:07:13,840
keep the name exactly the same just like
13351
08:07:11,520 --> 08:07:15,520
that and let's copy the 25 down just
13352
08:07:13,840 --> 08:07:17,840
like that so now these two are
13353
08:07:15,520 --> 08:07:19,478
instantiated exactly the same way let's
13354
08:07:17,840 --> 08:07:21,478
see what happens when we've run the
13355
08:07:19,478 --> 08:07:23,638
equals then we don't get anything
13356
08:07:21,478 --> 08:07:25,280
printed which means they're not the same
13357
08:07:23,638 --> 08:07:26,798
so because this doesn't automatically
13358
08:07:25,280 --> 08:07:28,798
tell us that these two are exactly the
13359
08:07:26,798 --> 08:07:30,840
same we can override that and provide
13360
08:07:28,798 --> 08:07:33,200
our own functionality just like we did
13361
08:07:30,840 --> 08:07:35,478
with the operate overload we override
13362
08:07:33,200 --> 08:07:37,318
equals we get passed in an object and we
13363
08:07:35,478 --> 08:07:39,760
need to check something inside it so
13364
08:07:37,318 --> 08:07:41,478
let's see what we can do inside here so
13365
08:07:39,760 --> 08:07:43,958
in order to check if something is equal
13366
08:07:41,478 --> 08:07:46,680
to another thing what can we do so we
13367
08:07:43,958 --> 08:07:48,878
have a name and age if this one is the
13368
08:07:46,680 --> 08:07:50,958
same as this and this is the same as
13369
08:07:48,878 --> 08:07:52,840
this then two people are exactly the
13370
08:07:50,958 --> 08:07:55,398
same so let's try that out and see what
13371
08:07:52,840 --> 08:07:56,760
happens so we can say name which is the
13372
08:07:55,398 --> 08:07:58,280
property inside the class that we're
13373
08:07:56,760 --> 08:07:59,958
currently inside
13374
08:07:58,280 --> 08:08:02,318
and then we can use equals because this
13375
08:07:59,958 --> 08:08:04,958
is just a string parameter and then we
13376
08:08:02,318 --> 08:08:06,840
can say that equals to
13377
08:08:04,958 --> 08:08:09,440
objectname but you can see we've got a
13378
08:08:06,840 --> 08:08:12,680
little problem here this object is very
13379
08:08:09,440 --> 08:08:15,600
generic it's just an object every class
13380
08:08:12,680 --> 08:08:17,280
that you create is derived by an object
13381
08:08:15,600 --> 08:08:19,360
what that means is every class or
13382
08:08:17,280 --> 08:08:21,558
integer or double at the end of the day
13383
08:08:19,360 --> 08:08:23,200
is also just known as an object through
13384
08:08:21,558 --> 08:08:25,120
inheritance which we'll discuss further
13385
08:08:23,200 --> 08:08:27,440
on this course but just know that
13386
08:08:25,120 --> 08:08:29,318
everything inside C is always an object
13387
08:08:27,440 --> 08:08:31,478
object somewhere down the line so what
13388
08:08:29,318 --> 08:08:33,878
that means is we have to convert it from
13389
08:08:31,478 --> 08:08:36,440
an object to our custom class which is
13390
08:08:33,878 --> 08:08:38,680
person in this case so we can use this
13391
08:08:36,440 --> 08:08:40,798
thing called casting so we can say
13392
08:08:38,680 --> 08:08:42,478
person person which is just to find a
13393
08:08:40,798 --> 08:08:46,000
new variable and we can make that
13394
08:08:42,478 --> 08:08:48,600
immediately equal to object as
13395
08:08:46,000 --> 08:08:51,120
person so what we've done here is we've
13396
08:08:48,600 --> 08:08:53,160
said our object which is this parameter
13397
08:08:51,120 --> 08:08:55,398
is currently an object right now we want
13398
08:08:53,160 --> 08:08:57,160
to cast it as a person which is kind of
13399
08:08:55,398 --> 08:08:59,398
like converting into a person and then
13400
08:08:57,160 --> 08:09:01,040
play placing the values inside here so
13401
08:08:59,398 --> 08:09:02,840
now if we get rid of this comment and
13402
08:09:01,040 --> 08:09:05,240
instead of using object we actually have
13403
08:09:02,840 --> 08:09:07,638
a new local variable called person so
13404
08:09:05,240 --> 08:09:09,440
then here we can say person. name there
13405
08:09:07,638 --> 08:09:11,280
we go now we have access to the person
13406
08:09:09,440 --> 08:09:13,520
property since we've converted this
13407
08:09:11,280 --> 08:09:15,638
object into a person and of course we
13408
08:09:13,520 --> 08:09:19,200
know that we have name and age
13409
08:09:15,638 --> 08:09:22,878
available so if the name is equal to the
13410
08:09:19,200 --> 08:09:24,360
personname and the age is equal to the
13411
08:09:22,878 --> 08:09:26,958
person.
13412
08:09:24,360 --> 08:09:28,680
AG then we must have a match and of
13413
08:09:26,958 --> 08:09:30,638
course want to keep using the properties
13414
08:09:28,680 --> 08:09:32,718
we're going to use a capital A and there
13415
08:09:30,638 --> 08:09:34,440
we go so we run the conversion on one
13416
08:09:32,718 --> 08:09:36,920
line and then we check if they're equal
13417
08:09:34,440 --> 08:09:38,680
to each other now in this specific case
13418
08:09:36,920 --> 08:09:40,840
you can actually pass anything inside
13419
08:09:38,680 --> 08:09:42,520
here so let's try it we can pass Five
13420
08:09:40,840 --> 08:09:45,440
inside here and as you can see there's
13421
08:09:42,520 --> 08:09:47,360
no compile error cuz like I said five is
13422
08:09:45,440 --> 08:09:49,000
just an integer which is an object at
13423
08:09:47,360 --> 08:09:50,718
the end of the day if we make it a
13424
08:09:49,000 --> 08:09:52,798
double that works as well there is
13425
08:09:50,718 --> 08:09:55,040
literally no compile errors and even if
13426
08:09:52,798 --> 08:09:56,638
you put M to make it into a decimal
13427
08:09:55,040 --> 08:09:58,520
again there's no compile errors because
13428
08:09:56,638 --> 08:10:00,160
they're all object at the end of the day
13429
08:09:58,520 --> 08:10:01,718
so let's pass test in there what you
13430
08:10:00,160 --> 08:10:03,240
will have to know is if you do
13431
08:10:01,718 --> 08:10:05,000
accidentally pass in a double for
13432
08:10:03,240 --> 08:10:07,160
example like this you're going to hit an
13433
08:10:05,000 --> 08:10:08,840
error here which will be a runtime error
13434
08:10:07,160 --> 08:10:10,558
it will try and convert it as soon as it
13435
08:10:08,840 --> 08:10:13,080
hits this line and obviously you can't
13436
08:10:10,558 --> 08:10:15,160
convert a double into our person so what
13437
08:10:13,080 --> 08:10:19,478
you could do just to wrap it around you
13438
08:10:15,160 --> 08:10:21,520
could say if the object is a person then
13439
08:10:19,478 --> 08:10:23,478
we can proceed with a conversion just
13440
08:10:21,520 --> 08:10:25,798
like this and then what you can do for
13441
08:10:23,478 --> 08:10:28,000
safety is just outside of this you can
13442
08:10:25,798 --> 08:10:30,120
actually just return back false just so
13443
08:10:28,000 --> 08:10:33,440
the user knows that this isn't equal to
13444
08:10:30,120 --> 08:10:35,878
each other so if the object is a person
13445
08:10:33,440 --> 08:10:38,798
then we convert it as a person is is
13446
08:10:35,878 --> 08:10:41,840
used for checking and as is used for
13447
08:10:38,798 --> 08:10:44,200
converting is this object a person and
13448
08:10:41,840 --> 08:10:45,558
if it is then we try and convert it and
13449
08:10:44,200 --> 08:10:47,318
then return back if they're equal to
13450
08:10:45,558 --> 08:10:49,160
each other and if we accidentally pass
13451
08:10:47,318 --> 08:10:51,558
something in that's not a person then we
13452
08:10:49,160 --> 08:10:52,840
just return back false so let's just
13453
08:10:51,558 --> 08:10:55,718
demonstrate this
13454
08:10:52,840 --> 08:10:59,200
first not the same so let's just save
13455
08:10:55,718 --> 08:11:01,240
five inside here it says not the same so
13456
08:10:59,200 --> 08:11:02,878
let's actually go into this place a
13457
08:11:01,240 --> 08:11:06,798
break point and let's run it
13458
08:11:02,878 --> 08:11:09,440
again hover over object then we get
13459
08:11:06,798 --> 08:11:11,600
five if we place this straight inside
13460
08:11:09,440 --> 08:11:13,840
the watch you can see object is person
13461
08:11:11,600 --> 08:11:15,798
is returning back false so it's skipping
13462
08:11:13,840 --> 08:11:17,478
this entire function and going into
13463
08:11:15,798 --> 08:11:20,718
return false and then it's returning
13464
08:11:17,478 --> 08:11:23,160
back false so we get not same let's stop
13465
08:11:20,718 --> 08:11:25,958
the program change this over to test and
13466
08:11:23,160 --> 08:11:27,920
rerun it using F5 and now we can see
13467
08:11:25,958 --> 08:11:30,760
object is person is is actually set to
13468
08:11:27,920 --> 08:11:33,520
true so now if you put the object inside
13469
08:11:30,760 --> 08:11:36,360
here you can see that generically it's
13470
08:11:33,520 --> 08:11:38,520
an object right here it says object so
13471
08:11:36,360 --> 08:11:40,600
what we can do now is we can place
13472
08:11:38,520 --> 08:11:42,080
person down here as well and you see
13473
08:11:40,600 --> 08:11:43,920
that person doesn't exist but if you
13474
08:11:42,080 --> 08:11:46,160
just give it a second and we change this
13475
08:11:43,920 --> 08:11:48,398
to just the
13476
08:11:46,160 --> 08:11:49,760
P there we go so obviously the person
13477
08:11:48,398 --> 08:11:53,080
doesn't exist cuz we're only up to this
13478
08:11:49,760 --> 08:11:55,398
line we press F10 and F10 again so now
13479
08:11:53,080 --> 08:11:57,600
it's created the variable and f101 so
13480
08:11:55,398 --> 08:12:00,120
skip over it and now you can see the
13481
08:11:57,600 --> 08:12:02,958
object has went from type object to type
13482
08:12:00,120 --> 08:12:05,440
string. override. person string override
13483
08:12:02,958 --> 08:12:07,280
is the name of our namespace and do
13484
08:12:05,440 --> 08:12:09,120
person with a capital P is the name of
13485
08:12:07,280 --> 08:12:11,080
our class now you can see that the
13486
08:12:09,120 --> 08:12:14,958
object and the values are exactly the
13487
08:12:11,080 --> 08:12:16,958
same 25 ab and 25 ABA as you can see
13488
08:12:14,958 --> 08:12:18,798
right here so now both of them are
13489
08:12:16,958 --> 08:12:22,120
exactly equal to each other so when we
13490
08:12:18,798 --> 08:12:26,478
check this name here is Aba name here is
13491
08:12:22,120 --> 08:12:28,200
ABBA age 25 and age is 25 so this must
13492
08:12:26,478 --> 08:12:30,120
return back true and we can check that
13493
08:12:28,200 --> 08:12:31,718
by highlighting everything dragging and
13494
08:12:30,120 --> 08:12:33,840
dropping it down here and we can see it
13495
08:12:31,718 --> 08:12:35,798
says true there so when it returns back
13496
08:12:33,840 --> 08:12:37,440
to our code it's going to go and print
13497
08:12:35,798 --> 08:12:40,200
their
13498
08:12:37,440 --> 08:12:41,478
same perfect there you go so that's it
13499
08:12:40,200 --> 08:12:43,760
for this video but let's just have a
13500
08:12:41,478 --> 08:12:45,440
recap before we wrap up so before this
13501
08:12:43,760 --> 08:12:47,718
video we'd always print the details out
13502
08:12:45,440 --> 08:12:48,878
like this person do return details
13503
08:12:47,718 --> 08:12:50,600
obviously we don't have that function
13504
08:12:48,878 --> 08:12:53,000
anymore so let's just bring it back
13505
08:12:50,600 --> 08:12:55,160
quickly there we go so we use return
13506
08:12:53,000 --> 08:12:57,240
details and this is just a bit long have
13507
08:12:55,160 --> 08:12:58,798
to do this every single time if if you
13508
08:12:57,240 --> 08:13:00,638
want to print the details of the test
13509
08:12:58,798 --> 08:13:02,638
object as well you have to do test Dot
13510
08:13:00,638 --> 08:13:04,240
and then return details again it'd be so
13511
08:13:02,638 --> 08:13:06,478
much easier if we could simply just use
13512
08:13:04,240 --> 08:13:08,478
it like this as you would do with a
13513
08:13:06,478 --> 08:13:11,120
normal variable like an integer double
13514
08:13:08,478 --> 08:13:13,398
string Etc and now it's made possible by
13515
08:13:11,120 --> 08:13:15,638
using our public override string two
13516
08:13:13,398 --> 08:13:17,240
string function it will contain exactly
13517
08:13:15,638 --> 08:13:19,040
the same contents as our previous
13518
08:13:17,240 --> 08:13:20,478
function except we won't have to go
13519
08:13:19,040 --> 08:13:22,478
through an additional function just to
13520
08:13:20,478 --> 08:13:24,200
print out the details we can override
13521
08:13:22,478 --> 08:13:25,878
the two string and then we can call it
13522
08:13:24,200 --> 08:13:27,878
straight in the console like this and
13523
08:13:25,878 --> 08:13:30,200
finally when you're doing equals inside
13524
08:13:27,878 --> 08:13:32,318
an object you can actually build in some
13525
08:13:30,200 --> 08:13:35,440
custom functionality we can convert the
13526
08:13:32,318 --> 08:13:37,080
person using object as person but first
13527
08:13:35,440 --> 08:13:39,680
before doing the conversion you want to
13528
08:13:37,080 --> 08:13:42,520
check if the object is indeed a person
13529
08:13:39,680 --> 08:13:44,080
type before doing the conversion this
13530
08:13:42,520 --> 08:13:45,958
will give you a runtime error on this
13531
08:13:44,080 --> 08:13:48,200
line so let me just demonstrate that
13532
08:13:45,958 --> 08:13:49,878
before we continue so if I comment out
13533
08:13:48,200 --> 08:13:51,680
this one and actually just change this
13534
08:13:49,878 --> 08:13:54,280
into
13535
08:13:51,680 --> 08:13:55,040
5D and then let's press into the chorde
13536
08:13:54,280 --> 08:13:57,360
right
13537
08:13:55,040 --> 08:13:59,638
here it's going to hit this line if I
13538
08:13:57,360 --> 08:14:00,878
press F10 then it's going to try and
13539
08:13:59,638 --> 08:14:03,160
convert it but you're going to get back
13540
08:14:00,878 --> 08:14:04,558
null and then if you run the code again
13541
08:14:03,160 --> 08:14:06,318
then you're going to exception thrown
13542
08:14:04,558 --> 08:14:08,398
here you're going to get a runtime
13543
08:14:06,318 --> 08:14:10,200
exception because it's a null reference
13544
08:14:08,398 --> 08:14:12,440
we're trying to run the equals function
13545
08:14:10,200 --> 08:14:14,638
on something that is null which is right
13546
08:14:12,440 --> 08:14:16,240
here null reference exception so you
13547
08:14:14,638 --> 08:14:18,440
shouldn't do this you should also make
13548
08:14:16,240 --> 08:14:20,638
sure that these are correct values
13549
08:14:18,440 --> 08:14:22,558
before doing it this is a form of error
13550
08:14:20,638 --> 08:14:24,120
checking if you don't do this then
13551
08:14:22,558 --> 08:14:26,280
you're going to have to wrap this inside
13552
08:14:24,120 --> 08:14:27,798
a TR catch and it will look something
13553
08:14:26,280 --> 08:14:29,840
like this and and the cord will be
13554
08:14:27,798 --> 08:14:30,840
considerably longer you can place these
13555
08:14:29,840 --> 08:14:33,600
two
13556
08:14:30,840 --> 08:14:36,280
lines inside here and then place your
13557
08:14:33,600 --> 08:14:38,360
cat inside here etc etc and you can
13558
08:14:36,280 --> 08:14:40,280
return false inside here but as you can
13559
08:14:38,360 --> 08:14:42,160
see the code is a bit more complicated
13560
08:14:40,280 --> 08:14:44,120
now and there's no reason for this extra
13561
08:14:42,160 --> 08:14:46,478
complication if you can prevent it with
13562
08:14:44,120 --> 08:14:49,200
an if statement so let's undo
13563
08:14:46,478 --> 08:14:52,398
that so what's best in this case is to
13564
08:14:49,200 --> 08:14:54,238
say if the object is of type person then
13565
08:14:52,398 --> 08:14:56,160
we can run the conversion if we run the
13566
08:14:54,238 --> 08:14:57,760
conversion and it's not of type person
13567
08:14:56,160 --> 08:14:59,478
then as you can see it's going to return
13568
08:14:57,760 --> 08:15:01,080
back null for the person and then when
13569
08:14:59,478 --> 08:15:02,520
you run this line it's actually going to
13570
08:15:01,080 --> 08:15:04,638
return you back a null reference
13571
08:15:02,520 --> 08:15:06,680
exception at this point but anyway the
13572
08:15:04,638 --> 08:15:08,318
whole point of this equals is to check
13573
08:15:06,680 --> 08:15:10,760
if two objects are going to be equal to
13574
08:15:08,318 --> 08:15:12,600
each other so we keep this back to test
13575
08:15:10,760 --> 08:15:14,958
because this is a custom classes you
13576
08:15:12,600 --> 08:15:16,600
can't just simply do person equals
13577
08:15:14,958 --> 08:15:18,200
equals test and assume that that will
13578
08:15:16,600 --> 08:15:20,478
just work these aren't buil-in
13579
08:15:18,200 --> 08:15:22,160
functionalities for custom classes we
13580
08:15:20,478 --> 08:15:23,718
covered operate overloading where you
13581
08:15:22,160 --> 08:15:25,718
can actually overload what you do with
13582
08:15:23,718 --> 08:15:27,798
the equal sign but we didn't cover the
13583
08:15:25,718 --> 08:15:30,120
do equals which is what we're doing
13584
08:15:27,798 --> 08:15:32,120
today in this video as well as the two
13585
08:15:30,120 --> 08:15:33,798
string this is a different kind of
13586
08:15:32,120 --> 08:15:35,680
overriding because in the other one when
13587
08:15:33,798 --> 08:15:37,440
we're doing operator overloading that's
13588
08:15:35,680 --> 08:15:39,360
specifically an operator that you're
13589
08:15:37,440 --> 08:15:41,318
just changing the parameters of whereas
13590
08:15:39,360 --> 08:15:43,318
this one is a strict override of a
13591
08:15:41,318 --> 08:15:45,318
function equals and the functions two
13592
08:15:43,318 --> 08:15:47,398
string this will make it easier to print
13593
08:15:45,318 --> 08:15:49,398
your custom classes out to the screen as
13594
08:15:47,398 --> 08:15:51,398
well as comparing them just like this
13595
08:15:49,398 --> 08:15:53,958
person. equals putting test inside the
13596
08:15:51,398 --> 08:15:55,520
bracket is a lot easier and quicker and
13597
08:15:53,958 --> 08:15:57,360
you can build in your own custom
13598
08:15:55,520 --> 08:15:58,798
functionality if you want only had the
13599
08:15:57,360 --> 08:16:00,840
age in there then you can compare the
13600
08:15:58,798 --> 08:16:02,200
age maybe you have a lot more properties
13601
08:16:00,840 --> 08:16:04,238
and you can always just chain them
13602
08:16:02,200 --> 08:16:05,680
inside a big if statement that's fine as
13603
08:16:04,238 --> 08:16:08,160
well but the point is you have full
13604
08:16:05,680 --> 08:16:10,638
control over how you compare your custom
13605
08:16:08,160 --> 08:16:12,878
classes maybe you had a list of names
13606
08:16:10,638 --> 08:16:15,478
inside a dictionary for example and you
13607
08:16:12,878 --> 08:16:18,000
wanted to add a new name or a new age or
13608
08:16:15,478 --> 08:16:19,478
a new user to this dictionary but first
13609
08:16:18,000 --> 08:16:21,440
before you add a new user to the
13610
08:16:19,478 --> 08:16:23,360
dictionary you want to check if the user
13611
08:16:21,440 --> 08:16:25,718
already exists so maybe you could have a
13612
08:16:23,360 --> 08:16:26,920
system where it uses the equals function
13613
08:16:25,718 --> 08:16:28,638
and it Loops through all of the
13614
08:16:26,920 --> 08:16:30,878
dictionary items and it checks if all
13615
08:16:28,638 --> 08:16:32,600
the values are not equal to this one if
13616
08:16:30,878 --> 08:16:34,680
you didn't build in this functionality
13617
08:16:32,600 --> 08:16:36,638
so you could do the equals function then
13618
08:16:34,680 --> 08:16:39,120
you won't be able to determine if this
13619
08:16:36,638 --> 08:16:41,558
one is equal to this one and now with
13620
08:16:39,120 --> 08:16:44,200
your equals function overrided you can
13621
08:16:41,558 --> 08:16:46,440
now have the functionality to do so
13622
08:16:44,200 --> 08:16:47,478
thank you for watching this C course I
13623
08:16:46,440 --> 08:16:49,360
hope you found it helpful in your
13624
08:16:47,478 --> 08:16:51,000
journey to becoming a proficient C
13625
08:16:49,360 --> 08:16:52,680
developer if you have any questions or
13626
08:16:51,000 --> 08:16:54,280
need further clarification on any topic
13627
08:16:52,680 --> 08:16:55,958
we have covered please don't hesitate to
13628
08:16:54,280 --> 08:16:57,718
leave a comment below and I will do my
13629
08:16:55,958 --> 08:16:59,680
best to assist you for those of you who
13630
08:16:57,718 --> 08:17:01,558
want to dive deeper in a cop consider
13631
08:16:59,680 --> 08:17:03,200
checking out my udemy course you'll find
13632
08:17:01,558 --> 08:17:05,080
more comprehensive lessons and Hands-On
13633
08:17:03,200 --> 08:17:07,120
exercises to enhance your skills
13634
08:17:05,080 --> 08:17:08,958
remember practice makes perfect keep
13635
08:17:07,120 --> 08:17:12,840
coding and exploring and I wish you all
13636
08:17:08,958 --> 08:17:12,840
the best on your programming Journey
1006590
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.