An alternative way to read it is "for x equals 1 through 5". It definitely
makes sense for a range. And I don't think anyone has any difficulty
intuitively understanding a for loop using =, even if "in" reads slightly
better.
Incidentally, it's not just Matlab that does it. Most variants of Basic use
it, as does Delphi, Fortran, Lua, Pascal, and Scilab. One thing you might
notice about most of the languages on the list is that they're languages
that were more oriented towards scientific computing, or languages designed
to teach programming concepts. It's a more intuitive notation when applied
to basic for loops (that is, where the variable is simply being incremented
a fixed number of times), with "in" really being of use when you want to
iterate over a data structure, rather than to iterate with a counting
variable.
I agree with Stefan's approach of using "=" for explicit ranges (which
represent the phrasing "for x equals 1 through 5") and "in" for application
to other structures ("for each x in the vector A"), with the other notation
being usable but somewhat discouraged. Besides, it's not like there's any
other natural use for = in that context.
Incidentally, it would be nice if ∈ could be used as another option - it's
just another way of saying "in", but it would look nicer in certain
mathematical contexts, and it's not like the symbol would be used in
another way in that position.
On Wednesday, 28 October 2015 00:26:44 UTC+10, Tom Breloff wrote:
>
> It's harmless, sure, but I would prefer that everyone uses "in"
> exclusively so that there's one less thing to waste brainpower on. You
> don't say "for each x equals the range 1 to n", you say "for each x in the
> range 1 to n". I don't think "=" has a place here at all except to allow
> copy/pasting of Matlab code (which creates other performance problems
> anyways).
>
> On Tue, Oct 27, 2015 at 10:04 AM, Stefan Karpinski <[email protected]
> <javascript:>> wrote:
>
>> My general approach is to only use = when the RHS is an explicit range,
>> as in `for i = 1:n`. For everything else I use `for i in v`. I would be ok
>> with dropping the = syntax at some point, but it seems pretty harmless to
>> have it.
>>
>> On Tue, Oct 27, 2015 at 8:56 AM, FANG Colin <[email protected]
>> <javascript:>> wrote:
>>
>>> Thank you. In that case I will happily stick with `in`.
>>>
>>>
>>> On Monday, October 26, 2015 at 8:43:22 PM UTC, Alireza Nejati wrote:
>>>>
>>>> There is no difference, as far as I know.
>>>>
>>>> '=' seems to be used more for explicit ranges (i = 1:5) and 'in' seems
>>>> to be used more for variables (i in mylist). But using 'in' for everything
>>>> is ok too.
>>>>
>>>> The '=' is there for familiarity with matlab. Remember that julia's
>>>> syntax was in part designed to be familiar to matlab users.
>>>>
>>>> On Tuesday, October 27, 2015 at 8:26:07 AM UTC+13, FANG Colin wrote:
>>>>>
>>>>> Hi All
>>>>>
>>>>> I have got a stupid question:
>>>>>
>>>>> Are there any difference in "for i in 1:5" and "for i = 1:5"?
>>>>>
>>>>> Does the julia community prefer one to the other? I see use of both in
>>>>> the documentations and source code.
>>>>>
>>>>> Personally I haven't seen much use of "for i = 1:5" in other
>>>>> languages.
>>>>>
>>>>> Thanks.
>>>>>
>>>>
>>
>