YouTube2Text

Learn C# Programming – Full Course with Mini-Projects — Transcript

by freeCodeCamp.org · 102,522 words · 13,636 segments · language en · Watch on YouTube

Full transcript

  1. 0:00this comprehensive C course will take
  2. 0:02you from the basics of C programming to
  3. 0:04advanced concepts all while integrating
  4. 0:07exciting mini projects to solidify your
  5. 0:09learning this course offers a structured
  6. 0:12path through essential topics such as
  7. 0:14syntax object oriented programming data
  8. 0:16structures and more ABA teaches this
  9. 0:19course and he will help you understand C
  10. 0:21both theoretically and also gain
  11. 0:24practical experience in applying your
  12. 0:25knowledge to real world scenarios hey
  13. 0:28there free ABA here join me in Walking
  14. 0:31you through everything you need to get a
  15. 0:32setup with cop let's have a quick walk
  16. 0:34through the course contents to let you
  17. 0:35know what to install for you we'll start
  18. 0:37with installing the visual studio
  19. 0:39program required to develop C
  20. 0:40applications you'll learn about data
  21. 0:42types including integers strings and
  22. 0:44many more and how to manipulate them
  23. 0:46inside your code next we'll dive into
  24. 0:48control flow you'll discover how to use
  25. 0:50conditional statements and Loops to
  26. 0:51control the flow of your program's
  27. 0:53execution strings are essential in many
  28. 0:55applications you'll Master powerful
  29. 0:57string functions in C allowing you to
  30. 0:59manipulate and transform Text
  31. 1:01effortlessly data structures are crucial
  32. 1:03for organizing and managing data from
  33. 1:05arrays to collections you'll explore
  34. 1:07various data structures in C and learn
  35. 1:09how to use them effectively functions
  36. 1:11are the backbone of any program you'll
  37. 1:13uncover the power of functions in C
  38. 1:15including how to create and call them to
  39. 1:17organize your code no program is immune
  40. 1:19to errors you'll learn how to handle
  41. 1:21exceptions gracefully in C ensuring that
  42. 1:23your applications remain robust and
  43. 1:25reliable debugging is a crucial skill
  44. 1:27for any developer you'll discover how to
  45. 1:29use visual studios's powerful debugging
  46. 1:31tools to identify and fix bugs in your C
  47. 1:34code efficiently finally we'll delve
  48. 1:36into object-oriented programming you'll
  49. 1:38learn the principles of object-oriented
  50. 1:40programming how to use classes objects
  51. 1:42inheritance and polymorphism to create
  52. 1:44modular and maintainable code by the end
  53. 1:46of this course you'll have a solid
  54. 1:48understanding of C programming and be
  55. 1:50well equipped to tackle a wide range of
  56. 1:52software development challenges thanks
  57. 1:54for tuning in let's get
  58. 1:56started so in order to start developing
  59. 1:58with C we need to install the latest
  60. 2:00version of visual studio in this case
  61. 2:02it's currently 2022 so let's head over
  62. 2:04Google and type in Visual Studio 2022
  63. 2:07download we will be presented with two
  64. 2:09links we have this one and this one we
  65. 2:11want to use a second one which says
  66. 2:12download visual studio tools so if you
  67. 2:14press on that and now will present it
  68. 2:16with the download area we have three
  69. 2:18additions Community professional
  70. 2:20Enterprise Community is free for
  71. 2:22students open source contributors and
  72. 2:24individuals which is exactly what we
  73. 2:25need for the content of this course we
  74. 2:27have the professional Enterprise Edition
  75. 2:29which paed for and only used by bigger
  76. 2:31companies or smaller teams so let's go
  77. 2:33ahead and press free download the
  78. 2:35download will start straight
  79. 2:37away after the download is complete
  80. 2:39let's start the installer so double
  81. 2:41click on the executable and press yes to
  82. 2:43allow it for admin permissions it'll pop
  83. 2:45up like this Visual Studio installer
  84. 2:47before you get started we need to set up
  85. 2:48a few things you can configure your
  86. 2:50installation you can read the privacy
  87. 2:52policy in the software terms if you'd
  88. 2:53like or you can just press continue so
  89. 2:55it's getting the visual studio installed
  90. 2:58already so so when you get to this
  91. 3:00section everything here is all the
  92. 3:01different packages and tools you can
  93. 3:03install within Visual Studio 2022 the
  94. 3:06main one that we're concerned within the
  95. 3:07content to this CA is the neck desktop
  96. 3:09environment so you can go ahead and
  97. 3:11press this one it will then tell you in
  98. 3:13the bottom right hand corner the total
  99. 3:15space required is 6.52 GB please make
  100. 3:18sure you have enough storage space
  101. 3:20available for this and then you can go
  102. 3:21ahead and press install if you want to
  103. 3:23check out other packages you can have a
  104. 3:24scroll through and read any other
  105. 3:26packages you may need and may want to
  106. 3:28install like I said the the only one
  107. 3:30required for this course is the net
  108. 3:31desktop
  109. 3:32environment you can also deal with noj
  110. 3:34development python development web
  111. 3:36development if you need to deal with
  112. 3:38individual components like specific
  113. 3:40versions you can access it here if you
  114. 3:42need some more languages installed you
  115. 3:44can access it here this should
  116. 3:45automatically select your default
  117. 3:47language for you that's based on your
  118. 3:48systems language and if you want it to
  119. 3:51you can also change the installation
  120. 3:52location in case you want to install it
  121. 3:54on a different drive or you just want it
  122. 3:56in a different folder so now that we
  123. 3:58selected the neck desktop environment
  124. 4:00we can go ahead and press the
  125. 4:01installation button we do have two
  126. 4:03options down here we have it install
  127. 4:05while downloading and a download all
  128. 4:06then install if you have a reasonably
  129. 4:09fast computer you can stick with the
  130. 4:10install while downloading and it will do
  131. 4:12the installation and the downloading at
  132. 4:14the same time if you have a slow
  133. 4:16computer it's preferred that you use
  134. 4:17this option to download it first and
  135. 4:19then install it just means that it won't
  136. 4:21slow down your computer too much so you
  137. 4:23can go for this one and press install
  138. 4:26now the installation will begin so now
  139. 4:28that Visual Studio is finished finish
  140. 4:29installing it will say done installing
  141. 4:31Visual Studio has been successfully
  142. 4:33installed we recommend rebooting soon to
  143. 4:35clean up any remaining files so this
  144. 4:37just means restart on your computer just
  145. 4:38so it has a chance to clean up any files
  146. 4:40that were kept during the installation
  147. 4:41process so we can just press okay if you
  148. 4:45have any other the versions of Visual
  149. 4:46Studio installed they'll also show up
  150. 4:48down here and now that Visual Studio
  151. 4:50Community 2022 is installed we can just
  152. 4:52press
  153. 4:55launch so now it'll prompt you to sign
  154. 4:57into Visual Studio we should definitely
  155. 4:59sign in but just for now I'm going to
  156. 5:00skip but feel free to create an account
  157. 5:02or sign in if you already have one
  158. 5:04existing we can skip this and then we
  159. 5:06get to choose your theme I personally
  160. 5:08prefer the Dark theme but you can go
  161. 5:09with blue blue extra contrast or the
  162. 5:11light mode so let's select dark and hit
  163. 5:13start Visual Studio it will do a first
  164. 5:16time
  165. 5:17setup and it's already done and we're
  166. 5:19already inside Visual Studio 2022 so
  167. 5:22that's it for the visual studio
  168. 5:25installation so now that we got Visual
  169. 5:27Studio 2022 installed let's see how to
  170. 5:30create your first project so once you've
  171. 5:31opened Visual Studio 2022 you'll see
  172. 5:34this window popping up you can clone a
  173. 5:36repository from things like GitHub in as
  174. 5:37you devops or open a project that you
  175. 5:39already have or open a folder which may
  176. 5:42have projects inside or you can create
  177. 5:43one from scratch if you don't want to do
  178. 5:46any of these options you can just open
  179. 5:47the visual studio program just on its
  180. 5:49own by clicking continue without code so
  181. 5:51let's go down the create a project path
  182. 5:53so click on create a new project and
  183. 5:55we'll get a list of all these different
  184. 5:56projects inside our system to be able to
  185. 5:58use the ones we're going to be be
  186. 5:59focusing on on this course is going to
  187. 6:01be C in the languages dropdown box and
  188. 6:04the platform is going to be windows and
  189. 6:06the project type is going to be console
  190. 6:08and they have one that uses net core
  191. 6:10which is for Linux Mac and windows and
  192. 6:12one that uses the Net Framework which is
  193. 6:14Windows so we want to go for this one so
  194. 6:16press on this one and then press
  195. 6:18next here we have our project name our
  196. 6:21location and our console app so we can
  197. 6:24start with the project name and say my
  198. 6:26first
  199. 6:27project as you can see it'll start
  200. 6:29copying over to the solution name as
  201. 6:31well and it tells you where the project
  202. 6:32will be created into so I'll be creating
  203. 6:34a folder called my first project which
  204. 6:37is the solution name plus my first
  205. 6:39project which is the project name inside
  206. 6:40that folder if we change the solution
  207. 6:42name you'll see update you can see here
  208. 6:45now we have multiple T's because we
  209. 6:46appended the solution name the solution
  210. 6:49name is almost like a folder that holds
  211. 6:51lots of projects inside so to begin with
  212. 6:53we'll have one solution that has one
  213. 6:55project inside and then later on in the
  214. 6:57course if you feel like you want to add
  215. 6:59more projects to it you can just add
  216. 7:00more to this solution so let's get
  217. 7:02started and press
  218. 7:08create so now we've created our very
  219. 7:10first project and just to explain what's
  220. 7:12here we have a static main void which is
  221. 7:15the main entry point to our program if
  222. 7:17you do a lowercase M then the system is
  223. 7:19not going to recognize this main
  224. 7:21function because the down NET Framework
  225. 7:23requires this function to be found in
  226. 7:24order to run a c console app and you can
  227. 7:27see here in the errors the program
  228. 7:29program does not contain a static main
  229. 7:31function and you can see it's capital M
  230. 7:33and C programming language is K
  231. 7:35sensitive so make sure to include it so
  232. 7:37now you understand how to use Visual
  233. 7:38Studio 2022 to create a project and also
  234. 7:41know where the main entry point is in C
  235. 7:43let's get started with a Hello World
  236. 7:45example to kick off this
  237. 7:48course so as we know our main entry
  238. 7:50point to our application is the static
  239. 7:52void main if we go inside here and press
  240. 7:54enter then we can actually start writing
  241. 7:56some code inside here just a quick note
  242. 7:59before we
  243. 8:00continue static void main is inside the
  244. 8:03class program and the class program is
  245. 8:06inside the namespace hello world and the
  246. 8:08reason why you can tell this is because
  247. 8:10of the curly braces the curly braces of
  248. 8:12hello world begins here and ends here
  249. 8:15and Within These curly braces we have a
  250. 8:17class program and also within the class
  251. 8:19program we have the styo
  252. 8:22men so just between these curly braces
  253. 8:25we want to type console. right line or
  254. 8:29open the brackets and we can type in
  255. 8:31some speech marks and we can simply Type
  256. 8:33Hello World there you go so as you can
  257. 8:36see now system has actually been
  258. 8:39highlighted so if we just delete this
  259. 8:42line you can see now system has been gr
  260. 8:45out and the reason why is it says it's
  261. 8:47unnecessary because it hasn't been
  262. 8:50used so if you undo it then this comes
  263. 8:55up because I showed you before that
  264. 8:57console right line is actually within
  265. 8:59the system Library what happens if we
  266. 9:01delete
  267. 9:02it if we delete it then it doesn't know
  268. 9:05where console exists from and because
  269. 9:07that just said system we can actually
  270. 9:09just use system. console. right line and
  271. 9:12then open the brackets and type in hello
  272. 9:14world and now this works again and the
  273. 9:17reason why this works it's because we
  274. 9:19don't have to Define that we're using
  275. 9:21system we could just use it straight in
  276. 9:24here if you have multiple lines like
  277. 9:27this then you don't want to be using
  278. 9:29system right at the start ideally you
  279. 9:31want to have the reference to it at the
  280. 9:32top of the screen and then use it
  281. 9:35throughout so now that we've included
  282. 9:37back in the project you can see that now
  283. 9:39these have turned gray and it says that
  284. 9:41it can be simplified if we press the
  285. 9:42little light bulb and we can say
  286. 9:44simplify member and it doesn't really
  287. 9:47matter because we can just delete the
  288. 9:48rest now so now that we've WR hello
  289. 9:51world let's press F5 which is a shortcut
  290. 9:53to the start
  291. 9:55button as you can see well you couldn't
  292. 9:58really see because the program closed
  293. 9:59too quickly and the reason for that is
  294. 10:02printed hello world to the console and
  295. 10:04then it didn't do anything else cuz
  296. 10:05there's nothing else for it to do so the
  297. 10:07console simply
  298. 10:08closed we can use another command called
  299. 10:12console read Line open the brackets and
  300. 10:14close the brackets and have a semicolon
  301. 10:16at the end all this does is it waits
  302. 10:18here until the user inputs something
  303. 10:21reads the next line of characters from
  304. 10:22the standard input
  305. 10:25stream so although the program will
  306. 10:27actually be reading stuff we just want
  307. 10:29this line to stop the application from
  308. 10:31closing so now if you press F5 and run
  309. 10:33the program you can see it actually says
  310. 10:35hello world and it's just waiting here
  311. 10:38flashing and this flashing means it just
  312. 10:41started this console read line so it
  313. 10:42doesn't matter what we type into here it
  314. 10:44just WS for us to press enter and it
  315. 10:46soon as we press enter this line
  316. 10:48executes and the program finishes
  317. 10:50congratulations you just wrote your
  318. 10:52first C program welcome to the amazing
  319. 10:55journey of
  320. 10:56programming so let's look at how to make
  321. 10:58VAR Ables in our C code let's start
  322. 11:01every code with a console read line and
  323. 11:04then we can hit F5 just to make sure our
  324. 11:06console doesn't
  325. 11:07close so let's begin with an integer so
  326. 11:11integers are whole numbers so we can
  327. 11:13just say int age equals
  328. 11:1523 so what we've actually done here is
  329. 11:18we've said we want an INT we want to
  330. 11:20call it age already made it equal to 23
  331. 11:25this is called initializing and
  332. 11:27declaring in the same line
  333. 11:30so what you could do alternatively is
  334. 11:31you can end the semicolon here and it
  335. 11:33just says int Edge and then on the next
  336. 11:36line you can write ede equals 23 so this
  337. 11:38is declaring it in the first line and
  338. 11:40then give it an initial value in the
  339. 11:42second line if they're right underneath
  340. 11:44each other like this case then you may
  341. 11:46as well just make it together because
  342. 11:47then you have less lines of code the
  343. 11:49reason why it says it's a green under
  344. 11:50line it's because it's the unnecessary
  345. 11:52assignment because we're not actually
  346. 11:54using ede so let's just point age to the
  347. 11:56console if we make some new lines and we
  348. 11:58can type in in console. right line and
  349. 12:00then press tab to finish it we can write
  350. 12:03Edge and if we run our code using F5 we
  351. 12:06can see that 23 gets print out to the
  352. 12:09screen so now that we've worked on the
  353. 12:11Edge Let's explore all data types let's
  354. 12:14define a long variable so let's start
  355. 12:16with the type long followed by space and
  356. 12:18then the variable name that we want and
  357. 12:20let's add equals and type in a very big
  358. 12:23number so now if we hover over it it
  359. 12:25says it's a system int32 and int32 is
  360. 12:29actually an integer but we're telling it
  361. 12:31it's a long which makes no sense we have
  362. 12:34an in 64 here but when we hover over the
  363. 12:36number it comes up as n32 and the reason
  364. 12:40for that is when C sees that you just
  365. 12:42have a number typed in like this you
  366. 12:44will always assume that it is an integer
  367. 12:47to tell the compiler that you indeed
  368. 12:49want this to be processed as a long
  369. 12:50number then you can put a capital l
  370. 12:52right at the end now if we hover over it
  371. 12:54it says in
  372. 12:5664 and this says in 64 so now we've got
  373. 12:59it working and we've got the long there
  374. 13:01so now we can take another console right
  375. 13:03line and just print this out to the
  376. 13:04screen and just a helpful tip if you
  377. 13:06type in CW and press tab twice then you
  378. 13:09get the console right line it's very
  379. 13:11easy to write your code and then you can
  380. 13:13type in big followed by a tab and it'll
  381. 13:15come up straight away so as you can see
  382. 13:17they're both being printed to the
  383. 13:18console
  384. 13:19now so now that we've made positive
  385. 13:21numbers we can also make these negative
  386. 13:24by simply just putting a negative sign
  387. 13:26behind them there we go and just to to
  388. 13:29show you how big these numbers are what
  389. 13:31we can do we can type in int. max value
  390. 13:34and then make another one using contr D
  391. 13:36and type in int. Min
  392. 13:38value so you can see integers can be
  393. 13:41positive 2.1 billion or netive - 2.1
  394. 13:46billion and if we do the same for
  395. 13:52long you can see that long is a very
  396. 13:55very big number because instead of being
  397. 13:56an INT 32 it's an in 64 which which
  398. 13:59takes up a lot more memory so now we've
  399. 14:02covered whole numbers let's have a look
  400. 14:04at decimals so we can have a double
  401. 14:07negative for example and we can say it's
  402. 14:09-
  403. 14:1055.2 and in this case when you just have
  404. 14:13a number typed in like this and it's got
  405. 14:15a decimal point in it it will always
  406. 14:17register it as a double but just to make
  407. 14:20sure just as we're doing with the long
  408. 14:22you can actually just put a capital D at
  409. 14:24the
  410. 14:25end now if we print this out to the
  411. 14:27screen and also print out how big these
  412. 14:30numbers can
  413. 14:31be max value and Min value and print
  414. 14:36this out so we get our minus 55.2 and
  415. 14:39you can see that these numbers can be
  416. 14:40very big e+ 308 means it's this number
  417. 14:45times by 10 the^ of 308 which is a very
  418. 14:48big number and we also have that as a
  419. 14:51negative so we have float let's call
  420. 14:53this precision and make equal
  421. 14:575.1 and then put a semic call the end
  422. 15:00now we've got a problem here this is
  423. 15:01trying to register as a double but we
  424. 15:03don't want to register a double we want
  425. 15:05to register as a float and this is the
  426. 15:06same issue we have the long so when you
  427. 15:09type any number that has a decimal point
  428. 15:11in it the C compiler will always
  429. 15:13register as a double the same way when
  430. 15:15you type in a whole number it'll always
  431. 15:17register as an INT so in this case we
  432. 15:19need to put a capital f at the end and
  433. 15:22it'll process this as you
  434. 15:24float so now we can print this
  435. 15:27out and then let's just print out the
  436. 15:29max values and the Min values just to
  437. 15:33show you what that looks
  438. 15:34like so as you can see here we have our
  439. 15:36number printed out and again we have
  440. 15:38quite a big number 3.4 * by 10 to the^
  441. 15:42of 38 so again it's a big number and we
  442. 15:44have that positive and negative so quite
  443. 15:46a big range of
  444. 15:48numbers and the last one we're going to
  445. 15:50cover is decimal for things like money
  446. 15:53and currency
  447. 15:5514.99 and again in this case it's trying
  448. 15:58to assign a double into a decimal so in
  449. 16:01order to denote what a decimal is you
  450. 16:03can put a capital M right at the
  451. 16:05end and for one last time let's print
  452. 16:08out the
  453. 16:09money and the decimal. max value contrl
  454. 16:13D and then we'll do Min
  455. 16:17value and you can see here that this is
  456. 16:19a really big number as well and it's
  457. 16:21even bigger than the
  458. 16:23long so we have our number printed and
  459. 16:25the Max and the minimum values so now
  460. 16:27that we've understood that how to
  461. 16:29declare a variable I have an extra
  462. 16:31little tip for you so if we had three
  463. 16:33variables int X an INT Y and an INT
  464. 16:37Z and let's say you're always going to
  465. 16:39declare them together like this you can
  466. 16:41actually miss out and do X comma y comma
  467. 16:44Z and that will do exactly the same
  468. 16:47thing there you go and that's really
  469. 16:50neat and it's a lot better let's say for
  470. 16:52example you had these variables but you
  471. 16:55actually had them set to something like
  472. 16:56equals 10 equals 20 equal 30 this is
  473. 17:01still practical but it might look a
  474. 17:03little
  475. 17:04uglier so you can actually type them in
  476. 17:06straight into here and if you comment
  477. 17:09these out again then it still works you
  478. 17:12can even put these onto new lines like
  479. 17:13this just so it looks a bit neater just
  480. 17:16prevents you from typing the additional
  481. 17:18int each time if they're all going to be
  482. 17:20the same variable type so that's it for
  483. 17:22the numbers inside our code so let's
  484. 17:24just have a quick recap so in order to
  485. 17:27Define a variable you want to use the
  486. 17:29data type in this case an integer
  487. 17:31followed by the variable name and then
  488. 17:33you can either add a semicolon at that
  489. 17:35point or you can add equals to give it a
  490. 17:37value this is just called declaring a
  491. 17:40value and this is called initializing a
  492. 17:42value if you want to decide you want to
  493. 17:44change the variable further down your
  494. 17:47program for example age equals 50 here
  495. 17:50then this is fine and this is now called
  496. 17:52an
  497. 17:53assignment it's called initialization
  498. 17:56because as soon as you make it you give
  499. 17:58it a value and then in later on in the
  500. 18:00code if you give it another value this
  501. 18:02is called simply assigning a value this
  502. 18:05is just called assigning it a
  503. 18:08value so now that we can store numbers
  504. 18:10in C let's see how we can store
  505. 18:13characters and letters so as you know
  506. 18:15from the introduction we have a string
  507. 18:16variable so let's call that name and
  508. 18:19make it equal to
  509. 18:20Aba please note that when you're
  510. 18:23defining a string and giving it a value
  511. 18:25that this value needs to be in speech
  512. 18:28marks if you put it in quotation marks
  513. 18:30it will not accept this because that's
  514. 18:32the notation to declare a Char which
  515. 18:35we'll get on to next so on the next line
  516. 18:37we can do a Char letter equals and we
  517. 18:40can't have something like this because
  518. 18:42the Char will only accept One Singular
  519. 18:44character so we have to wrap it around
  520. 18:46the quotation mark once you've wrapped
  521. 18:49it around the quotation mark then it
  522. 18:50will accept it so let's print these two
  523. 18:52to the screen so CW tab tab and we can
  524. 18:56just print these to the screen and if we
  525. 18:58just run the code you can see that we've
  526. 19:00got the values entered in
  527. 19:03here we can start to write
  528. 19:05stuff your name
  529. 19:08is and then we can output this and we
  530. 19:11can also put it on one
  531. 19:13line if we do a console right and a
  532. 19:16console right then maybe we can take the
  533. 19:18callon off and just have it space and we
  534. 19:20can say your name is ABBA and we' see
  535. 19:22that additional a because that's the
  536. 19:24right line for the letter so we can
  537. 19:26space it out with an empty console right
  538. 19:28line and and they'll add a gap between
  539. 19:29them both there we
  540. 19:32go so now we know how to declare a
  541. 19:34string variable and give it a value and
  542. 19:36we can declare a Char variable and also
  543. 19:38give it a value just a quick recap when
  544. 19:41you're declaring and initializing string
  545. 19:43variables you need to put the value in
  546. 19:45speech marks but when you're declaring
  547. 19:47and initializing a Char you don't have
  548. 19:49to put it in speech marks you put them
  549. 19:50in apostrophes whenever you see
  550. 19:52apostrophes that that can only ever
  551. 19:53refer to One Singular character when you
  552. 19:55see a speech mark it could be one or
  553. 19:57more characters
  554. 19:59also things can be empty this just means
  555. 20:02the string is empty this is also a valid
  556. 20:04character it just means that we're
  557. 20:05storing a string but it has no value
  558. 20:08with a Char that's not exactly the same
  559. 20:10you can't store an empty character
  560. 20:11literal it has to be given something
  561. 20:14Charles get defaulted at back sl0 which
  562. 20:17is just a default character but don't
  563. 20:19worry about that too much now we'll go
  564. 20:20into Escape characters which is the back
  565. 20:22SL layer on in the course for now just
  566. 20:24note that whenever you declare a string
  567. 20:26variable we need to put it in the speech
  568. 20:27marks and when we declaring a Char
  569. 20:29variable we need the quotation
  570. 20:32mark So now that we know how to define
  571. 20:34numbers in C and to also Define strings
  572. 20:37and characters then let's look at how to
  573. 20:39convert between them both so let's take
  574. 20:41some similar code from our first video
  575. 20:43and we can Define it age the number the
  576. 20:45negative the Precision and the money and
  577. 20:47we give it the same values from the
  578. 20:49first video we simply just print them
  579. 20:51out to the console here and if we just
  580. 20:53run we can see them all but this doesn't
  581. 20:55help us cuz we're just statically typing
  582. 20:57in the numbers what if these these will
  583. 20:58actually process a string and we want to
  584. 21:00convert them into an INT so just above
  585. 21:03each of the variables we're going to
  586. 21:04make another variable so we can say a
  587. 21:06string text age equals
  588. 21:08-23 and then for the age what we're
  589. 21:11going to use is this function called
  590. 21:12convert to int 32 and we can put in the
  591. 21:16string inside the brackets and what this
  592. 21:18is now saying is we're taking our string
  593. 21:21text Edge which is equal to -23 and
  594. 21:24we're passing it into this function
  595. 21:25convert to in32 and the reason why it's
  596. 21:28in 32 is because an INT is by definition
  597. 21:31an in32 which is a 32bit signed integer
  598. 21:35so what we want to do here is convert
  599. 21:37for our text age into int32 and store it
  600. 21:41back into our int and hopefully we
  601. 21:43should see the same value when we hit
  602. 21:46run 23 so now that's actually went from
  603. 21:49a string variable and then went into our
  604. 21:52integer and we've printed out as an
  605. 21:54integer and it's no longer a string
  606. 21:56variable now we can do the same for the
  607. 21:58other ones so we can have a string text
  608. 22:01big number and make it equal to this
  609. 22:04value just in the quotation
  610. 22:07marks now we're typing this value in the
  611. 22:09quotation marks we don't need to put the
  612. 22:11L at the end the L is just to tell the
  613. 22:13compiler that we want to use in 64 if we
  614. 22:15take the L away then it's going to think
  615. 22:17it wants in 32 this is not re applicable
  616. 22:20when it comes to text because we're
  617. 22:21going to explicitly stay on the next
  618. 22:23line we want to convert to N64 so the
  619. 22:26compiler already knows that it's in 64
  620. 22:28for and we don't need to put an L at the
  621. 22:30end now let's make sure this works and
  622. 22:32there we go so what we should end up
  623. 22:34with is the console should look exactly
  624. 22:36the same as how we had it at the start
  625. 22:38of the video but actually what we're
  626. 22:39doing is converting it from a string
  627. 22:41into the applicable value string text
  628. 22:46negative equal -
  629. 22:4955.2 and then in here we can do convert
  630. 22:53to double add the text negative inside
  631. 22:56here and let's just run it to check
  632. 22:59and we still have the same value so it's
  633. 23:01going well have a string text
  634. 23:07Precision make it equal to
  635. 23:10this and then instead of this we can
  636. 23:13have a convert two and in this case it's
  637. 23:16actually going to be a single and the
  638. 23:18reason why it's a single when you hover
  639. 23:20of a float the definition for a float is
  640. 23:22indeed a single so we can add that in
  641. 23:25text precision and we're just going to
  642. 23:27do the last one and just check it out Al
  643. 23:28together text money equals
  644. 23:3314.99 and then instead of this we can
  645. 23:35have a
  646. 23:36convert and let's have a look at this a
  647. 23:38decimal is indeed a decimal so we can
  648. 23:40say two decimal and then add the text
  649. 23:43money
  650. 23:43in perfect now if you run the line of
  651. 23:46code we can see that all of these values
  652. 23:48are exactly as we had them now they're
  653. 23:50being represented by their official
  654. 23:52descriptions Based On A String
  655. 23:54conversion as I mentioned before in the
  656. 23:56error video the problem you'll have with
  657. 23:58this is if this value actually has a
  658. 24:00character inside you will get a runtime
  659. 24:03error this can be fixed in the future
  660. 24:05when we look at how to resolve these
  661. 24:07errors and we can have exception
  662. 24:09handling and use functions like try pass
  663. 24:12so we can try and avoid these exceptions
  664. 24:14occurring because when these exceptions
  665. 24:16occur the rest of our code does not run
  666. 24:18and it becomes fatal at this
  667. 24:20point but for now don't worry about this
  668. 24:22too much just get used to the notation
  669. 24:25and how these things
  670. 24:27work so this is video we're going to
  671. 24:29focus on the Boolean data type so we can
  672. 24:31make a Boolean data type by saying Bo
  673. 24:34value equals
  674. 24:36true this Boolean value is literally
  675. 24:38just a true or false value we can store
  676. 24:41anything in here and maybe something
  677. 24:42like bull is M and we can just say it
  678. 24:45true and later on our code boings are
  679. 24:47used to make decisions we'll cover this
  680. 24:50in the next section of this course but
  681. 24:52boings are mainly used for making
  682. 24:54decisions if one thing is equal to
  683. 24:56another thing then we can take another
  684. 24:58path or if something is true or
  685. 25:00something is false then we can do
  686. 25:01another path for example if you make an
  687. 25:03output to the user you could check if
  688. 25:05this value is true and if the value is
  689. 25:07true then you can address the user as a
  690. 25:09male and if the value is false then you
  691. 25:11can address the user as a female these
  692. 25:13are the kind of things that booleans can
  693. 25:15help us with and just like any variable
  694. 25:17if you want to assign it later on you
  695. 25:18can just assign it again and if we print
  696. 25:21these out to the
  697. 25:24screen just like that you'll just see
  698. 25:26the value is true and the value is false
  699. 25:28appear on the screen as of right now in
  700. 25:30the course there's not a whole lot to do
  701. 25:32with boing just understand that they can
  702. 25:33hold a true or false
  703. 25:36value so now that we know how to define
  704. 25:38different variables and how to convert
  705. 25:40between them let's see how we can
  706. 25:42perform operations on different data
  707. 25:44types let's say we could have an INT age
  708. 25:47equals 23 and then what we can do to
  709. 25:49that age is we can say Age ++ and what
  710. 25:53this age will do is it will actually
  711. 25:54just increment age by one and if we do
  712. 25:57age minus minus it will decrement it by
  713. 25:59one so if we just print it out after and
  714. 26:01after this as well what we'll notice is
  715. 26:03there'll be no change because it'll go
  716. 26:05forward one and then back one there we
  717. 26:07go went from 23 plus plus to 24 and then
  718. 26:10minus minus to
  719. 26:1223 that's one of the important operators
  720. 26:15that you'll see a lot especially during
  721. 26:17the next section of this course so
  722. 26:19another way to be able to add one to a
  723. 26:21variable or anything to a variable we
  724. 26:23can replace this with age equals age + 1
  725. 26:28and what we're seeing here we want age
  726. 26:30to have the value of itself added to one
  727. 26:34which will give us the same result as
  728. 26:35doing h++ another way of writing this is
  729. 26:38a quick shorthand is you can say AG plus
  730. 26:41equals 1 these three things are doing
  731. 26:44exactly the same thing when you do an
  732. 26:47age Plus+ you can only increment by one
  733. 26:49and only one these two options give you
  734. 26:52the flexibility of having something like
  735. 26:5410 these two are doing exactly the same
  736. 26:58a AG equal AG + 10 and this is going to
  737. 27:00read exactly the same thing it's is when
  738. 27:02you use plus equals it will take the
  739. 27:04value of the current variable and add it
  740. 27:06to 10 which exactly what this line does
  741. 27:09to make it a lot shorter you can use age
  742. 27:11plus equals and then just put the value
  743. 27:13at the end just in case you forget to
  744. 27:15mention age twice so now if we just get
  745. 27:17rid of these two lines of code and we
  746. 27:19can just have a look and see if this
  747. 27:20runs so now the value should be 33
  748. 27:24perfect and now as we said we're going
  749. 27:26to cover plusus time and divide so just
  750. 27:30as you did with plus equals you can
  751. 27:31actually do a minus equals as well which
  752. 27:34will give us 13 and you can do a times
  753. 27:36equals which should give us
  754. 27:39230 now if we try to do divide equals
  755. 27:42we're going to run into an
  756. 27:43issue it says
  757. 27:462 23 / 10 is
  758. 27:502.3 and when you take 2.3 and make it
  759. 27:53into an integer it's just going to
  760. 27:55truncate the3 and leave you with two
  761. 27:58with integers if you add two integers or
  762. 28:00take away two integers or times them
  763. 28:02you'll always end up with an integer if
  764. 28:05you divide two integers you don't know
  765. 28:07what you'll end with for example if you
  766. 28:09make this into 20 then you'll always end
  767. 28:11up with two but that's not always the
  768. 28:13case as you just saw 23 / 10 is 2.3 so
  769. 28:17if you're ever going to do any division
  770. 28:19it's best to actually make this a double
  771. 28:22cuz now that we do the division we
  772. 28:23actually get 2.3 which is technically
  773. 28:25the right value so you can add minus
  774. 28:28times and divide any variables that you
  775. 28:30want these are just demonstrated in int
  776. 28:32and double but you can do this the Flor
  777. 28:34the decimal Etc so let's have a look and
  778. 28:36see what the plus and the minus
  779. 28:37operators do when you've got a string
  780. 28:39variable so we can have a string name
  781. 28:41equals
  782. 28:43abber and then we can say the name plus
  783. 28:48equals is
  784. 28:51programming and then we can just print
  785. 28:53out name to the screen and see what
  786. 28:54happens there you go you've actually
  787. 28:56just joined two strings together they're
  788. 28:58completely separate from each other and
  789. 28:59we've made them string
  790. 29:03together so if we try to do a minus
  791. 29:05equals you can't really remove from a
  792. 29:07string because a string is just text
  793. 29:09it'll be very difficult for the compiler
  794. 29:11and for the C language to be able to
  795. 29:13determine what's currently in the string
  796. 29:15and whether you can remove it or not
  797. 29:17this is not something that's built into
  798. 29:18the language this is not something
  799. 29:20that's built into the language so let's
  800. 29:22remove that and we can have the same
  801. 29:24thing with the
  802. 29:26CH now the problem with the ch
  803. 29:28is if we add a to B then let's see what
  804. 29:31happens to the
  805. 29:36output what it will try and do is
  806. 29:38actually get the uni code values for
  807. 29:40both of these items and try and add them
  808. 29:43together as integer
  809. 29:45variables so when you add a and b it
  810. 29:48will take the unic code value and you
  811. 29:49will actually get a capital A with a
  812. 29:51squiggle at the top and if we open
  813. 29:53character map you can see that this
  814. 29:54squiggle at the top is right here a
  815. 29:57which is unic cord 61 and 61 actually
  816. 30:00means 97 because it's 6 * by 16 + the 1
  817. 30:05and b means it's 6 * 16 which is 96 +
  818. 30:08the 2 so you have 97 and 98 and if you
  819. 30:12add them two values together you'll get
  820. 30:14the hex code which is C3 just for this
  821. 30:17character so what you're doing is you're
  822. 30:18kind of adding them both together and
  823. 30:20you're producing weird results so unless
  824. 30:23you really want to do this then you
  825. 30:24should be worryed about adding two
  826. 30:26characters together one more thing to
  827. 30:28note about when you're doing Plus+ let's
  828. 30:31bring another int variable and we just
  829. 30:33say int I equals z then we can do I ++
  830. 30:37and then if we print out I the value
  831. 30:39should be one which is fine what if we
  832. 30:42did
  833. 30:44i++ but we did Plus+ I so the value is
  834. 30:47still going to be one but what happens
  835. 30:49if we put this I ++ straight into these
  836. 30:52brackets let's try and run it and you'll
  837. 30:55still get the value of zero that's
  838. 30:57strange because we've said I ++ and what
  839. 31:00happens when you do Plus+ at the front
  840. 31:03it will actually execute this and return
  841. 31:05the value of I and then do the Plus+ so
  842. 31:08if you actually print the value of I
  843. 31:10after you'll realize that now it becomes
  844. 31:12one after if you ever had a situation
  845. 31:15where you need to increment first and
  846. 31:17then use the value you can use plus plus
  847. 31:20I now if you run it you should see the
  848. 31:22value is one twice the difference is
  849. 31:25that as the plus plus is before the
  850. 31:27variable name it tells the compiler
  851. 31:29please do this action first and then
  852. 31:31return back the I if you don't want that
  853. 31:33behavior then use I
  854. 31:37++ finding the remainder between the
  855. 31:39division of two integer variables can be
  856. 31:41very handy when you're determining
  857. 31:43whether a number is even or odd let's
  858. 31:45look at an example let's have it in
  859. 31:48first Nome design the value of 10 and in
  860. 31:51second Nome and give it the value of
  861. 31:52three now if we just printed the screen
  862. 31:55first num divided by second n what do
  863. 31:57you think you'll get so the value of
  864. 31:59this in the calculator will be 3.3
  865. 32:01reoccurring so we'd actually just get
  866. 32:03three because it's truncating it because
  867. 32:05of the
  868. 32:08integer so now what happens if we need
  869. 32:10the remainder between both of these you
  870. 32:12could actually work this out by seeing
  871. 32:14how many times three would be divided by
  872. 32:1610 as close as possible and you see the
  873. 32:19value is three and then 3 * by 3 is 9
  874. 32:23which will give you one left over for
  875. 32:24the 10 but that's a bit too long in our
  876. 32:27code so we have something called a
  877. 32:28modulus operator so we can actually
  878. 32:31change this divide for a percentage sign
  879. 32:34and what this will output is it will
  880. 32:35only give us the remainder so let's say
  881. 32:3810 / 3 is
  882. 32:423.3 as we've just said but the remainder
  883. 32:45is different remainder is exactly how
  884. 32:46you learned it at
  885. 32:48school 10 / 3 is actually 3 remainder
  886. 32:52one because 3 * 3 is 9 and then you have
  887. 32:56one left over to get 10 so that'll be 3
  888. 32:59* 3 which is equal to 9 and then one
  889. 33:02left over to get to 10 so now let's run
  890. 33:06our code and see if we do actually get
  891. 33:07one there we go so the best way to
  892. 33:11determine if a value is actually odd or
  893. 33:13even is to divide it by two so we can
  894. 33:17say 10 modulus 2 and if the value is
  895. 33:21zero then that means the value for first
  896. 33:23nor is actually even if the value is one
  897. 33:27then it's OD let's see how this works
  898. 33:29out if we had 10 modulus 2 then the
  899. 33:32value is zero because 10 can be
  900. 33:34divisible by two perfectly let's say we
  901. 33:37had 11 modulus 2 the closest multiple to
  902. 33:4111 is 10 which is 5 and then we have one
  903. 33:44left over so you just have the remainder
  904. 33:47is equal to one and if we just keep
  905. 33:50going up in the chain we'll realize that
  906. 33:52it's the same pattern every single time
  907. 33:54you go up a value the value just toggles
  908. 33:59so when we print this out to the console
  909. 34:01and the value is actually zero that
  910. 34:04means the number is even and if we make
  911. 34:06the number odd on purpose and then rerun
  912. 34:08the code you can see that it's a number
  913. 34:10one now which means this value is now
  914. 34:12odd let's look at a couple examples and
  915. 34:14see if you can figure out the answer if
  916. 34:17we do a th000 modulus 90 what do you
  917. 34:20think the value will be and let's write
  918. 34:22a couple more if we do 100 modulus 90
  919. 34:26and maybe something like 71 1 modulus 10
  920. 34:29so have a think about this in your head
  921. 34:31and see if you can figure out the
  922. 34:32answers pause the video and see if you
  923. 34:34are
  924. 34:35correct let's take a look at the
  925. 34:38answers so we can see we have 10 10 and
  926. 34:411 so the closest multiple to 1,000 from
  927. 34:4490 is
  928. 34:45990 so we have 10 left over to get to
  929. 34:491,000 and the same goes here the closest
  930. 34:51multiple of 100 to 90 is just indeed 90
  931. 34:55so we only have 10 to get from 90 to 100
  932. 34:58and in this case the closest multiple of
  933. 35:0071 to 10 is 70 and we have one to get up
  934. 35:04to 71 therefore these two answers are
  935. 35:06indeed
  936. 35:08correct so now that we know how to
  937. 35:10really Define a variable by specifying
  938. 35:12their type follow by the name and giving
  939. 35:14it a value let's see how we can do a
  940. 35:16very easy way of defining a variable so
  941. 35:19normally we would type in Edge what we
  942. 35:21can use this VAR keyword what this means
  943. 35:24is the compiler will figure out what
  944. 35:26type it needs to be based on the value
  945. 35:27we initialize it with so if we just make
  946. 35:30a variable called age then it's not
  947. 35:32going to like it because you need to
  948. 35:33give it a value this doesn't give the
  949. 35:35compiler enough information to figure
  950. 35:37out what this variable is going to be
  951. 35:39just because it says age this is just
  952. 35:41text to the compiler this does not mean
  953. 35:43anything so if you make it equal to 23
  954. 35:46then the compiler can see that this is
  955. 35:48indeed in 32 because it knows that so
  956. 35:51now if you hover over a VAR it says in
  957. 35:5432 now let's try and do this for the big
  958. 35:57number
  959. 35:59our big number equals
  960. 36:0290,000 and what will happen now is
  961. 36:05because it's Define as in32 it's going
  962. 36:07to say this is in32 as well and this is
  963. 36:10the same issue we had we'll move first
  964. 36:11defining the long variable in the first
  965. 36:13video we need to explicitly give it an L
  966. 36:16just so this knows that it's N64 and now
  967. 36:19the VAR has updated to an
  968. 36:21N64 instead of rewriting each one we can
  969. 36:24just change each of v and inspect what
  970. 36:26happens double
  971. 36:28double and now we have a float single
  972. 36:32single and finally the decimal we have a
  973. 36:35decimal and we have a decimal and the
  974. 36:37same thing goes for a string we can say
  975. 36:39VAR name equals ABA and we can have a
  976. 36:42VAR letter equals a there we go and
  977. 36:46they'll both register as there pickable
  978. 36:47data types as a string and as a Char and
  979. 36:52if we just print this out to the screen
  980. 36:53to make sure everything's working fine
  981. 36:56and run the code you can see that
  982. 36:57everything is exactly how we'd expect so
  983. 37:00just a quick recap you don't have to do
  984. 37:02this at the end of the day this will be
  985. 37:03exactly the same as writing this because
  986. 37:06when our code gets compiled down it
  987. 37:07doesn't matter what these things are cuz
  988. 37:09the compiler will deal with it however
  989. 37:11way it wants to this is just for us to
  990. 37:13read it in some cases if the value is
  991. 37:15very obvious like as we know an age
  992. 37:18cannot really be a point 23.1 years old
  993. 37:22no one says that in real life when you
  994. 37:23get asked your age so we can just assume
  995. 37:25that this value is going to be in an
  996. 37:27integer and that's fine because that's
  997. 37:29an easy number that's an easy example
  998. 37:32but if we say we want a negative or a
  999. 37:34big number then it doesn't really tell
  1000. 37:36us what we're meant to be like this big
  1001. 37:38number could be somewhere up to 2.1
  1002. 37:40billion which is what an integer
  1003. 37:42supports but in this case if it's going
  1004. 37:44to be a big number and we specified the
  1005. 37:46L then we're going to need a really big
  1006. 37:48number bigger than 2.1 billion so in my
  1007. 37:51honest opinion it' be very beneficial to
  1008. 37:53only use VAR if it's very obvious what
  1009. 37:56the variable is going to be and want
  1010. 37:58it's being assigned to using VAR too
  1011. 38:00much might hurt the readability of your
  1012. 38:02code in the future because you have to
  1013. 38:04inspect each variable individually to
  1014. 38:06read what the type is going to be if
  1015. 38:08you're scrolling through your code and
  1016. 38:09you can see that this is along very
  1017. 38:11quickly and you don't have to hover over
  1018. 38:13it then it can improve your read time
  1019. 38:15this is really important as readability
  1020. 38:17of code will make more sense when it
  1021. 38:18comes to Big projects when you have 20
  1022. 38:2030 lines just like this project then it
  1023. 38:22makes almost no difference cuz it only
  1024. 38:24takes a couple of seconds when you have
  1025. 38:26big functions big software when you work
  1026. 38:29at a job then it's going to be very
  1027. 38:31difficult to be able to find your way
  1028. 38:32around and to understand what's going on
  1029. 38:34especially if you're new to the code
  1030. 38:37something very simple like this where
  1031. 38:38you know you're defining a name it's
  1032. 38:40very obvious and VAR will be accepted
  1033. 38:43fine because you can tell that this is
  1034. 38:44always going to be a string there's no
  1035. 38:46other data type in C get specified by a
  1036. 38:49speech mark whereas when you're defining
  1037. 38:51numbers it can be very very hard to
  1038. 38:53understand what different data types
  1039. 38:55that people are using throughout the
  1040. 38:57code especially when these numbers are
  1041. 38:59not of statically typed in they might be
  1042. 39:01inputs from the console from the user
  1043. 39:03from a different function from anywhere
  1044. 39:05so having good variable names along with
  1045. 39:08their explicit types will be very handy
  1046. 39:10to understand code in the
  1047. 39:13future so now that you've understood how
  1048. 39:15to make a variable let's look at a
  1049. 39:17concept called constants so we defined a
  1050. 39:19variable called vat and we set it to 20
  1051. 39:22just to represent the 20% V in the UK
  1052. 39:25now later on in the program we can
  1053. 39:27actually change it to anything we want
  1054. 39:29and that's because it's a variable
  1055. 39:30variables can change in certain
  1056. 39:32circumstances like vat you might not
  1057. 39:34want to change it and you want to
  1058. 39:35disable the ability to change it to the
  1059. 39:37code if it's done accidentally let's say
  1060. 39:39for example you're making a banking
  1061. 39:41application and you wanted to fix the V
  1062. 39:43and you don't want anyone else to change
  1063. 39:45it you can add this keyword called const
  1064. 39:48right before the Declaration and now
  1065. 39:49when you go to reassign it you'll get an
  1066. 39:51error message and it says at the left
  1067. 39:53hand side of the assignment so anything
  1068. 39:55before the equal sign has to be a very
  1069. 39:57Able Property or indexer and in this
  1070. 39:59case if you hover over vat it states
  1071. 40:02that it's a local constant local because
  1072. 40:04there's no access modifier before it and
  1073. 40:06constant because we specified the con
  1074. 40:08keyword so now if we try and run the
  1075. 40:10code we'll actually get a compile error
  1076. 40:12because the vat cannot be changed so if
  1077. 40:15we remove that and then we just print it
  1078. 40:17out to the console using CW tab tab and
  1079. 40:19then we can put vat in the brackets and
  1080. 40:21press F5 and now we can see our 20 being
  1081. 40:24printed
  1082. 40:27so let's try and calculate some values
  1083. 40:29using this code if you have an INT
  1084. 40:32balance make it equal to 1,000 and let's
  1085. 40:35say we wanted to calculate the vat based
  1086. 40:37off this then you'd have to do a little
  1087. 40:39bit of a complicated calculation you
  1088. 40:41have to take the balance and because the
  1089. 40:42vat is a percentage we need to convert
  1090. 40:44it from decimal to percentage so if we
  1091. 40:47do times and then open the parentheses
  1092. 40:49we can insert
  1093. 40:51vat divided by 100 and just to make sure
  1094. 40:55that this gets process as a double we
  1095. 40:57want write 100 D cuz right now it's in
  1096. 40:5932 and if we leave this as an integer
  1097. 41:01division then we'll lose any decimal
  1098. 41:03places and decimal places are very
  1099. 41:05important when it comes to percentages
  1100. 41:07so we put a 100 D at the end now you can
  1101. 41:09see it says double so it'll actually
  1102. 41:11calculate as if it was a decimal number
  1103. 41:13so now if we print that to the screen so
  1104. 41:15we can see here that 20% of the balance
  1105. 41:18is indeed 200 instead of having to do
  1106. 41:21this calculation each time what we can
  1107. 41:23actually do is make another const this
  1108. 41:26time making a double because we're not
  1109. 41:27sure what the output of this division
  1110. 41:29will be and we don't want to truncate it
  1111. 41:31so we want it as a double and we can say
  1112. 41:33percent vat and what we can do here when
  1113. 41:36we assign the value just because it's a
  1114. 41:38constant it means that we can't change
  1115. 41:40the value but that also means we can
  1116. 41:42read it as many times as we want so we
  1117. 41:44can say the percentage V is actually the
  1118. 41:47current V that we've got specified here
  1119. 41:50divided by
  1120. 41:52100d this will give us the vat from a
  1121. 41:55number form into a decimal form form so
  1122. 41:58now when we do this calculation if we
  1123. 42:00duplicate this line using contrl D we
  1124. 42:02can remove these entire brackets and
  1125. 42:03literally replace it with percentage vat
  1126. 42:06and you should see we get the same
  1127. 42:07values
  1128. 42:09perfect this can be very helpful in the
  1129. 42:11future if you have things like version
  1130. 42:13numbers in your code you can have
  1131. 42:15version 1.0 and then if you ever specify
  1132. 42:18this version later on in your code you
  1133. 42:20can just change it once up and it will
  1134. 42:22change the rest this can be very helpful
  1135. 42:25you'll avoid a lot of typos and you
  1136. 42:26won't have to to redeclare this value
  1137. 42:28several times if you had three different
  1138. 42:30occasions where you displaying the
  1139. 42:32version number in your code then it's
  1140. 42:33going to be problematic once you update
  1141. 42:35the version number and you have to
  1142. 42:36change every single occurrence and you
  1143. 42:38might miss them so inserting it once and
  1144. 42:40only having to change it once is the
  1145. 42:42preferred option just to note with
  1146. 42:44constants when you compile your code all
  1147. 42:46the constants will actually do is do a
  1148. 42:49find and replace so it will take this
  1149. 42:50vat and find all references of v and
  1150. 42:54just drop the 20 in
  1151. 42:56there so this is more of a feature for
  1152. 42:58maintaining your code from a user and
  1153. 43:00programmer's perspective rather than
  1154. 43:02anything to do with the compiler because
  1155. 43:04when it compiles it just removes all the
  1156. 43:06constant anywhere and then you're left
  1157. 43:07over with the literal values that we've
  1158. 43:09gave it nonetheless this is still
  1159. 43:11something very handy to learn and to use
  1160. 43:13throughout your
  1161. 43:15code first you want to Define a variable
  1162. 43:18to hold your name so we can just say
  1163. 43:20string name equals abber perfect so
  1164. 43:23that's the first one achieved says
  1165. 43:25Define a variable to hold your phone
  1166. 43:27number number so at first this could be
  1167. 43:28quite easy we could say int number
  1168. 43:31equals and then we can start to type our
  1169. 43:33number in here so now that's our number
  1170. 43:36completed let's move on to holding your
  1171. 43:38age and we can just have an INT age
  1172. 43:40equals
  1173. 43:4123 print variables line by line to the
  1174. 43:44screen so we can use CW tab tab and type
  1175. 43:46in name and we can use contrl D to copy
  1176. 43:49and paste the line then we can do number
  1177. 43:51crl D again and then we can use ede now
  1178. 43:54if we run the code let's see what
  1179. 43:56happens
  1180. 43:58here we've actually hit a slight problem
  1181. 44:00it says 1 2 3 4 for the number but I'm
  1182. 44:03pretty sure that I started it off with
  1183. 44:05zero this is a problem with storing your
  1184. 44:07number as an integer as a number zeros
  1185. 44:10at the front don't really matter if we
  1186. 44:12had a 100 zeros here then it's not going
  1187. 44:15to change the actual value the only time
  1188. 44:17zeros matter is if it's throughout the
  1189. 44:19number or towards the end that actually
  1190. 44:21changes the number but if we have lots
  1191. 44:23of zeros at the start then it doesn't
  1192. 44:25change anything so what you actually
  1193. 44:27want to do in order to store a number is
  1194. 44:29you want to store it as a
  1195. 44:31string therefore when you run the code
  1196. 44:33it will actually rain the zero that we
  1197. 44:35typed in so now that we've accomplished
  1198. 44:37all of these let's see how we can Define
  1199. 44:39the variables using the VAR keyword so
  1200. 44:41if we hover over these you can see that
  1201. 44:43it's picking up that it's a string and a
  1202. 44:45string here and finally this is an
  1203. 44:48integer so what we can do very simply is
  1204. 44:51just change these to VAR and then let's
  1205. 44:53hover over the variables and just see if
  1206. 44:55the tool tip reads the correct data type
  1207. 44:57string string and integer and if you run
  1208. 45:01the code you should see no
  1209. 45:05difference so in this exercise I want
  1210. 45:07you to create and initialize two integer
  1211. 45:10variables make a variable to work out
  1212. 45:12the remainder output the remainder to
  1213. 45:14the screen and then change the first
  1214. 45:16integer variable to another number
  1215. 45:18recalculate the remainder and output the
  1216. 45:20new remainder to the screen so let's
  1217. 45:22take this step by step so first we need
  1218. 45:25to create an initialize two ins let's
  1219. 45:27say int num 1 equals to 10 int num 2
  1220. 45:31equals to two make a variable to work
  1221. 45:34out the remainder so we can have an INT
  1222. 45:36remainder equals to and if you remember
  1223. 45:39how to do the remainder it uses the
  1224. 45:41modulus symbol which is the percentage
  1225. 45:43sign so we can say num one percentage
  1226. 45:46num
  1227. 45:48two now it says output remainder to the
  1228. 45:50screen so we can use CW tubtub add
  1229. 45:53remainder in and hit run and just for
  1230. 45:56now we can see that the remainder is
  1231. 45:58indeed zero if the remainder is zero
  1232. 46:01then that means 10 is even because 10
  1233. 46:03modulus 2 gives a zero and if it's zero
  1234. 46:06then that means it's even if the
  1235. 46:08remainder was one then it would mean
  1236. 46:11it's odd so if we try 11 remainer two we
  1237. 46:13get one so 11 is indeed an odd number so
  1238. 46:17let's change this back to 10 and then it
  1239. 46:19says change the first into variable to
  1240. 46:21another number so just after here we
  1241. 46:23could say num one equals 11 and then
  1242. 46:27recalculate the remainder so we
  1243. 46:29essentially just need to do this section
  1244. 46:31again and we don't need to write in
  1245. 46:33before these two variables cuz int is
  1246. 46:35only use when you're declaring it this
  1247. 46:37is reassigning the
  1248. 46:39variable so we'll reassign variable num
  1249. 46:42one to 11 and then we reassign the
  1250. 46:45remainder to the calculation again and
  1251. 46:47now we can use the exact same line of
  1252. 46:48code above to print out the remainder to
  1253. 46:51the screen now you can see that it's 0er
  1254. 46:53and one it's zero because 10 is even and
  1255. 46:56it's one because 11 is
  1256. 46:59odd so far throughout the course we've
  1257. 47:02been using console right line to Output
  1258. 47:04stuff to the screen for example I can
  1259. 47:06output hello my name is Aba and we can
  1260. 47:08see it in the console the issue is so
  1261. 47:11far we haven't really brought any code
  1262. 47:13to be able to read in values from the
  1263. 47:14console so let's explore that option
  1264. 47:17right now so in the console Library we
  1265. 47:19have a console right line but what you
  1266. 47:21don't realize is we can also use this
  1267. 47:23console right line to ask the user a
  1268. 47:25question and to let the user type
  1269. 47:27something in so let's use CW tab tab and
  1270. 47:31say enter your
  1271. 47:33name with a callon and then end the
  1272. 47:36quotation
  1273. 47:37marks and then in order to return a
  1274. 47:40value from the console into your program
  1275. 47:42we need to use console.readline and if
  1276. 47:45you hover over readline let's have a
  1277. 47:47read at description reads the next line
  1278. 47:49of characters from the standard input
  1279. 47:51stream perfect this is exactly what we
  1280. 47:53want to do right now it's use at the end
  1281. 47:56of our code code just to stop the
  1282. 47:58console from closing so we can actually
  1283. 47:59see the output so if we just run this
  1284. 48:02right now we actually get the
  1285. 48:03opportunity to type but when we press
  1286. 48:05enter our program closes this is because
  1287. 48:07in this case we're simply using readline
  1288. 48:10just to wait until the user has pressed
  1289. 48:12enter so so our program doesn't close
  1290. 48:15but what you don't actually see is this
  1291. 48:17is returning as a string which means
  1292. 48:19that anything that we type into here and
  1293. 48:21when we press enter it actually gets a
  1294. 48:23return back to the code but we're not
  1295. 48:25doing anything with it because we don't
  1296. 48:27care at this point so now let's actually
  1297. 48:30do something that will make us care
  1298. 48:32about the user's input so we can make a
  1299. 48:34new string variable and assign it to
  1300. 48:36console readline and it should give us
  1301. 48:38the ability to store Whatever Gets
  1302. 48:40entered inside the console so we can
  1303. 48:43just do string name and we can end the
  1304. 48:45semicolon here and if you wanted to on
  1305. 48:47the next line you could do name equals
  1306. 48:49console do
  1307. 48:52readline or if you wanted to have it all
  1308. 48:54in one line then you can get rid of the
  1309. 48:55semicolon and bring it up like this this
  1310. 48:58is the preferred method as both lines
  1311. 49:00were on top of each other and it wasn't
  1312. 49:01really necessary to split them here
  1313. 49:03we're doing a declaration of string name
  1314. 49:06and an initialization and given it the
  1315. 49:08value of console. readline and this is
  1316. 49:10whatever value the user is going to type
  1317. 49:12in let's run our code and see what
  1318. 49:15happens enter your name abber now when I
  1319. 49:18press enter it successfully started ABA
  1320. 49:21inside this string name variable and now
  1321. 49:23it's waiting at this line the console
  1322. 49:25read line just just right here so when I
  1323. 49:28press enter the program closes so now
  1324. 49:31that we've made a variable and not done
  1325. 49:32anything with it let's print out to the
  1326. 49:34screen to make sure that something's
  1327. 49:35actually worked so if we use CW tab tab
  1328. 49:38and we type in name in the brackets and
  1329. 49:40we can run this code again and let's see
  1330. 49:42what happens as soon as I type in abber
  1331. 49:44and press enter then this second abber
  1332. 49:47is actually the variable printing out to
  1333. 49:48the screen so here it says enter your
  1334. 49:51name with a colon and normally when you
  1335. 49:53see the colon it sometimes lets you type
  1336. 49:55on the same line so if you'd prefer the
  1337. 49:57user to type on the same line then we
  1338. 49:59actually have another function for that
  1339. 50:01instead of using right line we can just
  1340. 50:04use right and the difference between
  1341. 50:06right line and right is the right line
  1342. 50:09will actually have a Terminator at the
  1343. 50:11end of the line to tell the console that
  1344. 50:13he wants to push everything after it
  1345. 50:15onto the next line whereas console.
  1346. 50:17right will just print it out to the
  1347. 50:19screen and not worry about any new lines
  1348. 50:22let's try run it now and now we can type
  1349. 50:25in abber as you can see it's a little
  1350. 50:27close to the colon so we can fix that in
  1351. 50:29a second so you press enter you can see
  1352. 50:31now that comes up on the second line
  1353. 50:34so we just put a space after the callon
  1354. 50:36just to give it a bit of room now if you
  1355. 50:38type in Abba you can see that there's a
  1356. 50:40bit of a gap and it's quite nicer to
  1357. 50:42look at now if we press enter you can
  1358. 50:44see that AB has been stored
  1359. 50:47successfully so what we can do now is
  1360. 50:49maybe ask for your age so we can have a
  1361. 50:52console.
  1362. 50:53right enter your age call on space and
  1363. 50:58then do a string age equals console.
  1364. 51:00readline and then we can have a age
  1365. 51:03output as well and let's see how that
  1366. 51:05works so we can say abber then say 23
  1367. 51:08and press enter and now we have abber
  1368. 51:10stored within the name variable and have
  1369. 51:1323 stored within the age variable so
  1370. 51:16let's try and output a string that's a
  1371. 51:17bit more meaningful to the user let's
  1372. 51:19clean this console up and see what we
  1373. 51:21can do so we don't need to Output their
  1374. 51:24result back to the console because they
  1375. 51:25literally just type that in and we don't
  1376. 51:28want to have this line because we just
  1377. 51:29use that as an example so if we run the
  1378. 51:31code right now it'll look better but it
  1379. 51:34does look a lot more structured enter
  1380. 51:36your name and then enter your age so now
  1381. 51:39when you press enter the program will
  1382. 51:40just close so let's try and output these
  1383. 51:42two variables in a formatted fashion so
  1384. 51:45as you now know we can use console. WR
  1385. 51:47to print a lot of stuff on one line so
  1386. 51:50we can use console. right and we can say
  1387. 51:53your name is and then add a space and
  1388. 51:56finish that line there and then
  1389. 51:59duplicate it down we can write name
  1390. 52:02duplicate that down and say space and
  1391. 52:05your age is duplicate that one down and
  1392. 52:09then we can type in age and let's see
  1393. 52:11what happens now abber
  1394. 52:1423 there we go so we actually have the
  1395. 52:17name output and the age output all on
  1396. 52:20one line if we all change these to right
  1397. 52:23lines then they're going to be on
  1398. 52:24separate lines and it's not going to
  1399. 52:25look very nice nice so I'm just going to
  1400. 52:27introduce you to a notation called
  1401. 52:29string concatenation this is the notion
  1402. 52:32of being able to concatenate two
  1403. 52:33different strings which just means to
  1404. 52:35add one string onto another as you can
  1405. 52:38see this is taken up four lines of code
  1406. 52:40and it's getting a bit ugly the better
  1407. 52:42and easier way to do this is by
  1408. 52:44containing it all within one line so we
  1409. 52:47can have a console right line and we can
  1410. 52:49take the first section your name is keep
  1411. 52:52the space at the end we can end the
  1412. 52:53speech marks and put a plus and then we
  1413. 52:56can send name so now we've just added
  1414. 52:58these two together if we just comment
  1415. 53:00out the rest of them and run the code
  1416. 53:02and you can see that they should be on
  1417. 53:04one line there you go so we can say your
  1418. 53:07name is Aba and let's just keep changing
  1419. 53:09the plus symbol so we can say and your
  1420. 53:13age is another space and the speech
  1421. 53:16marks and plus the age now we've got all
  1422. 53:19of this contained within one line and it
  1423. 53:21looks a lot better enter your name Abba
  1424. 53:25enter your age 23 and now you can see
  1425. 53:27exactly the same output but reduced to
  1426. 53:29one
  1427. 53:32line as you can see these three lines
  1428. 53:34are all stuck to each other so if you
  1429. 53:36wanted to add a blank line between these
  1430. 53:38two you can just put an MC console right
  1431. 53:41line just in here and what this will do
  1432. 53:43is simply just print the right line
  1433. 53:45character to the screen which will force
  1434. 53:47the console to make it on the next line
  1435. 53:50just like so here's the console right
  1436. 53:52line from the empty line that we just
  1437. 53:53wrote and this is the console right line
  1438. 53:55to Output the Val values so now that
  1439. 53:58you've understood how to read values in
  1440. 53:59from the console let's try and convert
  1441. 54:01some values as I showed you how to do
  1442. 54:04earlier on in the cuse you can actually
  1443. 54:05convert these strings into integers
  1444. 54:07ideally you want to store variables in
  1445. 54:09their data types as we covered before
  1446. 54:11ages should not be stored as strings
  1447. 54:14they should be stored as integers but if
  1448. 54:16you try and change this to an INT then
  1449. 54:17you're going to have an issue console
  1450. 54:19read line will return back a string so
  1451. 54:22therefore it can't implicitly convert
  1452. 54:25from a string to an integer so we need
  1453. 54:27to do this in two steps we can rename
  1454. 54:30this to age input and then on the line
  1455. 54:33underneath it we can say int age equals
  1456. 54:37convert to int 32 and then add age input
  1457. 54:41in these
  1458. 54:42brackets there we go and now when we
  1459. 54:44actually print the line of code nothing
  1460. 54:47should actually
  1461. 54:49change your name is abber and your age
  1462. 54:51is 23 but now what's actually changed is
  1463. 54:54we're storing the variables in the
  1464. 54:56correct format this is the ideal method
  1465. 54:58because in the next section when we
  1466. 54:59cover if statements you'll realize why
  1467. 55:01this is very important if you wanted to
  1468. 55:04minimize the amount of steps you need to
  1469. 55:06convert this then you can actually move
  1470. 55:08the console read line and get rid of the
  1471. 55:10string input variable and we can just
  1472. 55:12paste it straight into the bracket of
  1473. 55:14the converter in 32 and you can get rid
  1474. 55:17of the string input variable and
  1475. 55:19everything will work the same h 23 and
  1476. 55:22now it's working exactly the same if
  1477. 55:24this is too many steps for you or if
  1478. 55:26this looks too complicated then just
  1479. 55:28please revert it back just to keep it
  1480. 55:29the same way for the time
  1481. 55:32being so now we've learned how to read
  1482. 55:35in values from the console and output
  1483. 55:36them back to the screen now let's see
  1484. 55:38how we can use if statements to divert
  1485. 55:40our code into multiple paths so in the
  1486. 55:43previous section I told you that it's
  1487. 55:45very crucial for our code to have this
  1488. 55:47int age and the reason why is age is
  1489. 55:50meant to be stored as a number and not
  1490. 55:52as a string so we read it in first as a
  1491. 55:54string and then convert it to an integer
  1492. 55:56using this function and store it into
  1493. 55:58our age variable this is the reason why
  1494. 56:01so our condition goes as follows we
  1495. 56:04would write the word if and between the
  1496. 56:06brackets this needs to be evaluated to
  1497. 56:08either a true or false value and then we
  1498. 56:10can have the curly braces to tell us
  1499. 56:12what to do if this becomes true so in
  1500. 56:15this case you want to just check if our
  1501. 56:16age input is greater than or equal to 18
  1502. 56:19let's say this was an entrance to a
  1503. 56:21place that only allowed overes to enter
  1504. 56:23so we can do age input which refer es
  1505. 56:26the age input variable and we can use
  1506. 56:29the operators equals greater than
  1507. 56:32greater than or equal to less than or
  1508. 56:34less than or equal to or not equal to so
  1509. 56:36these are all the different options that
  1510. 56:38we have we can say if the age input is
  1511. 56:40equal to
  1512. 56:4218 perfect so now that we've read in the
  1513. 56:45age input we can check if it's 18 let's
  1514. 56:48just process a message in here says you
  1515. 56:50are 18 and let's have a look and see if
  1516. 56:53this works
  1517. 56:5618 now I press enter it says you are
  1518. 57:0018 but this is the problem with storing
  1519. 57:02everything as a string and then trying
  1520. 57:04to use it as if it was a number if I
  1521. 57:07type in 18 followed by a space as you
  1522. 57:10can see there's a blank space here now
  1523. 57:11and press enter you can see that it
  1524. 57:13doesn't come up and the reason why is
  1525. 57:15it's processing it like this with a
  1526. 57:17space at the end in order to avoid
  1527. 57:19situations like this this is why we
  1528. 57:21would convert it to a number and the
  1529. 57:24reason why we're using equals to here is
  1530. 57:26we can't use greater than or equal to
  1531. 57:28because strings can't be greater than or
  1532. 57:29equal to another string they can only
  1533. 57:32ever be equal to or not equal to string
  1534. 57:35is just text so if something equals to
  1535. 57:37something then it's fine the only way
  1536. 57:40that we can use these different
  1537. 57:41operators is if we actually interact
  1538. 57:43with our integer variable now let's see
  1539. 57:45how we can use this so we can say age
  1540. 57:48equals equals 18 and now you can see we
  1541. 57:50actually reference it as a number now
  1542. 57:52let's run the code if I do 18 Follow by
  1543. 57:55space it still works and the reason why
  1544. 57:59is it actually dismisses that space when
  1545. 58:01it's converting into the integer so now
  1546. 58:03a just started as an integer and we can
  1547. 58:05actually manipulate this a bit better so
  1548. 58:07instead of saying you are 18 we can say
  1549. 58:09you are 18 or older and now instead of
  1550. 58:12doing equal to we can have greater than
  1551. 58:15if we have greater than then we're
  1552. 58:16actually dismissing the number 18
  1553. 58:18because if the age is greater than 18 so
  1554. 58:21this will read like this and 18 is not
  1555. 58:23greater than 18 so this will return back
  1556. 58:26false what we actually want to use is
  1557. 58:28greater than or equal to if the age that
  1558. 58:31we've inputed is greater than or equal
  1559. 58:33to 18 then you are 18 hence the equal to
  1560. 58:37are greater than if you're older so now
  1561. 58:40let's try and run this AB 23 perfect you
  1562. 58:44are 18 or older let's change so let's
  1563. 58:47change some other if statements to add
  1564. 58:49some additional conditions so we can say
  1565. 58:51else if and the only reason why it would
  1566. 58:54come into this bracket if this condition
  1567. 58:57returns false it will move down the next
  1568. 59:00condition in the else chain if this was
  1569. 59:02wrote as a separate if statement then
  1570. 59:04this would run completely independently
  1571. 59:06from this one if you're going to have
  1572. 59:08chained if statements you need to make
  1573. 59:10sure that you chain them correctly in
  1574. 59:12this case I want to check if it ages
  1575. 59:15between 18 and 25 and then move on to a
  1576. 59:18different age bracket and these two
  1577. 59:20things are grouped together so we should
  1578. 59:22have this as an else if because with an
  1579. 59:24edge bracket you can only be of one age
  1580. 59:26bracket and not multiple so we can say
  1581. 59:29if age is greater than or equal to 25
  1582. 59:32and we can copy this and we can say you
  1583. 59:34are 25 or older now let's see what
  1584. 59:36happens enter your name abber and let's
  1585. 59:39just say that I am
  1586. 59:4035 it says you're 18 or older but I told
  1587. 59:44it the program I was 35 now the problem
  1588. 59:46we're facing is any value that's over 18
  1589. 59:49will always enter inside here and it
  1590. 59:51will skip over this one completely this
  1591. 59:53is not the behavior that we want so so
  1592. 59:56what we can do in our if statements is
  1593. 59:57we can use the double pipe which
  1594. 59:59represents or or the double Ampersand
  1595. 1:00:01which represents and and we can change
  1596. 1:00:04some conditions in here to make sure
  1597. 1:00:06that you implement some sort of range so
  1598. 1:00:09we can actually have a range between 18
  1599. 1:00:11and 25 so we can say if the age is
  1600. 1:00:14greater than or equal to 18 and the age
  1601. 1:00:18is less than or equal to 25 and we can
  1602. 1:00:21tweak this message you are between 18
  1603. 1:00:25and
  1604. 1:00:2725 and let's just run that for now and
  1605. 1:00:30see what happens abber 25 now it says 18
  1606. 1:00:34and
  1607. 1:00:3525 so now that we've got between 18 and
  1608. 1:00:3825 here we can tweak this to be greater
  1609. 1:00:41than equal to
  1610. 1:00:4426 if you're 25 will enter this
  1611. 1:00:47condition and if you're 26 or older into
  1612. 1:00:50this condition so let's test that now if
  1613. 1:00:52we enter
  1614. 1:00:5335 now it says your 26 older that's
  1615. 1:00:57perfect because now that we've
  1616. 1:00:58implemented range checking it gives us
  1617. 1:01:00the ability to differentiate between
  1618. 1:01:02different age groups more precisely
  1619. 1:01:04let's Show an example of how to use the
  1620. 1:01:06R so if we just have a block comment
  1621. 1:01:09around all of this we can write an if
  1622. 1:01:10statement in here and we can say if the
  1623. 1:01:13age is smaller than zero or the age is
  1624. 1:01:16bigger than 150 then we can output to
  1625. 1:01:19the screen in valid Age and what this is
  1626. 1:01:23saying is if we're less than zero and
  1627. 1:01:26notice that this is less than zero so
  1628. 1:01:28this value will actually start at minus
  1629. 1:01:30one and this value start at 150 is minus
  1630. 1:01:34one or less or over 150 then this will
  1631. 1:01:38happen notice why it's an or one of
  1632. 1:01:41these need to be true in order for this
  1633. 1:01:43to be true when you've got an and both
  1634. 1:01:46sides need to be true in order for this
  1635. 1:01:49condition to invalu it is true so let's
  1636. 1:01:51try our code we can say Abu and we put
  1637. 1:01:54minus one and now it says invalid age
  1638. 1:01:57which is perfect because this is our
  1639. 1:01:58range checking and we can try again with
  1640. 1:02:02151 as it's just outside the range it
  1641. 1:02:05will say invalid age and if we have an
  1642. 1:02:08else on here we can print valid
  1643. 1:02:12age notice there is no condition in here
  1644. 1:02:15if you have an open else like this it
  1645. 1:02:17means If This Were to return false it
  1646. 1:02:20will always do this regardless of any
  1647. 1:02:22condition in this case it's perfectly
  1648. 1:02:25reasonable to do this because we know
  1649. 1:02:27that any values that are underneath zero
  1650. 1:02:30or over 150 is invalid and anything
  1651. 1:02:33between 0 and 150 is indeed valid what
  1652. 1:02:37you can do instead is actually move
  1653. 1:02:40these if statements inside here so
  1654. 1:02:42instead of just printing out valid age
  1655. 1:02:45we can actually check the age as
  1656. 1:02:47well so let's run this now and we can
  1657. 1:02:50say Aba minus one and it will only print
  1658. 1:02:52out invalid age and then move on because
  1659. 1:02:55once it reads the this line of code it
  1660. 1:02:56will actually skip over the L's and
  1661. 1:02:59finish up down here and now if you enter
  1662. 1:03:01a real
  1663. 1:03:03value you are between 18 and 25 so let's
  1664. 1:03:07follow the path it got to here and
  1665. 1:03:09realize that the range check was
  1666. 1:03:10actually correct so jumped out of this
  1667. 1:03:12curly brace and went to the else because
  1668. 1:03:14this had returned false we jump into
  1669. 1:03:16this else and now we start checking if
  1670. 1:03:19ages between 18 and 25 which it was
  1671. 1:03:22because our age variable is 23 then it
  1672. 1:03:24outputed this to the screen
  1673. 1:03:28now if we do the same thing but have 35
  1674. 1:03:30instead then it follows the exact same
  1675. 1:03:32path except towards here this will
  1676. 1:03:35return back false and then it will check
  1677. 1:03:36the next condition if age is over 26
  1678. 1:03:40then we'll return
  1679. 1:03:42this so now let's just make a quick
  1680. 1:03:44multiplication program using if
  1681. 1:03:46statement to see if you can get the
  1682. 1:03:47answer correct so let's just comment
  1683. 1:03:49everything
  1684. 1:03:51out and we'll start the next section
  1685. 1:03:53underneath
  1686. 1:03:56so let's make a console. write and ask
  1687. 1:03:58the user to enter the first
  1688. 1:04:01number and then we can say int number a
  1689. 1:04:05equals convert to in32 and the value
  1690. 1:04:09that we want to convert is a console.
  1691. 1:04:11readline which is whatever the user
  1692. 1:04:13entered from the console again if you're
  1693. 1:04:16unfamiliar with this format you can just
  1694. 1:04:18type it in manually number a input
  1695. 1:04:21equals console read line and then we can
  1696. 1:04:23put the number input in here now if you
  1697. 1:04:25use shift and highlight these lines and
  1698. 1:04:26press contrl D we can duplicate this
  1699. 1:04:28entire section and we can say enter the
  1700. 1:04:31second number change a to B change a to
  1701. 1:04:35B and then finally change a to B perfect
  1702. 1:04:38so now we've got enter the first number
  1703. 1:04:40and enter the second number and we can
  1704. 1:04:42say our
  1705. 1:04:44answer equal to number a multi by number
  1706. 1:04:49B and then we can have a console. WR
  1707. 1:04:54value of we can add to it number A plus
  1708. 1:04:58an X Plus number B and then finally plus
  1709. 1:05:03a callon for the user to type in and
  1710. 1:05:05then we can say string answer input
  1711. 1:05:07equals console. readline and then
  1712. 1:05:11int actual answer equals convert to in32
  1713. 1:05:17and then we can have the input in
  1714. 1:05:19here so now that we've got one number
  1715. 1:05:21from the console the second number from
  1716. 1:05:23the console we're just making a variable
  1717. 1:05:25that will multiply them together and
  1718. 1:05:27store inside answer and then we're just
  1719. 1:05:29going to ask the user what's the value
  1720. 1:05:31of number a times my number B and they
  1721. 1:05:34can input that we store it as a string
  1722. 1:05:36and then convert it to an integer and
  1723. 1:05:38store the answer into actual answer so
  1724. 1:05:41now just after the section we can write
  1725. 1:05:43an if statement that says if the answer
  1726. 1:05:45equals to the actual answer then we can
  1727. 1:05:48say Well
  1728. 1:05:50done and else and the reason why we
  1729. 1:05:53don't need to condition this else
  1730. 1:05:55because with equals to the only opposite
  1731. 1:05:57thing you can have is if it's not equal
  1732. 1:05:59to so if you really wanted to you could
  1733. 1:06:01also write if answer is not equal to
  1734. 1:06:04actual answer then in here you can say
  1735. 1:06:08close but it was wrong you don't
  1736. 1:06:11actually need this this is just for
  1737. 1:06:13learning purposes because this will
  1738. 1:06:15return back false if they're not equal
  1739. 1:06:16to each other so this statement is just
  1740. 1:06:18a given if they're not equal to each
  1741. 1:06:20other then we can just assume that
  1742. 1:06:22they're not equal to each other so we
  1743. 1:06:24don't need this additional statement if
  1744. 1:06:26this condition evaluates to being false
  1745. 1:06:29then that means they're not equal to
  1746. 1:06:30each other we don't need this additional
  1747. 1:06:32check to check if they're not equal to
  1748. 1:06:34each other because that's a given if
  1749. 1:06:36this returns back false so we can just
  1750. 1:06:38leave this as an else let's try run the
  1751. 1:06:42code 10 10 value of 10 * 10 100 now it
  1752. 1:06:47says well done and let's just try that
  1753. 1:06:49again and we can enter a different value
  1754. 1:06:5190 Clause but it was wrong so let's just
  1755. 1:06:55have have a quick
  1756. 1:06:56recap so in the previous section we
  1757. 1:06:59talked about how to input from the
  1758. 1:07:00console and we generated this line of
  1759. 1:07:02code which will output your name and
  1760. 1:07:04your age in one line to the console and
  1761. 1:07:07then we looked at how we can use these
  1762. 1:07:08different operators in our if statements
  1763. 1:07:10and we did some range check in to check
  1764. 1:07:12if the age was between 18 and 25 and it
  1765. 1:07:15would output something to the screen and
  1766. 1:07:18if it was over 26 you'd get a different
  1767. 1:07:20output to the screen and then we took
  1768. 1:07:22that a little bit further and started
  1769. 1:07:24using the r operator
  1770. 1:07:26if the age is smaller than zero or it's
  1771. 1:07:28greater than 150 then it must be invalid
  1772. 1:07:31and if this condition returns false it
  1773. 1:07:34must be between 0 and 150 so we can come
  1774. 1:07:37into this L's bracket and inside here we
  1775. 1:07:39can do our range checkin from the
  1776. 1:07:41previous bit of code we can check the
  1777. 1:07:43ranges between 18 and 25 and print a
  1778. 1:07:46value or if it's over 26 we can print
  1779. 1:07:48another value and you can keep changing
  1780. 1:07:50these if you want you could have 26 to
  1781. 1:07:5240 and 40 to 60 Etc Etc that's up to
  1782. 1:07:57you so this is just a quick little
  1783. 1:07:59program to simulate multiplication
  1784. 1:08:02program we can enter the first number
  1785. 1:08:04using console. WR and then we start it
  1786. 1:08:06as a string and we put that string into
  1787. 1:08:08the converter in32 function to give us
  1788. 1:08:11back an integer variable and we do
  1789. 1:08:14exactly the same thing for the second
  1790. 1:08:15number and then we make a variable to
  1791. 1:08:18store both numbers multiplied by each
  1792. 1:08:20other we ask the user what is the value
  1793. 1:08:22of number a Times by number B and we
  1794. 1:08:25give them an opportunity to type in a
  1795. 1:08:26value we read that value in convert it
  1796. 1:08:29to an integer store it as an into actual
  1797. 1:08:32answer and then in our if statement we
  1798. 1:08:34say if the answer is equal to the actual
  1799. 1:08:36answer then we print well done to the
  1800. 1:08:40screen double equals are used for
  1801. 1:08:42equality and single equals are used for
  1802. 1:08:44assignment don't get them mixed up we
  1803. 1:08:47don't need an else condition in here
  1804. 1:08:49because if this returns true then this
  1805. 1:08:51will execute if this is indeed false
  1806. 1:08:54that they're not equal to each other
  1807. 1:08:55then we always want to do this section
  1808. 1:08:57and we don't need to put an additional
  1809. 1:08:59if statement inside here if you wanted
  1810. 1:09:01to but it's not necessary you can have
  1811. 1:09:03lse if answer is not equal to actual
  1812. 1:09:07answer but this is just extra code and
  1813. 1:09:09it hurts the readability and it should
  1814. 1:09:12be
  1815. 1:09:14avoided if statements are one of the
  1816. 1:09:16methods we can decide different paths in
  1817. 1:09:18our code the next method we can use is
  1818. 1:09:20called switch statements so let's write
  1819. 1:09:22a little program so we can demonstrate
  1820. 1:09:23this so we can just have a console right
  1821. 1:09:26enter date of the week and we can have
  1822. 1:09:29in date equals convert to in32 and we
  1823. 1:09:32can place our console. readline inside
  1824. 1:09:35the brackets so we take whatever the
  1825. 1:09:37user typed into the console and then
  1826. 1:09:39place it inside here and inside here
  1827. 1:09:41we're going to convert whatever they've
  1828. 1:09:42typed in to an integer 32 and star it
  1829. 1:09:45into our D variable so if we just print
  1830. 1:09:47this out to the screen to make sure it's
  1831. 1:09:48working
  1832. 1:09:49fine if we just type in seven we can see
  1833. 1:09:52that seven is returned so it's all
  1834. 1:09:53working so normally what we do is type
  1835. 1:09:56if D equals equals 1 and then we can add
  1836. 1:09:59the Cy braces and use CW tab tab and we
  1837. 1:10:02can print out Munday now if you run the
  1838. 1:10:04code and try use number one then Monday
  1839. 1:10:07works and that's fine and then let's do
  1840. 1:10:09another LS if and we can say day equals
  1841. 1:10:11equals 2 and then we can print
  1842. 1:10:14Tuesday perfect so if we try that
  1843. 1:10:16now there we go so now we have the
  1844. 1:10:18ability to print out Monday and Tuesday
  1845. 1:10:20to the console depending what day the
  1846. 1:10:22week the user enters but since we're
  1847. 1:10:25going to have have seven different paths
  1848. 1:10:26because we got seven days of the week
  1849. 1:10:28then sometimes it might be better and
  1850. 1:10:29cleaner to use a switch statement so
  1851. 1:10:32let's have a look at the syntax so we
  1852. 1:10:33can type in switch and then inside the
  1853. 1:10:35brackets it's what variable we want to
  1854. 1:10:37have a switch on so in this case we want
  1855. 1:10:39to be using the day variable so we can
  1856. 1:10:41add D in the brackets this is not a
  1857. 1:10:43condition in the brackets this is just
  1858. 1:10:44telling us what variable we're going to
  1859. 1:10:46be using for the switch statement so
  1860. 1:10:48let's open the curly braces after and
  1861. 1:10:50inside here instead of having paths like
  1862. 1:10:52this we're going to use cases so this
  1863. 1:10:55can be translated to in the case of
  1864. 1:10:57number one we can do this in the case of
  1865. 1:11:00number two we can do this so let's try
  1866. 1:11:03and see how it works we can type in case
  1867. 1:11:05followed by number one and then add a
  1868. 1:11:07callon and then we can type out our
  1869. 1:11:09Monday in here and then we need to
  1870. 1:11:12follow each case off by finishing the
  1871. 1:11:14section off with a break this break's
  1872. 1:11:16very essential and you'll get a syntax
  1873. 1:11:18error if you don't put it in so now if
  1874. 1:11:20we do case two and we do exactly the
  1875. 1:11:22same thing there we go and if I dis miss
  1876. 1:11:25the break then you'll realize that it
  1877. 1:11:27says it cannot fall out so we have to
  1878. 1:11:29use a break just the compiler knows that
  1879. 1:11:31we want to break here the reason why
  1880. 1:11:33this break is placed inside here is
  1881. 1:11:35because what we can do is we can say
  1882. 1:11:38case two and then add another one in
  1883. 1:11:41here called case 3 and what this now
  1884. 1:11:43reads is if we enter two it says choose
  1885. 1:11:46there and if we enter three It also says
  1886. 1:11:49choose it so what you can do is you can
  1887. 1:11:52actually chain so this would be
  1888. 1:11:53equivalent to writing if D equals 2 or D
  1889. 1:11:57equals equals 3 now this is exact
  1890. 1:12:00translation these two are exactly the
  1891. 1:12:02same as of right now we have D equal
  1892. 1:12:04equal 1 which is our K1 and then equals
  1893. 1:12:072 Ral 3 is right here if we go into the
  1894. 1:12:11case 2 path it will fall in the case 3
  1895. 1:12:13so we're saying for this break is we
  1896. 1:12:15want to end this section here so now if
  1897. 1:12:17we just revert this back and continue
  1898. 1:12:19the
  1899. 1:12:20chain I'll just skip this part just so
  1900. 1:12:22it's a little bit quicker
  1901. 1:12:26there we go so now that we've got all
  1902. 1:12:27seven days of the week let's just see if
  1903. 1:12:29it works let's try six and we have
  1904. 1:12:31Saturday so now we're kind of missing
  1905. 1:12:33something from here the only thing we're
  1906. 1:12:35missing is having our external lse just
  1907. 1:12:38in case the user types something invalid
  1908. 1:12:40so normally we would write something
  1909. 1:12:41like this enter value between 1 and 7
  1910. 1:12:46just to make sure that the user knows
  1911. 1:12:47what they're
  1912. 1:12:48doing now we don't seem to have anything
  1913. 1:12:51inside here that gives us the
  1914. 1:12:52opportunity to invalid so we have this
  1915. 1:12:55keyword called default and it works
  1916. 1:12:57exactly the same as the case except what
  1917. 1:12:59this will do is this is basically the
  1918. 1:13:01word that replaces else so we can
  1919. 1:13:03literally take this line of code and
  1920. 1:13:05place it again over here and add
  1921. 1:13:07ourselves a break this is exactly the
  1922. 1:13:09same as doing our L statement inside
  1923. 1:13:11here if none of these cases match this
  1924. 1:13:14variable then it'll go into the default
  1925. 1:13:16and Contin as normal so if we type in
  1926. 1:13:18zero we should see the message twice
  1927. 1:13:20because it's executing for the if
  1928. 1:13:21statement and executing down here and
  1929. 1:13:24let's do eight and you see the message
  1930. 1:13:26again so now that the case statement is
  1931. 1:13:28fully working let's just comment out the
  1932. 1:13:29if statement and make sure that it still
  1933. 1:13:31works one Monday let's just try another
  1934. 1:13:34one and we can do seven for Sunday so
  1935. 1:13:38just a quick recap case statements are
  1936. 1:13:39used by using the keyword switch
  1937. 1:13:41followed by the variable in question and
  1938. 1:13:43then if you want to check if this
  1939. 1:13:44variable number is equal to something
  1940. 1:13:46you put that something straight after
  1941. 1:13:48the word case followed by a colon and
  1942. 1:13:50then you can insert multiple lines of
  1943. 1:13:52Corde inside here and make sure you end
  1944. 1:13:54that line with a break if you want to
  1945. 1:13:56chain multiple like Dayal 2 or Dayal 3
  1946. 1:14:00then you can add the case above right
  1947. 1:14:02here so the case of zero or the case of
  1948. 1:14:05two it will do this little section so
  1949. 1:14:08it'll print out Tuesday if the user
  1950. 1:14:09enters zero or the user enters two of
  1951. 1:14:12course in this case we don't actually
  1952. 1:14:13want that after you filled out all of
  1953. 1:14:16the code make sure you add something
  1954. 1:14:18called the default the default is
  1955. 1:14:19equivalent to an lse keyword in our if
  1956. 1:14:21statement just to catch all the values
  1957. 1:14:23in case the user type something
  1958. 1:14:25incorrect and you want to let them know
  1959. 1:14:27what they should be typing
  1960. 1:14:30instead so now that we explored
  1961. 1:14:32different paths in our code using IF
  1962. 1:14:33statements and switch statements let's
  1963. 1:14:35have a look at how we can iterate
  1964. 1:14:36through our code an iteration in code is
  1965. 1:14:39just the ability to be able to Loop and
  1966. 1:14:41do a certain task so let's say we wanted
  1967. 1:14:43to print high to the console several
  1968. 1:14:45times then we just have to copy and
  1969. 1:14:47paste it down and this will be five
  1970. 1:14:49separate lines that do exactly the same
  1971. 1:14:51thing this will essentially achieve what
  1972. 1:14:54we want the problem is you just have
  1973. 1:14:56exactly the same code copy and pasted so
  1974. 1:14:59what we can do if we just block comment
  1975. 1:15:01this line for the time being we can
  1976. 1:15:03write something called a for Loop a for
  1977. 1:15:05Loop gives us the ability to Loop a
  1978. 1:15:07certain amount of times depending on the
  1979. 1:15:08parameters that we give it so this is
  1980. 1:15:10the syntax you can type for open the
  1981. 1:15:13bracket and we have to use a variable so
  1982. 1:15:16we can declare a new variable straight
  1983. 1:15:17into these brackets we can say inti
  1984. 1:15:19equals z and then have a semicolon and
  1985. 1:15:22there's three different sections to this
  1986. 1:15:24the first one is the variable decoration
  1987. 1:15:26and initialization and the second one is
  1988. 1:15:28the condition so in this case we want to
  1989. 1:15:30do this task five times so you want to
  1990. 1:15:33say I is less than five then we end that
  1991. 1:15:35semicolon there and now for the third
  1992. 1:15:37task every time this for Loop goes
  1993. 1:15:39around we want to increase I by one
  1994. 1:15:42because we want to go in steps of one so
  1995. 1:15:44we can do
  1996. 1:15:45i++ alternativ if you don't like using
  1997. 1:15:48Plus+ you can say I = I + 1 or to make
  1998. 1:15:52that quicker you could say I + equal 1 I
  1999. 1:15:55personally just prefer using Plus+ and
  2000. 1:15:57you'll see that everywhere else there is
  2001. 1:15:59certain cases where you would actually
  2002. 1:16:01want to change this to a different value
  2003. 1:16:03like I +al 2 and we'll show you that in
  2004. 1:16:05a second so this for Loop now says we
  2005. 1:16:08want to go from I equals 0 and once this
  2006. 1:16:11condition returns back false when I is
  2007. 1:16:13equal to 5 or greater then we want to
  2008. 1:16:15stop this for Loop every time this for
  2009. 1:16:17Loop goes around so in hits this curly
  2010. 1:16:19brace and comes back around we want to
  2011. 1:16:21do this action and in this case it's I
  2012. 1:16:24++ so if we just use console right line
  2013. 1:16:26and print out the value I we can see
  2014. 1:16:28what happens when the follow
  2015. 1:16:31runs you can see that the value of I is
  2016. 1:16:34increasing one each time and after the
  2017. 1:16:36fifth time is increased the value is
  2018. 1:16:39actually five so now this statement is
  2019. 1:16:41false because the value of I is equal to
  2020. 1:16:44five so therefore this is now false and
  2021. 1:16:46the for Loop is ended so now that we've
  2022. 1:16:49verified that you can repeat it five
  2023. 1:16:51times using this code we no longer want
  2024. 1:16:53to print I we want to print our high so
  2025. 1:16:55let's paste that inside the bracket and
  2026. 1:16:57run the code now you can see that these
  2027. 1:17:00two values will be exactly the same as
  2028. 1:17:02printing it out to the console just like
  2029. 1:17:03this but now we have a lot less code so
  2030. 1:17:07let's have a look at another example we
  2031. 1:17:08can say 4 in I equal 0 and we can say I
  2032. 1:17:13is less than or equal to 10 and then we
  2033. 1:17:16can do I + = 2 and then if we just print
  2034. 1:17:19I to the console so what this is now
  2035. 1:17:22saying is I want to go from zero all way
  2036. 1:17:25till 10 and I want to go up two times
  2037. 1:17:28each time so what this will be achieving
  2038. 1:17:30is printing out all of the even numbers
  2039. 1:17:32between 0 and
  2040. 1:17:3310 let's try and run
  2041. 1:17:36it now we can see we have all the even
  2042. 1:17:38numbers printed out between 0 and 10
  2043. 1:17:41because we're going up in
  2044. 1:17:42twos if we start this at one for example
  2045. 1:17:45and still keep every parameter exactly
  2046. 1:17:47the same then we'll be getting all of
  2047. 1:17:49the even numbers between 1 and 10 11 is
  2048. 1:17:52missing because our condition stops at
  2049. 1:17:54less than or equ equal to 10 what you
  2050. 1:17:56can also do with for Loops is you can
  2051. 1:17:57let the user determine what this value
  2052. 1:18:00is going to be let's test that so if we
  2053. 1:18:03block comment this section out what we
  2054. 1:18:05can do is we can appen this little
  2055. 1:18:06section so we can have a console. right
  2056. 1:18:09how many times do you want to say hi can
  2057. 1:18:13have a callon followed by speech marks
  2058. 1:18:15and then we can say in Loop counter
  2059. 1:18:18equals convert to in32 and we can have
  2060. 1:18:21console read line inside the
  2061. 1:18:23brackets how many times you want to say
  2062. 1:18:25hi the user types in the value you take
  2063. 1:18:28the console read line as a string
  2064. 1:18:30convert it to an integer and start
  2065. 1:18:31inside loop
  2066. 1:18:33counter so now what needs to change
  2067. 1:18:35inside here the five is what determines
  2068. 1:18:38how many times that we're going to Loop
  2069. 1:18:40so in this case we actually want to just
  2070. 1:18:41Loop by whatever the user types in the
  2071. 1:18:43counter let's just type five and make
  2072. 1:18:46sure it works and if we just type one it
  2073. 1:18:50works if we type zero however it will
  2074. 1:18:54just skip over the for Loop and the
  2075. 1:18:56reason for that is if you're going from
  2076. 1:18:580o to I is less than zero then this is
  2077. 1:19:00immediately going to be false so then
  2078. 1:19:03the for Loop is just going to get
  2079. 1:19:04skipped immediately there is nothing to
  2080. 1:19:06do if the user is typed in zero which is
  2081. 1:19:08perfectly reasonable and you can type in
  2082. 1:19:10a really big number like a 100 and it
  2083. 1:19:12will output them all to the
  2084. 1:19:15screen if the user types in zero then
  2085. 1:19:18nothing's going to come up so you always
  2086. 1:19:19want to be able to let the user know if
  2087. 1:19:21there's something wrong so we can say if
  2088. 1:19:23the loop counter equals equals to zero
  2089. 1:19:26or maybe we can change this to less than
  2090. 1:19:28or equal to because it could type in
  2091. 1:19:30minus numbers as well we could just say
  2092. 1:19:32a conso right line using CW tab tab
  2093. 1:19:35sorry please enter a value above zero
  2094. 1:19:40and then if this condition has returned
  2095. 1:19:42back false then inside our else
  2096. 1:19:44statement we don't need to give it an
  2097. 1:19:46else if because if it's less than or
  2098. 1:19:48equal to zero then we want to process
  2099. 1:19:50the other values because it has to be
  2100. 1:19:53greater or equal to one
  2101. 1:19:56so when this is false this section has
  2102. 1:19:59to be greater than or equal to one so we
  2103. 1:20:01can just place our fall Loop inside here
  2104. 1:20:03and that's fine and let's try to run the
  2105. 1:20:05code now and if we type in zero please
  2106. 1:20:08enter a value of zero and if we just try
  2107. 1:20:10any negative values we'll get the same
  2108. 1:20:12message if you want to print additional
  2109. 1:20:14code in here you can literally do
  2110. 1:20:15anything you want you can even ask the
  2111. 1:20:17user what they want to print up here and
  2112. 1:20:19then you can replace this with a message
  2113. 1:20:22for example we could have a string
  2114. 1:20:23message
  2115. 1:20:25and we can just change this to hello
  2116. 1:20:26world and this could be something that
  2117. 1:20:28you reading from the console so we can
  2118. 1:20:30say message and we can put that in here
  2119. 1:20:32and if we print that there we
  2120. 1:20:35go so we can just say console.
  2121. 1:20:39right what do you want to repeat and
  2122. 1:20:43then we can read this
  2123. 1:20:45in and actually it might be easier if we
  2124. 1:20:48change this Loop counter to Second so if
  2125. 1:20:51you highlight the section hold alt and
  2126. 1:20:53press down arrow twice we can move it
  2127. 1:20:55down and say and how many times do you
  2128. 1:20:59want to repeat it there we go so now
  2129. 1:21:03what we can do is we can use this
  2130. 1:21:05message inside here and then Loop
  2131. 1:21:07depending on how many times the user
  2132. 1:21:09typed in whatever so we can type in
  2133. 1:21:11hello world get this exact value and we
  2134. 1:21:13can say 10 times there we go so now this
  2135. 1:21:16entire program is fully customized by
  2136. 1:21:18the users
  2137. 1:21:20input so let's just have a quick recap
  2138. 1:21:23so we started off this code I print in
  2139. 1:21:25the console right line five times to the
  2140. 1:21:26screen we wrote Our simple far Loop that
  2141. 1:21:29went from in I equals 0 to I is less
  2142. 1:21:32than five and we did I ++ because we
  2143. 1:21:34want to count up in ones you can place
  2144. 1:21:37the high inside here and it'll print it
  2145. 1:21:39five times for us so now we've made our
  2146. 1:21:41code from five lines down to just three
  2147. 1:21:43lines for the follow and then we allowed
  2148. 1:21:46the user to type in how many times that
  2149. 1:21:48they want to repeat it and we change the
  2150. 1:21:50condition from I is less than 5 to I is
  2151. 1:21:52less than whatever the user has typed in
  2152. 1:21:55installed within the loop counter and
  2153. 1:21:57then we hit a problem where if we
  2154. 1:21:59entered zero anything less than zero the
  2155. 1:22:01for Loop would just not run and it might
  2156. 1:22:03look like some things went wrong so we
  2157. 1:22:05added a little if statement in here to
  2158. 1:22:07make sure that the user has entered a
  2159. 1:22:09value above zero and if they haven't we
  2160. 1:22:11can let them know we don't need another
  2161. 1:22:13if statement in here because if this has
  2162. 1:22:15returned back false we can assume that
  2163. 1:22:17the value is something that we can work
  2164. 1:22:19with we later tailored the program so we
  2165. 1:22:22can change this message that was fixed
  2166. 1:22:24to I to something that the user can type
  2167. 1:22:26in
  2168. 1:22:28manually so now that we know how to use
  2169. 1:22:31for Loops let's have a look at while
  2170. 1:22:32Loops for loops and while Loops still
  2171. 1:22:35iterate through code but they're used in
  2172. 1:22:37slightly different contexts for Loops
  2173. 1:22:39are used mainly when you know how many
  2174. 1:22:41times you can iterate through as in this
  2175. 1:22:43example we're going to print to the
  2176. 1:22:45console exactly 10 times but in a while
  2177. 1:22:47loop you might print until a condition
  2178. 1:22:50is true for example if you asking the
  2179. 1:22:52user to type a number and they kept
  2180. 1:22:54typing in something else you don't know
  2181. 1:22:56how many times it's going to take the
  2182. 1:22:58user to type in correctly so in that
  2183. 1:23:01case you would use a y Loop if you
  2184. 1:23:03executing a certain amount of code and
  2185. 1:23:05you're only doing it a fixed amount of
  2186. 1:23:07times then it's always best to use a for
  2187. 1:23:09Loop so first let's try and convert this
  2188. 1:23:11simple for Loop that just prints I to
  2189. 1:23:13the console 10
  2190. 1:23:15times just like so from zero all the way
  2191. 1:23:18till 9 so let's comment out this for
  2192. 1:23:21Loop and have a look at this syntax for
  2193. 1:23:23the while loop so type the world while
  2194. 1:23:26and then in here we have our condition
  2195. 1:23:28and then we can open our curly braces
  2196. 1:23:30and while this condition returns back
  2197. 1:23:32true it will always go inside the curly
  2198. 1:23:34braces and when this condition returns
  2199. 1:23:36back false then we'll exit the while
  2200. 1:23:39loop so let's try and mimic exactly what
  2201. 1:23:42we're doing inside here there's three
  2202. 1:23:44sections to the for Loop we Define the
  2203. 1:23:47variable that we're going to use for the
  2204. 1:23:48for Loop counter and we give it an
  2205. 1:23:50initial value we give it the stopping
  2206. 1:23:52condition and we give it how many many
  2207. 1:23:54times the value needs to go up every
  2208. 1:23:56time the for Loop iterates in the while
  2209. 1:23:58loop situation we only have the
  2210. 1:24:00condition available so let's take our
  2211. 1:24:02condition and paste it straight into
  2212. 1:24:04here and now we need to store our
  2213. 1:24:06variable so just because it's a y Loop
  2214. 1:24:09and we can't store it inside here we
  2215. 1:24:10have to do it outside the while loop so
  2216. 1:24:12let's make it in I and we'll make it
  2217. 1:24:14equal to zero so we've got our variable
  2218. 1:24:17we've got our condition now we need to
  2219. 1:24:19set
  2220. 1:24:20i++ so inside the Y Loop we can say i++
  2221. 1:24:24and then now we've achieved this entire
  2222. 1:24:26line and successfully converted it let's
  2223. 1:24:28try and put the console right line in
  2224. 1:24:30and see what
  2225. 1:24:32happens and let's run the
  2226. 1:24:34code so it's going from 1 all the way
  2227. 1:24:37till 10 and the reason why it's going
  2228. 1:24:39from 1 till 10 not from 0 to 10 is
  2229. 1:24:42because the i++ is at the start let's
  2230. 1:24:44move it down online using alt and down
  2231. 1:24:46arrow key to move it down and let's run
  2232. 1:24:49it again and you can see it goes from
  2233. 1:24:50zero till 9 and if we just block comment
  2234. 1:24:53this out
  2235. 1:24:55and undo the for
  2236. 1:24:57Loop you can see that they're doing
  2237. 1:24:59exactly the same thing so let's bring
  2238. 1:25:01the Y loop
  2239. 1:25:04back and as you can see what we have is
  2240. 1:25:07we've defined our I variable made it
  2241. 1:25:09equals to zero while the I variable is
  2242. 1:25:12less than the value of 10 we print the I
  2243. 1:25:15to the console and increment I by one
  2244. 1:25:17and we keep looping this until this
  2245. 1:25:20statement returns back false and then we
  2246. 1:25:22exit the Y Loop and jump underneath the
  2247. 1:25:24the curly braces and hit the console
  2248. 1:25:26read line and this is the output that
  2249. 1:25:28we've got so now I've just copied the
  2250. 1:25:30code from the if statement video and
  2251. 1:25:31let's make this code a little bit better
  2252. 1:25:33so let's comment out our while loop and
  2253. 1:25:35our for Loop that was just used for
  2254. 1:25:37explanation purposes and this is the
  2255. 1:25:39code that you've saw earlier on in the C
  2256. 1:25:41we read in the first number Star as a
  2257. 1:25:43string use that string to convert to an
  2258. 1:25:46integer star number a and Then star
  2259. 1:25:48number B and these two blocks are
  2260. 1:25:50exactly the same except you have enter
  2261. 1:25:52first number and enter second number
  2262. 1:25:54and we store number a and store number B
  2263. 1:25:57then we store the value of them both
  2264. 1:25:59multiplied inside our int answer
  2265. 1:26:02variable then we ask the user what's the
  2266. 1:26:04value of both answers times together
  2267. 1:26:07give them a chance to input their value
  2268. 1:26:09convert it to an integer and then we
  2269. 1:26:11check the value and print well done of
  2270. 1:26:13Cl but it was wrong now if we run this
  2271. 1:26:15code again and we type in 10 and 10 and
  2272. 1:26:19we know the value is 100 but let's just
  2273. 1:26:21type in 90 now this is kind of a
  2274. 1:26:23problematic user case we've asked the
  2275. 1:26:26user to type in a value and they've got
  2276. 1:26:28it wrong and then when they press enter
  2277. 1:26:30the program just closes there's no
  2278. 1:26:32opportunity to Loop or try this again so
  2279. 1:26:35what we can do is add a y Loop in here
  2280. 1:26:37and we can say while the answer is
  2281. 1:26:39incorrect we can keep asking the user to
  2282. 1:26:41keep on guessing so instead of reading
  2283. 1:26:44the input here let's turn this little
  2284. 1:26:45section into a question what's the value
  2285. 1:26:50of number a * number B and instead of
  2286. 1:26:52allowing an input here let's just put a
  2287. 1:26:54question mark and then let's add a
  2288. 1:26:57console right line just to give a little
  2289. 1:26:58bit of a gap and then in order to do our
  2290. 1:27:01while loop we actually want to create a
  2291. 1:27:03boan variable just to track if the
  2292. 1:27:05answer has been correct or
  2293. 1:27:07not this is the section that we want to
  2294. 1:27:10loop around in the while loop we want to
  2295. 1:27:12read in the user's answer check it
  2296. 1:27:14output a message and then give them the
  2297. 1:27:16opportunity to read in another value so
  2298. 1:27:18they can guess again while the answer is
  2299. 1:27:22not equal to the actual answer
  2300. 1:27:24then we can loop around our code so we
  2301. 1:27:26have to modify a few things here so we
  2302. 1:27:28can't just take this code and place it
  2303. 1:27:30inside the problem with doing this is
  2304. 1:27:33the variables that we want to check
  2305. 1:27:35answer is outside of this y Loop but
  2306. 1:27:37actual answer is inside and the reason
  2307. 1:27:40why there's a red line here says it does
  2308. 1:27:42not exist in the current context because
  2309. 1:27:44we're trying to check a variable that
  2310. 1:27:45hasn't been defined yet so what we can
  2311. 1:27:47do is take the Declaration for the
  2312. 1:27:49actual answer and place it outside here
  2313. 1:27:51and make it equal to zero and instead of
  2314. 1:27:54redeclaring it inside the while loop we
  2315. 1:27:55can take this in and get rid of it so we
  2316. 1:27:58set the actual answers to zero set the
  2317. 1:28:00answer to the multiplication while the
  2318. 1:28:02answer is not equal to the actual answer
  2319. 1:28:05then we want to loop around so we don't
  2320. 1:28:07actually need another check in here this
  2321. 1:28:10while loop will keep on repeating if the
  2322. 1:28:11answer is incorrect so we don't need a
  2323. 1:28:14path for correct and incorrect if this
  2324. 1:28:16while loop still continues then the
  2325. 1:28:18answer will always be incorrect so what
  2326. 1:28:20we can do to double check is we can say
  2327. 1:28:23if the answer answer does not equal the
  2328. 1:28:25actual answer then we can place this
  2329. 1:28:27message inside here and get rid of this
  2330. 1:28:31branch and then after the while loop has
  2331. 1:28:34continued we can say Well done so let's
  2332. 1:28:37just read through the code while the
  2333. 1:28:39answer is not equal to the actual answer
  2334. 1:28:42and when this while loop first enters
  2335. 1:28:43it's going to be true because you're
  2336. 1:28:45going to have 10 * 10 which is 100 and
  2337. 1:28:48the value of answer is zero so this will
  2338. 1:28:50return back true because these two
  2339. 1:28:52values are not equal to each other then
  2340. 1:28:54we're going to ask the user to enter the
  2341. 1:28:55value then we're going to read into the
  2342. 1:28:57actual answer check if they're not equal
  2343. 1:28:59to each other and then we output a
  2344. 1:29:01message because we're asking a question
  2345. 1:29:03here we want to ask another one using a
  2346. 1:29:05console right Line enter your answer
  2347. 1:29:10callon here we go so let's have a look
  2348. 1:29:12and see how this
  2349. 1:29:14works 100 100 what's the value 100 * 100
  2350. 1:29:19and let's just get it wrong on purpose
  2351. 1:29:2190 CLA but is wrong and now we get to
  2352. 1:29:23answer again
  2353. 1:29:24and we can keep answering this as many
  2354. 1:29:26times as we want until we reach the
  2355. 1:29:28correct answer which in this case is
  2356. 1:29:3110,000 and we've got the answer well
  2357. 1:29:33done so to make the console look a
  2358. 1:29:36little bit better cuz this is all in one
  2359. 1:29:37block we can separate these with console
  2360. 1:29:39right lines maybe after this line we can
  2361. 1:29:42print out a new line and then after each
  2362. 1:29:44guess we can print out another new line
  2363. 1:29:46so after each guess we'll be down here
  2364. 1:29:48we can just print out a blank console
  2365. 1:29:49right line and just after you ask them
  2366. 1:29:52to enter the second number we can print
  2367. 1:29:54another one here and let's just see how
  2368. 1:29:56that looks 10 10 now it looks a lot
  2369. 1:29:58better because you ask the user to input
  2370. 1:30:00the numbers then you ask them the
  2371. 1:30:02question and then you get the answer and
  2372. 1:30:04then it keeps looping around like
  2373. 1:30:06this so there's a little gap between
  2374. 1:30:09each one so you can efficiently read it
  2375. 1:30:10instead of it all being closed off
  2376. 1:30:12together now if you answer it correctly
  2377. 1:30:14100 now it says well done and when we
  2378. 1:30:17press enter again the program now closes
  2379. 1:30:19so that was using a while loop and now
  2380. 1:30:21we have something else called a do while
  2381. 1:30:24and a do while is very close to a while
  2382. 1:30:27except that the while will only happen
  2383. 1:30:29if this condition returns back true and
  2384. 1:30:31a do while will always happen once and
  2385. 1:30:34it'll check the condition at the end so
  2386. 1:30:36let's have a look and see how this works
  2387. 1:30:38so we write the word do and then we have
  2388. 1:30:40the curly braces and then after it we
  2389. 1:30:42have our while condition and let's just
  2390. 1:30:44copy the condition from the old while
  2391. 1:30:46loop and we can say answer is not equal
  2392. 1:30:49to actual answer so as you can see with
  2393. 1:30:51a while loop it will always check the
  2394. 1:30:53condition first first and then it opens
  2395. 1:30:55the curly braces in the do there is no
  2396. 1:30:58condition it will always do everything
  2397. 1:31:00inside the curly braces once and then it
  2398. 1:31:02will check this while condition if this
  2399. 1:31:05condition is still true then it'll loop
  2400. 1:31:07around and check it every time but it
  2401. 1:31:09will only check it at the end while
  2402. 1:31:11Loops will check the condition as the
  2403. 1:31:13first thing it does and then check it
  2404. 1:31:15every iteration so let's move our cord
  2405. 1:31:18down into here and let's just comment
  2406. 1:31:20out our while loop just to see how this
  2407. 1:31:22works and if we run it again to make
  2408. 1:31:25sure it's working fine 10 10 90 90 50
  2409. 1:31:30and then finally 100 and it says well
  2410. 1:31:32done so you could be thinking what are
  2411. 1:31:34the differences between a while loop and
  2412. 1:31:36a do while in certain cases you want the
  2413. 1:31:38while loop to make sure that this
  2414. 1:31:40condition is true before even continuing
  2415. 1:31:42in this specific case we always want the
  2416. 1:31:45user to answer the question it doesn't
  2417. 1:31:48matter if it executes once because
  2418. 1:31:50actually that's the behavior that we
  2419. 1:31:51want we've asked the user a question
  2420. 1:31:54right here so we want to always be
  2421. 1:31:56looping at least one time the while loop
  2422. 1:31:59user case doesn't quite apply here
  2423. 1:32:01because we don't necessarily have to
  2424. 1:32:03check if they've answered correctly if
  2425. 1:32:05they haven't even inputed a value yet we
  2426. 1:32:08always want to be able to give them the
  2427. 1:32:09option to do it once and then we check
  2428. 1:32:12their answer at the end they could get
  2429. 1:32:13it on the first answer 10 10 100 and
  2430. 1:32:17it'll still be fine the same is true
  2431. 1:32:19with a while loop but it'll be easier
  2432. 1:32:22and it makes more sense to read your
  2433. 1:32:24code when you have a while loop you can
  2434. 1:32:26see that this bit of code depends on a
  2435. 1:32:28condition whereas in a do while the
  2436. 1:32:31first iteration does not depend on
  2437. 1:32:33anything but the second and third and so
  2438. 1:32:35forth depend on the condition so
  2439. 1:32:38depending on your user case and what
  2440. 1:32:40situation you want to use it you should
  2441. 1:32:42either pick a while loop which will only
  2442. 1:32:44occur through a condition or pick a do
  2443. 1:32:47while that will occur once or multiple
  2444. 1:32:49times after the first condition has
  2445. 1:32:51passed
  2446. 1:32:54so when you have if statements that are
  2447. 1:32:56pretty basic you can actually make them
  2448. 1:32:58quite easy to read through the
  2449. 1:33:00conditional operator also known as the
  2450. 1:33:02tary operator so let's see how this
  2451. 1:33:04works let's say we have an INT age and
  2452. 1:33:06we set it to a minus value and we know
  2453. 1:33:09that this value can never be someone's
  2454. 1:33:11age because it's negative but let's say
  2455. 1:33:13that we have a situation where you ask
  2456. 1:33:15the user for their age and they've
  2457. 1:33:17accidentally typed in -10 and we know
  2458. 1:33:19that that value is not actually true so
  2459. 1:33:21we could write something like this age
  2460. 1:33:24is greater than or equal to zero then we
  2461. 1:33:26can print out
  2462. 1:33:27valid and then in the else we can take
  2463. 1:33:31this console right line and we can print
  2464. 1:33:33out invalid so that means if we run our
  2465. 1:33:35code now you can see that it says
  2466. 1:33:38invalid and if we change it to positive
  2467. 1:33:4010 now it says valid so this seems a
  2468. 1:33:43little bit long we are just checking one
  2469. 1:33:46condition printing a value and printing
  2470. 1:33:49another value if that condition returns
  2471. 1:33:51back false so our conditional operator
  2472. 1:33:54works like this we have a condition
  2473. 1:33:56followed by question mark then the value
  2474. 1:33:59if it returns true then the value if it
  2475. 1:34:01returns false let's see how you can
  2476. 1:34:03rewrite this if statement into one
  2477. 1:34:05simple line so let's take this in
  2478. 1:34:09steps these two values are both strings
  2479. 1:34:12that we've typed in so let's make a
  2480. 1:34:14string and we can call it result and
  2481. 1:34:16let's make it equal to our Turner
  2482. 1:34:19operator so the first step is to get the
  2483. 1:34:21condition and the condition in this case
  2484. 1:34:23is if the age is greater than equal to
  2485. 1:34:25zero we can follow that by a question
  2486. 1:34:28mark and then if this returns back true
  2487. 1:34:31we want the string to say valid and we
  2488. 1:34:33can have a call on and if it returns
  2489. 1:34:36back false then we want the string to
  2490. 1:34:38say invalid and we can add a semicolon
  2491. 1:34:41and then straight after it we can just
  2492. 1:34:42have a console right line that just says
  2493. 1:34:45result as you can see now that we get it
  2494. 1:34:47twice we can verify one comes from the
  2495. 1:34:49if statement and one comes from the turn
  2496. 1:34:52operator and now if we make this say
  2497. 1:34:53positive 10 then you can see it both
  2498. 1:34:55says valid so if we comment out the if
  2499. 1:34:58statement and just see this running on
  2500. 1:35:00its own just to double check there we go
  2501. 1:35:03and because this is just returning back
  2502. 1:35:04a string we can actually just take all
  2503. 1:35:07of this code in the conditional operator
  2504. 1:35:09and paste it straight into the console
  2505. 1:35:11right line so you run the line of code
  2506. 1:35:12now we get exactly the same thing if
  2507. 1:35:15this hurts the readability of your code
  2508. 1:35:18then it's best to just store it and then
  2509. 1:35:19use it after if you can read this code
  2510. 1:35:21and it's better for you in the long run
  2511. 1:35:24later on you might want to dismiss this
  2512. 1:35:26variable and do it straight inside the
  2513. 1:35:28brackets just to save a variable and
  2514. 1:35:30then print itto the screen straight away
  2515. 1:35:33it'll be better for memory if you just
  2516. 1:35:34print it out straight away but if you're
  2517. 1:35:36not familiar with this notation yet and
  2518. 1:35:38you want to get used to it then that's
  2519. 1:35:39fine as well so a quick recap we had an
  2520. 1:35:42if statement with a condition and if
  2521. 1:35:44this returned back true we printed valid
  2522. 1:35:47and if it return back false we' print
  2523. 1:35:48invalid and this is pretty simple the
  2524. 1:35:51only difference between these two paths
  2525. 1:35:53is this string literal that we've typed
  2526. 1:35:55into here so we can pull the string
  2527. 1:35:57values out and place them in our
  2528. 1:35:59conditional operator we can say string
  2529. 1:36:01result is equal to if the age is greater
  2530. 1:36:04than or equal to zero then we store
  2531. 1:36:07valid inside result and if it's not
  2532. 1:36:09greater than equal to zero then we can
  2533. 1:36:11St invalid straight in our string after
  2534. 1:36:14we've stored this value we can print
  2535. 1:36:16results straight to the screen which
  2536. 1:36:18will either give us valid or
  2537. 1:36:22invalid when when you're dividing two
  2538. 1:36:24numbers you might get an ugly output
  2539. 1:36:26just like
  2540. 1:36:27this the user sometimes doesn't want to
  2541. 1:36:29see such long decimal places and maybe
  2542. 1:36:32you just want to prevent it to
  2543. 1:36:3481.3 in order to do this we need to use
  2544. 1:36:37a format pattern so the language
  2545. 1:36:39understands what we're trying to achieve
  2546. 1:36:40so if we make another console right line
  2547. 1:36:42what we can do is use string. format and
  2548. 1:36:45inside the brackets we can give it a
  2549. 1:36:47format and then the object that we want
  2550. 1:36:48to format so in this case the object in
  2551. 1:36:51the second parameter is our value so
  2552. 1:36:53let's open some speech marks go to the
  2553. 1:36:56next parameter using the comma and just
  2554. 1:36:58type in our value and then inside the
  2555. 1:37:01speech marks we want to type in the
  2556. 1:37:02pattern if you type in curly braces as
  2557. 1:37:04zero then zero represents the first
  2558. 1:37:07valid you in this list we can do a space
  2559. 1:37:10followed by curly braces one and then
  2560. 1:37:12add another comma and we can add a
  2561. 1:37:14thousand and what this means is value
  2562. 1:37:17will get place where zero is and 1,000
  2563. 1:37:19will get Place one is so let's run this
  2564. 1:37:23and you see the exact same thing's
  2565. 1:37:25happened plus dis place and now we have
  2566. 1:37:27the Thousand which follows this pattern
  2567. 1:37:29exactly if this is confusing don't worry
  2568. 1:37:32too much about it I have the next
  2569. 1:37:33section all about string functions so
  2570. 1:37:36let's get rid of the one followed by the
  2571. 1:37:38thousand and run it again and we should
  2572. 1:37:40see the output being exactly the same
  2573. 1:37:42there we go so now let's see how we can
  2574. 1:37:45make this better in terms of decimal
  2575. 1:37:47places so just after the zero and the
  2576. 1:37:49zero represents the first value we can
  2577. 1:37:52add a call on and this column will tell
  2578. 1:37:54us what format we want the value to be
  2579. 1:37:56displayed as we can type in
  2580. 1:37:590. and what this means is all the
  2581. 1:38:01numbers before the decimal place will
  2582. 1:38:03get displayed and only two numbers after
  2583. 1:38:06the decimal place will get displayed so
  2584. 1:38:08let's try run this now and you can see
  2585. 1:38:09that the code output is a lot cleaner
  2586. 1:38:12one thing to note though by default this
  2587. 1:38:13will round it up 0.37 becomes
  2588. 1:38:170.4 not to worry though cuz this is what
  2589. 1:38:20the user will see on the screen and it
  2590. 1:38:21won't affect the actual value of the
  2591. 1:38:23variable if we print out value straight
  2592. 1:38:26after you'll see that the difference
  2593. 1:38:28hasn't been changed it's still 0.3 in
  2594. 1:38:31the original variable so that's one
  2595. 1:38:33method of doing it what we can do now is
  2596. 1:38:35just use controll D and copy it down and
  2597. 1:38:38we can use different formats to show you
  2598. 1:38:39what it looks like if we remove the 0
  2599. 1:38:43and then one of the zeros and leave it
  2600. 1:38:45like this and it gains one decimal place
  2601. 1:38:47each time you'll see what the output
  2602. 1:38:49looks like we just have the 81 which
  2603. 1:38:53essentially makes it into an integer
  2604. 1:38:55because it's a whole number then we have
  2605. 1:38:5781.0 and the reason why it's 0 is cuz
  2606. 1:39:000.3 will round down as it's under
  2607. 1:39:030.5 the 03 is going to round down as
  2608. 1:39:06it's under 05 so it'll round down to
  2609. 1:39:10zero and then finally we have the
  2610. 1:39:1381.4 and just like before this will
  2611. 1:39:15round down
  2612. 1:39:16to4 and just like before this will round
  2613. 1:39:19down to
  2614. 1:39:200.4 this will round down to 0 four let's
  2615. 1:39:24say you wanted to get rid of trailing
  2616. 1:39:26zeros because you don't want them then
  2617. 1:39:28you can actually change this zero to a
  2618. 1:39:30hashtag once you run the hashtag then it
  2619. 1:39:32will trim off any trailing zeros so what
  2620. 1:39:35this means is it will try and put it in
  2621. 1:39:37this format but if this format happens
  2622. 1:39:39to be a zero then it will just keep it
  2623. 1:39:41like this and if you just have 81 dot
  2624. 1:39:44then it'll also get rid of the dot for
  2625. 1:39:45you so then you just left with
  2626. 1:39:4781 it's really up to you as the
  2627. 1:39:50programmer what you want to display
  2628. 1:39:51depending on what context I'm just given
  2629. 1:39:53you the options that you have available
  2630. 1:39:56so let's say we want to work with money
  2631. 1:39:57now and we have a 10d / by a 3D and what
  2632. 1:40:02that value would be is somewhere between
  2633. 1:40:043333 and this will be reoccurring cuz
  2634. 1:40:06it's exactly 1/3 the reason why I've got
  2635. 1:40:09D's in here is because these are
  2636. 1:40:11registering as doubles if you type the
  2637. 1:40:13value in normally it'll get registers at
  2638. 1:40:15in 32 and the problem with in32 is when
  2639. 1:40:18you do this division it will process the
  2640. 1:40:21division as an integer and then store it
  2641. 1:40:23as a a double the issue with that is in
  2642. 1:40:25this case and in this case both values
  2643. 1:40:28will get truncated let me demonstrate if
  2644. 1:40:30I remove the D from both sides and down
  2645. 1:40:33in here it will'll see what the
  2646. 1:40:34difference is and let's print out money
  2647. 1:40:37and let's see what's happened in the
  2648. 1:40:39first case this isn't actually an issue
  2649. 1:40:41because we have a decimal place here
  2650. 1:40:43which means this will be a double so an
  2651. 1:40:45integer divid by double will give us a
  2652. 1:40:47double but we just place the D here just
  2653. 1:40:49so we don't forget and it makes the
  2654. 1:40:51compiler's job a lot easier cuz it
  2655. 1:40:53doesn't have to convert it from an
  2656. 1:40:54integer to a double and in this case we
  2657. 1:40:57have an INT that's divided by an INT so
  2658. 1:41:00we'll get 3.3 reoccurring but because
  2659. 1:41:02it's processing as an integer then we
  2660. 1:41:04just get three being displayed and this
  2661. 1:41:07is not the intended result so let's just
  2662. 1:41:09replace these back to D's and we'll see
  2663. 1:41:10what we get now which is the 3.3 perfect
  2664. 1:41:14so let's say that we want to display
  2665. 1:41:16this as actual currency because you
  2666. 1:41:17never see it displayed like this it's
  2667. 1:41:20always to two decimal places just as
  2668. 1:41:22follows so we can follow the same
  2669. 1:41:24approach as
  2670. 1:41:25above we can do string.
  2671. 1:41:28format 0 callon 0.0 and the curly brace
  2672. 1:41:32and then common money cuz that's the
  2673. 1:41:34variable in reference now if we run that
  2674. 1:41:36let's see what happens
  2675. 1:41:383.33 that's perfect and now what we can
  2676. 1:41:41do in here because this is the pattern
  2677. 1:41:43we can actually type in anything before
  2678. 1:41:45after this and it won't affect the
  2679. 1:41:47pattern just like so we've added some
  2680. 1:41:49Extra Spaces in here so if we get rid of
  2681. 1:41:51these spaces we can actually just play
  2682. 1:41:53is a pound sign in front of it and then
  2683. 1:41:56let's see what happens when we run it
  2684. 1:41:58perfect so now we actually have some
  2685. 1:42:00money and we can say in here £10
  2686. 1:42:05/3 equals this and now our output looks
  2687. 1:42:09a little bit better because we have some
  2688. 1:42:11formatting now the issue is when we
  2689. 1:42:13start getting into negatives let's see
  2690. 1:42:15what happens so we can say -10 IDE by 3
  2691. 1:42:20equals something and when we run we'll
  2692. 1:42:23see that the format is actually
  2693. 1:42:25incorrect now you never see the negative
  2694. 1:42:28symbol after the pound symbol it's
  2695. 1:42:30always this way around in this case what
  2696. 1:42:33we're doing is we're telling the
  2697. 1:42:35compiler hey we only want to see two
  2698. 1:42:37decimal places but always keep in this
  2699. 1:42:40format the pound sign followed by the
  2700. 1:42:42value and the value of money is -3 so
  2701. 1:42:46it'll Place -3 inside here and then add
  2702. 1:42:48the pound sign before it but we don't
  2703. 1:42:50exactly want that so we can add some
  2704. 1:42:53something called a culture info but we
  2705. 1:42:55can also give it some other formatting
  2706. 1:42:57some other way so let's explore some
  2707. 1:42:59other
  2708. 1:43:00options if you use money to string then
  2709. 1:43:03what we're actually doing here is using
  2710. 1:43:05the double variable and converting it to
  2711. 1:43:07a string I me get passed back a string
  2712. 1:43:10representative of this value and that's
  2713. 1:43:13fine and when we run the code you'll see
  2714. 1:43:15no difference between these two at this
  2715. 1:43:17point they're both being converted to
  2716. 1:43:19Strings but what we can do in this case
  2717. 1:43:22is when we open these brackets it tells
  2718. 1:43:23us there one of four what does this mean
  2719. 1:43:26exactly so in our two- string function
  2720. 1:43:28we have four ways that we can call this
  2721. 1:43:30function the first way you give nothing
  2722. 1:43:32in the
  2723. 1:43:35brackets and if we press the down arrow
  2724. 1:43:38the second way is to give it a format
  2725. 1:43:40provider the Third Way is just a string
  2726. 1:43:42format and the fourth way is both a
  2727. 1:43:45format and a provider so we'll use all
  2728. 1:43:47of these in a second but let's explore
  2729. 1:43:49the easiest one if you add a string in
  2730. 1:43:51there which gives us the third overload
  2731. 1:43:53string format and we can just type in a
  2732. 1:43:55Capital C let's just see what
  2733. 1:43:58happens perfect so now we have the best
  2734. 1:44:01format display to us and the reason why
  2735. 1:44:03this works is the C program will
  2736. 1:44:06recognize that the format of C is
  2737. 1:44:08telling us that we want to display
  2738. 1:44:10currency so then it's going to your
  2739. 1:44:12local machine and figuring out where you
  2740. 1:44:14are based on your date and time just
  2741. 1:44:16down here when you have adjust dat and
  2742. 1:44:18time it will figure it out based on this
  2743. 1:44:21we have UTC time and we have the date
  2744. 1:44:23and time listed up here based on that
  2745. 1:44:26information it can figure out that we
  2746. 1:44:27are indeed in the UK and we want to
  2747. 1:44:29display a pound sign and because the
  2748. 1:44:31program knows that it's - 3.33 it'll put
  2749. 1:44:34the pound sign in the right location so
  2750. 1:44:37now that we've explored C let's have a
  2751. 1:44:39look at the other ones that are
  2752. 1:44:40available so if we use contrl D and copy
  2753. 1:44:42this down three times we can use C 0 C1
  2754. 1:44:46and C2 and now if you run this again
  2755. 1:44:48you'll see what the output gets you c
  2756. 1:44:51will get you currency in it most native
  2757. 1:44:53form which is what we're used to using
  2758. 1:44:55in the UK c0 will only return you back
  2759. 1:44:58with the whole number C1 will give you
  2760. 1:45:01one decimal place and C2 mimic C on its
  2761. 1:45:04own as they're displaying the same
  2762. 1:45:06values now this is where it gets
  2763. 1:45:08interesting if you want to display
  2764. 1:45:10different currencies then you're going
  2765. 1:45:11to have to use this thing called a
  2766. 1:45:13culture info which will be able to
  2767. 1:45:15retrieve back different currency formats
  2768. 1:45:17based on a certain country code that you
  2769. 1:45:19give the
  2770. 1:45:20function so let's have a look and see
  2771. 1:45:22how that works if we scroll to the top
  2772. 1:45:24we can add a new library called system.
  2773. 1:45:27globalization inside globalization we
  2774. 1:45:29have a culture info so let's see how we
  2775. 1:45:32can tailor these to make it look a lot
  2776. 1:45:34better if you're worried about different
  2777. 1:45:36currencies so we us CW tab tab and we do
  2778. 1:45:40the same thing money. two string we add
  2779. 1:45:42our C and this is where our format
  2780. 1:45:44provider fits in so we can start type in
  2781. 1:45:47culture info it should come up like this
  2782. 1:45:50press enter and inside this is a class
  2783. 1:45:53that's why it went green if we press dot
  2784. 1:45:56then we get the current culture the
  2785. 1:45:58current culture will decide based on the
  2786. 1:46:00machine that the program is running on
  2787. 1:46:02if this program is running on a cloud
  2788. 1:46:04computer and the cloud computer is
  2789. 1:46:06stored in Europe then it might display
  2790. 1:46:08euros and if it's stored in America then
  2791. 1:46:10it'll display dollars so you want to
  2792. 1:46:12avoid using current culture if you're
  2793. 1:46:14going to scale your program worldwide in
  2794. 1:46:17the case of this example I'm going to
  2795. 1:46:19leave it because as you can see when I
  2796. 1:46:21run it it will display the same thing as
  2797. 1:46:22as the other ones because this one will
  2798. 1:46:25also recognize that my current culture
  2799. 1:46:27is UK and I'm also telling it to use the
  2800. 1:46:30current culture so in both of these
  2801. 1:46:31cases it's figured out that my current
  2802. 1:46:33culture is indeed the UK and it's
  2803. 1:46:36started using the pound sign but let's
  2804. 1:46:38say we want to give it a different
  2805. 1:46:39culture then what we can do if we
  2806. 1:46:41duplicate this line using contr D and
  2807. 1:46:44remove this section just after the dot
  2808. 1:46:47we can actually say create specific
  2809. 1:46:49culture and inside the parentheses we
  2810. 1:46:52can actually add a string and it says
  2811. 1:46:54here a predefined culture name or the
  2812. 1:46:56name of existing culture info object the
  2813. 1:46:58name is not case sensitive so in the
  2814. 1:47:01case of the UK you have English GB and
  2815. 1:47:05now we need another bracket at the end
  2816. 1:47:06and then if we use control D to copy
  2817. 1:47:08this down we can change GB to us let's
  2818. 1:47:12run this again and see what
  2819. 1:47:16happens these two don't actually output
  2820. 1:47:18anything different because we're just
  2821. 1:47:20doing the normal culture and then we're
  2822. 1:47:22telling it to find the GB culture which
  2823. 1:47:24is exactly the same thing for me so we
  2824. 1:47:26get these two values exactly the same
  2825. 1:47:28and apparently it's picking up that in
  2826. 1:47:30the US you have brackets around your
  2827. 1:47:32negative values which I can't personally
  2828. 1:47:34validate but this is how it's showing up
  2829. 1:47:37so let's try some other cultures and see
  2830. 1:47:39what happens so you can try Au for
  2831. 1:47:43Australian then you can see that they
  2832. 1:47:45actually use a negative followed by the
  2833. 1:47:47pound sign and then the value so even
  2834. 1:47:50though they're both in dollars one's US
  2835. 1:47:52Dollars and one is Australian dollars
  2836. 1:47:54and apparently the US uses brackets for
  2837. 1:47:56the negative values but Australians use
  2838. 1:47:58the negative value like we do so let's
  2839. 1:48:01just have a quick recap so we can
  2840. 1:48:03display the value just like this but
  2841. 1:48:05we'll have a problem with multiple
  2842. 1:48:06decimal places we can use these format
  2843. 1:48:09options to give us zero decimal places
  2844. 1:48:12one or two decimal places and then when
  2845. 1:48:14we use this format the problem with it
  2846. 1:48:17was when you display negative numbers
  2847. 1:48:19you get a nasty format where the
  2848. 1:48:21negative value actually comes after the
  2849. 1:48:23pound sign which is not correct and you
  2850. 1:48:25don't want to Output that to your users
  2851. 1:48:27instead you should use the C format to
  2852. 1:48:29tell the compiler that you want to
  2853. 1:48:31process this as a currency currency
  2854. 1:48:33processing is better because the
  2855. 1:48:35software will understand that the value
  2856. 1:48:37you're trying to represent depends on a
  2857. 1:48:38certain strict format just as follows
  2858. 1:48:41you don't want the negative to be here
  2859. 1:48:43instead of before it because that
  2860. 1:48:45doesn't look very nice to the end user
  2861. 1:48:47alternatively you could use this C info
  2862. 1:48:50if you're going to scale your program
  2863. 1:48:52scale your program means that it's going
  2864. 1:48:54to live on servers and this culture info
  2865. 1:48:57especially the current culture depends
  2866. 1:48:59on where the program is being run on
  2867. 1:49:01because it depends on the system time
  2868. 1:49:03different servers will have different
  2869. 1:49:05dat times set because they'll be in
  2870. 1:49:07different countries so you want to be
  2871. 1:49:09careful and if you want to display in a
  2872. 1:49:10certain currency you can create a
  2873. 1:49:12culture specification and pass the
  2874. 1:49:15country code inside the brackets and
  2875. 1:49:17then you'll get the correct output and
  2876. 1:49:19it doesn't matter where your program
  2877. 1:49:20lives if you always want to gener
  2878. 1:49:22generate in pound symbols then you
  2879. 1:49:24always use create specific culture and
  2880. 1:49:27always specify
  2881. 1:49:29NGB upon doing so you always get the
  2882. 1:49:31pound sign and it doesn't matter where
  2883. 1:49:33your program
  2884. 1:49:36lives so throughout this course you've
  2885. 1:49:38seen this notation a lot print out value
  2886. 1:49:41using console right to give the user an
  2887. 1:49:43opportunity to write on the same line
  2888. 1:49:45reading their value as a string and
  2889. 1:49:47store it in a variable and then use that
  2890. 1:49:49variable to convert it to an integer 32
  2891. 1:49:52and we're passing the string input
  2892. 1:49:54inside here and then we convert it to an
  2893. 1:49:56integer if we run the code you can see
  2894. 1:49:58that this actually works perfectly
  2895. 1:50:00assuming you actually enter a real
  2896. 1:50:02number like 10 we press enter and it
  2897. 1:50:05works and it outputs back to the screen
  2898. 1:50:07using this console right line and if you
  2899. 1:50:09press this restart button we can start
  2900. 1:50:11the program again and let's say we
  2901. 1:50:12accidentally type a character like L and
  2902. 1:50:15we press enter and we've get a format
  2903. 1:50:17exception now this is problematic
  2904. 1:50:20because our program will now close if we
  2905. 1:50:22press continue it doesn't even hit this
  2906. 1:50:24line or this as soon as it hits the
  2907. 1:50:26exception here our program just closes
  2908. 1:50:29if we use debug start with debugging and
  2909. 1:50:31now we're actually running the program
  2910. 1:50:33outside of visual studio so we won't get
  2911. 1:50:35that pop up when the error happens let's
  2912. 1:50:37see what happens if we type this in now
  2913. 1:50:39we can say 10p and press enter and now
  2914. 1:50:42we get that the program has stopped
  2915. 1:50:43working you've probably seen this pop up
  2916. 1:50:45a lot throughout Windows in different
  2917. 1:50:47versions of Windows because when a
  2918. 1:50:49program has a fatal error or an
  2919. 1:50:50exception you see there's pop up appear
  2920. 1:50:53here and the only thing you can do is
  2921. 1:50:54either debug or close it normally the
  2922. 1:50:57debug option isn't available but because
  2923. 1:50:59this is a c program and it's made
  2924. 1:51:01through Visual Studio and the visual
  2925. 1:51:02studio is being installed on the system
  2926. 1:51:04then it gives us the option but really
  2927. 1:51:07all we can do here is just terminate the
  2928. 1:51:09program it doesn't matter what happened
  2929. 1:51:11because we can't recover this if we
  2930. 1:51:13press close then it's literally just
  2931. 1:51:15going to close down it's going to tell
  2932. 1:51:16us the exception and then press any key
  2933. 1:51:18to continue and as soon as you press any
  2934. 1:51:20key then the program just closes so this
  2935. 1:51:23is very fatal for our code since if we
  2936. 1:51:25had some precious data stored in here or
  2937. 1:51:27anywhere else in the program it is now
  2938. 1:51:29lost before we can do anything this is
  2939. 1:51:32problematic and we want to avoid this
  2940. 1:51:34the best way to do this is to use
  2941. 1:51:36something called try pass right here the
  2942. 1:51:39program is assuming that your string
  2943. 1:51:41input is a perfect input and then you're
  2944. 1:51:43immediately converting it to an integer
  2945. 1:51:46now like I said what if this had 10 H in
  2946. 1:51:49it the program is going to cause an
  2947. 1:51:51exception and then the program is going
  2948. 1:51:52to close so let's see what we can do
  2949. 1:51:54with this trass function so the notation
  2950. 1:51:57is int.
  2951. 1:51:59trass the first parameter is a string s
  2952. 1:52:02string s is a string containing a number
  2953. 1:52:05to convert so what this trip pass
  2954. 1:52:07function will do as it says here
  2955. 1:52:09converts the string representation of a
  2956. 1:52:11number to its 32 bits signed integer
  2957. 1:52:14equivalent a return value indicates
  2958. 1:52:16whether the conversion succeeded okay
  2959. 1:52:18perfect so we'll get into that in a
  2960. 1:52:20second so our string s is a string
  2961. 1:52:22containing our number to convert and in
  2962. 1:52:25this case we want to be using the num
  2963. 1:52:26input so we copy and paste that down and
  2964. 1:52:29then we add a comma cuz we have more
  2965. 1:52:31than one parameter now we have an out in
  2966. 1:52:35result when this method returns result
  2967. 1:52:38which is our integer contains the 32bit
  2968. 1:52:41sign integer equivalent of the number
  2969. 1:52:43contained in S and S in this case is our
  2970. 1:52:45first parameter which is num input if
  2971. 1:52:48the conversion succeeded then we get the
  2972. 1:52:51value inside our result
  2973. 1:52:52or zero if the conversion failed the
  2974. 1:52:55conversion fails if the parameter s is
  2975. 1:52:57null or string empty is not of the
  2976. 1:53:00correct format or it represents a number
  2977. 1:53:02that's less or more than the maximum
  2978. 1:53:04value that the integer can store this
  2979. 1:53:07paramet is past uninitialize any value
  2980. 1:53:09originally supplies in result will be
  2981. 1:53:11overwritten let's dive in to see what
  2982. 1:53:13this means because it's an out in result
  2983. 1:53:17what this means is we can actually
  2984. 1:53:18process a variable inside here like intn
  2985. 1:53:21so we can say out num and what this will
  2986. 1:53:24do is when this line executes it will
  2987. 1:53:26try and convert num input and place the
  2988. 1:53:28value inside num so we don't need to run
  2989. 1:53:31this convert anymore we can just specify
  2990. 1:53:33that num is equal to zero so we've read
  2991. 1:53:36in the string that contains our 10 H and
  2992. 1:53:39we have our Nome that contains zero
  2993. 1:53:42let's just remove this console right
  2994. 1:53:44line so now we have our variable set up
  2995. 1:53:47let's see what happens if we do a
  2996. 1:53:49console right line and just print num
  2997. 1:53:51after this let's see what happens to the
  2998. 1:53:53code so if we type in 10 H and press
  2999. 1:53:56enter the value of num is zero but what
  3000. 1:53:59you notice now the program hasn't closed
  3001. 1:54:01because it's tried to pass it instead of
  3002. 1:54:05just passing it automatically and then
  3003. 1:54:06cause an exception so the reason why Try
  3004. 1:54:10pass is called try pass is first it
  3005. 1:54:12tries and then if it succeeds this will
  3006. 1:54:15contain the value and if it doesn't
  3007. 1:54:17succeed this will contain zero to know
  3008. 1:54:19if the number has actually been
  3009. 1:54:21converted we actually have to use the
  3010. 1:54:23bull return value so what this means is
  3011. 1:54:26because it returns us back a bull we can
  3012. 1:54:28store it inside a bull and we can give
  3013. 1:54:30it the name of success bu success equals
  3014. 1:54:33in. trass we try and pass the num input
  3015. 1:54:36and we place the value inside num the
  3016. 1:54:39reason why having this bu success is
  3017. 1:54:41very very essential let's say for
  3018. 1:54:43example the user actually enters
  3019. 1:54:46zero so technically as of this point num
  3020. 1:54:50zero is actually correct because we've '
  3021. 1:54:52got zero and we initialize it to zero
  3022. 1:54:54but of course after the trip pass runs
  3023. 1:54:57this is still going to be zero now we
  3024. 1:54:59don't know if the value zero is because
  3025. 1:55:02this has succeeded or the value is zero
  3026. 1:55:05because we initialized it to zero let me
  3027. 1:55:07demonstrate if I type in a zero here is
  3028. 1:55:11this zero being converted because this
  3029. 1:55:13was successful or is it presenting me
  3030. 1:55:16zero because this has been initialized
  3031. 1:55:18to zero and the only way we can know is
  3032. 1:55:21because our tripod pass will return us
  3033. 1:55:23back with true if s was converted
  3034. 1:55:26successfully otherwise false we can
  3035. 1:55:29actually write an if statement in here
  3036. 1:55:31if success open the curly braces and we
  3037. 1:55:34can start to type something in here so
  3038. 1:55:36we can take our console right line and
  3039. 1:55:38place it up here using alt and up Arrow
  3040. 1:55:41key and then in the lse we can say
  3041. 1:55:44failed to
  3042. 1:55:47convert so now we have a mini setup here
  3043. 1:55:51if it's been success uccessful we print
  3044. 1:55:53out the number if it hasn't been
  3045. 1:55:55successful then it'll go into this path
  3046. 1:55:57and it'll print fail to convert let's
  3047. 1:55:59have a look and see how this works now
  3048. 1:56:01if I press zero and I see Zero that
  3049. 1:56:04means it's followed down the success
  3050. 1:56:06path and this zero actually means it's
  3051. 1:56:09converted my input instead of just the
  3052. 1:56:11value that it's being initialized by if
  3053. 1:56:13you run the code again and type in 10 H
  3054. 1:56:17then we actually see it says fail to
  3055. 1:56:19convert and why that happened is we have
  3056. 1:56:2110 H appear and then this will return
  3057. 1:56:24back false when it tries to convert num
  3058. 1:56:27input and then after it hits this line
  3059. 1:56:29since success will be false it will
  3060. 1:56:32Dodge this path going into this path and
  3061. 1:56:34output fail to convert what you can do
  3062. 1:56:37if you really wanted to is you could
  3063. 1:56:39wrap this around in a while loop so
  3064. 1:56:41let's see what we can do so we can
  3065. 1:56:43specify a success and make that equal to
  3066. 1:56:47true and then we can add a while loop
  3067. 1:56:49with success in there and then open the
  3068. 1:56:51curly braces and put all of this code
  3069. 1:56:53inside here now we're going to have some
  3070. 1:56:55errors because we got success twice but
  3071. 1:56:58what we can do which will be a lot
  3072. 1:57:00easier is we can place this straight
  3073. 1:57:03inside our bracket and the reason why is
  3074. 1:57:06because this returns us back at booing
  3075. 1:57:08so we don't need to store it and then
  3076. 1:57:10use it another thing we can do is
  3077. 1:57:12instead of having Inn and then num right
  3078. 1:57:15here you can actually make this a
  3079. 1:57:17declaration as well so if we just remove
  3080. 1:57:20this value we can write out
  3081. 1:57:22in n and this will do exactly the same
  3082. 1:57:25thing it just uses less variables while
  3083. 1:57:28our success is true we come into here
  3084. 1:57:31and run this code if we failed to
  3085. 1:57:33convert then we still want it to be true
  3086. 1:57:35so we can loop around if we've converted
  3087. 1:57:38it and the value is successful then we
  3088. 1:57:40can actually make our success at this
  3089. 1:57:43point false cuz that will stop the W
  3090. 1:57:46loop from going around let's run the
  3091. 1:57:48code we can type in 10 H 10 a 10 a etc
  3092. 1:57:53etc and as soon as we type in a real
  3093. 1:57:55number then it will print it out and the
  3094. 1:57:57while loop will cancel and now right
  3095. 1:57:59here we're start on the console readline
  3096. 1:58:02now if we press enter the program will
  3097. 1:58:03close so let's have a
  3098. 1:58:06recap we use inter. trass so we can try
  3099. 1:58:10and pass a value before the program
  3100. 1:58:12assumes that the value is in the correct
  3101. 1:58:13format we give it a string variable and
  3102. 1:58:16it returns us back with a fresh new
  3103. 1:58:18integer called num or whatever you want
  3104. 1:58:20to call it inside here we we can place
  3105. 1:58:22this straight inside the condition of an
  3106. 1:58:24if statement because this function
  3107. 1:58:26returns us back with a true or false
  3108. 1:58:27value otherwise known as a bull if this
  3109. 1:58:30has been successful we stop the while
  3110. 1:58:32loop and print the value out to the
  3111. 1:58:34screen and if it hasn't been successful
  3112. 1:58:37then we print fail to convert and
  3113. 1:58:39because our success is still true it
  3114. 1:58:41will Lo back around to the start of the
  3115. 1:58:43while loop and ask the user to input
  3116. 1:58:46another value to try and convert this
  3117. 1:58:49while loop will only exit if it's indeed
  3118. 1:58:51been successful ful and the only way it
  3119. 1:58:53can exit is by having the success being
  3120. 1:58:56equal to false and the only way that can
  3121. 1:58:58happen is by having a successful
  3122. 1:59:01conversion the use of trass in every
  3123. 1:59:04program is very essential because if you
  3124. 1:59:06have a faat exception then your program
  3125. 1:59:08will close and you will lose all the
  3126. 1:59:10data the problem with that is you might
  3127. 1:59:12lose anything that you haven't processed
  3128. 1:59:14yet at that given point in your program
  3129. 1:59:16so you want to try and use triy pass
  3130. 1:59:18because you'll avoid any potential
  3131. 1:59:20exceptions and you can let the user know
  3132. 1:59:22if the conversion has been successful or
  3133. 1:59:24not and write yourself the same while
  3134. 1:59:26loop just like this so you can tell the
  3135. 1:59:28user hey you inputed it incorrectly but
  3136. 1:59:30this is how to do it right and make sure
  3137. 1:59:33that your program doesn't execute
  3138. 1:59:35incorrectly and make sure your program
  3139. 1:59:37doesn't just close unexpectedly and then
  3140. 1:59:39an error will be
  3141. 1:59:42caused in this exercise I want you to
  3142. 1:59:44print out some sort of times table and
  3143. 1:59:47what I mean by this is you want to ask
  3144. 1:59:48the user to input a number for the table
  3145. 1:59:51and then write WR a for Loop to print
  3146. 1:59:53out the x times table X is depend on the
  3147. 1:59:56value that they entered if they enter 10
  3148. 1:59:58in here then this will be the 10 times
  3149. 2:00:00table let's get started so we can have
  3150. 2:00:04our normal console.
  3151. 2:00:06write enter a
  3152. 2:00:08number and then use our int number
  3153. 2:00:11equals to convert to int32 and we can
  3154. 2:00:15say console. read line because we want
  3155. 2:00:16to read whatever the user types in then
  3156. 2:00:19we can have a four in I equals z i is is
  3157. 2:00:22less than 10 we're only going to print
  3158. 2:00:24up to the 10 * table so 1 * Something 2
  3159. 2:00:28* something all the way up to 10 times
  3160. 2:00:30the value that they
  3161. 2:00:32entered immediately we acknowledge an
  3162. 2:00:34error here when you do your 10 times
  3163. 2:00:36table for example you do 1 * 10 2 * 10
  3164. 2:00:40and so on and so forth the issue here is
  3165. 2:00:43we're going from 0 to 10 but because
  3166. 2:00:46it's less than 10 we're actually going
  3167. 2:00:47from 0 to 9 so let's change this to 1
  3168. 2:00:50and then less than or equal to 10 10 in
  3169. 2:00:52both cases we were actually looping the
  3170. 2:00:55same amount of times the issue is this
  3171. 2:00:57was starting on one and the other one
  3172. 2:01:00was starting on zero and the first one
  3173. 2:01:02was finishing on nine and this one's
  3174. 2:01:04finishing on 10 and we want them to go
  3175. 2:01:06from 1 till 10 instead of from 0 till 9
  3176. 2:01:10even though that's the same amount of
  3177. 2:01:11iterations it gives us different
  3178. 2:01:14values so in here we can just have a
  3179. 2:01:16console right line and this is how it's
  3180. 2:01:18going to work we're going to have our
  3181. 2:01:20first number times about our second
  3182. 2:01:22number and that's going to be equal to
  3183. 2:01:24something so our first number is our
  3184. 2:01:27value of I cuz that will say 1 *
  3185. 2:01:29Something 2 * something and so forth our
  3186. 2:01:33second number is whatever the user has
  3187. 2:01:35typed into the calculation which will be
  3188. 2:01:38number and our third number is going to
  3189. 2:01:40be the actual result of the calculation
  3190. 2:01:43which are these two times together so we
  3191. 2:01:45can have I star number and that should
  3192. 2:01:48be it let's run it and give it a shot
  3193. 2:01:52we can do our 10 times table and if we
  3194. 2:01:55press enter you can see that it's fully
  3195. 2:01:57working this will be our I variable
  3196. 2:01:59going down the side the second number is
  3197. 2:02:01what the user typed in and the third
  3198. 2:02:04number is this multiplied by this gives
  3199. 2:02:06you the output let's run it again with a
  3200. 2:02:09bigger number and it seems to handle
  3201. 2:02:12okay and because this is an integer
  3202. 2:02:14you'll expect to work okay up until
  3203. 2:02:16around 2.1 billion which is a big number
  3204. 2:02:19for some time tables anyway so just just
  3205. 2:02:22a quick recap we looped through from 1
  3206. 2:02:24till 10 instead of from 0 till 9 because
  3207. 2:02:27we made sure that when we do our
  3208. 2:02:29multiplications we don't want to start
  3209. 2:02:31the value at 0er * something we want to
  3210. 2:02:33start the first one at 1 * something and
  3211. 2:02:35we don't want to end at 9 we want to end
  3212. 2:02:37at 10 so using 1 and less than or equal
  3213. 2:02:40to 10 will get us the values that go
  3214. 2:02:42from 1 till 10 and that will make our
  3215. 2:02:45output look a lot more concise we're
  3216. 2:02:47using conditional format in here adding
  3217. 2:02:49the zero and curly braces and then the
  3218. 2:02:51one and then the two and in order to
  3219. 2:02:54insert the values in it's a comma
  3220. 2:02:55delimited list after the speech marks
  3221. 2:02:58the first one goes in the right order
  3222. 2:03:00zero is I one is number two is I times
  3223. 2:03:04the number running that code we'll get
  3224. 2:03:07this output there you
  3225. 2:03:11go so in this exercise I want you to
  3226. 2:03:13have a look and see if you can make the
  3227. 2:03:15Fizz bus game fiz buuz game is one of
  3228. 2:03:18the most popular interview questions
  3229. 2:03:19you'll come across
  3230. 2:03:21and the way to do this is to create a
  3231. 2:03:23for loop from one to something in this
  3232. 2:03:25case I'm just going to use 15 and inside
  3233. 2:03:27the for Loop you want to decide if the
  3234. 2:03:29numbers divisible by three and five then
  3235. 2:03:31you want to print Fizz buzz if it's
  3236. 2:03:33divisible by three then it's Fizz five
  3237. 2:03:36it's buzz and if it doesn't fall into
  3238. 2:03:38any of them categories then you want to
  3239. 2:03:40simply just print the number out to the
  3240. 2:03:41screen let's get started so we can have
  3241. 2:03:44a four and because we know how many we
  3242. 2:03:46want to do we want to do in I less than
  3243. 2:03:5015 i++
  3244. 2:03:53and then inside here we need to use the
  3245. 2:03:55modulus operator which is the percentage
  3246. 2:03:57sign so if you remember from the
  3247. 2:03:59tutorial in order to figure out if a
  3248. 2:04:01number is odd or even you could do the
  3249. 2:04:03number modulus 2 and if that value is
  3250. 2:04:06one then it must mean it's odd and if
  3251. 2:04:09that value is zero it must mean it's
  3252. 2:04:11even but what we can also differentiate
  3253. 2:04:14from here is if 10 modulus 2 equals 0
  3254. 2:04:17then that means that 10 is divisible by
  3255. 2:04:202 but also it is even and if it's even
  3256. 2:04:24that means it's divisible by two if it's
  3257. 2:04:26not divisible by two that means it's not
  3258. 2:04:28even and therefore it's odd so in this
  3259. 2:04:32case if we try to divide three and five
  3260. 2:04:34we can have the same calculation but we
  3261. 2:04:37can work out differently so 10 modulus 3
  3262. 2:04:40actually gives us a value of one because
  3263. 2:04:42it's three remainder one so if this
  3264. 2:04:44value is anything other than zero then
  3265. 2:04:47we know that these two were not
  3266. 2:04:48divisible by each other 9 percentage 3
  3267. 2:04:51equal 0 which means it's divisible by it
  3268. 2:04:54so in our if statement we can say if I
  3269. 2:04:57which is the number that we're currently
  3270. 2:04:58up to
  3271. 2:05:00percentage 3 equals equals to zero and
  3272. 2:05:04because we want to check for five as
  3273. 2:05:06well we want to say and I percentage 5
  3274. 2:05:09equal equals to Zer and that means
  3275. 2:05:11they're both divisible by each other so
  3276. 2:05:14we can do a console right line and we
  3277. 2:05:15can say fiz buzz and then in our lse if
  3278. 2:05:19we're going to check if it's three and
  3279. 2:05:21we can repurpose this exact condition
  3280. 2:05:23because this just checks if it's
  3281. 2:05:25divisible by three and then we can print
  3282. 2:05:28Fizz and then if we copy this exact
  3283. 2:05:30structure down and change this to a five
  3284. 2:05:32to check if it's divisible by five then
  3285. 2:05:34we can print out buzz and in the worst
  3286. 2:05:37case scenario where none of them get
  3287. 2:05:39applied then we can simply print I to
  3288. 2:05:41the screen and actually there's no point
  3289. 2:05:44in starting at zero because normally
  3290. 2:05:46these start at one so we'll just do one
  3291. 2:05:48and we can make it 15 by making it less
  3292. 2:05:50than or equal to so instead of before it
  3293. 2:05:53was actually going from 0 to 14 now it's
  3294. 2:05:55going from 1 to 15 and they will tell
  3295. 2:05:58you this in the interview they might say
  3296. 2:06:00counting from 1 to 100 print out all of
  3297. 2:06:03the Fizz buzzers so let's run the code
  3298. 2:06:05and see what's going
  3299. 2:06:07on one two fizz fizz is actually three
  3300. 2:06:11so three is divisible by three then you
  3301. 2:06:13have four and then five is divisible by
  3302. 2:06:16five so you get buzz and six goes into
  3303. 2:06:19three so you get Fizz nine you get Fizz
  3304. 2:06:22because it's divisible by three then 10
  3305. 2:06:24/ 5 gives you buzz 11 12 goes into three
  3306. 2:06:28so you've got Fizz and finally when you
  3307. 2:06:30hit 15 it goes into three and goes into
  3308. 2:06:34five so we get Fizz buzz and the way to
  3309. 2:06:36increase this to 100 is by simply
  3310. 2:06:38changing this to 100 if you need to and
  3311. 2:06:41you can scroll through the entire input
  3312. 2:06:42and you can see all the history if you
  3313. 2:06:44wanted to make this slightly efficient
  3314. 2:06:46what you could actually do is store the
  3315. 2:06:48Boolean values for these calculations
  3316. 2:06:50before you can store the values for
  3317. 2:06:52these calculations before so we can say
  3318. 2:06:56bu 3 div equals false and bull 5 div
  3319. 2:07:01equals false and what we can do at the
  3320. 2:07:04start of each of the for Loop is we can
  3321. 2:07:05say three div equals this value and five
  3322. 2:07:09div also equals this
  3323. 2:07:12value and then every time we find a
  3324. 2:07:15calculation for three or calculation for
  3325. 2:07:18five we can replace it with either three
  3326. 2:07:20div or five div so let's change this one
  3327. 2:07:23here and this one here and our five here
  3328. 2:07:28and here and because these are values
  3329. 2:07:31now we want to actually store them as
  3330. 2:07:33true or falses so we don't need to check
  3331. 2:07:35if they're equal to zero so we can just
  3332. 2:07:37remove them so we can say if three div
  3333. 2:07:40and five div are both true and they'll
  3334. 2:07:42only be true if these two are both zero
  3335. 2:07:45now if we run the code and just make it
  3336. 2:07:4715 again just we have a shorter output
  3337. 2:07:50you can see that our code is exact the
  3338. 2:07:52same the reason why this is more
  3339. 2:07:54efficient is we're only doing this
  3340. 2:07:55calculation twice whereas before we were
  3341. 2:07:58doing it twice for three and twice for
  3342. 2:08:00five now we're doing it once storing it
  3343. 2:08:04and then making use of it this is a lot
  3344. 2:08:06more memory efficient than having to
  3345. 2:08:07calculate it every single time it
  3346. 2:08:09computes a lot faster and it's better
  3347. 2:08:11for readability if the rules of the game
  3348. 2:08:14happen to change and this was divisible
  3349. 2:08:15by four or six then you could just
  3350. 2:08:18change it very easily whereas in the
  3351. 2:08:20previous example you would have to
  3352. 2:08:22change in every single
  3353. 2:08:24case in C when you're constructing
  3354. 2:08:27strings that have backs slashes you're
  3355. 2:08:30going to need to use double backslash
  3356. 2:08:32and the reason for this is the first
  3357. 2:08:34backslash is an indication that it's
  3358. 2:08:36actually going to be an escape character
  3359. 2:08:38you're going to put an escape character
  3360. 2:08:40in and then put the character that you
  3361. 2:08:42want to display and the reason for that
  3362. 2:08:44is they've chose backslash as the
  3363. 2:08:46designated character so you can use
  3364. 2:08:49backslash and then a backs slash to to
  3365. 2:08:51show one backslash only and what this
  3366. 2:08:54means is it will use the first backslash
  3367. 2:08:56as a reference so it knows that it's
  3368. 2:08:59going to process the next character as
  3369. 2:09:01an escape character and what these
  3370. 2:09:03Escape characters are you can have
  3371. 2:09:05things like T for tab and for new line
  3372. 2:09:09and you can even use speech marks
  3373. 2:09:12because if you outputting some dialogue
  3374. 2:09:14and you wanted to say something like
  3375. 2:09:16this he
  3376. 2:09:18said and then you added your own quote
  3377. 2:09:21in there if you want to put something in
  3378. 2:09:24quotes then you can't because the speech
  3379. 2:09:26mark dictates that this is the end of
  3380. 2:09:28the string literal that's why it's cting
  3381. 2:09:31it off like this and then now it's not
  3382. 2:09:32recognizing that something is just WR
  3383. 2:09:34here and what it's actually doing is
  3384. 2:09:37you've got two strings specified one
  3385. 2:09:39that says he said space and one that's
  3386. 2:09:42just empty and then you just have this
  3387. 2:09:44something that's just in the middle of
  3388. 2:09:46course we actually want to insert the
  3389. 2:09:48speech marks within another pair of
  3390. 2:09:50speech marks marks this is where the
  3391. 2:09:52backslash comes in so you can insert a
  3392. 2:09:54backslash before here and this is now
  3393. 2:09:57acceptable and the same thing with the
  3394. 2:09:59normal backslash because the backslash
  3395. 2:10:02character is also going to be the Escape
  3396. 2:10:04character then you need a double
  3397. 2:10:06backslash so then that equals to a
  3398. 2:10:08single backslash when you print it out
  3399. 2:10:11let's Show an example so we have C colon
  3400. 2:10:14sl/ slash which will return us back a
  3401. 2:10:16single slash but visually right now it's
  3402. 2:10:19a double slash but when it gets
  3403. 2:10:21processed or outputed to the screen
  3404. 2:10:23you'll see that it's a single slash so
  3405. 2:10:25this common example is when you're
  3406. 2:10:27constructing a path together and you
  3407. 2:10:29want to use backslashes to denote the
  3408. 2:10:31different directories so let's output
  3409. 2:10:34this to the screen and see what
  3410. 2:10:36happens and as you can see we have a
  3411. 2:10:38single backslash because of the Escape
  3412. 2:10:40character we don't see both of them and
  3413. 2:10:43if we print the speech out to the screen
  3414. 2:10:45just to show you that as well then we
  3415. 2:10:47can see that there's only the speech
  3416. 2:10:48marks around the something and we don't
  3417. 2:10:50have the backslash you can tell when an
  3418. 2:10:53escape character is being used because
  3419. 2:10:55it goes a slight hint of yellow instead
  3420. 2:10:58of the normal color for a string so now
  3421. 2:11:00that we've worked out that this is how
  3422. 2:11:02this works let's see a way that we can
  3423. 2:11:05undo this to make it easier for us so
  3424. 2:11:08you've used the dollar sign for string
  3425. 2:11:09interpolation and you've used the plus
  3426. 2:11:12just to concatenate two variables
  3427. 2:11:14together and just as a quick example it
  3428. 2:11:16looked something like this your name is
  3429. 2:11:19and then the name variable and using the
  3430. 2:11:22plus you would say your name is followed
  3431. 2:11:25by a space and the quotation marks and
  3432. 2:11:27do plus name that's fine and now we have
  3433. 2:11:30another one so similar to how we place a
  3434. 2:11:33dollar sign before the speech marks we
  3435. 2:11:35have a verbum identifier and what this
  3436. 2:11:38means because it says verbatim it just
  3437. 2:11:40means whatever you see is whatever
  3438. 2:11:42happens so let's try and Define this
  3439. 2:11:44path a slightly different way but still
  3440. 2:11:47containing the exact same information
  3441. 2:11:49that you see when you print out to the
  3442. 2:11:50screen
  3443. 2:11:52so let's have a look we can reassign the
  3444. 2:11:55path value and just before the speech
  3445. 2:11:57marks we can put it at symbol and this
  3446. 2:12:00at symbol means it will actually negate
  3447. 2:12:03all of the Escape characters it will
  3448. 2:12:05process a single backlash as a single
  3449. 2:12:08backslash let's take this line of code
  3450. 2:12:11and place inside here now what you see
  3451. 2:12:14is that tint of yellow is no longer
  3452. 2:12:16there so if we print this past to the
  3453. 2:12:18screen let's see what
  3454. 2:12:20happens we actually get the double
  3455. 2:12:22backslash because it's ignoring all the
  3456. 2:12:24Escape characters which means if we take
  3457. 2:12:26them all out this actually looks exactly
  3458. 2:12:29how we want and you don't have to worry
  3459. 2:12:31about the Escape
  3460. 2:12:33characters there you
  3461. 2:12:37go these two are identical even though
  3462. 2:12:40one is being displayed using Escape
  3463. 2:12:41characters and one's using the verbatim
  3464. 2:12:44identifier please note that if you're
  3465. 2:12:46going to use the back sln for example
  3466. 2:12:49and I'll just demonstrate at the end of
  3467. 2:12:50this new line test and if we run that
  3468. 2:12:53code again you'll see that it's printed
  3469. 2:12:55the path on one line the back sln has
  3470. 2:12:58made it go on to the next line and now
  3471. 2:13:00it's printing new line test on a
  3472. 2:13:02dedicated line that's fine the problem
  3473. 2:13:06is if you use a back sln or a back SLT
  3474. 2:13:09or any of these Escape characters during
  3475. 2:13:12the at symbol then it's going to process
  3476. 2:13:14it as literal characters which means you
  3477. 2:13:16can't mix a back sln with an at symbol
  3478. 2:13:20AS this is just taking the text verbatim
  3479. 2:13:23so if we try and run the code now you'll
  3480. 2:13:24see the back sln will actually appear in
  3481. 2:13:27the code if you wanted to do this then
  3482. 2:13:30maybe you could remove the back sln and
  3483. 2:13:32add a plus at the end and then make
  3484. 2:13:34another string that just has a back
  3485. 2:13:36slash n and then this string will not be
  3486. 2:13:39processed as a Verbatim string because
  3487. 2:13:41we don't have the at symbol right before
  3488. 2:13:44it so then we can have the exact same
  3489. 2:13:46thing here just to show you
  3490. 2:13:49this so now the these two are identical
  3491. 2:13:52again just as shown here and as shown
  3492. 2:13:56here the difference being that we can't
  3493. 2:13:59specify a back sln in the same string we
  3494. 2:14:01actually have to concatenate them
  3495. 2:14:03together because of the verbatim
  3496. 2:14:05identifier so let's have a look at
  3497. 2:14:07speech
  3498. 2:14:09marks so if we have a string name and we
  3499. 2:14:12can make this equal to hello and then
  3500. 2:14:15someone and we said before that we have
  3501. 2:14:18to use the backslash so we can activate
  3502. 2:14:20the Escape characters and that will work
  3503. 2:14:22and if we just print that to the screen
  3504. 2:14:24to make sure then we get hello someone
  3505. 2:14:26in the speech marks if we add the verbum
  3506. 2:14:29identify at the start then we see we've
  3507. 2:14:31got an error it's trying to process it
  3508. 2:14:35differently this is a little Quirk with
  3509. 2:14:37the verbatim identifier if you want to
  3510. 2:14:39insert a speech marks you actually want
  3511. 2:14:41a double speech mark on either side the
  3512. 2:14:44double speech marks will get processed
  3513. 2:14:46as a single speech mark because it's
  3514. 2:14:48within a speech mark the verb identifier
  3515. 2:14:51needs to know that you're specifying a
  3516. 2:14:53speech mark and this acts as the same
  3517. 2:14:56reason why you have a backslash here
  3518. 2:14:58it's slightly quirky doing it this way
  3519. 2:15:00but it still works as per you
  3520. 2:15:02expect and when you're specifying
  3521. 2:15:05something like name and we can say hello
  3522. 2:15:08someone so let's print name to the
  3523. 2:15:09screen and make sure it works in this
  3524. 2:15:12case we don't actually need to specify
  3525. 2:15:13any sort of Escape character because the
  3526. 2:15:16quotation mark is a separate character
  3527. 2:15:18to the speech mark and it doesn't
  3528. 2:15:20interrupt anything by placing a
  3529. 2:15:22quotation mark inside the string just
  3530. 2:15:24represents that as its own character
  3531. 2:15:26let's run that and make sure it works
  3532. 2:15:28there you go so just as a quick recap
  3533. 2:15:32Escape characters are very essential
  3534. 2:15:34because we can use things like back SLT
  3535. 2:15:36to give us a tab character in our
  3536. 2:15:38console output this can be very helpful
  3537. 2:15:40cuz you can almost mimic the use of
  3538. 2:15:42columns inside our console output you'll
  3539. 2:15:45notice that back sln is used quite a lot
  3540. 2:15:48in the console output so you can
  3541. 2:15:49separate lines without having to use
  3542. 2:15:51multiple console right
  3543. 2:15:53lines if you have a single backslash you
  3544. 2:15:56can't have it on its own if the string
  3545. 2:15:58is not a Verbatim string you have to
  3546. 2:16:01pair it with another character otherwise
  3547. 2:16:03you will get a syntax error if you want
  3548. 2:16:05to use the verbatim string then you can
  3549. 2:16:07put an add symbol before the string
  3550. 2:16:09definition and then it will ignore all
  3551. 2:16:11of the Escape characters for example
  3552. 2:16:14using backs sln Within These speech
  3553. 2:16:16marks will mean that the back sln will
  3554. 2:16:18literally be printed to the screen and
  3555. 2:16:20you won't get get the new line
  3556. 2:16:21functionality that backlash M provides
  3557. 2:16:24when you want to print speech marks to
  3558. 2:16:26the screen using the verbatim identifier
  3559. 2:16:28you have to use a double speech marks in
  3560. 2:16:30order for it to show up and as you can
  3561. 2:16:32see it slightly goes yellow and that's
  3562. 2:16:34the similar notation as using Escape
  3563. 2:16:36character when you don't have an a
  3564. 2:16:38Verbatim string if you're using other
  3565. 2:16:40characters that are similar to a speech
  3566. 2:16:42mark like a quotation mark because this
  3567. 2:16:45doesn't interrupt the syntax of the
  3568. 2:16:47language then this is fine because a
  3569. 2:16:49character can just be in inside a string
  3570. 2:16:52and to the compiler this is just a
  3571. 2:16:54character even though they are very
  3572. 2:16:55similar the quotation mark doesn't
  3573. 2:16:58affect the speech
  3574. 2:17:01mark the most common approach for
  3575. 2:17:03outputting values to the console is by
  3576. 2:17:05having this format you type something in
  3577. 2:17:08as a static string and then you add a
  3578. 2:17:10variable to it but when you're doing
  3579. 2:17:12long sentences this format can get messy
  3580. 2:17:15quite quickly so this will just output
  3581. 2:17:17both of these on separate lines the name
  3582. 2:17:20follow follow by the name variable and
  3583. 2:17:22the age followed by the age variable I
  3584. 2:17:25just have a console right line to split
  3585. 2:17:26up these two sections just so you can
  3586. 2:17:28see it visually in the console so if we
  3587. 2:17:30run this code you can see that both the
  3588. 2:17:33outputs are exactly the same and the
  3589. 2:17:36reason why is we can use this back sln
  3590. 2:17:39what this back sln is doing is inserting
  3591. 2:17:41a new line character just where my
  3592. 2:17:43cursor is here which is forcing the age
  3593. 2:17:46to jump onto the next line the reason
  3594. 2:17:48why this is a slightly different shade
  3595. 2:17:50to the age text is because it's an
  3596. 2:17:52Escape character back sln means new line
  3597. 2:17:56we'll explain this in a further video in
  3598. 2:17:58this section so don't worry about that
  3599. 2:18:00just know that it means that it produces
  3600. 2:18:02a new line so as you can see since we're
  3601. 2:18:04adding the name followed by the name and
  3602. 2:18:06then the age followed by the age then
  3603. 2:18:08it's getting a bit long and a little bit
  3604. 2:18:10tedious it's not too bad in this case
  3605. 2:18:13because we're not really printing out a
  3606. 2:18:14sentence we're just going straight to
  3607. 2:18:16another line but let's see what this is
  3608. 2:18:18going to look like if we start dealing
  3609. 2:18:20with more multiple pits of data your
  3610. 2:18:23name is followed by the name and then we
  3611. 2:18:26can add a comma and your age is and then
  3612. 2:18:31add the age and as you can see while
  3613. 2:18:33we're structuring a sentence then it
  3614. 2:18:35gets a little bit long we have to
  3615. 2:18:37remember to put in the spaces otherwise
  3616. 2:18:40our code is going to look funky when we
  3617. 2:18:42print out to the screen and the reason
  3618. 2:18:44for that is there's no spaces between
  3619. 2:18:46the name and the age variables so let's
  3620. 2:18:49put them back in and make sure the
  3621. 2:18:51output looks all
  3622. 2:18:53right now that the output looks like
  3623. 2:18:55this let's explore composite formatting
  3624. 2:18:57and what this means is we can get rid of
  3625. 2:18:59all of these plus symbols and replace it
  3626. 2:19:02with curly braces so let's actually
  3627. 2:19:04output this string again but just output
  3628. 2:19:06it completely static your name is
  3629. 2:19:09abber and your age is
  3630. 2:19:1223 and now if we've run this you'll see
  3631. 2:19:15that both outputs are identical this is
  3632. 2:19:18because I know the values for the string
  3633. 2:19:19variables appear here and I can input it
  3634. 2:19:22straight into here so now that we know
  3635. 2:19:24what our sentence is going to be we
  3636. 2:19:26don't need to add any pluses what we do
  3637. 2:19:28need to do is remove our variables so in
  3638. 2:19:31the case of this sentence our name
  3639. 2:19:33variable is here and our age variable is
  3640. 2:19:35here so we can replace this with this
  3641. 2:19:38curly brace notation we can have curly
  3642. 2:19:41braces and then we want to start this
  3643. 2:19:43value at
  3644. 2:19:44zero throughout the string you want to
  3645. 2:19:47have these values starting from zero and
  3646. 2:19:50going up incrementally from left to
  3647. 2:19:52right so our next variable would be our
  3648. 2:19:5523 and we can store it like this now
  3649. 2:19:59that it's got your name zero and your
  3650. 2:20:01age one let's see what happens when you
  3651. 2:20:03output it then we just get this and this
  3652. 2:20:05is very confusing that's because we're
  3653. 2:20:08not done yet so now that we've got our
  3654. 2:20:10string typed in we can actually place a
  3655. 2:20:13comma in here and now it lets us put any
  3656. 2:20:16object in and any object could be a
  3657. 2:20:18string an in a double or any data type
  3658. 2:20:21so we want to insert the values inside
  3659. 2:20:24here going from left to right so our
  3660. 2:20:26name is first and then we do a comma and
  3661. 2:20:29we type in Age and what this now says is
  3662. 2:20:32the name will get replaced by the curly
  3663. 2:20:34brace zero and the age will get replaced
  3664. 2:20:37by the curly brace one so now if we run
  3665. 2:20:40the code let's see what happens and now
  3666. 2:20:42you'll see that the output is exactly
  3667. 2:20:44the same and now look at the difference
  3668. 2:20:46between these two lines you'll see that
  3669. 2:20:49these multiple amount of Plus es and not
  3670. 2:20:51confusing and let's say you forget this
  3671. 2:20:53one then you're going to get a syntax
  3672. 2:20:55error and it's a bit frustrating to keep
  3673. 2:20:57on typing when you're trying to join
  3674. 2:20:59things together now we can clearly see
  3675. 2:21:01that this Flo is like a sentence and
  3676. 2:21:03this space here looks very unnatural but
  3677. 2:21:06this space here looks like it's part of
  3678. 2:21:08the sentence which makes our code a bit
  3679. 2:21:10more
  3680. 2:21:11complete and the same goes if you want
  3681. 2:21:13to print something like this and let's
  3682. 2:21:15convert this one so if we copy this line
  3683. 2:21:18down into here and we just remove the
  3684. 2:21:20spaces then it can look something like
  3685. 2:21:24this name followed by the name we don't
  3686. 2:21:27actually need this space here because
  3687. 2:21:28we're going to be going into a brake
  3688. 2:21:30line we can replace our name with curly
  3689. 2:21:32braces zero and our age with curly
  3690. 2:21:35braces one and then straight after we
  3691. 2:21:38can type name and age and if we output
  3692. 2:21:41this we'll see it's exactly the same
  3693. 2:21:43there we go so just a quick recap
  3694. 2:21:47printing strings and any other variables
  3695. 2:21:48to the screen has been not normally
  3696. 2:21:50achieved by doing a string followed by a
  3697. 2:21:53plus and then adding another variable to
  3698. 2:21:55it adding more than one can be a little
  3699. 2:21:58bit complicated and hard to track of all
  3700. 2:22:01the spaces that you're going to put in
  3701. 2:22:02and you might accidentally miss one of
  3702. 2:22:04them like this one here or this one here
  3703. 2:22:07are very crucial to the output if you
  3704. 2:22:09miss this one then it's going to say is
  3705. 2:22:11followed by the name all together with
  3706. 2:22:14no space we then introduce composite
  3707. 2:22:16formatting which lets us use this
  3708. 2:22:18notation of curly bra Z
  3709. 2:22:21and curly brace one to give us the
  3710. 2:22:23ability to store some variables outside
  3711. 2:22:25of the string and then insert them in
  3712. 2:22:29dynamically we took both of these
  3713. 2:22:31examples and made them down here using
  3714. 2:22:34composite
  3715. 2:22:36formatting so let's explore a new method
  3716. 2:22:38of concatenation that makes this look a
  3717. 2:22:40lot better the reason why this is an
  3718. 2:22:42issue is you have spaces in here and
  3719. 2:22:45sometimes you might forget these spaces
  3720. 2:22:47and it could mess up your program this
  3721. 2:22:49string here looks very very unnatural
  3722. 2:22:51because you're forcing the space but
  3723. 2:22:53when you run it you'll see that it is
  3724. 2:22:55actually what you expect the spaces are
  3725. 2:22:57complete and it makes perfect sense but
  3726. 2:23:00when you're writing it it kind of looks
  3727. 2:23:02a little bit stupid because you're
  3728. 2:23:04forcing the space and then adding a plus
  3729. 2:23:06and then the name and then another plus
  3730. 2:23:08and then another static string and this
  3731. 2:23:10just get a little bit long to type it's
  3732. 2:23:13fine when you have a smaller sentence
  3733. 2:23:14like this but when the program starts to
  3734. 2:23:16grow then it could be a little bit
  3735. 2:23:18problematic and you'll have a lot of
  3736. 2:23:19syntax ER is when you forget to type in
  3737. 2:23:21all the pluses luckily this can be fixed
  3738. 2:23:24let's duplicate this line down and let's
  3739. 2:23:26see what we can do so we have this thing
  3740. 2:23:29called string interpolation and it uses
  3741. 2:23:31a dollar sign as a reference we can
  3742. 2:23:34place the dollar sign before the
  3743. 2:23:35quotation marks and then what you can do
  3744. 2:23:38is you can actually keep this as one big
  3745. 2:23:40sentence so let's remove all of the
  3746. 2:23:43values and have it like this so now of
  3747. 2:23:46course this actually doesn't print out
  3748. 2:23:47variables because we've just replaced it
  3749. 2:23:49with text so if we just run this then
  3750. 2:23:51it's completely messed up because now
  3751. 2:23:53we're not using our variables and this
  3752. 2:23:56can be easily fixed because we've used
  3753. 2:23:58our dollar sign now we can actually wrap
  3754. 2:24:00our variables in this curly brace and
  3755. 2:24:03now you can see it went white if I
  3756. 2:24:05double click on it you can see that it's
  3757. 2:24:06referencing the
  3758. 2:24:08variable wrapping the variables in the
  3759. 2:24:10curly brace means that they can actually
  3760. 2:24:12be processed variables even though they
  3761. 2:24:14are still within the speech marks please
  3762. 2:24:16do not remove the dollar sign because
  3763. 2:24:18now this will be interpreted as string
  3764. 2:24:20text as you can see here putting a
  3765. 2:24:23dollar sign at the start will make into
  3766. 2:24:25string interpolation and then we get the
  3767. 2:24:27ability to have our brackets this can
  3768. 2:24:30make string concatenation very easy to
  3769. 2:24:32do because you don't have to worry about
  3770. 2:24:34all the pluses and adding them together
  3771. 2:24:37and don't have to worry about the spaces
  3772. 2:24:38as well this looks more like a natural
  3773. 2:24:41sentence and what you can do for
  3774. 2:24:43practice is you can actually just type
  3775. 2:24:44out the sentence your name is Aba your
  3776. 2:24:48age is 23 and then from there you can
  3777. 2:24:51actually just break down the variables
  3778. 2:24:53and know that you'll need one here so
  3779. 2:24:55you can replace that with name and then
  3780. 2:24:57you'll know you'll need one here so you
  3781. 2:24:58can replace that one with age and then
  3782. 2:25:00you can place the dollar sign right at
  3783. 2:25:02the end and then there you go you have
  3784. 2:25:04your solution this method means that
  3785. 2:25:07it's a lot cleaner to read easier to
  3786. 2:25:09maintain and make changes in the future
  3787. 2:25:12and that's the whole point of
  3788. 2:25:13programming if your code base is very
  3789. 2:25:15difficult to go around and understand
  3790. 2:25:17what's going on and potentially make
  3791. 2:25:19changes in the future then you've
  3792. 2:25:20already made a bad step and you don't
  3793. 2:25:22want to get into these nasty habits it's
  3794. 2:25:25fine if you're just going to have
  3795. 2:25:26something like this where it's not
  3796. 2:25:27really complicated at all a string plus
  3797. 2:25:30another string that's quite simple and
  3798. 2:25:32that's fine you don't have to go the
  3799. 2:25:34extra effort to put in the dollar sign
  3800. 2:25:36and wrap it around in the curly braces
  3801. 2:25:38even though I personally still would
  3802. 2:25:40because in my personal opinion this
  3803. 2:25:42looks a lot
  3804. 2:25:44better than having it like that you can
  3805. 2:25:47see that it's a lot neater it takes up
  3806. 2:25:49less code and you can see visually that
  3807. 2:25:51you're using a specific variable and you
  3808. 2:25:54don't have to worry too much about the
  3809. 2:25:55spaces because this looks like a real
  3810. 2:26:00sentence another method of string
  3811. 2:26:02concatenation which is the idea of
  3812. 2:26:04taking strings or objects and add them
  3813. 2:26:06together to form a sentence of sorts we
  3814. 2:26:08can say your name is followed by space
  3815. 2:26:11and then add the name followed by space
  3816. 2:26:14add your ages and then add the actual
  3817. 2:26:16age variable and we can also write the
  3818. 2:26:18exact same line using string
  3819. 2:26:20interpolation or composite formatting
  3820. 2:26:23just like this and like this we can
  3821. 2:26:26specify the dollar symbol and put our
  3822. 2:26:28variable in the bracket which is shown
  3823. 2:26:31here and here and for the composite
  3824. 2:26:33formatting we can take the content where
  3825. 2:26:35our variable would go and replace it
  3826. 2:26:37with incremental numbers 0er and one
  3827. 2:26:40wrapped around in a curly brace and then
  3828. 2:26:42specify in the correct order zero takes
  3829. 2:26:45a value of name and one takes a value of
  3830. 2:26:48age if we run this code you'll see that
  3831. 2:26:50that the outputs are
  3832. 2:26:51identical so let's explore this other
  3833. 2:26:54method we have a string test for example
  3834. 2:26:57and then inside the string Library we
  3835. 2:26:59have this function called concat and it
  3836. 2:27:01can takeing any amount of parameters
  3837. 2:27:03that we want to give it so we could say
  3838. 2:27:06your name is space and the quotation
  3839. 2:27:09marks and then we can add a comma and
  3840. 2:27:12then we can say we want to add the name
  3841. 2:27:14and then another comma and we want to
  3842. 2:27:16add and your
  3843. 2:27:17ages and the speech marks add add a
  3844. 2:27:20comma add the age variable and that's it
  3845. 2:27:23and we could just use CW tab tab and
  3846. 2:27:25type in test and let's run it and see
  3847. 2:27:27what happens and now as you can see we
  3848. 2:27:30get the exact same output and because
  3849. 2:27:32this just returns us back with the
  3850. 2:27:34string we can also just take this entire
  3851. 2:27:36line and paste it straight into the
  3852. 2:27:38console right line you don't necessarily
  3853. 2:27:40have to store it but you can if you want
  3854. 2:27:41to and there you go all methods produce
  3855. 2:27:44the same output another way you can use
  3856. 2:27:46this concap function is by giving it
  3857. 2:27:48array we haven't covered array so far so
  3858. 2:27:51I'm just going to show you this for
  3859. 2:27:52demonstration purposes don't worry about
  3860. 2:27:54understanding it so just a quick
  3861. 2:27:56explanation arrays are just ways of
  3862. 2:27:58storing multiple of the same data type
  3863. 2:28:00so if we have these three names inside
  3864. 2:28:03our names we can actually use the
  3865. 2:28:05console to print them all out so we can
  3866. 2:28:08say string. concat and then we can
  3867. 2:28:10concat the names together which will
  3868. 2:28:12just add all of these three together now
  3869. 2:28:14the problem is when you run it you'll
  3870. 2:28:16see they're stuck together so what you
  3871. 2:28:18could do is just add a space between
  3872. 2:28:20them and as you can see this is getting
  3873. 2:28:22a little bit tedious
  3874. 2:28:23now there's other methods that we can
  3875. 2:28:26use to concat some arrays together and
  3876. 2:28:28I'll show you that in the next section
  3877. 2:28:29but for now don't worry about it this is
  3878. 2:28:31concentrating on the string concat
  3879. 2:28:33function and we'll just demonstrate that
  3880. 2:28:35again so if we run the line of code we
  3881. 2:28:37can see that they're all the same so now
  3882. 2:28:39that you have four different methods of
  3883. 2:28:41printing out exactly the same
  3884. 2:28:42information you might be asking yourself
  3885. 2:28:45what's the best method I personally
  3886. 2:28:47prefer to use the string interpolation
  3887. 2:28:49because with this one you can easily
  3888. 2:28:51miss the spaces and you could produce a
  3889. 2:28:53bad output and it might be stuck
  3890. 2:28:55together it's fine when you can see the
  3891. 2:28:57lines right in front of you and you can
  3892. 2:28:59easily identify the spaces but when
  3893. 2:29:01you're adding about 10 variables
  3894. 2:29:03together then it could be a problem the
  3895. 2:29:05reason why like string interpolation
  3896. 2:29:07using the dollar signs and the variables
  3897. 2:29:09in the curly braces is that you don't
  3898. 2:29:11have to rely on missing out spaces
  3899. 2:29:13because it actually forms a sentence and
  3900. 2:29:15you just replace the values with your
  3901. 2:29:17variable composite format in which is
  3902. 2:29:19this one is very efficient and very nice
  3903. 2:29:21to look at because it allows you to
  3904. 2:29:24specify the sentence and then plug in
  3905. 2:29:26the variables after the only problem
  3906. 2:29:28with this is if you were to change this
  3907. 2:29:29value in the future or to change a
  3908. 2:29:31sentence you have to make sure that the
  3909. 2:29:33numbers add up just like in the first
  3910. 2:29:35example if you only have a short
  3911. 2:29:37sentence then this isn't really an issue
  3912. 2:29:39since you can see it all visually and
  3913. 2:29:41you also got to realize that these need
  3914. 2:29:42to be in the right order because if you
  3915. 2:29:44put Age first then it's going to put age
  3916. 2:29:46in this one and name in the second one
  3917. 2:29:48and that's going to be problematic
  3918. 2:29:50because it now mess up the sentence
  3919. 2:29:52using string interpolation you don't
  3920. 2:29:54actually have to worry about anything
  3921. 2:29:56because if you wanted to change this to
  3922. 2:29:57say your age is then you simply just do
  3923. 2:30:00this and that's very easy to do because
  3924. 2:30:02in this case you'd say your age is and
  3925. 2:30:05then you'd have to change a string and
  3926. 2:30:07then now that this is zero we need to
  3927. 2:30:09change this from being age and this one
  3928. 2:30:11to be name whereas in the first example
  3929. 2:30:14you could just easily change the value
  3930. 2:30:16in the curly braces instead of changing
  3931. 2:30:18this reference and the end reference
  3932. 2:30:20conar is not widely used just because
  3933. 2:30:23you have all of these methods available
  3934. 2:30:25where you can easily add stuff together
  3935. 2:30:27but there could be user cases later on
  3936. 2:30:28down the line where uses just so it's a
  3937. 2:30:31bit more obvious and it helps the
  3938. 2:30:32readability of your code and maintain
  3939. 2:30:35within a big project or just on your
  3940. 2:30:37personal projects when you're looking
  3941. 2:30:38back at your previous
  3942. 2:30:41code when you create an initialized a
  3943. 2:30:44string variable you might want to give
  3944. 2:30:45it the value of an empty quot and this
  3945. 2:30:47will just default the value to
  3946. 2:30:49essentially nothing you're giving it a
  3947. 2:30:51value but at the same time the value
  3948. 2:30:52doesn't actually contain any sort of
  3949. 2:30:54characters if we print this out to the
  3950. 2:30:56screen then you won't really see
  3951. 2:30:57anything this video is going to focus on
  3952. 2:30:59string.empty and string. empty will
  3953. 2:31:02actually just represent these quotation
  3954. 2:31:03marks string.empty will represent these
  3955. 2:31:06speech marks so what we can do is we can
  3956. 2:31:08type in string using a DOT and then we
  3957. 2:31:11can type in empty just like that we've
  3958. 2:31:13initialize our string name to a value of
  3959. 2:31:16empty now this isn't necessarily used
  3960. 2:31:18when you're declaring an initializing it
  3961. 2:31:21because this is almost identical to just
  3962. 2:31:23defining a string on its own main reason
  3963. 2:31:25why this is used let's say we actually
  3964. 2:31:27ask them user to input their name so we
  3965. 2:31:29can say a console. right enter your name
  3966. 2:31:32and then we can take this string
  3967. 2:31:33variable move it down using alt and then
  3968. 2:31:36arrow keys and remove this and let's
  3969. 2:31:39initialize it to a console. readline to
  3970. 2:31:41read in the value from the
  3971. 2:31:43console and now what we can do is using
  3972. 2:31:45our is string interpolation method that
  3973. 2:31:48we just learned we can print to the user
  3974. 2:31:50your name is and then the name in The
  3975. 2:31:51Curly braces now let's just see what
  3976. 2:31:54happens so let's actually type our name
  3977. 2:31:56in abber and it says your name is ABBA
  3978. 2:31:59but what if the user just types nothing
  3979. 2:32:01and just presses enter Then it still
  3980. 2:32:02says your name is blank now let's see
  3981. 2:32:05what we can do here we don't necessarily
  3982. 2:32:06want to say your name is blank we want
  3983. 2:32:08to check what this value is so we can
  3984. 2:32:10say if the name is not equal to empty
  3985. 2:32:13quotation marks then we can output your
  3986. 2:32:15name is and then then else we can say
  3987. 2:32:18name is empty
  3988. 2:32:20and then if we run it now and press
  3989. 2:32:21enter it says name is empty so we can
  3990. 2:32:24use string. empty instead of these
  3991. 2:32:26quotation
  3992. 2:32:28marks this looks a lot better when
  3993. 2:32:30you're trying to compare it your
  3994. 2:32:32name is and then when we press enter
  3995. 2:32:34it says name is empty this just helps to
  3996. 2:32:37keep your code a lot tidier just so you
  3997. 2:32:39don't have to write the two quotes if
  3998. 2:32:41you accidentally put a space in there
  3999. 2:32:43now your if statement is incorrect if
  4000. 2:32:45you always use string. empty then it
  4001. 2:32:47just helps you to make sure that your
  4002. 2:32:49code is going to to be maintainable and
  4003. 2:32:50readable in the future reading string.
  4004. 2:32:53empty gives you a better representation
  4005. 2:32:55of what's going on instead of two open
  4006. 2:32:59quotes if there is a situation where you
  4007. 2:33:02want to check if one string equals
  4008. 2:33:03another string then you can do the
  4009. 2:33:06following if message equals equals
  4010. 2:33:09compare then we can print out same and
  4011. 2:33:12then else we can print out
  4012. 2:33:15different and as we can see here they
  4013. 2:33:17both have a capital H and they both say
  4014. 2:33:19hello so they should be identical let's
  4015. 2:33:22run the code and find out so they say
  4016. 2:33:24same which is exactly what we'd expect
  4017. 2:33:27so we have another method of being able
  4018. 2:33:29to do this so even though we can apply
  4019. 2:33:31equals equals to two strings it's
  4020. 2:33:33normally better practice to use equals
  4021. 2:33:36and then place the other string inside
  4022. 2:33:38the brackets so you're saying if message
  4023. 2:33:40equals compare now if you run the code
  4024. 2:33:43you'll see that it's the same
  4025. 2:33:44output in certain circumstances where
  4026. 2:33:47you want to ask what the user has
  4027. 2:33:48entered enter your name and then we can
  4028. 2:33:52use a string name equals console.
  4029. 2:33:54readline we can say if the name does not
  4030. 2:33:58equal to empty strings then from there
  4031. 2:34:01what we can do is type in your name is
  4032. 2:34:05plus
  4033. 2:34:06name and then we get some output
  4034. 2:34:09your name is but since we're
  4035. 2:34:11checking if it's not equal to that that
  4036. 2:34:13means they've entered something in let's
  4037. 2:34:15see if there's another way that we can
  4038. 2:34:16actually write this so if we just
  4039. 2:34:18duplicate it and comment it out so we
  4040. 2:34:20got it for reference so what we want to
  4041. 2:34:22do is we're checking if name is not
  4042. 2:34:24equal to empty quotes so we can say name
  4043. 2:34:28do equals empty quotes but what this is
  4044. 2:34:31telling us if the name equals empty
  4045. 2:34:33quotes then we should do this so then
  4046. 2:34:35what we can do when we learn about the
  4047. 2:34:37if statements is we can use the not
  4048. 2:34:39operator and we can say if the name not
  4049. 2:34:42equals to empty quotes then we proceed
  4050. 2:34:44with this line else we can just
  4051. 2:34:48say and valid name
  4052. 2:34:51input let's run that and see what
  4053. 2:34:53happens if you type in then it
  4054. 2:34:55passes fine and if we type in nothing
  4055. 2:34:57then we get an invalid name input the
  4056. 2:34:59reason why equals equals and the equals
  4057. 2:35:02function are different is because lower
  4058. 2:35:04level using equals equals will actually
  4059. 2:35:07compare the reference in the memory and
  4060. 2:35:09the reference in the memory is what's
  4061. 2:35:11used up when you actually create these
  4062. 2:35:13variables so what this is doing is it's
  4063. 2:35:16comparing the value that's actually
  4064. 2:35:17stored in memory to the other one that's
  4065. 2:35:19stored in memory now the problem is when
  4066. 2:35:22it comes to something like arrays where
  4067. 2:35:24you actually have the same value but
  4068. 2:35:26they're slightly different let me
  4069. 2:35:27demonstrate so I'm just going to use
  4070. 2:35:29this code because we haven't covered
  4071. 2:35:31arrays yet so I'm just showing you an
  4072. 2:35:33example if we have a chart array that
  4073. 2:35:35literally says hello and then we can use
  4074. 2:35:37this line of code to join them together
  4075. 2:35:40but this is actually an object instead
  4076. 2:35:42of a string but if you see what we can
  4077. 2:35:44do in the if statement we can say if the
  4078. 2:35:46message which is equal to hello we can
  4079. 2:35:49use message. equals and we got the new
  4080. 2:35:51compare and this is actually an object
  4081. 2:35:54so now we're comparing a string with an
  4082. 2:35:56object and generically speaking an
  4083. 2:35:59object can hold any sort of value
  4084. 2:36:01because all of these variables that are
  4085. 2:36:03strings in double Etc they're all deriv
  4086. 2:36:05from something called an object which is
  4087. 2:36:07just a very generic way of storing
  4088. 2:36:09information so since these two data
  4089. 2:36:12types are little bit different they
  4090. 2:36:13still evaluate the same thing what the
  4091. 2:36:16equals function is doing is actually
  4092. 2:36:18checking the literal values of the
  4093. 2:36:20variables instead of checking the values
  4094. 2:36:22that are held inside the memory address
  4095. 2:36:24location and the difference here is even
  4096. 2:36:26though we have a character array that is
  4097. 2:36:28giving us the values hello and our
  4098. 2:36:31string is also given us the value of
  4099. 2:36:33hello if we run this line of code that
  4100. 2:36:35uses do equals then you'll actually see
  4101. 2:36:38that the second same actually represents
  4102. 2:36:40the bottom if statement so what that's
  4103. 2:36:42doing is it's actually giving us back
  4104. 2:36:45the comparison saying that it's true and
  4105. 2:36:47it is indeed true because hello is is
  4106. 2:36:49literally equal to hello but if you were
  4107. 2:36:52to use the double
  4108. 2:36:54equals then what you'll realize when you
  4109. 2:36:56run the code is now it'll say it's
  4110. 2:36:58different because what it's doing now is
  4111. 2:37:01it's taking a string object and
  4112. 2:37:03comparing it to an object and they are
  4113. 2:37:06not different because of their memory
  4114. 2:37:08and that's what the equals equals is
  4115. 2:37:10comparing it's not only comparing the
  4116. 2:37:12value it's comparing the memory address
  4117. 2:37:14location and that's going to be vastly
  4118. 2:37:16confusing because you don't want to hit
  4119. 2:37:18these situations so it's always best to
  4120. 2:37:20use the equals function because the
  4121. 2:37:22equals function is concerned about the
  4122. 2:37:24values rather than where they are in
  4123. 2:37:27your memory I hope that made sense and
  4124. 2:37:29it wasn't just confusing but basically
  4125. 2:37:31when you use equals equals it checks the
  4126. 2:37:34values that are actually wrote in your
  4127. 2:37:35computer's memory instead of the value
  4128. 2:37:38itself it will do both it will check the
  4129. 2:37:40memory and check the value whereas when
  4130. 2:37:43use do equals it is only concerned about
  4131. 2:37:45the values and that's all that matters
  4132. 2:37:48when we're doing an if statement like
  4133. 2:37:49this we're checking if they're both the
  4134. 2:37:51same so it shouldn't really matter how
  4135. 2:37:53and where they are stored the only thing
  4136. 2:37:55that matters is the values contained
  4137. 2:37:57within the variable we're only caring
  4138. 2:37:59about the values and not where they're
  4139. 2:38:03stored so when we have a string variable
  4140. 2:38:05defined in C we have it like this and
  4141. 2:38:08when we print it out to the console we
  4142. 2:38:10get the entire string being
  4143. 2:38:12displayed but what if you want to return
  4144. 2:38:14the first element back and not the rest
  4145. 2:38:16then what are your options so in the
  4146. 2:38:18later video in the of course we covered
  4147. 2:38:20the substring function and technically
  4148. 2:38:22you could go from the first character
  4149. 2:38:24till the second character and you will
  4150. 2:38:26just get C back so that's index zero
  4151. 2:38:28till index one and you'll get the cback
  4152. 2:38:31now the problem with that is that's an
  4153. 2:38:32extra function call of something that
  4154. 2:38:34should be quite simple and it is a
  4155. 2:38:36message is a string and strings tend to
  4156. 2:38:38be an array of characters which is
  4157. 2:38:41similar to this we haven't learned about
  4158. 2:38:43array so far and we'll cover that in the
  4159. 2:38:45next section but what an array
  4160. 2:38:46essentially is is it lets you store
  4161. 2:38:48multiple of that data type so we could
  4162. 2:38:51have a chart array that holds exactly
  4163. 2:38:53the same value and each position will
  4164. 2:38:55hold each one of these characters and
  4165. 2:38:57the reason why I'm mentioning this is
  4166. 2:38:59because with strings you can use the
  4167. 2:39:00square bracket notation so if you add
  4168. 2:39:02some square brackets at the end it says
  4169. 2:39:04here it returns back a Char and it takes
  4170. 2:39:07in an index gets the CH object at the
  4171. 2:39:10specified position in the current string
  4172. 2:39:12object so if we use message zero you can
  4173. 2:39:14see that returns us back with a ch ch
  4174. 2:39:17right here gets the CH object so we can
  4175. 2:39:20call this on a string and it can return
  4176. 2:39:22us back with a character because like I
  4177. 2:39:24said with the array of characters that's
  4178. 2:39:26essentially what a string is each one of
  4179. 2:39:29these is just its own independent
  4180. 2:39:31character and a string lets you pair
  4181. 2:39:33them all together so if we print this
  4182. 2:39:35out then we should get C because it's
  4183. 2:39:37zerob based so if we copy this down and
  4184. 2:39:39use
  4185. 2:39:40one 2 and three then what should we get
  4186. 2:39:44we get
  4187. 2:39:45C then we get actually nothing
  4188. 2:39:48technically because it's a space
  4189. 2:39:50character and then the third character
  4190. 2:39:52which is the fourth in the list because
  4191. 2:39:53it's zero based is going to be our I so
  4192. 2:39:57let's just print this out to the screen
  4193. 2:39:58see what happens there we go so even
  4194. 2:40:00though we don't see it visually there's
  4195. 2:40:02actually a space character right there
  4196. 2:40:04and that's the third character in The
  4197. 2:40:05List which is right here so of course we
  4198. 2:40:08don't tend to know how long the string
  4199. 2:40:10is unless we can see it visually and if
  4200. 2:40:12you were to reference one of these
  4201. 2:40:14values and the string is not actually
  4202. 2:40:16that long then you have a problem let's
  4203. 2:40:18demonstrate if we have the string just
  4204. 2:40:20called C and let's comment out the rest
  4205. 2:40:23then that's only two characters so only
  4206. 2:40:26zero and one will be applicable these
  4207. 2:40:28two will return back something let's
  4208. 2:40:30have a look and see what happens so the
  4209. 2:40:32first character and the second character
  4210. 2:40:34will run fine and let's put a console
  4211. 2:40:36read line in there just to break it up
  4212. 2:40:38so the other code doesn't run yet if we
  4213. 2:40:40run the code we get our C and that's the
  4214. 2:40:43max length of our string so far it's
  4215. 2:40:46zero and one now if we press enter let's
  4216. 2:40:49see what happens we get an index out of
  4217. 2:40:51range exception index was outside the
  4218. 2:40:53bounds of the array and the reason for
  4219. 2:40:55that is we only have two characters
  4220. 2:40:57inside our message and we're trying to
  4221. 2:40:59display the third and the fourth this is
  4222. 2:41:02problematic because we literally don't
  4223. 2:41:04have any of them characters to
  4224. 2:41:06display so what we can do instead is
  4225. 2:41:08actually write ourselves a little Loop
  4226. 2:41:10to go through the entire string and
  4227. 2:41:12return us back all of these character
  4228. 2:41:13positions instead of us just guessing
  4229. 2:41:15how long the string is so if we return
  4230. 2:41:17this string back to normal and then just
  4231. 2:41:19after this little section we can write a
  4232. 2:41:21for Loop in I equals 0 and I is less
  4233. 2:41:25than message and then we have access to
  4234. 2:41:27dot length this gets the number of
  4235. 2:41:29characters in the current string so we
  4236. 2:41:31don't have to worry about the maximum
  4237. 2:41:32length in the square brackets we can
  4238. 2:41:34always rely on dot length because we
  4239. 2:41:37don't have to keep track of it and the
  4240. 2:41:39language can know that for us and give
  4241. 2:41:41us the
  4242. 2:41:42value in this case we're going from zero
  4243. 2:41:44to whatever the length of these
  4244. 2:41:45characters are and then in here we can
  4245. 2:41:48actually just conso right line and we
  4246. 2:41:50can do the same notation we did here
  4247. 2:41:52we're saying we want to print the
  4248. 2:41:53message but not only the message we want
  4249. 2:41:56to print the index and the index is
  4250. 2:41:58going to be I in this case because we
  4251. 2:42:00start at zero since it's zero based and
  4252. 2:42:02then we're going to the last character
  4253. 2:42:05so let's print this and see what
  4254. 2:42:07happens so now we get C Is Awesome from
  4255. 2:42:10here down to the bottom c is awesome
  4256. 2:42:13print on every single line and that's a
  4257. 2:42:15little bit annoying to read so what we
  4258. 2:42:17could do is just change this to a
  4259. 2:42:18console right and maybe we can just
  4260. 2:42:20comment this section out just so we can
  4261. 2:42:22see this
  4262. 2:42:25properly there we go so we get c is
  4263. 2:42:27awesome and because it's a console right
  4264. 2:42:29it's right in them all next to each
  4265. 2:42:31other and they're being printed out one
  4266. 2:42:32by one let's add a cool little effect I
  4267. 2:42:35can demonstrate you for now so inside
  4268. 2:42:38the system. threading Library we have
  4269. 2:42:40this sleep function so what we can do is
  4270. 2:42:42we can add another using command system.
  4271. 2:42:45threading we already have threading do
  4272. 2:42:48tasks in there but the function that we
  4273. 2:42:49need is inside the threading Library so
  4274. 2:42:52there's two ways we can write this we
  4275. 2:42:54can actually write everything by hand
  4276. 2:42:56like this dot sleep or you could just
  4277. 2:42:59remove the system. threading because
  4278. 2:43:01we've added it at the Top If you don't
  4279. 2:43:03want to add it at the top that's not
  4280. 2:43:04really a problem you can just include it
  4281. 2:43:06straight into here adding it at the top
  4282. 2:43:08will include it within the entire
  4283. 2:43:09project but if you're only going to use
  4284. 2:43:11it once or twice then sometimes it's
  4285. 2:43:13better to just include it straight into
  4286. 2:43:14here because it'll make your overall
  4287. 2:43:16program size a lot smaller so let's just
  4288. 2:43:18keep keep it in so we can make our Corde
  4289. 2:43:20a bit more readable so we have thread
  4290. 2:43:23dos sleep and what this will do is it
  4291. 2:43:25will suspend the current thread for a
  4292. 2:43:26specified number of milliseconds there's
  4293. 2:43:29a th000 milliseconds in 1 second so what
  4294. 2:43:32we can do in here and we're trying to
  4295. 2:43:33mimic a typewriter effect so we can say
  4296. 2:43:36something like 200 which will be roughly
  4297. 2:43:38a quarter of a second so if we've run
  4298. 2:43:40the code now let's see what happens so
  4299. 2:43:42we get this cool effect where after it
  4300. 2:43:44prints out each of the characters it
  4301. 2:43:45will print out the next character only
  4302. 2:43:48after it's being asleep for a quarter of
  4303. 2:43:49a second and that looks kind of cool if
  4304. 2:43:52you wanted to speed it up you could just
  4305. 2:43:53decrease this number there you
  4306. 2:43:55go of course if you make it too low then
  4307. 2:43:58it might be too fast for you to actually
  4308. 2:43:59acknowledge what's happening and if you
  4309. 2:44:02also make it too slow then it's just
  4310. 2:44:04going to take too long to turn off and
  4311. 2:44:06at that point you can use contrl C on
  4312. 2:44:08your keyboard or use the stop button at
  4313. 2:44:10the top to terminate the program okay so
  4314. 2:44:12let's have a look at another thing that
  4315. 2:44:14we can do with this so maybe we can make
  4316. 2:44:16this 100 for now in the previous videos
  4317. 2:44:18we've used use a function called
  4318. 2:44:20contains and let's say we're just
  4319. 2:44:22checking if it contains C and we know it
  4320. 2:44:24does because we can see it visually
  4321. 2:44:26let's run the
  4322. 2:44:27code and we see we have true being
  4323. 2:44:30printed and the reason why true is
  4324. 2:44:32actually on the same line as c is
  4325. 2:44:34awesome is because we're using console.
  4326. 2:44:37right here so it's actually not made a
  4327. 2:44:39new line character just after the E so
  4328. 2:44:41when it's printing true it prints it
  4329. 2:44:43straight after that so what we can do to
  4330. 2:44:45fix that is just place a new line
  4331. 2:44:47character in here using console.
  4332. 2:44:48readline and we can just drop this a
  4333. 2:44:50little bit lower to 50 there we go so we
  4334. 2:44:53have true and what we can do with this
  4335. 2:44:56is we can actually make our own little
  4336. 2:44:57version of the contains function let's
  4337. 2:45:00demonstrate so if we make this exact for
  4338. 2:45:02loop again in I equals z i is less than
  4339. 2:45:05the message. length and i++ so first
  4340. 2:45:09let's kind of understand what's going on
  4341. 2:45:10with this function returns a value
  4342. 2:45:12indicated whether a specified subring
  4343. 2:45:14occurs within this string and what that
  4344. 2:45:16means is it will return a Boolean for us
  4345. 2:45:18if C is inside our message then this
  4346. 2:45:21will return back true and we know this
  4347. 2:45:22is true because we just outputed it to
  4348. 2:45:24the screen and we can see this visually
  4349. 2:45:26so what we need to do is we need to Loop
  4350. 2:45:28through our message and try and figure
  4351. 2:45:30out if we can find C inside our message
  4352. 2:45:33and then we can give back a value is
  4353. 2:45:35true so maybe we can do a bu contains
  4354. 2:45:38equal false and the reason why we
  4355. 2:45:40initial it to false and the reason why
  4356. 2:45:41we initialize it to false is because we
  4357. 2:45:43want to set it up telling it that we
  4358. 2:45:45haven't found it and then inside the for
  4359. 2:45:47Loop if we find it then we can set it to
  4360. 2:45:51True let's have a look so after the for
  4361. 2:45:53Loop we can print contains of the screen
  4362. 2:45:55and then inside the for Loop what we can
  4363. 2:45:57do as we've seen here it returns back
  4364. 2:46:00every single character so you'll get
  4365. 2:46:02back C sharp then the space then I then
  4366. 2:46:05s and so on and so forth and what we
  4367. 2:46:08want to do is we want to go through each
  4368. 2:46:09of the characters and then just ask if
  4369. 2:46:12the variable is equal to C if C is equal
  4370. 2:46:15to C then we can assign this to true and
  4371. 2:46:18then go through #is equal to C if space
  4372. 2:46:20is equal to C and so on and so forth so
  4373. 2:46:23we can write a if statement here that
  4374. 2:46:24says message square bracket I is equal
  4375. 2:46:28to equal to apostrophe C and the reason
  4376. 2:46:31why I said apostrophe is because this is
  4377. 2:46:33a character and this also returns us
  4378. 2:46:35back with a character so we need to
  4379. 2:46:37compare them both like that if we use a
  4380. 2:46:39string in here then it might complain
  4381. 2:46:41that we're trying to convert between a
  4382. 2:46:43Char and a string and it won't let you
  4383. 2:46:45do the equals equals operator between
  4384. 2:46:47two different variables maybe what we
  4385. 2:46:49can do instead is to follow the approach
  4386. 2:46:51that we used before and use the equals
  4387. 2:46:53function and place our C inside here so
  4388. 2:46:56if the character that we're currently up
  4389. 2:46:57to inside the for Loop is equal to our C
  4390. 2:47:00which is what we want to search for then
  4391. 2:47:03we can set our contains variable equal
  4392. 2:47:05to true we don't need to write an L
  4393. 2:47:07statement in here to set our contains to
  4394. 2:47:10false because we've already gave a value
  4395. 2:47:12at the start if we didn't have this
  4396. 2:47:14value then it'll be beneficial to do
  4397. 2:47:16this but since we have it then we can
  4398. 2:47:18remove this other path and we don't
  4399. 2:47:20exactly need it so let's have a look and
  4400. 2:47:22see if this
  4401. 2:47:24works perfect it says true maybe we can
  4402. 2:47:27change this character to something like
  4403. 2:47:28a make sure this
  4404. 2:47:30works which it does as well so perfect
  4405. 2:47:33so now we've made our own mini little
  4406. 2:47:35contains function inside here and we can
  4407. 2:47:37also use the C library one but the
  4408. 2:47:40purpose of this exercise was just to
  4409. 2:47:41show you how we can iterate through a
  4410. 2:47:43string variable and then decide what
  4411. 2:47:46each of the characters can be equal to
  4412. 2:47:48and then we can decide if they're equal
  4413. 2:47:49to another one and then let the user
  4414. 2:47:51know that we found a match for their
  4415. 2:47:53search
  4416. 2:47:56term so far in the course we've covered
  4417. 2:47:58the equals function and the contains
  4418. 2:48:01function when you're searching through
  4419. 2:48:02strings or trying to find values let's
  4420. 2:48:04have a look at a method that makes sure
  4421. 2:48:06a string is valid before we can process
  4422. 2:48:08information on it so in this quick
  4423. 2:48:10example we have a name output we're
  4424. 2:48:12reading the value into a string name and
  4425. 2:48:14then we simply output it to the screen
  4426. 2:48:17if we run the code and just press enter
  4427. 2:48:18then it says your name is blank and we
  4428. 2:48:21don't really want that because we want
  4429. 2:48:22to check if your name is equal to
  4430. 2:48:24something before we continue so let's
  4431. 2:48:26look at some methods so we have if the
  4432. 2:48:29name is not equal to nothing and then
  4433. 2:48:31let's just print zero to the screen and
  4434. 2:48:34that's one of the methods and we can say
  4435. 2:48:37if name is not equals to quotation marks
  4436. 2:48:42then let's print out number one so what
  4437. 2:48:44this is doing is checking if the name is
  4438. 2:48:46not equal to empty quotes and then this
  4439. 2:48:49is doing exactly the same thing except
  4440. 2:48:51we're using the equals method and let's
  4441. 2:48:53just run the code and press enter and
  4442. 2:48:56you can see the zero or the one doesn't
  4443. 2:48:58appear because it's detecting that it
  4444. 2:49:00contains nothing so if we type in a real
  4445. 2:49:02value we can see Zero and one coming up
  4446. 2:49:05so these are both valid ways of checking
  4447. 2:49:07if the string is not empty so let's look
  4448. 2:49:10at a third one and this one is actually
  4449. 2:49:12very important for reasons I'll explain
  4450. 2:49:14in a second so inside our string Library
  4451. 2:49:16we have is null or empty so we place our
  4452. 2:49:20name inside here and let's see what this
  4453. 2:49:22says now indicates whether the specified
  4454. 2:49:25string is null or at an empty string so
  4455. 2:49:28this can be very effective so what this
  4456. 2:49:31reads now is if the string is null or
  4457. 2:49:33empty then we print a value but we want
  4458. 2:49:36it to be not no lower empty because it
  4459. 2:49:38wants to have some contents so if we
  4460. 2:49:41type a value in again you'll see now we
  4461. 2:49:43get all three numbers and what them
  4462. 2:49:45three numbers mean is that it's past all
  4463. 2:49:47of these checks and each of the checks
  4464. 2:49:49just ensures that the value has got
  4465. 2:49:51something inside and that's fine but it
  4466. 2:49:54can be slightly problematic for this
  4467. 2:49:56reason using this method and this method
  4468. 2:49:59are fine in some cases except as it says
  4469. 2:50:02here it checks if the string is null and
  4470. 2:50:05null means it doesn't have a value but
  4471. 2:50:07it just has nothing instead of something
  4472. 2:50:10let me demonstrate if we assign the
  4473. 2:50:12value of empty quotation marks this
  4474. 2:50:15actually has a value but the contents is
  4475. 2:50:17empty but it still a string as it comes
  4476. 2:50:20up in here if we assign this the value
  4477. 2:50:22of null then it's just a key word that
  4478. 2:50:24we can use to denote that this literally
  4479. 2:50:26has no contents it's not that it has an
  4480. 2:50:29empty string because an empty string is
  4481. 2:50:31still actually a string whereas a null
  4482. 2:50:34it just means the variable is blank now
  4483. 2:50:36this is where the issue arises if we try
  4484. 2:50:39and call a method on something that's
  4485. 2:50:41null Watch What
  4486. 2:50:43Happens we actually get an exception and
  4487. 2:50:45our program just crushes you can't run
  4488. 2:50:48any any code on something if that
  4489. 2:50:50variable is actually defined as
  4490. 2:50:52null it doesn't mind if you check the
  4491. 2:50:55value of it but if you try and run a
  4492. 2:50:57function then you've got a problem so if
  4493. 2:50:59we comment this line out this ensures
  4494. 2:51:02everything it ensures it's null or it's
  4495. 2:51:05empty and this is the belt and braces
  4496. 2:51:07option because if it's null we don't
  4497. 2:51:09actually get an exception when we try
  4498. 2:51:11and run this code as
  4499. 2:51:14follows we get the zero showing up
  4500. 2:51:16because our variable has something
  4501. 2:51:19that's not speech marks and it's
  4502. 2:51:20actually null so we want to avoid this
  4503. 2:51:23option as well and also avoid this
  4504. 2:51:25option in case our string is actually
  4505. 2:51:27genuinely
  4506. 2:51:28null using is null or empty we check
  4507. 2:51:32both for the value of null which is
  4508. 2:51:34right now and if it's got empty quotes
  4509. 2:51:37EG the value is empty so if we just run
  4510. 2:51:40this again and have these two paths
  4511. 2:51:42commented out we can see that two is not
  4512. 2:51:44appearing if we change this back to our
  4513. 2:51:47console read line and we try and do it
  4514. 2:51:50then we get our number two because it's
  4515. 2:51:52validated that this string variable has
  4516. 2:51:55got something inside it and the variable
  4517. 2:51:57is not actually no this is a very good
  4518. 2:52:00way of being able to make sure that your
  4519. 2:52:02program doesn't get exceptions as with
  4520. 2:52:04calling these equals if you want to call
  4521. 2:52:07equals then call it inside here you can
  4522. 2:52:10check if that variable equals something
  4523. 2:52:13by simply placing that if statement in
  4524. 2:52:15inside here after you validated if the
  4525. 2:52:17name variable able actually has some
  4526. 2:52:20contents if it's null or it's empty
  4527. 2:52:23running equals on it will just break the
  4528. 2:52:25code further and you don't need to check
  4529. 2:52:27if it's equal to Something in here maybe
  4530. 2:52:29you were checking if the value is equal
  4531. 2:52:31to abber or something along them lines
  4532. 2:52:33and now if we run it again we actually
  4533. 2:52:36get the output
  4534. 2:52:37correct it's best to Nest them like this
  4535. 2:52:40because if you run this function on
  4536. 2:52:42something that's null it's actually
  4537. 2:52:44really safe but as you saw previously if
  4538. 2:52:46you run a function on something that's
  4539. 2:52:48null then you're going to get a null
  4540. 2:52:50reference exception and your program
  4541. 2:52:51will close down and break you want to
  4542. 2:52:54avoid exceptions at all cost so nesting
  4543. 2:52:57it inside a is null or Remy check will
  4544. 2:52:59be perfect for your code it improves the
  4545. 2:53:02stab ability of your program to ensure
  4546. 2:53:04that exceptions don't
  4547. 2:53:07happen so in this exercise I want you to
  4548. 2:53:09ask the user to input any message and
  4549. 2:53:11then I want you to print the message in
  4550. 2:53:13Order character by character and then
  4551. 2:53:15also print it in reverse character by
  4552. 2:53:17character let's get started so we could
  4553. 2:53:20just have a console right line but let's
  4554. 2:53:22change it to a right and just say enter
  4555. 2:53:24your message call on and then speech
  4556. 2:53:26marks and then just have a string
  4557. 2:53:28message
  4558. 2:53:30equals console. readline perfect so in
  4559. 2:53:34order to be able to Loop through the
  4560. 2:53:36string message we need to have a follow
  4561. 2:53:38Loop if you remember from our previous
  4562. 2:53:39videos we can have this string variable
  4563. 2:53:42followed by a square bracket and then we
  4564. 2:53:44can have an index inside and that will
  4565. 2:53:46print us back any sort of index based on
  4566. 2:53:48that position so if we just have this is
  4567. 2:53:52a test then it's prob print as the T
  4568. 2:53:55because that's the first index and so on
  4569. 2:53:57and so forth so what we can do with this
  4570. 2:53:59information is take what we can do with
  4571. 2:54:02the string index and then also use the
  4572. 2:54:04property which is do length and then use
  4573. 2:54:07these two values to be able to process
  4574. 2:54:09this is a test so we have the first
  4575. 2:54:12character Square braet 0 and the last
  4576. 2:54:14character is at length 14 so our message
  4577. 2:54:17variable now contains 14 characters
  4578. 2:54:20obviously When I close the program then
  4579. 2:54:21that value is lost and I have to reenter
  4580. 2:54:23it but because of that exact reason we
  4581. 2:54:25would always use do length because we
  4582. 2:54:27never know how long the users input is
  4583. 2:54:29going to be so we can have a for Loop so
  4584. 2:54:31we can say 4 into I equal 0 I is less
  4585. 2:54:35than and we can use the message. length
  4586. 2:54:37here and then we can say I
  4587. 2:54:39++ so we want to start at zero because
  4588. 2:54:42our first index position is indeed zero
  4589. 2:54:45and we want to go from the index zero
  4590. 2:54:47all the way to the length
  4591. 2:54:49now in that example I just printed out
  4592. 2:54:5114 for the length but you got to realize
  4593. 2:54:53is there might be 14 positions but
  4594. 2:54:56there's only 13 indexes so it's not
  4595. 2:54:59actually 13 index it's just up to 13
  4596. 2:55:02when you start at zero going from 0 to
  4597. 2:55:0413 gives you 14 positions so when we're
  4598. 2:55:07saying 0 is less than the message length
  4599. 2:55:10then that'll actually take us from 0 to
  4600. 2:55:1213 instead of 0 to 14 if we made this
  4601. 2:55:16greater than or equal to and change this
  4602. 2:55:18to to one for example then that's going
  4603. 2:55:20to go from 1 to 14 and first we're going
  4604. 2:55:22to miss out the first character and
  4605. 2:55:24we're actually going to go one over the
  4606. 2:55:26message length so you always want to
  4607. 2:55:28start from zero if we're going through
  4608. 2:55:30the entire string so this will go from 0
  4609. 2:55:32to 13 and then here we can just have a
  4610. 2:55:35console right line and just print
  4611. 2:55:37message Square braet I and then just for
  4612. 2:55:39the time being we can comment out these
  4613. 2:55:41lines just so it doesn't saturate the
  4614. 2:55:42output so let's run this code and see
  4615. 2:55:44what
  4616. 2:55:45happens this is a test and then we get
  4617. 2:55:48them all on separate lines maybe we can
  4618. 2:55:51just use a console. right instead so we
  4619. 2:55:53get each character on the same line
  4620. 2:55:55instead of a new line character being
  4621. 2:55:56outputed at the end of each of the lines
  4622. 2:55:59so we can say this as a test again and
  4623. 2:56:01there we go we get this as a test
  4624. 2:56:03outputed as well so now that we've
  4625. 2:56:05printed that in order let's see how we
  4626. 2:56:06can print it in reverse so just a quick
  4627. 2:56:09little tip throughout this course I've
  4628. 2:56:11been quite hard on you I've made you Mar
  4629. 2:56:13the for Loop out exactly the way it
  4630. 2:56:15should be but all done manually this is
  4631. 2:56:18so you can get used to the syntax and
  4632. 2:56:20after a while once you understand
  4633. 2:56:21exactly what to do then what you can do
  4634. 2:56:23is when you type in the fall you can see
  4635. 2:56:25that it says as a force snippet tab
  4636. 2:56:27twice to insert the force snippet and we
  4637. 2:56:30can do that now and it actually gives
  4638. 2:56:32you a little bit of an output you can
  4639. 2:56:34see these sections that have been
  4640. 2:56:35highlighted the reason they're
  4641. 2:56:36highlighted is once you're done with one
  4642. 2:56:38section you can actually press Tab and
  4643. 2:56:40it'll jump to the next one so now my
  4644. 2:56:42cursor is jumped on the length and we
  4645. 2:56:44can type in message. length if I press
  4646. 2:56:47tab again then it'll just keep cycling
  4647. 2:56:49through them until we're done and we can
  4648. 2:56:51press Escape upon pressing escape then
  4649. 2:56:54it gets out of the little message and
  4650. 2:56:55there we go we've now just defined a
  4651. 2:56:58really quick for Loop please only use
  4652. 2:57:00this once you're comfortable writing for
  4653. 2:57:02loops and don't use them immediately
  4654. 2:57:04because you might get stuck on what to
  4655. 2:57:06do and how to do it so it's best to
  4656. 2:57:08always write the for Loop syntax out
  4657. 2:57:10from scratch so you can get used to the
  4658. 2:57:11three different sections so this is
  4659. 2:57:13actually a full loop that goes forwards
  4660. 2:57:15it's going from 0 to 13 but what we
  4661. 2:57:18actually want to do now is go from 13 to
  4662. 2:57:21zero so let's actually change this for
  4663. 2:57:23Loop completely and we can do our little
  4664. 2:57:25shocket again so if we do four and then
  4665. 2:57:28we don't actually want to start on the
  4666. 2:57:29value of zero we want to start on
  4667. 2:57:32message. length and then you want a
  4668. 2:57:34minus one and just as we said here we
  4669. 2:57:37never actually want to get to the length
  4670. 2:57:39because we want to go from 0 to 13 so if
  4671. 2:57:41we want to go backwards we want to go
  4672. 2:57:43from 13 to zero and the only way that we
  4673. 2:57:46can get this 13 is by getting the 14
  4674. 2:57:49value which is message. length and
  4675. 2:57:51simply minusing one so now that we've
  4676. 2:57:53got our first variable which is in I
  4677. 2:57:55equals message length minus one which
  4678. 2:57:57will give us the 13 we want to go from I
  4679. 2:58:00and instead of less than it's going to
  4680. 2:58:03be greater than or equal to and then we
  4681. 2:58:05actually want to change it to a zero
  4682. 2:58:07because we actually want to stop at zero
  4683. 2:58:08in this case and then we don't want to
  4684. 2:58:10use I ++ because if we start at 13 we
  4685. 2:58:14want to go down in one instead of up in
  4686. 2:58:16one so we can simply just just do minus
  4687. 2:58:19minus we can take this exact line and
  4688. 2:58:21print out to the
  4689. 2:58:23screen and if we just print something
  4690. 2:58:25small in between like a hyphen just to
  4691. 2:58:28separate the two outputs and let's see
  4692. 2:58:30what
  4693. 2:58:31happens test there we go so we have test
  4694. 2:58:35and then t s e and t is now in Reverse
  4695. 2:58:39but now you might be wondering if we had
  4696. 2:58:42a shortcut for the four what's this four
  4697. 2:58:44with an additional R cords n it for a
  4698. 2:58:47reverse for Loop so if we tap tab twice
  4699. 2:58:49you can see that we have exactly the
  4700. 2:58:51same format inti equals something minus
  4701. 2:58:54one which we have right here and then we
  4702. 2:58:57have I is greater than or equal to zero
  4703. 2:58:59as is we have here and then we have
  4704. 2:59:01iusus just like here so we can literally
  4705. 2:59:04just change this into message do length
  4706. 2:59:08and then these two now are exactly the
  4707. 2:59:10same so just copying this down we'll get
  4708. 2:59:12us exactly what we need but obviously we
  4709. 2:59:14just wrote this I just wanted to show
  4710. 2:59:16you the shortcut so let's try it with a
  4711. 2:59:18longer string this is a test there you
  4712. 2:59:21go and if you wanted to you could just
  4713. 2:59:23print these on separate lines so you can
  4714. 2:59:24just change this into a right line for
  4715. 2:59:27example and have it like that it doesn't
  4716. 2:59:29really matter the rest is up to you but
  4717. 2:59:31the task is just so you can print it one
  4718. 2:59:33way and then print it the other way I
  4719. 2:59:35realized that what you can also do is
  4720. 2:59:38just simply print a console right line
  4721. 2:59:40and have message in there and just get
  4722. 2:59:42rid of this completely and the output
  4723. 2:59:44will still look exactly the same there
  4724. 2:59:47you go and and the reason why I told you
  4725. 2:59:49to write the for Loop is not because
  4726. 2:59:51it'll be easier if you do this it's more
  4727. 2:59:53for your Educational Learning purposes
  4728. 2:59:55having more practice to how to iterate a
  4729. 2:59:57string variable is very helpful for the
  4730. 3:00:00future and you can understand it fully
  4731. 3:00:02if you just print out to the screen then
  4732. 3:00:04you're not learning anything extra
  4733. 3:00:05you're just applying what you already
  4734. 3:00:07know so let's just have a quick recap we
  4735. 3:00:09ask the user to enter the message we STW
  4736. 3:00:11it into a string variable and then I
  4737. 3:00:13demonstrated that you could use the
  4738. 3:00:14square brackets to Output the first
  4739. 3:00:16index then you can output the length of
  4740. 3:00:18the entire variable using a follow Lo we
  4741. 3:00:20can go from zero to message length which
  4742. 3:00:23will give us from indexes 0 up until 13
  4743. 3:00:26in this case and then we can go through
  4744. 3:00:28i++ increment in one and then we can
  4745. 3:00:31have a console right which will just
  4746. 3:00:32print out message index I for the index
  4747. 3:00:35that we're currently up to and then we
  4748. 3:00:36break it off with a console right line
  4749. 3:00:38just in the middle because these are
  4750. 3:00:40printing on the same line so we need to
  4751. 3:00:42insert a new line character at the end
  4752. 3:00:44after we've done that what we can do now
  4753. 3:00:46is print it in reverse and now we can do
  4754. 3:00:49exactly the same thing except from 0 to
  4755. 3:00:5113 we go from 13 to 0 so we start at the
  4756. 3:00:54message length minus one because the
  4757. 3:00:56length was 14 and now we minus one to
  4758. 3:00:58get to 13 and we're going from I is
  4759. 3:01:00greater than or equal to zero because
  4760. 3:01:02that's the value that we want to stop at
  4761. 3:01:04and then we can do IUS minus if we were
  4762. 3:01:07to take out this equals operator let's
  4763. 3:01:10see what happens test and now we're
  4764. 3:01:12actually missing the last character
  4765. 3:01:14because what we've done is we've
  4766. 3:01:16actually went from 13 to 1 instead of 13
  4767. 3:01:18to zero so let's put that back in and
  4768. 3:01:21make sure it works and then we have t
  4769. 3:01:24set for test and then we're outputting
  4770. 3:01:26the exact same thing in both for Loops
  4771. 3:01:29because we're simply just printing the
  4772. 3:01:30character that we're currently up to and
  4773. 3:01:32because this for Loop starts at zero and
  4774. 3:01:34this for Loop starts at the length minus
  4775. 3:01:36one then they output in the same values
  4776. 3:01:39or what it appears to be the same values
  4777. 3:01:41it's just starting at different
  4778. 3:01:44indexes in this exercise we're going to
  4779. 3:01:47be building some sort of a password
  4780. 3:01:48validor these can be present in
  4781. 3:01:50something like a register form where you
  4782. 3:01:52ask the user to enter their password and
  4783. 3:01:54then enter it again to make sure that
  4784. 3:01:56they've entered it correctly you want to
  4785. 3:01:57ask user to enter their password in
  4786. 3:01:59twice andall them both check if they
  4787. 3:02:01both contain something if they do we can
  4788. 3:02:03print out password match if they don't
  4789. 3:02:05passwords do not match and if they both
  4790. 3:02:08contain nothing then you want to Output
  4791. 3:02:09please enter a password so let's get
  4792. 3:02:12started first we can have a console
  4793. 3:02:14console right we can have enter password
  4794. 3:02:19string password equals console.
  4795. 3:02:23readline and then we can just copy these
  4796. 3:02:25two lines down using contrl D and then
  4797. 3:02:28we can have a password C for password
  4798. 3:02:30confirmation and we can say enter
  4799. 3:02:32password again and let just print these
  4800. 3:02:34out to the screen just to make sure we
  4801. 3:02:35know what's going on just like so so we
  4802. 3:02:38can have abber and then abber again so
  4803. 3:02:40now we have both abber started and
  4804. 3:02:42that's
  4805. 3:02:43fine so let's see what we can do now we
  4806. 3:02:45need to check if they both contain
  4807. 3:02:47something so what are the different ways
  4808. 3:02:49that we can do this so we can have if
  4809. 3:02:51password do equals and then we can make
  4810. 3:02:53it equals to string. empty and because
  4811. 3:02:56we want to check if it's not empty then
  4812. 3:02:58we can simply place the not operator
  4813. 3:03:00right at the start and then we can join
  4814. 3:03:02them with another one not password.
  4815. 3:03:04equals and then exactly the same thing
  4816. 3:03:07string. empty but in this case we don't
  4817. 3:03:09want to use password we want to use
  4818. 3:03:11password see there we go so if we've
  4819. 3:03:14came into here then that means they must
  4820. 3:03:16have entered something so let's just
  4821. 3:03:18quickly do our exit path which is if
  4822. 3:03:20they're empty please enter a password so
  4823. 3:03:23we can say else and we don't need a
  4824. 3:03:25condition here because if this were to
  4825. 3:03:27fail then it has to mean that their
  4826. 3:03:29password's empty so we can have a
  4827. 3:03:31console. right line and we can just do
  4828. 3:03:33please enter a
  4829. 3:03:35password and let's just test if this
  4830. 3:03:37path works first and we just press enter
  4831. 3:03:40twice and we get please enter a password
  4832. 3:03:43and maybe if we type something in for
  4833. 3:03:44both and then we get nothing because
  4834. 3:03:46it's actually came here now perfect so
  4835. 3:03:50if we come through and we can say
  4836. 3:03:51password equals equals password C or we
  4837. 3:03:54could use the equals function and we can
  4838. 3:03:56say password. equals and then password C
  4839. 3:04:00and if they both equal to each other
  4840. 3:04:02then we can print out passwords
  4841. 3:04:05match there we go and if that were to
  4842. 3:04:09fail then we can print out passwords do
  4843. 3:04:12not
  4844. 3:04:14match here we go let's run that let's
  4845. 3:04:16find out Aba and abber and we get
  4846. 3:04:19passwords match and if we try abber and
  4847. 3:04:21abber with two A's and then we get
  4848. 3:04:23passwords do not match so what are some
  4849. 3:04:26other ways that we can do this you can
  4850. 3:04:28structure this completely differently
  4851. 3:04:30right now we're doing both checks in one
  4852. 3:04:32single if statement and we can actually
  4853. 3:04:34split this up into two and then provide
  4854. 3:04:36a different error message so right now
  4855. 3:04:38this is please enter a password you
  4856. 3:04:40might have entered the second one but
  4857. 3:04:42not have ented the first one or vice
  4858. 3:04:44versa but it doesn't matter because you
  4859. 3:04:46have to enter both of them in order to
  4860. 3:04:48pass this validation check so what you
  4861. 3:04:50could do instead is actually put another
  4862. 3:04:52if statement inside and paste this one
  4863. 3:04:55inside here and then just keep these as
  4864. 3:04:58two separate if statements and then you
  4865. 3:05:00can actually provide different error
  4866. 3:05:02messages so let's Nest this in one
  4867. 3:05:05deeper so we can say if the password is
  4868. 3:05:08not equal to empty and if this returns
  4869. 3:05:10back true then this variable has
  4870. 3:05:12something inside it and then once we go
  4871. 3:05:14inside these curly braces we get another
  4872. 3:05:16check and if the password C is not equal
  4873. 3:05:19to nothing then that means if we've came
  4874. 3:05:21in here then we pass both of these
  4875. 3:05:23validation checks and both of them
  4876. 3:05:24contain something and then we can
  4877. 3:05:26proceed to check the variables so what
  4878. 3:05:28you can do in the first L statement is
  4879. 3:05:31you can say please enter a password but
  4880. 3:05:33on the second one so just after this
  4881. 3:05:35curly brace we can have another L
  4882. 3:05:37statement or we can say please enter a
  4883. 3:05:41password
  4884. 3:05:43confirmation so I know that was a bit
  4885. 3:05:45quick so let's just have a quick recap
  4886. 3:05:47so we can have have one if statement
  4887. 3:05:48that processes the first password that
  4888. 3:05:50you type in and if this is empty then it
  4889. 3:05:53says please enter a password and if it's
  4890. 3:05:55not empty then it'll go inside this
  4891. 3:05:56curly brace hit the first line and check
  4892. 3:05:59if the password confirmation is not
  4893. 3:06:01empty if it is empty then it'll say
  4894. 3:06:04please enter a password confirmation if
  4895. 3:06:06both of these return back true then it
  4896. 3:06:08will go inside the success path in which
  4897. 3:06:10it will actually check if password is
  4898. 3:06:12equal to the password confirmation and
  4899. 3:06:14we can say the passwords match or if
  4900. 3:06:16they don't equal to each other we can
  4901. 3:06:18say the passwords do not match let's run
  4902. 3:06:21that and see what we get so if we type
  4903. 3:06:23something in for the second one but not
  4904. 3:06:24the first one so for the first one we
  4905. 3:06:26just press enter and then let's type
  4906. 3:06:28something in for the second one and it
  4907. 3:06:30says please enter a password and then if
  4908. 3:06:32we run that and do the complete opposite
  4909. 3:06:34type in p and then press enter for the
  4910. 3:06:37second one then it says please enter a
  4911. 3:06:38password confirmation so you can see
  4912. 3:06:40immediately what we can do is provide a
  4913. 3:06:42different error message for different
  4914. 3:06:44situations if you include these both in
  4915. 3:06:47one
  4916. 3:06:48then you can only give out more of a
  4917. 3:06:49generic error message this is similar
  4918. 3:06:51for when you go to login and when you
  4919. 3:06:53get your password or email wrong it
  4920. 3:06:55doesn't say which one you've got wrong
  4921. 3:06:57it just says them generically which one
  4922. 3:06:59you've got wrong it says invalid
  4923. 3:07:01credentials or invalid username or email
  4924. 3:07:04it'll say them generically like invalid
  4925. 3:07:06username or password but it won't tell
  4926. 3:07:08you which one it is because hackers can
  4927. 3:07:10exploit this and you don't want to be
  4928. 3:07:11doing this but in the case of a password
  4929. 3:07:13confirmation and this is just for a
  4930. 3:07:15register form then it's not exactly the
  4931. 3:07:17the worst thing to do this because
  4932. 3:07:19sometimes what you'll see is the text
  4933. 3:07:20boxes might go red to indicate that
  4934. 3:07:22they're both not equal to each other and
  4935. 3:07:24this is fine on registration but it's
  4936. 3:07:26not fine on the login what we can also
  4937. 3:07:28do in here once we've checked if they're
  4938. 3:07:30both not equal to something we can
  4939. 3:07:32actually just validate how long the
  4940. 3:07:33string needs to be we can have if
  4941. 3:07:36password. length and let's say you had a
  4942. 3:07:38restriction and it can only be over six
  4943. 3:07:41characters and we can say six and
  4944. 3:07:44password
  4945. 3:07:45c.length is greater than equal to six
  4946. 3:07:48and once it passes all three of these
  4947. 3:07:50checks then we can check if they're
  4948. 3:07:52equal to each other if they got
  4949. 3:07:54something inside and if they've got
  4950. 3:07:56something inside and if they've got both
  4951. 3:07:59the length is greater than or equal to
  4952. 3:08:01six then we can check it so we can have
  4953. 3:08:04something like password and then
  4954. 3:08:06password and then this returns back with
  4955. 3:08:08password matched if we have something
  4956. 3:08:10like abber then it's actually returns
  4957. 3:08:12back nothing because we don't have an
  4958. 3:08:14else for this specific bracket so we can
  4959. 3:08:17say else conso right line please enter
  4960. 3:08:22six or more
  4961. 3:08:25characters so if we type in Alber twice
  4962. 3:08:28then you can see we get this message now
  4963. 3:08:30please enter six or more characters so
  4964. 3:08:32right now we have a validation for the
  4965. 3:08:34first password and the second password
  4966. 3:08:37and for the length and if all these
  4967. 3:08:39checks go by then you can process the
  4968. 3:08:41actual passwords that they've entered
  4969. 3:08:43and check if they're equal to each other
  4970. 3:08:45you can technically do all of these
  4971. 3:08:46checks in one line it'll just be a
  4972. 3:08:49pretty long line but what that means is
  4973. 3:08:51you can't have three separate error
  4974. 3:08:53messages like this if you have it
  4975. 3:08:55process in one line the only error
  4976. 3:08:57message that you can give is very
  4977. 3:08:58generic there's no one generic message
  4978. 3:09:00that can fit for password empty and the
  4979. 3:09:03confirmation password empty and for the
  4980. 3:09:05length so maybe what you could do is
  4981. 3:09:07chain the both of them to be check if
  4982. 3:09:09they're not empty so then you can
  4983. 3:09:11process a message with something like
  4984. 3:09:13please enter a password and then you can
  4985. 3:09:15have another check for the password
  4986. 3:09:16length just to let them know that they
  4987. 3:09:18need to enter six or more characters
  4988. 3:09:20splitting these off into two separate
  4989. 3:09:22checks is going to be very helpful
  4990. 3:09:24because it'll let the user know
  4991. 3:09:25something a bit better than a generic
  4992. 3:09:27error message so in the previous
  4993. 3:09:29examples throughout the course we've
  4994. 3:09:31actually talked about using the to low
  4995. 3:09:32function and I said in majority of cases
  4996. 3:09:35where you want actually compare two
  4997. 3:09:36different things you want to actually
  4998. 3:09:38use the two lower function or the two
  4999. 3:09:40upper function and you can achieve this
  5000. 3:09:42by having two lower just here and then
  5001. 3:09:45also two lower just here
  5002. 3:09:48now the problem with comparing if
  5003. 3:09:49they're both equal to each other but
  5004. 3:09:51checking the lower gue values is this is
  5005. 3:09:53indeed a password if you're checking if
  5006. 3:09:55the user has inputed any capitals
  5007. 3:09:57lowercase symbols Etc then there's no
  5008. 3:10:00point implying a too low function
  5009. 3:10:02because that will just ruin their actual
  5010. 3:10:04password and reduce the security so in
  5011. 3:10:06certain cases you don't always want to
  5012. 3:10:08use two lower and two upper for
  5013. 3:10:10comparison you want to just check the
  5014. 3:10:12value straight up and the same thing is
  5015. 3:10:14when you're doing a login you don't want
  5016. 3:10:16to be checking the values lowcase
  5017. 3:10:18because that's their login these are
  5018. 3:10:20some of the specific situations where
  5019. 3:10:22you don't want to be using the two low
  5020. 3:10:23function if the data that your
  5021. 3:10:25processing is pretty sensitive and will
  5022. 3:10:27be messed up if you use the to function
  5023. 3:10:29that you should always avoid it
  5024. 3:10:31passwords fit this perfectly and
  5025. 3:10:33usernames as well if someone's added
  5026. 3:10:35some capitals inside the username then
  5027. 3:10:37you don't want to ruin that by doing
  5028. 3:10:38lower case or upper
  5029. 3:10:41case if your username is spelled like
  5030. 3:10:44this and you try and log on like this
  5031. 3:10:46then they're not equal to each other
  5032. 3:10:48because this will probably be two
  5033. 3:10:49different accounts you can have an
  5034. 3:10:51account with a capital B and have
  5035. 3:10:53account with a lowercase b so you don't
  5036. 3:10:54want to be lowercasing the input and
  5037. 3:10:56then doing a comparison because what you
  5038. 3:10:58might actually retrieve is a different
  5039. 3:11:00account than you're looking
  5040. 3:11:02for so now that we talked about stuff
  5041. 3:11:04theoretically let's see how we can do
  5042. 3:11:06things practically so as you heard in
  5043. 3:11:08the last video arrays are a way that we
  5044. 3:11:10can store multiple variables of the same
  5045. 3:11:12data type in one little group so if you
  5046. 3:11:15had something like num one equals 5 in
  5047. 3:11:18number 2 = 10 in number 3 = 15 and we
  5048. 3:11:22had a similar case in one of the
  5049. 3:11:23exercises like this where we had to work
  5050. 3:11:26out if a triangle was actually a
  5051. 3:11:27triangle so we asked the user to input
  5052. 3:11:29three different angles and then we
  5053. 3:11:31stalled them all and then we added them
  5054. 3:11:33up after and it looked something like
  5055. 3:11:35this in total equals num 1 plus num 2
  5056. 3:11:39and then finally plus num3 and then we
  5057. 3:11:42had some sort of if statement after to
  5058. 3:11:44check if the
  5059. 3:11:45total was equal to
  5060. 3:11:48180 and then we did something in the
  5061. 3:11:50curly braces just to Output that it's
  5062. 3:11:52valid and then else output it's
  5063. 3:11:54invalid you can see how this gets a
  5064. 3:11:56little tedious we have to create a new
  5065. 3:11:58variable every single time we want some
  5066. 3:12:00more data to be stored and in this case
  5067. 3:12:02we just simply want another number that
  5068. 3:12:04we can store but we have to just keep a
  5069. 3:12:06pending one to it in that case we could
  5070. 3:12:09have angle one angle two and angle three
  5071. 3:12:11but how about if you had something that
  5072. 3:12:13just called angles for now let's just
  5073. 3:12:15talk about how arrays work how we can
  5074. 3:12:17use them and then after I've went
  5075. 3:12:19through this example about arrays and
  5076. 3:12:20taught you what to do with it then I'll
  5077. 3:12:22will go get the code from the exercise
  5078. 3:12:24from the triangle video and actually
  5079. 3:12:26transform it into using an array so you
  5080. 3:12:28can see what the difference is and how
  5081. 3:12:29the new code will look like so let's get
  5082. 3:12:32rid of this little section so now that
  5083. 3:12:34we've got three integer variables let's
  5084. 3:12:36see how we can do this another way so
  5085. 3:12:38the Syntax for arrays you first type in
  5086. 3:12:40the data type that you want just like
  5087. 3:12:43before and then you add on a square
  5088. 3:12:44bracket and a closed square bracket
  5089. 3:12:46there's not nothing that needs to be
  5090. 3:12:47inside as of right now then you press
  5091. 3:12:50space and type in the variable name as
  5092. 3:12:52per always and then we can make that
  5093. 3:12:54equal to new in three so what this side
  5094. 3:12:57of the equal sign means is we want a new
  5095. 3:12:59instance of an integer array and we want
  5096. 3:13:02to have three positions since we have
  5097. 3:13:04three variables here that we want to
  5098. 3:13:05store we need to have three positions to
  5099. 3:13:07store them so this line in total says
  5100. 3:13:10that we want to integer array we want to
  5101. 3:13:12call it numbers and we want to
  5102. 3:13:13instantiate our new integer array with
  5103. 3:13:16three positions instancia is just the
  5104. 3:13:18term that we use when you turn something
  5105. 3:13:21into a new class at this point if we
  5106. 3:13:23don't use new then we're going to get
  5107. 3:13:25compiler error because it's unsure what
  5108. 3:13:28this is trying to tell you it's trying
  5109. 3:13:29to start an integer inside integer array
  5110. 3:13:32and that makes no sense so it won't
  5111. 3:13:34recognize the square brackets in this
  5112. 3:13:36case because it has no idea what it's
  5113. 3:13:38trying to do and this syntax is
  5114. 3:13:39completely incorrect so the compiler
  5115. 3:13:41doesn't know what error message to give
  5116. 3:13:42you so we can have a new int and what we
  5117. 3:13:45can do now is by us using the square
  5118. 3:13:47bracket reference we can actually
  5119. 3:13:49specify these three variables inside our
  5120. 3:13:51numbers array so now we have access to
  5121. 3:13:54numbers 0 = 5 and then if we use
  5122. 3:13:58controll D to copy this down twice we
  5123. 3:14:00change this to one and two and then
  5124. 3:14:03print 10 here and 15 here so now this is
  5125. 3:14:06exactly the same as this we have three
  5126. 3:14:09integer variables defined separately as
  5127. 3:14:115 10 15 and now that we've created an
  5128. 3:14:14array of numbers we can specify all
  5129. 3:14:16three these values inside our numbers
  5130. 3:14:18array so instead of having three
  5131. 3:14:20individual variables now we just have
  5132. 3:14:22one that holds everything like a group
  5133. 3:14:25and just to show you how this works if
  5134. 3:14:26we have a console right line we can say
  5135. 3:14:28num one space num two space num three
  5136. 3:14:33and if we use controll D to duplicate
  5137. 3:14:35that down then what we can have instead
  5138. 3:14:37of these num ones is simply just place
  5139. 3:14:39these values
  5140. 3:14:41inside changes to one and changes to two
  5141. 3:14:44and what you should notice is these two
  5142. 3:14:46lines are going to print the the exact
  5143. 3:14:47same
  5144. 3:14:49values there we go so in this example
  5145. 3:14:52we've took our sloppy code that will
  5146. 3:14:54roll us around appending one to this
  5147. 3:14:56number creating a new array and then
  5148. 3:14:58printing it out so let's say we want to
  5149. 3:15:00actually use these to read in values
  5150. 3:15:02from the console what can we do and
  5151. 3:15:04you'd use these exactly the same as how
  5152. 3:15:06You' use variables up here this is just
  5153. 3:15:09an integer variable at a specific
  5154. 3:15:11position held inside a group so we could
  5155. 3:15:13simply have a console right line delete
  5156. 3:15:16the right and just say enter a
  5157. 3:15:18number and then instead of starring five
  5158. 3:15:21we can say convert to in32 and then we
  5159. 3:15:23can have a console read
  5160. 3:15:26line and that will read in from the
  5161. 3:15:28console convert it to an integer 32 and
  5162. 3:15:31then store it into the first position of
  5163. 3:15:33our numbers array and now if we just
  5164. 3:15:35copy this down
  5165. 3:15:37twice and we can get rid of these two
  5166. 3:15:39lines cuz this is just staring it
  5167. 3:15:41statically and add one and two to here
  5168. 3:15:44so now what we're saying is enter a
  5169. 3:15:46number star into to 0 1 and 2 and let's
  5170. 3:15:49see what happens now so now we get asked
  5171. 3:15:52the numbers 10 20 and 30 and you can see
  5172. 3:15:54that they've been stored here of course
  5173. 3:15:57these haven't changed cuz this is still
  5174. 3:15:58referencing our static variables up here
  5175. 3:16:01so what you can see in this example is
  5176. 3:16:03now that we've made an array we can
  5177. 3:16:05specify each of the values very easily
  5178. 3:16:07inside here and if so we wanted to have
  5179. 3:16:10a number four instead of having to
  5180. 3:16:12duplicate it and rename it and change
  5181. 3:16:14the value for example we can just simply
  5182. 3:16:17turn this into four duplicate this
  5183. 3:16:19little section and then just change this
  5184. 3:16:21to a three one thing to note in C and
  5185. 3:16:25many other languages almost every single
  5186. 3:16:27index will start at zero in the last
  5187. 3:16:30section we covered the string functions
  5188. 3:16:32and they also started at zero when
  5189. 3:16:34you're dealing with strings and their
  5190. 3:16:36positions when you have a for loop with
  5191. 3:16:38the string then you always want to use
  5192. 3:16:40index Z as the first position the same
  5193. 3:16:43is true here we have four positions
  5194. 3:16:46which means it goes from zero all the
  5195. 3:16:48way till three it never goes from 1 to 4
  5196. 3:16:51or from 0 to 4 these are bad values it
  5197. 3:16:54always goes from 0 to
  5198. 3:16:56three you might be thinking this looks
  5199. 3:17:00really good but when we go to print it
  5200. 3:17:02we're going to have to copy and paste
  5201. 3:17:03this little section add another one and
  5202. 3:17:05rename this to three this gets a little
  5203. 3:17:07bit tedious if you just trying to print
  5204. 3:17:09them all to the screen so let's see how
  5205. 3:17:11we can fix that we can use a four Loop
  5206. 3:17:13for this so we can have four inti equals
  5207. 3:17:16z
  5208. 3:17:17I is less than and then we can have
  5209. 3:17:19numbers length and if you notice from
  5210. 3:17:22iterating through a string variable the
  5211. 3:17:24syntax is exactly the same we have a do
  5212. 3:17:27length property on the arrays as well as
  5213. 3:17:29we do with a string
  5214. 3:17:31variable so in order to print out this
  5215. 3:17:33line what we can do is print one of them
  5216. 3:17:36every time we go around the for Loop
  5217. 3:17:38instead of using a console right line we
  5218. 3:17:40can just use a console
  5219. 3:17:42right so we can have console.
  5220. 3:17:45right and then what we can simply do in
  5221. 3:17:47here is we can say numbers square
  5222. 3:17:50bracket I and then we can have a space
  5223. 3:17:53right after it just to M make what
  5224. 3:17:56happens here we have the number printed
  5225. 3:17:57out and then a space after it number
  5226. 3:18:00printed out a space after it and so on
  5227. 3:18:02and so forth so let's see what happens
  5228. 3:18:04now if we run the code 10 20 30 40 there
  5229. 3:18:10you go so you can see that these two
  5230. 3:18:11lines are exactly the same and now what
  5231. 3:18:13happens is let's just comment these two
  5232. 3:18:15lines out and if we were to change this
  5233. 3:18:17to five and we can duplicate the
  5234. 3:18:20section change this to a four and then
  5235. 3:18:23now we have five variables started and
  5236. 3:18:25we don't need to touch this for Loop
  5237. 3:18:26because the numbers do length will get
  5238. 3:18:28updated anyway so we can have 1 2 3 4 5
  5239. 3:18:32and you can see all five variables have
  5240. 3:18:34been printed automatically what also is
  5241. 3:18:36quite tedious here is we're having to
  5242. 3:18:38add another one to this just so we can
  5243. 3:18:40ask the user how many times that they
  5244. 3:18:42want to enter a number again this is
  5245. 3:18:44quite tedious but we also have a fix for
  5246. 3:18:46that
  5247. 3:18:47because we know how many times we want
  5248. 3:18:49to ask the user to enter a number then
  5249. 3:18:51we can use a for Loop to iterate through
  5250. 3:18:53and ask the user as you can see every
  5251. 3:18:56single line here is exactly the same
  5252. 3:18:58except for the index position that we're
  5253. 3:19:00dealing with and the same is true for
  5254. 3:19:02this for Loop this line will always be
  5255. 3:19:05the same as we're printing out here and
  5256. 3:19:07the only thing that changes between this
  5257. 3:19:09section and this section is the fact
  5258. 3:19:11that this says one and this says zero so
  5259. 3:19:14we can translate this into a f Loop to
  5260. 3:19:17make it
  5261. 3:19:18Dynamic and we can do exactly the same
  5262. 3:19:20thing above here so let's actually copy
  5263. 3:19:23and paste this for
  5264. 3:19:25Loop because we want to do exactly the
  5265. 3:19:27same
  5266. 3:19:29approach we want to go through all the
  5267. 3:19:31positions inside our numbers array and
  5268. 3:19:33then we can actually ask the user to
  5269. 3:19:35enter a number in so let's block comment
  5270. 3:19:37out this massive section and then we can
  5271. 3:19:40take one of them and place it inside the
  5272. 3:19:41for Loop so now we're asking the user
  5273. 3:19:44enter a number and then we're storing
  5274. 3:19:46inside index IND Z and of course if we
  5275. 3:19:48keep using index zero then it's just
  5276. 3:19:50going to override the first one and
  5277. 3:19:51we're not going to store any data so we
  5278. 3:19:53can change that to an i let's see what
  5279. 3:19:55happens when we run it now 1 2 3 4 5 and
  5280. 3:19:59then we get the five numbers being
  5281. 3:20:01printed this is awesome because what
  5282. 3:20:03we've got now is five output commands
  5283. 3:20:05coming straight to the console just from
  5284. 3:20:07a for Loop the for Loop is responsible
  5285. 3:20:10for printing them all out to the screen
  5286. 3:20:11reading in the value convert it to an
  5287. 3:20:14integer and then storing it inside our
  5288. 3:20:15array and then we have a separate for
  5289. 3:20:17Loop to print out to the screen what's
  5290. 3:20:19really really powerful about these two
  5291. 3:20:21for Loops that we've built is if we want
  5292. 3:20:23to have 10 numbers now we simply just
  5293. 3:20:25change this to 10 and then just rerun
  5294. 3:20:27the code as you can see we now have 10
  5295. 3:20:30numbers there you go so what we've just
  5296. 3:20:33done if we move this code down a lot now
  5297. 3:20:35just so we can have it close
  5298. 3:20:37by we've made our entire program that
  5299. 3:20:40was actually pretty lengthy into just a
  5300. 3:20:42couple of lines and two for Loops one
  5301. 3:20:45for Loop is responsible for looping x
  5302. 3:20:47amount of times x depends on whatever
  5303. 3:20:49that number is and then we go through
  5304. 3:20:51and ask the user to enter a number read
  5305. 3:20:54in from the console convert it to an
  5306. 3:20:56integer 32 and start it within the array
  5307. 3:20:59after this for Loop has finished running
  5308. 3:21:01then we have all the numers stored
  5309. 3:21:03inside our aray and then we can go
  5310. 3:21:05through and print them all out to the
  5311. 3:21:06console another way we can print this
  5312. 3:21:09out to the screen is by actually using a
  5313. 3:21:11for each so we can say int num and then
  5314. 3:21:14we want to be a number inside our number
  5315. 3:21:17so for each of the integers inside the
  5316. 3:21:20number we want to create a new local
  5317. 3:21:21variable called num and then we can use
  5318. 3:21:23it in here and if we take this line and
  5319. 3:21:25copy it down everything's exactly the
  5320. 3:21:28same except we don't need to specify the
  5321. 3:21:30main array numbers now and we don't need
  5322. 3:21:32to specify the square brackets we can
  5323. 3:21:34just change this completely and replace
  5324. 3:21:36it with n and if we just make this
  5325. 3:21:38number smaller so we don't have to
  5326. 3:21:39Output a lot of
  5327. 3:21:41values 1 2 and three and you can see
  5328. 3:21:44that they're exactly the same there's no
  5329. 3:21:46line between them CU I don't have a
  5330. 3:21:47console right line so let's quickly add
  5331. 3:21:50that
  5332. 3:21:51in and there we go 1 2 3 and you can see
  5333. 3:21:54they're outputting the exact same
  5334. 3:21:59values the reason why I needed a console
  5335. 3:22:01right line is because these fall Loops
  5336. 3:22:02are using console. right the problem
  5337. 3:22:04with console. right is even after
  5338. 3:22:06finishing writing all of the lines it
  5339. 3:22:08doesn't continue to the next line it
  5340. 3:22:11stays there so if I don't break up the
  5341. 3:22:13two outputs with the console right line
  5342. 3:22:15then all six outputs from the console
  5343. 3:22:17right and the other console right will
  5344. 3:22:19all go on one line this is just for
  5345. 3:22:21demonstration purpose anyway either one
  5346. 3:22:23of these Solutions are fine more people
  5347. 3:22:26tend to use this one the problem with
  5348. 3:22:28these two options is you can't always
  5349. 3:22:29use them
  5350. 3:22:31interchangeably this one will always go
  5351. 3:22:33through every single position it will
  5352. 3:22:36always go from zero and it will always
  5353. 3:22:37go to the length the problem with this
  5354. 3:22:40fall Loop is you can't choose how many
  5355. 3:22:42times you want to iterate through before
  5356. 3:22:44you stop there might be a very specific
  5357. 3:22:46case where you have a big array of data
  5358. 3:22:48but you only want to select a specific
  5359. 3:22:50range if you example where you had a
  5360. 3:22:52th000 array positions so from 0 to 999
  5361. 3:22:56and let's say you wanted to request and
  5362. 3:22:58run a for loop from 500 to 600 so you
  5363. 3:23:01would literally type in 500 in here and
  5364. 3:23:04then 600 inside here and this would be
  5365. 3:23:07fine as long as your array has that many
  5366. 3:23:09positions but with the 4 each Loop if
  5367. 3:23:12you run a 4 each loop on a thousand
  5368. 3:23:14length array then it's going to go
  5369. 3:23:15through every sing single position in
  5370. 3:23:17some cases you might not want that and
  5371. 3:23:19you only want to go through specific
  5372. 3:23:21cases so if you know you're going to go
  5373. 3:23:23through every single case then you can
  5374. 3:23:25use the for each but if you know you're
  5375. 3:23:27going to go through a certain amount of
  5376. 3:23:28cases then it's always best to use the
  5377. 3:23:30for Loop okay so we covered quite a lot
  5378. 3:23:32in this video so let's just have a recap
  5379. 3:23:35so initially we started off saying we
  5380. 3:23:37have our gome one gome two and gome
  5381. 3:23:39three then we had our static variables
  5382. 3:23:41stored inside there and then we created
  5383. 3:23:43our integer array which is now down here
  5384. 3:23:46and just just to make it a little bit
  5385. 3:23:47easier to store multiple variables
  5386. 3:23:49because if you wanted five variables
  5387. 3:23:51you'd now have to duplicate that and
  5388. 3:23:52change that to number five and this will
  5389. 3:23:54get very tedious if we had something
  5390. 3:23:56like 10 like before this is very long
  5391. 3:23:59and you don't need be specifying number
  5392. 3:24:0110 in your code because this also hurts
  5393. 3:24:03the readability of it using arrays can
  5394. 3:24:06solve this problem quite a lot you can
  5395. 3:24:08easily specify array using this notation
  5396. 3:24:11int square brackets followed by the name
  5397. 3:24:14and you make that equal to a new int
  5398. 3:24:16three the three can be any number that
  5399. 3:24:19you want as long as it's a whole number
  5400. 3:24:20cuz this tells us how many positions
  5401. 3:24:22that we've got we then outputed lots of
  5402. 3:24:24values to the console and read them into
  5403. 3:24:26our Ray but we had the same problem as
  5404. 3:24:29we did above where we just had to keep
  5405. 3:24:31duplicating the sections so what we did
  5406. 3:24:33to prevent that is by writing a little
  5407. 3:24:35for Loop that went through every single
  5408. 3:24:37position in numbers array outp puted
  5409. 3:24:39something to the screen read in the
  5410. 3:24:41value converted it and stored it into
  5411. 3:24:43the array and then after all that was
  5412. 3:24:45done we can use a for Loop or for each
  5413. 3:24:48Loop and print that back to the screen
  5414. 3:24:50now that we finished the base content of
  5415. 3:24:51this video and because I said at the
  5416. 3:24:53start that I was going to take the
  5417. 3:24:54triangle exercise and convert that into
  5418. 3:24:56an array so let's do that now so now
  5419. 3:24:59I've copy and paste a solution from the
  5420. 3:25:01triangle exercise straight into this
  5421. 3:25:03project I've just block commented
  5422. 3:25:05everything above just so it doesn't
  5423. 3:25:06interfere with our code so let's just
  5424. 3:25:08run this code to familiarizes with
  5425. 3:25:10what's going on so we enter 60 60 and 60
  5426. 3:25:14then it says valid
  5427. 3:25:17and if you run it again and enter
  5428. 3:25:18something else that doesn't equal to 180
  5429. 3:25:20then we get invalid so let's see how we
  5430. 3:25:23can make this code a lot better by the
  5431. 3:25:25use of arrays so as we said before we
  5432. 3:25:27don't want to be using this angle one
  5433. 3:25:29angle two and angle three let's make an
  5434. 3:25:32INT square bracket angles equal new int
  5435. 3:25:36and then in this case we have three
  5436. 3:25:38angles and what you could do as well is
  5437. 3:25:40you could say const int angle count
  5438. 3:25:43equals
  5439. 3:25:443 and then you can simp L use that
  5440. 3:25:46inside here so later on in the code if
  5441. 3:25:49we wanted to specify this to a different
  5442. 3:25:51value we can just change that later
  5443. 3:25:53on so now that we've got three angles we
  5444. 3:25:56can get rid of these three lines and
  5445. 3:25:58write ourselves a little for Loop so I
  5446. 3:26:01goes from zero all the way to the
  5447. 3:26:03angles. length and then every single
  5448. 3:26:06time we go around in the for Loop we
  5449. 3:26:08want to
  5450. 3:26:10say console. right enter angle one and
  5451. 3:26:15then we can say angles square bracket
  5452. 3:26:19and not zero in this case cuz we don't
  5453. 3:26:21be specifying index as it will go from
  5454. 3:26:23zero to one to two we want to be using I
  5455. 3:26:27I because we're inside a for Loop that's
  5456. 3:26:29already going through all the indexes
  5457. 3:26:31for us and make that equal to convert to
  5458. 3:26:34in32 and then a console.
  5459. 3:26:37readline now we've got all the values
  5460. 3:26:39stored inside here let's deal with this
  5461. 3:26:42problem so if we just comment out the
  5462. 3:26:44total for the time being let's run the
  5463. 3:26:46code
  5464. 3:26:51oh we have one more error down here
  5465. 3:26:53sorry let's run the code again and we
  5466. 3:26:55can say enter angle one and we can type
  5467. 3:26:57the values in but as you can see we've
  5468. 3:27:00got angle one for each of the times what
  5469. 3:27:03we can do in here is use our I index to
  5470. 3:27:05print out the value so we can turn this
  5471. 3:27:08into interpolation and then wrap the one
  5472. 3:27:10inside curly braces and we can simply
  5473. 3:27:12change as one to an i and let's see what
  5474. 3:27:15happens now so we have enter angle zero
  5475. 3:27:18and then one and then two and this is
  5476. 3:27:20fine but humans don't like reading zero
  5477. 3:27:23they like reading 1 2 and three so what
  5478. 3:27:25we can simply do is say I + 1 and
  5479. 3:27:28because we're using I and then adding
  5480. 3:27:30one to it it's not going to affect the
  5481. 3:27:32variable inside the for Loop so there's
  5482. 3:27:34no problems by doing this this is simply
  5483. 3:27:36just for the output purposes so now we
  5484. 3:27:39can have one two and three just shown up
  5485. 3:27:41like that perfect so what we can do now
  5486. 3:27:44that we have all the angles stored now
  5487. 3:27:47we need to replace this line by writing
  5488. 3:27:49a for Loop that can add up all the
  5489. 3:27:50values for us so we can have four each
  5490. 3:27:53in angle inside our angles and we can
  5491. 3:27:57just say int angle sum plus equals
  5492. 3:28:03angle the problem with this is we can't
  5493. 3:28:06specify a new variable inside here and
  5494. 3:28:09then use it outside an if statement so
  5495. 3:28:11what do we do in this case you want to
  5496. 3:28:13take angle sum and initialize it out
  5497. 3:28:16outside the for Loop and then give it a
  5498. 3:28:18value of zero and then inside the for
  5499. 3:28:20Loop we can remove the int and we can
  5500. 3:28:22say angle sum plus equals the whatever
  5501. 3:28:25angle that we're currently up to so if
  5502. 3:28:28we had three values that's going to say
  5503. 3:28:300 + 60 and then take that value and add
  5504. 3:28:33another 60 and then take that value and
  5505. 3:28:35add another 60 so it'll keep a running
  5506. 3:28:37total for us and if we just print out
  5507. 3:28:39angles some to the screen let's see what
  5508. 3:28:41happens 10 20 30 and now we have 60 and
  5509. 3:28:45that's the correct calculation if you
  5510. 3:28:47add them three together so now that we
  5511. 3:28:50have a running total for angle sum then
  5512. 3:28:52we're pretty much done if we get rid of
  5513. 3:28:54this line of code which is our previous
  5514. 3:28:56total and we actually bring back our if
  5515. 3:28:59statement there you go there's no error
  5516. 3:29:01now and all this is doing is checking if
  5517. 3:29:03the angle sum is equal to 180 and then
  5518. 3:29:06print valid and invalid so we can have
  5519. 3:29:0860 60 60 and it prints out valid and
  5520. 3:29:12let's just comment out our
  5521. 3:29:13output and then we can have 66 6059 and
  5522. 3:29:17then it prints out in valid which is the
  5523. 3:29:19exact same behavior that we saw at the
  5524. 3:29:20start so let's remove the extra cord in
  5525. 3:29:23here and keep it like this there you go
  5526. 3:29:27so now we've got our solution just like
  5527. 3:29:29this and it's a lot better because if
  5528. 3:29:31triangles were a transform in the future
  5529. 3:29:33and we wanted more than three sides then
  5530. 3:29:35we can easily update this value because
  5531. 3:29:37now that we're using AR raise or we're
  5532. 3:29:38using a for Loop to enter out the values
  5533. 3:29:41what we can do we don't actually need to
  5534. 3:29:43store it what I mean by that is we can
  5535. 3:29:45actually just get rid rid of this array
  5536. 3:29:46completely let me
  5537. 3:29:49demonstrate we can take this angle sum
  5538. 3:29:52and place it underneath here we can keep
  5539. 3:29:54the count so we can use that in the for
  5540. 3:29:56Loop so we're going from 0o to three
  5541. 3:29:58because that's our angle count and then
  5542. 3:30:00we can get rid of all
  5543. 3:30:02this and let's see what we can do now so
  5544. 3:30:06we have an angle count equals 3 so we go
  5545. 3:30:08from 0 to three which you'll just do as
  5546. 3:30:10we said before from 0 to two because
  5547. 3:30:13it's less than the angle count and let's
  5548. 3:30:16just print this to the screen see what
  5549. 3:30:17happens so the output's still exactly
  5550. 3:30:19the same but what's happening is these
  5551. 3:30:21values are not being stored they're just
  5552. 3:30:23being shown to the screen and then not
  5553. 3:30:25done anything there is no equal sign
  5554. 3:30:28here to assign it into anything so what
  5555. 3:30:30we can do is we can say angle sum plus
  5556. 3:30:33equals the value straight away and now
  5557. 3:30:35if we try it 60 60 60 then we get valid
  5558. 3:30:39the main differences between these two
  5559. 3:30:41situations is in the first one we used
  5560. 3:30:44an array to store all the different
  5561. 3:30:46values and then we added the total but
  5562. 3:30:49if we're not going to use these values
  5563. 3:30:51after then what's the point in storing
  5564. 3:30:53them we can just use a for Loop to ask
  5565. 3:30:55the user how many times they want to
  5566. 3:30:57input the values and then just add them
  5567. 3:30:59every single time that they add the
  5568. 3:31:01calculation in and at the end we could
  5569. 3:31:03just simply use a conditional operator
  5570. 3:31:05to be able to print at the screen
  5571. 3:31:06whether it's valid or not but let me
  5572. 3:31:08just undo that solution so I can show
  5573. 3:31:10you the array solution and we can have a
  5574. 3:31:12quick recap that was just an extra
  5575. 3:31:13little tip just in case you wanted to
  5576. 3:31:15know know what different ways you could
  5577. 3:31:17use to make this work there we go so
  5578. 3:31:19let's just have a quick recap here so we
  5579. 3:31:21have a Conant which all we'll do is let
  5580. 3:31:24us specify three inside here and then we
  5581. 3:31:26can use it here this isn't entirely
  5582. 3:31:28necessary but it does clean up your code
  5583. 3:31:30a little bit because it gives this
  5584. 3:31:32number three a bit of a meaning it says
  5585. 3:31:34the amount of angles is our angle count
  5586. 3:31:37having a three left over like this is
  5587. 3:31:39what we call a magic number they're
  5588. 3:31:41quite bad because it hurts the
  5589. 3:31:42readability of your code people want to
  5590. 3:31:44know what the three actually means so if
  5591. 3:31:47you bring in an angle count then you can
  5592. 3:31:49actually read we want a set of angles
  5593. 3:31:51and this is the amount that we need and
  5594. 3:31:53that's denoted by angle count it adds
  5595. 3:31:56text to a variable it gives you a way of
  5596. 3:31:59making sure that the text equivalent of
  5597. 3:32:02a variable is present so someone can
  5598. 3:32:04just read it after we've created our
  5599. 3:32:06integer we can actually make a for Loop
  5600. 3:32:08that goes from zero to the length and
  5601. 3:32:10then we ask the user to enter the angle
  5602. 3:32:12and in the previous example we had enter
  5603. 3:32:15angle one two and three so now we can
  5604. 3:32:17mimic that by using I but because I
  5605. 3:32:20starts at zero we want to use plus one
  5606. 3:32:22every time we loop around the for Loop
  5607. 3:32:24we ask the user to enter the number read
  5608. 3:32:26it in through console read line convert
  5609. 3:32:28it using convert 2 in 32 and then St
  5610. 3:32:31into the array after this for Loop is
  5611. 3:32:33finished we have all three values stored
  5612. 3:32:35in the angles array and then we have an
  5613. 3:32:37In Sum to count up all the values that
  5614. 3:32:39we need and then we use the fall Loop to
  5615. 3:32:42go through every single integer value
  5616. 3:32:44inside and make a running to total and
  5617. 3:32:46store it inside angle sum once angle sum
  5618. 3:32:49has the total count then what we can do
  5619. 3:32:51is use console right line along with a
  5620. 3:32:53conditional operator to print whether
  5621. 3:32:55it's valid or invalid to the
  5622. 3:32:59screen in this video we're going to look
  5623. 3:33:01at how we can use the built-in array
  5624. 3:33:03functions to be able to sort an array so
  5625. 3:33:05if you have an INT array called numbers
  5626. 3:33:08and then using the previous notation
  5627. 3:33:09that we looked at we can actually
  5628. 3:33:11specify any numbers that we want using a
  5629. 3:33:13common delimited list so if we just have
  5630. 3:33:16nine numbers inside here then we've
  5631. 3:33:18actually defined an integer array and
  5632. 3:33:20then the compiler can count the amount
  5633. 3:33:21of items that are inside here and give
  5634. 3:33:23it the length but we don't have to worry
  5635. 3:33:25about that ourselves so if we use a for
  5636. 3:33:27each and we can just print
  5637. 3:33:30Inn inside numbers we can do a console
  5638. 3:33:34right line on the new local variable
  5639. 3:33:36called num if print it out we can see
  5640. 3:33:38all of the values and maybe we can
  5641. 3:33:40actually just use a console
  5642. 3:33:43right followed by a space just to give
  5643. 3:33:46them all on the same line there we go so
  5644. 3:33:49as you saw before if you had a string
  5645. 3:33:51called test and we wanted to do
  5646. 3:33:53something to it for example if we wanted
  5647. 3:33:55to use the replace function if we wanted
  5648. 3:33:57to remove all spaces with an empty
  5649. 3:33:59string then if we check the replace tool
  5650. 3:34:01tie we can see that it returns a new
  5651. 3:34:03string most of the string functions will
  5652. 3:34:05return you back the new string after
  5653. 3:34:07doing something with it but in this case
  5654. 3:34:10if we type in Array followed by do sort
  5655. 3:34:13and we can place our array straight in
  5656. 3:34:15the bracket
  5657. 3:34:16what you realize is if you hover over
  5658. 3:34:18sord it will sort the elements in the
  5659. 3:34:20entire array but it will indeed return
  5660. 3:34:22back a void and vo just means it returns
  5661. 3:34:25back nothing so in this case what it's
  5662. 3:34:27doing is you're actually affecting the
  5663. 3:34:29numbers array straight away and you
  5664. 3:34:32don't have to do numbers equals array do
  5665. 3:34:34sort numbers for example if we were
  5666. 3:34:36doing the test replace we would have to
  5667. 3:34:38do this otherwise this would just be
  5668. 3:34:41returning back a string but we wouldn't
  5669. 3:34:43be using it the same is not true for the
  5670. 3:34:45arrays when you run the array functions
  5671. 3:34:48they tend to return back void and you
  5672. 3:34:50don't have to do anything other than
  5673. 3:34:52give it the array that you wanted to
  5674. 3:34:53sort so now if you run this code you
  5675. 3:34:56should just see the same output since
  5676. 3:34:57they already sorted anyway so let's
  5677. 3:35:00change some of these numbers
  5678. 3:35:02up and just see what happens
  5679. 3:35:05now there we go 02 55 6 7 8 999 and
  5680. 3:35:10that's it sorted just like that there
  5681. 3:35:12isn't a whole lot to this sorting
  5682. 3:35:14function but what you should note is you
  5683. 3:35:16can use this new array syntax to Define
  5684. 3:35:18how many items that you want in there
  5685. 3:35:20instead of having to change the amount
  5686. 3:35:22of positions that you have using the
  5687. 3:35:23square brackets you can just denote any
  5688. 3:35:26number of items that you want inside the
  5689. 3:35:28compiler will count them for you and PR
  5690. 3:35:30the length straight into the bracket for
  5691. 3:35:32you so you don't have to worry about it
  5692. 3:35:33using the array. sort function all you
  5693. 3:35:36have to do is give it the parameter of
  5694. 3:35:38the array that you want to sort and it
  5695. 3:35:40will affect the array straight away and
  5696. 3:35:42you don't have to restore it and then
  5697. 3:35:44we're just using a simple 4 each Loop to
  5698. 3:35:46print them out to the
  5699. 3:35:49screen in this video I want to discuss
  5700. 3:35:51the array reverse function so if you had
  5701. 3:35:53an INT numbers equal new int and if you
  5702. 3:35:57just had lots of numbers in here
  5703. 3:35:59defined just up till five then we can
  5704. 3:36:01add a semicolon and again using this
  5705. 3:36:04array function we can say array. reverse
  5706. 3:36:06and pass our array
  5707. 3:36:09inside and then we can just use a for H
  5708. 3:36:11of our item inside our
  5709. 3:36:14numbers and simply just print them out
  5710. 3:36:16using a console. write and we can use
  5711. 3:36:18string interpolation and just put a
  5712. 3:36:19space at the end there we go and if you
  5713. 3:36:22run the code then you can see our
  5714. 3:36:25numbers have just been reversed there
  5715. 3:36:26isn't a lot to this video I just wanted
  5716. 3:36:28to show you about the different
  5717. 3:36:29functions that you have available for
  5718. 3:36:31arrays these can come in handy when you
  5719. 3:36:33have something like a list returned from
  5720. 3:36:34a database for example and it's ordered
  5721. 3:36:37in a certain way let's say you're
  5722. 3:36:38returning back a list of lots of people
  5723. 3:36:40who are ordered back alphabetically and
  5724. 3:36:42it's going from a to zed and let's say
  5725. 3:36:44you have some sort of user interface
  5726. 3:36:46allows you to flip the order instead of
  5727. 3:36:48having to request the database to get
  5728. 3:36:50the data back in the opposite order you
  5729. 3:36:52can use this array function to just
  5730. 3:36:53reverse the list the same thing goes if
  5731. 3:36:55you're trying to order by lowest price
  5732. 3:36:57or highest price you can use this to
  5733. 3:36:59show the different values in different
  5734. 3:37:00orders that's where this array function
  5735. 3:37:02comes in handy when you have different
  5736. 3:37:04situations like the names or the price
  5737. 3:37:06by ascending or descending where you can
  5738. 3:37:08just flip the data instead of having to
  5739. 3:37:11request it back from the database again
  5740. 3:37:13flipping it locally is a lot easier than
  5741. 3:37:15going back to the database is that uses
  5742. 3:37:17more data and more computational power
  5743. 3:37:19on the back end and then return it to
  5744. 3:37:21the front end it's always best to be
  5745. 3:37:22able to use these functions instead of
  5746. 3:37:24having to use external things to be able
  5747. 3:37:26to return back the same data using it
  5748. 3:37:29locally like this will give us a
  5749. 3:37:30performance Advantage because it's done
  5750. 3:37:32directly on the data instead of having
  5751. 3:37:34to request everything again if you
  5752. 3:37:36wanted to do this manually let's have a
  5753. 3:37:38look and see what your options are you
  5754. 3:37:41can have your integer array just defined
  5755. 3:37:43like this with a dynamic list and then
  5756. 3:37:45we can have a sorted array using the
  5757. 3:37:47same length as the current array what
  5758. 3:37:49you can do instead of using reverse we
  5759. 3:37:52can actually just comment these lines
  5760. 3:37:53out you can bring back a normal for Loop
  5761. 3:37:56but if we use two RS it gives us a
  5762. 3:37:58reverse for Loop so the length is going
  5763. 3:38:00to be our numbers do length and the rest
  5764. 3:38:03of the paramet is exactly the same we
  5765. 3:38:05want to go from the last minus one all
  5766. 3:38:07the way till zero and we want to
  5767. 3:38:09decrement in one and we can say our
  5768. 3:38:11sorted number I is equal to numbers all
  5769. 3:38:15right but all this will do right now is
  5770. 3:38:17just give us back the same value stored
  5771. 3:38:19so what we need to do in a case like
  5772. 3:38:21this is make another variable something
  5773. 3:38:23like X and we can store that into zero
  5774. 3:38:26and what we can say is X is assigned the
  5775. 3:38:28value of whatever this numbers is
  5776. 3:38:30because this is going backwards but we
  5777. 3:38:32want this one to go forwards cuz we want
  5778. 3:38:34it to be reversed and we can say x++ and
  5779. 3:38:37see what happens when we run it
  5780. 3:38:39now so nothing's going to come up
  5781. 3:38:41because we don't have any output so
  5782. 3:38:42let's use a for
  5783. 3:38:44each we can have VAR
  5784. 3:38:47number inside our sorted numbers and we
  5785. 3:38:50can just take this and place it in here
  5786. 3:38:52and just print out the item and we can
  5787. 3:38:54use
  5788. 3:38:56num and there you go it's been flipped
  5789. 3:38:59the exact same value is just the other
  5790. 3:39:01way so this is obviously a little bit
  5791. 3:39:03more complicated because you have to use
  5792. 3:39:05an additional variable use a backwards
  5793. 3:39:07for Loop and then incent your own
  5794. 3:39:09separate variable and as well as
  5795. 3:39:11completely make a whole new array of
  5796. 3:39:13course you should only do this if you
  5797. 3:39:14need it as two separate arrays if not
  5798. 3:39:16you can always just affect the one that
  5799. 3:39:18you're up to right
  5800. 3:39:20now because I said before that arrays
  5801. 3:39:23are actually static data and they're
  5802. 3:39:24going to be fixed size then there's no
  5803. 3:39:27way to remove or add an item but you can
  5804. 3:39:29clear what you have already so let's
  5805. 3:39:31have a look array. clear sets a range of
  5806. 3:39:35elements in an array to the default
  5807. 3:39:37value of each element type so the first
  5808. 3:39:39parameter takes our array so let's use
  5809. 3:39:41numbers and we want to start off the
  5810. 3:39:43index that we want to clear from and
  5811. 3:39:45let's say zero and because we want to
  5812. 3:39:47clear the entire array let's go all the
  5813. 3:39:49way to numbers. length and now let's
  5814. 3:39:52just produce a for each our item inside
  5815. 3:39:55numbers and then just have a console
  5816. 3:39:57right string interpolation which just
  5817. 3:40:00prints out the item followed by space
  5818. 3:40:02and now let's run the code and see what
  5819. 3:40:04happens so we have lots of zeros in the
  5820. 3:40:06console and this is to be expected
  5821. 3:40:08because as we've talked about before the
  5822. 3:40:10default value for an integer is indeed
  5823. 3:40:13zero so all this is doing when it says
  5824. 3:40:15is it's clearing it's just actually
  5825. 3:40:17assigning all of the positions the value
  5826. 3:40:19of zero and we can mimic this quite
  5827. 3:40:21easily through a for Loop so if we
  5828. 3:40:24comment this out and write ourselves as
  5829. 3:40:26a for Loop in I equals z and we want to
  5830. 3:40:28go to numbers. length and then simply
  5831. 3:40:31inside we just want to say numbers
  5832. 3:40:33square braet I equals to 0 what you can
  5833. 3:40:37also write is the word default this will
  5834. 3:40:39pick up the default value for the data
  5835. 3:40:41type that you specified you can type in
  5836. 3:40:43zero or you can type in default it's up
  5837. 3:40:46to you but I think in this case if
  5838. 3:40:48you're trying to reset it back to
  5839. 3:40:49default then you don't have to worry
  5840. 3:40:51about remembering it or any typos you
  5841. 3:40:53can just use the keyword default so if
  5842. 3:40:56we run this code now let's see what
  5843. 3:40:57happens so effectively we've mimicked
  5844. 3:41:00what this function is doing but let's
  5845. 3:41:02have a look and see what other
  5846. 3:41:03parameters we have access to so if we
  5847. 3:41:06comment this out and drop the value then
  5848. 3:41:08you can still see we only have one
  5849. 3:41:10Constructor normally you'd see the open
  5850. 3:41:12down arrows on the left hand side to say
  5851. 3:41:14one of two in this case we only have
  5852. 3:41:16access to one Constructor it will only
  5853. 3:41:18give us a range of elements we want to
  5854. 3:41:21remove but really we just want to clear
  5855. 3:41:23it does say here in the tool tape the
  5856. 3:41:25array whose elements need to be cleared
  5857. 3:41:28and because it is a clear and not a
  5858. 3:41:29remove you got to remember when using
  5859. 3:41:31this function it doesn't actually remove
  5860. 3:41:33any of the positions it just resets the
  5861. 3:41:35values inside the array so what we can
  5862. 3:41:37do in this case is we can say numbers
  5863. 3:41:40comma 5 comma 5 and what that will do is
  5864. 3:41:44the first param arer is where you want
  5865. 3:41:46to start so if you want to start at the
  5866. 3:41:48fifth position and then we want to count
  5867. 3:41:50five forward and because we have 10
  5868. 3:41:53numbers then this should return back
  5869. 3:41:54okay let's comment out this for Loop so
  5870. 3:41:57it doesn't take an effect and let's run
  5871. 3:41:59the code and see what happens now so now
  5872. 3:42:01we took the position five which is
  5873. 3:42:03actually the sixth number because it's
  5874. 3:42:04zero base and said we want to remove
  5875. 3:42:07five forward so it's s and then four
  5876. 3:42:10more forward and there we go we have
  5877. 3:42:12five numbers being removed so instead of
  5878. 3:42:14removing all of the different values we
  5879. 3:42:16can actually clear a certain amount that
  5880. 3:42:18we want and to mimic this in a for Loop
  5881. 3:42:21we can do the following we can start it
  5882. 3:42:23at five and we want to go to 10 and
  5883. 3:42:26let's run the code and you can see the
  5884. 3:42:29output is exactly the same when doing in
  5885. 3:42:32a for Loop you need to actually
  5886. 3:42:33calculate what this is going to be so we
  5887. 3:42:35need to take both the numbers the fifth
  5888. 3:42:37index and the five length which is 5 + 5
  5889. 3:42:40and then we get 10 and we don't make
  5890. 3:42:42this less than or equal to 10 because we
  5891. 3:42:45have 10 numbers so we actually want to
  5892. 3:42:47go from position five to position 9
  5893. 3:42:50which will equate to less than 10 we
  5894. 3:42:52actually want to go from position five
  5895. 3:42:54all the way to position 9 which actually
  5896. 3:42:56covers five numbers 5 6 7 8 9 even
  5897. 3:43:01though it feels like the difference
  5898. 3:43:02between the two numbers is only four
  5899. 3:43:05because 9 - 5 is 4 but we actually have
  5900. 3:43:07five numbers between that 1 2 3 4 5
  5901. 3:43:11there we go so when we print the values
  5902. 3:43:14from 5 to till less than 10 we're going
  5903. 3:43:16from 5 to 9 which is exactly what this
  5904. 3:43:18is doing this is the index that you
  5905. 3:43:21start at and this is how many numbers
  5906. 3:43:23you want to count forward so let's just
  5907. 3:43:25have a quick recap of this function we
  5908. 3:43:27defined our numbers array using array.
  5909. 3:43:30clear it doesn't actually clear the
  5910. 3:43:32values it just gives us access to
  5911. 3:43:34removing them back to their default
  5912. 3:43:36values it doesn't actually clear the
  5913. 3:43:38array it just resets the values back to
  5914. 3:43:40their default type we can have numbers
  5915. 3:43:43parameter which will equal to our array
  5916. 3:43:45and we want to say we want to go from
  5917. 3:43:47index zero all the way to numbers.
  5918. 3:43:50length which will be every single value
  5919. 3:43:52inside the array and all that will do is
  5920. 3:43:54go through the array and set them all to
  5921. 3:43:56their default value which is similar to
  5922. 3:43:59what this for Loop is doing and before
  5923. 3:44:01we had in I equals 0 and I is less than
  5924. 3:44:04numbers. length and that will go through
  5925. 3:44:06every single position you can
  5926. 3:44:07technically use zero instead of default
  5927. 3:44:10but that would mean you have to remember
  5928. 3:44:11the default value of every single data
  5929. 3:44:13type it's easier to just the default
  5930. 3:44:15keyword and it doesn't promote any
  5931. 3:44:17typers using the default keyword is by
  5932. 3:44:20far the easiest and the most safest
  5933. 3:44:22option if you're going to have this
  5934. 3:44:23situation so making every number's index
  5935. 3:44:26equal to default in this case because
  5936. 3:44:28we're using integers it's going to
  5937. 3:44:30assign the value of zero and then after
  5938. 3:44:32this follow loes run with I is zero and
  5939. 3:44:34I is less than numbers. length it's
  5940. 3:44:37going to make all of these positions
  5941. 3:44:38equal to zero which is in essence what
  5942. 3:44:41this function is here for because arrays
  5943. 3:44:43can't be added or changed in terms terms
  5944. 3:44:45of the values that they have after
  5945. 3:44:46they've been created you can only really
  5946. 3:44:49modify what's already there or you can
  5947. 3:44:51reset them all back to zero using this
  5948. 3:44:53notation from zero to numbers length you
  5949. 3:44:56can get rid of all the values and make
  5950. 3:44:58them all zero using 5 to five means it
  5951. 3:45:01will start index 5 which is the sixth
  5952. 3:45:03position and then go five numbers
  5953. 3:45:05forward so it will delete these and
  5954. 3:45:07reset them back to zero you can mimic
  5955. 3:45:09this in a follow Loop just like this
  5956. 3:45:11start in I at five and then go to less
  5957. 3:45:14than 10 and the reason why it's less
  5958. 3:45:16than 10 is 5 + 5 gives you the 10th
  5959. 3:45:18index but because it's I is less than 10
  5960. 3:45:21you actually want to go to the 9th index
  5961. 3:45:24because from 5 to 9 that's actually
  5962. 3:45:26covering five different numbers and then
  5963. 3:45:28we can go through and assign the default
  5964. 3:45:30value and then when we print it out
  5965. 3:45:32you'll see that all of these numbers
  5966. 3:45:34have turned into zeros just like
  5967. 3:45:38so searching through arrays are very
  5968. 3:45:41helpful when you try to find some data
  5969. 3:45:43inside your array and you want to return
  5970. 3:45:45back the position to maybe do something
  5971. 3:45:47else with it so let's have a look at
  5972. 3:45:48this example so we have a numbers array
  5973. 3:45:51and let's say we want to search number
  5974. 3:45:53199 and find the index of it so first
  5975. 3:45:56let's ask the user what the number they
  5976. 3:45:58want to search enter number to search
  5977. 3:46:01then we can have in search is equal to
  5978. 3:46:04convert to in32 and we're converting the
  5979. 3:46:07console.
  5980. 3:46:09readline and then let's have a look at
  5981. 3:46:11the function so inside the array Library
  5982. 3:46:13we have an index of surges for the
  5983. 3:46:16specified object and Returns the index
  5984. 3:46:18of the first occurrence in a
  5985. 3:46:19one-dimensional array this is very
  5986. 3:46:21important as it says the first
  5987. 3:46:23occurrence which means if there was
  5988. 3:46:25another 199 at the end it's only going
  5989. 3:46:28to return you back this one and then
  5990. 3:46:30stop immediately and not give you the
  5991. 3:46:32value of this one so let's have a look
  5992. 3:46:34in the parameters and see what it takes
  5993. 3:46:36it takes our array and then a value so
  5994. 3:46:39our array is called numbers and the
  5995. 3:46:41value you want to search by is our
  5996. 3:46:42variable called search if you hover over
  5997. 3:46:44the index function again it says that it
  5998. 3:46:47returns back at integer zerob based
  5999. 3:46:49index of the first occurrence of the
  6000. 3:46:51value in the entire array if it was
  6001. 3:46:53found otherwise the value is going to be
  6002. 3:46:55minus one so we can say inter position
  6003. 3:46:58is equal to this and because it says if
  6004. 3:47:01it's going to be found then it's going
  6005. 3:47:03to have the value of a zero index and if
  6006. 3:47:05it's not going to be found it's going to
  6007. 3:47:07have the value of minus1 so we can say
  6008. 3:47:09if the position is greater than minus1
  6009. 3:47:12because if it's greater than minus1 that
  6010. 3:47:14means the value is zero onwards we can
  6011. 3:47:18also write this by saying if the
  6012. 3:47:19position is greater than or equal to
  6013. 3:47:21zero it's just up to you they both mean
  6014. 3:47:24the same thing and then we can say in
  6015. 3:47:26here number
  6016. 3:47:29search has been found at position
  6017. 3:47:34position there we go so let's run the
  6018. 3:47:36code and let's do a perfect situation so
  6019. 3:47:39we can say 50 and you can see it says
  6020. 3:47:41number 50 has been found at position
  6021. 3:47:43three and that's the fourth index since
  6022. 3:47:46it's zero based and what you could do if
  6023. 3:47:48you wanted to is actually add one to
  6024. 3:47:50this just so it looks like a bit more of
  6025. 3:47:52a readable number because four is going
  6026. 3:47:54to be the fourth number 1 2 3 4 and
  6027. 3:47:59let's try find a number that hasn't came
  6028. 3:48:01up like 60 it's going to return us back
  6029. 3:48:03with nothing this is really bad because
  6030. 3:48:05it looks like the search has failed
  6031. 3:48:07which it has but you need to Output a
  6032. 3:48:09message to let the user know so have
  6033. 3:48:11cons right line and say number search
  6034. 3:48:15has not been
  6035. 3:48:16found let's type that again 60 number 60
  6036. 3:48:20has not been found so let's see what we
  6037. 3:48:22can do to be able to write this function
  6038. 3:48:24ourselves if we just block comment this
  6039. 3:48:26for the time being we can actually use
  6040. 3:48:29in
  6041. 3:48:31position equals to minus1 and the reason
  6042. 3:48:34why I make equal to minus1 because when
  6043. 3:48:36we read the tool tip with this index
  6044. 3:48:38function it said if it's not going to be
  6045. 3:48:39found it's set to minus one so why don't
  6046. 3:48:41we set it to minus one first and then we
  6047. 3:48:43can try and find it so inti equals z and
  6048. 3:48:46we want to go to numbers. length and
  6049. 3:48:50inside here we want to scan each of the
  6050. 3:48:52positions so we can say numbers square
  6051. 3:48:54braet I and if that is equal to our
  6052. 3:48:57search then we can set the position
  6053. 3:48:59equal to I if the number we're up to is
  6054. 3:49:02equal to what the user has typed in then
  6055. 3:49:04we want to set our position to the index
  6056. 3:49:06we're currently up to and we don't need
  6057. 3:49:08to have an else because we've already
  6058. 3:49:09set it to minus one and after the for
  6059. 3:49:12Loop we can actually take this if
  6060. 3:49:14statement and put it back down here
  6061. 3:49:16because the code's going to be the same
  6062. 3:49:18if the position is greater than minus1
  6063. 3:49:20which in this case is our default value
  6064. 3:49:23so in essence if this value has changed
  6065. 3:49:25then this should always pass EG we've
  6066. 3:49:28found the number so let's run the code
  6067. 3:49:31and let's type a number in 30 now it's
  6068. 3:49:34found at position five now if we try 60
  6069. 3:49:37again number 60 has not been found there
  6070. 3:49:41we go so this function is exactly doing
  6071. 3:49:44some something similar to this it's
  6072. 3:49:46going through every single position
  6073. 3:49:48finding the variable and if they match
  6074. 3:49:50assigning the index and then after just
  6075. 3:49:53returning back to position back to the
  6076. 3:49:55console so let's comment our example and
  6077. 3:49:57let's see what other variants of the
  6078. 3:49:59function we can use and let's comment
  6079. 3:50:00back the first
  6080. 3:50:02one so using this line we can duplicate
  6081. 3:50:05it and just comment it so we have some
  6082. 3:50:07other parameters inside here if we add a
  6083. 3:50:10comma we can actually give it a start
  6084. 3:50:12Index this is the starting index of the
  6085. 3:50:14search and let's say you only want to
  6086. 3:50:17search from this second number onwards
  6087. 3:50:19then you can just type in the number two
  6088. 3:50:21so now if you've run the code and type
  6089. 3:50:22in 90 it's going to say 90 is not found
  6090. 3:50:25cuz 90 is the first one and let's try a
  6091. 3:50:28value like 50 now it says the number 50
  6092. 3:50:31is being found at position 4 because now
  6093. 3:50:33it's included in the range from two
  6094. 3:50:35onwards because this is two onwards
  6095. 3:50:37it'll be position two until the end of
  6096. 3:50:39the array if you wanted a very specific
  6097. 3:50:41range then you can actually give it
  6098. 3:50:43another value so so we can say we want
  6099. 3:50:45to go from zero and then we only want to
  6100. 3:50:47go two forward so if you run that now
  6101. 3:50:50and type in 22 it say is not being found
  6102. 3:50:53cuz we' started here and we said 1 2 so
  6103. 3:50:57now it's only searching between 90 and
  6104. 3:50:59199 and if you wanted to include the
  6105. 3:51:02middle ones you could say you want to
  6106. 3:51:04start a position one and go two forward
  6107. 3:51:06so now you get these two numbers and so
  6108. 3:51:08on and so forth so now if we try number
  6109. 3:51:1090 you'll see it's not being found
  6110. 3:51:13because 90 is not one of these two the
  6111. 3:51:15reason why you'd use different ones like
  6112. 3:51:17this if I revert back the first
  6113. 3:51:20one this will go from two till the end
  6114. 3:51:23this will go specific range and this
  6115. 3:51:25will go through everything the reason
  6116. 3:51:27why these exist is if you have a very
  6117. 3:51:29very big array let's say it's a th000
  6118. 3:51:31indexes then you don't want to search
  6119. 3:51:34everything unless you really need to
  6120. 3:51:36let's say for example you had a nam's
  6121. 3:51:37array and it was sorted alphabetically
  6122. 3:51:40and the person's name that you wanted to
  6123. 3:51:41search began with an a there is no point
  6124. 3:51:44in running the search all the way from a
  6125. 3:51:45to zed if you know it begins with a and
  6126. 3:51:48let's say for example you found the
  6127. 3:51:50first index of the user that has a name
  6128. 3:51:53B so you have now the indexes from the
  6129. 3:51:56start of the array up until the last
  6130. 3:51:59array position that has the value of
  6131. 3:52:02someone's name beginning with a so now
  6132. 3:52:04you have the two indexes of every single
  6133. 3:52:06a name inside this array if this was a
  6134. 3:52:10this was B and this was C and this was D
  6135. 3:52:12and you had these two positions then you
  6136. 3:52:15should only be searching all of the a
  6137. 3:52:17names instead of searching the entire
  6138. 3:52:19array and the reason for that is you'll
  6139. 3:52:21be searching lots more data for no
  6140. 3:52:23apparent reason and it's going to take
  6141. 3:52:24an extra long time for searching
  6142. 3:52:27something that you're never going to
  6143. 3:52:28find if you're searching for a name that
  6144. 3:52:29begins with a there is no point in
  6145. 3:52:31searching for anything A B C D and so on
  6146. 3:52:34and so forth that's where you'd use the
  6147. 3:52:36very specific range you can say you want
  6148. 3:52:38to go from zero all the way up to the
  6149. 3:52:40first B name so then you're only
  6150. 3:52:42searching the a names the this is just
  6151. 3:52:44one example and there is a lot more
  6152. 3:52:47every situation is very unique so use
  6153. 3:52:49the parameters that you want and always
  6154. 3:52:52use the ones that can get you the best
  6155. 3:52:54performance the least amount of things
  6156. 3:52:56that you can search is the best way
  6157. 3:52:58forward if you don't know what you're
  6158. 3:53:00going to be searching then you should
  6159. 3:53:02always use the top one which will just
  6160. 3:53:03search everything if you know where the
  6161. 3:53:06value is going to be or roughly going to
  6162. 3:53:08be then you can use this one or this one
  6163. 3:53:11to give you a better range and it's a
  6164. 3:53:13bit more focused instead of searching
  6165. 3:53:14for everything so a quick recap of this
  6166. 3:53:17index function what it will do is it
  6167. 3:53:19will return you back to position if it's
  6168. 3:53:21found it the position will be zero based
  6169. 3:53:23just as arrays are if it hasn't found
  6170. 3:53:26the position then it's going to return
  6171. 3:53:27this variable with minus one so
  6172. 3:53:30therefore after you've run the code you
  6173. 3:53:32can check if the value is greater than
  6174. 3:53:34minus one which will be from zero
  6175. 3:53:36onwards if the value position is zero
  6176. 3:53:38onwards then you know that that number
  6177. 3:53:40has been found and you can use the
  6178. 3:53:42position variable to Output that to the
  6179. 3:53:44screen if the value position is minus1
  6180. 3:53:47it means the array index function has
  6181. 3:53:50returned back negative and the value
  6182. 3:53:52position is now minus one so you can
  6183. 3:53:54output to the user that it has not been
  6184. 3:53:56found just in my little explanation down
  6185. 3:53:58below you can see that we can mimic this
  6186. 3:54:00function by setting the value of
  6187. 3:54:02position to minus one using a fall Loop
  6188. 3:54:05that goes from zero to numbers length
  6189. 3:54:07and if you want to two as well you could
  6190. 3:54:09use these inside here if you wanted to
  6191. 3:54:12mimic this this goes from two till the
  6192. 3:54:14end so you can say I equals 2 and you
  6193. 3:54:17still want to go to the length this one
  6194. 3:54:19goes from one and then two ahead so you
  6195. 3:54:22can actually put I equal 1 and then
  6196. 3:54:24because it's two ahead from one instead
  6197. 3:54:27of numbers. length it will just be I is
  6198. 3:54:29less than 3 because 1 + 2 gives you the
  6199. 3:54:32three so in order to do this manually
  6200. 3:54:35you can Lube through the elements that
  6201. 3:54:36you want check if the number you're up
  6202. 3:54:38to based off the index position of I is
  6203. 3:54:41equal to the search variable that the
  6204. 3:54:43user typed in of here and if you find a
  6205. 3:54:45match then you can assign the position
  6206. 3:54:48to the value of I after the for Loop is
  6207. 3:54:50run you can check the same if statement
  6208. 3:54:52as we discussed above these are copy and
  6209. 3:54:54pasted down below just to give you an
  6210. 3:54:56example in this little
  6211. 3:55:00section all these arrays have static
  6212. 3:55:02data which means that when you
  6213. 3:55:04initialize them with a certain length
  6214. 3:55:05you can't increase that length or
  6215. 3:55:07decrease the length you can only modify
  6216. 3:55:09the data inside it and you can't delete
  6217. 3:55:11any rows if you create an array of size
  6218. 3:55:14is five and you only stall three people
  6219. 3:55:16in then now you have two positions that
  6220. 3:55:18are being unused and that's unnecessary
  6221. 3:55:21amount of data stored in your memory if
  6222. 3:55:23you're not going to be using it up in
  6223. 3:55:25certain situations like when you're
  6224. 3:55:27making a game for example Tic Tac Toe
  6225. 3:55:29then you know you're always going to
  6226. 3:55:31need a 3X3 grid so a 2d array will be
  6227. 3:55:35perfect in this situation cuz you know
  6228. 3:55:37the exact width and height of your array
  6229. 3:55:39so you can do this perfectly whereas
  6230. 3:55:42lists are quite Dynamic if if you don't
  6231. 3:55:44know how many people are going to be
  6232. 3:55:46within a system for example if you're
  6233. 3:55:48signing up some sort of register for a
  6234. 3:55:51class or for a school or something of
  6235. 3:55:52the sort and you don't know how many
  6236. 3:55:54people are going to be in that class
  6237. 3:55:56then you can use something called a list
  6238. 3:55:58and list a dynamic cuz what you do when
  6239. 3:56:00you initialize it you just say that you
  6240. 3:56:02simply want a list and you can add items
  6241. 3:56:05and you can remove items and you can
  6242. 3:56:07also modify them if you wanted to expand
  6243. 3:56:10the size of an array it's slightly
  6244. 3:56:12complicated but it's not impossible
  6245. 3:56:14so if we had int numbers equals new int
  6246. 3:56:17and let's make that length four and
  6247. 3:56:19let's say we had a numbers array that we
  6248. 3:56:21wanted to increase to length
  6249. 3:56:24five we could have new numbers change
  6250. 3:56:26that position five and then what we're
  6251. 3:56:28going to have to do is from position
  6252. 3:56:30zero all the way to three which is our
  6253. 3:56:33fourth length from the numbers array
  6254. 3:56:35we're going to have to copy that into 0
  6255. 3:56:38to three of the new numbers array so now
  6256. 3:56:40they have the extra data plus the last
  6257. 3:56:43one which will give us our four so
  6258. 3:56:45that'll be five length 0 to three gives
  6259. 3:56:47us our four numbers and then we have one
  6260. 3:56:50empty one so we've just made a new array
  6261. 3:56:52and then copied all the data from
  6262. 3:56:54numbers into new numbers now of course
  6263. 3:56:56this is not really practical that if you
  6264. 3:56:57want to reduce the size or increase the
  6265. 3:56:59size you're going to have to do this
  6266. 3:57:01each time of course you don't want to do
  6267. 3:57:03this you just want to use something
  6268. 3:57:04called a list so let's stick with an
  6269. 3:57:06example and see what we can do so you
  6270. 3:57:08make this length
  6271. 3:57:09three and let's just put three numbers
  6272. 3:57:12inside here if you remember from the
  6273. 3:57:13array video we can also declare it like
  6274. 3:57:15this we can say number 0al 1 and then 1
  6275. 3:57:20and then two and change these values
  6276. 3:57:22these two are doing exactly the same
  6277. 3:57:24thing I'm initializing it with 1 2 and
  6278. 3:57:26three and also just saying that all the
  6279. 3:57:28positions in here are equal to 1 2 and
  6280. 3:57:30three so we don't need these twice but I
  6281. 3:57:32was just showing you as a quick recap so
  6282. 3:57:34we're declaring array we're calling it
  6283. 3:57:36numbers and we're giving it a length of
  6284. 3:57:38three and initializing it to 1 2 and
  6285. 3:57:41three now let's see the syntax to define
  6286. 3:57:43a list
  6287. 3:57:44you start off with a word list and then
  6288. 3:57:46you have a left and a right arrow which
  6289. 3:57:48makes a little section inside the
  6290. 3:57:50section you actually want to State the
  6291. 3:57:51data type that you want so if we're
  6292. 3:57:53copying the numbers array we can have an
  6293. 3:57:55INT inside here and that's it for the
  6294. 3:57:57Declaration of the data type and then we
  6295. 3:57:59can say new numbers or List numbers
  6296. 3:58:02equals to new and then there you go it
  6297. 3:58:06gets Auto filled for you but let's just
  6298. 3:58:08type it in we have list open the arrow
  6299. 3:58:11in in the middle and then close it so
  6300. 3:58:13now it's the same same on both sides but
  6301. 3:58:16because this is a new we need to add a
  6302. 3:58:17Constructor so we can open the curly
  6303. 3:58:19braces and the first Constructor says
  6304. 3:58:21initialize a new instance of the list
  6305. 3:58:23class that is empty and has the default
  6306. 3:58:25initial capacity press the down arrow we
  6307. 3:58:28can give it a capacity of the number of
  6308. 3:58:30elements that the new list can initially
  6309. 3:58:31store so we can start it off with five
  6310. 3:58:34length or three length or whatever it
  6311. 3:58:36doesn't actually matter it just means
  6312. 3:58:38that we actually have three positions
  6313. 3:58:39available immediately if we place three
  6314. 3:58:42in the brackets
  6315. 3:58:44and the last one we can actually give it
  6316. 3:58:46entire collection so let's say if we
  6317. 3:58:48loaded a list of numbers from a database
  6318. 3:58:50or a list of something from database and
  6319. 3:58:53we can actually insert it straight into
  6320. 3:58:55this list by putting a list inside these
  6321. 3:58:57brackets of course right now we don't
  6322. 3:58:59have a list that we're going to assign
  6323. 3:59:01it with so let's just leave this empty
  6324. 3:59:03and close the brackets and add a
  6325. 3:59:04semicolon so we could say list numbers
  6326. 3:59:07do add and then we can add one and then
  6327. 3:59:10duplicate this down and add two and add
  6328. 3:59:12three so now these do have exactly the
  6329. 3:59:15same contents instead of doing this you
  6330. 3:59:17can have the same notation like this so
  6331. 3:59:20you can have the curly braces in
  6332. 3:59:22here and we can put in 1 two and three
  6333. 3:59:25now that's doing exactly the same thing
  6334. 3:59:27and you see I can just keep adding more
  6335. 3:59:29numbers in here and the list simply
  6336. 3:59:31doesn't care but as soon as I add one
  6337. 3:59:33more into here then I'm going to get a
  6338. 3:59:35syntax error because it complains that
  6339. 3:59:37the length is not equal to the numbers
  6340. 3:59:39that we
  6341. 3:59:39have because the list are really Dynamic
  6342. 3:59:42it doesn't matter how many you put in
  6343. 3:59:43here because it can just go on forever
  6344. 3:59:45of course until you run out of memory so
  6345. 3:59:47let's have a little working example just
  6346. 3:59:49to see how this
  6347. 3:59:51works so we can have a for Loop and we
  6348. 3:59:53can go from 0 to 10 or maybe 0 to three
  6349. 3:59:56just so we don't have to enter as many
  6350. 3:59:57values we can have a console right that
  6351. 3:59:59kind of just says enter a number and
  6352. 4:00:02then let's read that in and we can say
  6353. 4:00:04int n equals convert to in32 and we can
  6354. 4:00:08place a console do readline inside
  6355. 4:00:11here and then after we've got the
  6356. 4:00:13console read line we've got an inn
  6357. 4:00:15that's being converted from the console
  6358. 4:00:17input inside our integer num and from
  6359. 4:00:20there we can take our list numbers do
  6360. 4:00:22adds and we can add the num inside there
  6361. 4:00:25or if you want to reduce a variable you
  6362. 4:00:26can just take this line and place it
  6363. 4:00:28straight into here now if you get rid of
  6364. 4:00:31that when this follow Loop runs we'll
  6365. 4:00:33have three iterations let's run the code
  6366. 4:00:35and find out 1 2 three so now we've
  6367. 4:00:39entered three numbers and now they've
  6368. 4:00:40been stored in the list so let's create
  6369. 4:00:43a follow loop after and we can go from
  6370. 4:00:44zero all the way to list numbers do
  6371. 4:00:47length but of course when we're using
  6372. 4:00:49list we don't have a length anymore when
  6373. 4:00:52we're using arrays we have length
  6374. 4:00:55because arrays have a fixed length
  6375. 4:00:57whereas lists have a count of the items
  6376. 4:00:59inside it so instead of length we simply
  6377. 4:01:02use do count and then inside here we
  6378. 4:01:05could just do what we normally do and
  6379. 4:01:07say list numbers square braet I and
  6380. 4:01:10let's run that and we should see the
  6381. 4:01:12numbers printed back out in the exam
  6382. 4:01:13exact same order 1 2 and 3 and the same
  6383. 4:01:16principle goes if you user for each Loop
  6384. 4:01:19we can say V item inside list numbers
  6385. 4:01:21and that's all you need to do and you
  6386. 4:01:23can just print item to the screen now we
  6387. 4:01:25should see the same output twice let's
  6388. 4:01:27have a look at other values that we have
  6389. 4:01:29inside here so we type in list numbers
  6390. 4:01:31Dot and let's have a look at the
  6391. 4:01:33function inside so we can add which you
  6392. 4:01:35just used above we can convert it to an
  6393. 4:01:37array we can remove a specific position
  6394. 4:01:40and we can clear it completely they have
  6395. 4:01:42other ones in here like B researchers we
  6396. 4:01:45can check if it contains a certain
  6397. 4:01:46element this is going to be very handy
  6398. 4:01:48when we do our
  6399. 4:01:49remove we can convert them all to a
  6400. 4:01:51different type we have a copy to we
  6401. 4:01:54check if a value exists
  6402. 4:01:56inside we have a find finding all of
  6403. 4:01:59them the difference between these two is
  6404. 4:02:01the find will return back the first one
  6405. 4:02:04whereas find all will retrieve all the
  6406. 4:02:05elements that match it finding the index
  6407. 4:02:08of a specific match finding the last
  6408. 4:02:11index iterating through them all you can
  6409. 4:02:13insert a specific position whereas the
  6410. 4:02:16add will just insert at the end of the
  6411. 4:02:17list adds an object to the end of the
  6412. 4:02:20list inserts an element to the list at a
  6413. 4:02:23specified index and then you have remove
  6414. 4:02:26which gives you the first occurrence so
  6415. 4:02:28you can type the number straight into
  6416. 4:02:30there so you can ask the user what
  6417. 4:02:32number do you want to delete and if they
  6418. 4:02:34type zero it will delete the first zero
  6419. 4:02:36in the list and you can remove all so if
  6420. 4:02:39you want to remove every single zero
  6421. 4:02:41inside the list you can do that as well
  6422. 4:02:43or you can move at a specific position
  6423. 4:02:45this is the most used function because
  6424. 4:02:47what you can
  6425. 4:02:48do and what you can do is use the find
  6426. 4:02:51index search for an element that matches
  6427. 4:02:53a condition and then returns to zerob
  6428. 4:02:55based index of the first occurrent so if
  6429. 4:02:58you want to delete the number zero from
  6430. 4:03:00the list you could use find index and
  6431. 4:03:02that returns you back with an INT and
  6432. 4:03:04then you can use the remove at and then
  6433. 4:03:06pass that index straight into there you
  6434. 4:03:09can also sort it so it goes up in number
  6435. 4:03:11order we did this with the array. sort
  6436. 4:03:13function as well and if you wanted to
  6437. 4:03:15you can also reverse the entire list
  6438. 4:03:17this can be very helpful as we did with
  6439. 4:03:19the array functions if you just wanted
  6440. 4:03:21to reverse them all in case someone had
  6441. 4:03:23something like a filter when you're
  6442. 4:03:25doing from Price high to low and low to
  6443. 4:03:27high the difference between them two
  6444. 4:03:29lists is simply the list flipped so
  6445. 4:03:31let's run a remove just inside here and
  6446. 4:03:33let's see what happens so we can say
  6447. 4:03:35remove that and let's say position
  6448. 4:03:37zero so we type in one two and three
  6449. 4:03:41you'll see that one two three gets
  6450. 4:03:42printed out and then I'll hit this
  6451. 4:03:44remove at and it will delete the first
  6452. 4:03:47index and in this case the first index
  6453. 4:03:49is going to be our number one so now
  6454. 4:03:51this has became our first index and this
  6455. 4:03:53is our second index and it's got rid of
  6456. 4:03:55the number
  6457. 4:03:57one so let's move on to different data
  6458. 4:04:00structure called dictionaries
  6459. 4:04:02dictionaries are generic collections
  6460. 4:04:03that store key value pairs in no
  6461. 4:04:05particular order they take in a t key
  6462. 4:04:08and a t value and these just mean the
  6463. 4:04:10key and the value can be any data type
  6464. 4:04:13that you want dictionaries come under
  6465. 4:04:14the system. collections. generic
  6466. 4:04:16namespace and if you don't include this
  6467. 4:04:18line at the top then you'll have to type
  6468. 4:04:20in system. collections do generic space
  6469. 4:04:23dictionary every single time you want to
  6470. 4:04:25specify dictionary putting the name
  6471. 4:04:27space at the top will include it within
  6472. 4:04:29the project and you can use dictionaries
  6473. 4:04:30freely so one thing to know is the keys
  6474. 4:04:33must be unique and they cannot either be
  6475. 4:04:34null values can be null or duplicate cuz
  6476. 4:04:37they're the values depending on the key
  6477. 4:04:39as long as the keys aren't the same then
  6478. 4:04:41the values can be similar so let's have
  6479. 4:04:43a look and see what we can do we can
  6480. 4:04:45type in dictionary and you can see it
  6481. 4:04:47has the arrows either side and you can
  6482. 4:04:50see here it says T key and T value and
  6483. 4:04:52what T key and T value is is it gives us
  6484. 4:04:55the flexibility to add any data types
  6485. 4:04:56that we want we can add in two strings
  6486. 4:04:58in here for example or we can do
  6487. 4:05:00anything that we please so in this
  6488. 4:05:02example I just want to start a list of
  6489. 4:05:04names and I want to give them a unique
  6490. 4:05:07identifier and this unique identifier
  6491. 4:05:09will just give them a number that their
  6492. 4:05:11profile is up against it's the same way
  6493. 4:05:13when whenever you make an account with
  6494. 4:05:15any sort of website when your data gets
  6495. 4:05:17stored in the database you have a unique
  6496. 4:05:19ID for your account and this is the same
  6497. 4:05:21thing that I'm trying to replicate here
  6498. 4:05:23we have a unique ID and with that unique
  6499. 4:05:26ID comes a name so we can say equals new
  6500. 4:05:29dictionary just like we do with the list
  6501. 4:05:32we can end the brackets and end the
  6502. 4:05:34semicolon there and now we've created
  6503. 4:05:36our dictionary and the exact same
  6504. 4:05:38principle we can use with the list we
  6505. 4:05:40can simply type in the name of the
  6506. 4:05:42variable of the dictionary and type in
  6507. 4:05:45add and then we have access to int key
  6508. 4:05:47and string value and this parameter is
  6509. 4:05:50going to change if this now becomes a
  6510. 4:05:52string as I'll show you
  6511. 4:05:54now and if we reopen the bracket you'll
  6512. 4:05:57see this has turned into a string key so
  6513. 4:06:00that will change depending on the data
  6514. 4:06:01types that you specified so let's say we
  6515. 4:06:04want to add one and we want to add
  6516. 4:06:06ourselves then we can duplicate this
  6517. 4:06:08down and add in two and three and we can
  6518. 4:06:12say test and test
  6519. 4:06:14and now if you run the code you'll see
  6520. 4:06:15that we have no errors and nothing's
  6521. 4:06:17going to show up I'm just demonstrate
  6522. 4:06:18that it compiles okay and the same thing
  6523. 4:06:21with the list we can also simplifies the
  6524. 4:06:23initialization process and it looks
  6525. 4:06:26something like this so because this is
  6526. 4:06:29in key value pair we can no longer just
  6527. 4:06:31type it in one line they need to be in a
  6528. 4:06:33little block so when we wrap these
  6529. 4:06:35around the curly brace we can have the
  6530. 4:06:37key on the left and the value on the
  6531. 4:06:39right and one of these is called a key
  6532. 4:06:41value pair key P value pair and we will
  6533. 4:06:45use these in the for each Loop in a
  6534. 4:06:47second when we print out all the values
  6535. 4:06:48to the console so let me just
  6536. 4:06:50demonstrate something about what I said
  6537. 4:06:52at the start I said that all keys must
  6538. 4:06:54be unique and there's a reason why I
  6539. 4:06:56said that if we duplicate the first
  6540. 4:06:58entry therefore giv us the same key as
  6541. 4:07:01one and we try run it and let's see what
  6542. 4:07:03happens we get something called a
  6543. 4:07:05runtime error this runtime error happens
  6544. 4:07:08when we run the program and it's got
  6545. 4:07:10nothing to do with compile time or our
  6546. 4:07:12logic an item with a same key has
  6547. 4:07:14already been added so we need to watch
  6548. 4:07:16out if we do this because our program is
  6549. 4:07:18going to compile but it's not going to
  6550. 4:07:19work at runtime and let me demonstrate
  6551. 4:07:21another fact if we make this number four
  6552. 4:07:24and our values are exactly the same once
  6553. 4:07:26we run this it will still work fine
  6554. 4:07:29values can be the same but the keys
  6555. 4:07:31cannot be the same the keys have to be
  6556. 4:07:33unique so just like with our lists and
  6557. 4:07:35our arrays we can use a for each or a
  6558. 4:07:38for Loop to access this directly so we
  6559. 4:07:40can say 4 in I equal 0 and we're want to
  6560. 4:07:43go names do count and the same reason
  6561. 4:07:45it's a do count for a dictionary as it
  6562. 4:07:47is for a list but it's not for an array
  6563. 4:07:50dictionaries don't have fixed length and
  6564. 4:07:52neither do lists they are very Dynamic
  6565. 4:07:54and basically are a list list can only
  6566. 4:07:57store one different data type whereas
  6567. 4:07:59dictionaries give you a key and a value
  6568. 4:08:01so as we said above we have a key value
  6569. 4:08:03pair so let's try and print this and see
  6570. 4:08:05what happens have a console right line
  6571. 4:08:07and we can say names square bracket I
  6572. 4:08:10and that will give us back the position
  6573. 4:08:12the issue with this is it will just give
  6574. 4:08:13us back the string and let's just see
  6575. 4:08:15what happens if we print
  6576. 4:08:17this then we get an exception because no
  6577. 4:08:20key was given and the problem with this
  6578. 4:08:22situation is we can't use this like a
  6579. 4:08:24list getso sets the value associated
  6580. 4:08:27with a specified key what this means is
  6581. 4:08:29when we give our square bracket I inside
  6582. 4:08:31here this is actually not talking about
  6583. 4:08:33the index this is actually trying to do
  6584. 4:08:35a search on these keys so if you
  6585. 4:08:38actually Chang this to one then we
  6586. 4:08:40should get abber return back this is not
  6587. 4:08:43actually doing an index lookup it's
  6588. 4:08:45doing a key lookup and the reason for
  6589. 4:08:47that is what we're going to have to do
  6590. 4:08:49instead is say names. element art and
  6591. 4:08:52then what we can do inside there is
  6592. 4:08:53place our index so this element art will
  6593. 4:08:56return us back with a key value pair as
  6594. 4:08:58I said above here so how this variable
  6595. 4:09:01works is we can do key value pair and
  6596. 4:09:04then we need to specify this variable
  6597. 4:09:06the same way we've got it up here so we
  6598. 4:09:08have an INT inside a string and then we
  6599. 4:09:11can say a pair and make that equal to
  6600. 4:09:13the function called element at as that
  6601. 4:09:16returns us back with a key value pair in
  6602. 4:09:18string and of course if you wanted to
  6603. 4:09:20make this a lot smaller you could just
  6604. 4:09:22simply use V but let's keep it as key
  6605. 4:09:24value pair just so you can understand
  6606. 4:09:25what's going on and the reason why we
  6607. 4:09:27need to get the key value pair is we
  6608. 4:09:29want to retrieve the integer and we want
  6609. 4:09:31to retrieve the string and now we can
  6610. 4:09:33use pair. key and pair. value and
  6611. 4:09:37because these are both integer and this
  6612. 4:09:39one is a string it matches our pair
  6613. 4:09:41exactly so we can take these values and
  6614. 4:09:44go to our console right line and we can
  6615. 4:09:46say something like this use string
  6616. 4:09:47interpolation and say pair. key and then
  6617. 4:09:50maybe add a hyphen and then say pair.
  6618. 4:09:53value and then just remove these two
  6619. 4:09:54lines and let's try and run it now there
  6620. 4:09:58we go so we get all the values and it
  6621. 4:10:00doesn't matter what this key is it can
  6622. 4:10:01be something random it doesn't really
  6623. 4:10:03make a difference like I said this
  6624. 4:10:05doesn't have to be in a specific order
  6625. 4:10:08so let's keep it as 1 2 3 there we go so
  6626. 4:10:11you can see using a for each Loop to
  6627. 4:10:12print this is is a little bit more
  6628. 4:10:14complicated let's have a look and see
  6629. 4:10:16what it takes when you use a for each
  6630. 4:10:18Loop for each VAR item inside our
  6631. 4:10:23names and as you can see here our item
  6632. 4:10:25is automatically a key value pair of int
  6633. 4:10:28and string and again what you can do is
  6634. 4:10:31type this straight into here just so you
  6635. 4:10:33can get used to the notation and there
  6636. 4:10:35you go it's still a key value pair and
  6637. 4:10:38then now what we can do is we can take
  6638. 4:10:40this exact line and paste into here and
  6639. 4:10:42instead of pair we have item and this
  6640. 4:10:45should return us back the exact same
  6641. 4:10:47values and if we just separate with a
  6642. 4:10:48console right line there we go okay so
  6643. 4:10:51now that we've understood this let's
  6644. 4:10:53take a look at an example let's comment
  6645. 4:10:55this out and let's create another
  6646. 4:10:57dictionary so we can say dictionary and
  6647. 4:10:59then we can up a string of
  6648. 4:11:01string and we can call this teachers
  6649. 4:11:04equals new dictionary string of string
  6650. 4:11:06again and we can add our curly brace
  6651. 4:11:08just so we can add our value straight to
  6652. 4:11:10here so as you said before we need to
  6653. 4:11:11open curly brace and close Cur
  6654. 4:11:14and then we can say the math teacher is
  6655. 4:11:15called abber and then we can add another
  6656. 4:11:18one and we say the science teacher is
  6657. 4:11:21called test there we go so we've
  6658. 4:11:22initialized our teachers and we said
  6659. 4:11:24that the value that we want to give it
  6660. 4:11:26is a unique identifier which is going to
  6661. 4:11:28be our string assigned to the subject
  6662. 4:11:31and this is going to be the teacher that
  6663. 4:11:32is assigned to the subject so what we
  6664. 4:11:34can do inside here is we can have a
  6665. 4:11:36console right line and we can do
  6666. 4:11:38something like this so remember when I
  6667. 4:11:40was saying in the for Loop we can't
  6668. 4:11:41actually use the square bracket notation
  6669. 4:11:43like this because this is referring to a
  6670. 4:11:46key rather than a position so we had to
  6671. 4:11:48use element at just to find out the key
  6672. 4:11:50value pair and then to print out to the
  6673. 4:11:52console so because the square brackets
  6674. 4:11:54is an actual surge let's try and make
  6675. 4:11:56use of that now so we know that there's
  6676. 4:11:58a math class available so we can type
  6677. 4:12:00math in here and what that should return
  6678. 4:12:02us back is abber there we go so what
  6679. 4:12:04that's doing is it's searching through
  6680. 4:12:05our dictionary finding this element
  6681. 4:12:08inside here and then return us back with
  6682. 4:12:10the value for that key gets all sets the
  6683. 4:12:13value associated with the specified key
  6684. 4:12:15the problem with using this approach is
  6685. 4:12:17it says here if the specified key is not
  6686. 4:12:19found a operation throws a key not found
  6687. 4:12:22exception let's just say for instance I
  6688. 4:12:23mistyped the math and now we're just
  6689. 4:12:25going to get a runtime error and it's
  6690. 4:12:27going to be a horrible exception and our
  6691. 4:12:29code is obviously going to break this is
  6692. 4:12:31similar to what we do when we have in.
  6693. 4:12:33trass we don't just trass it immediately
  6694. 4:12:36and convert it without actually checking
  6695. 4:12:38if it's a valid value so instead of
  6696. 4:12:40doing this line what we can do is we can
  6697. 4:12:43check if that key exists first before we
  6698. 4:12:46then use it so we can say if
  6699. 4:12:49teachers do try and get the value which
  6700. 4:12:52is similar to what we have with triy
  6701. 4:12:54pass and then the first parameter is the
  6702. 4:12:57key that we want which is going to be
  6703. 4:12:58our math and then out here is going to
  6704. 4:13:01be the variable that we want to Output
  6705. 4:13:03out so we can say out
  6706. 4:13:06teacher and then open the curly braces
  6707. 4:13:09and then inside here we can print out
  6708. 4:13:11the teacher so we try to get the value
  6709. 4:13:13for Math and if we found that key inside
  6710. 4:13:15these then we return back the value
  6711. 4:13:17inside our teacher the same way works
  6712. 4:13:20for our trip pass you take in a string
  6713. 4:13:22in here and the output is going to be an
  6714. 4:13:24integer if it's successfully passed it
  6715. 4:13:27and if it hasn't we can say cons output
  6716. 4:13:30math teacher not found so let's try and
  6717. 4:13:34run that now you see we get return back
  6718. 4:13:36Abba and if you actually drop the lower
  6719. 4:13:38case then it says the math teacher is
  6720. 4:13:40not
  6721. 4:13:41found alternatively what we can do
  6722. 4:13:43inside here let's assume that this is
  6723. 4:13:45correct we can take this value and place
  6724. 4:13:48it straight inside the square brackets
  6725. 4:13:50and do this again this is kind of
  6726. 4:13:52redundant because we've told it to get
  6727. 4:13:54the value for key place inside teacher
  6728. 4:13:56and then we're just going back to the
  6729. 4:13:57dictionary to get the value twice so
  6730. 4:14:00make sure if you have an output variable
  6731. 4:14:02that you actually use it inside here you
  6732. 4:14:03can see that you're not using it when it
  6733. 4:14:05goes a slightly dark color like this so
  6734. 4:14:07just like in the last examples when we
  6735. 4:14:09were placing values inside our list we
  6736. 4:14:11can also do that in dictionaries
  6737. 4:14:13we can say teachers square bracket math
  6738. 4:14:16and then we can make that equal to
  6739. 4:14:18something so if the math teacher were to
  6740. 4:14:20change and now the teachers called Joe
  6741. 4:14:22then we can run this line and print out
  6742. 4:14:23to the screen let's just take that for
  6743. 4:14:25each Loop and tailor it to the new
  6744. 4:14:27dictionary and see what happens when we
  6745. 4:14:29run it so let's just change this into a
  6746. 4:14:31VAR to make it simple and then we can
  6747. 4:14:33have teachers inside here there we go so
  6748. 4:14:35now let's just run this and you see now
  6749. 4:14:37it says abber is the first value that
  6750. 4:14:39abber is coming from console right line
  6751. 4:14:42here that says teacher this is one line
  6752. 4:14:44before we change it and we change the
  6753. 4:14:46value of the math teacher to Joe on the
  6754. 4:14:49next line and there we go it's committed
  6755. 4:14:51the change and just like in the last
  6756. 4:14:53video as well when we try to delete
  6757. 4:14:54something from the list let's see what
  6758. 4:14:56we can do to delete it from here so we
  6759. 4:14:58can have teachers do remove and you can
  6760. 4:15:00see that it says key in there and again
  6761. 4:15:03if we add the key in like this then it's
  6762. 4:15:04going to work just fine there you go we
  6763. 4:15:07just have science we don't have math
  6764. 4:15:08anymore but if I drop the lowercase then
  6765. 4:15:11it's going to cause us back with an
  6766. 4:15:12error but in this case instead of it
  6767. 4:15:14throwing an exception like it did when
  6768. 4:15:16we were trying to do up here and up here
  6769. 4:15:18as well the problem with using the
  6770. 4:15:21remove is you just simply won't remove
  6771. 4:15:23it but what it will do is it'll give you
  6772. 4:15:25back a booing if you remember from the
  6773. 4:15:27last video we actually had a look at a
  6774. 4:15:29section where in the list it gave you
  6775. 4:15:31back a booing whether the remove
  6776. 4:15:32function worked or not alternatively
  6777. 4:15:34what you could do instead of using the
  6778. 4:15:36remove function is to check if the item
  6779. 4:15:39exists in the first place we can say
  6780. 4:15:41contains key and we can Place math
  6781. 4:15:43inside here and if it does contain it
  6782. 4:15:45then we can run the same line of code
  6783. 4:15:47and we can say remove and then use math
  6784. 4:15:50just to make sure that this has been
  6785. 4:15:52actually removed and then in the else we
  6786. 4:15:54can say math not found retrieving values
  6787. 4:15:58back should be retrieve using try get
  6788. 4:16:00just so you don't cause an exception if
  6789. 4:16:02this is not found we can simply do an
  6790. 4:16:04update by placing the value of the key
  6791. 4:16:06inside the square brackets and then
  6792. 4:16:08simply just assigning it to something
  6793. 4:16:10else this assignment will change the
  6794. 4:16:12value of the key which is inside the
  6795. 4:16:14teacher dictionary and if you wanted to
  6796. 4:16:17remove something it's best to use the
  6797. 4:16:18contains key just to check if it
  6798. 4:16:20contains it first and then you can do
  6799. 4:16:22the remove and then if it doesn't
  6800. 4:16:23contain it you can tell the user that
  6801. 4:16:25math has not been found and then finally
  6802. 4:16:28we can just print out like
  6803. 4:16:30this so in this exercise I want you to
  6804. 4:16:33create two lists with the integer data
  6805. 4:16:35type one for even and one for odd and
  6806. 4:16:37then we're going to Loop through from
  6807. 4:16:38zero all the way till 20 and if the
  6808. 4:16:41number is even add it to the even and if
  6809. 4:16:43the number is odd add it to the odd list
  6810. 4:16:45and at the end print out all the values
  6811. 4:16:47for even and for odd I know you can just
  6812. 4:16:49print these out inside the for Loop we
  6813. 4:16:51don't have to have dedicated lists but
  6814. 4:16:53this is just for list practice and I
  6815. 4:16:55want to talk about different ways that
  6816. 4:16:56we can do this solution so let's start
  6817. 4:16:58we can have a list of int and we can say
  6818. 4:17:01odd new list int and we don't know the
  6819. 4:17:05size so we're just going to leave the
  6820. 4:17:06gaps empty and if we duplicate this down
  6821. 4:17:09we can simply just change this to even
  6822. 4:17:11now the reason why we're doing lists
  6823. 4:17:13we're going from 0 to 20 of course this
  6824. 4:17:1520 value could be anything it could be a
  6825. 4:17:17million it could be 100 but we don't
  6826. 4:17:19know and the point is lists have counts
  6827. 4:17:23whereas arrays have lengths if we set
  6828. 4:17:25the array length to something like a
  6829. 4:17:26thousand we know probably be able to
  6830. 4:17:28contain all the numbers inside there but
  6831. 4:17:30that's going to be a lot of positions
  6832. 4:17:31that we don't exactly need because
  6833. 4:17:33arrays are not really dynamic because
  6834. 4:17:35they're static then it's best to use
  6835. 4:17:37list in this case because we don't know
  6836. 4:17:39how many numbers are going to be odd or
  6837. 4:17:41are going to be even between between
  6838. 4:17:43this range if we knew the exact number
  6839. 4:17:45of positions that we needed in a given
  6840. 4:17:47solution then array would be better
  6841. 4:17:49since you don't want it to be dynamic if
  6842. 4:17:51it doesn't need to be so let's start can
  6843. 4:17:54have a for Loop that goes from 0 to 20
  6844. 4:17:56if you want to include the 20 as well we
  6845. 4:17:58can just place an equals the best way to
  6846. 4:18:00determine if a number is odd or even is
  6847. 4:18:02to use the modulus operator so we can
  6848. 4:18:04say I percentage 2 equals equal Z and
  6849. 4:18:07what this will do is it'll tell us if I
  6850. 4:18:09is divisible by two if the value is
  6851. 4:18:11equal to zero if you have two modulus 2
  6852. 4:18:15then the remainder is going to be zero
  6853. 4:18:17if you have three modulus 2 it's going
  6854. 4:18:19to be 1 * by two which gets you to two
  6855. 4:18:22and then to go from two to three you
  6856. 4:18:24actually have one so if you insert an
  6857. 4:18:27odd number and do a modulus two you're
  6858. 4:18:29going to get a one returned if you do an
  6859. 4:18:32odd number and you do modulus 2 then
  6860. 4:18:34you're going to get one returned if you
  6861. 4:18:35do an even number modulus 2 then you're
  6862. 4:18:38going to get zero returned so what we
  6863. 4:18:40can do in here is we can say even add
  6864. 4:18:43and then we can simply add I and then in
  6865. 4:18:45the else we can also simply just do odd
  6866. 4:18:48do add and then have I because the
  6867. 4:18:50values of the remainder can only be zero
  6868. 4:18:52or one we don't need to have an lse if
  6869. 4:18:54in here doing this is a little bit
  6870. 4:18:56redundant although it's still
  6871. 4:18:58technically correct the values can only
  6872. 4:19:00ever be zero or one so if you run this
  6873. 4:19:03calculation and the value is zero if you
  6874. 4:19:05run this calculation and the value is
  6875. 4:19:06zero the only other value can possibly
  6876. 4:19:09be is one so therefore we don't need to
  6877. 4:19:11additional condition so therefore we
  6878. 4:19:13don't need this additional condition and
  6879. 4:19:14we can simply get rid of it if I modulus
  6880. 4:19:172 is equal to Z then we add the number
  6881. 4:19:19to even and else we add it to odd
  6882. 4:19:22normally when you have situations like
  6883. 4:19:24this we can use the conditional operator
  6884. 4:19:26where we have the question mark followed
  6885. 4:19:28by the colon but in this case we're
  6886. 4:19:30actually doing two different assignments
  6887. 4:19:31we're either assigning into the even or
  6888. 4:19:34assigning into the odd so there's no
  6889. 4:19:36room for the conditional operator so at
  6890. 4:19:38this point we should have all the even
  6891. 4:19:39and odd values from zero all the way to
  6892. 4:19:4120 inside our two lists so then we can
  6893. 4:19:44now just print both the lists out so we
  6894. 4:19:47can say printing even numbers and then
  6895. 4:19:50we can have a for each Loop that goes v
  6896. 4:19:52item inside out even and then we can do
  6897. 4:19:55a console Right add some instring
  6898. 4:19:57interpolation and then we can say item
  6899. 4:19:59followed by space then we can print
  6900. 4:20:02environment. new line just to give it
  6901. 4:20:03some space and we can say printing odd
  6902. 4:20:06numbers here we go and then we can just
  6903. 4:20:08take this exact for Loop and copy it
  6904. 4:20:10down instead of set in even we want to
  6905. 4:20:13do in odd and this is exactly the same
  6906. 4:20:15since the variable name is exactly the
  6907. 4:20:17same let's try run the
  6908. 4:20:19code there we go it's got zero all the
  6909. 4:20:22way till 20 and you can see it goes up
  6910. 4:20:24in twos hence all even numbers and we
  6911. 4:20:26also get these going up in twos except
  6912. 4:20:28it starts from one and this covers all
  6913. 4:20:30of the odd numbers and if we change this
  6914. 4:20:32number up to something like 50 then the
  6915. 4:20:34list will just grow and they Cod should
  6916. 4:20:36still work okay you can see that all of
  6917. 4:20:38these numbers are still consistently
  6918. 4:20:39even so now that our task is actually
  6919. 4:20:41complete let's just have a little recap
  6920. 4:20:44so the reason why we're using lists and
  6921. 4:20:45we're not using arrays is for this exact
  6922. 4:20:47nature if we have 0 to 10 then we can
  6923. 4:20:51almost work out how many even numbers
  6924. 4:20:53and how many odd numbers we can have we
  6925. 4:20:55can manually count we have zero and then
  6926. 4:20:58two 4 6 8 and 10 and then for odd we
  6927. 4:21:01have 1 3 5 7 and 9 so we know how many
  6928. 4:21:05numbers exist within here but what if we
  6929. 4:21:08just change that number to 50 then now
  6930. 4:21:10we have a lot more odd and a lot more
  6931. 4:21:12even it's impractical to type these
  6932. 4:21:14numbers in and then to Simply count them
  6933. 4:21:16in your head and then make an array
  6934. 4:21:18length based on that position that's a
  6935. 4:21:19bad solution because not all cases
  6936. 4:21:22you're going to have the flexibility of
  6937. 4:21:24Simply typing in the numbers like this
  6938. 4:21:25and Counting them yourself these numbers
  6939. 4:21:27could be something that typed in by a
  6940. 4:21:29user or something loaded for something
  6941. 4:21:31external like a text file you need to
  6942. 4:21:33understand that these values can be
  6943. 4:21:35generated from anywhere and if you just
  6944. 4:21:37type them in then it provides an
  6945. 4:21:38unrealistic expectation of how to write
  6946. 4:21:40some programs so what you have to bear
  6947. 4:21:42in mind in situations like this is you
  6948. 4:21:45simply don't know how many even and how
  6949. 4:21:47many odd numbers will be appearing
  6950. 4:21:49during this range so you want to use a
  6951. 4:21:51list so it's as dynamic as possible we
  6952. 4:21:53have two lists in this case one to start
  6953. 4:21:56all the odd numbers and one to start all
  6954. 4:21:57the even numbers we don't initialize
  6955. 4:21:59them with any sort of capacity we just
  6956. 4:22:01keep it empty for the exact reason we
  6957. 4:22:03don't use an array we Loop through from
  6958. 4:22:05zero to anything we don't even have to
  6959. 4:22:07loop from zero we can also just start
  6960. 4:22:09this at 100 and go all the way till
  6961. 4:22:11th000 it really doesn't matter what our
  6962. 4:22:14number range is the only thing that
  6963. 4:22:15matters is that we having a list that is
  6964. 4:22:18going to be dynamic and then we can
  6965. 4:22:19calculate whether the number is odd or
  6966. 4:22:22even and add it to the right list how
  6967. 4:22:24this works is we use I which is the
  6968. 4:22:26number we're currently up to and we
  6969. 4:22:28calculate the modulus of it to check the
  6970. 4:22:30remainder if the remainder is zero then
  6971. 4:22:32it must be even as it's divisible by two
  6972. 4:22:35and if it's anything else than zero
  6973. 4:22:36which you can only ever be one in this
  6974. 4:22:38case when using modulus 2 then we add it
  6975. 4:22:40to the odd list after this volume is run
  6976. 4:22:43now our odd and even lists have all the
  6977. 4:22:45values that they should have and we
  6978. 4:22:47simply just write these two blocks are
  6979. 4:22:49exactly the same just print the even
  6980. 4:22:51numbers and print them out print the odd
  6981. 4:22:53numbers and simply print them out to the
  6982. 4:22:55screen and that's it and if you run the
  6983. 4:22:57code it might get a little bit crazy in
  6984. 4:22:58here but as you can see these will
  6985. 4:23:00actually work if you pick any random
  6986. 4:23:02numbers you can see that they're all
  6987. 4:23:04divisible by two and the same thing goes
  6988. 4:23:06for odd you can see that they're not
  6989. 4:23:08divisible at two cuz they're all odd
  6990. 4:23:12so in this exercise we're going to cover
  6991. 4:23:14a little task I like this call this task
  6992. 4:23:16an array of multiples so we want to
  6993. 4:23:18Define initialize two integers and num
  6994. 4:23:20into length so in the case of this
  6995. 4:23:22example we have a number seven and the
  6996. 4:23:24length of five so what this will do is
  6997. 4:23:26it will essentially give you seven times
  6998. 4:23:28table but only the first five numbers 74
  6999. 4:23:3221 28 and
  7000. 4:23:3435 we take the first number which is num
  7001. 4:23:37which will give us the multiples and we
  7002. 4:23:39take the second number which is the
  7003. 4:23:40length and that gives us the size of the
  7004. 4:23:42array so we create the integer array
  7005. 4:23:44with the size of length Loop through the
  7006. 4:23:46array and insert the loop count times
  7007. 4:23:48byome CU this is 1 * 7 2 * 7 and so on
  7008. 4:23:52so forth until we reach the length and
  7009. 4:23:54we place into the array and then finally
  7010. 4:23:56we paint the array just to give us these
  7011. 4:23:58values out to the console all right so
  7012. 4:23:59let's get started we can have an in norm
  7013. 4:24:02and make it equal to 7 and we have it in
  7014. 4:24:04length and make that one equal to five
  7015. 4:24:07now that we defined initialize the two
  7016. 4:24:08integers let's create the int array with
  7017. 4:24:11the size length in Array result equals
  7018. 4:24:14new int and we want size length after
  7019. 4:24:16that we want to create our for Loop now
  7020. 4:24:18this is the tricky part if we try go
  7021. 4:24:20from zero all the way to result. length
  7022. 4:24:23then let's see what happens so we want
  7023. 4:24:25to start result I you want to make that
  7024. 4:24:27equal to number which is our array of
  7025. 4:24:29multiples Times by the value that we're
  7026. 4:24:32currently up to now let's print our
  7027. 4:24:33array using for each Loop to see the
  7028. 4:24:35value so we can say V item inside our
  7029. 4:24:38result then we can have a console right
  7030. 4:24:40that just prints one of them at a time
  7031. 4:24:42so we can say item followed by spits now
  7032. 4:24:45let's see what happens when we run
  7033. 4:24:47it so we get 0 14 21 28 as you can see
  7034. 4:24:51these are the first four numbers so
  7035. 4:24:53we're close but we're nearly there
  7036. 4:24:56because our array starts at zero and it
  7037. 4:24:58goes to the length which in this case is
  7038. 4:25:00going to be 0 to less than five which
  7039. 4:25:03will cover the positions of 0 to
  7040. 4:25:064 with these positions it's going to do
  7041. 4:25:090 * 7 1 * 7 and then get to 4 * 7 and
  7042. 4:25:13not actually get to the fifth because
  7043. 4:25:14we're starting from zero so
  7044. 4:25:17alternatively what we can do or what you
  7045. 4:25:19think you can do is changes to a one and
  7046. 4:25:22changes to equal to so now what it's
  7047. 4:25:24doing is it's going from one all the way
  7048. 4:25:26to five which is still covering the same
  7049. 4:25:28amount of positions it's just one index
  7050. 4:25:30forward so now if we try to run the code
  7051. 4:25:33then we going a runtime exception and
  7052. 4:25:35the reason for that is it's trying to
  7053. 4:25:37access position number five if we hover
  7054. 4:25:39over I we have five and if you hover
  7055. 4:25:42over the result we actually have some of
  7056. 4:25:45the right values except it's trying to
  7057. 4:25:47get to the fifth position and it's not
  7058. 4:25:49working the reason why the zeroth index
  7059. 4:25:51is actually zero is simply because
  7060. 4:25:53that's the same value as the default
  7061. 4:25:55value because our for Loop started at in
  7062. 4:25:57I it's actually only starting from here
  7063. 4:25:59and going down so this calculation is
  7064. 4:26:01actually correct but it needs to be back
  7065. 4:26:04one we need to start the multiplication
  7066. 4:26:06on IAL 0 but our for Loop needs to start
  7067. 4:26:09on IAL 1 so we can get all the way
  7068. 4:26:12through
  7069. 4:26:13so we have two ways of doing this we
  7070. 4:26:15could say I minus 1 so even though it
  7071. 4:26:18goes from 1 to 5 it's still assigning
  7072. 4:26:20into the array using 0 to four so if we
  7073. 4:26:23run that we can see that that will give
  7074. 4:26:25us the desired result alternatively what
  7075. 4:26:27we can actually do instead of doing IUS
  7076. 4:26:30one inside here we can define a variable
  7077. 4:26:32called counter and make it equal to zero
  7078. 4:26:35we can change this to counter and we
  7079. 4:26:37don't need to change the multiplication
  7080. 4:26:39because that's already going from one to
  7081. 4:26:40the length which is 1 to 5 in this case
  7082. 4:26:43after the for Loop is run we need to
  7083. 4:26:45remember to increment counter so there
  7084. 4:26:47we go we have the desired effect another
  7085. 4:26:49hack if you're interested if you have
  7086. 4:26:52two variables that you want to increment
  7087. 4:26:53inside the for Loop you can see that we
  7088. 4:26:55already have I ++ in here which will
  7089. 4:26:57increment it by one and we want to
  7090. 4:26:59increment counter by one as well every
  7091. 4:27:01single time it loops around what you can
  7092. 4:27:04actually do is remove this and you know
  7093. 4:27:06how these are broken up by semicolons
  7094. 4:27:09that indicates that the section has
  7095. 4:27:10ended the first sections for declaration
  7096. 4:27:13the second is for the range and the
  7097. 4:27:14third is so you can increment the
  7098. 4:27:16variable or decrement it when you trying
  7099. 4:27:17to increment two variables inside a for
  7100. 4:27:19Loop instead of placing a semicolon here
  7101. 4:27:22and type in the next one you can
  7102. 4:27:24actually place a comma and do counter
  7103. 4:27:26Plus+ what that will tell the fall Loop
  7104. 4:27:28is every single time you go through you
  7105. 4:27:30want to do a series of these actions and
  7106. 4:27:33because we have i++ and counter Plus+
  7107. 4:27:36then that's going to work exactly the
  7108. 4:27:37same way it just prevents us having
  7109. 4:27:40counter Plus+ down here and we can move
  7110. 4:27:42move it inside the for Loop just so it
  7111. 4:27:43doesn't for us and it looks a lot
  7112. 4:27:45cleaner cuz you can clearly see that
  7113. 4:27:47every single time we loop around this
  7114. 4:27:49for loop we're going to increment these
  7115. 4:27:50two variables there we go so let's try
  7116. 4:27:54to put some different values in here
  7117. 4:27:55maybe make this 10 and we can have 10 on
  7118. 4:27:57both to give us the first 10 numbers
  7119. 4:28:00there we go from 10 all the way to 100
  7120. 4:28:02let's have a little recap so we're
  7121. 4:28:04essentially just making an array that
  7122. 4:28:06will give us back all the multiples so
  7123. 4:28:08it's like a mini times table but the
  7124. 4:28:10fact that we're doing it dynamically
  7125. 4:28:11using our array is quite important what
  7126. 4:28:14you can do if you wanted to is to extend
  7127. 4:28:16this program you can ask the user to
  7128. 4:28:18type these into the console and then
  7129. 4:28:20convert them and use them right now
  7130. 4:28:22we're just statically typing them in and
  7131. 4:28:23that's not really an issue since the
  7132. 4:28:25main focus was doing this for Loop if
  7133. 4:28:27you wanted to you could ask the user and
  7134. 4:28:29you can type these values in so you
  7135. 4:28:31don't have to change them every single
  7136. 4:28:32time you compile so now we've got the
  7137. 4:28:34number and the length let's make our
  7138. 4:28:35interray call result and initialize it
  7139. 4:28:37to the length we set up this counter so
  7140. 4:28:39we simply didn't have to do I minus one
  7141. 4:28:42here so it looks a little bit more
  7142. 4:28:43intuitive we start the follow from one
  7143. 4:28:46and go to the length so we can access 1
  7144. 4:28:48to five and the reason why we want to do
  7145. 4:28:50one to five so when we do our
  7146. 4:28:52multiplication it doesn't start at zero
  7147. 4:28:55these two are going through the exact
  7148. 4:28:56same amount of indexes 1 to 5 is five
  7149. 4:28:59numbers and 0 to four is also five
  7150. 4:29:01numbers so when we do a multiplication
  7151. 4:29:03we want to start from one since the Tim
  7152. 4:29:05table normally start at one and we go to
  7153. 4:29:07the length the reason why we're using
  7154. 4:29:09the counter is so we don't have to do
  7155. 4:29:10IUS one in here and it breaks up the
  7156. 4:29:12logic a little bit and it looks nicer
  7157. 4:29:14without our counter we start our counter
  7158. 4:29:16at zero and instead of incrementing down
  7159. 4:29:18here we use this fancy comma and we can
  7160. 4:29:21say we want to increment I and counter
  7161. 4:29:23every time the for Loop goes around so
  7162. 4:29:25we don't need to include the counter
  7163. 4:29:26increment inside the for Loop and this
  7164. 4:29:28one line is pretty simple we say result
  7165. 4:29:31the index of counter which will start at
  7166. 4:29:33zero which is the start of the array
  7167. 4:29:35since it's zero base and we say that's
  7168. 4:29:37equal to the number and the number is
  7169. 4:29:38going to be a fixed number since this is
  7170. 4:29:40the multiple and we say number times by
  7171. 4:29:43I and in this case it goes from one all
  7172. 4:29:45the way to the length so we get
  7173. 4:29:47essentially the times table and at the
  7174. 4:29:49end we just use a simple for each Loop
  7175. 4:29:51that will go through all the items
  7176. 4:29:52inside the result array and just print
  7177. 4:29:54them out to the screen with a comma
  7178. 4:29:56after each other using console. right
  7179. 4:29:58just so they're all on one line this is
  7180. 4:30:00the start of the function section
  7181. 4:30:02functions can also be called method they
  7182. 4:30:04are used interchangeably in the
  7183. 4:30:06introduction we discussed dry which
  7184. 4:30:08means don't repeat yourself and the
  7185. 4:30:10whole point of dry is so you don't have
  7186. 4:30:12multiple sections of code that do
  7187. 4:30:14basically the same thing you want to
  7188. 4:30:16wrap it around in a function functions
  7189. 4:30:18are used to improve the readability of
  7190. 4:30:20your code and the reusability of the
  7191. 4:30:22code as well having multiple section
  7192. 4:30:25code that are basically exactly the same
  7193. 4:30:27can be more prone to errors if you wrap
  7194. 4:30:29them around and place in One Singular
  7195. 4:30:31function you can make sure that the
  7196. 4:30:33error prone and then wherever you use it
  7197. 4:30:35it will be error prone as well if you
  7198. 4:30:37have to copy and paste and tweak some
  7199. 4:30:39code then you're actually exaggerating
  7200. 4:30:41the amount of error that could occur and
  7201. 4:30:43plus it makes your code look a lot
  7202. 4:30:45cleaner and the structure a lot better
  7203. 4:30:47since you can name the functions so
  7204. 4:30:48let's take a little example we have a
  7205. 4:30:51static void main straight into here this
  7206. 4:30:54is a function or a method and these are
  7207. 4:30:56the parameters for the function The NET
  7208. 4:30:59Framework expects every C program to
  7209. 4:31:02have a main function with a capital m
  7210. 4:31:04and it needs to be a static and avoid
  7211. 4:31:07the parameters don't exactly matter we
  7212. 4:31:08can also remove them and it won't be a
  7213. 4:31:10problem the only thing that matters is
  7214. 4:31:12this needs to be called main if we drop
  7215. 4:31:15the lower case on it and try and run the
  7216. 4:31:17code then we're going to get a build
  7217. 4:31:19error program does not contain a static
  7218. 4:31:22main method suitable for an entry point
  7219. 4:31:25so if you change this back to M and run
  7220. 4:31:26it again and then it works fine so
  7221. 4:31:29that's one thing to note about The NET
  7222. 4:31:31Framework is you need to include the
  7223. 4:31:33main function otherwise the program
  7224. 4:31:35won't run and it won't even compile so
  7225. 4:31:37let's take a look at how we can use this
  7226. 4:31:40we can have something like a console
  7227. 4:31:41right line and we can say welcome to my
  7228. 4:31:44program this is a line that we're
  7229. 4:31:46probably only going to use once but
  7230. 4:31:48let's say this is welcome in someone's
  7231. 4:31:49name welcome abber let's say you had
  7232. 4:31:52this in your software in many different
  7233. 4:31:53places you don't want to be recalling
  7234. 4:31:55this or retyping out this message in
  7235. 4:31:58case you type it wrong the fifth time
  7236. 4:31:59and you get a error of course this won't
  7237. 4:32:01be a syntax or a runtime error it'll
  7238. 4:32:04just simply be a logic error because
  7239. 4:32:06it's a typo so in order to make sure
  7240. 4:32:07this typo never occurs we can actually
  7241. 4:32:09make another function so outside of
  7242. 4:32:12these cury braces and just still within
  7243. 4:32:14the class we want to create a new
  7244. 4:32:15function so we want to follow this exact
  7245. 4:32:18same approach we can say static then
  7246. 4:32:21void and we can call it welcome message
  7247. 4:32:24and we don't need to give it any
  7248. 4:32:25parameters cuz we're not really going to
  7249. 4:32:26give it any values just for now now that
  7250. 4:32:28we've made that we have no syntax errors
  7251. 4:32:30and we can run it like this except of
  7252. 4:32:32course there's no body so let's place
  7253. 4:32:34our console right line within here now
  7254. 4:32:36if you run the code nothing is going to
  7255. 4:32:38happen and the reason for that is the
  7256. 4:32:40code will still execute in the main we
  7257. 4:32:43haven't called this function inside the
  7258. 4:32:45main we've defined it and called it
  7259. 4:32:47welcome message and we've gave it the
  7260. 4:32:49contents but we haven't actually made
  7261. 4:32:50use of it and you can see that when it's
  7262. 4:32:53highlighted in a darker color that means
  7263. 4:32:55it's not being used as it says here
  7264. 4:32:57private member welcome message is unused
  7265. 4:33:00so now we can simply type in here
  7266. 4:33:02welcome message followed by an Open
  7267. 4:33:04Bracket and then a Clos bracket to
  7268. 4:33:06denote the parameters that we're going
  7269. 4:33:07to give it and of course in this case
  7270. 4:33:09there is no parameters and then we close
  7271. 4:33:11the brackets and add a semicolon now if
  7272. 4:33:13you run the code you can see it says
  7273. 4:33:15welcome AB so just to go over some of
  7274. 4:33:17these words that we're using static
  7275. 4:33:19means that we can access it anywhere and
  7276. 4:33:22everywhere what I mean by that is we
  7277. 4:33:24don't have the keyword new just before
  7278. 4:33:27this we don't need to make a new
  7279. 4:33:28instance of the program class in order
  7280. 4:33:31to access this so what that means is
  7281. 4:33:33when we make future programs we're going
  7282. 4:33:35to have some functions inside our class
  7283. 4:33:37inside the class you can actually denote
  7284. 4:33:40if a function is going to be static or
  7285. 4:33:42it's not going to be static you don't
  7286. 4:33:44have to specify is it static but this
  7287. 4:33:46won't let you call it from here because
  7288. 4:33:48it's unaware where this function's
  7289. 4:33:49coming from an object reference is
  7290. 4:33:51required for the non-static field method
  7291. 4:33:53or property because this is static you
  7292. 4:33:56need to reference something else from a
  7293. 4:33:58static nature all static means is you
  7294. 4:34:00don't have to create a new instance of
  7295. 4:34:02this thing in order to use it we'll see
  7296. 4:34:04different examples in the future where
  7297. 4:34:06you have to create an instance in order
  7298. 4:34:08to use something so let me just show you
  7299. 4:34:10quickly if you have a test and I say
  7300. 4:34:13static void
  7301. 4:34:15something and we can just print out
  7302. 4:34:17nothing to the screen and let's try and
  7303. 4:34:19use this so because this class is
  7304. 4:34:21outside of the program we have to use
  7305. 4:34:24the notation test Dot and then it should
  7306. 4:34:28be available within here and the reason
  7307. 4:34:30why it's not is because we need to
  7308. 4:34:31insert this public word just before it
  7309. 4:34:33and public means everything has access
  7310. 4:34:35to it and static means you don't need to
  7311. 4:34:37create an instance now if I type in the
  7312. 4:34:39dot again you can see we can now see
  7313. 4:34:41something
  7314. 4:34:43and the reason why we can see something
  7315. 4:34:45is because this is a static now if I
  7316. 4:34:47remove the static word then you see that
  7317. 4:34:49we'll get a compile error because it
  7318. 4:34:50doesn't know where this is now if you
  7319. 4:34:53don't use static you need to do this you
  7320. 4:34:55need to say test test equals new test so
  7321. 4:34:58you make an instance of the class and
  7322. 4:35:01then inside test then we get the
  7323. 4:35:03something so that's the difference
  7324. 4:35:05between static and not static
  7325. 4:35:08essentially it's accessible from
  7326. 4:35:09everywhere but if you don't want to be
  7327. 4:35:11accessible from everywhere then you can
  7328. 4:35:13just simply remove the static word and
  7329. 4:35:15the user will have to create a new
  7330. 4:35:17instance before they can use it so this
  7331. 4:35:19is what it's like without
  7332. 4:35:21static and this is what it's like with
  7333. 4:35:23static don't worry if you don't
  7334. 4:35:25understand this too much this was just a
  7335. 4:35:27quick overiew but I have a dedicated
  7336. 4:35:29video later on in the course so don't
  7337. 4:35:31worry too much about it so now that
  7338. 4:35:32we've made a function and we've called
  7339. 4:35:34it successfully in the domain and it's
  7340. 4:35:36prined out to the
  7341. 4:35:39console so when we're declaring function
  7342. 4:35:42we want to specify what return type
  7343. 4:35:43we're going to have we have a static
  7344. 4:35:45void Main in here if you hover over main
  7345. 4:35:48it says void which means at the end of
  7346. 4:35:50this function it doesn't return anything
  7347. 4:35:51hence its void if you have a look at
  7348. 4:35:54something like an in. trass when we open
  7349. 4:35:56the brackets you can see that it takes
  7350. 4:35:58in a string and it takes in an integer
  7351. 4:36:00out parameter it also returns us back to
  7352. 4:36:03the Boolean so we can have something
  7353. 4:36:04like 1 2 3 out int result and then it
  7354. 4:36:08doesn't actually complain about anything
  7355. 4:36:10but what we can do is also so just store
  7356. 4:36:12this in to a bu success and then print
  7357. 4:36:14it out to the
  7358. 4:36:17screen there we go so it says it was
  7359. 4:36:19successfully true if we remove this or
  7360. 4:36:22whether we keep it in or not trip pass
  7361. 4:36:24function doesn't actually complain but
  7362. 4:36:26the main point here is it defines back a
  7363. 4:36:28bull that means at the end of this
  7364. 4:36:30function it will always return you back
  7365. 4:36:32with a bull and it's up to you if you
  7366. 4:36:34want to make use of it and you can
  7367. 4:36:35alternatively you can place this
  7368. 4:36:37straight inside the console right line
  7369. 4:36:39and get the value printed straight to
  7370. 4:36:41the screen or you could wrap this around
  7371. 4:36:43inside an if statement like we've done
  7372. 4:36:44in the past Etc and make use of it that
  7373. 4:36:47way and have the result being printed
  7374. 4:36:48out inside here the point is you have
  7375. 4:36:50functions that return something and
  7376. 4:36:52functions that don't if you remember
  7377. 4:36:54that we had the array. copy for example
  7378. 4:36:57you'll see that they have void here
  7379. 4:36:59because they just apply a function to
  7380. 4:37:01something but they don't actually give
  7381. 4:37:02you something back this is the
  7382. 4:37:04difference of having something like this
  7383. 4:37:06a string test that says hello and then
  7384. 4:37:09you can do test. replace and then you
  7385. 4:37:12can return you back with the string and
  7386. 4:37:14it says here it returns a new string
  7387. 4:37:16whereas when we had a string builder for
  7388. 4:37:19example and we did string builder.
  7389. 4:37:22replace you can see that although it
  7390. 4:37:24does say it replaces a new string
  7391. 4:37:26Builder you can type anything inside
  7392. 4:37:28here and it will still affect the
  7393. 4:37:30current string Builder it won't have to
  7394. 4:37:31do SB equals s SP replace as you would
  7395. 4:37:34need to do here because this returns you
  7396. 4:37:36back with a new string and if you wanted
  7397. 4:37:38to replace test you would literally have
  7398. 4:37:40to say test equals test. replace with
  7399. 4:37:44something like a string Builder all the
  7400. 4:37:46array functions you don't actually have
  7401. 4:37:48to do any of that so you can say array.
  7402. 4:37:50empty and it will just return the empty
  7403. 4:37:52array and the ones that we used before
  7404. 4:37:54things like equals and that will give
  7405. 4:37:56you back a Bool because that's
  7406. 4:37:58determined whether it's equal or not but
  7407. 4:38:00if you're doing functions like sort for
  7408. 4:38:02example and doing things like reverse it
  7409. 4:38:04just simply applies the data but it
  7410. 4:38:07doesn't return you back with anything so
  7411. 4:38:08you can literally just call it inside
  7412. 4:38:10here put your array in the brackets and
  7413. 4:38:12it'll use it happily now depending on
  7414. 4:38:15your user case you need to define
  7415. 4:38:17whether or not you need to use if return
  7416. 4:38:19type or not for now let's just cover
  7417. 4:38:22void which means it won't do anything so
  7418. 4:38:24let's just Define a function static void
  7419. 4:38:27create and print an array open the
  7420. 4:38:29brackets close the brackets and we can
  7421. 4:38:31have it int numbers make it equal to new
  7422. 4:38:34int with length three and let's just
  7423. 4:38:36have 0 1 and two inside here as the
  7424. 4:38:39initializer let's just do a for each VAR
  7425. 4:38:42item inside numbers and then just do a
  7426. 4:38:45console right string interpolation and
  7427. 4:38:47just have item followed by space and now
  7428. 4:38:50that it's dark gray because we haven't
  7429. 4:38:51called the function so let's make use of
  7430. 4:38:53it at the top of our code and let's run
  7431. 4:38:55it now you can see we get 012 printed to
  7432. 4:38:58the screen and if we make this into a
  7433. 4:39:00five and simply just add more values in
  7434. 4:39:03here and then we run the code you'll see
  7435. 4:39:05that now it all ofd but because this
  7436. 4:39:07doesn't return us back or anything and
  7437. 4:39:09we don't actually use the integer array
  7438. 4:39:11I outside of these curly braces it's not
  7439. 4:39:13actually available up here numbers won't
  7440. 4:39:15be accessible inside here because it's
  7441. 4:39:17simply not within the scope numbers is
  7442. 4:39:20only available within this curly brace
  7443. 4:39:23and this curly brace which means when
  7444. 4:39:25this function finishes numbers has now
  7445. 4:39:27been deleted from memory so you can't
  7446. 4:39:29access it after this line void doesn't
  7447. 4:39:31do anything it simply just does the
  7448. 4:39:34contents of the function and as soon as
  7449. 4:39:36it reaches the bottom that's all that
  7450. 4:39:37happens you can have another function
  7451. 4:39:39defined that can return a value
  7452. 4:39:41like I said with in. trass for example
  7453. 4:39:44at the end of the function you'll see a
  7454. 4:39:46word that says something like this it
  7455. 4:39:48will have return and then return back
  7456. 4:39:50something followed by a semicolon and
  7457. 4:39:52that will dictate that as soon as that
  7458. 4:39:54function is done it's going to give you
  7459. 4:39:56something back now we're going to cover
  7460. 4:39:58that in the next video but for now we're
  7461. 4:40:00only going to cover void functions so we
  7462. 4:40:02can have something more obvious like a
  7463. 4:40:04static void welcome
  7464. 4:40:06message and simply just do a console
  7465. 4:40:08right line welcome to my program and we
  7466. 4:40:11don't need to return anything here
  7467. 4:40:12because all this function will do is
  7468. 4:40:14simply print out a welcome to the screen
  7469. 4:40:17and we can use that just before the
  7470. 4:40:18print array so we can say welcome
  7471. 4:40:20message Open Bracket close bracket and
  7472. 4:40:23then a semicolon and hit F5 to run and
  7473. 4:40:26there we go as you can see our main is
  7474. 4:40:28really tidy now it gives us function
  7475. 4:40:30names that tells us exactly what's going
  7476. 4:40:32on we're creating and printing an array
  7477. 4:40:34and then welcoming the user to the
  7478. 4:40:36console and then having a console read
  7479. 4:40:38line at the end there isn't an awful lot
  7480. 4:40:40of void functions all you need to know
  7481. 4:40:42is that when you create a function you
  7482. 4:40:43need to give it a return type it returns
  7483. 4:40:45back a certain data type or it simply
  7484. 4:40:47just doesn't and depending on what you
  7485. 4:40:49want to do and what you want to achieve
  7486. 4:40:51with that function really depends on if
  7487. 4:40:53you want to return a data type or not in
  7488. 4:40:55the case of these since they're just
  7489. 4:40:57doing something and then moving on you
  7490. 4:40:59don't actually need to return anything
  7491. 4:41:00by it with in. tripod you want to make
  7492. 4:41:02the function is executed successfully so
  7493. 4:41:05when you use int. tripas you can see
  7494. 4:41:07that returns back a bull and the bull
  7495. 4:41:09determines whether the conversion has
  7496. 4:41:11been successful and based on that return
  7497. 4:41:13value you can determine the next steps
  7498. 4:41:15of your program if all your function
  7499. 4:41:16needs to do is provide some sort of
  7500. 4:41:18functionality and then quit then that's
  7501. 4:41:21fine and avoid is fine but if you need
  7502. 4:41:23to provide some functionality and then
  7503. 4:41:25also return a value then you'll need to
  7504. 4:41:28specify the data type in here which
  7505. 4:41:29we'll cover in the next
  7506. 4:41:32video so far we've covered aoid
  7507. 4:41:35functions but we haven't covered return
  7508. 4:41:37types vo functions are where the
  7509. 4:41:38function just does something but doesn't
  7510. 4:41:40actually return anything hence the name
  7511. 4:41:42void let's start with an example if you
  7512. 4:41:45have a static void print
  7513. 4:41:48introduction might want to just return
  7514. 4:41:50back your name after you've printed this
  7515. 4:41:52line it's going to print it to the
  7516. 4:41:53console and that's all it's going to
  7517. 4:41:55do there we go let's say there's a
  7518. 4:41:58situation where you actually want to use
  7519. 4:41:59this inside your code you can actually
  7520. 4:42:01set the console's title by typing in
  7521. 4:42:03console. title and type in your name
  7522. 4:42:05inside the quotation marks or you can
  7523. 4:42:06type in anything inside here if you run
  7524. 4:42:08that you'll see Abra is in the title of
  7525. 4:42:10the console right now and that looks
  7526. 4:42:11pretty cool what you got to realize is
  7527. 4:42:14now we have two references to the same
  7528. 4:42:15value now at this point we can actually
  7529. 4:42:18do something better we can type in
  7530. 4:42:20static string name equals abber and then
  7531. 4:42:23instead of using abber in here we can
  7532. 4:42:25say name and instead of using abber in
  7533. 4:42:27here we can also say a name and they're
  7534. 4:42:29both using this variable and if you run
  7535. 4:42:30the code now you see that the same
  7536. 4:42:32output will happen you have abber twice
  7537. 4:42:34both in the title and both in the
  7538. 4:42:35console output now the problem with this
  7539. 4:42:37approach is you're actually making use
  7540. 4:42:38of a global variable which we do not
  7541. 4:42:40like code in situation like this you
  7542. 4:42:42shouldn't need to make a global variable
  7543. 4:42:44because there are ways around it so
  7544. 4:42:46instead of having this which has a print
  7545. 4:42:48introduction and we've got the name used
  7546. 4:42:49in there but the name is coming from
  7547. 4:42:51here maybe we can use this somewhere
  7548. 4:42:53else so let's say we make another
  7549. 4:42:54function called Static and instead of
  7550. 4:42:57void we want to actually return back a
  7551. 4:42:58string so we can say string and then we
  7552. 4:43:01can say my
  7553. 4:43:03name or something like return name or
  7554. 4:43:06anything you want just make sure you
  7555. 4:43:08give it a meaningful name so it dictates
  7556. 4:43:10what the function is trying to do so
  7557. 4:43:11this ISS to return the name and the
  7558. 4:43:13reason why there's a red underline is it
  7559. 4:43:15says not all called path return a value
  7560. 4:43:18since we need to use our keyword return
  7561. 4:43:21now that we had some empty codes on the
  7562. 4:43:22return it's going to execute fine and
  7563. 4:43:24the compile error is gone so obviously
  7564. 4:43:26we don't want to just return nothing we
  7565. 4:43:27actually want to take this ab and return
  7566. 4:43:29in here so let's get rid of our Global
  7567. 4:43:32variable and let's see what we're going
  7568. 4:43:33to break so we're going to break these
  7569. 4:43:35two references to the name variable and
  7570. 4:43:37simply inside here we can actually just
  7571. 4:43:39call return name name and then here as
  7572. 4:43:42well we can call return name and what
  7573. 4:43:44this will do is inside here it will
  7574. 4:43:47execute this function and then return
  7575. 4:43:49the value in here if this is too many
  7576. 4:43:52steps for you you can have a string name
  7577. 4:43:54equals return name and then simply use
  7578. 4:43:56the name inside the brackets these two
  7579. 4:43:59lines are doing exactly the same thing
  7580. 4:44:01except this is using a variable cuz
  7581. 4:44:03we're storing it first and then we're
  7582. 4:44:04using it whereas if you place it
  7583. 4:44:06straight inside the brackets that's just
  7584. 4:44:08going to store it and use it immediately
  7585. 4:44:09and there's no additional variable but
  7586. 4:44:11don't worry too much if you're going to
  7587. 4:44:13do this notation because an extra
  7588. 4:44:15variable in this case doesn't really
  7589. 4:44:16matter because as soon as this function
  7590. 4:44:18is executed the variable used to run
  7591. 4:44:20this function gets deleted since it's
  7592. 4:44:22inside the scope of these two curly
  7593. 4:44:24braces so now if you run the code you'll
  7594. 4:44:27see that again we have abber and abber
  7595. 4:44:28but now what we've done is actually
  7596. 4:44:30control everything throughout here so if
  7597. 4:44:32your name were to change or maybe this
  7598. 4:44:34changes in a different circumstance
  7599. 4:44:36based on the user that's logged in then
  7600. 4:44:38you can simply have the same thing now
  7601. 4:44:40printed in two different locations in
  7602. 4:44:42the title and in the console output and
  7603. 4:44:45it's actually coming from one function
  7604. 4:44:47so we can have a return name and we can
  7605. 4:44:49make a static in now and can say return
  7606. 4:44:52H and then we can have simply return 23
  7607. 4:44:55and that's our name and our age being
  7608. 4:44:57returned I Chang this back to abber and
  7609. 4:44:59then instead of the print introduction
  7610. 4:45:01maybe we actually want to print a little
  7611. 4:45:02bit more than the name and maybe in the
  7612. 4:45:05title what we can do is have string
  7613. 4:45:07interpolation wrap the return name and
  7614. 4:45:10maybe put a high in there and then we
  7615. 4:45:12can say return age so if we on the code
  7616. 4:45:14now you can see we have abber hyphen 23
  7617. 4:45:18so maybe that's the title of our console
  7618. 4:45:20but then in the introduction we can use
  7619. 4:45:22a bit more than that so we can have int
  7620. 4:45:24age equals return age and then in the
  7621. 4:45:26console output we can have something
  7622. 4:45:28like this hello my name is name and my
  7623. 4:45:32age is age and then after we've compiled
  7624. 4:45:35that together we can place the output
  7625. 4:45:37inside here there we go so we're using
  7626. 4:45:40the name and the age inside the console
  7627. 4:45:42title and then we have this function
  7628. 4:45:44that runs to get us back the name and
  7629. 4:45:46the age and then we're using it using
  7630. 4:45:48string interpolation of course these
  7631. 4:45:50four lines can be all squished into one
  7632. 4:45:53you can simply take this dollar sign let
  7633. 4:45:55me duplicate this
  7634. 4:45:57line you can take this and paste it
  7635. 4:46:00straight into here and where you have
  7636. 4:46:01name we can have our return name and
  7637. 4:46:04where you have age you can have return
  7638. 4:46:05age and that will actually rule out all
  7639. 4:46:08of these lines and you can wrap it all
  7640. 4:46:10into one
  7641. 4:46:11there you
  7642. 4:46:13go and if age were to change like I said
  7643. 4:46:15before you can simply type this number
  7644. 4:46:17in change it and you can see in both
  7645. 4:46:19cases in the title and in the console
  7646. 4:46:21output it has officially changed
  7647. 4:46:23functions can be very helpful to reduce
  7648. 4:46:25repetition in code let's show you an
  7649. 4:46:27example if you had three sections where
  7650. 4:46:30you said enter a
  7651. 4:46:32number and you had int num one equals
  7652. 4:46:35convert to n32 and then we add a
  7653. 4:46:38console. readline
  7654. 4:46:41and then you had this section and you
  7655. 4:46:43wanted to do num
  7656. 4:46:44two and you had this section again and
  7657. 4:46:46you actually wanted to do num three now
  7658. 4:46:49at this point even though these three
  7659. 4:46:50are actually identical you want to be
  7660. 4:46:52using array in this case so you could
  7661. 4:46:54have int numbers equals new in square
  7662. 4:46:58braet 3 and then here we could say
  7663. 4:47:00numers
  7664. 4:47:01zero numbers
  7665. 4:47:03one numbers two and then we don't need
  7666. 4:47:06the ins cuz it's already been defined
  7667. 4:47:08and then we have them like this but
  7668. 4:47:10because again all these three sections
  7669. 4:47:11are exactly identical we can actually
  7670. 4:47:13run a for Loop that goes from numbers do
  7671. 4:47:16length and then we can place one of
  7672. 4:47:18these straight inside here and then
  7673. 4:47:20change this to I and now we've achieved
  7674. 4:47:22exactly the same thing but we're not
  7675. 4:47:24actually done yet so if you run the code
  7676. 4:47:26and we do one 2 3 now we've stored all
  7677. 4:47:28three numbers so we have enter a number
  7678. 4:47:31and then we have the console read line
  7679. 4:47:32and maybe we can actually make these two
  7680. 4:47:34lines a little bit shorter so let's make
  7681. 4:47:36a function static and because we
  7682. 4:47:38essentially want to return an integer we
  7683. 4:47:40can say static in read number from
  7684. 4:47:45console and then if we take these two
  7685. 4:47:47lines place them inside here and because
  7686. 4:47:49we need to return back an integer this
  7687. 4:47:51is the value that we're going to be
  7688. 4:47:52returning so instead of assigning this
  7689. 4:47:54position to something we'll actually
  7690. 4:47:56just immediately return at this point so
  7691. 4:47:58instead of doing this line now and
  7692. 4:48:01instead of doing all of this we can
  7693. 4:48:03simply make this into one line of code
  7694. 4:48:05read number from console so every time
  7695. 4:48:07this will execute will jump into here
  7696. 4:48:09print out the value enter a number the
  7697. 4:48:12user will typee it in it will read it in
  7698. 4:48:14convert it to an integer 32 and this
  7699. 4:48:16will return back the integer that the
  7700. 4:48:18user's typed in and then that will
  7701. 4:48:19return it back to the console and
  7702. 4:48:20essentially replace this so if the user
  7703. 4:48:23is typed in five then it essentially
  7704. 4:48:24replace this with five store it loop
  7705. 4:48:26around and have to placed in 10 the next
  7706. 4:48:28time and then you replace this with 10
  7707. 4:48:30essentially and then that gets stored
  7708. 4:48:32inside and so on and so forth until all
  7709. 4:48:34the positions have been complete let's
  7710. 4:48:36test that 1 2 3 and it still works okay
  7711. 4:48:40of course I'm not actually printing the
  7712. 4:48:41values out to the screen so if you
  7713. 4:48:42wanted to see that quickly for each can
  7714. 4:48:46have V item inside our numbers and then
  7715. 4:48:48do a console right with some string
  7716. 4:48:50interpolation item followed by a space
  7717. 4:48:53and let's run that one two and three and
  7718. 4:48:55there we go we get one two and three of
  7719. 4:48:57course you can make these functions and
  7720. 4:48:58they can return any bits of data that
  7721. 4:49:00you want you can even have something
  7722. 4:49:02like an INT array and we can say create
  7723. 4:49:05random array and then we can create an
  7724. 4:49:06array call numbers make that equal to
  7725. 4:49:09new int square bracket 3 and we can
  7726. 4:49:12actually give it the values of 0 1 and
  7727. 4:49:14two and then just at the end here we can
  7728. 4:49:16actually just return numbers and then
  7729. 4:49:18just in here we can have an INT array or
  7730. 4:49:20we can call that new numbers since we
  7731. 4:49:22have numbers above just above here and
  7732. 4:49:24then we can make that equal to create
  7733. 4:49:26random array and because this returns us
  7734. 4:49:28back with an integer array there's no
  7735. 4:49:31compile array inside here and we can
  7736. 4:49:33have a four each follows the exact same
  7737. 4:49:35approach as the one above new numbers
  7738. 4:49:37console right line into a console right
  7739. 4:49:40and then have item followed by a space
  7740. 4:49:43now if we print these both out you can
  7741. 4:49:45see that the zero one and two actually
  7742. 4:49:47comes from the new one it's just we have
  7743. 4:49:49two console rights right next to each
  7744. 4:49:51other so there's no new line separating
  7745. 4:49:52them so if we just add a console right
  7746. 4:49:54line here 1 2 and three and there you go
  7747. 4:49:581 2 and three comes from the three
  7748. 4:49:59inputs we just typed in and 0 1 and two
  7749. 4:50:02actually come from the function that
  7750. 4:50:03we've just created of course if you
  7751. 4:50:06don't like this notation then what you
  7752. 4:50:08can do is you can make it a lot easier
  7753. 4:50:09you don't even need to make a new
  7754. 4:50:11variable you can actually just return
  7755. 4:50:13the integer just like this so if we
  7756. 4:50:15place
  7757. 4:50:16this straight inside here actually just
  7758. 4:50:20make it on one line just so it looks a
  7759. 4:50:21bit neater then there you go we have our
  7760. 4:50:23entire function reduced in one line the
  7761. 4:50:26goal of functions is to make them do as
  7762. 4:50:28little as possible if your function has
  7763. 4:50:30two different things it's doing or more
  7764. 4:50:32then you should break that out into
  7765. 4:50:34another function for example if you had
  7766. 4:50:35a function that did two distinct
  7767. 4:50:37operations one was a conversion and one
  7768. 4:50:39was a calculation then you want to split
  7769. 4:50:41out into two the first function should
  7770. 4:50:43be the conversion and the second
  7771. 4:50:44function should be the calculation and
  7772. 4:50:46the reason why they're separate is
  7773. 4:50:48because if you want to reuse them as a
  7774. 4:50:50pair then you won't be able to reuse
  7775. 4:50:52each of the pairs if you wanted to have
  7776. 4:50:54a calculation on its own then you can't
  7777. 4:50:56do that if both functions do the same
  7778. 4:50:58thing for example you could have static
  7779. 4:51:02and it just could be something let's
  7780. 4:51:03just call it void and it could just be
  7781. 4:51:05test for example and if you had the
  7782. 4:51:07conversion here and then if you had the
  7783. 4:51:09calculation here then you need to split
  7784. 4:51:11this up into two different functions you
  7785. 4:51:13want have static valed
  7786. 4:51:16conversion and static vied calculation
  7787. 4:51:19if you wanted to use conversion or
  7788. 4:51:21calculation separately you can't because
  7789. 4:51:23now it's in this pair together so if you
  7790. 4:51:24wanted to use them separately you simply
  7791. 4:51:26can't it's best to keep functions on the
  7792. 4:51:28RW anywhere just so they have a single
  7793. 4:51:30purpose so you can give them a name cuz
  7794. 4:51:32as you can see here we can't give this
  7795. 4:51:34an appropriate name because it's doing
  7796. 4:51:35more than one task this is doing the
  7797. 4:51:37conversion this is doing the calculation
  7798. 4:51:39and this is doing mult multiple things
  7799. 4:51:41this could be doing something if you
  7800. 4:51:43have to think too hard about the
  7801. 4:51:44function title then you might be doing
  7802. 4:51:46too much within each of functions these
  7803. 4:51:48are very easy to be able to decide what
  7804. 4:51:50they do cuz this one converts and this
  7805. 4:51:52one's going to calculate reading number
  7806. 4:51:54from console if this was reading the
  7807. 4:51:56number from the console and doing
  7808. 4:51:58something else then it's going to be a
  7809. 4:52:00bit of a problem because now you've got
  7810. 4:52:01a bigger name you don't want to be doing
  7811. 4:52:03reading number from console and and
  7812. 4:52:05something else cuz then your function
  7813. 4:52:07name gets also too big and you got
  7814. 4:52:09multiple dependencies so the best thing
  7815. 4:52:11to do is to call them accordingly and
  7816. 4:52:13keep them as short as possible so let's
  7817. 4:52:15say we wanted to make a function that
  7818. 4:52:16added two values we can have a static
  7819. 4:52:18int call it add and then we can return 5
  7820. 4:52:21+ 5 now this function is a little bit
  7821. 4:52:23redundant although it will actually work
  7822. 4:52:25so if we go back to our main and we do a
  7823. 4:52:27console right line and actually print
  7824. 4:52:29the value of
  7825. 4:52:31ADD and we go through our function then
  7826. 4:52:33we see we get the value of 10 which is
  7827. 4:52:35indeed 5 +
  7828. 4:52:375 but since it's 5 + 5 won't change
  7829. 4:52:40range it's always going to be 5 + 5 this
  7830. 4:52:43is where parameters will come in which
  7831. 4:52:44we'll cover in the next videos
  7832. 4:52:45parameters are a way of being able to
  7833. 4:52:47give the function value so these two can
  7834. 4:52:49be actually changed there's no point in
  7835. 4:52:51having an add function if the only
  7836. 4:52:53ability you can do is add 5 + 5 cuz you
  7837. 4:52:56may as well just return 10 at this point
  7838. 4:52:58so I covered a lot in this video and
  7839. 4:52:59there's lots of different functions that
  7840. 4:53:01do different things of course there is
  7841. 4:53:03quite a lot of lines we're up to 87 this
  7842. 4:53:05is probably the biggest project that
  7843. 4:53:06we've wrote so far and even though this
  7844. 4:53:08is not really a project it's just a buns
  7845. 4:53:10functions that do little things that are
  7846. 4:53:12made for demonstrations so let's have a
  7847. 4:53:14recap so first here we're setting the
  7848. 4:53:16title as a result of return name and
  7849. 4:53:19return age we can actually take this and
  7850. 4:53:22develop this further if you want to we
  7851. 4:53:24could actually say return name age pair
  7852. 4:53:27and if you wanted to collapse that
  7853. 4:53:28inside one function then that could be
  7854. 4:53:30really easy you could say static string
  7855. 4:53:34return name age pair and then you can
  7856. 4:53:37have return and literally the exact same
  7857. 4:53:39line above you can have return name
  7858. 4:53:41followed by a hyphen and then you can
  7859. 4:53:43have a return age and then end the line
  7860. 4:53:46like that so that just concatenates the
  7861. 4:53:48two values that are returned from these
  7862. 4:53:49two functions but the point is you keep
  7863. 4:53:51them all separate if you were to type in
  7864. 4:53:53very statically inside here AB 25 then
  7865. 4:53:57obviously like I've talked about above
  7866. 4:53:58then you're going to have duplication of
  7867. 4:54:00data and of course we're going to use
  7868. 4:54:01this name inside something like here and
  7869. 4:54:04as you can see this name age pair is
  7870. 4:54:06actually not applicable when it comes
  7871. 4:54:07back to this format so it's always best
  7872. 4:54:09to keep the these two separately so if
  7873. 4:54:12you bring it back and we can actually
  7874. 4:54:13keep it like this now instead of having
  7875. 4:54:15this code we can actually say return
  7876. 4:54:18name age pair that will give us the same
  7877. 4:54:20value at the top of the code it will
  7878. 4:54:22have ABA hyphen
  7879. 4:54:2325 so that's that bit in our print
  7880. 4:54:26introduction If We Hold control and we
  7881. 4:54:28actually press on this function it will
  7882. 4:54:30actually jump us right down and then if
  7883. 4:54:32we press the back arrow top left we can
  7884. 4:54:34go back to it there we go and then click
  7885. 4:54:36forward so in this function we can break
  7886. 4:54:38down all of the return values into to
  7887. 4:54:40string variables we have a string name
  7888. 4:54:42equals return name and inage equals
  7889. 4:54:44return age then we have a string input
  7890. 4:54:46that concatenates them both together
  7891. 4:54:48hello my name is name and my age is age
  7892. 4:54:51and then we print out to the screen or
  7893. 4:54:52if you wanted to we could wrap all four
  7894. 4:54:54of these lines straight up into one we
  7895. 4:54:57don't need to assign these into
  7896. 4:54:58variables although it does help the
  7897. 4:55:00readability if you keep them separate
  7898. 4:55:02but I prefer to keep it like this since
  7899. 4:55:03I'm used to this notation we can place
  7900. 4:55:06return name straight into there which
  7901. 4:55:07will get rid of our name variable and
  7902. 4:55:09the same thing with age which will get
  7903. 4:55:11rid of these two variables and of course
  7904. 4:55:13we're actually storing the string value
  7905. 4:55:15before we printing it so we could just
  7906. 4:55:17take all of this and place it straight
  7907. 4:55:19instead of here scrolling back to the
  7908. 4:55:23top this little section was
  7909. 4:55:25demonstrating how we can use integer
  7910. 4:55:27arrays to read in all the values from
  7911. 4:55:29the console but wrap it around in a
  7912. 4:55:31function so before in here you would
  7913. 4:55:33have something like a console right line
  7914. 4:55:35You' print out the value to the screen
  7915. 4:55:36that would say something like a ter
  7916. 4:55:38number and then you have on the next
  7917. 4:55:39line a convert to in32 and then after
  7918. 4:55:43you converted it in 32 you'll actually
  7919. 4:55:45start straight to numbers but because
  7920. 4:55:47this is going to be the same thing for
  7921. 4:55:49inserting all the numbers you can
  7922. 4:55:50actually wrap this around in a function
  7923. 4:55:52so if we hold control again and go to
  7924. 4:55:54this you'll see that them two lines are
  7925. 4:55:55very simply placed in here console right
  7926. 4:55:58Line enter a new number and then convert
  7927. 4:56:00in 32 based on the console read line and
  7928. 4:56:03then we simply just returning that back
  7929. 4:56:05ready to be stored in the index of the
  7930. 4:56:07numbers array and then straight after
  7931. 4:56:09that we're just printing out all of
  7932. 4:56:10values to make sure it's worked and that
  7933. 4:56:12all succeeded fine here I was just
  7934. 4:56:14demonstrating that you can have
  7935. 4:56:15different return values and it can
  7936. 4:56:16literally be anything so as you can see
  7937. 4:56:18here if you hover over create random
  7938. 4:56:20array it's inside the program class it's
  7939. 4:56:23called create random array and it
  7940. 4:56:24returns back an integer array so that's
  7941. 4:56:26what we're signed into and if we hold
  7942. 4:56:28control and click on this again you can
  7943. 4:56:30see that all this simply does is returns
  7944. 4:56:33us back a three length array with 0 1
  7945. 4:56:36and two as the array positions and then
  7946. 4:56:38if we scroll back up when we execute
  7947. 4:56:40this for each Loop then we're simply
  7948. 4:56:42going to get 0 1 and two just like that
  7949. 4:56:45since it's a console right and it'll be
  7950. 4:56:47on the same line with a space after each
  7951. 4:56:51other this demonstration was slightly
  7952. 4:56:54theoretical I was saying in one given
  7953. 4:56:56situation a function should only do one
  7954. 4:56:58task if your function converts and does
  7955. 4:57:00a calculation at the same time then
  7956. 4:57:02these are actually two different things
  7957. 4:57:04you should split them so you use the
  7958. 4:57:06conversion in this step and you use the
  7959. 4:57:08calculation in this step if you want to
  7960. 4:57:10convert something or calculate something
  7961. 4:57:12in the future then you can only do it
  7962. 4:57:14now because we've separated them whereas
  7963. 4:57:16before if you have them both inside here
  7964. 4:57:19then you simply can't do one operation
  7965. 4:57:21because you've wrapped it around in one
  7966. 4:57:23section the same is true when I was
  7967. 4:57:25discussing this earlier on if we kept
  7968. 4:57:27this name pair as the only way to access
  7969. 4:57:29the age in the name then it's going to
  7970. 4:57:31mess up when we use it in here and then
  7971. 4:57:32we're going to have a duplication of
  7972. 4:57:34data but because we separated the name
  7973. 4:57:36and we separated the age we can use it
  7974. 4:57:38in this function and we can in this
  7975. 4:57:40function and it doesn't interrupt
  7976. 4:57:42anything this simply Returns the name
  7977. 4:57:44this simply Returns the age and these
  7978. 4:57:46two functions make use of them if we had
  7979. 4:57:49something else underneath there were
  7980. 4:57:50another function that depended on the
  7981. 4:57:52name of the edge then again we can
  7982. 4:57:54simply use them if it was restricted to
  7983. 4:57:56this function then it's going to be a
  7984. 4:57:58problem using it in this function or any
  7985. 4:58:00future
  7986. 4:58:01ones and finally just to finish off add
  7987. 4:58:04function and all it simply does is
  7988. 4:58:06return 5 + 5 and I was saying as of
  7989. 4:58:08right now even though return return
  7990. 4:58:10returns back a value it's kind of
  7991. 4:58:11pointless CU you may as well just return
  7992. 4:58:13back 10 since it's doing the same thing
  7993. 4:58:15the reason I included this function is
  7994. 4:58:17that we can set it up and start planting
  7995. 4:58:19the seeds to understand why functions
  7996. 4:58:21exist and how to make use of parameters
  7997. 4:58:23what a parameter is is it lets you put
  7998. 4:58:25things Within These brackets so you can
  7999. 4:58:27give this function some data so if you
  8000. 4:58:29have an in. trass for example these
  8001. 4:58:32things that we place inside the brackets
  8002. 4:58:33are actually the parameters so we have a
  8003. 4:58:35string s and out in something and that's
  8004. 4:58:38how the parameters work on our trip pass
  8005. 4:58:40function and just to show you this is
  8006. 4:58:43how it's actually wrote It's a Bool and
  8007. 4:58:45then it's called
  8008. 4:58:53Triassic and I know it's give us an
  8009. 4:58:55error because we don't have the control
  8010. 4:58:57path if we just use result and assign it
  8011. 4:58:59to zero and then return back true you
  8012. 4:59:01can see that the compil error is now
  8013. 4:59:03gone and as you can see here it says
  8014. 4:59:04bull trip pass string s out in result
  8015. 4:59:07and if we actually try and use the trip
  8016. 4:59:09pass in here and press the bracket you
  8017. 4:59:11can see that these parameters are
  8018. 4:59:13exactly identical to parameters that
  8019. 4:59:15were inside of the tripar coming from
  8020. 4:59:17the integer Library so in the future
  8021. 4:59:20we're going to study how we can use
  8022. 4:59:21parameters to pass data inside our
  8023. 4:59:23function but just for now know that this
  8024. 4:59:26function is essentially pointless
  8025. 4:59:28because it's only going to return back
  8026. 4:59:2910 and this function will gain the
  8027. 4:59:31functionality it needs when we start
  8028. 4:59:33adding parameters this should
  8029. 4:59:35essentially be add five + 5 except this
  8030. 4:59:38is kind of a pointless function because
  8031. 4:59:40we already know the value is going to be
  8032. 4:59:4110 but again this is just helping you
  8033. 4:59:44understand why parameters are very
  8034. 4:59:45essential to
  8035. 4:59:48functions we've covered how to create a
  8036. 4:59:50VI function and a function that returns
  8037. 4:59:52a data type but now we want to create a
  8038. 4:59:54function that gives it some sort of data
  8039. 4:59:56you may not realize it but all these
  8040. 4:59:58functions that you normally use like
  8041. 4:59:59console right line you're actually
  8042. 5:00:01inserting data consistently into all of
  8043. 5:00:03these functions if you hover over
  8044. 5:00:05console right line you see his void
  8045. 5:00:07console right line and right now there's
  8046. 5:00:09Open Bracket and there's a Clos bracket
  8047. 5:00:11if we delete this bracket and reopen it
  8048. 5:00:12you see there's 19 different ways that
  8049. 5:00:14you can call this function and as soon
  8050. 5:00:15as you start to type a string you can
  8051. 5:00:17see we have a string format and an
  8052. 5:00:19object if you need to and if you type in
  8053. 5:00:20another string you can see it says
  8054. 5:00:22string value this string value is
  8055. 5:00:24actually a parameter If We Hold control
  8056. 5:00:26and press on right line you can see all
  8057. 5:00:28of the different functions right here
  8058. 5:00:30this is the most basic version that just
  8059. 5:00:32inserts a right line character to the
  8060. 5:00:33console and then these are all the
  8061. 5:00:35different parameters that you can give
  8062. 5:00:36the value of course you can't double
  8063. 5:00:39click and view the source code because
  8064. 5:00:40this is hidden inside Microsoft's
  8065. 5:00:43libraries the values that are placed
  8066. 5:00:45inside these parentheses are actually
  8067. 5:00:46called the parameters this is known as
  8068. 5:00:49functional overloading which we'll cover
  8069. 5:00:51later on in the course basically that
  8070. 5:00:53means you can have the same name
  8071. 5:00:54function but give it different
  8072. 5:00:56parameters and you'll see why this is
  8073. 5:00:57helpful later on down the line so let's
  8074. 5:00:59close this tab in the top right hand
  8075. 5:01:01corner and let's bring back out code so
  8076. 5:01:03now that we've passed hello into this
  8077. 5:01:04function that was actually given this a
  8078. 5:01:06string value parameter and that hello is
  8079. 5:01:09going to get past straight into the
  8080. 5:01:10console right line so then when we run
  8081. 5:01:12the code it's going to print into the
  8082. 5:01:13console so let's create our own function
  8083. 5:01:15with our own parameters so if you take
  8084. 5:01:17the function I had last time it was
  8085. 5:01:19static int add Open Bracket close
  8086. 5:01:21bracket and it simply returned 5 + 5
  8087. 5:01:24which we all know is going to be 10 so
  8088. 5:01:26if we place add inside these brackets
  8089. 5:01:28and we run the code we should get 10
  8090. 5:01:30return back there we go so of course
  8091. 5:01:32this is a little bit pointless since
  8092. 5:01:34it's always going to be adding 5 + 5
  8093. 5:01:36there is no way we can change this
  8094. 5:01:38without the use of parameters so in here
  8095. 5:01:40we can actually just do what we do when
  8096. 5:01:42we normally declare a variable we say
  8097. 5:01:44the data type int and then the variable
  8098. 5:01:46name and then if you want to add
  8099. 5:01:48multiple parameters you can simply add a
  8100. 5:01:49comma so now we can say int a and int B
  8101. 5:01:53and you can see now we've got a syntax
  8102. 5:01:54error because it expects us to have both
  8103. 5:01:56of these values inside if you hover over
  8104. 5:01:58it there is no argument given that
  8105. 5:02:00corresponds to the required formal
  8106. 5:02:01parameter a and then if we insert a by
  8107. 5:02:04having five for example and if we hover
  8108. 5:02:06over it it complains about not having B
  8109. 5:02:09so we can do comma and then do five
  8110. 5:02:11again so just this on its own is not
  8111. 5:02:13actually going to do anything we're
  8112. 5:02:14actually giving the add function the
  8113. 5:02:16values of five and five and place them
  8114. 5:02:18inside a and inside B variables these
  8115. 5:02:21are now known as local variables and
  8116. 5:02:23they only exist in memory between these
  8117. 5:02:25two curly braces as soon as the function
  8118. 5:02:27is done these are deleted from memory
  8119. 5:02:29and you can no longer access in a and in
  8120. 5:02:31B you can't just do something like a
  8121. 5:02:33inside here and think that this can be
  8122. 5:02:35used up here these are in two completely
  8123. 5:02:38different Scopes this is one for
  8124. 5:02:39function and this is another function
  8125. 5:02:41and they're completely separate from
  8126. 5:02:43each other when you type in five and
  8127. 5:02:44five it goes in order so int a is the
  8128. 5:02:47first one so five gets placed into here
  8129. 5:02:49and this five gets placed into B so now
  8130. 5:02:52that we need to make use of them we can
  8131. 5:02:53simply change this to a and change this
  8132. 5:02:56one to B now if you run the code we
  8133. 5:02:58should still get 10 but now our function
  8134. 5:03:00Works a bit better we can give it a set
  8135. 5:03:02of data so if you actually just put
  8136. 5:03:04zeros instead of here then we should get
  8137. 5:03:06a 100 returned perfect so now that we
  8138. 5:03:09figured out how to to do this let's have
  8139. 5:03:10a look at different ways that we can
  8140. 5:03:11make use of this function so let's say
  8141. 5:03:13we had a situation where we did console
  8142. 5:03:15right enter a
  8143. 5:03:18number and we add intn equals convert to
  8144. 5:03:23in32 and then you're converting the
  8145. 5:03:25console do
  8146. 5:03:27readline and let's wrap this inside a
  8147. 5:03:30function so before what I was doing is
  8148. 5:03:32simply just returning back in int it was
  8149. 5:03:34static int read
  8150. 5:03:37int and then we took these two lines and
  8151. 5:03:40place it straight inside here and remove
  8152. 5:03:42the fact that we have an integer and
  8153. 5:03:43simply just change this to a return and
  8154. 5:03:45what this now will say is let's output
  8155. 5:03:47enter a number to the console let the
  8156. 5:03:49user type it in inside the console read
  8157. 5:03:52line and then read it in convert it to
  8158. 5:03:54an integer and then give it back to
  8159. 5:03:55whatever calls it so now we can get rid
  8160. 5:03:57of this and get rid of this convert and
  8161. 5:03:59simply just type in read in inside here
  8162. 5:04:02and after that line let's just print it
  8163. 5:04:04out to make sure it's worked okay enter
  8164. 5:04:06a number 10 press enter and we have 10
  8165. 5:04:09again and that's fine except if we want
  8166. 5:04:11to use this in another case for example
  8167. 5:04:13if you have num one maybe we don't
  8168. 5:04:15actually want to enter a number maybe
  8169. 5:04:17this time it's going to be enter an
  8170. 5:04:19angle or something of this sort so what
  8171. 5:04:21we can do if we undo that we can
  8172. 5:04:23actually introduce a string message and
  8173. 5:04:25what this will do now is actually
  8174. 5:04:27specify the message that we put in here
  8175. 5:04:29maybe there's a situation where we
  8176. 5:04:31always want to enter something so maybe
  8177. 5:04:33we can just replace this and put a
  8178. 5:04:34string inter polation and wrap this
  8179. 5:04:36number around in the curly brace and
  8180. 5:04:38replace it to message so then in here we
  8181. 5:04:40can say enter a and then we can pass the
  8182. 5:04:44string variable which will get placed in
  8183. 5:04:46here so if this is a number then we can
  8184. 5:04:48type a number and maybe this is
  8185. 5:04:49something like an
  8186. 5:04:51angle and then here we can type an angle
  8187. 5:04:54so we have 10 enter a number and then
  8188. 5:04:56there you go it says enter an angle then
  8189. 5:04:58we can have 100 and that's working as
  8190. 5:05:00well alternatively if you want to
  8191. 5:05:02specify the entire message then you can
  8192. 5:05:04remove these and keep it like this and
  8193. 5:05:07then in both of these situations we can
  8194. 5:05:08say enter and number and copy that down
  8195. 5:05:11like this and make sure you put the call
  8196. 5:05:13on at the end followed by the space
  8197. 5:05:15there we go enter a number 10 and an
  8198. 5:05:18angle 20 maybe the situation where this
  8199. 5:05:21text actually changed and you just want
  8200. 5:05:23it to be angle and if you run that again
  8201. 5:05:25then you'll see that these are
  8202. 5:05:26completely different now one says enter
  8203. 5:05:28a number and one says angle but this
  8204. 5:05:30gives us a lot of flexibility in what we
  8205. 5:05:32pass in if we keep it as enter a and
  8206. 5:05:35then let the user decide on what this
  8207. 5:05:36section is then you rule out any
  8208. 5:05:38opportunity to be able to do that option
  8209. 5:05:40if you wanted to to prevent the colon in
  8210. 5:05:42the space always being at the end you
  8211. 5:05:44can actually just add that in manually
  8212. 5:05:46if you bring back the string
  8213. 5:05:47interpolation we can say colon followed
  8214. 5:05:49by space so you don't have to remember
  8215. 5:05:51to always put that in there we go so
  8216. 5:05:53that's doing the exact same thing and
  8217. 5:05:54we're actually just reducing the amount
  8218. 5:05:56of repetition because we have to
  8219. 5:05:58remember to put a call on Follow by
  8220. 5:06:00space inside here we might just forget
  8221. 5:06:02it and then when you leave it like this
  8222. 5:06:04then the cord's going to look ugly
  8223. 5:06:05because it looks like this and it's all
  8224. 5:06:07stuck together so let's make use of
  8225. 5:06:09these two functions and let's see if we
  8226. 5:06:10can write a really basic calculator we
  8227. 5:06:13can say int first num equals read int
  8228. 5:06:16and we can say enter first number and
  8229. 5:06:19then duplicate that down change first to
  8230. 5:06:21second and then change this also to
  8231. 5:06:23Second and then inside a console right
  8232. 5:06:25line we can actually have the add
  8233. 5:06:27function and we can type in first number
  8234. 5:06:29and then we can type in second number
  8235. 5:06:31and if you don't want to call the
  8236. 5:06:32function inside here you can actually
  8237. 5:06:33just make another int and we can say int
  8238. 5:06:35result equals add and just bring this
  8239. 5:06:38line over to here and then in the
  8240. 5:06:39console right line you can just print
  8241. 5:06:41the
  8242. 5:06:42result there we go so we read in both
  8243. 5:06:44the values execute the ad function and
  8244. 5:06:47then return it back to the user using a
  8245. 5:06:49console right line so these are the
  8246. 5:06:51other ones and then here they are so if
  8247. 5:06:53we do 50 and we do 50 again and you see
  8248. 5:06:55that I've messed up because we have
  8249. 5:06:56double colums now so although our
  8250. 5:06:58calculator did indeed work let's just go
  8251. 5:07:00and correct that so we can take these
  8252. 5:07:02off now because we added it inside
  8253. 5:07:06here 50 and 25 we should get set 75
  8254. 5:07:10perfect so now we've made a little basic
  8255. 5:07:12calculator by simply making use these
  8256. 5:07:14functions and if we just expand this
  8257. 5:07:16just to show you what it would have
  8258. 5:07:17looked like so we go in from this and we
  8259. 5:07:21place our message inside to here and
  8260. 5:07:24then we have our convert line just like
  8261. 5:07:27this and then if we duplicate that down
  8262. 5:07:29we can change this to Second n and then
  8263. 5:07:31to second
  8264. 5:07:32number and then as for the result we can
  8265. 5:07:35say first num plus second
  8266. 5:07:38n there we go you can see the code is
  8267. 5:07:40drastically longer and it's a bit
  8268. 5:07:41meaningless to have duplica code this is
  8269. 5:07:44literally the exact same line in both of
  8270. 5:07:47these sections except for the fact that
  8271. 5:07:48this ISS first and this ISS second and
  8272. 5:07:51we're starring in first and we're
  8273. 5:07:52starring in second so let's undo that
  8274. 5:07:54and get back to our more efficient code
  8275. 5:07:56there we go you can see that these three
  8276. 5:07:58lines are considerably shorter and
  8277. 5:08:00easier to read this is telling us we
  8278. 5:08:02want to read an integer here is the
  8279. 5:08:03value and we want to return it inside
  8280. 5:08:05our first Nome and so on and so forth
  8281. 5:08:07let's have a look at another example
  8282. 5:08:09where we can make use of this so we can
  8283. 5:08:10have a string called name and we can
  8284. 5:08:12read this in from the console console.
  8285. 5:08:16readline but we need to Output a message
  8286. 5:08:18so we can have another console right and
  8287. 5:08:21we can say enter your
  8288. 5:08:23name and then we can have this twice and
  8289. 5:08:27we can do enter your age and then have
  8290. 5:08:29an INT
  8291. 5:08:30age but of course we don't want to be
  8292. 5:08:32using this approach and putting a
  8293. 5:08:34console right line inside here with a
  8294. 5:08:36convert in32 because although this is
  8295. 5:08:38actually technically correct now what
  8296. 5:08:40UND doing the point Val function so
  8297. 5:08:42let's change this into our readint and
  8298. 5:08:44then as for our message well we have the
  8299. 5:08:46message right here and again we don't
  8300. 5:08:47need the space callon so let's remove
  8301. 5:08:49that and then as for the name we can
  8302. 5:08:51actually do this differently so this is
  8303. 5:08:53actually going to do a string variable
  8304. 5:08:55so what we want to do instead of having
  8305. 5:08:56read in we can have another function
  8306. 5:08:58called read string another string
  8307. 5:09:01message parameter and then do exactly
  8308. 5:09:03the same thing have a console right and
  8309. 5:09:06we can place string interpolation and
  8310. 5:09:08place a message inside here followed by
  8311. 5:09:10callon space and then we can just simply
  8312. 5:09:12return the console. readline there we go
  8313. 5:09:16and instead of doing all of this we can
  8314. 5:09:18actually just put our read string inside
  8315. 5:09:20here and then place our ENT your name
  8316. 5:09:22inside here just without the callon
  8317. 5:09:25space because we don't need that so now
  8318. 5:09:27we reduce this section to two lines as
  8319. 5:09:29well so now what we can do is have a
  8320. 5:09:31string details and make that equal to a
  8321. 5:09:33function that we're going to use now we
  8322. 5:09:35can say user details if we want to give
  8323. 5:09:37it the name and give it the age and that
  8324. 5:09:39will give us back a string that we can
  8325. 5:09:41print to the console so we can say
  8326. 5:09:43details so let's go ahead and make use
  8327. 5:09:45of this function so we can have a static
  8328. 5:09:47and it's going to return us back a
  8329. 5:09:48string cuz we're placing it inside the
  8330. 5:09:50string and then it's going to be user
  8331. 5:09:52details and then our name is a string
  8332. 5:09:55our age as an INT and then inside here
  8333. 5:09:58we just want to concatenate them two
  8334. 5:09:59values together so we can say hello my
  8335. 5:10:02name is name and my age is age and
  8336. 5:10:07because that's returning us back with a
  8337. 5:10:09string we can make use of it inside
  8338. 5:10:10string details or we can place this
  8339. 5:10:13straight inside the console right line
  8340. 5:10:15let's leave it as a function call assign
  8341. 5:10:17into string details and then print out
  8342. 5:10:19to the screen let's just skip over these
  8343. 5:10:22sections enter your name abber enter
  8344. 5:10:24your age 23 and then there you go hello
  8345. 5:10:26my name is abber and my age is
  8346. 5:10:2923 so this is how we can make use of
  8347. 5:10:31parameters inside our functions so we
  8348. 5:10:33can actually give it some sort of data
  8349. 5:10:35that's meaningful if you didn't use
  8350. 5:10:36parameters then what you're going to
  8351. 5:10:37have to face is for example example what
  8352. 5:10:39we showed you with this integer if you
  8353. 5:10:41don't use parameters then you're going
  8354. 5:10:42to have situations like I showed you
  8355. 5:10:44before with this integer function it
  8356. 5:10:46returns back an integer but it only gave
  8357. 5:10:48us the value of 5 + 5 I mean technically
  8358. 5:10:50you don't need to return anything you
  8359. 5:10:52can actually just keep this aoid and
  8360. 5:10:54just wrap this around in console right
  8361. 5:10:55line the only issue with this approach
  8362. 5:10:57is you don't have any option other than
  8363. 5:10:59to print out of the screen what if you
  8364. 5:11:01didn't want to print out of the screen
  8365. 5:11:03and you simply just wanted to return the
  8366. 5:11:04calculation and then store it inside
  8367. 5:11:06another variable this doesn't give you
  8368. 5:11:08the flexibility of that so we change
  8369. 5:11:10this into an INT and instead of
  8370. 5:11:11returning it back to the console we just
  8371. 5:11:13return back the pure value and then if
  8372. 5:11:15the user decides that they want to print
  8373. 5:11:17this out to the screen that's fine like
  8374. 5:11:19here we've made a decision that we want
  8375. 5:11:21to print it out let's just say we had a
  8376. 5:11:23variable that said in calculation then
  8377. 5:11:25we place that inside but we actually
  8378. 5:11:27never wanted to print it maybe we kept
  8379. 5:11:29it like this and then use Cal for
  8380. 5:11:31something else down below or maybe that
  8381. 5:11:33was something like storing inside a
  8382. 5:11:35database not everything will be printed
  8383. 5:11:37out to the screen for the user to see
  8384. 5:11:39the best thing to do is return the
  8385. 5:11:40values in their purus form because you
  8386. 5:11:42never know what the developer or the
  8387. 5:11:44user is going to make use of that data
  8388. 5:11:45later on down the line keeping that it's
  8389. 5:11:47purus form is the best way possible for
  8390. 5:11:49example in this case we're actually
  8391. 5:11:51reading back the integer and then print
  8392. 5:11:52it out to the screen but in this exact
  8393. 5:11:54case which is the best case scenario
  8394. 5:11:56that I just showed you we're actually
  8395. 5:11:58making use of first n and second n but
  8396. 5:12:00first n and second n are never actually
  8397. 5:12:02print to the screen it's always just
  8398. 5:12:04used for the calculation if we use
  8399. 5:12:06console right line to print out first n
  8400. 5:12:08and second them then we're not going to
  8401. 5:12:10be able to use it inside our calculation
  8402. 5:12:12and therefore our very basic calculator
  8403. 5:12:14is now being rendered pointless so read
  8404. 5:12:16in and add or return back values that we
  8405. 5:12:19can use in their purest form and that is
  8406. 5:12:21the whole goal of
  8407. 5:12:24parameters so now that we've covered
  8408. 5:12:26parameters that are always going to be
  8409. 5:12:27required let's have a look at another
  8410. 5:12:29concept called optional parameters so we
  8411. 5:12:32had a static in add and we add int a and
  8412. 5:12:35int B for example and then we simply
  8413. 5:12:37just returned a plus b like use in the
  8414. 5:12:39previous examples if we have an INT
  8415. 5:12:41result and we want to call that function
  8416. 5:12:44and we give it the value five and five
  8417. 5:12:46and then print out to the screen
  8418. 5:12:47everything runs okay because we've added
  8419. 5:12:49in exactly what we expect let's say we
  8420. 5:12:52don't actually have this additional five
  8421. 5:12:53of course this doesn't make any sense
  8422. 5:12:55cuz we're meant to be adding two numbers
  8423. 5:12:57but just hum me for a minute then what
  8424. 5:12:59this will do is says there's no argument
  8425. 5:13:01given that corresponds to the parameter
  8426. 5:13:03B now of course this is a problem
  8427. 5:13:04because it's a red underline which means
  8428. 5:13:06if we try and run the code we're going
  8429. 5:13:07to get an issue let's say you were
  8430. 5:13:09creating some sort of system that if you
  8431. 5:13:12entered both values it uses both values
  8432. 5:13:15but if you only entered one it
  8433. 5:13:16automatically set the second value to
  8434. 5:13:19something else and we can achieve this
  8435. 5:13:21Behavior using optional parameters so an
  8436. 5:13:24optional parameter doesn't exactly mean
  8437. 5:13:26quite optional it kind of just means
  8438. 5:13:28that it's going to be optional but you
  8439. 5:13:30can also give it a default value this
  8440. 5:13:32will always add two numbers A and B even
  8441. 5:13:35if we make a or b optional it's going to
  8442. 5:13:37still need two numbers to add in order
  8443. 5:13:40for the function to run so in order to
  8444. 5:13:41make a parameter optional let's just say
  8445. 5:13:44we want to make B optional we can type
  8446. 5:13:46in it equals straight after it and
  8447. 5:13:48actually give it a value so as you can
  8448. 5:13:49see our syntax error is now gone and if
  8449. 5:13:52you run the code we should get
  8450. 5:13:5455 and the reason why this works is a is
  8451. 5:13:57now five if you hover over here we can
  8452. 5:13:59see a is five and then the square
  8453. 5:14:02bracket around the int B actually
  8454. 5:14:04denotes that it's optional and it says
  8455. 5:14:06int b equals 50 cuz we have that h here
  8456. 5:14:09so it's actually using this as 50 and if
  8457. 5:14:12we don't specify the second parameter
  8458. 5:14:14then it's practically just doing this
  8459. 5:14:16which is completely fine so the reason
  8460. 5:14:18why I said it's not quite optional is
  8461. 5:14:20because you still need to give it a
  8462. 5:14:21value of course in here you can use the
  8463. 5:14:23default keyword in the case of an
  8464. 5:14:25integer the default value is actually
  8465. 5:14:27zero so if you just run that code now
  8466. 5:14:29it's going to say 5 + 0 which is just
  8467. 5:14:32five so it can be optional but you still
  8468. 5:14:34need to give it some sort of value of
  8469. 5:14:36course if you're doing a calculator and
  8470. 5:14:38you're adding to values if you only
  8471. 5:14:40specify the first one then adding zero
  8472. 5:14:42to it is simply not going to change it
  8473. 5:14:44so zero or default is going to be the
  8474. 5:14:46best user case when you're dealing with
  8475. 5:14:48default values with integers so another
  8476. 5:14:50way you can actually specify this is by
  8477. 5:14:52inserting this thing called an attribute
  8478. 5:14:54if you remove this equal zero and just
  8479. 5:14:57before the end we can type in Open
  8480. 5:14:59Bracket close bracket and type in
  8481. 5:15:00optional with the capital O and then
  8482. 5:15:03you're going to get a red underline if
  8483. 5:15:05you hover over it it says we need to
  8484. 5:15:07include this extra Library which which
  8485. 5:15:09will import it at the top of our code as
  8486. 5:15:11you can see here now that this is
  8487. 5:15:13available it says indicates that a
  8488. 5:15:15parameter is optional and what this will
  8489. 5:15:17do by default is it'll give you the
  8490. 5:15:19basic value for this number so what I
  8491. 5:15:21mean by that is we can type in here just
  8492. 5:15:23to show you this b equals and then B and
  8493. 5:15:26this will just give you the default
  8494. 5:15:28value based on the data type and as we
  8495. 5:15:30just said a minute ago that value is
  8496. 5:15:31actually going to be zero there you go B
  8497. 5:15:34is zero so all it's doing is saying 5 +
  8498. 5:15:370 = 5
  8499. 5:15:40so that's another way of doing it or you
  8500. 5:15:41could just leave it as I said before you
  8501. 5:15:43can say into b equals default or into b
  8502. 5:15:46equals 0 it's completely up to you I
  8503. 5:15:48prefer using the default keyword so you
  8504. 5:15:50don't have to keep remembering which
  8505. 5:15:52different data types have the default
  8506. 5:15:53values in the inter just case it's zero
  8507. 5:15:56but I just like leaving the default and
  8508. 5:15:57we can get rid of this line now let's
  8509. 5:15:59have a look at different situations
  8510. 5:16:01where this will be helpful if you had a
  8511. 5:16:02function called print name and we add a
  8512. 5:16:05string name in here and then we can
  8513. 5:16:07simply just say console right line my
  8514. 5:16:10name is and then name this is quite easy
  8515. 5:16:13to use so we can say print name and then
  8516. 5:16:16Place AB inside the brackets and then
  8517. 5:16:18run the code and we get abber printed to
  8518. 5:16:20the screen and that's fine let's say if
  8519. 5:16:22we had someone else's name in there as
  8520. 5:16:25Joe for example then we get Joe to the
  8521. 5:16:26screen and let's just say as the
  8522. 5:16:28developer you only want to default this
  8523. 5:16:30to abber if you don't actually insert
  8524. 5:16:32anything so now if you don't insert
  8525. 5:16:34anything of course we're going to get a
  8526. 5:16:35compile error because we need a
  8527. 5:16:36parameter but if we assume what we did
  8528. 5:16:39here and just follow the same approach
  8529. 5:16:41if we use default then it's going to
  8530. 5:16:43give us nothing because the default
  8531. 5:16:45value for a string is just empty quotes
  8532. 5:16:47which is just nothing but instead of
  8533. 5:16:49default we can actually specify value so
  8534. 5:16:52we can say if no one types their name
  8535. 5:16:54inside the bracket then use
  8536. 5:16:57abber there we go so you can have this
  8537. 5:17:00optional parameter that defaults to
  8538. 5:17:02something and if someone types in jaw or
  8539. 5:17:04whatever inside the print name function
  8540. 5:17:06then the default optional parameter is
  8541. 5:17:07not going to be used since this will be
  8542. 5:17:09overridden if you have a parameter
  8543. 5:17:11placed inside here so now the ab is
  8544. 5:17:13going to get overridden by the jaw
  8545. 5:17:15because this is actually the parameter
  8546. 5:17:17that's been used in the
  8547. 5:17:19function so you've looked at functions
  8548. 5:17:21with normal parameters optional
  8549. 5:17:23parameters and now let's take a look at
  8550. 5:17:25name parameters so let's say you had a
  8551. 5:17:26situation where you had string name
  8552. 5:17:28abber in age equal 23 and string address
  8553. 5:17:32equals one something rad and then you
  8554. 5:17:35had a function that would simply print
  8555. 5:17:37these details out to the console so
  8556. 5:17:39static void print details there string
  8557. 5:17:42name inage String
  8558. 5:17:45address so you have three console right
  8559. 5:17:47lines that just print out a name value
  8560. 5:17:49pair just like
  8561. 5:17:51this and then when you go to call this
  8562. 5:17:53function what you're going to have to do
  8563. 5:17:55is specify the name then the age then
  8564. 5:17:57the address in that specific order as
  8565. 5:18:00per the parameter show if you try and
  8566. 5:18:02run the code now we should get the
  8567. 5:18:03desired
  8568. 5:18:05output perfect let's say for example you
  8569. 5:18:07wanted to rearrange the parameters and
  8570. 5:18:09put Age first you'll see we have a
  8571. 5:18:11compiler error now because age and name
  8572. 5:18:13are in the wrong order string name
  8573. 5:18:15should be first and int age to be second
  8574. 5:18:17we can still achieve this by using
  8575. 5:18:19something called name parameters we
  8576. 5:18:21don't have to stick to this order if we
  8577. 5:18:22tell the compiler which parameter we're
  8578. 5:18:24directly talking about so we can say age
  8579. 5:18:27callon because our parameter is now
  8580. 5:18:29called age as you can see it's been
  8581. 5:18:31highlighted and then after the colon
  8582. 5:18:33It's the variables that we're talking
  8583. 5:18:34about so it just happens to be that age
  8584. 5:18:37is the same value as the edge
  8585. 5:18:39so maybe we can change to age input and
  8586. 5:18:42place input the end of these so then we
  8587. 5:18:44can have age equals age input and name
  8588. 5:18:47parameter equals the name input and the
  8589. 5:18:49address parameter equals the address
  8590. 5:18:51input now we can have it like this and
  8591. 5:18:53to make it look even better you can
  8592. 5:18:55actually add new lines in here just to
  8593. 5:18:57break it off like this and this is a
  8594. 5:18:59similar pattern that you'll see in
  8595. 5:19:00normal production code putting a space
  8596. 5:19:02here can separate them out a little bit
  8597. 5:19:04and there we go it doesn't matter which
  8598. 5:19:06order that we put these in right now we
  8599. 5:19:07can even put address first first and
  8600. 5:19:10it'll still work okay and if we run it
  8601. 5:19:12you should still get exactly the same
  8602. 5:19:13output it's just specified in a
  8603. 5:19:15different way so let's just have a
  8604. 5:19:17little recap to make use of name
  8605. 5:19:18parameters you don't need to do anything
  8606. 5:19:20to the original function you just write
  8607. 5:19:22it exactly the way you're used to doing
  8608. 5:19:23you specify the parameters in the order
  8609. 5:19:25that you think is sensible and when you
  8610. 5:19:27go to call it you can simply just type
  8611. 5:19:29in the parameter's name first followed
  8612. 5:19:31by callon and then the variable name
  8613. 5:19:33that you want to address once you've
  8614. 5:19:35done that then you can use name
  8615. 5:19:36parameters you can go back to using the
  8616. 5:19:38normal function Fun by just getting rid
  8617. 5:19:39of the name followed by the colon and
  8618. 5:19:41just replacing all of the parameters
  8619. 5:19:43using the correct order and the original
  8620. 5:19:45variables instead of the new notation
  8621. 5:19:47having Name colon followed by name
  8622. 5:19:51input so far we've covered functions
  8623. 5:19:53with no parameters normal parameters
  8624. 5:19:56optional parameters named parameters and
  8625. 5:19:58let's look at a concept called out
  8626. 5:20:00parameters let's demonstrate a quick
  8627. 5:20:01example so we call a function called
  8628. 5:20:03test and we pass in a value and let's
  8629. 5:20:05just make this work so we can have an
  8630. 5:20:07INT Norm equal zero and then we have a
  8631. 5:20:09test function so we can say static void
  8632. 5:20:12test and we can say in n and inside here
  8633. 5:20:16let's make num the value of five now in
  8634. 5:20:18this example if we print out num to the
  8635. 5:20:20screen what do you think is going to
  8636. 5:20:22happen is num going to take the value of
  8637. 5:20:24five or is you're going to keep the
  8638. 5:20:25value zero I'll give you a second to
  8639. 5:20:27think and let's run the
  8640. 5:20:29code so let's run it and see what
  8641. 5:20:33happens and if you guess five you're
  8642. 5:20:36indeed Incorrect and if you guess zero
  8643. 5:20:38you are correct correct and the reason
  8644. 5:20:40why it's not five is because when you
  8645. 5:20:42pass a num inside this parameter it just
  8646. 5:20:45becomes a parameter and therefore it
  8647. 5:20:47becomes a local variable even though
  8648. 5:20:49these two variables are actually called
  8649. 5:20:51exactly the same thing you'll see that
  8650. 5:20:53when I highlight it it only highlights
  8651. 5:20:55this one and doesn't highlight this one
  8652. 5:20:58and the same is true for this it
  8653. 5:20:59highlights these three references but
  8654. 5:21:01not the ones down here the num variable
  8655. 5:21:03is only accessible in each other scope
  8656. 5:21:06this one is available through here and
  8657. 5:21:08here and this one is available through
  8658. 5:21:10here and here therefore if we use this
  8659. 5:21:13assignment then it's actually doing
  8660. 5:21:14nothing if we print out gnome inside
  8661. 5:21:16here num equals num and then we print
  8662. 5:21:20that we can see that it is assigned the
  8663. 5:21:23value five but because it doesn't do
  8664. 5:21:24anything at the end of this function
  8665. 5:21:26then it simply doesn't get returned back
  8666. 5:21:28or anything happens to it so let's see
  8667. 5:21:30how we can make this in to five when it
  8668. 5:21:32gets printed out here so the options
  8669. 5:21:34that we have is we can actually make
  8670. 5:21:36this viid an integer and we can say num
  8671. 5:21:39equals 5 and then we can return num or
  8672. 5:21:41alternatively we could simply just
  8673. 5:21:43return five and not do both of these
  8674. 5:21:44lines so if we run that now then we
  8675. 5:21:47still get zero and the reason why we get
  8676. 5:21:49zero is the test function is being
  8677. 5:21:51called and it returns back an integer
  8678. 5:21:54but we're simply not doing anything with
  8679. 5:21:55it so we can say num equals the return
  8680. 5:21:58value of this function which will be
  8681. 5:22:00five and now if we run it we should see
  8682. 5:22:02how five perfect now this is a little
  8683. 5:22:05bit cumbersome and let's say you wanted
  8684. 5:22:07to return a value and return back
  8685. 5:22:09another value at the same time than what
  8686. 5:22:10are your options so let me show you a
  8687. 5:22:12quick example when you're using int.
  8688. 5:22:14trass we have a string s an out int
  8689. 5:22:18result and a Bool so what this function
  8690. 5:22:20is effectively doing is return you back
  8691. 5:22:23a true or false value to dictate whether
  8692. 5:22:25or not the conversion has been
  8693. 5:22:27successful and it's also returned you an
  8694. 5:22:29out in result and what this means is you
  8695. 5:22:31can actually specify integer variable
  8696. 5:22:34straight inside the brackets so in
  8697. 5:22:35essence this function has gave you back
  8698. 5:22:38two values it's give you back something
  8699. 5:22:40from the integer parameter and it's also
  8700. 5:22:42returned back a Boolean variable so
  8701. 5:22:44let's convert this to code and see what
  8702. 5:22:46the third option is of trying to return
  8703. 5:22:48back five over to this so if we just
  8704. 5:22:51comment out this line and as you saw in
  8705. 5:22:53the trip pass it simply just need to put
  8706. 5:22:55the word out just before
  8707. 5:22:57here out int num now we don't actually
  8708. 5:23:02need to return anything in this specific
  8709. 5:23:03example because we want to just change a
  8710. 5:23:05num variable using the out parameter but
  8711. 5:23:09if we wanted to return something
  8712. 5:23:10separate to the num like a boing like
  8713. 5:23:12the tri pass did then we have that
  8714. 5:23:14option so we can return back true but
  8715. 5:23:16just before we return back true as
  8716. 5:23:18there's a red underline here it will say
  8717. 5:23:20that the out parameter num must be
  8718. 5:23:22assigned so we need to give num a
  8719. 5:23:24value we give it the value five because
  8720. 5:23:27what we're actually doing here even if
  8721. 5:23:29we double click on this we'll still get
  8722. 5:23:31num being selected here cuz the scope is
  8723. 5:23:34still exactly the same now except what's
  8724. 5:23:36happening is when this variable gets
  8725. 5:23:38returned it actually gets put into the
  8726. 5:23:39variable that it was initially called
  8727. 5:23:41with so let's fix these compiler errors
  8728. 5:23:43first so the reason why it's complaining
  8729. 5:23:45is because we need to give it the out
  8730. 5:23:46keyword so when we're specifying in Tri
  8731. 5:23:49pass our first parameter is a string so
  8732. 5:23:51if the user types in 1 2 3 we put in the
  8733. 5:23:53first parameter and then we'd say out in
  8734. 5:23:56result or something along them lines you
  8735. 5:23:58have an out and then you say you want to
  8736. 5:24:00make a new integer and you want to call
  8737. 5:24:02it result see in our case we don't need
  8738. 5:24:04to put int just like in here because
  8739. 5:24:06we've already created our num so the
  8740. 5:24:08only keyword we need is how to follow by
  8741. 5:24:10space and now of course we're trying to
  8742. 5:24:12assign this back into an integer which
  8743. 5:24:15we don't need anymore because this is a
  8744. 5:24:16booing so we can say Bo
  8745. 5:24:19success equals test out n so we print
  8746. 5:24:22out
  8747. 5:24:24Success Print them both the screen now
  8748. 5:24:26we should have five and true the true
  8749. 5:24:28comes from the return true and the five
  8750. 5:24:30comes from num equals 5 so what's
  8751. 5:24:33actually happening here is we're
  8752. 5:24:34creating our new integer we're passing
  8753. 5:24:36it into this function we're also telling
  8754. 5:24:38it's going to be an out parameter so
  8755. 5:24:40this function can still output things to
  8756. 5:24:42our number variable and then when it
  8757. 5:24:44gets placed inside here we assign num
  8758. 5:24:46equal to five and then just because we
  8759. 5:24:48have a bull we have to return true or we
  8760. 5:24:50can return false of course we can just
  8761. 5:24:53take this out completely take away the
  8762. 5:24:55return true and just keep this as
  8763. 5:24:57test of course in both scenarios these
  8764. 5:25:00aren't actually do anything they're just
  8765. 5:25:01completely random values it's just made
  8766. 5:25:03for demonstration so bu is returned back
  8767. 5:25:06true or false doesn't actually mean
  8768. 5:25:07anything I was just demonstrating that
  8769. 5:25:09you can have two return values from one
  8770. 5:25:11function there we go we get five when we
  8771. 5:25:13run it just like this so now I've
  8772. 5:25:15completed this little example let's have
  8773. 5:25:17a look at how it's used in the real
  8774. 5:25:18world so before we continue let's see if
  8775. 5:25:20we can rewrite the tripal function in
  8776. 5:25:23our code so it's going to be a static
  8777. 5:25:25returns back a bu we have a triy pass
  8778. 5:25:27our first parameter is a string s and
  8779. 5:25:29our second parameter is out into result
  8780. 5:25:32and inside there we can just do result
  8781. 5:25:35equals to zero and then we can return
  8782. 5:25:37back true of course we actually need to
  8783. 5:25:38do conversions inside here if you want
  8784. 5:25:40this function to work but I'm just
  8785. 5:25:42joining you for demonstration purpose if
  8786. 5:25:44we uncomment this line out and have a
  8787. 5:25:46look at the parameters again you'll see
  8788. 5:25:47that now they line up perfectly string s
  8789. 5:25:50out in result and it returns back a Bo
  8790. 5:25:52which we have exactly the same here
  8791. 5:25:54there you go Bo in. trass string s out
  8792. 5:25:57in result so that's how this function
  8793. 5:25:59has been declared of course they're
  8794. 5:26:01doing something inside the body that can
  8795. 5:26:03take in the string convert it to an
  8796. 5:26:04integer place it inside result and then
  8797. 5:26:07return whether or not the function has
  8798. 5:26:08been executed successfully so now that
  8799. 5:26:11we understand how to make an example and
  8800. 5:26:13how to make the trip pass function
  8801. 5:26:14ourselves and to give you the
  8802. 5:26:15Declaration let's have a look at a real
  8803. 5:26:17world example so if we had a list of
  8804. 5:26:20string and we had our shopping list that
  8805. 5:26:22was used from the examples we can have
  8806. 5:26:24list string and let's just create our
  8807. 5:26:27own initializer just with coffee and
  8808. 5:26:28milk
  8809. 5:26:29inside there we go so now that we've
  8810. 5:26:31created that we've got our shopping list
  8811. 5:26:33if you wanted to find something inside
  8812. 5:26:35the shopping list we have the function
  8813. 5:26:36index of and what that will do as it
  8814. 5:26:38says here searches for the specified
  8815. 5:26:40object and Returns the zero based index
  8816. 5:26:43of the first occurrence so if we were
  8817. 5:26:45search for coffee then we should get
  8818. 5:26:47return back zero so let's try and run
  8819. 5:26:50this and put it in the console right
  8820. 5:26:51line and see what the value is there we
  8821. 5:26:53go we get zero since it's the first item
  8822. 5:26:56and if we place milk inside here we
  8823. 5:26:57should get back one there we go so let's
  8824. 5:27:01try and rewrite this function into our
  8825. 5:27:03own code so all this will do is loop
  8826. 5:27:06around all of the positions until it
  8827. 5:27:07finds it and it'll find the first one
  8828. 5:27:10and it'll return us back the index so in
  8829. 5:27:12this case there's no out parameters but
  8830. 5:27:14there is a return value so we can also
  8831. 5:27:16include an out parameter and then
  8832. 5:27:19instead of assigning the value of minus
  8833. 5:27:20one we can actually assign the value of
  8834. 5:27:22true or false to dictate whether or not
  8835. 5:27:25that we found it so let's write the code
  8836. 5:27:26normally in here and then let's wrap it
  8837. 5:27:28around in a function so we can say int
  8838. 5:27:30index equal minus1 because we want it to
  8839. 5:27:32start a value that we know is incorrect
  8840. 5:27:35since the lists are from zero onwards
  8841. 5:27:37the value is only ever minus one then
  8842. 5:27:40that means it's not in the list then we
  8843. 5:27:41can then create a for Loop intire equal
  8844. 5:27:43Z shopping list do count because we want
  8845. 5:27:46to go through every single item inside
  8846. 5:27:48the shopping list and inside here we
  8847. 5:27:50want to do a lowercase comparison so we
  8848. 5:27:52can do if the shopping list square
  8849. 5:27:54bracket I do to
  8850. 5:27:56lower do equals and then we want to make
  8851. 5:27:59that equal to just something that we're
  8852. 5:28:01going to type in statically so we can
  8853. 5:28:03just say coffee we can make this into a
  8854. 5:28:06parameter after but this is just for
  8855. 5:28:07demonstration purposes so we have a
  8856. 5:28:09bracket and if we found that match then
  8857. 5:28:11we can say index equals to I cuz the
  8858. 5:28:14index of the position that we want to
  8859. 5:28:15find must be up to I cuz that's the one
  8860. 5:28:17that we've made a match with and we
  8861. 5:28:19don't need an else inside here we don't
  8862. 5:28:22need to set an else inside here to make
  8863. 5:28:24it equal to minus one because if it
  8864. 5:28:25simply doesn't find it then the index
  8865. 5:28:27variable won't change its value so it
  8866. 5:28:29will always stay minus one if it doesn't
  8867. 5:28:31find anything so after the followup is
  8868. 5:28:33run let's just print index out to the
  8869. 5:28:36screen there we go we have zero and if
  8870. 5:28:39we change this over to milk run it again
  8871. 5:28:41then we actually have one placed inside
  8872. 5:28:44here perfect so now what our function is
  8873. 5:28:47doing is giving us the values of the
  8874. 5:28:49index if it finds it now if you type in
  8875. 5:28:51something else if you just add another
  8876. 5:28:53can let's see what happens then we get
  8877. 5:28:55minus one and minus one means that it's
  8878. 5:28:57incorrect and it hasn't found it and
  8879. 5:29:00what we can do inside here we can say if
  8880. 5:29:02the index is greater than minus1 then we
  8881. 5:29:05can say found or else we can say not
  8882. 5:29:08found so let's run the code now and see
  8883. 5:29:10if we get a visual representation not
  8884. 5:29:13found and if we changes back to milk run
  8885. 5:29:15it again then we get found perfect so as
  8886. 5:29:19you can see here we're actually checking
  8887. 5:29:20if the value is greater than minus one
  8888. 5:29:22but it'd be easier if you had a variable
  8889. 5:29:24called found and it actually a booing
  8890. 5:29:26variable so we can make a boo found and
  8891. 5:29:28make that equal to an if statement
  8892. 5:29:31condition so we can say something like
  8893. 5:29:32index is greater than minus one which is
  8894. 5:29:35exactly what we had here a second ago
  8895. 5:29:38and because this is a condition it's
  8896. 5:29:39either going to return back true or
  8897. 5:29:41false and it's going to place the value
  8898. 5:29:43inside found and then we're going to use
  8899. 5:29:45found inside here this is easier logic
  8900. 5:29:47to understand if this is true then it's
  8901. 5:29:50found if this is not true then it's not
  8902. 5:29:52found true true false false it's quite
  8903. 5:29:56really straightforward and very easy and
  8904. 5:29:58clear you have an index and you have
  8905. 5:30:00whether or not it's been found so let's
  8906. 5:30:02try and wrap this around inside a
  8907. 5:30:03function so just as the trip pass does
  8908. 5:30:06it returns us back with a Bool which in
  8909. 5:30:08this case will be our found and it also
  8910. 5:30:10gives us back an integer result but in
  8911. 5:30:12this case it's going to be our index so
  8912. 5:30:14let's see what we can do to write this
  8913. 5:30:16so we can have a static we want to
  8914. 5:30:17return back the Bo and we can say find
  8915. 5:30:20in list we need to give it the item
  8916. 5:30:23we're going to find so we're going to
  8917. 5:30:25need a string s we need a string s for
  8918. 5:30:27the item that we're going to find we
  8919. 5:30:29need to give it the list as well and
  8920. 5:30:31then finally we need our out in
  8921. 5:30:34index there we go so let's comment out
  8922. 5:30:37this code here
  8923. 5:30:40and let's copy this down into our
  8924. 5:30:42function and see what we need to do so
  8925. 5:30:44index has the value of minus one we
  8926. 5:30:46don't need the integer variable
  8927. 5:30:48declaration here because we've already
  8928. 5:30:49declared it inside the parameter so
  8929. 5:30:51after declaring inside here we give it
  8930. 5:30:53the value of minus one minus one
  8931. 5:30:55indicates it hasn't been found since the
  8932. 5:30:57index are zero based so instead of
  8933. 5:31:00shopping list we now just have list and
  8934. 5:31:03list here as well and then instead of
  8935. 5:31:05having it being equal to milk we
  8936. 5:31:07actually want to use our s variable and
  8937. 5:31:09to make it better we can convert it to
  8938. 5:31:10lower case just so we have lowercase
  8939. 5:31:12conversions on both sides of the equals
  8940. 5:31:15if we find a match between the list that
  8941. 5:31:16we've passed in and the S variable which
  8942. 5:31:18is our surge term that we've passed in
  8943. 5:31:20then we assign I to the value of index
  8944. 5:31:23and then it's still got a red underline
  8945. 5:31:25because we haven't returned back
  8946. 5:31:27bu so right now we actually want to
  8947. 5:31:29return back the found variable so we
  8948. 5:31:30have two options here we can return back
  8949. 5:31:32found or we can simply take this and
  8950. 5:31:35return it back in here we don't need to
  8951. 5:31:37run this code to see if it's found or
  8952. 5:31:38not and then store it and then return it
  8953. 5:31:40after since we're not doing anything
  8954. 5:31:42else with it we could just return it
  8955. 5:31:43like this this makes the code
  8956. 5:31:44considerably shorter because this will
  8957. 5:31:46just return back a true or false if the
  8958. 5:31:49index is greater than minus1 then it
  8959. 5:31:50will turn back true if the index is less
  8960. 5:31:52than minus1 then it will turn back false
  8961. 5:31:55so let's see how we can use this
  8962. 5:31:56function inside our code now so we
  8963. 5:31:58wanted to search for milk so let's just
  8964. 5:32:00copy and paste this line and let's see
  8965. 5:32:01if we get the same output as the
  8966. 5:32:03shopping list index of so now our
  8967. 5:32:05function is called finding list you have
  8968. 5:32:08a bracket and then our string s is going
  8969. 5:32:09to be our milk our list is our shopping
  8970. 5:32:12list and our out in
  8971. 5:32:15index so this is going to return us back
  8972. 5:32:18with a booing so we can print that
  8973. 5:32:19booing to the screen and then if we just
  8974. 5:32:21duplicate that down and we can print
  8975. 5:32:23index to the screen and maybe using a
  8976. 5:32:25console right line and just place a
  8977. 5:32:27space right at the
  8978. 5:32:29start so you print out whether it's
  8979. 5:32:31successfully converted or not and we get
  8980. 5:32:33the value back from
  8981. 5:32:35it let's run the code
  8982. 5:32:39so we get true plus the value of one so
  8983. 5:32:42that means it's found it and it's found
  8984. 5:32:43it at one index and the previous line
  8985. 5:32:46before it which just run the normal
  8986. 5:32:47index of function also return back one
  8987. 5:32:50so they're both the same let's just get
  8988. 5:32:51rid of that
  8989. 5:32:56space so if we change these both to
  8990. 5:32:59coffee then hopefully we should see the
  8991. 5:33:01same result but with index zero there we
  8992. 5:33:03go this zero comes from the index
  8993. 5:33:05function this true is from our function
  8994. 5:33:07tell us has been successful and this
  8995. 5:33:09zero is the index so we can actually
  8996. 5:33:11take this and put this straight into an
  8997. 5:33:13if
  8998. 5:33:14statement so just like we had down here
  8999. 5:33:16with the found and not found we can say
  9000. 5:33:18if and then this function call and then
  9001. 5:33:21we can say found and then the item what
  9002. 5:33:24we passed in so in this case it's going
  9003. 5:33:25to be coffee found
  9004. 5:33:27coffee found coffee at index index and
  9005. 5:33:31if it hasn't been found then we can just
  9006. 5:33:33print not
  9007. 5:33:35found and this is getting an error
  9008. 5:33:37because we have two indexes up here so
  9009. 5:33:39let's comment out this line and let's
  9010. 5:33:41try running
  9011. 5:33:42out found coffee index zero and you can
  9012. 5:33:45see these two are the same again one
  9013. 5:33:47comes from here and one comes from that
  9014. 5:33:48index we change these both to milk now
  9015. 5:33:52milk milk and milk then run the code and
  9016. 5:33:55you see found milk index one if you
  9017. 5:33:58wanted to just before you pass in here
  9018. 5:34:00you can ask the
  9019. 5:34:02user enter an item to
  9020. 5:34:06search store it inside string search
  9021. 5:34:09equals console.
  9022. 5:34:12readline and then instead of passing mil
  9023. 5:34:14here we can just pass in Search and then
  9024. 5:34:16instead of passing milk in here we can
  9025. 5:34:18also use the search variable so now if
  9026. 5:34:20we try run it again we can type in milk
  9027. 5:34:22and then we go file milk index one and
  9028. 5:34:24then coffee found coffee index zero and
  9029. 5:34:26just to give this a test let's just try
  9030. 5:34:28and find m not
  9031. 5:34:31found
  9032. 5:34:33perfect so let's have a little recap
  9033. 5:34:35based on this function so initially we
  9034. 5:34:37try to assign the value of zero and then
  9035. 5:34:39print out a normal function and inside
  9036. 5:34:41test we just had a simple num equals 5
  9037. 5:34:45and what this was doing inside is just
  9038. 5:34:46locally assigning it but not actually
  9039. 5:34:48return anything and then we said we
  9040. 5:34:50could assign it and then return it back
  9041. 5:34:52and that was another option of being
  9042. 5:34:53able to assign the value to five and
  9043. 5:34:55then we use the out parameter which
  9044. 5:34:57allowed you to pass in an integer when
  9045. 5:34:59you assign in here it will pass it back
  9046. 5:35:01into the original call and place it in
  9047. 5:35:03this variable here so when we print out
  9048. 5:35:05on the next line it says it's equal to
  9049. 5:35:07five we wrote our own little shopping
  9050. 5:35:09list for the list of strings and then we
  9051. 5:35:11called the shopping list function index
  9052. 5:35:13of and then we actually created our own
  9053. 5:35:15index of function and made it into a
  9054. 5:35:18function so just a little recap what's
  9055. 5:35:20going on here we have a string s which
  9056. 5:35:22is our search term the list full of the
  9057. 5:35:24items in the index that we're going to
  9058. 5:35:26pass back to the user as well as
  9059. 5:35:27returning back a bu to indicate whether
  9060. 5:35:29we found it or not we set the index to
  9061. 5:35:32minus one so then that completes our
  9062. 5:35:34logic at the bottom minus one means it
  9063. 5:35:37hasn't been found found since the list
  9064. 5:35:38is all zero based if this value is
  9065. 5:35:41greater than minus1 then that means that
  9066. 5:35:44we found it inside the list since the
  9067. 5:35:46list is zero based and if it's not that
  9068. 5:35:48and this returns back false then we
  9069. 5:35:50mustn't have found it because this line
  9070. 5:35:52would have executed so we Loop through
  9071. 5:35:54the list we do a lowercase comparison on
  9072. 5:35:57both sides of the equation on the list
  9073. 5:35:58item and on the surge term if they both
  9074. 5:36:01match we run the index and store the I
  9075. 5:36:04variable inside the index and then we
  9076. 5:36:06return whether index is greater than
  9077. 5:36:07minus one so after we return this back
  9078. 5:36:09from the end of the function we can see
  9079. 5:36:11once we run it inside the console right
  9080. 5:36:13Line This is just going to return us
  9081. 5:36:14back with a true or false value as well
  9082. 5:36:16as creating our new integer variable
  9083. 5:36:18called index so on the next line we can
  9084. 5:36:20use index and print out of the screen
  9085. 5:36:22this line should return true or false
  9086. 5:36:24since it's a Boolean return and this
  9087. 5:36:26variable will either give us minus one
  9088. 5:36:28or something above minus one but in this
  9089. 5:36:30case when we actually make use of the
  9090. 5:36:31function we can ask a user to enter
  9091. 5:36:33something get them to type it in and
  9092. 5:36:36pass that into the function but more
  9093. 5:36:38importantly when this function returns
  9094. 5:36:40if it's true we output it's been found
  9095. 5:36:42and if it's false we output not found
  9096. 5:36:45and we only make use of this integer
  9097. 5:36:47variable should this return back true
  9098. 5:36:49there is no point in printing minus one
  9099. 5:36:52to this screen because that's what
  9100. 5:36:53you're going to get if it's not
  9101. 5:36:55found it's going to look something like
  9102. 5:36:57this which is kind of redundant because
  9103. 5:36:59the user doesn't care about this so we
  9104. 5:37:01don't need to include it when we're
  9105. 5:37:02printing not found but we should include
  9106. 5:37:04it when we're printing out the found of
  9107. 5:37:06course this is just a random example Le
  9108. 5:37:07and you might not need to print it out
  9109. 5:37:09to the screen or you might just do
  9110. 5:37:10something else inside here you might be
  9111. 5:37:12searching for something and then you
  9112. 5:37:14might want to edit it or do something
  9113. 5:37:15else with it and then you can make use
  9114. 5:37:17of that index if you wanted to ask the
  9115. 5:37:19user to search for an item and then tell
  9116. 5:37:21them what they want to replace it with
  9117. 5:37:23then you can also do that just as we've
  9118. 5:37:25covered in the last
  9119. 5:37:28examples so after we've covered out
  9120. 5:37:30parameters and normal parameters let's
  9121. 5:37:32have a look at a concept called passing
  9122. 5:37:34by reference so we created inome equals
  9123. 5:37:3610 and then let's call it assign and we
  9124. 5:37:38passed it straight into here and we can
  9125. 5:37:41have a static void assign in
  9126. 5:37:45N if we say n equals 20 if we use a
  9127. 5:37:48console right line up here then num is
  9128. 5:37:50still going to be 10 because we're not
  9129. 5:37:53returning it and we're not really
  9130. 5:37:54affecting it now other ways we can do
  9131. 5:37:57this is by changing this into an INT and
  9132. 5:37:59then we can say return 20 and then we
  9133. 5:38:02don't really need this anymore so we
  9134. 5:38:04could just say
  9135. 5:38:05num num equals a sign and then we'll get
  9136. 5:38:0820 assign back in there we go all we can
  9137. 5:38:11actually make this an out into num and
  9138. 5:38:14then we can say num equals to
  9139. 5:38:1720 and then inside here we can say out
  9140. 5:38:20num we don't need to return back in
  9141. 5:38:22anymore so we can make this valued and
  9142. 5:38:24you see the value 20 so there are
  9143. 5:38:26different ways that we can assign a
  9144. 5:38:27variable straight from here so let's
  9145. 5:38:29have a look at another method so instead
  9146. 5:38:31of having out we can actually use a new
  9147. 5:38:33keyw called ref and the reason why it's
  9148. 5:38:35ref it means it's passing it by
  9149. 5:38:37reference so when you pass a value in
  9150. 5:38:38through a parameter normally you're
  9151. 5:38:40actually passing by value and what that
  9152. 5:38:42means is you're copying this value that
  9153. 5:38:44gets passed inside the brackets into
  9154. 5:38:47here and it's making a whole new
  9155. 5:38:48variable what I mean by that is this
  9156. 5:38:51might have memory dress location one and
  9157. 5:38:53then when this variable gets created it
  9158. 5:38:55has memory dress location 2 for example
  9159. 5:38:57and then it's used within here and then
  9160. 5:38:59two is now deleted because you've got
  9161. 5:39:01rid of the variable because of this
  9162. 5:39:02function but one is still alive since in
  9163. 5:39:05the main function when you pass by
  9164. 5:39:06reference what you're doing is you're
  9165. 5:39:08saying give me the memory address
  9166. 5:39:10location of the variable inside the
  9167. 5:39:11brackets and give it into this function
  9168. 5:39:14rather than copying the value so if this
  9169. 5:39:15has memory just location one and you
  9170. 5:39:17pass it into here then it's also going
  9171. 5:39:19to have one so what this actually means
  9172. 5:39:21is if we put this ref keyword just
  9173. 5:39:23before the int just like we do with the
  9174. 5:39:25out and we can place it here as well
  9175. 5:39:27we're now saying we want to pass this by
  9176. 5:39:29reference so when this 10 gets pass in
  9177. 5:39:31here we actually have a link if you will
  9178. 5:39:34to this gome is now linked to this gome
  9179. 5:39:37which means if we simply do nor equals
  9180. 5:39:3920 and run the code then we're going to
  9181. 5:39:41see 20 the main differences between
  9182. 5:39:44reference and using the out keyword is
  9183. 5:39:46when you're using out within a function
  9184. 5:39:48you actually need to make use of the
  9185. 5:39:50value if I don't include anything within
  9186. 5:39:52here then I'm going to get exception and
  9187. 5:39:54it says the out parameter num must be
  9188. 5:39:56assigned so when using the out keyword
  9189. 5:39:59num has to be assigned when using the
  9190. 5:40:02ref keyword you don't have to make any
  9191. 5:40:03assignment because we're just passing a
  9192. 5:40:05reference in with the out keyword word
  9193. 5:40:07you literally tell the function I want
  9194. 5:40:09to Output this value but if you haven't
  9195. 5:40:11gave it a value to Output then of course
  9196. 5:40:13it's going to be an error so we can
  9197. 5:40:15assign in the reference if we really
  9198. 5:40:17want to but you don't have to and it's
  9199. 5:40:19not required so there are the two main
  9200. 5:40:20differences between passing by reference
  9201. 5:40:22and passing by value and also using the
  9202. 5:40:25out parameter passing by reference is
  9203. 5:40:27technically quicker because of
  9204. 5:40:28efficiency when you don't pass it by
  9205. 5:40:30reference you pass it by value which
  9206. 5:40:32means it'll essentially place that 10
  9207. 5:40:34inside these
  9208. 5:40:35brackets instead of passing the variable
  9209. 5:40:38in it actually just passes the value and
  9210. 5:40:40then it makes a whole new variable down
  9211. 5:40:42here when you pass it in by reference
  9212. 5:40:44you're essentially actually giving it a
  9213. 5:40:46link to this memory address location
  9214. 5:40:48from inside this function which means
  9215. 5:40:50that you don't actually have another
  9216. 5:40:52local variable inside here because this
  9217. 5:40:54one is affecting this one directly you
  9218. 5:40:57can do this with any amount of
  9219. 5:40:58references you want by simply just
  9220. 5:41:00chaining them and adding another one in
  9221. 5:41:02like that for example and then we can
  9222. 5:41:04have a string name and then we can pass
  9223. 5:41:07our ref name inside here and inside here
  9224. 5:41:10we can say name equals hour and this
  9225. 5:41:13doesn't like it if it's unassigned so we
  9226. 5:41:15just give it empty quotes or we can sign
  9227. 5:41:17it another name it doesn't really matter
  9228. 5:41:19it's just a demonstration and if you run
  9229. 5:41:21that but let's just print out the name
  9230. 5:41:23as well to the
  9231. 5:41:24screen there we go we get abber assigned
  9232. 5:41:27into
  9233. 5:41:28there and same thing with this variable
  9234. 5:41:30is we're actually just passing in the
  9235. 5:41:32variable as a reference rather than as a
  9236. 5:41:34value it doesn't matter what the value
  9237. 5:41:36is going to be B cuz it's just given
  9238. 5:41:38this variable inside this function so
  9239. 5:41:40we're actually saying name equals we
  9240. 5:41:42basically just assign it back into the
  9241. 5:41:44original variable so let's just have a
  9242. 5:41:45look at another example so we can have
  9243. 5:41:47some sort of like change name function
  9244. 5:41:49and we can give it the name variable as
  9245. 5:41:51a reference and then we can give it the
  9246. 5:41:53new name that we're going to pass in in
  9247. 5:41:54a second so if we make a new function
  9248. 5:41:56static void change name you're going to
  9249. 5:41:58need a ref string name and a string new
  9250. 5:42:01name and we can simply say in here name
  9251. 5:42:04equals new
  9252. 5:42:05name perfect
  9253. 5:42:07so let's give this a shot so if we just
  9254. 5:42:09comment out this little section and we
  9255. 5:42:11can say console right enter your new
  9256. 5:42:14name then we have a string new name
  9257. 5:42:17equals console.
  9258. 5:42:20readline and then just up here as we've
  9259. 5:42:22done before we could just declare the
  9260. 5:42:23name as being Joe and then we enter the
  9261. 5:42:26new name call this function and then
  9262. 5:42:28after we can say your new name is and
  9263. 5:42:32then we can pass in name perfect let's
  9264. 5:42:34run
  9265. 5:42:35that so at this point the name name is
  9266. 5:42:37actually Joe cuz we've made it appear
  9267. 5:42:39and then we'll enter a new name so we
  9268. 5:42:40can type in abber now it says your new
  9269. 5:42:43name is abber perfect so all this
  9270. 5:42:45function is doing I know it's pretty
  9271. 5:42:47pointless right now cuz it's just doing
  9272. 5:42:48one line but all it's doing is assigning
  9273. 5:42:51the reference to the variable that we
  9274. 5:42:52pass in so what we can do now is make
  9275. 5:42:54sure that we can actually assign this
  9276. 5:42:56variable in and we can also pass in a
  9277. 5:42:58Boolean variable so what we should do
  9278. 5:43:00now is actually make sure that we can do
  9279. 5:43:02the assignment properly cuz you don't
  9280. 5:43:03want to assign it if the value is empty
  9281. 5:43:05and then return back a Bo Lan type just
  9282. 5:43:08to indicate whether or not it's being
  9283. 5:43:09assigned so what we can do in here is
  9284. 5:43:11just on the change name we can wrap this
  9285. 5:43:13around an if statement and if this
  9286. 5:43:15returns back true then your name has
  9287. 5:43:17been changed and if not we can say
  9288. 5:43:20console right Line new name cannot be
  9289. 5:43:22empty or
  9290. 5:43:24no in order to be able to do this check
  9291. 5:43:27as we've used before we could say if new
  9292. 5:43:30name and then just around that we can
  9293. 5:43:32say string do is n or empty and wrap
  9294. 5:43:34that inside the brackets and we want to
  9295. 5:43:36say if it's not and the L empty then we
  9296. 5:43:38want to do our
  9297. 5:43:40assignment after we've done our
  9298. 5:43:42assignment we can return back true
  9299. 5:43:43inside here and then just outside we can
  9300. 5:43:46return back false notice how in this
  9301. 5:43:48little bit of code we actually don't
  9302. 5:43:49have an else we don't need to write else
  9303. 5:43:52inside here and place return false
  9304. 5:43:55inside here because if this returns back
  9305. 5:43:57true then it's going to execute this one
  9306. 5:43:59line return back true and since it's a
  9307. 5:44:02return it's going to jump to the end of
  9308. 5:44:03the function so we don't need the else
  9309. 5:44:05in there because the only way that this
  9310. 5:44:07line will ever execute is if we actually
  9311. 5:44:09had this function returning back false
  9312. 5:44:12so if this returns back true it's going
  9313. 5:44:13to hit this line hit the return jump to
  9314. 5:44:16the end if this returns back false it's
  9315. 5:44:18going to skip over the if statement and
  9316. 5:44:19then just return back false to the user
  9317. 5:44:21perfect so now we've got no compile
  9318. 5:44:22errors let's give it a run so if we want
  9319. 5:44:25to change your name to nothing if we
  9320. 5:44:27just press enter then we can see it says
  9321. 5:44:29new name cannot be empty or no and if we
  9322. 5:44:31type something in then we get our name
  9323. 5:44:33changed so just to round up this video
  9324. 5:44:35let's talk about the main differences
  9325. 5:44:37between the ref parameter and the out
  9326. 5:44:38parameter when you sign in by reference
  9327. 5:44:40you actually have to make the variable
  9328. 5:44:42before you pass it in for example you
  9329. 5:44:44can't just write ref string in here and
  9330. 5:44:46that actually works whereas you can't
  9331. 5:44:48actually do that with the in let me
  9332. 5:44:50demonstrate so if you had the in. trass
  9333. 5:44:53and that has an out parameter you can
  9334. 5:44:55actually type out int index inside here
  9335. 5:44:58you don't actually have to make the int
  9336. 5:45:00index inside here and then specify it
  9337. 5:45:03outside you could just initialize and
  9338. 5:45:05declare the variable straight inside the
  9339. 5:45:07brackets whereas because you're passing
  9340. 5:45:09a reference with the string you actually
  9341. 5:45:11need to make the string first and then
  9342. 5:45:12pass it in so that's a big difference
  9343. 5:45:14between the out and the ref when you
  9344. 5:45:16make news of the actually inside the
  9345. 5:45:18function if you're using a reference
  9346. 5:45:20then you never have to assign it or at
  9347. 5:45:22least it's not required you could have a
  9348. 5:45:24series of if statements inside this body
  9349. 5:45:26and then you might determine that you
  9350. 5:45:27might not need to assign it which is
  9351. 5:45:29exactly what we've got here the only way
  9352. 5:45:31we assign name is if this returns back
  9353. 5:45:33true if it doesn't return back true then
  9354. 5:45:35we simply don't assign it and move to
  9355. 5:45:37here this is a problem if the out
  9356. 5:45:39variable if we change this to an out
  9357. 5:45:41then we're going to get a compiler error
  9358. 5:45:43because it doesn't return back a value
  9359. 5:45:45so we'd have to write something like
  9360. 5:45:46this at the top name equals something
  9361. 5:45:48and then potentially sign it here and
  9362. 5:45:50then return back false so with reference
  9363. 5:45:52you don't have to worry about the fact
  9364. 5:45:54that you might not assign in every
  9365. 5:45:55single path of your code and finally the
  9366. 5:45:58main difference is the fact that out
  9367. 5:45:59parameters are still used by value and
  9368. 5:46:02this is by reference reference will be
  9369. 5:46:04slightly quicker since you'll get the
  9370. 5:46:05memory just location being passed in
  9371. 5:46:07instead of the Val we won't have two
  9372. 5:46:09copies of the same
  9373. 5:46:12variable the focus of this task is to
  9374. 5:46:15create a function that calculates the
  9375. 5:46:16area of a triangle so we're going to ask
  9376. 5:46:18the user for width and height install
  9377. 5:46:20them both create a function to calculate
  9378. 5:46:21the area the function is going to
  9379. 5:46:23calculate it using width times by height
  9380. 5:46:26taking that calculation and dividing the
  9381. 5:46:28value by two call it in the main and
  9382. 5:46:29print out the area of the triangle to
  9383. 5:46:31the screen so let's start with entering
  9384. 5:46:33the width and the height so we can say
  9385. 5:46:35enter width colon
  9386. 5:46:37and have int width equals convert to
  9387. 5:46:42in32 console.
  9388. 5:46:45readline and if you copy these
  9389. 5:46:47down change width to height and change
  9390. 5:46:50WID here to height and just before we
  9391. 5:46:52continue because we did copy and paste
  9392. 5:46:54down then this is quite bad and we
  9393. 5:46:56should use a function here so in both of
  9394. 5:46:58the cases we're going to output
  9395. 5:46:59something to the console and then read
  9396. 5:47:00it in and convert it back as an integer
  9397. 5:47:02so we can say static int because we
  9398. 5:47:04return back an integer so we can store
  9399. 5:47:06it we can say read int and we want a
  9400. 5:47:09string message so we can pass in this
  9401. 5:47:11value inside
  9402. 5:47:12here so we take these two lines and
  9403. 5:47:15place inside here instead of enter
  9404. 5:47:18height we're now going to have message
  9405. 5:47:19instead of int height we're just going
  9406. 5:47:21to have a return value and then we'll
  9407. 5:47:23return in the conversion from the
  9408. 5:47:24console read line and now we can use it
  9409. 5:47:26inside here read in inside the brackets
  9410. 5:47:29you want to put the string output which
  9411. 5:47:30is going to be enter height if we paste
  9412. 5:47:32that inside here and then do the same
  9413. 5:47:35thing for the previous section and and
  9414. 5:47:36change height to width and now get rid
  9415. 5:47:38of these so we just have these two lines
  9416. 5:47:41perfect so let's just print these out to
  9417. 5:47:43the screen on separate lines just to
  9418. 5:47:45make sure it's
  9419. 5:47:47working so the width of 10 and height of
  9420. 5:47:5015 we get 10 and 15 Perfect Since that's
  9421. 5:47:53working okay we can now create a
  9422. 5:47:54function that can calculate the area for
  9423. 5:47:56us so we can get static int calc area
  9424. 5:48:00and we can have int width and int
  9425. 5:48:05height and then taking this formula we
  9426. 5:48:07can simply just do return bracket width
  9427. 5:48:10times by height / two as you can see
  9428. 5:48:13this is literally exactly the same as
  9429. 5:48:15what we've got here it's crucial to
  9430. 5:48:17include the brackets because we want to
  9431. 5:48:19do this calculation first take the value
  9432. 5:48:21of that and divide it by two if this is
  9433. 5:48:23too many steps you can actually break it
  9434. 5:48:25down we can say int calculation is equal
  9435. 5:48:28to the width times by the height and
  9436. 5:48:30then we can simply return calculation /
  9437. 5:48:33two back to the function it's slightly
  9438. 5:48:35gred out because we haven't used use it
  9439. 5:48:37but that's not a problem we're going to
  9440. 5:48:38use it now I personally just to prefer
  9441. 5:48:40is to keep that inside here and just
  9442. 5:48:42have the brackets since a bit more
  9443. 5:48:43obvious to me what's going on but it's
  9444. 5:48:45up to you as the programmer either way
  9445. 5:48:47it makes no difference it's just more
  9446. 5:48:49for the programmer's convenience so now
  9447. 5:48:51we need to call the function above so we
  9448. 5:48:53can say int result equals calc area and
  9449. 5:48:55then pass in the width and pass in the
  9450. 5:48:57height and then we can have console
  9451. 5:48:59right line that just says the area is
  9452. 5:49:01and then result and then let's run it
  9453. 5:49:04you have 10 and 15 and there we go we
  9454. 5:49:06get the area area is
  9455. 5:49:0775 perfect if you didn't want to call
  9456. 5:49:10the function inside here and stall it
  9457. 5:49:11inside a variable you could just output
  9458. 5:49:13this straight to the screen by placing
  9459. 5:49:15the function call straight in the cly
  9460. 5:49:16braces and if you run that again just to
  9461. 5:49:18make sure 105 and we get 75 again
  9462. 5:49:22perfect so just a quick little recap we
  9463. 5:49:24have the console right plus the convert
  9464. 5:49:26inside this function which will let us
  9465. 5:49:28return as back an integer and we can
  9466. 5:49:30pass in the message straight in the
  9467. 5:49:31variable to make this a little bit
  9468. 5:49:32quicker and a bit easier you can
  9469. 5:49:34actually just change this one word
  9470. 5:49:35inside here so you could actually say
  9471. 5:49:38enter message call on space and then
  9472. 5:49:41just inside here you can say height and
  9473. 5:49:43just inside this one you can say width
  9474. 5:49:45just to reduce on code because in this
  9475. 5:49:47specific case the only thing that
  9476. 5:49:49changes between them two string
  9477. 5:49:50variables was the fact that one was
  9478. 5:49:52width and one's
  9479. 5:49:53height there we go you can see it's
  9480. 5:49:55exactly the same now so after we've read
  9481. 5:49:58in these two variables we pass it into
  9482. 5:50:00the function and all the function will
  9483. 5:50:01do is take width and height times them
  9484. 5:50:04both together take that value and divide
  9485. 5:50:06it by two and return it back to the user
  9486. 5:50:08and then we can call it straight in here
  9487. 5:50:09the area is calc area and pass in width
  9488. 5:50:11and
  9489. 5:50:15height in this task we're going to
  9490. 5:50:16create a function that will return us
  9491. 5:50:18back with the sum of numbers inside an
  9492. 5:50:20integer array so first step is to create
  9493. 5:50:23an initialize an array of numbers create
  9494. 5:50:25the function with the int return type
  9495. 5:50:27and it also takes a int array as a
  9496. 5:50:28parameter the function should return a
  9497. 5:50:30total of all the numbers inside the
  9498. 5:50:32interray call it in the main output the
  9499. 5:50:35total and just for extra bonus points
  9500. 5:50:37inside the function instead of using a
  9501. 5:50:39for Loop immediately you can check if
  9502. 5:50:41the array length is greater than zero
  9503. 5:50:43and then return minus one if it's empty
  9504. 5:50:45and then check that inside the main and
  9505. 5:50:47then output a specific message and also
  9506. 5:50:49see if you can think of any other
  9507. 5:50:51implementations you can use instead of
  9508. 5:50:53having to do return minus one how else
  9509. 5:50:55can you make this function whether that
  9510. 5:50:56means you have to change the parameters
  9511. 5:50:58or return type or anything like that
  9512. 5:51:00just have a think about the situation
  9513. 5:51:01and see what other options you can make
  9514. 5:51:03of course the output at the end is still
  9515. 5:51:04going to be the same but it's the way
  9516. 5:51:06you do it and the problem solving skills
  9517. 5:51:08that I'm trying to get you to get used
  9518. 5:51:09to let's get started so the create
  9519. 5:51:11initialize an array of numbers so you
  9520. 5:51:13have int numbers equals new int and
  9521. 5:51:16let's just allocate them manually 1 2 3
  9522. 5:51:184 5 and add a semicolon there we go now
  9523. 5:51:21let's create the function sum of
  9524. 5:51:24numbers and we want it to return an
  9525. 5:51:26integer back with the total and we want
  9526. 5:51:27to take an interray called
  9527. 5:51:30numbers and then inside here we need to
  9528. 5:51:32actually return back an INT so we can
  9529. 5:51:34create an in total make that equal to Z
  9530. 5:51:37and at the end we can return total and
  9531. 5:51:39then during the middle we're going to
  9532. 5:51:40have to write our for Loop so we can go
  9533. 5:51:42through the integer array and count
  9534. 5:51:43through everything so we can use a for
  9535. 5:51:45each V item inside our numbers and just
  9536. 5:51:48inside here we can say total plus equals
  9537. 5:51:51item perfect so if we can call it in
  9538. 5:51:54here we can have a console right line
  9539. 5:51:56and we can just say sum of numbers and
  9540. 5:51:57pass our numbers array in the parameters
  9541. 5:52:00so we're just going to Output it to the
  9542. 5:52:01screen just for the time
  9543. 5:52:02being so sum of values from zero all the
  9544. 5:52:05way till 5 is 15
  9545. 5:52:08perfect so now that we've done that
  9546. 5:52:10we've created a function that Returns
  9547. 5:52:11the total numbers call it in the main
  9548. 5:52:13I'll put the total so we can just say
  9549. 5:52:15the
  9550. 5:52:15total the total is and then wrap this
  9551. 5:52:19around in the C
  9552. 5:52:20brace there we go the total is
  9553. 5:52:2315 now with the extra points we can
  9554. 5:52:25check the array length and return minus
  9555. 5:52:27one if the array is empty so let's see
  9556. 5:52:30what we can do in here before we even
  9557. 5:52:31end up going inside anything to do with
  9558. 5:52:33the total we can wrap this around in if
  9559. 5:52:35statement that says if the numbers do
  9560. 5:52:37length is greater than zero then we can
  9561. 5:52:39proceed with this little section so if
  9562. 5:52:41we highlight all of this hold alt and
  9563. 5:52:43press upar key then because it's one
  9564. 5:52:45back just press tab once and just press
  9565. 5:52:47the delete key here and let's get rid of
  9566. 5:52:48the empty Line This is going to return
  9567. 5:52:50us back with an error because not all
  9568. 5:52:52code path to return a value if this
  9569. 5:52:54returns back true then we'll eventually
  9570. 5:52:56hit a return but if this returns back
  9571. 5:52:58false then we not do anything inside
  9572. 5:53:00here so let's have a look at our
  9573. 5:53:01requirements return minus one if the
  9574. 5:53:04array is empty this dictates whether the
  9575. 5:53:06ray is empty so if this returns back
  9576. 5:53:08false then it's going to come into here
  9577. 5:53:10and we can say return minus one so we've
  9578. 5:53:13got this function call in here we don't
  9579. 5:53:15want to print the total right now
  9580. 5:53:17because we want to make sure that this
  9581. 5:53:18total is actually greater than zero
  9582. 5:53:21because we don't want to make a sum of
  9583. 5:53:23the numbers if the array is empty so if
  9584. 5:53:25you run this right now going to get the
  9585. 5:53:27total is 15 and if we get rid of all
  9586. 5:53:29these variables and just have the length
  9587. 5:53:31of five but not give it any value so all
  9588. 5:53:34the values are going to be zero then if
  9589. 5:53:35we still run that then the total is
  9590. 5:53:37going to be zero but that's still an
  9591. 5:53:38acceptable value because that's actually
  9592. 5:53:40got values
  9593. 5:53:41inside but if you said the array length
  9594. 5:53:44is zero then the total is minus one of
  9595. 5:53:46course we don't want the user to see
  9596. 5:53:48minus one so we want to bring this back
  9597. 5:53:51and instead of calling the function
  9598. 5:53:52immediately down here let's call it here
  9599. 5:53:55we can say int result is equal to this
  9600. 5:53:58and then we can say if the result is
  9601. 5:54:00greater than minus one then we can
  9602. 5:54:02output the total and again press into
  9603. 5:54:04this line hold alt and press Arrow key
  9604. 5:54:07and then we can place the result inside
  9605. 5:54:09here and the L's to this will be canot
  9606. 5:54:12add up an empty array there we go so now
  9607. 5:54:15if you run it we should still see 15 but
  9608. 5:54:18if we get rid of this and put it back to
  9609. 5:54:20zero there we go we cannot add an empty
  9610. 5:54:24array so we bring that back perfect so
  9611. 5:54:27now if we check this we've got check in
  9612. 5:54:28the main output the message and then now
  9613. 5:54:31we've got this little solution do we
  9614. 5:54:33need to return minus one how else can we
  9615. 5:54:35make this so right now we're going to
  9616. 5:54:37have to return back minus one cuz that's
  9617. 5:54:39the only way that we can tell the user
  9618. 5:54:41whether or not it's been successful if
  9619. 5:54:43the r is empty we return minus one and
  9620. 5:54:46if the ray is not empty then we return
  9621. 5:54:47something greater than minus one which
  9622. 5:54:49will be something above zero or zero
  9623. 5:54:51itself but as long as it's greater than
  9624. 5:54:53minus one that's how we know it's
  9625. 5:54:54complete but we don't actually want to
  9626. 5:54:56do this we want to be ideally returning
  9627. 5:54:59back a Boolean so let's duplicate this
  9628. 5:55:01function and show you both
  9629. 5:55:03implementations let's change this into a
  9630. 5:55:05Bo
  9631. 5:55:07and then because we're still going to
  9632. 5:55:08need to reference the total we're going
  9633. 5:55:09to have an out in total so we need to
  9634. 5:55:12change up a few things inside here
  9635. 5:55:14instead of this return minus one we can
  9636. 5:55:16do return false to indicate that it
  9637. 5:55:18hasn't been successful instead of
  9638. 5:55:20returning back total we can actually
  9639. 5:55:21just return back true now we haven't
  9640. 5:55:23done everything yet we don't need to
  9641. 5:55:25redeclare in total inside here because
  9642. 5:55:27we already declared it inside here so we
  9643. 5:55:29could just initialize total to equal to
  9644. 5:55:31zero but the problem here is if we make
  9645. 5:55:34total equal to zero just within this
  9646. 5:55:36path
  9647. 5:55:36then it's still going to complain that
  9648. 5:55:38total must be assigned so we can take
  9649. 5:55:40this and just assign it right at the
  9650. 5:55:41start of the function just like that
  9651. 5:55:44there we go so we make total equal to
  9652. 5:55:46zero that gets rid of the compil errors
  9653. 5:55:48and gives it a value the reason why we
  9654. 5:55:50have to give it a value is because this
  9655. 5:55:51out in parameter doesn't like it if you
  9656. 5:55:54don't assign the value that is a
  9657. 5:55:55requirement for using the out parameter
  9658. 5:55:57so we make total equal to zero check the
  9659. 5:55:59numbers length If This Were to return
  9660. 5:56:01back false then we return false to
  9661. 5:56:03indicate that we can't do anything if it
  9662. 5:56:06this returns back true it will go
  9663. 5:56:07through and count all of the items place
  9664. 5:56:10inside total and then return back true
  9665. 5:56:13so let's have a look and see what else
  9666. 5:56:15we can write this little section of code
  9667. 5:56:16so instead of having to call inside here
  9668. 5:56:18and use it within there then we can
  9669. 5:56:20actually do this another way the reason
  9670. 5:56:22why we actually calling this straight
  9671. 5:56:23inside here is because we can't place
  9672. 5:56:26this straight into here since it returns
  9673. 5:56:27us back with an integer and now that is
  9674. 5:56:29actually fine if that returns back of
  9675. 5:56:31the integer cuz as you can see there's
  9676. 5:56:33no problems here the issue arises is
  9677. 5:56:35when we have our result variable we can
  9678. 5:56:37use it inside the bracket and down here
  9679. 5:56:39there's no point in calling this twice
  9680. 5:56:41to get the same value although this
  9681. 5:56:42would still technically work but this is
  9682. 5:56:44not really efficient since you're
  9683. 5:56:45calling the function twice it's best to
  9684. 5:56:48call it
  9685. 5:56:49once store the value and then make use
  9686. 5:56:51of it twice when we use our out
  9687. 5:56:54parameter you'll see that this gets a
  9688. 5:56:55bit easier so straight inside the if
  9689. 5:56:57statement we can say sum of numbers and
  9690. 5:56:59then we can pass in our numbers and then
  9691. 5:57:01space out into total and what that'll do
  9692. 5:57:04now is because this returns us back with
  9693. 5:57:06the fol we don't need any additional
  9694. 5:57:08conditions like we have one here cuz
  9695. 5:57:09this will turn us back with a true or
  9696. 5:57:11false true if it succeeds false if it
  9697. 5:57:14doesn't so we don't need to say if it's
  9698. 5:57:16greater than minus one because we don't
  9699. 5:57:17actually care about the value of the
  9700. 5:57:19total right now we just care if this
  9701. 5:57:20returns back with true or false so
  9702. 5:57:23inside here we can take this same output
  9703. 5:57:24message and instead of having result
  9704. 5:57:26here we can have
  9705. 5:57:27total and then we can take this exact L
  9706. 5:57:30statement and place it here and let's
  9707. 5:57:32see what happens now the total is 15 and
  9708. 5:57:35the total is 15 so they both turning
  9709. 5:57:37back the same values let's add something
  9710. 5:57:39else on here just to make sure it works
  9711. 5:57:41there we go so you can see that both
  9712. 5:57:43these Solutions look very identical but
  9713. 5:57:45one is considerably different we have to
  9714. 5:57:47make a variable outside use inside here
  9715. 5:57:50and use inside here whereas in this
  9716. 5:57:52function we can actually just call the
  9717. 5:57:53function straight inside that will
  9718. 5:57:55process our new out into total and then
  9719. 5:57:58we can simply use total inside here and
  9720. 5:58:00we don't have to require another
  9721. 5:58:01additional variable just outside here as
  9722. 5:58:04this one will and you can see that the
  9723. 5:58:06functions is a little bit better because
  9724. 5:58:08we're returning true and returning false
  9725. 5:58:10to indicate whether or not it's been
  9726. 5:58:11successful whereas in this solution we
  9727. 5:58:13have to return back an arbitrary value
  9728. 5:58:15and then check that in the console the
  9729. 5:58:18reason with returning minus one is kind
  9730. 5:58:20of a problem is what if these numbers
  9731. 5:58:22are actually all negative then this
  9732. 5:58:24value is going to be very bad this can
  9733. 5:58:27only really work if all of these values
  9734. 5:58:29are positive if you had negatives for
  9735. 5:58:32all of these and we tried running the
  9736. 5:58:34code there you go this is a major issue
  9737. 5:58:37cannot add up an empty array well it's
  9738. 5:58:39not the fact that it is empty it's the
  9739. 5:58:41fact that these values are smaller than
  9740. 5:58:43minus one so you don't actually want to
  9741. 5:58:45be going with something like this
  9742. 5:58:46approach where you're comparing the
  9743. 5:58:48value because you don't know what
  9744. 5:58:50numbers are going to be typed into here
  9745. 5:58:51if the user were type in positive or
  9746. 5:58:53negative you want to provide a solution
  9747. 5:58:55just like this one that works for either
  9748. 5:58:57positive or negative you don't want to
  9749. 5:58:59restrict that by checking if it's
  9750. 5:59:01greater than minus one will only works
  9751. 5:59:03if all of these are positive values all
  9752. 5:59:05this is an index for the array since
  9753. 5:59:07arrays start at zero and go upwards you
  9754. 5:59:09can't have a negative -1 array position
  9755. 5:59:11but in terms of numbers that are
  9756. 5:59:13randomly typed inside of a ray you
  9757. 5:59:14should always be wary of using this
  9758. 5:59:16option because if some of them are
  9759. 5:59:18negative then this could return back
  9760. 5:59:20false so let's just have a quick recap
  9761. 5:59:23so we have an integer array we have our
  9762. 5:59:25values initialized down here and then
  9763. 5:59:27the first solution we call the function
  9764. 5:59:29passing the numbers array get our result
  9765. 5:59:31back check if it's greater than minus
  9766. 5:59:33one then that means it's been successful
  9767. 5:59:35so we print it out and else we tell it
  9768. 5:59:37it's an empty array now second option we
  9769. 5:59:40actually call the function because it
  9770. 5:59:41returns back a boly in this case so we
  9771. 5:59:43can place it straight inside the if
  9772. 5:59:44statement and then we can pass in the
  9773. 5:59:46numbers AR and then we have an out in
  9774. 5:59:48total which will give us the total and
  9775. 5:59:50in this case we don't need to check the
  9776. 5:59:51value because this returns us back with
  9777. 5:59:53the true or false and in this case true
  9778. 5:59:55determines whether it's been successful
  9779. 5:59:57and false it doesn't if it returns back
  9780. 5:59:59true then it has some values inside the
  9781. 6:00:01numbers array that we can actually add
  9782. 6:00:03up and if it returns back false then
  9783. 6:00:05that means the array is empty so let's
  9784. 6:00:07have a look at the implementations so
  9785. 6:00:09without the out parameters this is the
  9786. 6:00:10first solution up here we get the
  9787. 6:00:13numbers length check if it's greater
  9788. 6:00:15than zero if it's not we return minus
  9789. 6:00:17one make a new variable called total
  9790. 6:00:19make equal to zero and we run a for each
  9791. 6:00:21Loop V item inside the numbers and we
  9792. 6:00:24just say total plus equals item add them
  9793. 6:00:26all up in the for Loop and simply return
  9794. 6:00:28the total and then in the out solution
  9795. 6:00:31we have a total equals zero which just
  9796. 6:00:33initializes this variable otherwise
  9797. 6:00:35we'll get a compiler then we can say if
  9798. 6:00:37the numbers is greater than zero then we
  9799. 6:00:39can say if numbers. length is greater
  9800. 6:00:40than zero then we can do the for Loop
  9801. 6:00:43and return back true and if it turn back
  9802. 6:00:45false then we can say false inside here
  9803. 6:00:47and then when we use it straight in the
  9804. 6:00:49brackets we have access to it straight
  9805. 6:00:50away since it uses a Boolean variable
  9806. 6:00:53and then our value will be placed inside
  9807. 6:00:54our out into total parameter which is
  9808. 6:00:57used down here for the output to the
  9809. 6:00:59screen and again if this returns back
  9810. 6:01:01false then it will say I cannot add up
  9811. 6:01:03an empty rray and then we hit the
  9812. 6:01:04console read line and we at the the end
  9813. 6:01:06there we go that covers everything
  9814. 6:01:07inside
  9815. 6:01:09here exceptions are used to prevent your
  9816. 6:01:11code from crashing and then the program
  9817. 6:01:13closes down let's have a look at a very
  9818. 6:01:16very common example let's have a console
  9819. 6:01:18right and we can say enter a number and
  9820. 6:01:21then we can use int Norm equals convert
  9821. 6:01:24to int32 and then we can have a console.
  9822. 6:01:27readline inside here and let's just
  9823. 6:01:29print out number to the screen now if we
  9824. 6:01:31assume everything goes well and we
  9825. 6:01:33actually enter an integer like 15 and we
  9826. 6:01:35press enter then we simply get 15
  9827. 6:01:38returned and as we saw in the past when
  9828. 6:01:40we covered the trip pass function if you
  9829. 6:01:42enter anything else that's not exactly a
  9830. 6:01:44number then you get again exception
  9831. 6:01:47exception unhandled unhandled means that
  9832. 6:01:49we haven't wrote anything specific that
  9833. 6:01:51will handle this exception so it's
  9834. 6:01:53causing the program to close it's a
  9835. 6:01:55system. format exception it says the
  9836. 6:01:58input string was not in a correct format
  9837. 6:02:00so this is a format exception because
  9838. 6:02:01we've entered in a very weird set of
  9839. 6:02:04characters that can't be converted over
  9840. 6:02:05to an integer so now our program is
  9841. 6:02:08forced to crash and now rest of our code
  9842. 6:02:10is going to be deleted now because it
  9843. 6:02:11won't be able to run so if you had any
  9844. 6:02:14important data that was stored just
  9845. 6:02:15before this point then it is now lost
  9846. 6:02:17this is very crucial because if you have
  9847. 6:02:19a system that rides to a database or it
  9848. 6:02:21stores some very important information
  9849. 6:02:23that someone's typed in you want to
  9850. 6:02:24handle the exceptions so if something
  9851. 6:02:26were to arise it doesn't actually close
  9852. 6:02:28the program down and they lose all of
  9853. 6:02:30their data I'm sure you've had it in the
  9854. 6:02:31past where you've tried to do something
  9855. 6:02:33and the program comes up Windows and it
  9856. 6:02:35says this program had to stop responding
  9857. 6:02:37when that normally comes up it means
  9858. 6:02:39that Something's Happened that's so
  9859. 6:02:41fatal that the program cannot continue
  9860. 6:02:43it has to crash down and in most cases
  9861. 6:02:45this is what's called an exception so if
  9862. 6:02:47we run this code again let's have a look
  9863. 6:02:48and see what else we can do to make this
  9864. 6:02:50code break so of course as you heard
  9865. 6:02:52before a gnome integer can only survive
  9866. 6:02:55up to about 2 billion numbers so if you
  9867. 6:02:57enter a number bigger than that let's
  9868. 6:02:59see what happens then we get an overflow
  9869. 6:03:01exception value is either too large or
  9870. 6:03:04too small for an in3 too of course this
  9871. 6:03:06number is very big and only a long would
  9872. 6:03:09handle something like this an integer
  9873. 6:03:11definitely won't so these are the kind
  9874. 6:03:13of exceptions where you'd have to handle
  9875. 6:03:15it yourself manually otherwise your
  9876. 6:03:17program's going to crash coming up next
  9877. 6:03:19we're going to go through the ways that
  9878. 6:03:20we can handle these and make sure
  9879. 6:03:22programs don't come to a halt exactly
  9880. 6:03:24like these
  9881. 6:03:27examples I want to explore a technique
  9882. 6:03:29called trycatch this is very similar to
  9883. 6:03:32when we use the int. trass what this
  9884. 6:03:35essentially does does is it tries to do
  9885. 6:03:37something and it lets you know if
  9886. 6:03:39something went wrong this is exactly the
  9887. 6:03:41same when we're dealing with exceptions
  9888. 6:03:43except when we're doing a try catch what
  9889. 6:03:45the catch means is it's catching the
  9890. 6:03:47exceptions that happen and therefore it
  9891. 6:03:49is now a handled exception instead of
  9892. 6:03:51unhandled exception so before we try and
  9893. 6:03:53fix how this works let's just explore
  9894. 6:03:55the syntax so we can trpe in try Follow
  9895. 6:03:58by curly brace and just after this curly
  9896. 6:04:00brace we can say catch and then we're
  9897. 6:04:03going to put something inside the
  9898. 6:04:04brackets and then we have it opens
  9899. 6:04:06brackets if you wanted a quick shortcut
  9900. 6:04:08to this just like we've done the for
  9901. 6:04:10Loop and we double tap tab we can
  9902. 6:04:12actually delete this so you can type in
  9903. 6:04:13try and press tab twice and then you get
  9904. 6:04:16try the Open brackets you get the
  9905. 6:04:17exception and then we get a throw so
  9906. 6:04:20let's get rid of this and what we can do
  9907. 6:04:22right now is we can actually place the
  9908. 6:04:24code that we want inside here inside
  9909. 6:04:26this Tri bit because we're telling the
  9910. 6:04:29compiler that we want to try to do the
  9911. 6:04:30things inside here and then we want to
  9912. 6:04:32catch these things so inside these
  9913. 6:04:34brackets what you want to actually do is
  9914. 6:04:37decide what thing that you want to catch
  9915. 6:04:39if you type in exception this will catch
  9916. 6:04:42every single exception now the problem
  9917. 6:04:44with that is you'll actually face some
  9918. 6:04:46difficulties when you're trying to
  9919. 6:04:48Output very very specific messages so as
  9920. 6:04:51we've said before when you type in a
  9921. 6:04:53value too large you get an overflow
  9922. 6:04:54exception but when you type a value that
  9923. 6:04:57can't be converted like a square bracket
  9924. 6:04:59then you get an input format exception
  9925. 6:05:01so first what you actually want to do is
  9926. 6:05:03run this code outside the try and let's
  9927. 6:05:06run it now and let's get them bad values
  9928. 6:05:09so we had the square brackets and we can
  9929. 6:05:10get this and let's copy it down and
  9930. 6:05:12place it inside a comment just like this
  9931. 6:05:15and then let's rerun
  9932. 6:05:17it and if you type a number too big take
  9933. 6:05:21this stop the program and put it inside
  9934. 6:05:24here so now that you know what the
  9935. 6:05:25exceptions are called we can actually
  9936. 6:05:27write these inside the code so we have a
  9937. 6:05:29form exception and an overflow exception
  9938. 6:05:31but before we get into these specific
  9939. 6:05:33exceptions let's just catch everything
  9940. 6:05:35this is just a catch all and then see
  9941. 6:05:38how this
  9942. 6:05:39works so as we've put it back inside
  9943. 6:05:42here let's see what actually happens so
  9944. 6:05:44we can just put a console right on here
  9945. 6:05:46and say something has went
  9946. 6:05:49wrong the reason why it's something has
  9947. 6:05:52went wrong is because this is a catch
  9948. 6:05:54all any exception appears during this
  9949. 6:05:57code it will always come into here
  9950. 6:05:59regardless of what the exception is
  9951. 6:06:01whether it's a format or an overflow it
  9952. 6:06:03doesn't matter it will always come into
  9953. 6:06:05here so it's going to be something
  9954. 6:06:06because this is not specific at all so
  9955. 6:06:09let's run the code enter a very big
  9956. 6:06:12number something has went wrong but if
  9957. 6:06:14you notice how the program doesn't
  9958. 6:06:15actually clash it's still very much
  9959. 6:06:17alive and we can test this by putting a
  9960. 6:06:19simple message here at the end that says
  9961. 6:06:21goodbye and we run it again enter a big
  9962. 6:06:24number and there you go we get the
  9963. 6:06:26message as goodbye so it successfully
  9964. 6:06:28skipped over this section without
  9965. 6:06:30actually causing an exception that
  9966. 6:06:31breaks the program so even though we've
  9967. 6:06:34entered something very fertile because
  9968. 6:06:36we've handled it then there's no
  9969. 6:06:38problems so if we now type the other one
  9970. 6:06:40where we have the square brackets then
  9971. 6:06:42you'll see we'll also get something has
  9972. 6:06:43went
  9973. 6:06:44wrong so if you wanted to catch a very
  9974. 6:06:47specific exception what we can do is say
  9975. 6:06:49catch open the brackets and we can say
  9976. 6:06:52format exception just like it says above
  9977. 6:06:54here and then place the curry braces and
  9978. 6:06:57what you can do now is you can actually
  9979. 6:06:58chain these and these go from top to
  9980. 6:07:01bottom when an exception occurs up here
  9981. 6:07:04it will match this one if if it doesn't
  9982. 6:07:05match it will match this one and you
  9983. 6:07:08want to put the catch all right at the
  9984. 6:07:09end so if it doesn't match any of the
  9985. 6:07:11ones above it it will always match the
  9986. 6:07:13last one so you can have a generic
  9987. 6:07:15message inside here but you can have a
  9988. 6:07:17very specific message inside here
  9989. 6:07:19because it's a format exception you can
  9990. 6:07:21say please only enter numbers so now if
  9991. 6:07:24you run the code and if we type in
  9992. 6:07:25square brackets it'll say please only
  9993. 6:07:27enter numbers there we go so this is a
  9994. 6:07:29very specific example and if we copy and
  9995. 6:07:32paste this down again and then we can
  9996. 6:07:34say overflow exception
  9997. 6:07:36and we can say please enter a number
  9998. 6:07:40less than 2
  9999. 6:07:43billion if you enter a very big number
  10000. 6:07:46again please enter a number that's less
  10001. 6:07:48than 2 billion
  10002. 6:07:49perfect so this will get checked like if
  10003. 6:07:52statement it will start on the first one
  10004. 6:07:54and go down the list imagine this is a
  10005. 6:07:56if and then we have an else if and then
  10006. 6:07:58else if as well if you place this one
  10007. 6:08:01right at the start then you're actually
  10008. 6:08:03going to get compiled errors and you
  10009. 6:08:05can't actually have the catch all right
  10010. 6:08:07at the start so even if you wanted to
  10011. 6:08:09the compiler literally doesn't let you
  10012. 6:08:11and anyway you don't really want to be
  10013. 6:08:13doing that because then you'll be ruling
  10014. 6:08:14out these two and it's kind of pointless
  10015. 6:08:16writing this code if it's never going to
  10016. 6:08:18get used anyway so now when we run the
  10017. 6:08:20code you can see even if we typ in a bad
  10018. 6:08:22value our program will still run and
  10019. 6:08:24everything works okay so now what we can
  10020. 6:08:26do is actually make this into a real
  10021. 6:08:28life
  10022. 6:08:29situation so if we actually type in 10.2
  10023. 6:08:32then it says please only enter numbers
  10024. 6:08:34then we're going to have an issue here
  10025. 6:08:35here because if we try and print the
  10026. 6:08:37value down here then it's going to cause
  10027. 6:08:38a problem let's try and print out num
  10028. 6:08:41and you can see that num does not exist
  10029. 6:08:43now we have an issue of scope in is only
  10030. 6:08:45declared within here and here so if you
  10031. 6:08:47wanted to print out the value of gome
  10032. 6:08:49we'd have to print it out up here so now
  10033. 6:08:51let's run it again and if we enter 20 20
  10034. 6:08:53comes up if we've entered 15.5 then it
  10035. 6:08:56actually skips over this console right
  10036. 6:08:58line because the exception occurs right
  10037. 6:09:00here and then it'll jump into one of
  10038. 6:09:02these paths so in order to store the
  10039. 6:09:04value of num you can either the move num
  10040. 6:09:06outside here and make it equal to zero
  10041. 6:09:08and then assign num inside here and then
  10042. 6:09:11you can take this console right line and
  10043. 6:09:13actually put it right after the triy
  10044. 6:09:14pass but I don't really suggest this
  10045. 6:09:16option so if you said 15.5 then you're
  10046. 6:09:19just going to get zero returned but we
  10047. 6:09:21don't really care about the zero because
  10048. 6:09:23we're not going to get the value
  10049. 6:09:24converted so why do we need to print the
  10050. 6:09:26zero and the answer is we don't so what
  10051. 6:09:29we should do is keep it all inside here
  10052. 6:09:33again and then what we should do instead
  10053. 6:09:36is wrap this around in a while loop so
  10054. 6:09:39this will try to do the code this
  10055. 6:09:41section will catch them all and then we
  10056. 6:09:43simply have a good buy at the end but
  10057. 6:09:45let's just make sure that this actually
  10058. 6:09:46runs because if we type in
  10059. 6:09:4915.5 then the user still hasn't entered
  10060. 6:09:51the number but our program was closed so
  10061. 6:09:54we want to loop around until the user
  10062. 6:09:56has successfully entered a correct
  10063. 6:09:58number so we can say bull looping equals
  10064. 6:10:01true and we can have while the cord is
  10065. 6:10:04looping that off the curly braces let's
  10066. 6:10:06place our entire Tri cat inside here if
  10067. 6:10:09we highlight it all hold alt and press
  10068. 6:10:11the op power key and then tab it once
  10069. 6:10:14and then get rid of this empty line so
  10070. 6:10:16now we'll keep looping around here until
  10071. 6:10:18this is going to be a different value so
  10072. 6:10:20because the while loop will only keep
  10073. 6:10:22looping until this is true and the
  10074. 6:10:24moment when it's false is when it will
  10075. 6:10:26cancel the W Loop so as I said before
  10076. 6:10:28when we run this code and then just at
  10077. 6:10:31this specific line if the exception
  10078. 6:10:33occurs it will actually skip this line
  10079. 6:10:34and jumped below here so if it skip this
  10080. 6:10:37line then what we can actually do is we
  10081. 6:10:39know if it reaches this line then the
  10082. 6:10:41conversion has been successful so we can
  10083. 6:10:44print out to the screen and we can say
  10084. 6:10:45looping equals false and we don't need
  10085. 6:10:48to place looping is equal to True inside
  10086. 6:10:50all of these three paths because if we
  10087. 6:10:52don't assign it to true then it's still
  10088. 6:10:55going to be the value false no matter
  10089. 6:10:56how many times you loop around so let's
  10090. 6:10:58try and run that
  10091. 6:11:01now enter a number 15.5 enter a number
  10092. 6:11:0415
  10093. 6:11:06and we can keep doing this forever as
  10094. 6:11:08it's a while loop and as soon as we
  10095. 6:11:10enter a valid character 20 and now it
  10096. 6:11:12says goodbye because we hit the end of
  10097. 6:11:14the
  10098. 6:11:15program so that's it for this video but
  10099. 6:11:17let's just have a little recap so we've
  10100. 6:11:20determined when we run our code just
  10101. 6:11:22these two lines that in both situations
  10102. 6:11:24we can have a format exception and an
  10103. 6:11:26overflow exception we can use these to
  10104. 6:11:28catch them specific exceptions and then
  10105. 6:11:30have a catch all at the end the reason
  10106. 6:11:33why you want to catch multiple is you
  10107. 6:11:34can have a very specific message
  10108. 6:11:36overflow dictates we have a number
  10109. 6:11:38that's too big or too small to fit
  10110. 6:11:40inside of an integer it can be less than
  10111. 6:11:42negative2 billion or can be greater than
  10112. 6:11:452 billion and that's how you get the
  10113. 6:11:46Overflow exception we get him a format
  10114. 6:11:49exception if you entered something
  10115. 6:11:50that's literally not a number or
  10116. 6:11:52something that has decimal places and
  10117. 6:11:54then our catch all can be if anything
  10118. 6:11:55else goes wrong we can output a generic
  10119. 6:11:57message by 99% of the cases you'll
  10120. 6:12:00either enter number too big or too small
  10121. 6:12:03or it in the wrong format and this AC
  10122. 6:12:05ction is just so it catches everything
  10123. 6:12:06else should anything else happen so at
  10124. 6:12:09this point even though our program
  10125. 6:12:10doesn't actually close and there's no
  10126. 6:12:11exceptions the user hasn't had a chance
  10127. 6:12:13to enter the number again so we use this
  10128. 6:12:15inside a while loop and we can say while
  10129. 6:12:17looping is true when we hit a conversion
  10130. 6:12:20that's successful we say looping is
  10131. 6:12:21equal to false and once it hits this
  10132. 6:12:23line the Y Loop will then exit and then
  10133. 6:12:25we'll get our goodbye message and we
  10134. 6:12:27wrap the entire try catch inside the W
  10135. 6:12:30Loop just so this keeps on looping until
  10136. 6:12:32the user has typed in a successful value
  10137. 6:12:35as demonstrated
  10138. 6:12:37above we can type all this and 15.2 15
  10139. 6:12:41Point 15 and then finally says 15 and
  10140. 6:12:45then
  10141. 6:12:47goodbye so I want to explore different
  10142. 6:12:49ways you can output messages inside this
  10143. 6:12:51catch block when you have a catch
  10144. 6:12:53exception this is actually a parameter
  10145. 6:12:55that we're passing in this parameter can
  10146. 6:12:57actually have a variable as well
  10147. 6:12:59normally when you specify parameters in
  10148. 6:13:01a function form you say in and then some
  10149. 6:13:06something whereas right now we're kind
  10150. 6:13:08of defining the data type which is our
  10151. 6:13:10exception in the same case as int here
  10152. 6:13:13but we're actually not giving it a name
  10153. 6:13:14so it looks something like this of
  10154. 6:13:16course when we do this inside a function
  10155. 6:13:17we're going to get a syntax error but
  10156. 6:13:19for a catch we're not going to get any
  10157. 6:13:21errors at all and the reason is because
  10158. 6:13:22we can catch without having to use a
  10159. 6:13:24local variable so if we actually add a
  10160. 6:13:26local variable make you call it e for
  10161. 6:13:29exception then we actually have lots of
  10162. 6:13:31options inside here Type e then we have
  10163. 6:13:33lots of data and the stack traces very
  10164. 6:13:35helpful when you debuging but we'll
  10165. 6:13:37cover that at later date the thing I
  10166. 6:13:38want to focus on right now is the
  10167. 6:13:40message so we can use this and print out
  10168. 6:13:42to the screen so we can say error callon
  10169. 6:13:46and then type in E message and let let
  10170. 6:13:49run that now if you enter something
  10171. 6:13:51invalid input string was not in a
  10172. 6:13:54correct format if you enter a very big
  10173. 6:13:56number then values either too large or
  10174. 6:13:59too small for an in32 as you can see
  10175. 6:14:01these error messag are pretty
  10176. 6:14:02descriptive and normal users should be
  10177. 6:14:04able to read this and understand it of
  10178. 6:14:06course if you want this error message to
  10179. 6:14:08be more specific then you can add some
  10180. 6:14:10more catches inside here and we can have
  10181. 6:14:12format
  10182. 6:14:14exception this will be if you type in
  10183. 6:14:16something other than number but if you
  10184. 6:14:17do exactly the same thing in here then
  10185. 6:14:20it's kind of redundant because you're
  10186. 6:14:21just outputting exactly the same value
  10187. 6:14:23if we try and put the square brackets
  10188. 6:14:25again then we're going to get this
  10189. 6:14:26string which is actually outputed from
  10190. 6:14:28this line but even if we include the
  10191. 6:14:30catch all exception you get exactly the
  10192. 6:14:32same error message outputed from here
  10193. 6:14:35the moral of the story is you should
  10194. 6:14:37always have a catch all right at the end
  10195. 6:14:39of your chain but if the only thing
  10196. 6:14:41you're going to do inside here is simply
  10197. 6:14:43output this message to the screen then
  10198. 6:14:45you don't need any multiple catches what
  10199. 6:14:47you should do is catch multiple
  10200. 6:14:49exceptions that you know are going to
  10201. 6:14:50get fired in your code and then you want
  10202. 6:14:52to handle them and then have a catch all
  10203. 6:14:54at the bottom that will display the
  10204. 6:14:55message generically instead of
  10205. 6:14:57displaying the message right here you
  10206. 6:14:59can actually have a more specific
  10207. 6:15:00message like we covered in the last
  10208. 6:15:01video so this could be please only enter
  10209. 6:15:05numbers and we don't actually need the
  10210. 6:15:07message in this case because we know
  10211. 6:15:09what exception we're going to catch
  10212. 6:15:10because we've specified formal exception
  10213. 6:15:13another little tip if you hover over the
  10214. 6:15:15function call it says here format
  10215. 6:15:17exception and overfall exception which
  10216. 6:15:19are the two that we handled in the
  10217. 6:15:20previous video this is very easy and
  10218. 6:15:23understandable if you can just read the
  10219. 6:15:24values from here and to see what
  10220. 6:15:26exceptions can get raised and the same
  10221. 6:15:28thing here you get an IO exception for
  10222. 6:15:30the console right line the console right
  10223. 6:15:32can also give back an IO exception as
  10224. 6:15:34well generally speaking these console
  10225. 6:15:36functions almost never return back
  10226. 6:15:38exceptions I don't think I've ever hit a
  10227. 6:15:41situation where this exception has been
  10228. 6:15:42returned unless you do something
  10229. 6:15:44ridiculous and maybe you type in way too
  10230. 6:15:46many values that the console right line
  10231. 6:15:48can handle let's try an example and we
  10232. 6:15:51can do something like this if you enter
  10233. 6:15:52a very big number that can't be
  10234. 6:15:54specified inside the integer then
  10235. 6:15:56console right line has so many overloads
  10236. 6:15:58that actually processes it as a long
  10237. 6:16:00right now so you almost can't break this
  10238. 6:16:02unless something fundamental to your
  10239. 6:16:04computer actually happens so you have
  10240. 6:16:06exception e and with exception e you can
  10241. 6:16:09actually take this local variable and
  10242. 6:16:10print out the message this is the
  10243. 6:16:12preferred pattern because instead of
  10244. 6:16:14outputting here something went wrong
  10245. 6:16:16just very generically you can actually
  10246. 6:16:18give them the error message that comes
  10247. 6:16:19back from the exception the reason why
  10248. 6:16:21this is helpful is because if it doesn't
  10249. 6:16:23hit any of these exceptions and it just
  10250. 6:16:25happens to fall into the catch all at
  10251. 6:16:27least the user has an idea of what the
  10252. 6:16:29error message is going to be the
  10253. 6:16:31preferred option is to try get to a
  10254. 6:16:32point where you never have to reach the
  10255. 6:16:34catch all and try chain as many catches
  10256. 6:16:36as you can in order to handle as many
  10257. 6:16:39exceptions based on the code that you're
  10258. 6:16:40writing which can be found if you hover
  10259. 6:16:42over the function and read the
  10260. 6:16:44exceptions once you've covered as many
  10261. 6:16:46options as you can throughout the
  10262. 6:16:47catches then you almost never have to
  10263. 6:16:49worry about the code entering this
  10264. 6:16:51section because we've covered both the
  10265. 6:16:53exceptions in format and overflow and we
  10266. 6:16:55can do the Overflow right now please
  10267. 6:16:58enter a value under 2
  10268. 6:17:00billion then that means we've actually
  10269. 6:17:02covered all of the catches that are to
  10270. 6:17:04do with converting an integer 32 covered
  10271. 6:17:07format and covered overflow so when we
  10272. 6:17:09run this code the only two things that
  10273. 6:17:11can possibly go wrong are these two
  10274. 6:17:12functions the format exception overflow
  10275. 6:17:15exception and we won't ever hit the
  10276. 6:17:17catch all in this case but if anything
  10277. 6:17:19goes wrong at least we have a belt and
  10278. 6:17:21Brace option to catch it and we can tell
  10279. 6:17:23the user what happened and because it's
  10280. 6:17:25e. message then the value is going to be
  10281. 6:17:27quite descriptive so the user can read
  10282. 6:17:28it without any
  10283. 6:17:31problems so in this task we want to be
  10284. 6:17:34able to convert a string to an INT
  10285. 6:17:36successfully without any errors
  10286. 6:17:37occurring and without any exceptions
  10287. 6:17:39that could break our code so in steps we
  10288. 6:17:41want to create an INT and try and
  10289. 6:17:43convert any string from the console to
  10290. 6:17:45an integer notice the error that appears
  10291. 6:17:47when you try to do that write a try
  10292. 6:17:49catch handle around it to successfully
  10293. 6:17:51operate that and output the message to
  10294. 6:17:52the screen and you won't need to change
  10295. 6:17:55the console out print and the convert
  10296. 6:17:57in32 to be able to make this work so you
  10297. 6:17:59don't want to change the current code
  10298. 6:18:01you just want to add the triun Handler
  10299. 6:18:03around it so why is a bad situation how
  10300. 6:18:06can we know if it's been converted this
  10301. 6:18:08is just a question that you should keep
  10302. 6:18:09at the back of your head and we'll
  10303. 6:18:10answer this shortly and then finally we
  10304. 6:18:13want to create a custom tri pass
  10305. 6:18:14function find the real trip pass
  10306. 6:18:16function and copy the return Titan
  10307. 6:18:18parameters and see if you can mimic Its
  10308. 6:18:20Behavior read the tool tip it gives you
  10309. 6:18:22to give you ideas on what to do so let's
  10310. 6:18:24take these in steps first we can have a
  10311. 6:18:26console right and say enter a number and
  10312. 6:18:29then Inn equals convert to in32 and then
  10313. 6:18:33we need a console read line
  10314. 6:18:36and then if you run the
  10315. 6:18:38code if we try and just convert some
  10316. 6:18:41square brackets then we get a format
  10317. 6:18:44exception so that's the exception that
  10318. 6:18:46we're going to be handling right now so
  10319. 6:18:48we can take this code and we can wrap it
  10320. 6:18:50around and a
  10321. 6:18:51try use alt and up Arrow key and then
  10322. 6:18:55just after do a catch and then we want
  10323. 6:18:56to do a format exception and then space
  10324. 6:18:58e to give it a variable and then we do
  10325. 6:19:00e. message out to the screen and now
  10326. 6:19:03let's run the code square brackets input
  10327. 6:19:06string was not in a correct format so we
  10328. 6:19:08get our message and our exception has
  10329. 6:19:09been successfully handled and our
  10330. 6:19:11program hasn't crutched but the problem
  10331. 6:19:13is is has got to this console read line
  10332. 6:19:15and the user hasn't had a chance to
  10333. 6:19:17actually enter a number so what we can
  10334. 6:19:18do in here to ensure the user always has
  10335. 6:19:21access to writing a number is we can say
  10336. 6:19:23something like this bu success equals
  10337. 6:19:26false and then just after here we can
  10338. 6:19:28say success equals the
  10339. 6:19:30true and just down here we can say
  10340. 6:19:33console right line so success question
  10341. 6:19:35mark yay and then call on or
  10342. 6:19:40no so if it converts it 15 we should get
  10343. 6:19:43a yay and if we convert it and it fails
  10344. 6:19:45then we should get an o no along with
  10345. 6:19:47the
  10346. 6:19:48message of course what you can also do
  10347. 6:19:50is wrap this entire section inside a
  10348. 6:19:52while loop what we've already covered
  10349. 6:19:53that inside the tutorial and that means
  10350. 6:19:55it'll keep repeating this until this is
  10351. 6:19:57set to true and therefore exit the while
  10352. 6:19:59loop and then will the users finally
  10353. 6:20:01typed in a number so let's go back so
  10354. 6:20:03we've noticed an error and we've brought
  10355. 6:20:04a TR catch handle around it now we want
  10356. 6:20:06to catch the error and output the error
  10357. 6:20:08message with through done and then we
  10358. 6:20:09haven't changed the current code which
  10359. 6:20:11was these two lines we've added a booing
  10360. 6:20:13and set it to True here just so we can
  10361. 6:20:15have a bit of an output message before
  10362. 6:20:17so why is this a bad situation how can
  10363. 6:20:19we know if it's being converted so this
  10364. 6:20:21code is a bad situation because we just
  10365. 6:20:23have these two lines outputed and then
  10366. 6:20:25now that we've added our booing to tell
  10367. 6:20:27the user whether it's been successful or
  10368. 6:20:28not is actually making it a bit more
  10369. 6:20:30meaningful the problem is even if we
  10370. 6:20:32print or no then the user doesn't really
  10371. 6:20:33have a chance to enter it again so like
  10372. 6:20:35I said before you could loop around and
  10373. 6:20:37use a while loop to make sure that this
  10374. 6:20:38situation can go from something that
  10375. 6:20:41gets handled into a situation where the
  10376. 6:20:43user can actually type it in there's no
  10377. 6:20:44point hand on an exception and realizing
  10378. 6:20:46there's an error if you don't give the
  10379. 6:20:48user a chance to enter another value if
  10380. 6:20:50they enter out 15 followed by full stop
  10381. 6:20:53when they only meant to type 15 then you
  10382. 6:20:55need to give them another chance to type
  10383. 6:20:56in that 15 value otherwise the program
  10384. 6:20:59is simply just going to end and
  10385. 6:21:00nothing's going to happen for example 15
  10386. 6:21:03DOT it says or no or you can have
  10387. 6:21:05another message that says something went
  10388. 6:21:07wrong or you can have more specific
  10389. 6:21:09message about the conversion and then
  10390. 6:21:11once you press enter the program simply
  10391. 6:21:12closes this is not an interaction that
  10392. 6:21:14you want you want the US it to be able
  10393. 6:21:16to reselect another value so that's why
  10394. 6:21:18it's a bad situation because you know
  10395. 6:21:20it's being converted or not heuse now
  10396. 6:21:22build success but the problem is we
  10397. 6:21:23don't have any sort of looping so let's
  10398. 6:21:26have a look so now let's comment out
  10399. 6:21:28this and let's see how we can make our
  10400. 6:21:29custom trip pass so you type it in. trip
  10401. 6:21:32pass you can see they have a bu inpass
  10402. 6:21:35string s and out in result so let's try
  10403. 6:21:38and copy that and make our own custom
  10404. 6:21:39function so we say static Bull and it's
  10405. 6:21:42Tri pass and then we have a string input
  10406. 6:21:46and out in
  10407. 6:21:49result perfect so now we can get rid of
  10408. 6:21:52this line just so we use it for
  10409. 6:21:53reference and let's use it straight in
  10410. 6:21:54here and then we can write the
  10411. 6:21:55implementation in a second so we can say
  10412. 6:21:57if trass the thing that we want to pass
  10413. 6:22:00is actually a console read line and then
  10414. 6:22:03we just say out into
  10415. 6:22:06result and the console read line is
  10416. 6:22:08going to be answered to this question
  10417. 6:22:10which is enter a number you place that
  10418. 6:22:12straight inside here and then we say out
  10419. 6:22:13in result so if this has been successful
  10420. 6:22:16we can print out yay again and else if
  10421. 6:22:19it's not successful we can print out or
  10422. 6:22:20not of course you can do other things
  10423. 6:22:22inside here or you can even print out
  10424. 6:22:24the value straight from here so we can
  10425. 6:22:26say yay plus a space and then result
  10426. 6:22:29just so we can get that back to the user
  10427. 6:22:30and then oh know if something goes wrong
  10428. 6:22:32of course this is just so we can test
  10429. 6:22:33things and see how they work so this Tri
  10430. 6:22:36pass function it will take in a string
  10431. 6:22:38input and it'll try and give us back the
  10432. 6:22:40result so in order to be able to make
  10433. 6:22:42this successful function what we need to
  10434. 6:22:44do is essentially do the conversion
  10435. 6:22:45ourself which will be this convert in 32
  10436. 6:22:49so first we need to assign this out int
  10437. 6:22:51so we can say a result equals minus1 so
  10438. 6:22:53now that we've used that we actually
  10439. 6:22:55need to have a return type as well but
  10440. 6:22:56let's worry about that in a second so
  10441. 6:22:58essentially what we actually want to do
  10442. 6:23:00is say that the result is equal to
  10443. 6:23:03convert to in 32 and then we want to
  10444. 6:23:05convert the input store it inside the
  10445. 6:23:07result and if this goes successful then
  10446. 6:23:09we want to return true and if it doesn't
  10447. 6:23:11go successful then we want to return
  10448. 6:23:13false but of course we need to be able
  10449. 6:23:15to do these things separately so like we
  10450. 6:23:17said with the try you only wrap around
  10451. 6:23:19the section of which is going to be
  10452. 6:23:20problematic so in this case as we've
  10453. 6:23:22done here we've wrapped the convert in
  10454. 6:23:2432 inside the try so we're going to need
  10455. 6:23:27to do that down here as well so we can
  10456. 6:23:29open the try right here and place these
  10457. 6:23:31two lines
  10458. 6:23:32inside and then have a catch have a
  10459. 6:23:35little think about what sort of catch do
  10460. 6:23:37we need the whole point in this function
  10461. 6:23:39is to Simply convert value and return
  10462. 6:23:41back whether it's been successful or not
  10463. 6:23:43there is no messages here we just need
  10464. 6:23:46has it been successful yes or no and
  10465. 6:23:49because we only need to know if it's a
  10466. 6:23:50yes or a no then we can just have a
  10467. 6:23:52generic exception and we don't need the
  10468. 6:23:54E variable because we're not going to
  10469. 6:23:56access the message we're going to keep
  10470. 6:23:57it as an exception and instead of
  10471. 6:23:59returning false down here we actually
  10472. 6:24:00want to do this straight inside the
  10473. 6:24:02catch so now the code reads like this
  10474. 6:24:04we set the result to minus one and the
  10475. 6:24:06reason why we do that is because if we
  10476. 6:24:08don't then we're going to get a compiler
  10477. 6:24:10error and it says that not all paths
  10478. 6:24:12will going to assign result
  10479. 6:24:13alternatively we can also assign result
  10480. 6:24:15straight inside here just so we have an
  10481. 6:24:17assignment in both paths or you could
  10482. 6:24:19just leave it outside at the top so we
  10483. 6:24:21assign result to minus one and then we
  10484. 6:24:23place our problematic code straight
  10485. 6:24:24inside here we try and convert the
  10486. 6:24:26result and if this line returns back
  10487. 6:24:28successful then it's going to execute
  10488. 6:24:30the second line and return back true if
  10489. 6:24:32this line returns back an exception it's
  10490. 6:24:34going to miss this line completely and
  10491. 6:24:36jump straight into the exception and
  10492. 6:24:38then return us back with false so let's
  10493. 6:24:40actually try this and see what happens
  10494. 6:24:4215 DOT or no and then 15 y 15 so the
  10495. 6:24:48reason why we don't care about the
  10496. 6:24:49exception in here we can add in
  10497. 6:24:51different values as we've used before
  10498. 6:24:53with format exception overflow exception
  10499. 6:24:55but as we saw with trass it simply just
  10500. 6:24:57returns us back with the bull the bull
  10501. 6:24:59simply tells us if it's been successful
  10502. 6:25:01or not so we could stack these multiple
  10503. 6:25:04times times we can say catch and then we
  10504. 6:25:06have the format exception and then we
  10505. 6:25:08can place a return false in there and
  10506. 6:25:10then we have another catch for the
  10507. 6:25:11Overflow exception and we can place
  10508. 6:25:13another return false in there but as you
  10509. 6:25:15can see we're just returning false
  10510. 6:25:16inside here so now that we've changed
  10511. 6:25:18these it's kind of pointless each of
  10512. 6:25:20them return back false and because we're
  10513. 6:25:22not doing anything very specific we
  10514. 6:25:24don't actually need to chain them all
  10515. 6:25:25together we don't care about the message
  10516. 6:25:28we don't care about exception we only
  10517. 6:25:30care about the fact that something has
  10518. 6:25:32went wrong and if something has went
  10519. 6:25:34wrong then we return back false if
  10520. 6:25:36everything goes okay then we return back
  10521. 6:25:37true and that's all this Tri pass
  10522. 6:25:39function is going to need to do it's
  10523. 6:25:41simply convert it or it doesn't convert
  10524. 6:25:43it using an input and outputting a
  10525. 6:25:46result so if you run it retype in a real
  10526. 6:25:49number we get a number back and then 15
  10527. 6:25:52DOT Z then we get all no because it
  10528. 6:25:54can't convert it there we go we've met
  10529. 6:25:56all our requirements for this
  10530. 6:25:59program let's have a look at how we can
  10531. 6:26:01do debugging using visual studio to to
  10532. 6:26:04step through our code manually so this
  10533. 6:26:06is the example I used last time int age
  10534. 6:26:08equal 35 and ideally in this case you'd
  10535. 6:26:11want something like 30 plus to be
  10536. 6:26:12printed instead of 18 plus even though
  10537. 6:26:1518 plus is still technically true we'd
  10538. 6:26:17still like 30 to be printed in this case
  10539. 6:26:19because this is if it's over 18 do 18 if
  10540. 6:26:22it's over 30 do 30 plus so in order to
  10541. 6:26:25start the debugging process you
  10542. 6:26:26shouldn't run the code first if you run
  10543. 6:26:28the code first it's going to execute so
  10544. 6:26:31fast that you won't get a chance to
  10545. 6:26:32actually put any code in and and try and
  10546. 6:26:34debug it there we go we've already hit
  10547. 6:26:36the console read line and it's executed
  10548. 6:26:38for a spit second so the best thing to
  10549. 6:26:41do just to the left of all the line
  10550. 6:26:44numbers you want to press in the Gap and
  10551. 6:26:46that will create a red dot and it'll
  10552. 6:26:48tell you that this is a debuging red dot
  10553. 6:26:50just in this little tool tip debugging
  10554. 6:26:52do program. men debuging program. men so
  10555. 6:26:56it tells us we're in the program class
  10556. 6:26:57and we'll going inside the main you can
  10557. 6:26:59also address the debug menu up here and
  10558. 6:27:01it says we can have a toggle breakpoint
  10559. 6:27:03we can add new one we can delete them
  10560. 6:27:05all or we can actually disable deleting
  10561. 6:27:08them will just simply get rid of them as
  10562. 6:27:10you can see here if I set a couple more
  10563. 6:27:13and press disable then it's going to
  10564. 6:27:15hide them so when I run our code it
  10565. 6:27:17doesn't actually apply the debugging but
  10566. 6:27:18it doesn't delete them and then we can
  10567. 6:27:20enable them and then we can also delete
  10568. 6:27:22them all and it gives you a little
  10569. 6:27:24prompt because if you're doing this
  10570. 6:27:25inside a big project and you just spent
  10571. 6:27:27a lot of time going through and
  10572. 6:27:28debugging certain functions then you
  10573. 6:27:30don't want to be able to delete them all
  10574. 6:27:32without any confirmation so to delete
  10575. 6:27:33them all and put it back on line 13
  10576. 6:27:35which is our int Edge so let's run the
  10577. 6:27:38code and see what happens so as you can
  10578. 6:27:40see it minimizes our console for us and
  10579. 6:27:42nothing's actually been printed yet
  10580. 6:27:43because we're only up to this line and
  10581. 6:27:45then we can use these buttons up here
  10582. 6:27:47step into goes inside a function step
  10583. 6:27:51over goes over the line so it will go
  10584. 6:27:53from this line to this line step out is
  10585. 6:27:56used after you've stepped in if you
  10586. 6:27:58stepped into a function and then you
  10587. 6:28:00don't want to see the rest of the
  10588. 6:28:01function executing you can step out to
  10589. 6:28:03jump back out of it which I'll
  10590. 6:28:04demonstrate later on in this video so
  10591. 6:28:06now that we've hit this line you can
  10592. 6:28:08actually see what's going on so we have
  10593. 6:28:09int age equals z because when this line
  10594. 6:28:12is highlighted it means this line hasn't
  10595. 6:28:14executed just yet so we can press this
  10596. 6:28:17or we can press F10 on our keyboard if I
  10597. 6:28:20press F10 you can now see int age has
  10598. 6:28:22the value 35 and what you can actually
  10599. 6:28:25do is double click on it and just change
  10600. 6:28:26the value to 30 for example now if you
  10601. 6:28:28highlight you can see the value is 30 so
  10602. 6:28:30you need to double click change the
  10603. 6:28:32value to something and then simply press
  10604. 6:28:34enter if you change the value but leave
  10605. 6:28:36the box like this then it won't change
  10606. 6:28:38the value so you have to remember to
  10607. 6:28:40press enter so now I've made the age 40
  10608. 6:28:42but let's just change it back to 35 to
  10609. 6:28:43keep it the same and then now what we
  10610. 6:28:45can do we can explore what's going on
  10611. 6:28:47here if age is greater than 18 then
  10612. 6:28:51we're going to check if this condition
  10613. 6:28:52is true or false so in order to check if
  10614. 6:28:54this returns back true or false you can
  10615. 6:28:56actually hover over the operator and it
  10616. 6:28:58says here AG is greater than 18 is equal
  10617. 6:29:00to true and if we hover over this one it
  10618. 6:29:03says AG is greater than 30 is equal to
  10619. 6:29:05two notice what we actually did there we
  10620. 6:29:08essentially ran this condition through
  10621. 6:29:10the debugger without actually reaching
  10622. 6:29:12this line we're only up to line 15 as of
  10623. 6:29:14right now but we've managed to execute
  10624. 6:29:16this and give us back a function so as
  10625. 6:29:18you can see age greater than 30 is true
  10626. 6:29:21and age greater than 18 is true and
  10627. 6:29:23because this is lsif chain it only cares
  10628. 6:29:26about the first one that turns back true
  10629. 6:29:28and because this one has turned back
  10630. 6:29:29true then if you press F10 again you'll
  10631. 6:29:32see that now it's went inside this curly
  10632. 6:29:33bra is which will print out the console
  10633. 6:29:35right line if I press F10 again you can
  10634. 6:29:38see we're now waiting on here if I use
  10635. 6:29:40alt tab I can get back to the console
  10636. 6:29:42you see that 18 plus has now been
  10637. 6:29:44printed now that we're waiting here we
  10638. 6:29:46actually press F10 one more time and
  10639. 6:29:48then it jump straight to this read line
  10640. 6:29:50of course because this is L if chain as
  10641. 6:29:52soon as one of these become true and it
  10642. 6:29:54jumps over the else if because of course
  10643. 6:29:56this is a chain the first one it hits
  10644. 6:29:59that's true it skips over the rest if we
  10645. 6:30:01didn't want this to be a chain then we
  10646. 6:30:03could keep them as both independent if
  10647. 6:30:05statements if age is greater than 18 and
  10648. 6:30:08if age is greater than 30 and don't use
  10649. 6:30:10the lse keyword but in this case because
  10650. 6:30:12you do want to Output a different
  10651. 6:30:13message for every single age range you
  10652. 6:30:15do want to place the L's word in there
  10653. 6:30:17and now that we've hit this line we can
  10654. 6:30:19press
  10655. 6:30:20F10 and then now we jump back in the
  10656. 6:30:22console and now we're waiting right here
  10657. 6:30:25and just a little tip when it comes to
  10658. 6:30:26debugging if at any point you just want
  10659. 6:30:28to exit the debuging you can either
  10660. 6:30:30press continue or just press F5 and the
  10661. 6:30:32program will run for example if we
  10662. 6:30:35investigate this variable and we can see
  10663. 6:30:37the value is 35 and then now we're happy
  10664. 6:30:40with the rest of the Coe we can just
  10665. 6:30:42press F5 and our code will run as normal
  10666. 6:30:45we'll get the out print and we'll get a
  10667. 6:30:46read line waiting here so we know our
  10668. 6:30:49problem here is both conditions return
  10669. 6:30:51back true so just like in the last video
  10670. 6:30:53we actually change this condition so to
  10671. 6:30:55make sure that it only goes between this
  10672. 6:30:57range but let's not repeat ourselves
  10673. 6:30:59let's have a look at a different example
  10674. 6:31:00so we can have string name equals Abu
  10675. 6:31:03and and then int age and then age equal
  10676. 6:31:0523 which I'll demonstrate why I've done
  10677. 6:31:07it like this in a second and then let's
  10678. 6:31:09have a function just so I can show you
  10679. 6:31:11how to step in so we can have a static
  10680. 6:31:13string print details string name in
  10681. 6:31:17Edge and here actually we want to just
  10682. 6:31:20return back a void because we just want
  10683. 6:31:22to print out to the screen and we can
  10684. 6:31:23say name space edge and maybe just put a
  10685. 6:31:27dash between them and let's run that
  10686. 6:31:28code now we can see
  10687. 6:31:31aa-23 of course making that function
  10688. 6:31:33wasn't the point I actually want to show
  10689. 6:31:35you what's going on so if we just print
  10690. 6:31:37this a couple of times just so we can
  10691. 6:31:38show you the step out and step in so if
  10692. 6:31:40you run the code now we can see our
  10693. 6:31:42string name if we go over that you can
  10694. 6:31:45see it's called abber and then now that
  10695. 6:31:46it's stepped over this line cuz there's
  10696. 6:31:48nothing really to execute here it's just
  10697. 6:31:50creating back a variable so you can see
  10698. 6:31:52that age is zero cuz that's the default
  10699. 6:31:53value for an integer and when we skip
  10700. 6:31:56over that now it's got the value of 23
  10701. 6:31:58so one line creates the integer with the
  10702. 6:32:00default value equal to zero and then the
  10703. 6:32:02next line will make it equal to 23 and
  10704. 6:32:05then we're passing on the variables here
  10705. 6:32:06and if you wanted to investigate it you
  10706. 6:32:08could simply just hover over them like
  10707. 6:32:09this so you can see what the values
  10708. 6:32:11contain and then now that we've hit this
  10709. 6:32:13function if we press F10 then it's just
  10710. 6:32:16going to skip over it but what we
  10711. 6:32:17actually want to do is be able to go
  10712. 6:32:20inside the function so this is where our
  10713. 6:32:22step in goes step over doesn't care if
  10714. 6:32:25it's a function or a normal line it will
  10715. 6:32:27just step over it regardless so we want
  10716. 6:32:29to in here we want to use step into or
  10717. 6:32:31we can press f11 once we press F 11 you
  10718. 6:32:34can see that now it's jumped from print
  10719. 6:32:36details up here to print details down
  10720. 6:32:38here and then now look if I step through
  10721. 6:32:41it's going to actually execute these one
  10722. 6:32:43by one so right now it hasn't executed
  10723. 6:32:45anything so I step over this once using
  10724. 6:32:47F10 you'll see it now appears and then
  10725. 6:32:49the same thing with this one and this
  10726. 6:32:51one and now that I've executed three we
  10727. 6:32:53should see three in the console perfect
  10728. 6:32:55so now what I want to demonstrate is
  10729. 6:32:57let's say what happy with the rest of
  10730. 6:32:58this function and then now we want to
  10731. 6:33:00just step out so I press this button and
  10732. 6:33:02you can see it jumps back back up to
  10733. 6:33:04print details f11 to go in and then
  10734. 6:33:07shift f11 to go out so now that we're
  10735. 6:33:09back in here the only thing we can do
  10736. 6:33:11really from this point is either press
  10737. 6:33:13F5 to stop debugging or press F10 to
  10738. 6:33:16jump to this line and at this point
  10739. 6:33:17we've already hit the last line now code
  10740. 6:33:19so we don't really need to use F10
  10741. 6:33:21anymore cuz we nothing to step over so
  10742. 6:33:23we can just press F5 and let our program
  10743. 6:33:25finish and there we go so let's have a
  10744. 6:33:27look another example can have a string
  10745. 6:33:29message equals read from console then we
  10746. 6:33:32can pass this in and
  10747. 6:33:34anything and then we can have a console
  10748. 6:33:36right line and we can just print the
  10749. 6:33:37message and then let's create this
  10750. 6:33:39function so because it's a return value
  10751. 6:33:41and start inside a string we're going to
  10752. 6:33:43need a static string read from console
  10753. 6:33:47and then this is one string parameter so
  10754. 6:33:49we have a string
  10755. 6:33:54message and then we can just do a
  10756. 6:33:56console
  10757. 6:33:57right and then print the message to the
  10758. 6:33:59screen and then we just want to return
  10759. 6:34:01the console. readline there we go and
  10760. 6:34:05again let's put a break point here and
  10761. 6:34:07we can remove this one so it doesn't
  10762. 6:34:09always have to be the first line if
  10763. 6:34:11we're perfectly happy with this amount
  10764. 6:34:12of code then we can simply take that off
  10765. 6:34:15and it will stop when it gets after this
  10766. 6:34:18line now if you look at the console
  10767. 6:34:20you'll see all of this is executed that
  10768. 6:34:22function is executed four times to print
  10769. 6:34:24the name and age of the screen and now
  10770. 6:34:25we're waiting here so we have our read
  10771. 6:34:27from console and now we can step into
  10772. 6:34:30using f11 and you can see that if you
  10773. 6:34:32hover over string m message has enter
  10774. 6:34:34anything which is our parameter that we
  10775. 6:34:36specified up here and then there you go
  10776. 6:34:38and then that will print into the screen
  10777. 6:34:40so you tab back in you'll see it says
  10778. 6:34:41enter anything and then just at the read
  10779. 6:34:43line we need to actually enter it
  10780. 6:34:45now
  10781. 6:34:47something and then press enter and then
  10782. 6:34:49just right here it's going to return it
  10783. 6:34:51back so if we use F10 it jumps back in
  10784. 6:34:54here and then actually we need to
  10785. 6:34:56execute this line so F10 again and then
  10786. 6:34:58if you hover over a message you see it
  10787. 6:35:00says something which is exactly what we
  10788. 6:35:02typed in the console here and then we
  10789. 6:35:04just print it back to the screen there
  10790. 6:35:06you go and now we just have the console
  10791. 6:35:08read line and we can press F5 to finish
  10792. 6:35:10up there we go so debugging is mainly
  10793. 6:35:13used by pressing this button on the side
  10794. 6:35:15so debugging is used by pressing on the
  10795. 6:35:17empty Gap just to the left of the line
  10796. 6:35:19number and it'll create a red dot once
  10797. 6:35:21you create a red dot it'll highlight
  10798. 6:35:23this for you once you've clicked on it
  10799. 6:35:24it should show up a red dot you can also
  10800. 6:35:26disable or delete all the bra points
  10801. 6:35:28from the debug window at the top of the
  10802. 6:35:29screen when you run the code it will
  10803. 6:35:31stop here and then you can either use F9
  10804. 6:35:34as a shortcut as well click into the
  10805. 6:35:35line that you want to debug and press F9
  10806. 6:35:37please note there is specific cases
  10807. 6:35:39where if you try and press this one
  10808. 6:35:41right here it's not going to let you if
  10809. 6:35:43the line doesn't really do anything for
  10810. 6:35:45example this is creating an integer
  10811. 6:35:47there's no real action that's happening
  10812. 6:35:49here that you should or want to debug
  10813. 6:35:52this is an assignment this is an
  10814. 6:35:53assignment this is a function call and
  10815. 6:35:55so on and so forth and this is also a
  10816. 6:35:57function call when it comes to just
  10817. 6:35:59declaring variables then a compiler does
  10818. 6:36:02not let you put in a break point because
  10819. 6:36:04there's nothing really to investigate
  10820. 6:36:06when you're just creating a variable so
  10821. 6:36:08don't get thrown off or think
  10822. 6:36:09something's wrong if you click on the
  10823. 6:36:10empty line and nothing appears so that's
  10824. 6:36:12it for debugging but let's just have a
  10825. 6:36:14little recap we can use F10 to step over
  10826. 6:36:18f11 to step into functions shift f11 to
  10827. 6:36:21step out or you can use the buttons that
  10828. 6:36:23appear at the top of the screen when you
  10829. 6:36:25run the code just right
  10830. 6:36:28here hover over variables and you get
  10831. 6:36:30their values as I showed you in the if
  10832. 6:36:32statements if you hover over the
  10833. 6:36:34operator inside the if statement it will
  10834. 6:36:36actually tell you what the condition is
  10835. 6:36:38and whether it's true or false so you
  10836. 6:36:39say if 1 equals equals to one and let's
  10837. 6:36:42just print out true to the screen just
  10838. 6:36:44so I can demonstrate
  10839. 6:36:46this if I hover over the equals equals
  10840. 6:36:48it says 1 equals equals 1 is
  10841. 6:36:53true so that's how you check if a
  10842. 6:36:56condition is going to be true or false
  10843. 6:36:57when you're
  10844. 6:36:58debugging you can check the parameter
  10845. 6:37:00values by simply hovering over it and
  10846. 6:37:02the tool tip should include the
  10847. 6:37:06value just like so and if you wanted to
  10848. 6:37:08step into this function you can use step
  10849. 6:37:10into and then use step over which is F10
  10850. 6:37:14just here and then you can actually
  10851. 6:37:16investigate these values as well or you
  10852. 6:37:18can look at the parameters and once
  10853. 6:37:19you're done with the function you can do
  10854. 6:37:20shift f11 or you can use step out and
  10855. 6:37:24then you can skip over this and the same
  10856. 6:37:26thing with this you can have a look at
  10857. 6:37:27the string variable once it gets
  10858. 6:37:29returned something in here and now it
  10859. 6:37:32says something thing in here and string
  10860. 6:37:34message and then we can print out to the
  10861. 6:37:36screen Etc so you can have a little bit
  10862. 6:37:38of Freedom with what you can do in terms
  10863. 6:37:40of debugging play around to debuging in
  10864. 6:37:42the past exercises that we've made
  10865. 6:37:44together and then what you can actually
  10866. 6:37:45do is gain experience in debugging so it
  10867. 6:37:48comes to more complicated projects you
  10868. 6:37:49can also apply that learning there and
  10869. 6:37:51make sure that your code is error
  10870. 6:37:55free so we covered how to debug by
  10871. 6:37:58pressing the white space just to the
  10872. 6:38:00left of the line number if we run the
  10873. 6:38:02code
  10874. 6:38:04the program will stop where our
  10875. 6:38:05breakpoint is and it won't execute the
  10876. 6:38:07line that we're up to we have to step
  10877. 6:38:09over it so we've actually used this
  10878. 6:38:10before to analyze our functions and see
  10879. 6:38:13what values are inside things so we have
  10880. 6:38:15these extra windows called the local and
  10881. 6:38:17the auto so let's take a look at them
  10882. 6:38:19and see what they're about so again if
  10883. 6:38:21you don't have this layout you can use
  10884. 6:38:23window reset layout and it should be on
  10885. 6:38:25the left panel not the right one this
  10886. 6:38:27time so we've got watch which we'll
  10887. 6:38:29explore later and we're going to have
  10888. 6:38:31locals and autos so the autos because
  10889. 6:38:34we're up to the age then it shows us the
  10890. 6:38:37age has value zero and then that
  10891. 6:38:39prevents us from hovering over it and if
  10892. 6:38:41we step over it you'll see that now it's
  10893. 6:38:43got age 23 and now name has appeared the
  10894. 6:38:46auto tab will try and figure out what
  10895. 6:38:48functions of what parameters you need to
  10896. 6:38:51be displayed down here based on the
  10897. 6:38:53section of Corde that you're currently
  10898. 6:38:54up to so my guess is because we're up to
  10899. 6:38:56this function and it takes in a name and
  10900. 6:38:59age then we're getting name and age
  10901. 6:39:00appearing before when we were on this
  10902. 6:39:02line it was only showing back the age
  10903. 6:39:04and because that section only used the
  10904. 6:39:06age we only got age here but if you
  10905. 6:39:09press locals you can see all the
  10906. 6:39:10variables that are local to this main
  10907. 6:39:12function are actually showing up we have
  10908. 6:39:14a parameter called ARs and technically
  10909. 6:39:16this is a parameter and it's a string
  10910. 6:39:17array type but it's currently unused but
  10911. 6:39:20it's still a parameter so you can see it
  10912. 6:39:22down here and then in order we have the
  10913. 6:39:24name variable down here then we've got
  10914. 6:39:26the age and then we've got the message
  10915. 6:39:28which is currently said to know because
  10916. 6:39:30we haven't created this in memory yet
  10917. 6:39:31but as you can see this changes depend
  10918. 6:39:34on the scope of our variables so now
  10919. 6:39:36that we're about to hit this function
  10920. 6:39:37let's use the step into and you'll see
  10921. 6:39:40that the locals have now changed we've
  10922. 6:39:42got rid of name age argument and string
  10923. 6:39:44message and it's replaced with name and
  10924. 6:39:46age because remember what I was saying
  10925. 6:39:48about the scope of the variables
  10926. 6:39:49parameters inside a function are always
  10927. 6:39:51going to be local to that function
  10928. 6:39:53around the curly braces that they're
  10929. 6:39:54surrounded by name and age is only
  10930. 6:39:57within here so when it's trying to shows
  10931. 6:39:59the local variables it's only local to
  10932. 6:40:01what's inside these curly braces which
  10933. 6:40:03in this case is name and age so now if
  10934. 6:40:06we step over
  10935. 6:40:07this and then if we step into this read
  10936. 6:40:10from console you'll see that just before
  10937. 6:40:12we step into this function that these
  10938. 6:40:13have all came back cuz again the scope
  10939. 6:40:15has changed back to this function which
  10940. 6:40:18is our main function which has all these
  10941. 6:40:20variables inside so our read from
  10942. 6:40:22console will only have our string
  10943. 6:40:24message so hopefully when we press f11
  10944. 6:40:26or press this step into button at the
  10945. 6:40:28top we should only see the message
  10946. 6:40:30variable inside here there we go we get
  10947. 6:40:33into anything cuz that's what was passed
  10948. 6:40:34in over here and then as soon as we
  10949. 6:40:37return back to
  10950. 6:40:40that let we type something in and then
  10951. 6:40:43we get all of the values again and you
  10952. 6:40:45can see that system. console. rine had
  10953. 6:40:47returned hello which is this line and
  10954. 6:40:50then if we press F10 again you'll see
  10955. 6:40:51that even though we've got it back here
  10956. 6:40:53our message is about to be assigned once
  10957. 6:40:55we step over this there you go it says
  10958. 6:40:57hello and as you can see this changes in
  10959. 6:41:00real time so as soon as we step over
  10960. 6:41:02lines hello got play straight into there
  10961. 6:41:04this helps us tremendously because we
  10962. 6:41:06don't have to keep hovering over
  10963. 6:41:07variables like this to keep checking
  10964. 6:41:09over their values you can simply use
  10965. 6:41:11this menu and it will always update
  10966. 6:41:13because whenever you change Scopes
  10967. 6:41:15between functions that will
  10968. 6:41:16automatically update as you just saw in
  10969. 6:41:18them examples there we go and then when
  10970. 6:41:20the program ends obviously this window
  10971. 6:41:22is going to close down so that's a very
  10972. 6:41:23helpful method of being able to debug
  10973. 6:41:25without having to hover over anything in
  10974. 6:41:27the next video we're going to cover how
  10975. 6:41:29to watch variables and this can be very
  10976. 6:41:31handy when you're looking for specific
  10977. 6:41:32areas
  10978. 6:41:37another really good debuging technique
  10979. 6:41:38is the watch window let's press a break
  10980. 6:41:40point just of left of line 13 and let's
  10981. 6:41:43run the code and let's see what this is
  10982. 6:41:44about so we covered that we have the
  10983. 6:41:47locals window and we have the auto
  10984. 6:41:49window that will automatically give us
  10985. 6:41:50the variables that we're currently in
  10986. 6:41:52the scope of but let's forget about that
  10987. 6:41:54right now let's have a look at the watch
  10988. 6:41:55window it says here add an item to watch
  10989. 6:41:59so there's a couple ways that we can do
  10990. 6:42:00this you can actually double click on a
  10991. 6:42:02variable and write click or you can just
  10992. 6:42:04highlight it like this and then right
  10993. 6:42:06click and it says here add watch and you
  10994. 6:42:08can press it it says name value null
  10995. 6:42:10because we haven't created it yet so the
  10996. 6:42:12default value is null as of right now
  10997. 6:42:14cuz it's not actually assigned anything
  10998. 6:42:16so if you press F10 and step over it
  10999. 6:42:18you'll see that immediately changes to
  11000. 6:42:20abber without was having to do anything
  11001. 6:42:22except simply press F10 which is the
  11002. 6:42:24shortcut for our step over button right
  11003. 6:42:26at the top of the screen this prevents
  11004. 6:42:28us from having to hold over the
  11005. 6:42:29variables to get the little tool tip so
  11006. 6:42:31we can see the value the of this is we
  11007. 6:42:33can watch variables and we don't have to
  11008. 6:42:35worry about showing in the locals or the
  11009. 6:42:37auto because it's always there and if
  11010. 6:42:39you have lots of variables like this
  11011. 6:42:41then you're only concerned about the one
  11012. 6:42:43that you're watching locals and aluto
  11013. 6:42:45will always change depending on the
  11014. 6:42:46scope that you currently at whereas
  11015. 6:42:48watch will only do what you want it to
  11016. 6:42:50do so because we don't put age inside
  11017. 6:42:53there then it simply won't come up just
  11018. 6:42:54because it likes it if we check the
  11019. 6:42:56locals we have agent there and the Autos
  11020. 6:42:59we have agent there as well but we don't
  11021. 6:43:00have it in the watch so what else you
  11022. 6:43:02can you do is actually just type
  11023. 6:43:04straight into here age
  11024. 6:43:0723 if you really wanted to you can
  11025. 6:43:09actually also type in conditions age
  11026. 6:43:11equals equals 23 it returns back true so
  11027. 6:43:14if you had an if statement that you
  11028. 6:43:15wanted to track then you could do that
  11029. 6:43:17as well so let's have a
  11030. 6:43:20look so had this statement that said if
  11031. 6:43:23age is greater than 18 then we can just
  11032. 6:43:25print out 18 plus to the screen so let's
  11033. 6:43:27run the code now and see what happens so
  11034. 6:43:29as you know we have this condition right
  11035. 6:43:31here so if you actually highlight this
  11036. 6:43:32and and drop it you can see that the
  11037. 6:43:34cursor changes and you can drop it
  11038. 6:43:36inside here let's click on the age equal
  11039. 6:43:38equal 23 and click delete so of course
  11040. 6:43:41we can't really render this function
  11041. 6:43:43right now because we haven't even
  11042. 6:43:44created the integer variable so let's
  11043. 6:43:46run over the integer variable and as you
  11044. 6:43:48can see here it says AG is zero which is
  11045. 6:43:51the default value for integer and that
  11046. 6:43:53is indeed zero right now and then AG is
  11047. 6:43:55greater than 18 is false but as soon as
  11048. 6:43:58I step over this line and it makes age
  11049. 6:44:00the value of 23 you'll see that these
  11050. 6:44:02two immediately change you know they
  11051. 6:44:03change because the values have went red
  11052. 6:44:06red means that the values have just
  11053. 6:44:07changed in the last button that you
  11054. 6:44:08pressed whether that means you stepped
  11055. 6:44:10into over or stepped out it doesn't
  11056. 6:44:12matter red always dictates that the
  11057. 6:44:14value is just changed so now it's
  11058. 6:44:16telling us that age is greater than 18
  11059. 6:44:18is returned back true so when I step
  11060. 6:44:20over this line because this is returned
  11061. 6:44:22back true I should render 18 plus let's
  11062. 6:44:25have a look F10 F10 and there we go
  11063. 6:44:28we've reached the middle which means
  11064. 6:44:29that's going to get printed out
  11065. 6:44:31now so so moving on to another example
  11066. 6:44:34we can have print details if we go into
  11067. 6:44:36this function and let's say we're only
  11068. 6:44:38concerned about the name then we can
  11069. 6:44:39print out the name down here but as you
  11070. 6:44:41can see we already have it twice so
  11071. 6:44:43that's just going to update depending on
  11072. 6:44:45the parameter name and even if you
  11073. 6:44:46wanted to show something like this you
  11074. 6:44:48can drag this down here and it actually
  11075. 6:44:50shows you the output this is very handy
  11076. 6:44:52so you can see the output because
  11077. 6:44:54sometimes when you have it like this it
  11078. 6:44:55doesn't look very clear you can also
  11079. 6:44:58hover over it right there and you can
  11080. 6:44:59see the value placed here but again it's
  11081. 6:45:02very handy to have it here so you don't
  11082. 6:45:04have to keep hovering over the tool tape
  11083. 6:45:06just to see the values and from here you
  11084. 6:45:08can actually change the values so we can
  11085. 6:45:10just say Harry is now the value for the
  11086. 6:45:12name and Harry is going to be age 35 so
  11087. 6:45:16when we press F5 and let the program run
  11088. 6:45:18naturally you'll see that Harry is age
  11089. 6:45:2035 now and that's all done through the
  11090. 6:45:22watch list down here and it doesn't even
  11091. 6:45:24matter because we have full control over
  11092. 6:45:27the watch list variables and we can
  11093. 6:45:28change their contents to see how our
  11094. 6:45:30code acts in different situations so
  11095. 6:45:33please use this watch list as much as
  11096. 6:45:34you can and don't forget that once you
  11097. 6:45:36can double click on variables and add it
  11098. 6:45:38to the watch just from here you can also
  11099. 6:45:41say quick watch and what that'll let you
  11100. 6:45:43do is it'll give you the value and then
  11101. 6:45:45if you want to add it you can press add
  11102. 6:45:47watch here or you can just keep pressing
  11103. 6:45:49re-evaluate once you've skipped over
  11104. 6:45:51some lines so let's say we skip over it
  11105. 6:45:53now and then go back onto it and because
  11106. 6:45:56it has a shortcut shift F9 we can just
  11107. 6:45:58quickly press it like this and it says
  11108. 6:46:00Alber inside here and we can change the
  11109. 6:46:02expression
  11110. 6:46:03maybe do something like age and then
  11111. 6:46:05click re-eval it and it changes the
  11112. 6:46:07value to zero and then we can even
  11113. 6:46:09change this to like 50 for example as
  11114. 6:46:11you can see down here it's changed to 50
  11115. 6:46:13as well so you can drag and drop it into
  11116. 6:46:16here or you can double click and press
  11117. 6:46:18add watch it's up to you please make use
  11118. 6:46:20of this watch variable and don't rely
  11119. 6:46:22too much on the autos and the local just
  11120. 6:46:24so you can keep your own variables
  11121. 6:46:25inside here and you can have complicate
  11122. 6:46:27outputs like these you can check
  11123. 6:46:29conditions you can check console outputs
  11124. 6:46:31as well as just checking normal
  11125. 6:46:32variables so using the watch window you
  11126. 6:46:35have lots of different opportunities to
  11127. 6:46:37keep track of your
  11128. 6:46:40code so we've got another debugging task
  11129. 6:46:42let's try and run the code and see what
  11130. 6:46:45happens we're going to get printed over
  11131. 6:46:4718 so our value is agal 35 and we're
  11132. 6:46:50already getting over 18 printed to the
  11133. 6:46:52screen so if we place a Brak line inside
  11134. 6:46:55here and actually run the code we can
  11135. 6:46:56see what's going on we can add them
  11136. 6:46:58inside the watch just like this we can
  11137. 6:47:01add in all the conditions inside
  11138. 6:47:04just like
  11139. 6:47:05this perfect so we can actually see here
  11140. 6:47:08that because our age is over 35 it's
  11141. 6:47:11saying greater than 18 is true greater
  11142. 6:47:13than 30 is true but greater than 50
  11143. 6:47:15isn't true and we can easily see that
  11144. 6:47:17except because this is an if and an LSI
  11145. 6:47:19chain as soon as the first one is true
  11146. 6:47:21it's going to enter that one you press
  11147. 6:47:23F10 then we get over 18 and then as soon
  11148. 6:47:26as it finishes that path it actually
  11149. 6:47:28skips all the way to the end the problem
  11150. 6:47:30with this is we only want it to print
  11151. 6:47:32over 30 since it's over 30 so we have to
  11152. 6:47:35introduce ranges inside here and we can
  11153. 6:47:37say age is less than 30 and then you can
  11154. 6:47:40have AG is greater than 30 here and then
  11155. 6:47:42you can also insert an and and say age
  11156. 6:47:44is less than 50 and there we go and now
  11157. 6:47:47it says over
  11158. 6:47:4930 and if we change this to 55 then we
  11159. 6:47:52should get over 50 and then finally if
  11160. 6:47:54we change this to something like 23 then
  11161. 6:47:57we should also get over 18 perfect so
  11162. 6:48:00what was going on here is because we had
  11163. 6:48:02if El if chain then you're only going to
  11164. 6:48:04get one of them printed and because a
  11165. 6:48:06value over 18 satisfied both the first
  11166. 6:48:09path and the second path then it's only
  11167. 6:48:11going to print the first path if we
  11168. 6:48:13actually undo our changes all the way
  11169. 6:48:15till the start and then we actually flip
  11170. 6:48:17the order of this if we put our this one
  11171. 6:48:19first and delete the else and change it
  11172. 6:48:22to lse if inside here so now we have a
  11173. 6:48:24new chain except the first one in the
  11174. 6:48:26chain is actually if age is greater than
  11175. 6:48:2830 now if you're on the code you'll see
  11176. 6:48:31that the first one actually says over 30
  11177. 6:48:3230 and it says over 30 and it doesn't
  11178. 6:48:35say over 18 anymore but simply because
  11179. 6:48:37this is the first one in the list as
  11180. 6:48:39soon as it hits the first one as I'll
  11181. 6:48:41demonstrate now it will go inside this
  11182. 6:48:43bracket hit this curly brace and then
  11183. 6:48:45jump straight to the
  11184. 6:48:46end you should always order them from
  11185. 6:48:49smallest to biggest because that will
  11186. 6:48:51probably make the most logical sense
  11187. 6:48:53when you're reviewing your code and in
  11188. 6:48:55doing so you can actually have a more
  11189. 6:48:57formatted code but you need to watch out
  11190. 6:48:59that the ranges need to make sense if
  11191. 6:49:01you have something like this then you
  11192. 6:49:03need to restrict this one because it's
  11193. 6:49:04going to be blocking the second one and
  11194. 6:49:06you don't want to make these all ifs
  11195. 6:49:08because then you get multiple outputs
  11196. 6:49:10now we should get 18 and 30 being
  11197. 6:49:12printed to the screen this is not
  11198. 6:49:14something you want so make sure that you
  11199. 6:49:16place your ranges inside here by adding
  11200. 6:49:18an and and then placing the other end of
  11201. 6:49:20it which will be 30 and then this one
  11202. 6:49:22will continue from it so just to sum up
  11203. 6:49:24what you need to do is make sure that
  11204. 6:49:25you use l ifs and make sure that you
  11205. 6:49:28have ranges just so when one exits it's
  11206. 6:49:31going to check the other one and the
  11207. 6:49:32other one you don't want the first
  11208. 6:49:34condition to beat the second one if
  11209. 6:49:36that's for the right path age 35 goes
  11210. 6:49:39into this path or at least we want it to
  11211. 6:49:41but it's been restricted by the fact
  11212. 6:49:43that age is greater than 18 when we had
  11213. 6:49:45it like
  11214. 6:49:46this this is restricting everything over
  11215. 6:49:4918 to go inside this path if this was
  11216. 6:49:51even like 65 we should only fall into
  11217. 6:49:54this one and we run the code then we're
  11218. 6:49:56still going to get over 18 which is
  11219. 6:49:58still technically true it is still over
  11220. 6:50:0018 but we have different outputs for
  11221. 6:50:02this sort of age range make sure that
  11222. 6:50:03you use El ifs and you keep in an
  11223. 6:50:05appropriate range and keep them in the
  11224. 6:50:07order of the if statements going from
  11225. 6:50:09lowest to highest or whatever makes
  11226. 6:50:11sense in that situation that you're
  11227. 6:50:12using
  11228. 6:50:13it this is a start of objectoriented
  11229. 6:50:16programming let's start with a concept
  11230. 6:50:17called structures structures allow us to
  11231. 6:50:20store different data types inside one
  11232. 6:50:22data type so it's almost like a group of
  11233. 6:50:25data types we can demonstrate this if
  11234. 6:50:26you wanted to stall something like a
  11235. 6:50:28person you would normally have something
  11236. 6:50:29like a string name equals ABA ear it in
  11237. 6:50:32age equal 23 so in here we have a string
  11238. 6:50:35name that represents the name and inage
  11239. 6:50:38which represents their age and you can
  11240. 6:50:39see that because these are two different
  11241. 6:50:41data types you can't wrap them around
  11242. 6:50:43inside an array you can't have a string
  11243. 6:50:45array called person and then say we want
  11244. 6:50:47to have two different values although
  11245. 6:50:49this will still compile we can't store
  11246. 6:50:51both these data types inside our array
  11247. 6:50:53unless you make the age and store it as
  11248. 6:50:55a string which will be 23 inside the
  11249. 6:50:58quotation marks and then you'll have to
  11250. 6:51:00worry about converting it later on in
  11251. 6:51:01your program so this is not really ideal
  11252. 6:51:04so we have something called a structure
  11253. 6:51:06so just outside here we can say struct
  11254. 6:51:09which stands for structure and then we
  11255. 6:51:11can have our name straight after so we
  11256. 6:51:13can say person and the next time we just
  11257. 6:51:15open the curly braces and let's see what
  11258. 6:51:16we've just created so now in order to
  11259. 6:51:18make an instance of this we can actually
  11260. 6:51:20use that like a data type so we can say
  11261. 6:51:22person person there we go so now we've
  11262. 6:51:24created our person variable now we see
  11263. 6:51:26we've got our own data type before we
  11264. 6:51:28were just using string and ins and
  11265. 6:51:30arrays and things like that but now we
  11266. 6:51:32actually have a new person so because
  11267. 6:51:34our person is made up of a name and an
  11268. 6:51:36age let's place that in here string name
  11269. 6:51:39and then int age of course you don't
  11270. 6:51:41want to give them default values because
  11271. 6:51:43like all variables you don't know what
  11272. 6:51:45the value is going to be to begin with
  11273. 6:51:47so now what we can do person Dot and we
  11274. 6:51:50don't actually have access to using
  11275. 6:51:52these values yet so what we're going to
  11276. 6:51:53have to do there is a concept called
  11277. 6:51:55access modifiers which we'll cover on
  11278. 6:51:57later on in this course but just for now
  11279. 6:52:00what this means is you either have
  11280. 6:52:01private or public public and they're the
  11281. 6:52:03main two words there's other words that
  11282. 6:52:04you can have but they're the main two
  11283. 6:52:06for now if you have it private it means
  11284. 6:52:08you can't access it down here through
  11285. 6:52:10person. name and but as soon as you make
  11286. 6:52:12this
  11287. 6:52:13public then now it becomes
  11288. 6:52:17accessible there we go we can make that
  11289. 6:52:19equal to something and if we take off
  11290. 6:52:23public then you see we don't have access
  11291. 6:52:25to it it's inaccessible due to its
  11292. 6:52:28protection level because the protection
  11293. 6:52:29level is private which means that you
  11294. 6:52:31can only access name Within These two
  11295. 6:52:33curly braces the second you make it
  11296. 6:52:35public it means that anyone outside of
  11297. 6:52:37the structure EG down here can also have
  11298. 6:52:39access to name so if we do that to our
  11299. 6:52:41public on in age and string name then we
  11300. 6:52:44actually can access both now we can say
  11301. 6:52:46person. Ag and it comes up like this the
  11302. 6:52:49purple cube is a function and the blue
  11303. 6:52:51is Fields you can actually fil them out
  11304. 6:52:53by pressing them down here show only
  11305. 6:52:55methods and show only Fields so we can
  11306. 6:52:58do age equals 23 and we can actually
  11307. 6:53:00just place abber inside here
  11308. 6:53:03and now that this data is essentially
  11309. 6:53:04the same as this the benefit that we
  11310. 6:53:06have now is when we print to the screen
  11311. 6:53:08we can say person. name and then hyphen
  11312. 6:53:11person. age and if you run that
  11313. 6:53:15now we get Abid
  11314. 6:53:1723 if you actually copy and paste is
  11315. 6:53:20lined out and just remove person dot
  11316. 6:53:22just so we're using these two variables
  11317. 6:53:24at the top of here we in the code you
  11318. 6:53:26should see exactly the same values
  11319. 6:53:27coming up there we go but now what's
  11320. 6:53:30really handy about this situation is we
  11321. 6:53:32have two data types that are wrapped
  11322. 6:53:34inside of one data type instead of
  11323. 6:53:36having a name and age we have a person
  11324. 6:53:39because what gets really easy now is if
  11325. 6:53:40you wanted to add something else inside
  11326. 6:53:42here like for example birth month which
  11327. 6:53:45for me is five so in here you'd have to
  11328. 6:53:47make another one birth month equals 5
  11329. 6:53:50and then all we have to do inside our
  11330. 6:53:52person is just say person. birth month
  11331. 6:53:55and we can just assign it right here
  11332. 6:53:56instead of having to create a whole new
  11333. 6:53:58variable we add it up here and then we
  11334. 6:54:00can use it down here and then we can add
  11335. 6:54:01that into here person. birthmonth
  11336. 6:54:06make add this to this one and then get
  11337. 6:54:08rid of birth month and as you can see
  11338. 6:54:10it's exactly the
  11339. 6:54:12same so we've taken these data types and
  11340. 6:54:14placed it inside our structure so then
  11341. 6:54:16we can just reference it by the person
  11342. 6:54:19this becomes very very handy for this
  11343. 6:54:21exact reason that I'm going to show you
  11344. 6:54:22now let's say you wanted to read this
  11345. 6:54:24data in from the user so you have static
  11346. 6:54:27and then you're a bit stuck on what data
  11347. 6:54:28type you want to return if you forget
  11348. 6:54:30about the birth month then we have a
  11349. 6:54:32name and age is a very basic person but
  11350. 6:54:34our return type only lets us have one
  11351. 6:54:36type so that's going to be a problem now
  11352. 6:54:38so you could do a string return that
  11353. 6:54:40will give us back the name and then we
  11354. 6:54:42can say return person and inside the
  11355. 6:54:45brackets what you can actually do is do
  11356. 6:54:47out in Edge so you can have two output
  11357. 6:54:49variables an out age and then a return
  11358. 6:54:52string so inside here we could
  11359. 6:54:55say enter your name and then string name
  11360. 6:54:59equals console. readline
  11361. 6:55:02if we copy and paste this down and then
  11362. 6:55:04we can say enter your age and then we
  11363. 6:55:06can say int age equals convert to in 32
  11364. 6:55:10and then put the console read line
  11365. 6:55:12inside there there we go now that we've
  11366. 6:55:14got that we don't actually need in age
  11367. 6:55:16because we've already got output
  11368. 6:55:17parameter so we just need to be age and
  11369. 6:55:20then after we've retrieved them two
  11370. 6:55:21values we can return name there we go so
  11371. 6:55:25let's actually try that up here so we
  11372. 6:55:28can say new name is equal to return
  11373. 6:55:32person and then out into new age and you
  11374. 6:55:35can take this exact line paste it down
  11375. 6:55:38here get rid of the birth month and then
  11376. 6:55:40we can use new age in here and new name
  11377. 6:55:42inside here so let's run that now ask us
  11378. 6:55:46what is your name Abba what is your age
  11379. 6:55:4823 and then we got Abid 23 which is the
  11380. 6:55:50same as what we've got before but you
  11381. 6:55:52see that this situation is pretty messy
  11382. 6:55:54we have to return a string variable and
  11383. 6:55:56also use an output in order to be able
  11384. 6:55:58to give back two values alternatively
  11385. 6:56:00what we could do is actually just turn
  11386. 6:56:02back void and then we can say string
  11387. 6:56:04name and then inage but we're going to
  11388. 6:56:07have to use references inside here and
  11389. 6:56:10then have string new name equals nothing
  11390. 6:56:13and then new inage equals zero so we
  11391. 6:56:17have string new name equals nothing and
  11392. 6:56:19then into New Age also equal to nothing
  11393. 6:56:21and then inside our function instead of
  11394. 6:56:23having out parameters we can actually
  11395. 6:56:25use a reference and we can say new name
  11396. 6:56:27and then ref New Age and what that will
  11397. 6:56:30do is we can just return it like this
  11398. 6:56:32because it's void instead of declaring a
  11399. 6:56:34new string name we can actually just
  11400. 6:56:35assign it to the reference that just
  11401. 6:56:37passed and because it's a void parameter
  11402. 6:56:39now and for the return type we don't
  11403. 6:56:40actually need to return anything so we
  11404. 6:56:42can actually assign these by reference
  11405. 6:56:44enter your name abber and 23 so as you
  11406. 6:56:47can see it's still works fine but let's
  11407. 6:56:49say we wanted to add another birth month
  11408. 6:56:51so we' have to take the birth month
  11409. 6:56:52variable new birth month make it equal
  11410. 6:56:55to zero and then add yet another
  11411. 6:56:58parameter ref new birth month and then
  11412. 6:57:02we need a new birth month inside
  11413. 6:57:04here ref int birth month and then we
  11414. 6:57:09need another one from here enter your
  11415. 6:57:12birth
  11416. 6:57:12month and then we'd store it inside
  11417. 6:57:15birth month and then finally we can also
  11418. 6:57:18return it up
  11419. 6:57:20here as new birth
  11420. 6:57:22month Here We Go abber 23 and then five
  11421. 6:57:26so we get them values being returned so
  11422. 6:57:28in order to keep adding different
  11423. 6:57:29properties to your code you have to make
  11424. 6:57:30a new parameter and reference variable
  11425. 6:57:33and then append the parameters inside
  11426. 6:57:35the function now the problem that is
  11427. 6:57:37even though you can have an infinite
  11428. 6:57:38amount of parameters then this gets very
  11429. 6:57:40tedious very quickly and you don't want
  11430. 6:57:42this so let's have a look at how we can
  11431. 6:57:44make this function exactly the same way
  11432. 6:57:46just using our new structure you can see
  11433. 6:57:50that instead of having to pass all of
  11434. 6:57:51these different things in or return back
  11435. 6:57:53a certain value we can actually just
  11436. 6:57:55return back a complete person so all of
  11437. 6:57:58this can be squished pretty quietly so
  11438. 6:58:01actually let's just comment all of this
  11439. 6:58:04out and let's comment out this function
  11440. 6:58:06and let's have another
  11441. 6:58:08rewrite so now that everything's gone we
  11442. 6:58:10only want to be returning and dealing
  11443. 6:58:12with the person so we can say static and
  11444. 6:58:15instead of void we actually want to say
  11445. 6:58:17we want to give the user back a person
  11446. 6:58:19return person and we don't need any
  11447. 6:58:21parameters in this case this is only
  11448. 6:58:24passed in because it's a reference so we
  11449. 6:58:26can assign these values back to the
  11450. 6:58:28original variables but because we're
  11451. 6:58:30going to be returning a person then we
  11452. 6:58:31don't need to do that so we can
  11453. 6:58:33essentially take this code and put it in
  11454. 6:58:35here and we can say string name now we
  11455. 6:58:38have an INT here and then also an in
  11456. 6:58:40here there we go so now that we've
  11457. 6:58:43constructed all our variables that we
  11458. 6:58:45need for the person what we can do is we
  11459. 6:58:47can make a new person person person and
  11460. 6:58:50then person. name equals name and then
  11461. 6:58:53duplicate this down we say person. AG is
  11462. 6:58:56equal to age and finally the birth month
  11463. 6:58:58is equal to the birth
  11464. 6:59:00month and then now that we've
  11465. 6:59:02constructed our person we still got a
  11466. 6:59:04red underline because we haven't
  11467. 6:59:05returned anything yet and after all of
  11468. 6:59:07that we can say return
  11469. 6:59:09person there we
  11470. 6:59:11go and now simply in here we could just
  11471. 6:59:14say person person equals return person
  11472. 6:59:19there we go and then we can take this
  11473. 6:59:22console right line here and place it
  11474. 6:59:24straight underneath since it uses the
  11475. 6:59:25same properties now let's try and run it
  11476. 6:59:28h
  11477. 6:59:30235 there you go so now instead of
  11478. 6:59:32having to make a whole new variable all
  11479. 6:59:35you have to do is simply just add
  11480. 6:59:36another one down here so we can just say
  11481. 6:59:38number this could be your favorite
  11482. 6:59:40number or anything and we can add it
  11483. 6:59:42onto
  11484. 6:59:45here person.
  11485. 6:59:47number and then here we can paste this
  11486. 6:59:50down we can say enter your number and
  11487. 6:59:53then it'll be number in here we can
  11488. 6:59:56paste it down here and change this to
  11489. 6:59:58number and there we go that's all we
  11490. 7:00:00need to change and this C doesn't change
  11491. 7:00:02cuz it's simply calling back the
  11492. 7:00:04function and we're doing all the hard
  11493. 7:00:05work inside here so now if you run this
  11494. 7:00:08enter your name AB 23 5 and finally four
  11495. 7:00:13and there you go we get the values being
  11496. 7:00:16returned so this looks a little bit
  11497. 7:00:18tedious we have to assign each one
  11498. 7:00:20individually every single time we do it
  11499. 7:00:22so we can actually use something called
  11500. 7:00:24a Constructor in order to achieve this
  11501. 7:00:26result we make the person we set them
  11502. 7:00:28and then we return it it'd be easier if
  11503. 7:00:30we could have the Constructor that we
  11504. 7:00:32can assign straight inside here and then
  11505. 7:00:34just return it immediately instead of
  11506. 7:00:36having to do all of these lines and then
  11507. 7:00:37reference each one individually so let's
  11508. 7:00:40have a look and see what we can do
  11509. 7:00:42inside here we can have a new function
  11510. 7:00:44that'll just be called whatever the
  11511. 7:00:46structure is called then you open the
  11512. 7:00:47brackets close the brackets for now and
  11513. 7:00:49this is called a Constructor in order to
  11514. 7:00:51be able to use it down here we need to
  11515. 7:00:53make it public in order for someone else
  11516. 7:00:55to access it so this says that it needs
  11517. 7:00:57explicit parameters in order to be able
  11518. 7:01:00to compile it says that it needs to
  11519. 7:01:02assign name age birth month and number
  11520. 7:01:04so let's see how we can do that as we've
  11521. 7:01:06used Constructors in the past for
  11522. 7:01:09example when we had things like the
  11523. 7:01:11string Builder we had s SP equals new
  11524. 7:01:13string Builder and when you make a new
  11525. 7:01:15something that means you make a new
  11526. 7:01:17Constructor so all of these sixes are
  11527. 7:01:20the parameters that go inside of this
  11528. 7:01:22but each one of these is a separate
  11529. 7:01:23Constructor if we control click this we
  11530. 7:01:25should be able to see them all there we
  11531. 7:01:27go you see that the class is called
  11532. 7:01:29string Builder and this is called string
  11533. 7:01:31Builder and these two names match
  11534. 7:01:33exactly so to make a string Builder you
  11535. 7:01:35can give it any one of these as long as
  11536. 7:01:37your parameters match one of these six
  11537. 7:01:40you can give it nothing or you can give
  11538. 7:01:42it some other values and that's the same
  11539. 7:01:44is true with our current
  11540. 7:01:46person so what we need to do in this
  11541. 7:01:48case is we don't have multiple ways that
  11542. 7:01:51we can construct this in the case of our
  11543. 7:01:53person we have to provide all of these
  11544. 7:01:55details otherwise the compiler is not
  11545. 7:01:57going to like it so we can have a string
  11546. 7:01:59name int age int birth mon
  11547. 7:02:02and in number now we got our parameters
  11548. 7:02:05set up we actually need to make use of
  11549. 7:02:06them in
  11550. 7:02:07here so because we have name and name
  11551. 7:02:11and they're exactly the same string by
  11552. 7:02:13string then here we can't say name
  11553. 7:02:15equals name in terms of global and local
  11554. 7:02:18variables this is global within the
  11555. 7:02:20structure as it's within these two Co
  11556. 7:02:22braces and all of these variables are
  11557. 7:02:24only Within These two curly braces local
  11558. 7:02:27variables always take presidence over
  11559. 7:02:29the main one if I double click on this
  11560. 7:02:31you'll see that all three are
  11561. 7:02:33referencing this Constructor if I double
  11562. 7:02:35click on this you'll see that both name
  11563. 7:02:37references on line 20 down here is all
  11564. 7:02:40referencing this one in the parameter
  11565. 7:02:42and not the one up top we want it to be
  11566. 7:02:45assigned into here so you have two
  11567. 7:02:46options in this case you could
  11568. 7:02:48capitalize all of these so then you
  11569. 7:02:50don't run into this issue and then
  11570. 7:02:52instead of name equals name it's capital
  11571. 7:02:54N equals name and that fixes it
  11572. 7:02:56alternatively if you want it to be a
  11573. 7:02:58lowercase then what you can simply do is
  11574. 7:03:00you can place this Dot
  11575. 7:03:03if you use this it goes back to the
  11576. 7:03:04current scope and the current scope of
  11577. 7:03:06name is going to be this which is within
  11578. 7:03:09the
  11579. 7:03:10structure when you use this it doesn't
  11580. 7:03:12reference the local variables it
  11581. 7:03:14references the global variables up here
  11582. 7:03:16so when I double click on this now
  11583. 7:03:17you'll see that that gets highlighted so
  11584. 7:03:19we can say this. age equals age this.
  11585. 7:03:22birth month equals birth month and
  11586. 7:03:24finally this do number equals number so
  11587. 7:03:27we're just getting all the parameters
  11588. 7:03:29from here and putting them into these
  11589. 7:03:31values this might look a little bit
  11590. 7:03:33redundant but I'll show you why this is
  11591. 7:03:34a lot
  11592. 7:03:35cleaner so back in our return person
  11593. 7:03:37function we have the variable and then
  11594. 7:03:40we assign them all manually but look how
  11595. 7:03:42easy this now has became if I comment
  11596. 7:03:44out this return we can say a return
  11597. 7:03:46person so now we can reference person
  11598. 7:03:48with a capital P this time cuz I want to
  11599. 7:03:50reference the structure directly so
  11600. 7:03:52after the person we can open the
  11601. 7:03:53brackets and then start to type in so
  11602. 7:03:56our first one is name age and the birth
  11603. 7:03:57month name comma age comma birth month
  11604. 7:04:01comma and then number comma and then
  11605. 7:04:02this is not going to like it because we
  11606. 7:04:04also need to place new
  11607. 7:04:09inside as I said before when you making
  11608. 7:04:11a string Builder you have a new keyword
  11609. 7:04:14and this new means that you want to
  11610. 7:04:15create a new instance of this you could
  11611. 7:04:18think of this structure as a template
  11612. 7:04:20this is the template for something and
  11613. 7:04:22until you start using that something
  11614. 7:04:24it's actually not going to do anything
  11615. 7:04:26so we have to make a new instance of it
  11616. 7:04:28because it's not already available so we
  11617. 7:04:30need to make it available through our
  11618. 7:04:31data type so when we say we want a new
  11619. 7:04:33person that gives us access to the
  11620. 7:04:35Constructor if I remove that you'll see
  11621. 7:04:37we've got one of two now we can create
  11622. 7:04:39an empty person and that will just make
  11623. 7:04:41us assign all the values individually
  11624. 7:04:43but we don't want that we want to use
  11625. 7:04:44the second path and if you drop the
  11626. 7:04:46arrow you'll be able to see the
  11627. 7:04:47different parameters which is the ones
  11628. 7:04:49we just typed in name age birth month
  11629. 7:04:52and then number and now that we've
  11630. 7:04:54returned back this we actually don't
  11631. 7:04:55even need to do any of these lines right
  11632. 7:04:58here we can actually just return it
  11633. 7:05:00exactly like this so now if you it again
  11634. 7:05:02you should see exactly the same output
  11635. 7:05:0323 5 and four perfect so let's just have
  11636. 7:05:07a little recap so normally when we're
  11637. 7:05:09creating variables we use common
  11638. 7:05:11variables that we've used in the past
  11639. 7:05:13string in double Flo bu Etc but with the
  11640. 7:05:16use of structures we can create our own
  11641. 7:05:19group of variables and then we can use
  11642. 7:05:21them variables inside functions and
  11643. 7:05:23inside our code to make things a lot
  11644. 7:05:25cleaner and a lot easier so inside
  11645. 7:05:27person we actually store four different
  11646. 7:05:29variables and they can be an infin
  11647. 7:05:31amount of variables should you will and
  11648. 7:05:33you can easily remove them and then
  11649. 7:05:35remove them from the parameters and from
  11650. 7:05:37the assignment and then that's it if we
  11651. 7:05:39use this without our structure then
  11652. 7:05:40we're going to have to declare every
  11653. 7:05:42single variable separately and I know
  11654. 7:05:44this is the same as doing it up here
  11655. 7:05:46it's actually a little bit more work cuz
  11656. 7:05:47you need to create a new parameter and
  11657. 7:05:49create all the the instances inside here
  11658. 7:05:51and assign the
  11659. 7:05:52values now the benefit of doing it
  11660. 7:05:54within a structure is that when you go
  11661. 7:05:56to do a function look how easy this line
  11662. 7:05:59is before you'd have to have name a age
  11663. 7:06:01birth month all as references or have
  11664. 7:06:04them as out parameters and it gets a
  11665. 7:06:05little bit long and a bit sloppy in
  11666. 7:06:07order to include the month in here we'd
  11667. 7:06:09have to have another in number and you
  11668. 7:06:12can see that the parameters are growing
  11669. 7:06:13if you wanted to store a user from a
  11670. 7:06:15registration form you might have name
  11671. 7:06:17address phone number country and the
  11672. 7:06:19list goes on it can be a very long form
  11673. 7:06:21and you therefore you'll have a lot of
  11674. 7:06:23parameters so it's very easy to wrap it
  11675. 7:06:25around inside a structure and then
  11676. 7:06:27simply reference it like that and you'll
  11677. 7:06:28need zero parameters because everything
  11678. 7:06:31is contained within the data type and
  11679. 7:06:33then inside here we can ask for the
  11680. 7:06:34users's details individually store them
  11681. 7:06:36as their integers and string variables
  11682. 7:06:39and then instead of using this notation
  11683. 7:06:40we can now use our fancy Constructor
  11684. 7:06:43where we can pass it in in this exact
  11685. 7:06:44order right here name age birth month
  11686. 7:06:47and then the number perfect and then we
  11687. 7:06:50can use inside here we can call each of
  11688. 7:06:52these positions separately and
  11689. 7:06:53concatenate them
  11690. 7:06:56together so now that we covered
  11691. 7:06:58structures known by struct as the
  11692. 7:07:00keyword let's have a look look at
  11693. 7:07:01classes and how to define them
  11694. 7:07:03syntactically so we can make a struct
  11695. 7:07:05called person and we can give them a
  11696. 7:07:07string name and inage and to make them
  11697. 7:07:10accessible we'll make them public and we
  11698. 7:07:12can say public person string name and
  11699. 7:07:15int Agee and that means we can assign
  11700. 7:07:18these values using the Constructor so we
  11701. 7:07:20can say this.name equals name and this.
  11702. 7:07:23age equals age perfect so's get rid of
  11703. 7:07:26our compiler error since we're assigning
  11704. 7:07:28both the values and then down here we
  11705. 7:07:30can use it we can say person person
  11706. 7:07:32equals new person and then have abber
  11707. 7:07:35and then 23 in the brackets and we can
  11708. 7:07:38print to the screen person. name and
  11709. 7:07:40then if use contrl D to duplicate that
  11710. 7:07:42down and we can do age and if we run
  11711. 7:07:45that right now we should see abber in 23
  11712. 7:07:48being printed to the
  11713. 7:07:50console perfect so as we talked about
  11714. 7:07:52introduction syntactically the struct
  11715. 7:07:54and the classes are very similar so
  11716. 7:07:56let's comment this out so we don't get
  11717. 7:07:58any compile errors and let's see how
  11718. 7:08:00this is going to work so of course we're
  11719. 7:08:01going to get error on this line but once
  11720. 7:08:03we make our class the error should
  11721. 7:08:04disappear from here so instead of struct
  11722. 7:08:07person we're simply just going to have
  11723. 7:08:09class person exactly the same and
  11724. 7:08:12because these are fields we can also
  11725. 7:08:14copy these down so we can have public
  11726. 7:08:16string name and public int
  11727. 7:08:18Edge and then we can have public person
  11728. 7:08:22and then string name in Edge this. name
  11729. 7:08:25equals name and this. age equals age so
  11730. 7:08:29as you can see this is exactly the same
  11731. 7:08:30as this except for the fact we have
  11732. 7:08:32structure and we have class inside here
  11733. 7:08:35now you see how compile error is gone
  11734. 7:08:36and if youve won the code we should see
  11735. 7:08:38exactly the same
  11736. 7:08:40output there we go now the main
  11737. 7:08:42difference about classes and about
  11738. 7:08:43structures is that you don't actually
  11739. 7:08:45have to give these a default value
  11740. 7:08:47inside the
  11741. 7:08:48Constructor if we just comment this one
  11742. 7:08:50out for a
  11743. 7:08:52second and if we bring the structure
  11744. 7:08:54back and we actually remove the
  11745. 7:08:56parameters from here then we're actually
  11746. 7:08:58going to get compile errors and it says
  11747. 7:09:00that the name and age must be assigned
  11748. 7:09:02before it's returned and stroks cannot
  11749. 7:09:04have parameterless Constructors now
  11750. 7:09:06that's a bit of a problem in certain
  11751. 7:09:08cases you might want to create an
  11752. 7:09:10instance of something but not actually
  11753. 7:09:12derive any values so you could actually
  11754. 7:09:14make these into default values you can
  11755. 7:09:16have string name equals nothing and int
  11756. 7:09:18age equals z and therefore once we place
  11757. 7:09:21inside here then there's no problems if
  11758. 7:09:23you run the code then you will get the
  11759. 7:09:25output this will be nothing this will be
  11760. 7:09:27zero and you'll get errors and you won't
  11761. 7:09:30get any compile errors doing this
  11762. 7:09:31but the point is you have to include
  11763. 7:09:33them and you don't have a
  11764. 7:09:35choice so if we just comment this back
  11765. 7:09:37out now what you'll notice about the
  11766. 7:09:39class
  11767. 7:09:40person is even if we get rid of it then
  11768. 7:09:43it still lets us have it of course we
  11769. 7:09:45can't actually give it parameters since
  11770. 7:09:46we don't have a Constructor that lets us
  11771. 7:09:48have parameters but what you can do as
  11772. 7:09:50well is you can overload this and
  11773. 7:09:52actually include it as well so you can
  11774. 7:09:54have another set that literally let you
  11775. 7:09:56define both of
  11776. 7:09:58them and then you can even have another
  11777. 7:10:00overload that lets you just include the
  11778. 7:10:02name without the ede and then also
  11779. 7:10:05another one that lets you include the
  11780. 7:10:06int age without the name or you include
  11781. 7:10:09both the details so in these cases what
  11782. 7:10:12you can do is you can store the values
  11783. 7:10:14up here as the fields let them use a
  11784. 7:10:16default Constructor or only give it the
  11785. 7:10:18name or the age or you can actually give
  11786. 7:10:20it both so when we come down into here
  11787. 7:10:23we actually now have four different
  11788. 7:10:24Constructors we can make an empty person
  11789. 7:10:27we can make it with an edge with a name
  11790. 7:10:29or with both
  11791. 7:10:32and what you can do in here is you can
  11792. 7:10:33say this.name is equal to nothing and
  11793. 7:10:36then here you can say this. AG is equal
  11794. 7:10:38to minus one just to default it to a
  11795. 7:10:40value that's incorrect so after we've
  11796. 7:10:42got this let's have a look at the
  11797. 7:10:44different ways so we can give it a
  11798. 7:10:45person which gives us an empty value so
  11799. 7:10:47if you run that now we get blank and
  11800. 7:10:50zero and if we just give it the name
  11801. 7:10:52which is Aba then we should get ABA and
  11802. 7:10:54minus one yep and then if you just give
  11803. 7:10:57it the ede you should get nothing in 23
  11804. 7:11:00and if you actually give them both then
  11805. 7:11:02you should get both printed out to the
  11806. 7:11:03screen and there we
  11807. 7:11:06go so now we have four different ways
  11808. 7:11:08that we can create our person with our
  11809. 7:11:10structure we're only really limited to
  11810. 7:11:12one since all of these values need to be
  11811. 7:11:14assigned as the structure is created
  11812. 7:11:16structures are meant to be used as an
  11813. 7:11:18extension to a data type rather than
  11814. 7:11:20almost like a mini management system
  11815. 7:11:23when we make a class we have so many
  11816. 7:11:25options of different ways that we can do
  11817. 7:11:26this inside here and later on in the
  11818. 7:11:28course we're going to show you how to
  11819. 7:11:30include functions inside here and then
  11820. 7:11:32eventually do other Advanced Techniques
  11821. 7:11:34with classes that aren't directly
  11822. 7:11:36available structures if you have a basic
  11823. 7:11:38example where you want to extend
  11824. 7:11:40something to use inside a function or a
  11825. 7:11:43data type like storing name and age
  11826. 7:11:45instead of having to do two return types
  11827. 7:11:47inside a function then a struct is
  11828. 7:11:49perfect the second you want to do more
  11829. 7:11:51complicated things or make it a bit more
  11830. 7:11:53specific and flexible then classes of
  11831. 7:11:55the way forward you'll see it very often
  11832. 7:11:57in games where they have struct position
  11833. 7:12:00you'll see in video games where they
  11834. 7:12:01have something like a struct position
  11835. 7:12:03and they might have int X int Y and int
  11836. 7:12:05Zed and that's for their 3D position or
  11837. 7:12:07they might even just have an X and Y or
  11838. 7:12:09you could have something simple like a
  11839. 7:12:11rectangle and you can give it a width
  11840. 7:12:15and a length so now when you use that
  11841. 7:12:17inside the code you're going to have
  11842. 7:12:19different flexibilities of the amount of
  11843. 7:12:20data types that you can store and then
  11844. 7:12:22when you return that back from a
  11845. 7:12:23function that'll be okay as well since
  11846. 7:12:25you can use this as a data type so if
  11847. 7:12:28you want something simple and
  11848. 7:12:29straightforward then go for the
  11849. 7:12:31the second you want something a bit more
  11850. 7:12:33complicated and a bit flexible then you
  11851. 7:12:35probably want a class we'll go into more
  11852. 7:12:37different situations inside this course
  11853. 7:12:38so you understand in which cases to use
  11854. 7:12:40what and in most cases you'll notice
  11855. 7:12:42that the class is more replicable so if
  11856. 7:12:44you wanted to you could ask the user to
  11857. 7:12:45enter these details in and then you can
  11858. 7:12:47pass them inside the Constructor so we
  11859. 7:12:49could say console right line change it
  11860. 7:12:52to a right enter your
  11861. 7:12:54name String name console. readline and
  11862. 7:12:59then copy and pastees down changes into
  11863. 7:13:01into a in Edge and then wrap this around
  11864. 7:13:03inside of convert in 32 instead of enter
  11865. 7:13:06your name it'll be ENT your Edge and
  11866. 7:13:08then C these are grade out it means we
  11867. 7:13:10haven't used them so we can change abber
  11868. 7:13:11to your name and then change 23 to age
  11869. 7:13:14perfect so now if you run the code now
  11870. 7:13:17enter your name ABA enter your age 23
  11871. 7:13:20then we get ABA 23 printed out to the
  11872. 7:13:22screen so just a quick recap
  11873. 7:13:25Constructors are basically defined the
  11874. 7:13:26same way you have public followed by the
  11875. 7:13:29class or the structure name and then the
  11876. 7:13:30pr inside the brackets and then you can
  11877. 7:13:33assign it after without structure you
  11878. 7:13:35have to declare all of the variables
  11879. 7:13:37inside the parameter and assign them all
  11880. 7:13:39down here otherwise you won't be able to
  11881. 7:13:40run your code with classes we have the
  11882. 7:13:43flexibility of just including one
  11883. 7:13:45default function which will just be our
  11884. 7:13:47Constructor and even if it does nothing
  11885. 7:13:49this is still acceptable and then we
  11886. 7:13:51broke it down to multiple sections where
  11887. 7:13:53you might have situations where you only
  11888. 7:13:55want their name or their age or you
  11889. 7:13:57might even collect both to store it if
  11890. 7:13:59you collect their name then we'll
  11891. 7:14:00initialize age to minus one just so you
  11892. 7:14:02know that this is a bad value and if you
  11893. 7:14:04declare only the age then will'll
  11894. 7:14:06initialize aim to nothing just so you
  11895. 7:14:08know that it's an empty value and if you
  11896. 7:14:11accept both then we simply assign both
  11897. 7:14:13and down here we have access to it we
  11898. 7:14:16can even make this a little bit better
  11899. 7:14:17by checking these values so we can say
  11900. 7:14:20if string is not null or empty of
  11901. 7:14:23person. name and we can put a knot
  11902. 7:14:26inside here then we can print out this
  11903. 7:14:31and then the same thing for age we can
  11904. 7:14:32say if person. AG we can say greater
  11905. 7:14:36than minus one since we set minus one as
  11906. 7:14:38the bad value and then we can move this
  11907. 7:14:40line up and let's try run it now so
  11908. 7:14:43these two if statements are
  11909. 7:14:44fundamentally separate from each other
  11910. 7:14:46one is checking the name and one is
  11911. 7:14:47checking the age so let's try and
  11912. 7:14:49manually type these in and see what
  11913. 7:14:51happens so enter your name let's make
  11914. 7:14:54this blank and only include the age and
  11915. 7:14:56let's see if this runs okay so you had
  11916. 7:14:58no name and the age is 23 then you see
  11917. 7:15:01we only get printed 23 because it'll
  11918. 7:15:03skip over this line as you can see if we
  11919. 7:15:05put a break point in now and just
  11920. 7:15:07refresh the code we get empty name plus
  11921. 7:15:1023 and when we press F10 over this line
  11922. 7:15:13it jumps the next one because if we
  11923. 7:15:15hover over this you'll see that it
  11924. 7:15:16should return us back with false there
  11925. 7:15:19we go we get strings null or empty is
  11926. 7:15:20false so that we skip over that and
  11927. 7:15:23because the age is 23 then this line
  11928. 7:15:25going to execute let's run it again and
  11929. 7:15:26do the opposite we could say name is Aba
  11930. 7:15:30and then age is min one and then we get
  11931. 7:15:32ABA printed be not the
  11932. 7:15:34edge there we go so if you wanted to fix
  11933. 7:15:37these in and type them manually so we
  11934. 7:15:39can say
  11935. 7:15:4023 there we go we get 23 and if we only
  11936. 7:15:43include the
  11937. 7:15:45name
  11938. 7:15:47AB then we only get Al printed out and
  11939. 7:15:49if we include both of
  11940. 7:15:52them then we get them both printed out
  11941. 7:15:56perfect so now that we've created our
  11942. 7:15:58class we've gave it some fields and
  11943. 7:16:00we've made our Constructor that lets us
  11944. 7:16:02initialize it let's see how we can print
  11945. 7:16:03the details out to the console so we can
  11946. 7:16:05have a CW tab tab a console right line
  11947. 7:16:08and then we can say string interpolation
  11948. 7:16:11name callon space and then we can have
  11949. 7:16:13our curly braces and we can say person.
  11950. 7:16:16name we can access these fields using
  11951. 7:16:18the dot operator and then we can say
  11952. 7:16:20back sln for the new line and then say
  11953. 7:16:22age and the same thing person. AG let's
  11954. 7:16:25try run that and see what we've
  11955. 7:16:27got so now we've outputed name and age
  11956. 7:16:30out to the screen let's see how we can
  11957. 7:16:31do this inside a function if you wanted
  11958. 7:16:33to place this in different part of your
  11959. 7:16:35code then you're going to have to copy
  11960. 7:16:36and paste this and as we know this is
  11961. 7:16:38bad for our code because it's j a
  11962. 7:16:39duplication we want to be able to wrap
  11963. 7:16:41this inside a function somewhere so
  11964. 7:16:43let's see what we can do we can have a
  11965. 7:16:44static string so it can return as back
  11966. 7:16:47with this that we can output straight to
  11967. 7:16:48the console and we can say return
  11968. 7:16:51details and for our parameter we can
  11969. 7:16:53have something like string name and int
  11970. 7:16:56age because that's what our class is
  11971. 7:16:57made up of and inside here we can simply
  11972. 7:17:00take this
  11973. 7:17:01put it inside here and put a return
  11974. 7:17:03before it and then get rid of the person
  11975. 7:17:05dot since we have an aim and age
  11976. 7:17:07variables as local to the function so
  11977. 7:17:10now instead of this line we can
  11978. 7:17:11duplicate this down take out the
  11979. 7:17:13contents and we can say return details
  11980. 7:17:16and then we can place person. name and
  11981. 7:17:18person. AG inside here let's run the
  11982. 7:17:21code so we get exactly the same output
  11983. 7:17:24but the problem with this function is
  11984. 7:17:26we're having to type out the parameters
  11985. 7:17:28manually we have them here and we're
  11986. 7:17:30going to have to to type them again in
  11987. 7:17:31here and again for the parameters this
  11988. 7:17:33is a bit of a problem cuz we've just
  11989. 7:17:35made our class but we haven't really
  11990. 7:17:37used it when we're passing it in so
  11991. 7:17:38instead of having string name and int
  11992. 7:17:40age we can actually duplicate this
  11993. 7:17:42method and just comment it out just so
  11994. 7:17:43you can see it for reference and instead
  11995. 7:17:45of doing this you can actually just pass
  11996. 7:17:47in an entire person so now we have
  11997. 7:17:50access to person and we can bring this
  11998. 7:17:52back and we can say person. name and we
  11999. 7:17:54can also say person. age and the reason
  12000. 7:17:57why this method is preferred is if you
  12001. 7:17:59actually have something else public int
  12002. 7:18:01number and then you added another one
  12003. 7:18:03inside here in number and then you said
  12004. 7:18:05this do number equals number the
  12005. 7:18:08parameters won't have to change inside
  12006. 7:18:10the function all you have to do is
  12007. 7:18:12simply pass in the person and then now
  12008. 7:18:15we actually have access to doing person.
  12009. 7:18:17number inside here should we need to if
  12010. 7:18:19you're passing them all in one by one
  12011. 7:18:21like this then you have to append into
  12012. 7:18:24number on the end and then add it
  12013. 7:18:25straight to here and then also add it
  12014. 7:18:27inside here as you have like this it's
  12015. 7:18:29easy to pass the class in because all of
  12016. 7:18:31these properties above are just simply
  12017. 7:18:33used throughout the function so now if
  12018. 7:18:35we just bring this
  12019. 7:18:39back and let's have a run and see if we
  12020. 7:18:42get the same
  12021. 7:18:43output which we do name ABA age 23
  12022. 7:18:46perfect so now that we've thought of two
  12023. 7:18:49functions how to do this we can manually
  12024. 7:18:51type in the strings and the integer and
  12025. 7:18:52any other parameters that we need all we
  12026. 7:18:54can actually pass in the person the
  12027. 7:18:56problem with these two approaches is the
  12028. 7:18:58whole point of having classes and
  12029. 7:19:00structures of of the like is so we can
  12030. 7:19:02wrap around lots of functionality inside
  12031. 7:19:03it if you want to send this to another
  12032. 7:19:05developer for them to use in their
  12033. 7:19:07project or maybe this is a file that is
  12034. 7:19:08shared within the project then you don't
  12035. 7:19:10want them to include a separate function
  12036. 7:19:12that is simply outside of the class we
  12037. 7:19:14want to include the functions inside the
  12038. 7:19:16class so of course as you know these are
  12039. 7:19:19functions so inside here we can also
  12040. 7:19:22make functions so let's do it so we can
  12041. 7:19:24have public and we want a string return
  12042. 7:19:27details and then we don't need to pass
  12043. 7:19:29anything in the the values that we're
  12044. 7:19:32going to be returning are these two
  12045. 7:19:33right here which is technically local to
  12046. 7:19:35our class so therefore we're not going
  12047. 7:19:37to have any parameters I'll explain that
  12048. 7:19:40further in a second so we can take this
  12049. 7:19:42exact line paste it straight into here
  12050. 7:19:44and get rid of the person Dot and get
  12051. 7:19:46rid of the person do on this side so
  12052. 7:19:48then we have the name variable and the
  12053. 7:19:50age variable if you double click on them
  12054. 7:19:52you see it references these variables
  12055. 7:19:54and the same thing for age the reason
  12056. 7:19:56why we don't have parameters is when you
  12057. 7:19:58call this function as I'll show you in a
  12058. 7:19:59second you call it directly on the
  12059. 7:20:01person so the person that you're trying
  12060. 7:20:03to print has already got these contents
  12061. 7:20:05inside of them so let's have a look we
  12062. 7:20:07can duplicate this down and instead of
  12063. 7:20:09calling return details and passing the
  12064. 7:20:11person you actually do it the other way
  12065. 7:20:13around you say the person object dot
  12066. 7:20:16return details so now we're calling
  12067. 7:20:18return details inside our person
  12068. 7:20:21object now let's run the code you see
  12069. 7:20:24that the output is exactly the same
  12070. 7:20:26except now this functionality is a lot
  12071. 7:20:27better our return details is wrapped
  12072. 7:20:30inside our class which is the preferred
  12073. 7:20:32way now we can actually get rid of these
  12074. 7:20:34inside here and then we shouldn't really
  12075. 7:20:36use this notation even though it is
  12076. 7:20:38technically correct but you want to wrap
  12077. 7:20:40all your functionality inside here and
  12078. 7:20:42just bear in mind that scope is also
  12079. 7:20:44applicable here return details is within
  12080. 7:20:46this class which means if you try to run
  12081. 7:20:49this function inside the main then it's
  12082. 7:20:50not going to node exist this return
  12083. 7:20:53details function was able to be used
  12084. 7:20:55inside here because this was in the
  12085. 7:20:57scope of the class program which is this
  12086. 7:20:59curly brace and this curly bra but this
  12087. 7:21:02return details is only within the scope
  12088. 7:21:04of this class which means you can't just
  12089. 7:21:06suddenly use it inside the main so
  12090. 7:21:08please bear that in mind when you
  12091. 7:21:09produce in these classes and you trying
  12092. 7:21:11to understand where everything goes
  12093. 7:21:13every function that you place inside
  12094. 7:21:15here is accessible through the dot
  12095. 7:21:16notation after the object once you've
  12096. 7:21:18created the class say person equals new
  12097. 7:21:21person and then after we type person dot
  12098. 7:21:23then we'll get a list of all the
  12099. 7:21:24functions the same way if you have
  12100. 7:21:26something else just like this and then
  12101. 7:21:30if you do person Dot and then you see
  12102. 7:21:32that something has appeared there so all
  12103. 7:21:34the functions that you create up inside
  12104. 7:21:36in this class for every person that you
  12105. 7:21:38make it'll be accessible through the do
  12106. 7:21:40notation so just to round up this video
  12107. 7:21:42you should always make your functions
  12108. 7:21:44inside your class having them inside the
  12109. 7:21:46class is the whole point of making a big
  12110. 7:21:48class and have lots of functionality
  12111. 7:21:50inside if you keep the function that
  12112. 7:21:52does exactly the same thing outside of
  12113. 7:21:54the class then if you were to share this
  12114. 7:21:56file or come back to it in the future
  12115. 7:21:58then you're not going to understand why
  12116. 7:21:59you've done this and and it's outside
  12117. 7:22:01the class keeping everything wrapped
  12118. 7:22:02inside the class is the best way forward
  12119. 7:22:04in most case
  12120. 7:22:07scenarios so now we have our class
  12121. 7:22:09person it's got a name and age a
  12122. 7:22:11Constructor and a function to return
  12123. 7:22:13back the details and we can simply use
  12124. 7:22:15it like this and return back the details
  12125. 7:22:17like this but so far we've covered how
  12126. 7:22:19to make classes Constructors Etc and
  12127. 7:22:22assign the values but the values are
  12128. 7:22:24only get assigned throughout the
  12129. 7:22:26Constructor but what if a different part
  12130. 7:22:28of our program the user wants to change
  12131. 7:22:30their name or update their age Etc then
  12132. 7:22:32what options do we have so you know we
  12133. 7:22:34can access our Fields using person. name
  12134. 7:22:37and then you can simply type equals and
  12135. 7:22:39then type in another name and we can say
  12136. 7:22:41person. AG equals 25 now we've changed
  12137. 7:22:44these properties if we use the console
  12138. 7:22:46right line and return details you'll see
  12139. 7:22:48that the values have indeed
  12140. 7:22:50changed as you can see there it's
  12141. 7:22:52changed the hurry and
  12142. 7:22:5425 this is okay except for the fact that
  12143. 7:22:57this shouldn't really be possible we
  12144. 7:22:59should try and restrict this Direct
  12145. 7:23:00corly by the use of gets and sets so for
  12146. 7:23:03now we're going to cover fields and how
  12147. 7:23:04to restrict certain values and later on
  12148. 7:23:07in the course we're going to go through
  12149. 7:23:08what properties are so before alter
  12150. 7:23:10properties got invented we had to use
  12151. 7:23:13gets and sets gets and sets are
  12152. 7:23:15essentially functions that will prevent
  12153. 7:23:17this from happening so the reason why we
  12154. 7:23:20can do this is simply because this
  12155. 7:23:22public word is placed right here if we
  12156. 7:23:25change it to private it means that name
  12157. 7:23:27is still accessible within here and here
  12158. 7:23:30which is only the limit of the class but
  12159. 7:23:32as you can see it's not applicable here
  12160. 7:23:35this is why you need the public Getters
  12161. 7:23:37and Setters so I'll explain that what
  12162. 7:23:39that means now so we prevent these from
  12163. 7:23:41accessing outside the class by making
  12164. 7:23:44them private so this functionality no
  12165. 7:23:46longer works so what you have to do
  12166. 7:23:48instead is for every field that you have
  12167. 7:23:50you need to create a GTO and a seter
  12168. 7:23:53function so in order to set a value we
  12169. 7:23:55can have public and because we're
  12170. 7:23:57setting it we don't need to return
  12171. 7:23:58anything so it'll be void and we can say
  12172. 7:24:00set name and then we pass in string name
  12173. 7:24:04and then we simply just say this. name
  12174. 7:24:06equals name so when someone call set
  12175. 7:24:09name and pass in the name like this we
  12176. 7:24:11just override it so let's see how that
  12177. 7:24:13looks so if we comment these two lines
  12178. 7:24:15out we can simply say person do set name
  12179. 7:24:18and then we can say Harry so this line
  12180. 7:24:21is directly replaced by this line and
  12181. 7:24:23now if you run this we'll see that Harry
  12182. 7:24:25should be changed there you go abber is
  12183. 7:24:28changed to Harry this is powerful
  12184. 7:24:31because what we can do now is restrict
  12185. 7:24:33the access inside here and make sure
  12186. 7:24:35that the value contains something
  12187. 7:24:36genuine we can say if string is null or
  12188. 7:24:41empty and then pass the name inside
  12189. 7:24:43there if the string isn't null or empty
  12190. 7:24:46then we assign it and if it is null or
  12191. 7:24:48empty then what we can do inside here is
  12192. 7:24:51we can say else this do name equals
  12193. 7:24:53nothing and we can keep it blank or we
  12194. 7:24:55can use string. empty just to make sure
  12195. 7:24:58that we know that the name has been
  12196. 7:24:59blank out if they've entered something
  12197. 7:25:02invalid if they've entered something
  12198. 7:25:03that's valid we assign it if it's
  12199. 7:25:05invalid then we don't assign it and we
  12200. 7:25:07just initialize it back to nothing and
  12201. 7:25:09because our options are endless with
  12202. 7:25:10this we can actually make this into
  12203. 7:25:12something else we can simply just say
  12204. 7:25:14invalid name so then what happens is if
  12205. 7:25:17we try and change this to nothing and we
  12206. 7:25:19run the
  12207. 7:25:20code we can see that we get invalid name
  12208. 7:25:22being printed to the screen and that's a
  12209. 7:25:24lot better than if we had string.
  12210. 7:25:27empty because if you had string. empty
  12211. 7:25:30then it would kind of say nothing then
  12212. 7:25:31it looks like something's went wrong if
  12213. 7:25:33you have something like invalid then
  12214. 7:25:35that gives you a bit of a Barr message
  12215. 7:25:37so we can have invalid name there so
  12216. 7:25:40using our Setter Let's us change our
  12217. 7:25:41name from down here without directly
  12218. 7:25:44exposing the public if he expose the
  12219. 7:25:46public then everyone who uses classes
  12220. 7:25:48can simply change the name to whatever
  12221. 7:25:50they want having it private and only
  12222. 7:25:52changing the name within the set name
  12223. 7:25:54function is very preferable because you
  12224. 7:25:57can restrict the access and what people
  12225. 7:25:58can set the values to if they try to set
  12226. 7:26:01it to something that's not valid then we
  12227. 7:26:03set it to invalid name whereas before
  12228. 7:26:06when we did this and if we make this
  12229. 7:26:08public
  12230. 7:26:10again we can simply set this to
  12231. 7:26:12something bad like null null is a very
  12232. 7:26:14bad value but it still lets us have it
  12233. 7:26:17and the reason why it's very bad is if
  12234. 7:26:19you try to do something like this
  12235. 7:26:21person. equals and just some empty
  12236. 7:26:24brackets now even if I just leave this
  12237. 7:26:26empty the fact that you try call equals
  12238. 7:26:29on S inside person. name is going to be
  12239. 7:26:32a bit of a
  12240. 7:26:33problem so we can say person object the
  12241. 7:26:36name property inside it and we checking
  12242. 7:26:38if it equals to Aba for example but
  12243. 7:26:40because we've just said it to null let's
  12244. 7:26:42see what
  12245. 7:26:43happens you see that we actually get an
  12246. 7:26:45exception it's a null reference
  12247. 7:26:47exception because if you hover over name
  12248. 7:26:49you see that returns back null nulls are
  12249. 7:26:51very bad in this case whereas if you
  12250. 7:26:53actually bring our function back and we
  12251. 7:26:56make this into a private and then if we
  12252. 7:26:59try and take this value our null value
  12253. 7:27:01and place it straight inside our set
  12254. 7:27:03name if you uncomment it get rid of the
  12255. 7:27:06quotation marks and if you actually try
  12256. 7:27:08and assign it to null let's see what
  12257. 7:27:10happens then we're going to get invalid
  12258. 7:27:12name and this prevents us from breaking
  12259. 7:27:14our code if a function like equals were
  12260. 7:27:17to be called on any of our properties
  12261. 7:27:18inside of name so now that we've done it
  12262. 7:27:20for our name we can also do it for our
  12263. 7:27:22age but before we do that we can also
  12264. 7:27:25call a function called get name return
  12265. 7:27:28details gives you back these details
  12266. 7:27:30in a very specific fashion but what if
  12267. 7:27:32you wanted to do something like this
  12268. 7:27:34your name is and then you wanted to do
  12269. 7:27:37person. name but of course you can't do
  12270. 7:27:39that because name is no longer public so
  12271. 7:27:42you can't reference it directly so we've
  12272. 7:27:44got a set name and then now we need to
  12273. 7:27:46do a get name so we can have public and
  12274. 7:27:49because name is a string the return type
  12275. 7:27:52is going to be string and we can say get
  12276. 7:27:54name and this function is very simple it
  12277. 7:27:56just returns back name and that's it
  12278. 7:27:59there's no no parameters because all it
  12279. 7:28:01does is return back a value and in this
  12280. 7:28:03case it's just returning back name so
  12281. 7:28:06instead of using person. name in here we
  12282. 7:28:08can say person do get name open close
  12283. 7:28:11the brackets to indicate it's a function
  12284. 7:28:12but there's no parameters now you see
  12285. 7:28:14that we have no compile errors so now if
  12286. 7:28:16you run that you'll see that it says
  12287. 7:28:18your name is invalid name so let's
  12288. 7:28:21actually give it a
  12289. 7:28:22value and we can say Harry again run the
  12290. 7:28:25code there you go your name is Harry so
  12291. 7:28:28it's officially change it now let's work
  12292. 7:28:30on the
  12293. 7:28:31edge so just following the exact same
  12294. 7:28:33approach we can have a public void set
  12295. 7:28:37Edge in age and instead of just doing
  12296. 7:28:41this. age equals age let's try set some
  12297. 7:28:43boundaries on this we can wrap this
  12298. 7:28:46inside an if statement so as for the
  12299. 7:28:48condition we want to validate if the age
  12300. 7:28:50is successful so we want to do if age is
  12301. 7:28:52greater than or equal to zero and age is
  12302. 7:28:54less than or equal to 150 so this means
  12303. 7:28:57if the age is less than zero or greater
  12304. 7:28:59than 150 then this is going to return
  12305. 7:29:02back false and then it's not going to
  12306. 7:29:03assign the value and we can simply
  12307. 7:29:05assign it to minus1 -1 is just so we
  12308. 7:29:08know it's a bad value so now that we've
  12309. 7:29:10created our set age we can actually make
  12310. 7:29:12our get age so we want to return back in
  12311. 7:29:14int because that's going to be the data
  12312. 7:29:17type for age so we do public int get age
  12313. 7:29:20we don't need any parameters cuz as like
  12314. 7:29:22the name we're just simply returning
  12315. 7:29:24something so we can say return age so
  12316. 7:29:27now that you've made the set age and the
  12317. 7:29:28get age let's actually use it quote so
  12318. 7:29:31we've made our set name and let's say
  12319. 7:29:33person do set age and we have 25 inside
  12320. 7:29:36here just like above and then now that
  12321. 7:29:39we've set the age let's appin this
  12322. 7:29:40sentence we can say and your age is and
  12323. 7:29:43then person. get age perfect so let's
  12324. 7:29:47run the code and see what happens there
  12325. 7:29:49we go we have 25 and Harry being
  12326. 7:29:53changed so another little tip when we do
  12327. 7:29:55in this code I get age as one line and
  12328. 7:29:58I'll set age as a couple of lines but
  12329. 7:30:00fundamentally only assigning Edge so
  12330. 7:30:02what we can do is we can make these into
  12331. 7:30:04the tertiary operator so as we can see
  12332. 7:30:06here this is the condition if this
  12333. 7:30:09returns back true then we assign age our
  12334. 7:30:11local parameter and if this returns back
  12335. 7:30:13false we assign minus one so we can
  12336. 7:30:16actually make this into this. AG equals
  12337. 7:30:19if you remember the format of our
  12338. 7:30:20conditional operator use condition true
  12339. 7:30:23and then condition false so this is our
  12340. 7:30:26condition that is leading our if
  12341. 7:30:28statement and if this returns back true
  12342. 7:30:30then we do question mark and put age in
  12343. 7:30:32there and then call on if it returns
  12344. 7:30:34back false then it's simply minus one
  12345. 7:30:36there we go so this is actually being
  12346. 7:30:38entirely replaced by this one simple
  12347. 7:30:41line and we can do the same thing for
  12348. 7:30:43our string so we can say this.name is
  12349. 7:30:46equal to string that is null or empty
  12350. 7:30:48pass the name inside there and if that
  12351. 7:30:50is true then we assign it to name and if
  12352. 7:30:53that is false we say invalid name let's
  12353. 7:30:55comment this one out and let's see if
  12354. 7:30:57our code runs okay still
  12355. 7:31:00hurry 25 hurry 25 and now let's try our
  12356. 7:31:03bad values so we can try to set the age
  12357. 7:31:05to 200 and the value to nothing and
  12358. 7:31:08let's see what happens we get invalid
  12359. 7:31:11name minus one and there you
  12360. 7:31:13go perfect so that's working okay let's
  12361. 7:31:15undo them values and you can see that
  12362. 7:31:18this has been shortened completely
  12363. 7:31:20another top tip for functions like this
  12364. 7:31:22you can actually use something called
  12365. 7:31:23Arrow functions and if you've done any
  12366. 7:31:25JavaScript this might be quite familiar
  12367. 7:31:27so we can have our get Edge and in
  12368. 7:31:29instead of open the curly brace at this
  12369. 7:31:31point we can actually place this Arrow
  12370. 7:31:33inside so if we comment out this one
  12371. 7:31:36with arrow functions you can only return
  12372. 7:31:38back one simple line so because this is
  12373. 7:31:41only one line we can have return Edge
  12374. 7:31:43but our functions don't like the keyword
  12375. 7:31:46return any variable that you put
  12376. 7:31:48straight inside here is automatically
  12377. 7:31:50applied that it's a return type so we
  12378. 7:31:52literally just put age get age returns
  12379. 7:31:55us back with age if this is confusing
  12380. 7:31:57then stick to this approach but this can
  12381. 7:32:00might tidy up your code because instead
  12382. 7:32:02of having all of this and all the curly
  12383. 7:32:04braces for one simple line you can use
  12384. 7:32:06Arrow function so if you actually copy
  12385. 7:32:08and paste but first comment out all of
  12386. 7:32:11these functions you can see how small
  12387. 7:32:13the code can actually get there we go
  12388. 7:32:16and then let's actually have a public
  12389. 7:32:18void set age let's put our R function in
  12390. 7:32:22and then we can take this one line and
  12391. 7:32:24paste it straight into here so you can
  12392. 7:32:26see that these two lines are now being
  12393. 7:32:28replaced by all of this
  12394. 7:32:30and of course we need our int age inside
  12395. 7:32:32here there we
  12396. 7:32:35go so now we can do the same thing with
  12397. 7:32:37our set name we can take
  12398. 7:32:40this we can have public viid set name
  12399. 7:32:44String name and then have our Arrow
  12400. 7:32:46function and then simply paste that in
  12401. 7:32:48you can see that this will get a little
  12402. 7:32:49bit long now but it's still ideal I've
  12403. 7:32:52got the code zoomed in but if you put
  12404. 7:32:54that to 100% you'll see that this code
  12405. 7:32:56is actually pretty small it's just I've
  12406. 7:32:58zoomed in just so you can see the code a
  12407. 7:33:00lot
  12408. 7:33:01clearer and then finally for public
  12409. 7:33:05string and we can get name and then we
  12410. 7:33:07simply just do arrow and then name and
  12411. 7:33:10you can see that with all this other
  12412. 7:33:11code commented we're just relying on
  12413. 7:33:13these functions as we just created and
  12414. 7:33:15you'll see there's no console errors or
  12415. 7:33:17anything else so if you try and run it
  12416. 7:33:19you'll see that everything is going okay
  12417. 7:33:21of course if you don't like this
  12418. 7:33:23notation and you don't have to include
  12419. 7:33:24it you can stick with the curly braces I
  12420. 7:33:27don't use the arrow functions a lot in
  12421. 7:33:28practice because there isn't many
  12422. 7:33:30situations where you'd actually use them
  12423. 7:33:32and even if you do some people might not
  12424. 7:33:34like them or there might be certain
  12425. 7:33:36guidelines that you might be following
  12426. 7:33:37inside the
  12427. 7:33:38company what I do suggest you stick with
  12428. 7:33:41is the tertiary operator these are a lot
  12429. 7:33:44good and a lot better to use since it
  12430. 7:33:46reduces the code drastically if we
  12431. 7:33:48actually remove this and I'll paste it
  12432. 7:33:51back in just so you can see it for
  12433. 7:33:52reference inside the resources but you
  12434. 7:33:54can see that these four functions are
  12435. 7:33:56now reduced to on one pitch you can see
  12436. 7:33:59that they're all got one lines and
  12437. 7:34:00they're pretty short so let me undo that
  12438. 7:34:02just so you can see the original code
  12439. 7:34:04and have it for
  12440. 7:34:05reference so there's a lot covered
  12441. 7:34:07inside this video so let's just have a
  12442. 7:34:09little recap before we finish up so just
  12443. 7:34:12to start at the beginning when we
  12444. 7:34:14initialize our person with their name
  12445. 7:34:16and their age we don't actually have any
  12446. 7:34:18other opportunities to change their
  12447. 7:34:19value which is where this comes in you
  12448. 7:34:22can actually just say a person. name and
  12449. 7:34:24assign any sort of value you can put
  12450. 7:34:26anything inside there and it'll assign
  12451. 7:34:28it directly unconditionally the problems
  12452. 7:34:30of doing this is someone might expose it
  12453. 7:34:32to null by accident and somewhere in
  12454. 7:34:34your code if you're using something like
  12455. 7:34:36equals this will actually prevent an
  12456. 7:34:38exception of course you can wrap this
  12457. 7:34:40inside a try catch but this is
  12458. 7:34:41unnecessary because we can prevent this
  12459. 7:34:43in the first place it's always best to
  12460. 7:34:45prevent an exception then to put
  12461. 7:34:47additional cord in like a try catch in
  12462. 7:34:49order to catch something that you can
  12463. 7:34:50prevent in the first place prevention is
  12464. 7:34:52definitely better than the Cure in this
  12465. 7:34:54case so having something like a person.
  12466. 7:34:57name being exposed it's not so much for
  12467. 7:34:59the integer because integers will always
  12468. 7:35:02have number values whereas strings they
  12469. 7:35:04can be initialized to null there is no
  12470. 7:35:06such thing as null when our age that our
  12471. 7:35:09default value for an integer is zero so
  12472. 7:35:12we don't really have the same issue we
  12473. 7:35:13started introducing the name function
  12474. 7:35:15and the age function these are used so
  12475. 7:35:17we can control what values get assigned
  12476. 7:35:19to our local variables and we also have
  12477. 7:35:22our get functions get name and get age
  12478. 7:35:24so let's explore how these are made
  12479. 7:35:26inside our Cass and expand on that and
  12480. 7:35:28see how they work so first we need to
  12481. 7:35:30make sure that our variables are
  12482. 7:35:32actually private they're private so we
  12483. 7:35:34can't access them just like this if I
  12484. 7:35:36copy and paste this line You'll see
  12485. 7:35:37we're going to get a compile error and
  12486. 7:35:39this error is because we can't actually
  12487. 7:35:41use it because as it says here it's
  12488. 7:35:44inaccessible due to its protection level
  12489. 7:35:46and the protection level comes from the
  12490. 7:35:48fact that this is indeed private it's
  12491. 7:35:51private so it's only accessible within
  12492. 7:35:53class person so every reference and name
  12493. 7:35:56can be used inside here just not outside
  12494. 7:35:58so the only way to be able to change the
  12495. 7:36:00name now you've made it private is by
  12496. 7:36:02enforcing this function the good thing
  12497. 7:36:04about this function is we can check if
  12498. 7:36:06the value is invalid and then we can
  12499. 7:36:08star it if it's valid and then change it
  12500. 7:36:10to something else if it's invalid and
  12501. 7:36:12then we can wrap this if statement
  12502. 7:36:13around in a conditional Operator just to
  12503. 7:36:15make it a little bit shorter and in
  12504. 7:36:17order to retrieve a name because now
  12505. 7:36:19that it made it public we can neither
  12506. 7:36:21set or retrieve it so we have our get
  12507. 7:36:24name function that simply just returns
  12508. 7:36:26it back to the screen nothing special
  12509. 7:36:28here and the same thing for our set age
  12510. 7:36:30we can check if the age is between 0 and
  12511. 7:36:32150 and assign it and if it's not assign
  12512. 7:36:34it to minus1 and then again wrap it
  12513. 7:36:37around in our conditional operator and
  12514. 7:36:39then for get age we can just return age
  12515. 7:36:41I demonstrated our functions inside here
  12516. 7:36:44you can take all of these on line
  12517. 7:36:45functions and actually add them all in
  12518. 7:36:47one line without the need of curly
  12519. 7:36:49braces if you want to return back in age
  12520. 7:36:51then you simply just type in age just
  12521. 7:36:53like you can see here and here when you
  12522. 7:36:55do assignment you want to specify this.
  12523. 7:36:58name equals but when you doing returns
  12524. 7:37:00you just need to type in the variable
  12525. 7:37:01that you need of course this looks a
  12526. 7:37:03little bit cleaner and a bit shorter but
  12527. 7:37:05not Everyone likes this notation so it's
  12528. 7:37:07completely up to the developer all the
  12529. 7:37:09environment that you're working in and
  12530. 7:37:11finally we just have our return details
  12531. 7:37:13and then we can make use of them just
  12532. 7:37:15like this by using person which is the
  12533. 7:37:17name of our local variable for our new
  12534. 7:37:19instance followed by a DOT and then set
  12535. 7:37:21name set age and then get name and then
  12536. 7:37:24get
  12537. 7:37:27age so now that we're dealing with
  12538. 7:37:29classes it's important you focus on the
  12539. 7:37:31scope we have a person person here
  12540. 7:37:33that's defined within the main which
  12541. 7:37:35means it's only Within These two curly
  12542. 7:37:37braces so if we have a static void and a
  12543. 7:37:39function that did something inside here
  12544. 7:37:42we can't access person inside here if we
  12545. 7:37:44take out the comment and we try it the
  12546. 7:37:46only reference we have is accessing the
  12547. 7:37:49class directly not this local variable
  12548. 7:37:52if you want to access local variable
  12549. 7:37:54from inside this function then you'd
  12550. 7:37:56simply have to pass it in like this and
  12551. 7:37:58then it'll finally become available as
  12552. 7:38:00you can see now there's two and it says
  12553. 7:38:02as a parameter and the same goes for
  12554. 7:38:04this class above just because we have
  12555. 7:38:07name and age up here it doesn't
  12556. 7:38:09necessarily mean that we can magically
  12557. 7:38:10use name down here if we have something
  12558. 7:38:13like a static int defined right here
  12559. 7:38:16then we can use this integer inside
  12560. 7:38:18these functions as it says there it's
  12561. 7:38:20available we can sign that to 10 and
  12562. 7:38:23it's also down here available and we can
  12563. 7:38:25sign that one to 20 and you see we have
  12564. 7:38:26no compil erors but if we take this and
  12565. 7:38:29put it inside our return details
  12566. 7:38:30function then you're going to get an
  12567. 7:38:32exception because X is not available
  12568. 7:38:34within that scope and the functional
  12569. 7:38:35scope matters inside here as well if you
  12570. 7:38:38have a string test inside
  12571. 7:38:40here we can just access it in another
  12572. 7:38:42function for example The Constructor we
  12573. 7:38:45can't just print out test as the screen
  12574. 7:38:48the handy things or classes if you want
  12575. 7:38:49variables to be available throughout the
  12576. 7:38:51class then you put them at the top we
  12577. 7:38:53can have that private string test and
  12578. 7:38:56there you go you see that the compiler
  12579. 7:38:57error is now gone and we give that
  12580. 7:38:59initial value and then you can use that
  12581. 7:39:01to print to the screen Etc and just to
  12582. 7:39:04note when you have a variable that's
  12583. 7:39:06called exactly the same thing we have a
  12584. 7:39:09private string test up here and we can
  12585. 7:39:11say hello then we also have a string
  12586. 7:39:13test down here so let me demonstrate
  12587. 7:39:16what happens when two variables are the
  12588. 7:39:17same so let's move this into our
  12589. 7:39:20Constructor so if we run this as it is
  12590. 7:39:22the test equals hello and then we print
  12591. 7:39:24out test and then run the
  12592. 7:39:26code there you go we get hello printed
  12593. 7:39:29and just ignore this this is from the
  12594. 7:39:31return details function as we've got in
  12595. 7:39:33the main down here return details so
  12596. 7:39:37we've made our new Constructor and then
  12597. 7:39:39assigned it the value with abber and 23
  12598. 7:39:41and then we can get hello printed to the
  12599. 7:39:43screen but let's say we actually made a
  12600. 7:39:44string test inside here and we said hi
  12601. 7:39:47instead of hello then which one do you
  12602. 7:39:48think it's used the global one or the
  12603. 7:39:51local one let's run it and find out it's
  12604. 7:39:54actually the local one the compiler will
  12605. 7:39:56always prioritize the local variables
  12606. 7:39:58over the glob Global variables and this
  12607. 7:40:00is the reason why you need to type this
  12608. 7:40:02dot because when you use name it's
  12609. 7:40:05always going to reference the parameter
  12610. 7:40:06when it's name on its own use console
  12611. 7:40:09right line and say name and then if we
  12612. 7:40:11do after this line and another console
  12613. 7:40:12right line and we say this. name then
  12614. 7:40:14these are completely different this one
  12615. 7:40:16is the parameter and this one is talking
  12616. 7:40:18about the variables from the class and
  12617. 7:40:20these are known as our Fields local
  12618. 7:40:23variables take presidence overall Fields
  12619. 7:40:26as long as they're called the same thing
  12620. 7:40:28as you can see now says it's assigned
  12621. 7:40:30but the value is never used if you take
  12622. 7:40:32the string test out then you can see
  12623. 7:40:33it's now being
  12624. 7:40:36used there you go hello gets printed out
  12625. 7:40:39to the screen and there you go the green
  12626. 7:40:41line has went because we're using it
  12627. 7:40:43down here scope is very important when
  12628. 7:40:45you're designing these classes and just
  12629. 7:40:47to note that the variables that are
  12630. 7:40:49available inside here are not going to
  12631. 7:40:52be necessarily available inside here
  12632. 7:40:54unless you make them public and even
  12633. 7:40:56when you make them public that doesn't
  12634. 7:40:58necessarily mean mean that this is still
  12635. 7:41:00available magically over here name will
  12636. 7:41:04only be available through an instance of
  12637. 7:41:06the person class so if we do person.
  12638. 7:41:08name now we can see that that's
  12639. 7:41:10available as our field so that's why
  12640. 7:41:13that makes it
  12641. 7:41:14public if we try and make this private
  12642. 7:41:16again just to return that value
  12643. 7:41:19back and we scroll down you'll see now
  12644. 7:41:21we get a compile error because it's
  12645. 7:41:23inaccessible because it's outside the
  12646. 7:41:25scope because it's now a private
  12647. 7:41:28variable
  12648. 7:41:31so we've covered what it means to have a
  12649. 7:41:33public or a private field if you have a
  12650. 7:41:35private field then you're going to have
  12651. 7:41:37to use Setters and Getters in order to
  12652. 7:41:39work with this field if you make it
  12653. 7:41:41directly public then you won't need any
  12654. 7:41:43Setters and Getters because the user can
  12655. 7:41:45just type in do name after their public
  12656. 7:41:48instance so you can have person followed
  12657. 7:41:50by the dot and right now you can't see
  12658. 7:41:52name tied here and the blue slittle icon
  12659. 7:41:55is no longer there the reason why it's
  12660. 7:41:57not there is cuz there's no public
  12661. 7:41:59Fields if we then change this to
  12662. 7:42:02public remove the dot and read it you'll
  12663. 7:42:04see that our blue square is now back
  12664. 7:42:07because our field is now accessible and
  12665. 7:42:09we can assign that to anything that we
  12666. 7:42:11want the whole point of making this
  12667. 7:42:12private is you only restrict the access
  12668. 7:42:15so you know what you're doing and the
  12669. 7:42:17reason why I say that is if they enter
  12670. 7:42:20something that's invalid then we want to
  12671. 7:42:21specify something like invalid if we
  12672. 7:42:23give them Public Access then we're not
  12673. 7:42:25going to know and we won't be able to
  12674. 7:42:27validate what they've entered so in
  12675. 7:42:29order to do it through a Setter we can
  12676. 7:42:31make it private create our set function
  12677. 7:42:33with a return type it's going to take in
  12678. 7:42:35a string parameter and this string has
  12679. 7:42:37to be the same data type as the prety
  12680. 7:42:39you're talking about call it name or
  12681. 7:42:42whatever you want it to be and because
  12682. 7:42:44these two are called exactly the same
  12683. 7:42:45thing we have to use this dot so we're
  12684. 7:42:47referencing this name instead of this
  12685. 7:42:49name in the parameters and here we're
  12686. 7:42:51just using a conditional operator and
  12687. 7:42:52we're saying if the string is not null
  12688. 7:42:54or empty hence the exclamation point
  12689. 7:42:56then we assign it to name name gets put
  12690. 7:42:59in this.name and if this were to return
  12691. 7:43:02back false then we place invalid name
  12692. 7:43:04straight into this. name and for the get
  12693. 7:43:07we simply just return a string since
  12694. 7:43:09that's a data type for name and then
  12695. 7:43:11just return it back and it's that simple
  12696. 7:43:13and the same thing for age we validate
  12697. 7:43:15the age range set it and if it's outside
  12698. 7:43:17this range we can set it to minus one so
  12699. 7:43:19as you can see this is a bit long we
  12700. 7:43:21have our private variables then we have
  12701. 7:43:24two functions one for set one for get
  12702. 7:43:27and the same thing for age so these are
  12703. 7:43:29called fields and gets and Setters so
  12704. 7:43:32I'm going to introduce a notion called
  12705. 7:43:34properties and properties make it easier
  12706. 7:43:36to do this exact notation so let's now
  12707. 7:43:38see how property is created since these
  12708. 7:43:41fields are private we need to make
  12709. 7:43:43something that's actually public so we
  12710. 7:43:45can do something like this we can say
  12711. 7:43:47public string and we can say name with a
  12712. 7:43:49capital N so now you can see that name
  12713. 7:43:53and name are not actually clashing and
  12714. 7:43:55we can assign that in here should we
  12715. 7:43:56want to and you see we don't need this
  12716. 7:43:58do anymore because this is now different
  12717. 7:44:01it's a capital N so this one always
  12718. 7:44:04references this and this references this
  12719. 7:44:07so we need to add something else onto
  12720. 7:44:09here so if you create them
  12721. 7:44:11both now you can see we can do age
  12722. 7:44:14equals age and there'll be no problems
  12723. 7:44:16with this so now that we've made our two
  12724. 7:44:18new Fields so now we have our two new
  12725. 7:44:20public variables which are known as our
  12726. 7:44:22Fields let's see how we can turn them
  12727. 7:44:24into properties so if you remove the
  12728. 7:44:26semicolon you can actually add in some
  12729. 7:44:28curve curly braces so inside here we
  12730. 7:44:31need a getter and Setter just like we
  12731. 7:44:34had down there but we have a quicker
  12732. 7:44:35notation for it instead of making two
  12733. 7:44:38distinct functions you can actually
  12734. 7:44:39create a little section inside these
  12735. 7:44:41curly braces to get and set the name so
  12736. 7:44:44you got to think of this as a middleman
  12737. 7:44:46the same way this is a middleman we call
  12738. 7:44:49this function and this function will
  12739. 7:44:51affect the name and this is going to be
  12740. 7:44:53the same principle we make use of name
  12741. 7:44:56with a capital N but it will actually be
  12742. 7:44:58affecting the lowercase end variable
  12743. 7:45:00these are going to be our new middle BS
  12744. 7:45:02and they will directly replace the gets
  12745. 7:45:04and sets here so what we can do in here
  12746. 7:45:06is we can say get press enter go on the
  12747. 7:45:09next line and make some C braces and
  12748. 7:45:11then we can also have a set so this is
  12749. 7:45:13like our mini get a mini set function
  12750. 7:45:16just like we've got here but as you can
  12751. 7:45:18see it's a lot shorter we don't need to
  12752. 7:45:20give it a distinct name or set the
  12753. 7:45:21public or anything else we just give it
  12754. 7:45:24a simple set and get so if you have a
  12755. 7:45:26look at the compiler error it says not
  12756. 7:45:29all cause path return of value because
  12757. 7:45:31as it says here this get function is
  12758. 7:45:33actually a mini function built into this
  12759. 7:45:36property person. name which is our name
  12760. 7:45:38with a capital N and then it's do get
  12761. 7:45:41and this returns back a string so if we
  12762. 7:45:43actually just type in return this then
  12763. 7:45:45it gets rid of the compiler error but of
  12764. 7:45:47course we don't want to return nothing
  12765. 7:45:48we actually want to return name because
  12766. 7:45:50remember what I said string name and
  12767. 7:45:52intage up here as our private field they
  12768. 7:45:55actually start the data the public
  12769. 7:45:57string name and the public inage is the
  12770. 7:45:59middle mans that coordinates all of the
  12771. 7:46:01gets and sets to do with the private
  12772. 7:46:03variables so for the set just for now we
  12773. 7:46:05can say name equals and then what do we
  12774. 7:46:08give it the value of so if we hover over
  12775. 7:46:11set it doesn't actually give you any
  12776. 7:46:13indication of what it's going to be so
  12777. 7:46:15let's have a look if we press control
  12778. 7:46:17space then we don't also get the list of
  12779. 7:46:19everything that we can add inside here
  12780. 7:46:21now it's very difficult to understand
  12781. 7:46:23what to assign this to when we have set
  12782. 7:46:26name we've gave it a string name
  12783. 7:46:28parameter so we can use name down here
  12784. 7:46:30so what's our parameter in this case
  12785. 7:46:32well the function itself doesn't
  12786. 7:46:33actually tell us but I'm telling you
  12787. 7:46:34right now that the parameter is actually
  12788. 7:46:36just called value and this is a very
  12789. 7:46:38default parameter that you'll see
  12790. 7:46:40everywhere when you're doing gets and
  12791. 7:46:42sets inside a property you don't have to
  12792. 7:46:44specify value or anything it just gets
  12793. 7:46:47simply assigned in just like here where
  12794. 7:46:49we have string name this will be a
  12795. 7:46:52string value so it will look something
  12796. 7:46:53like this behind the scenes but of
  12797. 7:46:55course you can't actually type this in
  12798. 7:46:57cuz remember these properties they
  12799. 7:46:59trying to give you a short hand on how
  12800. 7:47:00to do gets and Setters so you just got
  12801. 7:47:02to remember that when you're trying to
  12802. 7:47:04set you get the property inserted as
  12803. 7:47:06value so you can start that into name so
  12804. 7:47:09that's it the name is actually sorted
  12805. 7:47:11now so before we do the same thing for
  12806. 7:47:13age let's see how we can use this inside
  12807. 7:47:15the code so we scroll down to our main
  12808. 7:47:18we have a person person and then we have
  12809. 7:47:19a console right line that prints the
  12810. 7:47:21details so we can actually do person Dot
  12811. 7:47:24and you can see now that it's slightly
  12812. 7:47:26different if you ignore the edge since
  12813. 7:47:28start to return back as a field since we
  12814. 7:47:30haven't done that yet you'll see that
  12815. 7:47:32name now has a little Cog next to it
  12816. 7:47:34sure only properties this is a new icon
  12817. 7:47:37that we haven't saw yet so this is our
  12818. 7:47:39new icon it says it's going to return us
  12819. 7:47:41back with a string and we can actually
  12820. 7:47:43execute a get and execute a set straight
  12821. 7:47:46on this name then we can say name equals
  12822. 7:47:48Harry and we don't get any compiler
  12823. 7:47:50errors whatsoever this lets us do it
  12824. 7:47:52completely so now if we do another
  12825. 7:47:54console right line and we say person do
  12826. 7:47:56return details and let's run that and
  12827. 7:47:58you should see that the name gets
  12828. 7:48:01changed there we go ABA gets changed to
  12829. 7:48:04hurry perfect so after we've got person.
  12830. 7:48:07name let's see how we can also get the
  12831. 7:48:10value so we can just say string
  12832. 7:48:12interpolation your name is and then we
  12833. 7:48:15do person and followed by a DOT and as
  12834. 7:48:18you can see the exact same list that we
  12835. 7:48:20got given over here is given right here
  12836. 7:48:22as well name also says get and set and
  12837. 7:48:25it returns back a property which is a
  12838. 7:48:27string type so we can open like this and
  12839. 7:48:30you'll notice that we don't have the
  12840. 7:48:31parentheses after it and the reason for
  12841. 7:48:33that is because the brackets indicate
  12842. 7:48:35that you're calling a function but a
  12843. 7:48:37property is just a property it's almost
  12844. 7:48:39like a variable so you're not actually
  12845. 7:48:41needing to put any brackets after it
  12846. 7:48:43because there's no function to call so
  12847. 7:48:45as you can see this is exactly the same
  12848. 7:48:47as this in this case we're actually
  12849. 7:48:49setting the value and in this case we're
  12850. 7:48:52actually getting the value so let's
  12851. 7:48:54comment this line out and let's see what
  12852. 7:48:56happens your name is Harry so that's
  12853. 7:48:59worked okay so now let's do the same
  12854. 7:49:01thing for
  12855. 7:49:02age remove the semicolon add the curly
  12856. 7:49:05brace we can say get and then return age
  12857. 7:49:09and then simply do set and then do age
  12858. 7:49:12equals value there we go so now we got
  12859. 7:49:15that set up if you return back down here
  12860. 7:49:17and we see a person dot so as you can
  12861. 7:49:19see now the age is being removed as
  12862. 7:49:21being a field as we saw in the blue icon
  12863. 7:49:24it's now been turned into a spanner
  12864. 7:49:26which means it's a property as shown
  12865. 7:49:27here show only properties so now you can
  12866. 7:49:30see it returns back an integer and we've
  12867. 7:49:32got a get in a set so we can set that to
  12868. 7:49:3425 for example and then we can append
  12869. 7:49:36onto here and your AG is person. AG you
  12870. 7:49:40see that it's a capital A just as this
  12871. 7:49:42one is a capital N and now we have
  12872. 7:49:44getting set available and again we don't
  12873. 7:49:46need the brackets inside here since this
  12874. 7:49:48is a property not a function now let's
  12875. 7:49:50run it we should see Harry 25 there you
  12876. 7:49:53go your name is Harry and your age is
  12877. 7:49:5625 perfect
  12878. 7:49:58so as you can see now is instead of
  12879. 7:50:00having our set name and get name which
  12880. 7:50:02are these two functions we actually have
  12881. 7:50:04them contained within a
  12882. 7:50:06property we call this property and it
  12883. 7:50:09affects these variables but as you can
  12884. 7:50:11see just as these functions below were a
  12885. 7:50:14middleman we've now moved the middleman
  12886. 7:50:16into these properties so as these are
  12887. 7:50:18only one line you're probably thinking
  12888. 7:50:21there's probably a better way of doing
  12889. 7:50:22this and it's a lot quicker for syntax
  12890. 7:50:24and you're right there is so let's
  12891. 7:50:26explore that so I said before about
  12892. 7:50:28about Arrow functions when you're doing
  12893. 7:50:30something like this get name you can
  12894. 7:50:32have public string get name and then you
  12895. 7:50:35can simply do Arrow name this is a very
  12896. 7:50:37common string function get a name that
  12897. 7:50:40just returns back a value and you can
  12898. 7:50:41make that into an arrow function by
  12899. 7:50:43literally just putting back name so we
  12900. 7:50:45can actually do the same thing but for
  12901. 7:50:46our properties so these both contain one
  12902. 7:50:49line so we can actually minimize this
  12903. 7:50:52and if we just block comment this
  12904. 7:50:54out we can say public string name and
  12905. 7:50:58then we can have get and then the same
  12906. 7:51:00thing with the arrow function when we
  12907. 7:51:02doing Arrow function down here you
  12908. 7:51:04simply won't use the return keyword you
  12909. 7:51:06would just type in the variable you want
  12910. 7:51:07to return so we can say get arrow and
  12911. 7:51:10then simply lowcase name and then add a
  12912. 7:51:12semicolon just so you know that that
  12913. 7:51:14section is ended and then straight after
  12914. 7:51:16we can have set and then arrow and then
  12915. 7:51:19name equals value just like that so as
  12916. 7:51:22you can see these contents are exactly
  12917. 7:51:24the same we've actually returned them
  12918. 7:51:26back in a short space and of course here
  12919. 7:51:28as we got a compile error it just
  12920. 7:51:30expects a semicolon to be right at the
  12921. 7:51:32end here there you go and now we do the
  12922. 7:51:34same thing for our
  12923. 7:51:37age we can do public int ede get arrow
  12924. 7:51:42ede and set Arrow age equals value
  12925. 7:51:46perfect so now you can see that this has
  12926. 7:51:48been drastically reduced if I cut this
  12927. 7:51:50just underneath you'll see that almost
  12928. 7:51:52all of these lines are just replaced
  12929. 7:51:54into a two and you'll see that our
  12930. 7:51:56functionality will still work exactly
  12931. 7:51:57the same if we just run the code Harry
  12932. 7:52:00and 25 is being reassigned over our
  12933. 7:52:02default variables perfect of course now
  12934. 7:52:05if you want to add some more
  12935. 7:52:06functionality inside here for example in
  12936. 7:52:09our set name we had a little check that
  12937. 7:52:11makees sure that it's a valid name
  12938. 7:52:12before we assign it so we actually take
  12939. 7:52:16this and paste it over here then you see
  12940. 7:52:19the lines grown quite a lot so what's
  12941. 7:52:21pred to do is if you're going to have
  12942. 7:52:23any data inside here that does a little
  12943. 7:52:25bit of logic then it's best to keep the
  12944. 7:52:27curly bra is since you can have a bigger
  12945. 7:52:30output and it looks a bit more sensible
  12946. 7:52:32if you're just simply getting and you're
  12947. 7:52:34setting the variables then you can leave
  12948. 7:52:35this notation so let's undo this and
  12949. 7:52:38bring back the original ones in this
  12950. 7:52:43case there we go and then we can take
  12951. 7:52:45that line and in the set we can actually
  12952. 7:52:47replace it here so as you can see this
  12953. 7:52:49is a little bit grayed out so let's put
  12954. 7:52:51the semicon at the end and let's see why
  12955. 7:52:53this is grayed out name can be
  12956. 7:52:55simplified so what we're doing now is
  12957. 7:52:57name and all of these variables are
  12958. 7:52:59actually looking at the private variable
  12959. 7:53:02whereas if you actually look down here
  12960. 7:53:03and we uncomment this you'll see that
  12961. 7:53:06set name if we double click on this one
  12962. 7:53:08this is the only reference to it which
  12963. 7:53:11is our private variable up and the
  12964. 7:53:13reason why this doesn't need to be used
  12965. 7:53:15in this case is because our parameter is
  12966. 7:53:18now called
  12967. 7:53:21value so it's saying the name is equal
  12968. 7:53:24to checking if the value is null or
  12969. 7:53:26empty assigning the value into name if
  12970. 7:53:29not and then if it is empty then we hide
  12971. 7:53:32invalid straight into name in the case
  12972. 7:53:34of our set name our name was our
  12973. 7:53:37parameter the same goes here if we
  12974. 7:53:39change this to Value we can change these
  12975. 7:53:41two for Value as you can see now this
  12976. 7:53:43dot is actually grade out and it can be
  12977. 7:53:45simplified to just simply name and the
  12978. 7:53:47same principle applies above so let's
  12979. 7:53:50comment these back out and scroll back
  12980. 7:53:52to the top so now that we've assigned
  12981. 7:53:54name there we go we've got our check
  12982. 7:53:57inside there and you can also leave this
  12983. 7:53:58on one line if you want you can even
  12984. 7:54:00drop the curly brace in just so it looks
  12985. 7:54:02like this instead of having it all on
  12986. 7:54:04one line so you could do this and this
  12987. 7:54:07would be applicable as well but the
  12988. 7:54:09second you start to put it all on one
  12989. 7:54:11line then it gets a little bit messy
  12990. 7:54:13quickly so if you leave the outer
  12991. 7:54:15bracket just like that then it can be
  12992. 7:54:17pretty short since you don't need more
  12993. 7:54:19curly braces so now that we've done that
  12994. 7:54:21for the name let's do it for the age as
  12995. 7:54:23well so we had this. age we check the
  12996. 7:54:26range and we assign the relevant values
  12997. 7:54:28so let's take that paste it over this we
  12998. 7:54:32don't need this dot anymore and age in
  12999. 7:54:34this case is actually going to be the
  13000. 7:54:36value gets passed
  13001. 7:54:37in to replace all the ages with value
  13002. 7:54:41there we go so if the value passes the
  13003. 7:54:43range check we put the value inside age
  13004. 7:54:46or not we can put minus one inside age
  13005. 7:54:49and again we can have the same thing we
  13006. 7:54:51can use our arrows inside here there we
  13007. 7:54:54go so now we've got our Cod drastically
  13008. 7:54:57reduced so we went from these big
  13009. 7:54:59functions that have to be defined with
  13010. 7:55:01specific names over to our properties
  13011. 7:55:04and they went from really small to just
  13012. 7:55:05a little bit bigger but now they have
  13013. 7:55:08advanced connections just to make sure
  13014. 7:55:10that the variables are okay before you
  13015. 7:55:12assign it in so now that we've got our
  13016. 7:55:13properties let's have a look so inside
  13017. 7:55:15our Constructor we don't also need to
  13018. 7:55:18use our local variables which is our
  13019. 7:55:20private ones directly we should actually
  13020. 7:55:23get rid of this now and we should always
  13021. 7:55:25do everything through our properties
  13022. 7:55:27name equals name and because this is a
  13023. 7:55:29capital and this is a lower case we
  13024. 7:55:31don't need this dot in
  13025. 7:55:33there there we
  13026. 7:55:35go so what you should do in the return
  13027. 7:55:38details function is also use the capital
  13028. 7:55:40n and the capital age so when you hover
  13029. 7:55:42over these you're actually referring to
  13030. 7:55:44the property because like I said before
  13031. 7:55:47these are the middleman to communicate
  13032. 7:55:48with the private variables you shouldn't
  13033. 7:55:51need to access the private variables
  13034. 7:55:53directly you should use the middle man
  13035. 7:55:54as much as possible so now that we've
  13036. 7:55:56done that I have one last thing to
  13037. 7:55:58introduce so if we actually abandon all
  13038. 7:56:00of this code just to keep it for
  13039. 7:56:02reference we have something called an
  13040. 7:56:03auto property and you briefly saw it
  13041. 7:56:06when we saw it down here so if we
  13042. 7:56:08actually just bring them back for a
  13043. 7:56:11second and if you hover over a name down
  13044. 7:56:13here it says string name get and set in
  13045. 7:56:16the curly brace but you see how the get
  13046. 7:56:18and set is empty it just has two values
  13047. 7:56:21inside there and it just ends a curly
  13048. 7:56:23brace with no actual logic you see in
  13049. 7:56:25our get we're saying age and our set
  13050. 7:56:28with saying age equals this so when
  13051. 7:56:30we're doing Auto properties what you can
  13052. 7:56:32do is you can actually make these public
  13053. 7:56:37again change these to a capital N
  13054. 7:56:39changes to a capital A and just straight
  13055. 7:56:42after it you can say Get Set just like
  13056. 7:56:47that and what that will do is although
  13057. 7:56:49they're actually made public what it's
  13058. 7:56:51doing behind the scenes is actually
  13059. 7:56:53doing this for you it actually has a
  13060. 7:56:56private and a public for you behind the
  13061. 7:56:59scenes this is just a Shand these are
  13062. 7:57:01what's called a auto property what it
  13063. 7:57:04does is it makes an anonymous property
  13064. 7:57:05behind the scenes as I'll show you in
  13065. 7:57:07Microsoft documentation right now as it
  13066. 7:57:10says here in c three or later Auto
  13067. 7:57:12implemented properties make property
  13068. 7:57:14declaration more concise when no
  13069. 7:57:16additional logic is required and what
  13070. 7:57:18that means is no additional logic all of
  13071. 7:57:22these are additional logic if you're
  13072. 7:57:24just simply assigning a value and
  13073. 7:57:26reading it back just like here then this
  13074. 7:57:29is no need to write get name and set
  13075. 7:57:32name if all you're doing is getting and
  13076. 7:57:33setting it if you're getting and setting
  13077. 7:57:35it but there is a certain amount of
  13078. 7:57:36logic as we have here we essentially
  13079. 7:57:38have a conditional operator that's
  13080. 7:57:40making a subconscious decision about
  13081. 7:57:42what to assign the name to but if you
  13082. 7:57:44just simply assign in something like
  13083. 7:57:46this then an auto property is perfect
  13084. 7:57:49they also enable client code to create
  13085. 7:57:51objects when you declare a property as
  13086. 7:57:53shown in the following example the
  13087. 7:57:54compiler creates a private Anonymous
  13088. 7:57:57backing field that can only only be
  13089. 7:57:58accessed to the properties get and
  13090. 7:58:01sets as you can see here just like in
  13091. 7:58:03our example we have public int something
  13092. 7:58:06and then get in set and then they have a
  13093. 7:58:09public in something with the get in set
  13094. 7:58:10as well and they can use it just here to
  13095. 7:58:13access it
  13096. 7:58:14directly and because they're simply just
  13097. 7:58:16assigning and getting the values and
  13098. 7:58:18there isn't any additional logic then
  13099. 7:58:20this is the best way to do it and you
  13100. 7:58:22get as it says here an anonymous private
  13101. 7:58:24backing field so this would be the same
  13102. 7:58:27thing as doing this private string name
  13103. 7:58:31and private int age so once you do that
  13104. 7:58:34and you make this into the get and set
  13105. 7:58:36properties which are these two right
  13106. 7:58:41here this and this line together make a
  13107. 7:58:44property what the auto implemented
  13108. 7:58:47property does is actually combine this
  13109. 7:58:49line and this line together and make the
  13110. 7:58:51private one behind the scenes so the
  13111. 7:58:53programmer doesn't see it but it will be
  13112. 7:58:55applicable when the program runs in the
  13113. 7:58:56compiler and then you only get publicly
  13114. 7:58:59exposed with this parameter which will
  13115. 7:59:01be our name property so it will actually
  13116. 7:59:04make the private for you if you just
  13117. 7:59:06keep it like we had it a second
  13118. 7:59:08ago just like this so if you leave it
  13119. 7:59:11like this it will actually create the
  13120. 7:59:13private variable behind the scenes as it
  13121. 7:59:15says here it's Anonymous backing field
  13122. 7:59:17and it can only be accessed through name
  13123. 7:59:20and Edge and as you can see none of our
  13124. 7:59:22code has actually changed all of these
  13125. 7:59:25still function okay and if we run the
  13126. 7:59:26code we should still get Harry and
  13127. 7:59:2925 so just to sum this video up since
  13128. 7:59:31there was a lot covered the bottom line
  13129. 7:59:33is if you're going to have any sort of
  13130. 7:59:36values that require any logic then you
  13131. 7:59:38should use normal properties if you're
  13132. 7:59:40simply just going to assign and get the
  13133. 7:59:42value and it doesn't matter and you have
  13134. 7:59:44no control over it then you can use the
  13135. 7:59:46auto implemented properties since that
  13136. 7:59:48will do the same thing with less code
  13137. 7:59:50using the gets and sets is the old way
  13138. 7:59:52of doing this and you should always use
  13139. 7:59:54the properties going forward gets and
  13140. 7:59:56Setters are very helpful to know just so
  13141. 7:59:58you understand what they are and why
  13142. 8:00:00they exist but you should note that
  13143. 8:00:02properties are the best way forward
  13144. 8:00:04properties give you access to doing the
  13145. 8:00:05same logic as it says inside here if you
  13146. 8:00:08do an auto implemented properties then
  13147. 8:00:10you're not allowed to use the logic
  13148. 8:00:12inside the set or the get so if you
  13149. 8:00:14don't want to do any logic then don't
  13150. 8:00:16use the auto implemented properties only
  13151. 8:00:18use the normal properties to make a get
  13152. 8:00:20and Setter you just do set name get the
  13153. 8:00:22parameter and assign the local variable
  13154. 8:00:25and the get just returns it getting back
  13155. 8:00:27a property you want to capitalize your
  13156. 8:00:29private variable if you had private
  13157. 8:00:31string name with a lowercase n you want
  13158. 8:00:33public string name with a capital N you
  13159. 8:00:36get the name and then you can set the
  13160. 8:00:37name with some sort of conditional
  13161. 8:00:39operator or any if statement or anything
  13162. 8:00:41inside here and the same thing goes with
  13163. 8:00:43the age you can have a public int age
  13164. 8:00:45with a capital A and A Private in age
  13165. 8:00:47with a lowas A you can get the age and
  13166. 8:00:50then set the age with a conditional
  13167. 8:00:51operator again or any other code if you
  13168. 8:00:54don't require any logic like these two
  13169. 8:00:56lines indicate then you can simply just
  13170. 8:00:57just use the auto properties as
  13171. 8:00:59demonstrated above here and they can be
  13172. 8:01:01used quite easily throughout the
  13173. 8:01:03Constructor and our functions and even
  13174. 8:01:05outside the function when the user is
  13175. 8:01:07trying to call
  13176. 8:01:09them but one thing that we haven't been
  13177. 8:01:11able to solve and you may not have
  13178. 8:01:13noticed this as a nuisance the problem
  13179. 8:01:15is when you try to return details about
  13180. 8:01:17something we're having to call this
  13181. 8:01:19function every single time if we have an
  13182. 8:01:21in age for example made that equal to 23
  13183. 8:01:24and we did console right line and simply
  13184. 8:01:25placed age inside the brackets we don't
  13185. 8:01:27have to do anything like this because it
  13186. 8:01:29simply just prints the value out if we
  13187. 8:01:31run the code you'll see that it simply
  13188. 8:01:34just says 23 this should be pretty
  13189. 8:01:36simple for our person as well but it's
  13190. 8:01:39not we need to call return details every
  13191. 8:01:41single time we want to print the
  13192. 8:01:43properties and if we don't do this the
  13193. 8:01:45only way to do it is by making some
  13194. 8:01:47string interpolation and we can say
  13195. 8:01:49person.
  13196. 8:01:51name and then also include person. AG
  13197. 8:01:54Etc and then that can print out to the
  13198. 8:01:56screen just like that
  13199. 8:01:59of course it doesn't have the same
  13200. 8:02:00format in but we can also just take this
  13201. 8:02:03place it straight inside here add our
  13202. 8:02:05person dot before them person Dot and
  13203. 8:02:08there you go then you get exactly the
  13204. 8:02:10same output as Callum return details but
  13205. 8:02:13of course you don't want to do this
  13206. 8:02:14every time and you certainly don't want
  13207. 8:02:15to have to type in return details
  13208. 8:02:17function every single time as well you
  13209. 8:02:19want to keep it as just person now of
  13210. 8:02:21course this is not going to work
  13211. 8:02:23Straight Out of the Box let's try run
  13212. 8:02:24that and see what happens so we get
  13213. 8:02:26string override person and what's going
  13214. 8:02:29on here is it's saying names space is
  13215. 8:02:31string override and then it's a class
  13216. 8:02:32person so it's just printing out this
  13217. 8:02:34person object but it's not really doing
  13218. 8:02:36anything because we haven't really told
  13219. 8:02:38it to do something so what we can do
  13220. 8:02:40here is we can actually override the two
  13221. 8:02:42string method so let me explain so let
  13222. 8:02:44me set up a couple of examples just to
  13223. 8:02:47show you what's going to happen at the
  13224. 8:02:48end so right now we call return details
  13225. 8:02:50to return these details what we can also
  13226. 8:02:52do is say dot two string and then we can
  13227. 8:02:55also do these three options return the
  13228. 8:02:58details convert it to a string or just
  13229. 8:03:00print the person straight to the console
  13230. 8:03:02at the end of this little section we're
  13231. 8:03:04going to make all three of these print
  13232. 8:03:05out the same values so let's have a look
  13233. 8:03:07now two string and printing out normally
  13234. 8:03:11these two lines right here are actually
  13235. 8:03:13doing exactly the same thing behind the
  13236. 8:03:15scenes when you place anything inside
  13237. 8:03:17these brackets inside a console right
  13238. 8:03:18line it will try and convert it to a
  13239. 8:03:20string anywhere so whether you put two
  13240. 8:03:22string or you leave it as person the
  13241. 8:03:24console right line function will still
  13242. 8:03:26accept it so just coming out the age for
  13243. 8:03:28the time being in order to make the two
  13244. 8:03:30string function work as well as just
  13245. 8:03:32placing person inside the brackets we
  13246. 8:03:34need to make a special function which is
  13247. 8:03:36an override function so let's have a
  13248. 8:03:38look so just underneath our return
  13249. 8:03:40details still within the scope of the
  13250. 8:03:42class we say public and then override
  13251. 8:03:45after we press tab we can follow it by a
  13252. 8:03:47space and we can see the functions that
  13253. 8:03:49we can override so in this video we're
  13254. 8:03:51actually going to cover two string and
  13255. 8:03:52do equals get hash code will actually
  13256. 8:03:55serve as a default hash function if I
  13257. 8:03:57show you the the documentation it says
  13258. 8:03:59here a hash code is numeric value that
  13259. 8:04:01is used to insert and identify an object
  13260. 8:04:03in a hash based collection this is
  13261. 8:04:05normally use when you're doing hash
  13262. 8:04:07functions as it says here and for the
  13263. 8:04:09time being we're not actually going to
  13264. 8:04:10be using this right now so the two we
  13265. 8:04:13can do a two string and equals but as
  13266. 8:04:15you can see our two string is available
  13267. 8:04:17right now we'll cover equals as an
  13268. 8:04:19extension in a minute so let's double
  13269. 8:04:21click on our two string and we get this
  13270. 8:04:23little template placed in if you don't
  13271. 8:04:25want the template then it's just an
  13272. 8:04:26override if returns back a string since
  13273. 8:04:28it's converting to string it needs to be
  13274. 8:04:31this exact notation if you drop the T
  13275. 8:04:33then it's got nothing to override now
  13276. 8:04:36you go be a compile error because
  13277. 8:04:37there's nothing to override itself so
  13278. 8:04:40play two string in the capitals there's
  13279. 8:04:42no parameters passed in and then we're
  13280. 8:04:43going to be returning something so the
  13281. 8:04:46thing that we're going to be returning
  13282. 8:04:47is exactly the same as our return
  13283. 8:04:48details function we actually have two
  13284. 8:04:50options here we could say return and
  13285. 8:04:52then return details so what that will do
  13286. 8:04:55is it will go to this function it will
  13287. 8:04:57call this function grab that and
  13288. 8:04:59essentially place inside here and then
  13289. 8:05:01return it back to the console in the
  13290. 8:05:03ideal world we don't actually need this
  13291. 8:05:04return details function so we could
  13292. 8:05:06comment it out but I'm going to leave it
  13293. 8:05:08in just so the demonstration doesn't
  13294. 8:05:09break down here so we can take this and
  13295. 8:05:12place it straight inside here now we
  13296. 8:05:14have two functions that essentially do
  13297. 8:05:16the same thing but the advantage is this
  13298. 8:05:18function is something that needs to be
  13299. 8:05:19called explicitly do return details
  13300. 8:05:22whereas this function the two string
  13301. 8:05:24override actually just gets called
  13302. 8:05:26naturally without having to add any
  13303. 8:05:28extensions so if we run the code now
  13304. 8:05:31you'll see that now they all equal
  13305. 8:05:32exactly the same value we get three
  13306. 8:05:34sections that have this maybe we can
  13307. 8:05:37place back SLT instead just so we can
  13308. 8:05:39have them all in the same
  13309. 8:05:41line there we go so you can see all
  13310. 8:05:44three are exactly the same so what we
  13311. 8:05:47can do now is if we comment this option
  13312. 8:05:49and we comment out this function we
  13313. 8:05:51don't have to include two string if we
  13314. 8:05:52don't want to we can also comment this
  13315. 8:05:54one out and we can keep it just as
  13316. 8:05:56person so as we do when we declare the
  13317. 8:05:58integer we say integ equal 23 and then
  13318. 8:06:00we simply print out and this is exactly
  13319. 8:06:03the notation that we want it should
  13320. 8:06:05really be this simple there you go one
  13321. 8:06:07line creates the person and one line
  13322. 8:06:10with just a variable name goes inside
  13323. 8:06:12the brackets to print them out the same
  13324. 8:06:13thing here person person Edge and Edge
  13325. 8:06:16and let's run that code now and there
  13326. 8:06:18you go you get the person printed and
  13327. 8:06:20the edge printed exactly and
  13328. 8:06:22straightforward as you may think it
  13329. 8:06:24should be so now that we've done that
  13330. 8:06:25let's explore what the equals function
  13331. 8:06:27needs to be done let's make another
  13332. 8:06:29person we can say person test equals new
  13333. 8:06:32person and we can have hurry age
  13334. 8:06:3423 then we don't need to print them out
  13335. 8:06:36in this example we just need to
  13336. 8:06:38demonstrate this equals function so if
  13337. 8:06:40you remember with strings we can have a
  13338. 8:06:41string called abber for example and we
  13339. 8:06:43can say do equals straight after it we
  13340. 8:06:46can say if ABA is equal to hurry then we
  13341. 8:06:48can do something inside here what I'm
  13342. 8:06:50trying to demonstrate is once you've got
  13343. 8:06:52an object you can place do equals and
  13344. 8:06:54then you can compare it like that so you
  13345. 8:06:56can also do that without classes so we
  13346. 8:06:58can say person do equals and then equals
  13347. 8:07:01to test and in here we could just say
  13348. 8:07:04same let's try and make these as same as
  13349. 8:07:06possible and see what happens so let's
  13350. 8:07:08keep the name exactly the same just like
  13351. 8:07:11that and let's copy the 25 down just
  13352. 8:07:13like that so now these two are
  13353. 8:07:15instantiated exactly the same way let's
  13354. 8:07:17see what happens when we've run the
  13355. 8:07:19equals then we don't get anything
  13356. 8:07:21printed which means they're not the same
  13357. 8:07:23so because this doesn't automatically
  13358. 8:07:25tell us that these two are exactly the
  13359. 8:07:26same we can override that and provide
  13360. 8:07:28our own functionality just like we did
  13361. 8:07:30with the operate overload we override
  13362. 8:07:33equals we get passed in an object and we
  13363. 8:07:35need to check something inside it so
  13364. 8:07:37let's see what we can do inside here so
  13365. 8:07:39in order to check if something is equal
  13366. 8:07:41to another thing what can we do so we
  13367. 8:07:43have a name and age if this one is the
  13368. 8:07:46same as this and this is the same as
  13369. 8:07:48this then two people are exactly the
  13370. 8:07:50same so let's try that out and see what
  13371. 8:07:52happens so we can say name which is the
  13372. 8:07:55property inside the class that we're
  13373. 8:07:56currently inside
  13374. 8:07:58and then we can use equals because this
  13375. 8:07:59is just a string parameter and then we
  13376. 8:08:02can say that equals to
  13377. 8:08:04objectname but you can see we've got a
  13378. 8:08:06little problem here this object is very
  13379. 8:08:09generic it's just an object every class
  13380. 8:08:12that you create is derived by an object
  13381. 8:08:15what that means is every class or
  13382. 8:08:17integer or double at the end of the day
  13383. 8:08:19is also just known as an object through
  13384. 8:08:21inheritance which we'll discuss further
  13385. 8:08:23on this course but just know that
  13386. 8:08:25everything inside C is always an object
  13387. 8:08:27object somewhere down the line so what
  13388. 8:08:29that means is we have to convert it from
  13389. 8:08:31an object to our custom class which is
  13390. 8:08:33person in this case so we can use this
  13391. 8:08:36thing called casting so we can say
  13392. 8:08:38person person which is just to find a
  13393. 8:08:40new variable and we can make that
  13394. 8:08:42immediately equal to object as
  13395. 8:08:46person so what we've done here is we've
  13396. 8:08:48said our object which is this parameter
  13397. 8:08:51is currently an object right now we want
  13398. 8:08:53to cast it as a person which is kind of
  13399. 8:08:55like converting into a person and then
  13400. 8:08:57play placing the values inside here so
  13401. 8:08:59now if we get rid of this comment and
  13402. 8:09:01instead of using object we actually have
  13403. 8:09:02a new local variable called person so
  13404. 8:09:05then here we can say person. name there
  13405. 8:09:07we go now we have access to the person
  13406. 8:09:09property since we've converted this
  13407. 8:09:11object into a person and of course we
  13408. 8:09:13know that we have name and age
  13409. 8:09:15available so if the name is equal to the
  13410. 8:09:19personname and the age is equal to the
  13411. 8:09:22person.
  13412. 8:09:24AG then we must have a match and of
  13413. 8:09:26course want to keep using the properties
  13414. 8:09:28we're going to use a capital A and there
  13415. 8:09:30we go so we run the conversion on one
  13416. 8:09:32line and then we check if they're equal
  13417. 8:09:34to each other now in this specific case
  13418. 8:09:36you can actually pass anything inside
  13419. 8:09:38here so let's try it we can pass Five
  13420. 8:09:40inside here and as you can see there's
  13421. 8:09:42no compile error cuz like I said five is
  13422. 8:09:45just an integer which is an object at
  13423. 8:09:47the end of the day if we make it a
  13424. 8:09:49double that works as well there is
  13425. 8:09:50literally no compile errors and even if
  13426. 8:09:52you put M to make it into a decimal
  13427. 8:09:55again there's no compile errors because
  13428. 8:09:56they're all object at the end of the day
  13429. 8:09:58so let's pass test in there what you
  13430. 8:10:00will have to know is if you do
  13431. 8:10:01accidentally pass in a double for
  13432. 8:10:03example like this you're going to hit an
  13433. 8:10:05error here which will be a runtime error
  13434. 8:10:07it will try and convert it as soon as it
  13435. 8:10:08hits this line and obviously you can't
  13436. 8:10:10convert a double into our person so what
  13437. 8:10:13you could do just to wrap it around you
  13438. 8:10:15could say if the object is a person then
  13439. 8:10:19we can proceed with a conversion just
  13440. 8:10:21like this and then what you can do for
  13441. 8:10:23safety is just outside of this you can
  13442. 8:10:25actually just return back false just so
  13443. 8:10:28the user knows that this isn't equal to
  13444. 8:10:30each other so if the object is a person
  13445. 8:10:33then we convert it as a person is is
  13446. 8:10:35used for checking and as is used for
  13447. 8:10:38converting is this object a person and
  13448. 8:10:41if it is then we try and convert it and
  13449. 8:10:44then return back if they're equal to
  13450. 8:10:45each other and if we accidentally pass
  13451. 8:10:47something in that's not a person then we
  13452. 8:10:49just return back false so let's just
  13453. 8:10:51demonstrate this
  13454. 8:10:52first not the same so let's just save
  13455. 8:10:55five inside here it says not the same so
  13456. 8:10:59let's actually go into this place a
  13457. 8:11:01break point and let's run it
  13458. 8:11:02again hover over object then we get
  13459. 8:11:06five if we place this straight inside
  13460. 8:11:09the watch you can see object is person
  13461. 8:11:11is returning back false so it's skipping
  13462. 8:11:13this entire function and going into
  13463. 8:11:15return false and then it's returning
  13464. 8:11:17back false so we get not same let's stop
  13465. 8:11:20the program change this over to test and
  13466. 8:11:23rerun it using F5 and now we can see
  13467. 8:11:25object is person is is actually set to
  13468. 8:11:27true so now if you put the object inside
  13469. 8:11:30here you can see that generically it's
  13470. 8:11:33an object right here it says object so
  13471. 8:11:36what we can do now is we can place
  13472. 8:11:38person down here as well and you see
  13473. 8:11:40that person doesn't exist but if you
  13474. 8:11:42just give it a second and we change this
  13475. 8:11:43to just the
  13476. 8:11:46P there we go so obviously the person
  13477. 8:11:48doesn't exist cuz we're only up to this
  13478. 8:11:49line we press F10 and F10 again so now
  13479. 8:11:53it's created the variable and f101 so
  13480. 8:11:55skip over it and now you can see the
  13481. 8:11:57object has went from type object to type
  13482. 8:12:00string. override. person string override
  13483. 8:12:02is the name of our namespace and do
  13484. 8:12:05person with a capital P is the name of
  13485. 8:12:07our class now you can see that the
  13486. 8:12:09object and the values are exactly the
  13487. 8:12:11same 25 ab and 25 ABA as you can see
  13488. 8:12:14right here so now both of them are
  13489. 8:12:16exactly equal to each other so when we
  13490. 8:12:18check this name here is Aba name here is
  13491. 8:12:22ABBA age 25 and age is 25 so this must
  13492. 8:12:26return back true and we can check that
  13493. 8:12:28by highlighting everything dragging and
  13494. 8:12:30dropping it down here and we can see it
  13495. 8:12:31says true there so when it returns back
  13496. 8:12:33to our code it's going to go and print
  13497. 8:12:35their
  13498. 8:12:37same perfect there you go so that's it
  13499. 8:12:40for this video but let's just have a
  13500. 8:12:41recap before we wrap up so before this
  13501. 8:12:43video we'd always print the details out
  13502. 8:12:45like this person do return details
  13503. 8:12:47obviously we don't have that function
  13504. 8:12:48anymore so let's just bring it back
  13505. 8:12:50quickly there we go so we use return
  13506. 8:12:53details and this is just a bit long have
  13507. 8:12:55to do this every single time if if you
  13508. 8:12:57want to print the details of the test
  13509. 8:12:58object as well you have to do test Dot
  13510. 8:13:00and then return details again it'd be so
  13511. 8:13:02much easier if we could simply just use
  13512. 8:13:04it like this as you would do with a
  13513. 8:13:06normal variable like an integer double
  13514. 8:13:08string Etc and now it's made possible by
  13515. 8:13:11using our public override string two
  13516. 8:13:13string function it will contain exactly
  13517. 8:13:15the same contents as our previous
  13518. 8:13:17function except we won't have to go
  13519. 8:13:19through an additional function just to
  13520. 8:13:20print out the details we can override
  13521. 8:13:22the two string and then we can call it
  13522. 8:13:24straight in the console like this and
  13523. 8:13:25finally when you're doing equals inside
  13524. 8:13:27an object you can actually build in some
  13525. 8:13:30custom functionality we can convert the
  13526. 8:13:32person using object as person but first
  13527. 8:13:35before doing the conversion you want to
  13528. 8:13:37check if the object is indeed a person
  13529. 8:13:39type before doing the conversion this
  13530. 8:13:42will give you a runtime error on this
  13531. 8:13:44line so let me just demonstrate that
  13532. 8:13:45before we continue so if I comment out
  13533. 8:13:48this one and actually just change this
  13534. 8:13:49into
  13535. 8:13:515D and then let's press into the chorde
  13536. 8:13:54right
  13537. 8:13:55here it's going to hit this line if I
  13538. 8:13:57press F10 then it's going to try and
  13539. 8:13:59convert it but you're going to get back
  13540. 8:14:00null and then if you run the code again
  13541. 8:14:03then you're going to exception thrown
  13542. 8:14:04here you're going to get a runtime
  13543. 8:14:06exception because it's a null reference
  13544. 8:14:08we're trying to run the equals function
  13545. 8:14:10on something that is null which is right
  13546. 8:14:12here null reference exception so you
  13547. 8:14:14shouldn't do this you should also make
  13548. 8:14:16sure that these are correct values
  13549. 8:14:18before doing it this is a form of error
  13550. 8:14:20checking if you don't do this then
  13551. 8:14:22you're going to have to wrap this inside
  13552. 8:14:24a TR catch and it will look something
  13553. 8:14:26like this and and the cord will be
  13554. 8:14:27considerably longer you can place these
  13555. 8:14:29two
  13556. 8:14:30lines inside here and then place your
  13557. 8:14:33cat inside here etc etc and you can
  13558. 8:14:36return false inside here but as you can
  13559. 8:14:38see the code is a bit more complicated
  13560. 8:14:40now and there's no reason for this extra
  13561. 8:14:42complication if you can prevent it with
  13562. 8:14:44an if statement so let's undo
  13563. 8:14:46that so what's best in this case is to
  13564. 8:14:49say if the object is of type person then
  13565. 8:14:52we can run the conversion if we run the
  13566. 8:14:54conversion and it's not of type person
  13567. 8:14:56then as you can see it's going to return
  13568. 8:14:57back null for the person and then when
  13569. 8:14:59you run this line it's actually going to
  13570. 8:15:01return you back a null reference
  13571. 8:15:02exception at this point but anyway the
  13572. 8:15:04whole point of this equals is to check
  13573. 8:15:06if two objects are going to be equal to
  13574. 8:15:08each other so we keep this back to test
  13575. 8:15:10because this is a custom classes you
  13576. 8:15:12can't just simply do person equals
  13577. 8:15:14equals test and assume that that will
  13578. 8:15:16just work these aren't buil-in
  13579. 8:15:18functionalities for custom classes we
  13580. 8:15:20covered operate overloading where you
  13581. 8:15:22can actually overload what you do with
  13582. 8:15:23the equal sign but we didn't cover the
  13583. 8:15:25do equals which is what we're doing
  13584. 8:15:27today in this video as well as the two
  13585. 8:15:30string this is a different kind of
  13586. 8:15:32overriding because in the other one when
  13587. 8:15:33we're doing operator overloading that's
  13588. 8:15:35specifically an operator that you're
  13589. 8:15:37just changing the parameters of whereas
  13590. 8:15:39this one is a strict override of a
  13591. 8:15:41function equals and the functions two
  13592. 8:15:43string this will make it easier to print
  13593. 8:15:45your custom classes out to the screen as
  13594. 8:15:47well as comparing them just like this
  13595. 8:15:49person. equals putting test inside the
  13596. 8:15:51bracket is a lot easier and quicker and
  13597. 8:15:53you can build in your own custom
  13598. 8:15:55functionality if you want only had the
  13599. 8:15:57age in there then you can compare the
  13600. 8:15:58age maybe you have a lot more properties
  13601. 8:16:00and you can always just chain them
  13602. 8:16:02inside a big if statement that's fine as
  13603. 8:16:04well but the point is you have full
  13604. 8:16:05control over how you compare your custom
  13605. 8:16:08classes maybe you had a list of names
  13606. 8:16:10inside a dictionary for example and you
  13607. 8:16:12wanted to add a new name or a new age or
  13608. 8:16:15a new user to this dictionary but first
  13609. 8:16:18before you add a new user to the
  13610. 8:16:19dictionary you want to check if the user
  13611. 8:16:21already exists so maybe you could have a
  13612. 8:16:23system where it uses the equals function
  13613. 8:16:25and it Loops through all of the
  13614. 8:16:26dictionary items and it checks if all
  13615. 8:16:28the values are not equal to this one if
  13616. 8:16:30you didn't build in this functionality
  13617. 8:16:32so you could do the equals function then
  13618. 8:16:34you won't be able to determine if this
  13619. 8:16:36one is equal to this one and now with
  13620. 8:16:39your equals function overrided you can
  13621. 8:16:41now have the functionality to do so
  13622. 8:16:44thank you for watching this C course I
  13623. 8:16:46hope you found it helpful in your
  13624. 8:16:47journey to becoming a proficient C
  13625. 8:16:49developer if you have any questions or
  13626. 8:16:51need further clarification on any topic
  13627. 8:16:52we have covered please don't hesitate to
  13628. 8:16:54leave a comment below and I will do my
  13629. 8:16:55best to assist you for those of you who
  13630. 8:16:57want to dive deeper in a cop consider
  13631. 8:16:59checking out my udemy course you'll find
  13632. 8:17:01more comprehensive lessons and Hands-On
  13633. 8:17:03exercises to enhance your skills
  13634. 8:17:05remember practice makes perfect keep
  13635. 8:17:07coding and exploring and I wish you all
  13636. 8:17:08the best on your programming Journey

About this transcript

This page contains the full transcript of Learn C# Programming – Full Course with Mini-Projects by freeCodeCamp.org, generated from the public captions YouTube serves with the video. The transcript has 102,522 words across 13,636 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.