switch块有条件地执行几个选择中的一个语句,每个选择都包含在case语句中。
matlab中switch语句语法为-
switch <switch_expression> case <case_expression> <statements> case <case_expression> <statements> ... ... otherwise <statements> end
创建一个脚本文件,并在其中键入以下代码-
grade='B'; switch(grade) case 'A' fprintf('Excellent!\n' ); case 'B' fprintf('Well done\n' ); case 'C' fprintf('Well done\n' ); case 'D' fprintf('You passed\n' ); case 'F' fprintf('Better try again\n' ); otherwise fprintf('Invalid grade\n' ); end
运行文件时,它会显示-
Well done
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)