条件语句对于每个程序员来说都是非常基本和重要的。在某些情况下,仅当特定条件为True时才必须执行程序或特定块。在这种情况下,这些条件语句将非常方便且富有成果。这些条件语句的工作方式与其他语言相同。但是,语法因语言而异。以下是我们可以在MATLAB中使用的条件语句。
- 如果结束
- if-else-end
- 末端嵌套
- if-elseif-elseif-else-end
- 开关盒
- 嵌套开关盒
if-end语句
if-end语句是最简单的决策语句。它根据给定的布尔条件决定是否必须执行特定的代码块。仅当给定条件为true时,它才会在块内执行语句,否则不执行。
句法:
if (condition)
% statement(s) will execute
% if the boolean expression is true
end
例子:
MATLAB
% MATLAB program to illustrate
% if-end statement
number = 28;
if number>10
fprintf('The number is greater than 10.');
end
MATLAB
% MATLAB program to illustrate
% if-else-end statement
number = 28;
if number<10
fprintf('The number is greater than 10');
else
fprintf('The number is not less than 10');
end
MATLAB
% MATLAB program to illustrate
% if-else chaining
number = 28;
if number<10
fprintf('The number is less than 10\n');
else
if number<20
fprintf('The number is less than 20\n');
else
fprintf('The number is less than 30\n');
end
end
MATLAB
% MATLAB program to illustrate
% Nested if-end statements
number = 2;
if number<10
fprintf('The number is less than 10\n');
% Executes when then above if
% condition is true
if number<5
fprintf('Also The number is less than 5');
end
end
MATLAB
% MATLAB program to illustrate
% if-elseif-else-end statement
number = 28;
if number<10
fprintf('The number is less than 10\n');
elseif number<20
fprintf('The number is less than 20\n');
elseif number<30
fprintf('The number is less than 30\n');
else
fprintf('The number is less than 40\n');
end
MATLAB
grade = 'A';
switch(grade)
case 'A'
fprintf('Excellent!\n' );
case 'B'
fprintf('Well done\n' );
case 'C'
fprintf('Good\n' );
case 'D'
fprintf('You passed\n' );
case 'F'
fprintf('Better try again\n' );
otherwise
fprintf('Invalid grade\n' );
end
MATLAB
% MATLAB program to illustrate
% String evaluation while using
% switch case
% First letter of the name was
% given small letter intentionally
name = 'geeksforGeeks'
switch(name)
case 'GeeksforGeeks'
fprintf('Hello from %s',name);
otherwise
fprintf('Not Matched!');
end
MATLAB
% MATLAB program to illustrate
% switch case
days = 28;
switch(days)
case 28
fprintf('Normal Year!\n' );
otherwise
fprintf('Leap Year\n' );
end
MATLAB
% MATLAB program to illustrate
% nested switch case
days = 29;
month = 'February'
switch(days)
case 29
fprintf('This Year is ' );
switch month
case 'february'
fprintf('Leap Year!\n' );
otherwise
fprintf('Not Leap Year!\n' );
end
end
输出:
The number is greater than 10.
if-else-end语句
在条件if语句中,将附加的代码块合并为else语句,如果条件为false则执行else语句,如果条件为True则不执行else条件。
句法:
if (condition)
% statement(s) will execute
% if the boolean expression is true
else
% statement(s) will execute
% if the boolean expression is false
end
范例1:
的MATLAB
% MATLAB program to illustrate
% if-else-end statement
number = 28;
if number<10
fprintf('The number is greater than 10');
else
fprintf('The number is not less than 10');
end
输出:
The number is not less than 10
示例2:您还可以使用多个条件将if-else-end语句链接起来。
的MATLAB
% MATLAB program to illustrate
% if-else chaining
number = 28;
if number<10
fprintf('The number is less than 10\n');
else
if number<20
fprintf('The number is less than 20\n');
else
fprintf('The number is less than 30\n');
end
end
输出:
The number is less than 30
嵌套的if-end语句
在某些情况下,必须满足多个条件才能执行代码块,然后使用嵌套的if-end语句。这不过是if条件中的另一个if条件。
句法:
if (condition)
% Executes when the boolean expression 1 is true
if (condition)
% Executes when the boolean expression 2 is true
end
end
例子:
的MATLAB
% MATLAB program to illustrate
% Nested if-end statements
number = 2;
if number<10
fprintf('The number is less than 10\n');
% Executes when then above if
% condition is true
if number<5
fprintf('Also The number is less than 5');
end
end
输出:
The number is less than 10
Also The number is less than 5
if-elseif-elseif-else-end
if语句后可以跟一个(或多个)可选的elseif和else语句,这对于测试各种条件非常有用。
句法:
if (condition)
% Executes when the expression 1 is true
elseif (condition)
% Executes when the boolean expression 2 is true
elseif (condition)
% Executes when the boolean expression 3 is true
else
% executes when the none of the above condition is true
end
例子:
的MATLAB
% MATLAB program to illustrate
% if-elseif-else-end statement
number = 28;
if number<10
fprintf('The number is less than 10\n');
elseif number<20
fprintf('The number is less than 20\n');
elseif number<30
fprintf('The number is less than 30\n');
else
fprintf('The number is less than 40\n');
end
输出:
The number is less than 30
开关盒
一个switch块对if-elif-else-end语句很熟悉。除语法外,它的工作方式与其他语言相同。但是它们之间几乎没有关键区别。一个switch块有条件地从多个选择中执行一组语句。每个选择都包含在案例说明中。开关表达式可以是以下任意一个。
- 号码
- 字符
- 弦乐
- 对象
在这里,字符串表达式和大小写表达式的比较区分大小写。开关字符串表达式的每个字符都必须与大小写字符串表达式匹配。
句法:
switch (condition)
case condition
case (condition)
…
…
otherwise
end
范例1:
的MATLAB
grade = 'A';
switch(grade)
case 'A'
fprintf('Excellent!\n' );
case 'B'
fprintf('Well done\n' );
case 'C'
fprintf('Good\n' );
case 'D'
fprintf('You passed\n' );
case 'F'
fprintf('Better try again\n' );
otherwise
fprintf('Invalid grade\n' );
end
输出:
Excellent!
范例2:
的MATLAB
% MATLAB program to illustrate
% String evaluation while using
% switch case
% First letter of the name was
% given small letter intentionally
name = 'geeksforGeeks'
switch(name)
case 'GeeksforGeeks'
fprintf('Hello from %s',name);
otherwise
fprintf('Not Matched!');
end
输出:
Not Matched!
范例3:
的MATLAB
% MATLAB program to illustrate
% switch case
days = 28;
switch(days)
case 28
fprintf('Normal Year!\n' );
otherwise
fprintf('Leap Year\n' );
end
输出:
Normal Year!
嵌套开关盒
这类似于嵌套的if语句。我们可以将switch用作switch内部语句的一部分。即使内部和外部开关的大小写常量包含公共值,也不会发生冲突。
句法:
switch (condition)
case (condition)
switch (condition)
case (condition)
…..
end
case (condition)
end
示例:此示例说明了嵌套开关大小写和字符串比较在MATLAB中的工作方式。
的MATLAB
% MATLAB program to illustrate
% nested switch case
days = 29;
month = 'February'
switch(days)
case 29
fprintf('This Year is ' );
switch month
case 'february'
fprintf('Leap Year!\n' );
otherwise
fprintf('Not Leap Year!\n' );
end
end
输出:
This Year is Not Leap Year!
笔记:
- 与其他编程语言不同,我们不需要在switch情况下使用break语句。
- 与其他编程语言不同,在条件语句中编写条件(即if,if嵌套等)时,我们不使用任何括号(即(),{},:)。我们确实使用end语句来结束条件语句。
- 如有必要,请使用分号(;)以避免不必要的输出。