最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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
}
发布评论

评论列表(0)

  1. 暂无评论