English subtitles

← While Loops - JavaScript Basics

Get Embed Code
5 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. Hey James, wasn't it kind of annoying that you had to type out
  2. the same if statement twice?
  3. >> Yeah. I think I broke one of the cardinal rules of programming: don't
  4. repeat yourself.
  5. >> Mm-hm.
  6. Yeah.
  7. And with that in mind,
  8. I think it's probably time that we should talk about loops.
  9. >> So is that why we're looking at an animated GIF?
  10. >> Okay. That's one reason.
  11. The second is because that is an awesome GIF.
  12. But really, I think the important part here is that a GIF repeats itself
  13. automatically.
  14. We don't have to tell a GIF to play itself again, it just does it.
  15. And in fact, JavaScript gives us a few tools to do this as well.
  16. And we're going to go ahead and start with a while loop.
  17. >> A while loop lets us repeat a piece of code so
  18. long as some condition evaluates to true.
  19. Once it doesn't evaluate to true, we exit the while loop.
  20. The syntax is while condition.
  21. And then in curly braces, some sort of code here.
  22. So for this one we have doSomething();.
  23. >> Hey let's let's show an example.
  24. >> Okay. While(cameron.job ==="course dev") should make courses.
  25. >> I like that example.
  26. Notice how this is similar to python, but it includes conditions in parentheses,
  27. and actions and curly braces, just like with if statements.
  28. Remember, if the condition evaluates to true,
  29. we'll do the action, then ask ourselves again, is the condition true?
  30. We'll continue following this loop.
  31. Until the condition is no longer true, at which point will exit the loop.
  32. Over here, so long as my job is course developer, I'll keep making courses.
  33. The while loop will ask itself again, is Cameron still a course developer?
  34. If so, make courses.
  35. Until at some point,
  36. when my job is no longer course developer, at which point will exit the loop.
  37. >> How might that happen?
  38. >> Hm.
  39. Perhaps there's a limit on how many courses I can make.
  40. 10 courses maximum.
  41. You know what?
  42. Let's see what this looks like in code.
  43. Outside of the loop,
  44. I'll create an iterator called courses, which is going to start at 0.
  45. This is going to keep track of how many courses I've made.
  46. After making a course,
  47. I'm going to add 1 to the number of courses that I've made.
  48. Let's add an if statement to our loop that checks if the number of courses I've
  49. made is 10.
  50. And if so, changes my job to learning specialist.
  51. Afterwards, the loop exits.
  52. >> Interesting.
  53. Let's check to see if that works.
  54. Here's the make course function that console logs Made a course every time
  55. it's run.
  56. And lets console.log,(cameron.job) when we're finished running the loop.
  57. So, when we run it,
  58. we can see that it's printed made a course to the counsel log 10 times.
  59. And then when it finished,
  60. it printed out learning specialist, Cameron's new job.