文档详情

第四章 M文件及程序设计.pdf

发布:2017-06-01约2.1万字共16页下载文档
文本预览下载声明
第四章 M 文件和程序设计 4.1 程序控制语句 4.1.1 分支控制 分支控制语句有 if 和 switch 两种语句 1. if 结构 if expression commands end 【说明】:当表达式expression 的值为真,则执行commands 语句组,否则跳过 commands 语句组, 执行 end 之后的语句。 2. if-else 结构 if expression commands1 else commands2 end 【说明】:如果表达式expression 的值为真,则执行语句组commands1,然后跳过语句组 commands2 向下执行;若表达式expression 的值为假,则跳过语句组commands1 而执行语句组 commands2 。 3. if-elseif-else 结构 if expression_1 commands_1 elseif expression_2 commands_2 …… elseif expression_n commands_n else commands_el end 【说明】:如果表达式expression_1 的值为真,则执行语句组commands_1,若表达式 expressions_1 的值为假,则判断 expression_2 的值,若为真,则执行语句组commands_2,否则向下继续判断表达式。 如果所有表达式都为假,则执行语句组 commands_el 。 3x +4 x −1 ⎧ ⎪⎪−x f (x ) e =−1≤x ≤1 例 4.1.1 有分段函数 ⎨ ,编程输入x 的值,计算并显示函数值。 ⎪ sin x +cos x x 1 ⎪⎩ 解:运行下面语句,输入 x 的值,观察运行结果 x=input(请输入自变量值: ); if x-1 49 str=3x+4; y=3*x+4; elseif x=1 str=exp(-x); y=exp(-x); else str=sin(x)+cos(x); y=sin(x)+cos(x); end disp([str,blanks(4),num2str(y)]) 4. switch-case 结构 switch exp_const case value_1 commands_1 case value_2 commands_2 ┅ case value_n commands_n otherwise
显示全部
相似文档