ANUPAM BISHT
ANUPAM BISHT

Reputation: 31

IF statement condtion in a range of values

Say If I have a condition which i have to agree in my program.It is like the value of a certain element 'v' has to be in the range 0.0001 to 0.001 ,only if it is that i will accept the value v else not.

i.e mathematically :-v belongs to (0.0001,0.001)

How do I write this using IF statement.

if(v < 0.001 && v>0.0001)

But I feel this will also accept values for v= 2 which I don't want.

Please guide me

Thank you

Anupam

Upvotes: 0

Views: 1319

Answers (1)

mikkola
mikkola

Reputation: 3476

Your if statement is fine. Using the && logical operation is equal to saying you want both the first condition AND the second condition to hold - it is the and operator.

If you want, you can try some values and make MATLAB print an output just to check how the if statement works, and which values it will accept. For example:

values = [0.001 0.0002 2 3]; % your values you want to test

for v = values % loop over all the values
    if(v < 0.001 && v>0.0001)
        disp(['I accepted value ' num2str(v)]);
    else
        disp(['I did not accept value ' num2str(v)]);
    end
end

Output:

I did not accept value 0.001
I accepted value 0.0002
I did not accept value 2
I did not accept value 3

Upvotes: 1

Related Questions