f



if with two conditions

Hi all,

Is there a way to build an if statement with two conditions such as:
if[a>b and a>0,...]. If there is, could you let me know the syntax?

Thanks
nt

0
nt
3/30/2010 10:01:13 AM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

6 Replies
4934 Views

Similar Articles

[PageSpeed] 33

f[x_, y_] := If[x > y && Mod[x, 2] == 0, 1, 0]

g[x_, y_] := If[And[x > y, Mod[x, 2] == 0], 1, 0]

mat = Table[{x, y}, {x, 0, 4}, {y, 0, 4}];

Apply[f, mat, {2}]

{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, 
   {1, 1, 0, 0, 0}, {0, 0, 0, 0, 0}, 
   {1, 1, 1, 1, 0}}

% == Apply[g, mat, {2}]

True


Bob Hanlon

---- nt <sagittarius5962@gmail.com> wrote: 

=============
Hi all,

Is there a way to build an if statement with two conditions such as:
if[a>b and a>0,...]. If there is, could you let me know the syntax?

Thanks
nt



0
Bob
3/31/2010 10:24:46 AM
"nt" <sagittarius5962@gmail.com> wrote in message 
news:hosi59$o49$1@smc.vnet.net...
> Hi all,
>
> Is there a way to build an if statement with two conditions such as:
> if[a>b and a>0,...]. If there is, could you let me know the syntax?
>
> Thanks
> nt
>


a = 3;
b = 2;
If[a > b && a > 0, Print["correct"], Print["try again"]]

or

a = 3;
b = 2;
If[And[a > b, a > 0], Print["correct"], Print["try again"]]

?If

--Nasser 



0
Nasser
3/31/2010 10:25:39 AM
On Mar 30, 1:01 pm, nt <sagittarius5...@gmail.com> wrote:
> Hi all,
>
> Is there a way to build an if statement with two conditions such as:
> if[a>b and a>0,...]. If there is, could you let me know the syntax?
>
> Thanks
> nt

Hello

I believe this is what you're looking for:

If[ a>b && a>0 , ... ]
or in FullForm:
If[ And[a>b, a>0] , ... ]

0
David
3/31/2010 10:26:01 AM
Look up "And" or "&&".

Bobby

On Tue, 30 Mar 2010 05:01:24 -0500, nt <sagittarius5962@gmail.com> wrote:

> Hi all,
>
> Is there a way to build an if statement with two conditions such as:
> if[a>b and a>0,...]. If there is, could you let me know the syntax?
>
> Thanks
> nt
>


-- 
DrMajorBob@yahoo.com

0
DrMajorBob
3/31/2010 10:26:11 AM
You almost had it.

If[ And[ a>b, a>0], ...]

There are a number of ways to write it

If[ (a>b)~And~(a>0), ...],

If[ (a>b) && (a>0), ...]

Most people like the last one.

Daniel

0
dr
3/31/2010 10:27:26 AM
Did you at all attempt to use the documentation to answer your question?

If not, you might begin by using the search field in the Documentation 
Center to look up each of:

   if
   and

Actually, unless you're a complete novice at Mathematica, you'd already 
know that the names of built-in objects always begin with an upper-case 
letter, so you'd save some time sifting through lists of references by 
looking up instead each of:

   If
   And

On 3/30/2010 6:01 AM, nt wrote:
> Hi all,
>
> Is there a way to build an if statement with two conditions such as:
> if[a>b and a>0,...]. If there is, could you let me know the syntax?
>
> Thanks
> nt
>

-- 
Murray Eisenberg                     murray@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

0
Murray
3/31/2010 10:27:58 AM
Reply: