logical operators in if statement

164 次查看(过去 30 天)
richard
richard 2014-8-14
评论: Guillaume 2016-7-7
I have a code that looks like
for x = 1:7:length(list)
if(list(x+2)>0 && list(x+2)<1024)
energys(list(x+2))=energys(list(x+2))+1;
end
end
"list" is a column vector and "energys" is a row vector of zero's from 1,1024
When the logical expression say evaluates at 2000, (greater than 0, but also greater than 1024), does my loop end? Can someone explain the && operator in the context of my code?
  3 个评论
Stephen23
Stephen23 2016-7-7
编辑:Stephen23 2016-7-7
@Kenneth Lamury: what you proposed does not make any difference, because the operator precedence rules clearly state that > and < have a higher precedence than &&. So adding brackets around the two equivalence operations makes no difference whatsoever, because the operator precedence already ensures that those comparisons are performed before the && operation.
In fact the original question has too many brackets already:
if list(x+2)>0 && list(x+2)<1024
does exactly the same thing. Why make it more cluttered than it needs to be ?
Guillaume
Guillaume 2016-7-7
It seems a lot of people like to enclose their if expression in brackets (a hold-over from C?), including Mathworks in some of their own m-files (see actxserver.m for example)
I agree with Stephen. This makes it look cluttered for no apparent benefit.

请先登录,再进行评论。

回答(3 个)

Andrei Bobrov
Andrei Bobrov 2014-8-14
about && see here
  2 个评论
richard
richard 2014-8-14
I know, I read this before I posted but it doest give a scenario where expr1 is true but expr2 is false.
Steven Lord
Steven Lord 2016-7-7
true and false is false regardless of whether you write "and" as & or &&.
In the case you described you'd be executing essentially "if false, < stuff >, end" and that will not execute the body of the if statement.
But in this case, you don't really need a loop. Extract the appropriate elements of list as a vector, use logical indexing on that vector as a whole to select only those elements in the range (0, 1024), then accumulate using accumarray.

请先登录,再进行评论。


Iain
Iain 2014-8-14
Your loop's final execution (barring errors) will have x = length(list)
list(x+2) is liable to throw an error when x + 2 is greater than length(list)
In this code:
if x && y
disp('then')
else
disp('else')
end
&& simply applies the "and" operation to x and y

Adam
Adam 2014-8-14
编辑:Adam 2014-8-14
The logical expression you have won't cause your loop to end other than if it crashes as described by Iain. It will simply cause the expression in the if statement to either happen or not.
for x = 1:7:length(list)
is the only thing that controls your loop termination. If you had a break statement in an else part of your if then that would terminate the loop, but otherwise the logical expression is independent of loop termination
  2 个评论
richard
richard 2014-8-14
OK I understand what you mean now, and I understand how my loop could crash as described by lain, but the funny thing is that this doesn't crash when seemingly it should for the reasons described by lain.
This subfunction I have is part of a larger code...Would you be interested in looking at a part of it?
Adam
Adam 2014-8-14
编辑:Adam 2014-8-14
Well it isn't guaranteed to crash because you increment in jumps of 7 so
1:7:length(list)
will produce an array whose largest number is at most the length of the list, but must also be increments of 7 away from 1. So if e.g. your list length was 10 it would just evaluate to
x = 1:7:10 == [1 8]
which would not crash your code because your x + 2 will have a maximum value of 10 which is a valid index into your list.
If your code is guaranteed to always fall into this situation where incrementing by 7 will always leave you at least 2 below your list length then it will not crash. If your list length is arbitrary though then it will crash any time incrementing by 7 takes you within 1 of your list length.

请先登录,再进行评论。

类别

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