Use elseif inside a case switch. What is Wrong with my code? Only the first "if" case only works none of the elseif work?? Please any advice would help.
2 views (last 30 days)
Show older comments
clc
clear all
disp ('Cody,Ward')
disp ('EG167, 07')
disp('Assignment #12')
disp(' ')
disp ('Problem: #21')
disp(' ')
rp=input('What is the rental period for the car (days): ');
cartype=input('What type of car class (B, C, D) ?: ','s');
switch cartype
case 'B'
if (1<rp) && (rp<6);
cost=rp*27;
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==7;
n=162
cost=n
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif (7<rp) && (rp<27);
n=rp-7;
cost=162+(n*25);
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==28;
n=662;
cost=n;
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
elseif (28<rp) && (rp<=60);
n=rp-28;
cost=662+(n*23);
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
else rp>60;
fprintf('\nRental is not available for more than 60 days.\n\n')
end
case 'C'
if (1<rp) && (rp<6);
cost=rp*34;
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==7;
n=204
cost=n
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif (7<rp) && (rp<27);
n=rp-7;
cost=162+(n*31);
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==28;
n=824;
cost=n;
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
elseif (28<rp) && (rp<=60);
n=rp-28;
cost=824+(n*28);
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
else rp>60;
fprintf('\nRental is not available for more than 60 days.\n\n')
end
case 'D'
if (1<rp) && (rp<6);
fprintf('\nClass D cannot be rented for less than 7 days.\n\n')
elseif rp==7;
n=276
cost=n
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif (7<rp) && (rp<27);
n=rp-7;
cost=204+(n*31);
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==28;
n=1136;
cost=n;
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
elseif (28<rp) && (rp<=60);
n=rp-28;
cost=1136+(n*28);
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
else rp>60;
fprintf('\nRental is not available for more than 60 days.\n\n')
end
end
0 Comments
Accepted Answer
Teja Muppirala
on 3 May 2011
x = 0.1;
if 0 < x < 0.2
disp('hello')
end
This sort of "IF" test does not work in MATLAB.
What happens is it first checks (0 < x)? --> true (=1)
Then (1 < 0.2)? --> false
Instead you have to write out both conditions like this:
x = 0.1;
if (0 < x) && (x < 0.2)
disp('hello')
end
3 Comments
Teja Muppirala
on 3 May 2011
>> doc if
Or
Just for more general information,
Help -> MATLAB -> Getting Started -> Programming
More Answers (1)
Paulo Silva
on 3 May 2011
This condition
elseif 7<rp<=27;
is valid for rp=27 so why do you have this one
elseif rp==27;
after it?
7 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!