简体   繁体   English

end 语句可以替换 Octave 中的 endfor 语句吗?

[英]Can end statement replace endfor statement in Octave?

This link illustrates that we should use endfor statement to close the scope of for loop.链接说明我们应该使用endfor语句来关闭for循环的 scope。

But replacing it with end results into the same behavior.但是用end结果替换它会导致相同的行为。

Does using end rather than endfor have any unexpected side effects?使用end而不是endfor是否有任何意想不到的副作用?

end is synonymous with endfor when closing a for loop. end for循环时 end 与endfor同义。

The only side effect of using end is that your code will also be compatible with MATLAB, as endfor is an extension of the language invented by Octave.使用end的唯一副作用是您的代码也将与 MATLAB 兼容,因为endfor是 Octave 发明的语言的扩展。 I recommend that you do not use endfor and the like ( endif , endfunction , endswitch , endwhile ).我建议您不要使用endfor等( endifendfunctionendswitchendwhile )。

In practice I mostly agree with Cris' answer, and I follow the same advice in principle.在实践中,我大多同意 Cris 的回答,并且我原则上遵循相同的建议。 However, I feel a need to play devil's advocate here, because it makes it sound like the octave developer's decision to provide these keywords was misguided, when actually there is good reason, and knowing the benefits of using endfor, endif allows you to make an informed decision on when and why to avoid them.但是,我觉得有必要在这里扮演魔鬼的拥护者,因为这听起来像是 octave 开发人员提供这些关键字的决定被误导了,实际上有充分的理由,并且知道使用 endfor 的好处,endif 允许您制作一个明智地决定何时以及为什么要避免它们。

Allow me to demonstrate their usefulness with an example:请允许我用一个例子来证明它们的用处:

function f(a)
    if a == 1
       disp('a == 1')
    else if a == 2
       disp('a == 2')
    else
       disp('a == 3')
    end
    disp('do some very important thing here that absolutely must be done always')
end

Can you spot the subtle but deadly bug here?你能在这里发现微妙但致命的错误吗?

Whereas if you try to run this code:而如果您尝试运行此代码:

function f(a)
    if a == 1
       disp('a == 1')
    else if a == 2
       disp('a == 2')
    else
       disp('a == 3')
    endif
    disp('do important thing')
endfunction

octave will immediately warn you that something is awfully wrong, and it will stop you from shooting yourself on the foot. octave 会立即警告您出现严重错误,它会阻止您对自己的脚开枪。

There are many other situations where endifs and endfors would protect you when simple end wouldn't.在许多其他情况下,endifs 和 endfors 会保护您,而简单的end不会。 And, at the very least, they provide a very strong visual signposting of where you are in the code (did I just close that for loop, or did I just exit an if block).而且,至少,它们为您在代码中的位置提供了非常强大的视觉标志(我是刚刚关闭了 for 循环,还是刚刚退出了 if 块)。 Especially given the state of most academic code out there, which is usually a bunch of unindented spaghetti code with lots of nested if and for blocks that goes on for miles in a single file.特别是考虑到大多数学术代码的 state,这通常是一堆未缩进的意大利面条代码,其中包含大量嵌套的iffor块,在单个文件中持续数英里。 Chances are if you follow good software engineering principles, clean, modular, properly indented code, then the extra layer of protection added by writing endfor instead of end is probably overkill.如果您遵循良好的软件工程原则、干净、模块化、适当缩进的代码,那么通过编写endfor而不是end添加的额外保护层可能是多余的。 But just because this is the case doesn't mean they're not there for good reason.但仅仅因为这种情况并不意味着他们没有充分的理由。

But, I agree, if you desire matlab compatibility, and you probably do, then it's best you use the 'slightly less safe but ultimately compatible' version.但是,我同意,如果您希望与 matlab 兼容,并且您可能会这样做,那么最好使用“安全性稍差但最终兼容”的版本。

Ironically, I often find myself writing code that looks like this:具有讽刺意味的是,我经常发现自己编写的代码如下所示:

if something
  % large block of code here
end % if

just to be matlab compatible, but still make it visually clear that the block that just closed was actually an if block.只是为了与 matlab 兼容,但仍然可以清楚地看到刚刚关闭的块实际上是一个 if 块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM