MATLAB 的条件分支语句:
1)if...end
2)if...else...end
3) if...elseif...elseif...else...end
a=30; if a<20 fprintf(‘\na is less than 20\n‘); else fprintf(‘a is larger than 20‘) end fprintf(‘a is %d\n‘,a);
结果输出:
4)switch语句
switch语句语法:
switch <switch_expression> case <case_expression> <statements> case <case_expression> <statements> ... ... otherwise <statements> end
例如:
grade = ‘B‘; switch(grade) case ‘A‘ fprintf(‘Excellent! ‘ ); case ‘B‘ fprintf(‘Well done ‘ ); case ‘C‘ fprintf(‘Well done ‘ ); case ‘D‘ fprintf(‘You passed ‘ ); case ‘F‘ fprintf(‘Better try again ‘ ); otherwise fprintf(‘Invalid grade ‘ ); end
结果:
原文地址:https://www.cnblogs.com/shenxiaolin/p/9234576.html
时间: 2024-11-09 10:49:26