YouTube2Text

Programming Basics: Statements & Functions: Crash Course Computer Science #12 — Transcript

by CrashCourse · 2,389 words · 223 segments · language en · Watch on YouTube

Full transcript

  1. 0:03Hi, I’m Carrie Anne, and welcome to CrashCourse Computer Science!
  2. 0:05Last episode we discussed how writing programs in native machine code, and having to contend
  3. 0:09with so many low level details, was a huge impediment to writing complex programs.
  4. 0:14To abstract away many of these low-level details, Programming Languages were developed that
  5. 0:18let programmers concentrate on solving a problem with computation, and less on nitty gritty
  6. 0:22hardware details.
  7. 0:23So today, we’re going to continue that discussion, and introduce some fundamental building blocks
  8. 0:28that almost all programming languages provide.
  9. 0:30INTRO
  10. 0:40Just like spoken languages, programming languages have statements.
  11. 0:43These are individual complete thoughts, like “I want tea” or “it is raining”.
  12. 0:47By using different words, we can change the meaning; for example, “I want tea” to
  13. 0:51“I want unicorns”.
  14. 0:52But we can’t change “I want tea” to “I want raining” - that doesn’t make
  15. 0:54grammatical sense.
  16. 0:55The set of rules that govern the structure and composition of statements in a language
  17. 0:59is called syntax.
  18. 1:00The English language has syntax, and so do all programming languages.
  19. 1:03“A equals 5” is a programming language statement.
  20. 1:06In this case, the statement says a variable named A has the number 5 stored in it.
  21. 1:10This is called an assignment statement because we're assigning a value to a variable.
  22. 1:13To express more complex things, we need a series of statements, like “A is 5, B is
  23. 1:18ten, C equals A plus B”
  24. 1:20This program tells the computer to set variable ‘A’ equal to 5, variable ‘B’ to 10,
  25. 1:25and finally to add ‘A’ and ‘B’ together, and put that result, which is 15, into -- you
  26. 1:29guessed it -- variable C.
  27. 1:31Note that we can call variables whatever we want.
  28. 1:33Instead of A, B and C, it could be apples, pears, and fruits.
  29. 1:36The computer doesn’t care, as long as variables are uniquely named.
  30. 1:39But it’s probably best practice to name them things that make sense in case someone
  31. 1:42else is trying to understand your code.
  32. 1:44A program, which is a list of instructions, is a bit like a recipe: boil water, add noodles,
  33. 1:49wait 10 minutes, drain and enjoy.
  34. 1:50In the same way, the program starts at the first statement and runs down one at a time
  35. 1:55until it hits the end.
  36. 1:56So far, we’ve added two numbers together.
  37. 1:58Boring.
  38. 1:59Let’s make a video game instead!
  39. 2:00Of course, it’s way too early to think about coding an entire game, so instead, we’ll
  40. 2:04use our example to write little snippets of code that cover some programming fundamentals.
  41. 2:08Imagine we’re building an old-school arcade game where Grace Hopper has to capture bugs
  42. 2:12before they get into the Harvard Mark 1 and crash the computer!
  43. 2:15On every level, the number of bugs increases.
  44. 2:18Grace has to catch them before they wear out any relays in the machine.
  45. 2:21Fortunately, she has a few extra relays for repairs.
  46. 2:24To get started, we’ll need to keep track of a bunch of values that are important for
  47. 2:27gameplay, like what level the player is on, the score, the number of bugs remaining, as
  48. 2:32well as the number of spare relays in Grace’s inventory.
  49. 2:34So, we must “initialize” our variables, that is, set their initial value: “level
  50. 2:39equals 1, score equals 0, bugs equals 5, spare relays equals 4, and player name equals “Andre”.
  51. 2:45To create an interactive game, we need to control the flow of the program beyond just
  52. 2:48running from top to bottom.
  53. 2:50To do this, we use Control Flow Statements.
  54. 2:52There are several types, but If Statements are the most common.
  55. 2:55You can think of them as “If X is true, then do Y”.
  56. 2:59An English language example is: “If I am tired, then get tea”
  57. 3:02So if “I am tired” is a true statement, then I will go get tea
  58. 3:06If “I am tired” is false, then I will not go get tea.
  59. 3:09An IF statement is like a fork in the road.
  60. 3:11Which path you take is conditional on whether the expression is true or false -- so these
  61. 3:15expressions are called Conditional Statements.
  62. 3:18In most programming languages, an if statement looks something like …. “If, expression,
  63. 3:21then, some code, then end the if statement”.
  64. 3:24For example, if “level” is 1, then we set the score to zero, because the player
  65. 3:28is just starting.
  66. 3:29We also set the number of bugs to 1, to keep it easy for now.
  67. 3:32Notice the lines of code that are conditional on the if-statement are nested between the
  68. 3:35IF and END IF.
  69. 3:37Of course, we can change the conditional expression to whatever we want to test, like “is score
  70. 3:40greater than 10” 5 or “is bugs less than 1”.
  71. 3:43And If-Statements can be combined with an ELSE statement, which acts as a catch-all if the
  72. 3:46expression is false.
  73. 3:47If the level is not 1, the code inside the ELSE block will be executed instead, and the
  74. 3:51number of bugs that Grace has to battle is set to 3 times the level number.
  75. 3:55So on level 2, it would be six bugs, and on level 3 there’s 9, and so on.
  76. 3:59Score isn’t modified in the ELSE block, so Grace gets to keep any points earned.
  77. 4:03Here are some examples of if-then-else statements from some popular programming languages -- you
  78. 4:07can see the syntax varies a little, but the underlying structure is roughly the same.
  79. 4:11If-statements are executed once, a conditional path is chosen, and the program moves on.
  80. 4:15To repeat some statements many times, we need to create a conditional loop.
  81. 4:19One way is a while statement, also called a while loop.
  82. 4:22As you might have guessed, this loops a piece of code “while” a condition is true.
  83. 4:25Regardless of the programming language, they look something like this:
  84. 4:28In our game, let’s say at certain points, a friendly colleague restocks Grace with relays!
  85. 4:32Hooray!
  86. 4:33To animate him replenishing our stock back up to a maximum of 4, we can use a while loop.
  87. 4:37Let’s walk through this code.
  88. 4:39First we’ll assume that Grace only has 1 tube left when her colleague enters.
  89. 4:43When we enter the while loop, the first thing the computer does is test its conditional…is
  90. 4:47relays less than 4?
  91. 4:49Well, relays is currently 1, so yes.
  92. 4:52Now we enter the loop!
  93. 4:53Then, we hit the line of code: “relays equals relays plus 1”.
  94. 4:56This is a bit confusing because the variable is using itself in an assignment statement,
  95. 5:00so let's unpack it.
  96. 5:01You always start by figuring out the right side of the equals sign first, so what does
  97. 5:05“relays plus 1” come out to be?
  98. 5:07Well, relays is currently the value 1, so 1 plus 1 equals 2.
  99. 5:11Then, this result gets saved back into the variable relays, writing over the old value,
  100. 5:16so now relays stores the value 2.
  101. 5:17We’ve hit the end of the while loop, which jumps the program back up.
  102. 5:21Just as before, we test the conditional to see if we’re going to enter the loop.
  103. 5:24Is relays less than 4?
  104. 5:26Well, yes, relays now equals 2, so we enter the loop again!
  105. 5:292 plus 1 equals 3.
  106. 5:30So 3 is saved into relays.
  107. 5:32Loop again.
  108. 5:33Is 3 less than 4?
  109. 5:34Yes it is!
  110. 5:35Into the loop again.
  111. 5:363 plus 1 equals 4.
  112. 5:38So we save 4 into relays.
  113. 5:39Loop again.
  114. 5:40Is 4 less than 4?....
  115. 5:41No!
  116. 5:42So the condition is now false, and thus we exit the loop and move on to any remaining
  117. 5:46code.
  118. 5:47That’s how a while loop works!
  119. 5:48There’s also the common For Loop.
  120. 5:50Instead of being a condition-controlled loop that can repeat forever until the condition
  121. 5:53is false, a FOR loop is count-controlled; it repeats a specific number of times.
  122. 5:58They look something like this:
  123. 5:59Now, let’s put in some real values.
  124. 6:01This example loops 10 times, because we’ve specified that variable ‘i’ starts at
  125. 6:05the value 1 and goes up to 10.
  126. 6:07The unique thing about a FOR loop is that each time it hits NEXT, it adds one to ‘i’.
  127. 6:11When ‘i’ equals 10, the computer knows it’s been looped 10 times, and the loop
  128. 6:15exits.
  129. 6:16We can set the number to whatever we want -- 10, 42, or a billion -- it’s up to us.
  130. 6:20Let’s say we want to give the player a bonus at the end of each level for the number of
  131. 6:24vacuum relays they have left over.
  132. 6:25As the game gets harder, it takes more skill to have unused relays, so we want the bonus
  133. 6:30to go up exponentially based on the level.
  134. 6:32We need to write a piece of code that calculates exponents - that is, multiplying a number
  135. 6:36by itself a specific number of times.
  136. 6:38A loop is perfect for this!
  137. 6:40First lets initialize a new variable called “bonus” and set it to 1.
  138. 6:43Then, we create a FOR loop starting at 1, and looping up to the level number.
  139. 6:48Inside that loop, we multiply bonus times the number of relays, and save that new value
  140. 6:52back into bonus.
  141. 6:53For example, let’s say relays equals 2, and level equals 3.
  142. 6:57So the FOR loop will loop three times, which means bonus is going to get multiplied by
  143. 7:01relays... by relays... by relays.
  144. 7:03Or in this case, times 2, times 2, times 2, which is a bonus of 8!
  145. 7:07That’s 2 to the 3rd power!
  146. 7:08This exponent code is useful, and we might want to use it in other parts of our code.
  147. 7:12It’d be annoying to copy and paste this everywhere, and have to update the variable
  148. 7:15names each time.
  149. 7:16Also, if we found a bug, we’d have to hunt around and update every place we used it.
  150. 7:21It also makes code more confusing to look at.
  151. 7:23Less is more!
  152. 7:24What we want is a way to package up our exponent code so we can use it, get the result, and
  153. 7:28not have to see all the internal complexity.
  154. 7:30We’re once again moving up a new level of abstraction!
  155. 7:40To compartmentalize and hide complexity, programming languages can package pieces of code into
  156. 7:44named functions, also called methods or subroutines in different programming languages.
  157. 7:49These functions can then be used by any other part of that program just by calling its name.
  158. 7:53Let’s turn our exponent code into a function!
  159. 7:55First, we should name it.
  160. 7:57We can call it anything we want, like HappyUnicorn, but since our code calculates exponents, let’s
  161. 8:02call it exponent.
  162. 8:03Also, instead of using specific variable names, like “relays” and “levels”, we specify
  163. 8:07generic variable names, like Base and Exp, whose initial values are going to be “passed”
  164. 8:12into our function from some other part of the program.
  165. 8:15The rest of our code is the same as before, now tucked into our function and with new
  166. 8:19variable names.
  167. 8:20Finally, we need to send the result of our exponent code back to the part of the program
  168. 8:24that requested it.
  169. 8:25For this, we use a RETURN statement, and specify that the value in ‘result’ be returned.
  170. 8:29So our full function code looks like this:
  171. 8:32Now we can use this function anywhere in our program, simply by calling its name and passing
  172. 8:36in two numbers.
  173. 8:37For example, if we want to calculate 2 to the 44th power, we can just call “exponent
  174. 8:412 comma 44.”
  175. 8:43and like 18 trillion comes back.
  176. 8:45Behind the scenes, 2 and 44 get saved into variables Base and Exp inside the function,
  177. 8:50it does all its loops as necessary, and then the function returns with the result.
  178. 8:54Let’s use our newly minted function to calculate a score bonus.
  179. 8:58First, we initialize bonus to 0.
  180. 9:00Then we check if the player has any remaining relays with an if-statement.
  181. 9:04If they do, we call our exponent function, passing in relays and level, which calculates
  182. 9:08relays to the power of level, and returns the result, which we save into bonus.
  183. 9:12This bonus calculating code might be useful later, so let’s wrap it up as a function too!
  184. 9:17Yes, a function that calls a function!
  185. 9:19And then, wait for it…. we can use this function in an even more complex function.
  186. 9:24Let’s write one that gets called everytime the player finishes a level.
  187. 9:27We’ll call it “levelFinished” - it needs to know the number of relays left, what level
  188. 9:32it was, and the current score; those values have to get passed in.
  189. 9:35Inside our function, we’ll calculate the bonus, using our calcBonus function, and add
  190. 9:40that to the running score.
  191. 9:42Also, if the current score is higher than the game’s high score, we save the new high
  192. 9:46score and the players name.
  193. 9:48Finally, we return the current score.
  194. 9:50Now we’re getting pretty fancy.
  195. 9:51Functions are calling functions are calling functions!
  196. 9:54When we call a single line of code, like this the complexity is hidden.
  197. 9:57We don’t see all the internal loops and variables, we just see the result come back
  198. 10:01as if by magic…. a total score of 53.
  199. 10:04But it’s not magic, it’s the power of abstraction!
  200. 10:07If you understand this example, then you understand the power of functions, and the entire essence
  201. 10:11of modern programming.
  202. 10:13It’s not feasible to write, for example, a web browser as one gigantically long list
  203. 10:17of statements.
  204. 10:18It would be millions of lines long and impossible to comprehend!
  205. 10:21So instead, software consists of thousands of smaller functions, each responsible for
  206. 10:25different features.
  207. 10:27In modern programming, it’s uncommon to see functions longer than around 100 lines
  208. 10:30of code, because by then, there’s probably something that should be pulled out and made
  209. 10:34into its own function.
  210. 10:35Modularizing programs into functions not only allows a single programmer to write an entire
  211. 10:39app, but also allows teams of people to work efficiently on even bigger programs.
  212. 10:44Different programmers can work on different functions, and if everyone makes sure their
  213. 10:47code works correctly, then when everything is put together, the whole program should
  214. 10:51work too!
  215. 10:51And in the real world, programmers aren’t wasting time writing things like exponents.
  216. 10:56Modern programming languages come with huge bundles of pre-written functions, called Libraries.
  217. 11:01These are written by expert coders, made efficient and rigorously tested, and then given to everyone.
  218. 11:06There are libraries for almost everything, including networking, graphics, and sound
  219. 11:10-- topics we’ll discuss in future episodes.
  220. 11:12But before we get to those, we need to talk about Algorithms.
  221. 11:16Intrigued?
  222. 11:17You should be.
  223. 11:18I’ll see you next week.

About this transcript

This page contains the full transcript of Programming Basics: Statements & Functions: Crash Course Computer Science #12 by CrashCourse, generated from the public captions YouTube serves with the video. The transcript has 2,389 words across 223 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 choose a caption track available on the video.

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, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.