SKILL Language 的运算符和语句:
关系运算符(Relational Operators): <, <=, >, >=, ==, !=
逻辑运算符(Logical Operators
): !, &&(and), ||(or)
判断语句(Branching): if, when, unless
(Multi-way Branching): case
循环语句(Iteration): for, foreach
Relational Operators
关系操作符和C语言基本差不多主要是用于判断,但是要注意Skill的关系操作符返回值是t或者nil。
图片待定
Logical Operators
使用以下操作符比较数据值。如果数据类型不合适,SKILL将生成错误。这些操作符都返回t或nil。
图片待定
逻辑操作符主要是用于完成“与”和“或”运算,返回值可以使数值、或者t或者nil。
!注:SKILL中只有nil 是假(FALSE),其余的任何值都是真(TRUE)。
and/or逻辑操作只有在需要计算第二个表达式时,才计算第二个表表达式:
&&操作:当第一个表达时为假时,不计算第二个表达式。
当第一个表达式为真时,计算第二个表达式
|| 操作:当第一个表达时为假时,计算第二个表达式。
当第一个表达式为真时,不计算第二个表达式
其返回的结果是最后一个计算的表达式,因此and/or逻辑操作可以代替繁琐的if / when控制语句。
(!注:此处是不是就与c语言不同了,对比着学习不但可以巩固c,还可以提高skill)
Branching
使用if函数有选择地对一组或多组表达式求值。if表达式中的条件计算为nil或非nil。if表达式的返回值是最后计算的值。
shapeType = "rect"
if( shapeType == "rect"
then
println( "Shape is a rectangle" )
++rectCount
else
println( "Shape is not a rectangle" )
++miscCount
)
SKILL在执行过程中执行大部分的错误检查。涉及if表达式的错误消息可能是模糊的,一定要注意:
注意括号的位置:if(…then…else…)
避免在if关键字之后立即出现空白。在符合逻辑的情况下使用then和else。
当您不小心在if关键字后放置空白时,请考虑错误消息:
shapeType = "rect"
if ( shapeType == "rect"
then
println( "Shape is a rectangle" )
++rectCount
else
println( "Shape is not a rectangle" )
++miscCount
)
Message: *Error* if: too few arguments (at least 2 expected, 1 given)…
当您不小心删除then关键字,但是包含了else关键字,并且条件返回nil时,请考虑错误消息:
shapeType = "label"
if( shapeType == "rect"
println( "Shape is a rectangle" )
++rectCount
else
println( "Shape is not a rectangle" )
++miscCount
)
Message: *Error* if: too many arguments …
Multi-way Branching
Iteration