Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Basic if statement with OR operator

Subject: Basic if statement with OR operator

From: SG

Date: 17 Jul, 2011 22:54:08

Message: 1 of 3

Hi,

This may be a ridiculous question, but I cannot seem to figure out how to make the OR operator work in an if statement.

In something as simple as:

if 4 == 5|6
'yes'
else
'no'
end

the answer is 'yes' - why is that? am I not coding: "if 4 equals 5 or 6, tell me 'yes' else tell me 'no' "??

I must me making an error somewhere, so help would be very much appreciated!

Thanks

Subject: Basic if statement with OR operator

From: Roger Stafford

Date: 17 Jul, 2011 23:55:10

Message: 2 of 3

"SG " <sigrdson@gmail.com> wrote in message <ivvp6g$2mn$1@newscl01ah.mathworks.com>...
> .......
> if 4 == 5|6
> 'yes'
> else
> 'no'
> end
>
> the answer is 'yes' - why is that? am I not coding: "if 4 equals 5 or 6, tell me 'yes' else tell me 'no' "??
> ........
- - - - - - - - - - -
  No, that syntax is not correct. It should be:

if 4==5 | 4==6

  The reason you got 'yes' is that the '6' by itself is nonzero and is therefore regarded as a true statement, so the whole assertion is considered true.

Roger Stafford

Subject: Basic if statement with OR operator

From: Matt J

Date: 18 Jul, 2011 03:29:08

Message: 3 of 3


For a situation like this, you could also do

if ismember(4,[5,6])
...
end

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us