Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

if with two conditions

6,616 views
Skip to first unread message

nt

unread,
Mar 30, 2010, 6:01:13 AM3/30/10
to
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

Bob Hanlon

unread,
Mar 31, 2010, 6:24:46 AM3/31/10
to

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 <sagitta...@gmail.com> wrote:

=============

Nasser M. Abbasi

unread,
Mar 31, 2010, 6:25:39 AM3/31/10
to

"nt" <sagitta...@gmail.com> wrote in message
news:hosi59$o49$1...@smc.vnet.net...


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

David

unread,
Mar 31, 2010, 6:26:01 AM3/31/10
to

Hello

I believe this is what you're looking for:

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

DrMajorBob

unread,
Mar 31, 2010, 6:26:11 AM3/31/10
to
Look up "And" or "&&".

Bobby

On Tue, 30 Mar 2010 05:01:24 -0500, nt <sagitta...@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
>


--
DrMaj...@yahoo.com

Bill Rowe

unread,
Mar 31, 2010, 6:26:22 AM3/31/10
to
On 3/30/10 at 5:01 AM, sagitta...@gmail.com (nt) wrote:

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

Either

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

or

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

will do what you want


dr DanW

unread,
Mar 31, 2010, 6:27:26 AM3/31/10
to
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

Murray Eisenberg

unread,
Mar 31, 2010, 6:27:58 AM3/31/10
to
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 mur...@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

Manfred Plagmann

unread,
Apr 1, 2010, 6:58:44 AM4/1/10
to
Logical operators are && for AND and || for OR etc. Just string your logic together:

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

The documentation is your friend and will list all logical operators.

0 new messages