一、控制语句
1.1 if … else 语句
-
基本语法格式:
if expression statements elseif expression statements else statements end
-
Octave 中条件不需要用括号 () 括起来 (当然使用括号会让程序更加明
了), 而且 statments 程序块不需要用大括号*{ }* 括起来。
-
Octave 中需要用 end 命令来标识 if 语句的 结束。。在 Octave 中,if…end 和 if…endif 都是合法的. 使用 end 可以让 Octave 的文件与 Matlab 文件 兼容
-
像 if 这样的控制语句,通常都使用在 Octave 的脚本之中,同样也能在命令行中使用
-
Octave 缩进没有影响
>> a=1;b=2; >> if a>b c=5 else c=6 end c = 6 >>
1.2 switch 语句
-
若需要多重 if/elseif 语句来实现从多重选项中选择时,使用 switch 语句更好