MATLAB: I am trying to make this script loop until you have made “a” to be a number between 100 and 1. The script works when you input a number larger than 100 but it doesn’t loop when you use a number that is smaller than 1. Does anyone see whats wrong

.mloopMATLABscriptwhile

clc, clear all, close all
a=150;
while (100<a) && (1<a)
a=input('Please type a number between 1 and 100: ');
if 100>=a && a>50
disp('Your number is larger than 50')
elseif a>100
disp('Your number is too big, please try again')
elseif 1>a
disp('Your number is too small, please try again')
elseif 50>a>=1
disp('Your number is smaller than 50')
elseif a==50
disp('Your number is 50')
end
end

Best Answer

This while condition
while (100<a) && (1<a)
is equivalent to
while (100<a)
which doesn't do what I think you meant it to do.
I think what you intended for this condition is for the loop to continue until the user gives an input that is between 1 and 100 inclusive. Then a message is displayed and the loop ends. If that is what you intended, then use this condition instead:
while (a > 100) || (a < 1) % while "a" is outside the intended range, continue the loop