Why is this logical "&&" passing the "if" statement?

조회 수: 2 (최근 30일)
Andrew
Andrew 2023년 3월 30일
댓글: the cyclist 2023년 3월 30일
My below code determines valid dates. I am not looking for the answer to the code, but rather just some insight as to why my logical functions are working incorrectly. I've inserted 4/31/2018 as my date and it passes on line checking 31 day months below. The 31 days meets criteria, but the months returns a logic array of zeros so it shouldn't be valid. Yet it continues to stop there when debugging. Why is this?
I'm using whatever the most current online Matlab software is.
function valid = valid_date(year,month,day)
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid = false;
return
end
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid = false;
return
end
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid = false;
return
end
if year/4 && (year/100 && year/400) && (month == 2 && day <= 29)
valid = true;
elseif month == 2 && day <= 28
valid = true;
elseif month == [1 3 5 7 8 10 12] && day <= 31 %passing here when it should move to next "elseif"
valid = true;
elseif (month == [4 6 9 11]) || (day <= 30)
valid = true;
else
valid = false;
end
end
I've edited the code and it passes on that line. However in the case of 4/1/2018 it fails on the following line where it should pass.
function valid = valid_date(year,month,day)
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid = false;
return
end
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid = false;
return
end
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid = false;
return
end
if year/4 && (year/100 && year/400) && (month == 2 && day <= 29)
valid = true;
elseif month == 2 && day <= 28
valid = true;
elseif (month == [1 || 3 || 5 || 7 || 8 || 10 || 12]) && (day <= 31)
valid = true;
elseif (month == [4 || 6 || 9 || 11]) && (day <= 30)
valid = true;
else
valid = false;
end
end
Updated the code and now runs smooth:
function valid = valid_date(year,month,day)
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid = false;
return
end
if ~isscalar(month) || month < 1 || month ~= fix(month)
valid = false;
return
end
if ~isscalar(day) || day < 1 || day ~= fix(day)
valid = false;
return
end
if month == 2
if day < 29
valid = true
elseif mod(year,4) == 0 && mod(year,100) == 0
if mod(year,400) == 0
if day <= 29
valid = true;
end
else
valid = false;
end
elseif mod(year,4) == 0
if day <= 29
valid = true;
else
valid = false;
end
else
valid = false;
end
elseif mod(year,4) == 0 && (month == 2 && day <= 29)
valid = true;
elseif month == 2 && day <= 28
valid = true;
elseif (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31)
valid = true;
elseif (month == 4 || month == 6 || month == 9 || month == 11) && (day <= 30)
valid = true;
else
valid = false;
end
end
  댓글 수: 2
Torsten
Torsten 2023년 3월 30일
What do you expect is the result of
if year/4 && (year/100 && year/400)
? It will always return true.
Further, month is a scalar, not a vector.
Thus
elseif month == [1 3 5 7 8 10 12]
elseif (month == [4 6 9 11])
will always return false.
Andrew
Andrew 2023년 3월 30일
Thanks for the response! However that first reponse does not always return true, but rather false and therefore my debugger skipped it and passed to the next portion of code successfully. Just a note on this comment. Second part was helpful though.

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2023년 3월 30일
편집: the cyclist 2023년 3월 30일
Your code suggests that you think
if year/4
is checking if year is divisible by 4. But that is not what the syntax is doing. I think you want
if mod(year,4)==0
Also,
1 || 3 || 5 || 7 || 8 || 10 || 12
ans = logical
1
is definitely not doing what you expect.
I think you want something like
ismember(month,[1 3 5 7 8 10 12])
Sorry to sound harsh, but it seems that you are just coding in a way that seems to make sense to you, but doesn't actually correspond to MATLAB syntax.
  댓글 수: 3
the cyclist
the cyclist 2023년 3월 30일
I think I was editing my answer while you were asking this question, to show you exactly that:
ismember(month,[1 3 5 7 8 10 12])
will be true if month is one of those values.
the cyclist
the cyclist 2023년 3월 30일
You might find the free MATLAB Onramp tutorial useful.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by