简体   繁体   English

for循环范围语法

[英]for loop range syntax

I spent a couple of hours debugging a problem that I would have thought would have been a syntax error. 我花了几个小时来调试一个我认为会出现语法错误的问题。

a = zeros(3);
for i=1:1size(a,2) % note the missing colon between 1 and size(a,2)
    i
end

The following only displays 以下仅显示

ans = 3
1

Essentially, it seems Matlab/Octave parses the above as: 从本质上讲,似乎Matlab / Octave将上述内容解析为:

for i=1:1
    size(a,2)
    i
end

Note however that 但请注意

i=1:1size(a,2)

produces a syntax error. 产生语法错误。 Is there a good reason that Matlab/Octave has this for loop syntax? 有没有一个很好的理由Matlab / Octave有这个for循环语法? Is there something that it's supposed to make easier? 是否有一些它应该变得更容易? Just curious if anyone else has any thoughts about it. 只是好奇是否有人对此有任何想法。 Thanks. 谢谢。

It is indeed a bit of a surprise that Matlab's syntax allows this. Matlab的语法允许这一点确实有点令人惊讶。 I don't know why this is allowed. 我不知道为什么这是允许的。 One reason might be to allow for-loops on one line: 一个原因可能是在一行上允许for循环:

>> for i=1:3 disp(i);end
     1
     2
     3

But interestingly, removing the space is not allowed: 但有趣的是,不允许删除空格:

>> for i=1:3disp(i);end
 for i=1:3disp(i);end
        |
Error: Unexpected MATLAB operator.

This reason for this is probably that a number followed by d is another way of writing a floating point number ( 3d10 == 3e10 ), so the parser/tokenizer initially thinks you define a number, but then gets confused when it sees the i . 这个原因可能是d之后的数字是写浮点数的另一种方式( 3d10 == 3e10 ),因此解析器/标记器最初认为你定义了一个数字,但是当它看到i时会感到困惑。 Daniel's example with fprintf does work, since a number followed by an f is not a valid number, so the tokenizer understands that you started a new token. Daniel的fprintf示例确实有效,因为后跟f的数字不是有效数字,因此tokenizer会理解您启动了一个新令牌。

I guess that many years ago (>30?), when they defined matlab's syntax, they did not foresee that this could introduce this kind of hard-to-spot problems. 我想多年前(> 30?),当他们定义matlab的语法时,他们没有预见到这会引入这种难以发现的问题。 I guess matlab was originally written by engineers for engineers, and not by someone who knows how to design a general purpose programming language. 我猜matlab最初是由工程师为工程师编写的,而不是由知道如何设计通用编程语言的人编写的。 Other languages like C or Python use punctuation to separate loop conditions from loop body, so there is no ambiguity. 其他语言(如C或Python)使用标点符号将循环条件与循环体分开,因此没有歧义。 I don't know if it is still possible to correct Matlab's syntax, since it might break old code that relies on the current behavior. 我不知道是否仍然可以纠正Matlab的语法,因为它可能会破坏依赖于当前行为的旧代码。

At least, if you use a recent version of Matlab, the editor warns for various problems in your code. 至少,如果您使用最新版本的Matlab,编辑器会警告您的代码中存在各种问题 Paying attention to the small red dashes in the right hand border could have saved you a few hours of debugging time (but maybe you were using octave). 注意右边框中的小红色破折号可以为你节省几个小时的调试时间(但也许你正在使用八度音阶)。 I try to make it a habit to fix all the warnings it indicates. 我试图养成一个习惯来修复它所表示的所有警告。 For your code, it shows the following: 对于您的代码,它显示以下内容:

编辑截图

Your code is equivalent to 你的代码相当于

a = zeros(3);
for i=1:1
    size(a,2)
    i
end

There are some places where everyone would use newline or white space, but the parser itself does not require. 有些地方每个人都会使用换行符或空格,但解析器本身并不需要。

A minimal loop: 最小循环:

for i=1:3fprintf('%d',i),end

but I recommend to use at least a comma seperated version, everything else is horrible to read: 但我建议至少使用一个逗号分隔版本,其他一切都很难读:

for i=1:3,fprintf('%d',i),end

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

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