![](https://csdnimg.cn/release/download_crawler_static/85920349/bg6.jpg)
ii、While 循环
与 for 循环以固定次数求一组命令相反,while 循环以不定的次数求一组语句的值。
while expression
{commands}
end
只要在表达式(expression)里的所有元素为真,就执行 while 和 end 语句之间的命令串
{commands}。
iii、If-Else-End 结构
(a)有一个选择的一般形式是:
if expression
{commands}
end
如果在表达式(expression)里的所有 元 素 为 真 , 就 执 行 if 和 end 语句 之 间 的 命 令 串
{commands}。
先建立 M 文件 fun1.m 定义函数 f(x),再在 Matlab 命令窗口输入 fun1(2),fun1(-1)即可。
(b) 有三个或更多的选择的一般形式是:
if (expression1)
{commands1}
else if (expression2)
{commands2}
else if (expression3)
{commands3}
else if ……
…………………………………
else
{commands}
end
end
end
……
end
先建立 M 文件 fun2.m 定义函数 f(x),再在 Matlab 命令窗口输入 fun2(2),fun2(0.5),
fun2(-1)即可。
)1(),2(,
12
11
)(
2
�
�
�
�
�
��
� ff
xx
xx
xf 求设例
)1(),5.0(),2(,
0x
1x02
1x1x
f(x)
3
2
�
�
�
�
�
�
�
��
��
� fff
x
x 求设例