Are there any possibilities to use "goto" inside switch statement in javascript? - Stack Overflow

This is my example program in JS. I have to iterate or loop inside the switch statement. So I have used

This is my example program in JS. I have to iterate or loop inside the switch statement. So I have used goto, but it doesn't working.

Are there any other options to loop this or is my syntax wrong?

var input = 1;
switch (input)
{
    case 1: 
        ...
        break;
    case 2: 
        ...
        break;
    case 3: 
        ...
        if (..) { }
        else    
        {
            goto case 2;
        }
        break;
    default: 
        alert("No Return");
        break;
}

This is my example program in JS. I have to iterate or loop inside the switch statement. So I have used goto, but it doesn't working.

Are there any other options to loop this or is my syntax wrong?

var input = 1;
switch (input)
{
    case 1: 
        ...
        break;
    case 2: 
        ...
        break;
    case 3: 
        ...
        if (..) { }
        else    
        {
            goto case 2;
        }
        break;
    default: 
        alert("No Return");
        break;
}
Share Improve this question edited Nov 21, 2015 at 13:07 user663031 asked Nov 21, 2015 at 4:24 Goutham SelvarajGoutham Selvaraj 1031 gold badge2 silver badges9 bronze badges 4
  • 1 Even if this were possible, case 2 was already false, so it will be false again. Explain, specifically, what you are trying to acplish--i.e. what are you test for true? – Cory Commented Nov 21, 2015 at 4:27
  • i have added the same goto case 2 in case 1:..But Still not working – Goutham Selvaraj Commented Nov 21, 2015 at 4:29
  • 1 There is no GOTO in JS. Maybe you are looking for case fallthrough: javascript.about./od/hintsandtips/a/fallthrough.htm – Cory Commented Nov 21, 2015 at 4:33
  • This code has nothing to do with jQuery. – Regent Commented Nov 21, 2015 at 5:21
Add a ment  | 

2 Answers 2

Reset to default 4

Are there any possibilities to use "goto" inside switch statement in javascript?

No, there are no possibilities to use "goto" inside switch statement, or anywhere else, since JavaScript has no such statement.

To solve your problem, as suggested in a ment, organize your cases (moving 3 above 2) so you can use fall-through:

var input = 1;
switch (input)
{
    case 1: 
        ...
        break;
    case 3: 
        ...
        if (..) { ...; break; }
        // fall through to case 2
    case 2: 
        ...
        break;
    default: 
        alert("No Return");
        break;
}

In this case, make sure to ment the fall-through so people looking at your code don't think it's a bug. You may also need to add a hint so that your linter does not plain.

Or, just put the logic mon to 2 and 3 in a little function:

function someLogicFor2Or3() { ... }

    case 2: 
        someLogicFor2OrMaybe3();
        break;
    case 3: 
        ...
        if (..) { }
        else someLogicFor2OrMaybe3();
        break;

I have used the following technique to do this :

var input = 1;
while (1) {
  switch (input) {
  case 1: 
    ...
    break; // switch

  case 2: 
    ...
    break; // switch

  case 3: 
    ...
    if (..) { }
    else {
      input = 2; // goto case 2;
      continue;  // while
    }
    break; // switch

  default: 
    alert ("No Return");
    break;  // switch
  }
  break;  // while
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743890251a4524758.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信