热度 17| |
The if Function
Use the if function to selectively evaluate two groups of one or more expressions.
使用if函数来有选择地评估一个或多个表达式的两个组。
The condition in an if expression evaluates to nil or non-nil.
if表达式中的条件将被评估为nil或非nil。
The return value of the if expression is the value last computed.
if表达式的返回值是最后计算的值
if( shapeType == "rect"
then
println( "Shape is a rectangle" )
++rectCount
else
println( "Shape is not a rectangle" )
++miscCount
);if
这段Cadence Skill代码是一个条件语句,根据变量shapeType的值来判断形状类型是否为矩形。如果shapeType的值为"rect",则输出"Shape is a rectangle"并将rectCount加1;否则输出"Shape is not a rectangle",并将miscCount加1。在Cadence Skill中,条件语句使用if-then-else结构表示,语句结尾需要添加分号。
这里的++miscCount是一个自增运算符,表示将变量miscCount的值加1。它等价于miscCount = miscCount + 1。
SKILL does most of its error checking during execution.
SKILL在执行过程中会大部分进行错误检查。
Error messages involving if expressions can be obscure.
涉及if表达式的错误信息可能会比较晦涩难懂。
Be sure to
■ Be aware of the placement of the parentheses: if( … then … else … ).
■ Avoid white space immediately after the if keyword.
■ Use then and else when appropriate to your logic.
Consider the error message when you accidentally put white space after the if keyword.
当你在if关键字后面意外地加上空格时,请考虑错误信息。
shapeType = "rect"
if ( shapeType == "rect"
then
println( "Shape is a rectangle" )
++rectCount
else
println( "Shape is not a rectangle" )
++miscCount
) ;if
Message: *Error* if: too few arguments (at least 2 expected, 1 given)…
Consider the error message when you accidentally drop the then keyword, but include an else keyword,
and the condition returns nil.
当你意外省略了then关键字,但包含了else关键字,并且条件返回nil时,请考虑错误信息
shapeType = "label"
if( shapeType == "rect"
println( "Shape is a rectangle" )
++rectCount
else
println( "Shape is not a rectangle" )
++miscCount
) ;if
Message: *Error* if: too many arguments …
The when and unless Functions
Use the when function whenever you have only then expressions.
每当你只有then表达式时,请使用when函数。
when( shapeType == "rect"
println( "Shape is a rectangle" )
++rectCount
) ; when
when( shapeType == "ellipse"
println( "Shape is an ellipse" )
++ellipseCount
) ; when
Use the unless function to avoid negating a condition. Some users find this less confusing.
使用unless函数来避免否定一个条件。一些用户认为这样做会更不容易混淆。
unless( shapeType == "rect" || shapeType == "line"
println( "Shape is miscellaneous" )
++miscCount
) ; unless
如果形状类型不是矩形或线条,则打印出“形状是杂项”并增加杂项计数器的值。
使用了unless函数来避免对条件取反,即当条件不满足时执行代码块。
The when and unless functions both return the last value evaluated within their body or nil.
The case Function
The case function offers branching based on a numeric or string value.
case函数根据数值或字符串值提供分支选择。
case( shapeType
( "rect"
++rectCount
println( "Shape is a rectangle" )
)
( "line"
++lineCount
println( "Shape is a line" )
)
( "label"
++labelCount
println( "Shape is a label" )
)
( t
++miscCount
println( "Shape is miscellaneous" )
)
) ; case
这段代码是一个基于形状类型进行分支选择的代码块。它使用了case函数,根据shapeType的值进行选择。
如果shapeType的值等于"rect",则执行++rectCount(矩形计数器加1)和println("Shape is a rectangle");
如果shapeType的值等于"line",则执行++lineCount(线条计数器加1)和println("Shape is a line");
如果shapeType的值等于"label",则执行++labelCount(标签计数器加1)和println("Shape is a label");
如果shapeType的值不是上述三个值,则执行++miscCount(杂项计数器加1)和println("Shape is miscellaneous")。
最后返回case函数的返回值。
代码的目的是根据给定的shape类型执行特定的操作并进行计数跟踪,同时通过打印语句输出相应的形状信息。
The optional value t acts as a -all and should be handled last. The case function returns the value of the last expression evaluated. In this example:
可选值t起到了类似于"-all"的作用,应该在最后处理。case函数返回最后一个被评估的表达式的值。在这个例子中:
■ The value of the variable shapeType is compared against the values rect, line, and label. If SKILL finds a match, the several expressions in that arm are evaluated.
变量shapeType的值与"rect"、"line"和"label"进行比较。如果匹配成功,则会评估该分支中的多个表达式。
■ If no match is found, the final arm is evaluated.
如果没有找到匹配项,则会评估最后一个分支。
■ When an arm’s target value is actually a list, SKILL searches the list for the candidate value. If SKILL finds the candidate value, all the expressions in the arm are evaluated.
当某个分支的目标值实际上是一个列表时,SKILL会在列表中搜索候选值。如果找到候选值,则会评估该分支中的所有表达式。
case( shapeType
( "rect"
++rectCount
println( "Shape is a rectangle" )
)
( ( "label" "line" )
++labelOrLineCount
println( "Shape is a line or a label" )
)
( t
++miscCount
println( "Shape is miscellaneous" )
)
) ; case
aznsj: 真的别学, 没用, 还不如学哥MATLAB PYTHON。 都可以做。
aznsj: SKILL 没有市场, 只能用cadence. 不要学 浪费时间,有空多学python. 什么都可以