user466534
user466534

Reputation:

use debugger in matlab

i want to debug following simplest code in matlab and clarify why it executes always if statement

function testfile(x)
if 3<x<6
disp('in the middle of range');
else
    disp('out of range');

end
end

i have used following code for debugger

echo testfile on
 testfile(-2)
in the middle of range


testfile(6)
in the middle of range

why it does not execute else statement?i have used following code as a test

 5<4<8

ans =

     1

so does it mean that writing if statement in this style is wrong?a i understood it is same as if 5<4 || 4<8?then it makes clear for me why it executes only if statement and never reaches to else

Upvotes: 0

Views: 75

Answers (1)

Jonas
Jonas

Reputation: 74940

5<4<8 is evaluated as (5<4)<8. If we resolve the expression in the parentheses first, we have 0<8, which is true. Test with 5<4==0, which evaluates to true.

What you want to do is check whether x is both bigger than 3 and smaller than 6, i.e.

3<x && x<6

Upvotes: 3

Related Questions