MathGroup Archive 2008

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

Search the Archive

Re: Instructions in for

  • To: mathgroup at smc.vnet.net
  • Subject: [mg89390] Re: Instructions in for
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sun, 8 Jun 2008 02:31:32 -0400 (EDT)

>______________________________________________
>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?

Use a compound expression. Look up compound expression in the
Documentation Center. As an example;

For[i = 1, i < 5, i++, Print[i]; Print[2 i]]

will print 10 lines.

Also, while it is clearly possible to use For and code in
Mathematica in a similar fashion to what would be done in C or
C++, this almost always leads to much poorer performance. My
experience with Mathematica indicates For in Mathematica is very
much like goto in C. It is there and can be used, but good code
that runs efficiently avoids this usage.



  • Prev by Date: Re: Major problem with 6.0.2.1
  • Next by Date: ListAnimate or movie with No AppearanceElements
  • Previous by thread: Re: Instructions in for
  • Next by thread: Re: Instructions in for