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

4 views (last 30 days)
Andrew
Andrew on 30 Mar 2023
Commented: the cyclist on 30 Mar 2023
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 Comments
Torsten
Torsten on 30 Mar 2023
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 on 30 Mar 2023
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.

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 30 Mar 2023
Edited: the cyclist on 30 Mar 2023
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 Comments
the cyclist
the cyclist on 30 Mar 2023
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.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!