Return to Video

While Loops - JavaScript Basics

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

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
ud804 - Javascript Basics
Duration:
02:53

English subtitles

改訂 Compare revisions