MATLAB: Use elseif inside a case switch. What is Wrong with the code? Only the first “if” case only works none of the elseif work?? Please any advice would help.

case switchelseifif statementMATLAB

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

Best Answer

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