(1)决策(选择):基于决策的流分支
(2)循环(重复):流循环准许语句反复执行
分别介绍如下:
(1)决策(选择):基于决策的流分支
if结构
语法如下
if condition
statement
end
举例
function grader( grade )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes
here
if grade
>=60
disp('passing grade') end
将这个称为单行if(single-line if)
end
在matlab中直接调用如下:
>> grader(89)
passing grade
ERROR函数
用于捕获基本的错误,语法如下
error(msg)
举例如下:
代码如下
function f = errortest( x )
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes
here
if x==0 ,error('zero value encountered'),end
f=1/x;
end
运行测试如下
>> errortest(10)
ans =
0.1000
>> errortest(0)
??? Error using ==> errortest at 5
zero value encountered
逻辑条件
if-else结构
if
contion
statements;
else
statements;
end
if-elseif结构
if
contion1
statements1;
elseif
contion2
statements2;
。。。。。
else
statements(else);
end
switch结构
与C语言一致,是特定的值