Can end statement replace endfor statement in Octave?

牧云@^-^@ 提交于 2020-12-12 07:05:15

问题


This link illustrates that we should use endfor statement to close the scope of for loop.

But replacing it with end results into the same behavior.

Does using end rather than endfor have any unexpected side effects?


回答1:


end is synonymous with endfor when closing a for loop.

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. I recommend that you do not use endfor and the like (endif, endfunction, endswitch, endwhile).




回答2:


In practice I mostly agree with Cris' answer, and I follow the same advice in principle. 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.

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.

There are many other situations where endifs and endfors would protect you when simple end wouldn't. 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). 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. 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. 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.

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.



来源:https://stackoverflow.com/questions/61151768/can-end-statement-replace-endfor-statement-in-octave

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!