0

I have a for loop, but in one condition, I want to skip some steps so that I have used goto statement...

for (var rows = 0; rows < result.data.length; rows++) {
  [lbl] topOfLoop:

  var row = result.data[rows]
  if (row[0] == "") {
    goto topOfLoop;
  }

  ----- // some code
}

Its not working ? Can anyone tell me, how it could be done ?

Yoshi
  • 54,081
  • 14
  • 89
  • 103
Ashok Damani
  • 3,896
  • 4
  • 30
  • 48
  • 2
    replace 'goto', with 'break', remove '[lbl]', place the 'topOfLoop:' label statement above the 'for' statement. Or, just use 'continue'. – BGerrissen Jan 15 '13 at 10:10
  • 3
    There is no goto in javascript. – Ravi Y Jan 15 '13 at 10:11
  • 2
    You definitely want to be using continue. – SBI Jan 15 '13 at 10:12
  • "go to" is a bad "engineering" practice, I have read somewhare I can't remember! – sadaf2605 Jan 15 '13 at 10:13
  • And no matter what language you are using if you EVER use 'goto' then your code is bad. goto is considered very bad (spagetti anyone?) https://www.google.com/search?q=why+goto+is+bad&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a – Paul Sullivan Jan 15 '13 at 10:14
  • I wonder why you even tried that... of course it's not working because such a thing does not exist in JavaScript. What happened to reading tutorials? – Felix Kling Jan 15 '13 at 10:17
  • ::but in continue keyword, the value of rows also will increment & i dont want to increment it... – Ashok Damani Jan 15 '13 at 10:24
  • 1
    @user1948530: But if `row` is not incremented, `result.data[rows][0]` will always be the same value and your `goto` statement will effectively be an infinite loop. Or is there other code in between? – Felix Kling Jan 15 '13 at 10:32
  • 1
    @Felix it [began as a joke](http://summerofgoto.com/) but looks like people are actually using it. It was published [here](http://stackoverflow.com/a/9751229/447356). (If you ask me both that post here and that site should be removed) – Shadow The GPT Wizard Jan 15 '13 at 11:04
  • @ShadowWizard: `:-O` I didn't know that. – Felix Kling Jan 15 '13 at 11:18

3 Answers3

4

i want to skip some steps so that i'v used goto statement...

Use continue statement instead of goto

for (var rows = 0; rows < result.data.length; rows++) 
    {
        var row = result.data[rows]
        if (row[0] == "") 
        {
           continue;
        }
Habib
  • 219,104
  • 29
  • 407
  • 436
  • ::but in continue keyword, the value of rows also will increment & i dont want to increment it.... – Ashok Damani Jan 15 '13 at 10:23
  • @user1948530, you can decrement the row and then use continue, but I am not sure why you want such thing ? – Habib Jan 15 '13 at 10:26
4

Pretty sure you want to use continue;:

for (var rows = 0; rows < result.data.length; rows++)  {
    var row = result.data[rows];
    if (row[0] == "") {
        continue;  
    }
    // some code
}
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
1

Use continue statement in your code;

The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears.

It is one of the Jump statements.

if (row[0] == "")
{
     continue;  
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364