How do I properly combine "and", "or" and "if"

11 views (last 30 days)
Hi,
My function needs to calculate the cost of a train ticket. The first mile is $2. Each additional mile up to 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents. Children under or equal to 18 and seniors older or equal to 60 pay 80%. The inputs to the function are distance and age.
I have below code, but for some reason it doesn't take the discount into consideration.
function price=fare(d,a)
if 18<a<60 && d <=1
price=2
elseif 18<a<60 && 1<d<10
price=2+d*0.25
elseif 18<a<60 && d==10
price=2+9*0.25
elseif 18<a<60 && d>10
price=2+(9*0.25)+((d-10)*0.10)
elseif a<=18 || a>=60 && d <=1
price=2*0.80
elseif a<=18 || a>=60 && 1<d<10
price=(2+d*0.25)*0.80
elseif a<=18 || a>=60 && d==10
price=(2+9*0.25)*0.80
elseif a<=18 || a>=60 && d>10
price=(2+(9*0.25)+((d-10)*0.10))*0.80
end
  1 Comment
Steven Lord
Steven Lord on 23 Feb 2017
To add to John and Jan's answers, a couple suggestions:
  • Brevity may be the soul of wit, but (slightly) longer names can help make code easier to understand.
You use the variable name price in your code. How easy is it to guess what that variable represents? Now consider the variable named d. What does that represent? You may know because you read the problem description, but wouldn't it be easier to guess if it were called something like distance?
  • If you use magic constants in your code, but they aren't actually constants, figuring out which values need to change can be difficult and/or time consuming.
Somewhat related to the previous suggestion, what would happen if the train company decided to increase the fare for miles 2-10 from 25 cents to 35 cents? If you used a variable named CostForMiles2To10 in your code, adjusting the fare in your code is a matter of a one character change (and where to make that change is obvious.)
CostForMiles2To10 = 0.25;
% becomes
CostForMiles2To10 = 0.35;
Is the variable name CostForMiles2To10 more to type than 0.25? Yes. But tab completion, copy-and-paste (of the variable name, not the variable value), or abbreviation (Cost2_10) can mitigate some of that while still helping you understand what you wrote days, weeks, months, or years after you wrote it.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 23 Feb 2017
Edited: John D'Errico on 23 Feb 2017
Note that this code fragment ALWAYS returns true
18<a<60
since regardless of the value of a, (18<a) ALWAYS returns either 0 or 1, i.e., false or true. Are both 0 and 1 less than 60? Yes.
Just because you choose to use some nice mathematical short hand, does not mean that it is equivalent to
(18<a) && (a<60)
You use similar expressions repeatedly in your code. A bad idea.
  1 Comment
JGraf
JGraf on 23 Feb 2017
Follow up question. I just added fprintf(%.2f, ....) and get two answers (4 and 3 for fare(4,44). Why is that? The correct answer is 3.
function price=fare(d,a)
if 18<a && a<60 && d <=1;
price=2;
elseif 18<a && a<60 && 1<d && d<10;
price=fprintf('%.2f',(2+d*0.25));

Sign in to comment.

More Answers (2)

Jan
Jan on 23 Feb 2017
Edited: Jan on 23 Feb 2017
The code can be simplified:
function price=fare(d,a)
if 18<a && a<60
if d <=1
price = 2
elseif d < 10 % 1 < d is excluded before already
price = 2+d*0.25
% elseif d==10 % Covered by d < 10 already
% price = 2+9*0.25
else % not test required: if d>=10
price = 2+(9*0.25) + ((d-10) * 0.10)
end
else % Test not required: a<=18 || a>=60
if d <=1
price = 2*0.80
elseif d < 10 % 1 < d is excluded before
price=(2+d*0.25)*0.80
% elseif d==10 % Covered by d > 10
% price=(2+9*0.25)*0.80
else % no test required: if d>=10
price = (2+(9*0.25)+((d-10)*0.10))*0.80
end
end
If you have tested for d <= 1, you can omit the test d > 1 in the following.
Or in short:
function price = fare(d, a)
if d <= 1
price2 = 2;
else
price2 = 2 + (min(9, d) * 0.25) + (max(0, d-10) * 0.10);
end
if 18 >= a || a >= 60
price2 = price2 * 0.80;
end
  4 Comments
Jan
Jan on 24 Feb 2017
Edited: Jan on 24 Feb 2017
The best idea to find out, what's going on is to use the debugger: Set a breakpoint in the first line and step through your code line by line. Examine the type and contents of the variables in teh Workspace Browser or in the Command Window.

Sign in to comment.


JGraf
JGraf on 23 Feb 2017
Thank you

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!