📅  最后修改于: 2021-01-07 02:10:55             🧑  作者: Mango
如果第一个条件不成立,那么我们可以使用else关键字定义其他要运行的语句。
if expression
Statements
else
Statements
end
% program to check the number is even or odd
a = randi(100,1);
if rem(a,2) == 0
disp('an even number')
else
disp('an odd number')
end
输出:
a = 15
an odd number
使用if-else语句确定两个数中的最大值
num1 = input('Enter number 1: ');
num2 = input('Enter number 2: ');
if (num1 > num2)
disp('num1 is greater than num2')
else if (num1 < num2)
disp('num1 is less than num2')
else if (num1 == num2)
disp('num1 is equal to num2')
end
输出:
Enter number 1: 15
Enter number 2:20
num1 is less than num2