Trouble With Simple 'while' Loop Logical Operator
2 次查看(过去 30 天)
显示 更早的评论
Can't seem to get my while loop to execute.
-No error showing
-Getting rid of 1 initial condition in 'while' loop and the script runs (not how I want though). I need my 'while' loop to accept a number between 1 and 12 (corresponds to 1=Jan, 2=Feb, etc).
Any help with this greatly appreciated!
Script Below:
clear all
clc
M = 100;
Y = -100;
disp('After selecting a month and year, the number of days within that month will be displayed.')
while Y<0
Y = input('Please select a year: ');
end
while (M<0) & (M>12)
M = input('Please select a month: ');
end
0 个评论
采纳的回答
James Kerns
2013-1-30
It would appear you need an OR logical operator in the
while (M<0) & (M>12)
I.e. make it
while (M<0) || (M>12)
3 个评论
Jan
2013-1-30
Btw., omit the useless clear all: removing all loaded functions from the memory wastes time only. If you really want to clear the variables, use clear variabels.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!