Multiple conditional statements with logic

2 views (last 30 days)
Thomas Sun
Thomas Sun on 31 Mar 2020
Commented: Image Analyst on 26 May 2020
I want to write a function called under_age that inputs whether a person is too young or not. It has multiple inputs of age and limit.
If the age is less than the limit than too_young = true and if age is greater than too_young = false. I also want to put in a case when the age is inputed
In which case, the age is of the limit of automatically set to 21.
At the moment the function works if the person is too_young = true. But I can't figure out why it doesn't work when too_young = false.
function [too_young] = under_age(age, limit)
if (nargin == 2)
age < limit
too_young = true;
elseif (nargin == 2)
age >= limit
too_young = false;
elseif (nargin == 1) || isempty(limit)
age < 21
too_young = true;
elseif (nargin == 1) || isempty(limit)
age >= 21
too_young = false;
end
  1 Comment
Image Analyst
Image Analyst on 26 May 2020
Apparently this is homework, because it's a duplicate of this question. I will label it as homework, like it should have been originally so that no one gave complete solutions.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 31 Mar 2020
Edited: Image Analyst on 17 May 2020
Try this:
too_young = under_age(22, 21)
too_young = under_age(11, 25)
too_young = under_age(33)
too_young = under_age(18)
function too_young = under_age(age, limit)
fprintf('nargin = %d\n', nargin);
if (nargin == 2)
too_young = age < limit; % Returns true or false.
else
too_young = age < 21; % Returns true or false.
end
end
  2 Comments
Image Analyst
Image Analyst on 17 May 2020
Alright, I've added comments. So many that there is now more comments than code. It should be even more self-explanatory now.
% Top part of m-file:
% Call the under_age() function with various values and number of input arguments.
% First call with two input arguments, each with a different limit.
too_young = under_age(22, 21)
too_young = under_age(11, 25)
% Now call with no second input argument. The under_age() function will assume a limit of 21.
too_young = under_age(33)
too_young = under_age(18)
% Bottom part of m-file, the function definition.
function too_young = under_age(age, limit)
% Print out how many input arguments the user called the function with.
fprintf('nargin = %d\n', nargin);
if (nargin == 2)
% The function was called with 2 input arguments.
% We need to see if the age (first argument) is below or above limit (the second argument).
too_young = age < limit; % Returns true or false.
else
% The function was called with only one argument.
% So we need to check only this one age against a value of 21, not against any other value.
too_young = age < 21; % Returns true or false.
end
end % Final end is required if the function is in the same m-file as the script, but not if it's all by itself in it's own m-file.

Sign in to comment.


Alberto Chavez
Alberto Chavez on 31 Mar 2020
I think you have part of your expressions in the statements section.
Maybe this works.
function [too_young] = under_age(age, limit)
if (nargin == 2) && age < limit
too_young = true;
elseif (nargin == 2) && age >= limit
too_young = false;
elseif (nargin == 1) && isempty(limit) && age < 21
too_young = true;
elseif (nargin == 1) && isempty(limit) && age >= 21
too_young = false;
end
  3 Comments
Image Analyst
Image Analyst on 31 Mar 2020
You don't need all that complicated stuff. Did you try my code? It works fine, either for one or two inputs.
Alberto Chavez
Alberto Chavez on 1 Apr 2020
Edited: Alberto Chavez on 1 Apr 2020
Oh my bad, this should work.
function [too_young] = under_age(age,limit)
if nargin==2
if isempty(limit)==1
if age<21
too_young = true;
elseif age>=21
too_young = false;
end
elseif isempty(limit)==0
if age<limit
too_young = true;
elseif age>=limit
too_young = false;
end
end
elseif nargin==1
if age<21
too_young = true;
elseif age>=21
too_young = false;
end
end
I had to restructure your code, you have 3 levels of possibilities in your inputs. First whether you have:
under_age(age,limit)
%% or
under_age(age)
%% Second, whether you have
limit=[] % or
limit=anynumber
%% Third, whether you have
age<limit || 21 % or
age>=limit || 21
I ran it on my PC and it works now.

Sign in to comment.

Categories

Find more on Get Started with Control System Toolbox 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!