If condition with multiple OR statements

517 次查看(过去 30 天)
Hi,
I would like to understand what's the problem here.
PURPOSE: Not print 'd', if the multiple OR statements are satisfied.
clearvars d
for d = 1 : 26
if (d ~= 1 || d ~= 3 ||d ~= 7 ||d ~= 9 ||...
d ~= 18 || d ~= 20 || d ~= 24 || d ~= 26)
d
end
end
So that only cases that d = 2, 4, 5, 6, 8 are printed.
But it happens that all values are showed... why?
Wouldn't the code above be the negative of the code below? * This code below works, btw.
clearvars d
for d = 1 : 26
if (d == 1 || d == 3 ||d == 7 ||d == 9 ||...
d == 18 || d == 20 || d == 24 || d == 26)
else
d
end
end
Thanks
  1 个评论
Stephen23
Stephen23 2020-7-10
编辑:Stephen23 2020-7-10
"But it happens that all values are showed... why?"
Because that is the correct interpretation of the boolean logic that you specified. Can you show us one finite integer value d for which this statment returns false?:
d~=1 || d~=3
(hint: there is no such value, that statement will always return true because at most one of its terms can be false for any value of d. But to get a false output you would need all of its terms to be false, and that will never happen).
"Wouldn't the code above be the negative of the code below?"
No, that would be incorrect. One correct way to form the negation is to use De Morgan's laws:
and use AND instead of OR:
d~=1 && d~=3 && ...
But you should skip all of that and just use the standard MATLAB approach, i.e. ismember.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2020-7-10
Let's take a look at a shorter version of your code.
for d = 1 : 3
if (d ~= 1 || d ~= 3)
disp(d)
end
end
When d = 1, we evaluate your if condition. (d ~= 1) is false, (d ~= 3) is true, and false || true is true. So we display d.
When d = 2, we evaluate your if condition. (d ~= 1) is true, so MATLAB doesn't need to evaluate the rest of the expression. true || anything is true. So we display d.
When d = 3, we evaluate your if condition. (d ~= 1) is true, so MATLAB doesn't need to evaluate the rest of the expression. true || anything is true. So we display d.
I would use ismember in this situation.
for d = 1:3
if ~ismember(d, [1 3])
disp(d)
end
end
When d = 1, ismember(d, [1 3]) is true so ~ismember(d, [1 3]) is false and we don't display d.
When d = 2, ismember(d, [1 3]) is false so ~ismember(d, [1 3]) is true and we display d.
When d = 3, ismember(d, [1 3]) is true so ~ismember(d, [1 3]) is false and we don't display d.
  2 个评论
drummer
drummer 2020-7-10
Thanks @Steven Lord.
So, for MATLAB, the 1st statement being true is enough to execute everything within the if condition.
Indeed, I have already solved my problem using the negative of ismember.
I just would like to understand why using the first scenario would show every 'd' value.
Now it's clear.
Cheers.
Steven Lord
Steven Lord 2020-7-10
The || operator short-circuits (as does the && operator.)
x = true || error("This never gets thrown")
x = false && error("Nor does this")
true or anything is true. MATLAB doesn't need to know what happens when it runs "anything", so it doesn't even try to run it.
false and anything is false.
Note that if it didn't, there would be two reasons for the "anything" sections to throw an error in this particular case.
>> y = error("Can we call error with an output argument?")
Error using error
Too many output arguments.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by