0

The continue statement in javascript doesn't really make sense to me, or am I over thinking this?

For example, let's say I am for inning inside an object:

for (i in JM) {              
    if (JM[i].event == 'StartLocation') {
        continue;
    }
}

If that if statement is true, it's going to technically hop over that iteration. Why do we use the word continue then? Wouldn't it make more sense to use the word break? But, break does the total opposite, it stops the loop, right? (Or maybe a statement called hop) would make sense. Never really thought of this until a couple minutes ago :P

egrunin
  • 23,366
  • 5
  • 44
  • 92
NiCk Newman
  • 1,464
  • 3
  • 19
  • 43
  • 1
    but, what is the question here? – Rasalom May 04 '15 at 12:23
  • 1
    Yup you are right..`break` will make execution to come out of the loop, wherease `continue` will make execution to stop executing whatever below `continue` and start with a next iteration... – Rakesh_Kumar May 04 '15 at 12:23
  • 2
    [`continue`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue) _skips_ the rest of the loop and immediately goes to the next iteration. – musically_ut May 04 '15 at 12:23
  • 2
    You pretty much answered your own question. Continue, continues to the next iteration. Break, breaks out of the loop. – Robert Moskal May 04 '15 at 12:23
  • 1
    I think you may indeed be overthinking this. *continue* continues the loop from the next iteration, while *break* breaks out of the loop completely. – Robin May 04 '15 at 12:23
  • 1
    In some languages, `next` and `last` are used instead of `continue` and `break`. – Phssthpok May 04 '15 at 12:31
  • Hey guys see my posts under Bill the Lizard's post. Upvoted all as well. – NiCk Newman May 04 '15 at 12:48

3 Answers3

2

Yes, break takes you out of the loop and places you at the line after the loop. continue takes you directly back to the start of the next iteration of the loop, skipping over any lines left in the loop.

for(i in JM)
{ // continue takes you directly here

    if(JM[i].event=='StartLocation'){
        continue;
    }

    // some other code

} // break takes you directly here

If you're only asking about the word choice, then there probably isn't a great answer to this question. Using continue does cause loop iteration to continue, just on the next iteration. Any keyword could have been chosen. The first person to specify a language with this behavior just chose the word "continue."

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
  • I see, so `continue` is essentially a iteration hop that continues RIGHT after its true statement? It's just confusing to me because the word continue to me, would mean continuing the loop, not hopping over anything. Would it be okay to view the continue statement as another word like 'hop' or 'next'? Those words make much more sense to me, not sure if it's me though lol. – NiCk Newman May 04 '15 at 12:31
  • 1
    @NiCkNewman Yes, the loop immediately stops its current iteration and skips over *everything* after the `continue` keyword, even if there are statements in the same if statement. Sure, if it helps you remember how `continue` actually behaves, it's fine to think of it as more of a "hop" statement. – Bill the Lizard May 04 '15 at 12:33
  • Yeah, just making sure the words have the same function behind them. If I think of using the `continue` statement, I think of a roller coaster that just keep on going, not hopping over obstacles (or in this case, an iteration). If I think of `hop`, it makes much more sense. If this is true, then hop over the iteration, not continue. But to go even deeper, I think the continue might read what is above it and if there is nothing there and if the continue is in a true statement block, the language just iterates over it. That's what I think is happening.. or so I believe. – NiCk Newman May 04 '15 at 12:38
  • Now looking at your code example. You are saying that continue `takes you to here`, that just confused me more. Are you saying that, that continue statement makes the loop re-iterate again and hops over itself? If so, can continue be a performance issue? Also, technically isn't the continue statement making the loop do more work, right? Since it has to go back? – NiCk Newman May 04 '15 at 12:42
  • 1
    @NiCkNewman No, it does't reiterate. When you hit the `continue` statement it skips over the rest of the lines in the current iteration, and just immediately starts the next iteration of the loop. It should never be a performance issue. – Bill the Lizard May 04 '15 at 12:48
  • I just found [this](http://stackoverflow.com/questions/11728757/why-are-continue-statements-bad-in-javascript), all my worrying has gone away. I thought I was going crazy. Thanks for your help, marked as best. – NiCk Newman May 04 '15 at 12:51
1

The continue keyword is similar to break, in that it influences the progress of a loop. When continue is encountered in a loop body, control jumps out of the body and continues with the loop’s next iteration.

maioman
  • 15,071
  • 4
  • 30
  • 42
1

Understand it like this:

for(i in JM){
  if(JM[i].event=='StartLocation'){
        continue;
  }
  /*This code won't executed if the if statement is true and instead will move to next iteration*/
  var testCode = 3;// Dummy code
}

break will break the for loop(come out of for loop) but continue will skip the current iteration and will move to next iteration.

You will find the relevant docs Here.

Zee
  • 8,051
  • 5
  • 31
  • 58