一般来说,在else语句下使用goto循环语句是可以的吗?

我有一个任务,需要编程让机器人(AUBO)拾取不同的物品并按照特定的顺序放置它们(点A,B,C,D)。 我使用了一些名为pim60的视觉系统。因此,如果检测到物体,它会去拾取,并且程序的其余部分是落产品的路线点。第一个问题是我想让它到达下一个落点,而第二个问题是,不能跳过下一个检测到该落点的物体之前的落点。

在我的代码中,我编写了这样一个相当冗长的程序。

::LoopA::
script_common_interface(“SICKCamera”,“takePhoto”)
script_common_interface(“SICKCamera”,“getResult”)
Located = script_common_interface(“SICKCamera”,“partLocated”)
if(Located == 1) then
.
.
.
在位置A放置
else
goto LoopA
end

::LoopB::
script_common_interface(“SICKCamera”,“takePhoto”)
script_common_interface(“SICKCamera”,“getResult”)
Located = script_common_interface(“SICKCamera”,“partLocated”)
if(Located == 1) then
.
.
.
在位置B放置
else
goto LoopB
end

::LoopC::
script_common_interface(“SICKCamera”,“takePhoto”)
script_common_interface(“SICKCamera”,“getResult”)
Located = script_common_interface(“SICKCamera”,“partLocated”)
if(Located == 1) then
.
.
.
在位置C放置
else
goto LoopC
end

::LoopD::
script_common_interface(“SICKCamera”,“takePhoto”)
script_common_interface(“SICKCamera”,“getResult”)
Located = script_common_interface(“SICKCamera”,“partLocated”)
if(Located == 1) then
.
.
.
在位置D放置
else
goto LoopD
end

没有错误,程序按预期运行。但是,我想知道是否有更好的方法。

点赞
用户235548
用户235548

唯一被普遍接受的使用 goto 的用例是错误处理,例如跳转到清理代码。但即使为此,通常也可以和应该避免使用它。

你可能需要像这样做:

-- 循环 B
repeat
  take photo, etc.
  located = ...
until(located == 1)

放置在B位置

此外,如果您正在重复相同的代码三次,则应将其提取为一个函数,并可能将位置作为参数给出。或者至少将整个代码放入一个for循环中。

2019-06-14 16:41:43