Octave 教程:if/switch/while/for/break/continue 语句

  本内容将介绍 Octave 的控制语句(if/switch)和循环语句 (for/while/break/continue)的使用方法。
  如果您已经懂任何一门编程语言,只需要粗略阅读即可,基本上就知道如何使用了。如果您不懂任何编程语言,建议详细阅读以下内容。

一、控制语句

  Octave 提供 if 和 switch 控制语句。

1.1 if 语句

1.1.1 if…end 语句

  在 Octave 中,if…end 语句的使用语法如下:

if <expression>
  <statements>
end

  如果表达式的计算结果是 true,将执行其中的语句;否则,不执行。
  在 Octave 中输入以下代码:

fprintf('Hello world !\n');

if false         % 表达式的值为 false,不执行 if 和 end 之间的语句
  fprintf('my name is Octave.\n');
end

运行以上代码,输出以下内容:

Hello world !
1.1.2 if…else…end 语句

  在 Octave 中,if…end 语句的使用语法如下:

if <expression>
  <statements>
else
  <statements>
end

  如果表达式的计算结果是 true,将执行 if 和 else 之间的语句;否则,执行 else 和 end 之间的语句。
  在 Octave 中输入以下代码:

fprintf('Hello world !\n');

if true % 表达式的值为 true,执行 if 和 else 之间的语句
  fprintf('my name is Octave.\n');
else
  fprintf('my name is Matlab.\n');
end

运行以上代码,输出以下内容:

Hello world !
My name is Octave.
1.1.3 if…elseif…else…end 语句

  在 Octave 中,if…end 语句的使用语法如下:

if <expression>
  <statements>
elseif
  <statements>
else
  <statements>
end

  最终只会执行其中的某一个语句。在实际使用中,中间可以存在多个 elseif,并且最后一个 else 是可选的。
  在 Octave 中输入以下代码:

fprintf('Hello world !\n');

a = 20;
if a == 10  % 表达式的值为 false,跳转到 elseif 语句进行判断
  fprintf('my name is XXX\n');
elseif a == 20% 表达式的值为 true,执行elseif和else之间的语句,执行完之后,跳转到 end 之后
  fprintf('my name is Octave.\n');
else
  fprintf('my name is Matlab.\n');
end

运行以上代码,输出以下内容:

Hello world !
My name is Octave.
1.1.4 嵌套 if 语句

  在 Octave 中,if…end 语句的使用语法如下:

if <expression>
  if <expression>
    <statements>
  end
end

  在 Octave 中输入以下代码:

fprintf('Hello world !\n');

a = 10;
if a < 20
  if true
    fprintf('my name is Octave.\n');
  end
end

运行以上代码,输出以下内容:

Hello world !
My name is Octave.

1.2 switch 语句

  switch 语句有条件地执行一组语句,这组语句是从几个选项里选择执行的,其中每个选项涵盖了一个 case 语句。
  在 Octave 中,switch 语句的使用语法如下:

switch <switch_expression>
  case <case_expression>
    <statements>
  case <case_expression>
    <statements>
  ...
  ...
  otherwise
    <statements>
end

  当有一个 case 表达式为 true 时,Octave 就执行与之对应的语句,并且不再执行其他语句,然后直接退出 switch 语句块。其中, otherwise 语句块是可选的,case 语句块个数为任意多个。
  注意:在 Octave 中其他 case 的关键字是 otherwise,和一般的编程语言的 default 不同。
  在 Octave 中输入以下代码:

fprintf('Hello world !\n');

name = 'Octave';
switch (name)
  case 'Matlab'
    fprintf('My name is Matlab.\n');
  case 'Octave'
    fprintf('My name is Octave.\n');
   otherwise
    fprintf('My name is XXX.\n');
end

运行以上代码,输出以下内容:

Hello world !
My name is Octave.

  switch 语句和 if 语句一样也可以进行嵌套使用,具体使用方法大致相同,就不详细说明了。

二、循环语句

  在一般情况下,程序中的语句都是按照顺序执行的。不过循环语句允许程序多次执行一个语句。
  Octave 提供 while 和 for 循环语句,以及 break 和 continue 循环控制语句。

2.1 while 循环

  只要表达式为 true,while 循环会重复执行 while 和 end 之间的运算式。
  在 Octave 中,while 语句的使用语法如下:

while <expression>
  <statements>
end

  在 Octave 中输入以下代码:

a = 10;
while (a < 15)
  fprintf('value of a: %d\n', a);
  a++;
end

运行以上代码,输出以下内容:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14

2.2 for 循环

  for 循环是一个重复的控制结构,可以有效地写一个循环,只是执行的次数是特定的。
  在 Octave 中,for 语句的使用语法如下:

for index = values
  <statements>
end

  for 循环的值有下面三种形式:

格式 描述
init_val:end_val 索引变量以步进值为 1,从初始值递增,并重复执行程序语句,直到索引值大于终值。
init_val:step:end_val 与上面的格式相同,不过这里的步进值为 step。
val_array 在每个迭代 val_array 数组的后续列中创建列向量索引。

  看完上面的解释可能还是很懵懂,可以参照下面的例子进行理解。
例 1:
  在 Octave 中输入以下代码:

for a = 10:20
  fprintf('value of a: %d\n', a);
end

运行以上代码,输出以下内容:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20

例 2:
  在 Octave 中输入以下代码:

for a = 10:2:20
  fprintf('value of a: %d\n', a);
end

运行以上代码,输出以下内容:

value of a: 10
value of a: 12
value of a: 14
value of a: 16
value of a: 18
value of a: 20

例 3:
  在 Octave 中输入以下代码:

for a = [10, 20, 16, 18, 12, 14]
  fprintf('value of a: %d\n', a);
end

运行以上代码,输出以下内容:

value of a: 10
value of a: 20
value of a: 16
value of a: 18
value of a: 12
value of a: 14

  循环语句和控制语句一样,也可以进行嵌套使用。

2.3 break 语句

  break 语句用于终止 for 或 while 循环的执行。当在循环体内执行到该语句的时候,程序将会跳出循环,继续执行循环体语句的下一语句。
  在 Octave 中输入以下代码:

for a = [10, 20, 16, 18, 12, 14]
  if (a == 18)
    break;
  end
  fprintf('value of a: %d\n', a);
end

运行以上代码,输出以下内容:(当 a == 18 时,直接跳出循环体)

value of a: 10
value of a: 20
value of a: 16

2.4 continue 语句

  continue 语句控制跳过循环体的某些语句。当在循环体内执行到该语句时,程序将跳过循环体中剩余的语句,继续下一次循环。与 break 的不同点是:break 是直接跳出循环体;而 continue 只是终止本轮循环,没有跳出循环体。
  在 Octave 中输入以下代码:

for a = [10, 20, 16, 18, 12, 14]
  if (a == 18)
    continue;
  end
  fprintf('value of a: %d\n', a);
end

运行以上代码,输出以下内容:(当 a == 18 时,终止本轮循环,继续下轮循环。可以对比上面的 break 语句例子)

value of a: 10
value of a: 20
value of a: 16
value of a: 12
value of a: 14

参考:
[1] https://www.w3cschool.cn/matlab/

猜你喜欢

转载自blog.csdn.net/benzhujie1245com/article/details/83177393