for语句
for i = 最小值(初值):步长:最大值(终值)
执行语句
end
举个例子
for i = 1 : 3
i = i + 1;
end
当步长为1时,步长可以省略
- for语句可以嵌套
while语句
while 条件表达式(例如n<=10)
执行语句
end
举个例子
while n <= 10
n = n + 1;
end
if…end语句
if 表达式
执行语句
end
举个例子
a=20
b=540
if a <= b
'成立'
end
运行结果为'成立'
if…else…end 语句
if 表达式
执行语句
else
执行语句
end
举个例子
a=20
b=540
if a <= b
'成立'
else
'不成立'
end
运行结果为'成立'
switch…case语句
switch 表达式(数值或字符串)
case 数值或者字符串1
执行语句1;
case 数值或者字符串2
执行语句2;
。。。
otherwise(可以省略,省略时当case都不满足时跳出switch结构)
执行语句n;
end
关于语句加不加分号的问题
参考这篇blog(Matlab分号的使用)
https://blog.csdn.net/sthintheway/article/details/82831577