Re: Difference between ; and , in for loop
- To: mathgroup at smc.vnet.net
- Subject: [mg113970] Re: Difference between ; and , in for loop
- From: Pierre Albarede <pa.news at free.fr>
- Date: Fri, 19 Nov 2010 05:12:08 -0500 (EST)
- References: <firstname.lastname@example.org>
Hello, On Nov 18, 1:03 pm, graser <gra... at gmail.com> wrote: > Would anyone explain me the difference between ; and , in conditional > loop like for loop? > For example, > > 1) For[i = 1, i < 2, i++; Print[i]] > Answer is 2 > > But > > 2) For[i = 1, i < 2, i++, Print[i]] > The Answer is 1 > > Why does first loop get 2 instead of 1? Please read the documentation of For (in the help browser). In a;b ; suppresses output of a. Consequently, a can only act by changing a variable or printing (side effect). For is supposed to take 3 or 4 arguments. For[start,test,incr,body] "For[start,test,incr] does the loop with a null body. " Although For exists in Mathematica, you should not use it because is it complicated (as shown by your question) and slow. Instead you should use Map or, if you need an index, MapIndexed, which eliminate the variable i and all the problems you can have with it. Moreover, you should use output (not printout) for your results, so that you can apply another function on top of it easily and cleanly.