1.switch语句 switch 表达式 … case 条件 … 语句 …case 条件 语句 … otherwise 语句 end
编辑器:
function[t]=wendu(x)
switch fix(x/100) %取整
case{0,1}
rate=0;
case{2,3,4}
rate=3/100;
case num2cell(5:9) %取5-9
rate=5/100;
case num2cell(10:24)
rate=8/100;
otherwise
rate=10/100;
end
t=x*(1-rate); %如果没有分号,那么调用函数时候,会输出两个t的值
end
命令行:
>> t=wendu(100)
t =
100
2.if语句 if 条件 语句 elseif 条件 语句 。。。 end
Switch能实现的if也能,但是if能实现的switch未必都能实现