For 循环中注释的放置
Placement of Comments in For Loops
我的懒惰导致写了一些 for 循环,如下所示:
z={};
for i=2:length(a)
temp=b{i};
if(length(temp)==2)
c=temp{2};
d=strtrim(c);
e=strsplit(d, ' ');
z{(i-1)}=e;
else
z{(i-1)}={};
endif
endfor
%comment explaining indexing choices
但我想为正在查看它的其他人在线注释,改为:
z={};
for i=2:length(a)
temp=b{i};
if(length(temp)==2)
c=temp{2};%Comment explaining this indexing choice
d=strtrim(c);
e=strsplit(d, ' ');
z{(i-1)}=e;
else
z{(i-1)}={};
endif
endfor
其中唯一的区别是注释被移动到循环内和具有关键中间步骤的行的末尾。更改导致此工作循环立即崩溃。
TL:DR:有人知道 Octave 中的一项规定,即评论必须在自己的行上吗?
没有。更有可能是您在移动注释时引入了语法错误(例如,您可能不小心删除了末尾的 ;
)。 "Causing it to crash" 信息量不大,如果包含错误消息会更清楚。
但是,为了回答您的问题,有一个一个场景,其中八度音阶特别不允许评论,那就是当您在多行上执行字符串延续时,特别是当使用 double-quotes:
mystr = "one \
two, \ % this is the second number
three.";
以上将失败,因为除了换行符之外,反斜杠后应该没有其他字符,甚至没有一个space.
在这种非常特殊的情况下,您确实需要将评论移到字符串结构之外:
mystr = "one \
two, \
three."; % line two refers to the second number
或者,分步创建字符串,以便您发表评论:
mystr = ['one ', ...
'two, ', ... this is the second number.
'three'];
请注意“...”充当注释运算符,因此此处不需要 %。
我的懒惰导致写了一些 for 循环,如下所示:
z={};
for i=2:length(a)
temp=b{i};
if(length(temp)==2)
c=temp{2};
d=strtrim(c);
e=strsplit(d, ' ');
z{(i-1)}=e;
else
z{(i-1)}={};
endif
endfor
%comment explaining indexing choices
但我想为正在查看它的其他人在线注释,改为:
z={};
for i=2:length(a)
temp=b{i};
if(length(temp)==2)
c=temp{2};%Comment explaining this indexing choice
d=strtrim(c);
e=strsplit(d, ' ');
z{(i-1)}=e;
else
z{(i-1)}={};
endif
endfor
其中唯一的区别是注释被移动到循环内和具有关键中间步骤的行的末尾。更改导致此工作循环立即崩溃。
TL:DR:有人知道 Octave 中的一项规定,即评论必须在自己的行上吗?
没有。更有可能是您在移动注释时引入了语法错误(例如,您可能不小心删除了末尾的 ;
)。 "Causing it to crash" 信息量不大,如果包含错误消息会更清楚。
但是,为了回答您的问题,有一个一个场景,其中八度音阶特别不允许评论,那就是当您在多行上执行字符串延续时,特别是当使用 double-quotes:
mystr = "one \
two, \ % this is the second number
three.";
以上将失败,因为除了换行符之外,反斜杠后应该没有其他字符,甚至没有一个space.
在这种非常特殊的情况下,您确实需要将评论移到字符串结构之外:
mystr = "one \
two, \
three."; % line two refers to the second number
或者,分步创建字符串,以便您发表评论:
mystr = ['one ', ...
'two, ', ... this is the second number.
'three'];
请注意“...”充当注释运算符,因此此处不需要 %。