版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012314976/article/details/78606391
一个分支
a=rand();
if a>0.5
disp('a is bigger than 0.5');
end
如果随机数a
比0.5大,输出提示。
两个分支
a=rand();
if a>0.5
disp('a is bigger than 0.5');
else
disp('a is not bigger than 0.5');
end
如果随机数a
比0.5大,输出提示;否则输出另外一个提示。
多个分支
a=rand();
if a>0.5
disp('a is bigger than 0.5');
elseif a==0.5
disp('a is equal to 0.5');
else
disp('a is smaller than 0.5');
end
a=rand();
if a>0.7
disp('a is bigger than 0.7');
elseif a>0.3
disp('a is bigger than 0.3');
elseif a>0.1
disp('a is bigger than 0.1');
else
disp('a is too small');
end
switch条件分支
switch
相对于if
更强调相等的条件,更规范,但没有if
灵活。详情参考doc switch
。