%%分支语句if、switch, 循环语句for、while, 其他pause、break、return、error 2020.8.26 马玉华
%if分支结构
%if 条件
% 执行语句
%elseif 条件
% 执行语句
%else
% 执行语句
%end
x = input('请输入x的值:') %会自动识别double和char
if x<=10;
y = cos(x)
elseif x == 10
y = sqrt(x)
else
y = sin(x)
end
%switch……case……otherwise……end 可忽略,可忽略
%switch 数值或字符串
% case 数值或字符串条件
% 执行语句
% case 数值或字符串条件
% 执行语句
% otherwise
% 执行语句
%end
n = input('请输入一个数:')
switch n;
case 1
d = 'print outcome 1'
case 2
d = 'print outcome 2'
otherwise
d = 'no outcome'
end
% for 循环格式:
%for 条件
% 执行语句
%end
%例子:求1?+2?+3?+4?+5?
sum =0;
for n = [1:1:5]; %[x1:步长:x2]
sum = sum + n^2;
end
sum
% while循环:
%while 条件
% 执行语句
%end
%例子:求1+2+3……+10的和
n = 1;
sum = 0;
while n <= 10;
su
2.1.1 分支语句if、switch, 循环语句for、while
猜你喜欢
转载自blog.csdn.net/qq_43328166/article/details/108249441
今日推荐
周排行