How to use while loop with an inequality condition

Hi, I'm trying use a while loop in order to get a user to input a value in between a two separate values. (ie. 0.01 < x < 1000)
I currently have written this down in my script:
clear
clc
display('this is a test');
F = input('input test number: ');
while (F>1000 && F<0.01)
disp('test unsuccessful');
F = input('Please input new number: ');
end
display('Test successful');
Whenever I run the code, I input a number higher than 1000 or lower than 0.01 for 'x'. However, instead of moving into the while loop, it simply skips over and the prompt displays my 'Test successful' message.
I can get the code to work when only one condition is present but as soon as I include the second condition with the && statement, the while loop no longer operates.
What am I doing incorrectly?
edit: the purpose of me inputting a value above 1000 and below 0.01 is to trigger the while loop
_edit2:
instead of &&, || ended up working, using the same condtions: (F>1000 || F<0.01)

 Accepted Answer

You mean
while (F<1000 && F>0.01)

5 Comments

Just to be certain, I went ahead and tried that, but it didn't work.
Also, my understanding is that the while loop should continue to run when the conditions are true. Therefore, if x is greater than 1000 and less than 0.01, the loops should continue since x>1000 and x<0.01, which is what my conditions in the loop state.
What I am trying to do is to get the user to input a number between 0.01 and 1000. When the user does not, I want to give an error statement and then proceed to ask the user to input a value again until the correct value is input.
If my logic is wrong on this, please explain why.
Jon, it's not possible that x is greater than 1000 and at the same time less then 0.01, maybe you want to say: greater than 1000 or less than 0.01
while (F>1000 || F<0.01)
I understand what you're saying. The while loop will only continue if both statements are TRUE, as opposed to if both statements are FALSE, so this was a complete blunder on my part.
I went ahead and tried replacing the conditions with:
while(F<1000 || F>0.01)
I tested the loop by inputting 100000. The loop gave me my error message and asked for a new value.
Then, I input 0.001 into the loops. The loop gave me my error message again and asked for a new value.
The problem was when I input a value between 0.01 and 1000 (ie. 100), the loop still gave me my error message and continued asking me for another value. I had to use the 'break' hot key in order to cancel the program.
After re-evaluated my code, I went ahead and tried what you suggested. However, I slightly modified it.
while (F>1000 || F<0.01)
This ended up working. Thank you for your patience and your suggestions. They are greatly appreciated.
Exact, that's what I meant

Sign in to comment.

More Answers (1)

Use OR instead of AND:
This works just fine:
F = input('input test number: ');
while (F>1000 || F<0.01)
disp('test unsuccessful');
F = input('Please input new number: ');
end
display('Test successful');

1 Comment

this is exactly what it was. sorry for not seeing your message sooner.

Sign in to comment.

Categories

Tags

Asked:

Jon
on 25 Nov 2013

Commented:

Jon
on 25 Nov 2013

Community Treasure Hunt

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

Start Hunting!