Trouble With Simple 'while' Loop Logical Operator

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

 채택된 답변

James Kerns
James Kerns 2013년 1월 30일

2 개 추천

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
Jan 2013년 1월 30일
편집: Jan 2013년 1월 30일
Obviously this cannot be true ever: "M lower than 0 and M greater than 12". Frequently reading the source code loud helps to find bugs.
Dave
Dave 2013년 1월 30일
Thanks so much! I thought I went through all the logical operators for the sake of debugging but I guess I didn't. Thanks again :)
Jan
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개)

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 1월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by