MathGroup Archive 2006

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

Search the Archive

Re: Beginner--Programming Problem with "if"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65798] Re: Beginner--Programming Problem with "if"
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Mon, 17 Apr 2006 02:28:54 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 4/16/06 at 1:45 AM, LLCOOLT at gmx.at wrote:

>I write my programms like this

>For[i=1,1>5,i++,
>  Code line 1;
>  Code line 2;
>  Code line 3
>];

While the above is correct syntax and will work, it is generally has poorer perforamance (slower) than functional programming methods.

>This works until I use If

>is there any possibility to combine the code?

>this doesn't work: 

>If[i==1,(
>  True line 1;
>  True line 2;
>  True line 3
> ),(
>  Else line 1;
>  Else line 2;
>  Else line 3
>)]; 

You need to use correct Mathematica syntax, i.e.,

If[i == 1,
    line 1;
    line 2;
    line 3,
    line 1;
    line 2;
    line 3]

Note where the ; and , appear. The ; separates lines within a block of lines. The , separates the block which executes when the test is true (first block) from the block that executes when the test is false (second block).
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: Problem with limiits
  • Next by Date: Re: adding matrices
  • Previous by thread: Beginner--Programming Problem with "if"
  • Next by thread: Re: Beginner--Programming Problem with "if"