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...elseif...elseif...else...end 语句
MATLAB 的 if...elseif...elseif...else...end 语句中 if 语句可以跟随一个(或多个)可选的 elseif... else 语句,这是非常有用的,可以用来对各种条件进行测试。
使用 if... elseif...elseif...else 语句,要注意以下几点:
一个 if 可以有零个或多个 else,但是它必须跟在 elseif 后面(即只有 elseif 存在才会有 else)。
一个 if 可以有零个或多个 elseif ,必须出现else。
一旦 elseif 匹配成功,余下的 elseif 将不会被测试。
if <expression 1>
% Executes when the expression 1 is true
<statement(s)>
elseif <expression 2>
% Executes when the boolean expression 2 is true
<statement(s)>
Elseif <expression 3>
% Executes when the boolean expression 3 is true
<statement(s)>
else
% executes when the none of the above condition is true
<statement(s)>
end
详细例子如下:
在MATLAB中建立一个脚本文件,并输入下述代码:
a = 100;
%check the boolean condition
if a == 10
% if condition is true then print the following
fprintf('Value of a is 10
' );
elseif( a == 20 )
% if else if condition is true
fprintf('Value of a is 20
' );
elseif a == 30
% if else if condition is true
fprintf('Value of a is 30
' );
else
% if none of the conditions is true '
fprintf('None of the values are matching
');
fprintf('Exact value of a is: %d
', a );
end
编译和执行上述代码,产生如下结果:
None of the values are matching
Exact value of a is: 100