Re: Instructions in for
- To: mathgroup at smc.vnet.net
- Subject: [mg89392] Re: Instructions in for
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 8 Jun 2008 02:31:54 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <g2dboa$9pm$1@smc.vnet.net>
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