MATLAB 的条件分支语句:
1)if...end
2)if...else...end
3) if...elseif...elseif...else...end
a=30; if a<20 fprintf(' a is less than 20 '); else fprintf('a is larger than 20') end fprintf('a is %d ',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
结果: