admin管理员组文章数量:1360325
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
2 Answers
Reset to default 4Are 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
}
本文标签: Are there any possibilities to use quotgotoquot inside switch statement in javascriptStack Overflow
版权声明:本文标题:Are there any possibilities to use "goto" inside switch statement in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743890251a2556789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论