logical operators in if statement
131 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 7 Jul 2016
Edited: Stephen23
on 7 Jul 2016
@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
on 7 Jul 2016
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.
Answers (3)
Andrei Bobrov
on 14 Aug 2014
2 Comments
Steven Lord
on 7 Jul 2016
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
on 14 Aug 2014
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
0 Comments
Adam
on 14 Aug 2014
Edited: Adam
on 14 Aug 2014
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 Comments
Adam
on 14 Aug 2014
Edited: Adam
on 14 Aug 2014
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.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!