MATLAB 教程
- 第1节:MATLAB 介绍
- 第2节:MATLAB 安装
- 第3节:MATLAB 基本语法
- 第4节:MATLAB 变量
- 第5节:MATLAB 命令
- 第6节:MATLAB M-Files
- 第7节:MATLAB 数据类型
- 第8节:MATLAB 运算符
- 第9节:MATLAB 算术运算
- 第10节:MATLAB 逻辑运算
- 第11节:MATLAB 关系运算
- 第12节:MATLAB 位运算
- 第13节:MATLAB 集合操作
- 第14节:MATLAB 决策制定
- 第15节:MATLAB if...end 语句
- 第16节:MATLAB if...else...end 语句
- 第17节:MATLAB if...elseif...elseif...else...end 语句
- 第18节:MATLAB 嵌套if语句
- 第19节:MATLAB switch语句
- 第20节:MATLAB 嵌套switch语句
- 第21节:MATLAB 循环类型
- 第22节:MATLAB while循环
- 第23节:MATLAB for循环
- 第24节:MATLAB 嵌套循环
- 第25节:MATLAB break语句
- 第26节:MATLAB continue语句
- 第27节:MATLAB 向量
- 第28节:MATLAB 向量的加法和减法
- 第29节:MATLAB 标量向量乘法
- 第30节:MATLAB 转置向量
- 第31节:MATLAB 追加向量
- 第32节:MATLAB 向量的模
- 第33节:MATLAB 向量点积
- 第34节:MATLAB 等差元素向量
- 第35节:MATLAB 矩阵
- 第36节:MATLAB 矩阵的加法和减法
- 第37节:MATLAB 除法(左,右)矩阵
- 第38节:MATLAB 矩阵标量操作
- 第39节:MATLAB 矩阵的转置
- 第40节:MATLAB 串联矩阵
- 第41节:MATLAB 矩阵乘法
- 第42节:MATLAB 矩阵的行列式
- 第43节:MATLAB 逆矩阵
- 第44节:MATLAB 数组
- 第45节:MATLAB 冒号符号
- 第46节:MATLAB 数字
- 第47节:MATLAB 字符串
- 第48节:MATLAB 函数
- 第49节:MATLAB 数据导入
- 第50节:MATLAB 数据导出
- 第51节:MATLAB 绘图
- 第52节:MATLAB 图形
- 第53节:MATLAB 代数
- 第54节:MATLAB 微积分
- 第55节:MATLAB 多项式
- 第56节:MATLAB 变换
- 第57节:MATLAB GNU Octave教程
MATLAB if...else...end 语句
在MATLAB的 if...else...end 语句中,if 语句后面可以跟一个可选择的 else 语句,当执行的表达式为假的时候,执行 else 语句。
if...else...end 语句语法:
MATLAB 中一个 if ... else 语句的语法示例:
if <expression>
% statement(s) will execute if the boolean expression is true
<statement(s)>
else
<statement(s)>
% statement(s) will execute if the boolean expression is false
end
如果布尔表达式的值为 “true”,那么执行 if 的代码块;如果布尔表达式的值为 “false”,else 的代码块将被执行。
if...else...end 语句流程图:
详细例子如下:
在MATLAB中建立一个脚本文件,并输入下述的代码:
a = 100;
% check the boolean condition
if a < 20
% if condition is true then print the following
fprintf('a is less than 20
' );
else
% if condition is false then print the following
fprintf('a is not less than 20
' );
end
fprintf('value of a is : %d
', a);
编译和执行上述代码,产生下述结果:
a is not less than 20
value of a is : 100