我正在使用的语言中没有任何goto
,jump
函数。例如,Matlab。 你能帮我解决如何避免使用它吗?有没有简单的技巧来解决我的问题?goto和jump命令的替代方法
回答
那么,首先你不妨问一下没有这个matlab标签,你可能会得到更好的答案。这是因为几乎所有现代语言都存在这种问题。
代替goto
和jump
你应该使用条件语句像if
,if-else
或循环一样while
,for
,这取决于你想要达到的目标。
结账GOTO still considered harmful?,Why is goto poor practise?。
您应该考虑使用break
和continue
相反的:
for ...
...
if ...
goto nextstuff:
end
end
nextstuff:
你可以这样做:
for ...
...
if ...
break
end
end
而作为@andrey说,你通常可以通过if-else
goto
而不是做的:
if cond
goto label
end
...
foobar()
...
label:
foobar2()
,你可以这样做:
if ~cond
...
foobar()
...
end
foobar2()
当您使用goto语句回去,你可以通过同时替换:
而不是做的:
redothat:
foobar()
...
if cond
goto redothat;
end
你可以做:
while cond
foobar()
...
end
非常感谢您的回复,我很抱歉发现错误的标签。我的问题是我想实现一个状态图(网格编码),我必须回到我的code.I不知道是否有什么明显的地方我不能看到(很可能),但我找不到解决方案。 –
你应该更详细地描述你的问题,如果你想要一个不太普遍的答案... – Oli
我有一个100个随机(二进制)数字的序列。 –
As @Andrey提及您可以使用if
或if-else
声明。在很多情况下,像while
,for
这样的循环是if-else
和goto
的一对一替代品。
您还应该考虑使用break
和continue
声明,如@Oli上面所述。
在一些罕见的情况下,您可以使用异常(我不知道Matlab是否支持它)以便“返回”。这有点争议,但在你的情况下它可能适合。
while(true){
try {
foobar();
...
break;
}
catch(YourApplicationException e){
//do nothing, continiue looping
}
}
而且里面foobar的()在一些地方,你
if cond
throw YourApplicationException();
end
:
redothat:
foobar()
...
在一些地方,你
if cond
goto redothat;
end
你可以做
而且里面foobar的()
或者你可以这样做:
你可以这样做:
boolean isOk = false;
while(! isOk){
try {
foobar();
...
isOk=true;
}
catch(YourApplicationException e){
//do nothing, continiue looping
}
}
- 1. goto语句和替代方法C
- 2. goto语句的替代方法
- 3. Visual Basic“msgbox”和GoTo命令
- 4. Linux Bash goto命令?
- 5. 替代“goto”在PHP
- 6. IMAP搜索命令的替代方案
- 7. goto命令不起作用
- 8. SED替代命令
- 9. Python中的'in'命令的替代方法是什么?
- 10. PowerShell的源替代命令
- 11. 替代“的perl -e”命令
- 12. 在selenium webdriver中使用“Thread.sleep(5000)”命令的替代方法
- 13. 有没有对“echo”命令的替代方法?
- 14. 使用ubuntu CLI,SCREEN命令的任何替代方法
- 15. 在Java中处理命令行数据(args []的替代方法)
- 16. 桑达替代和外部命令
- 17. Xcode4中的* goto line *命令在哪里?
- 18. 双GOTO命令 - 基于DOS的时钟
- 19. 替代“清除”命令?
- 20. Awk替代命令问题
- 21. 替代niceload命令在Ubuntu?
- 22. 替代“退出”命令
- 23. 替代python中的Goto关键字
- 24. 解决方法goto
- 25. MIPS中的JUMP指令
- 26. “GOTO”命令有什么问题?
- 27. 批量使用goto方法和()
- 28. 在Java中替代goto语句
- 29. 如何使vim的方法跳转命令']]'和'[['使用现代方法语法?
- 30. 替代方法
如果没有'在MATLAB goto',那么你应该没有问题,避免它;)在M文件跳转]的 – Chris
可能重复(HTTP://计算器.com/questions/1082605/jump-in-m-file) – gnovice