MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Instructions in for


Alejandra Lozada wrote:
> Hello Group,
> 
> ______________________________________________
> QUESTION: how to introduce many instructions in for?
> ______________________________________________
> 
> 
> In C++ it is possible to use while/for and
> include many instructions:
> 
> for( i=0; i<n; i++ ){
>     instructions,
>     as many as you
>     like }
> 
> In Mathematica, one instruction (Print)  :
> 
> For[ i=1, i<n, i++, Print[i] ]
> how can I include more instructions?

What you are looking for is how to build compound expression. To do so, 
use the *CompoundExpression[]* built-in function or its shortcut ; (the 
semicolumn). For example,

For[t = 1; k = 1, k <= 5, k++, t *= k; Print[t];
  If[k < 2, Continue[]];
     t += 2]

Note that, as in C++, we can also have multiple initializations, tests, 
and incrementation in one for-loop header. In the example above, both t 
and k are initialized at the beginning of the loop. The built-in 
functions *Continue[]* and *Break[]* should do what you expect from a 
C++ background.

See

http://reference.wolfram.com/mathematica/ref/CompoundExpression.html


Regards,
-- Jean-Marc


  • Prev by Date: Re: Instructions in for
  • Next by Date: A question about N[...]
  • Previous by thread: Re: Instructions in for
  • Next by thread: Re: Instructions in for