Neatware Header
Home   Products   Forums   Partners   Buy   Company   Contact   Blog   

4. Control

4.1 Sequence

Sequence commands execute each command one after another.



  • set v e command assigns a value e at the second argument to a variable v at the first argument.
  • incr v n command increases variable value v by increment n. Its first argument is a variable name and its second optional argument is an increment integer. Negative increment will decrease a variable. Its default increment is 1 if there is no second argument.
  • expr e command evaluates math expression with C expression syntax. It concatenates all the arguments to form an input string. The expression may be integer, double, or boolean. The return value is a numeric string. Many basic math functions in the standard C math library are available as built-in. In mCL x is equivalent to $x as a value. However you need to use $foo(0) as an element of an array foo to distinguish a function such as foo(x).
  • puts s command shows its argument string to console. There is only one argument of puts. In many cases it is convenitnet to show a variable’s value for simple debugging with puts.
  • comment of mCL starts with # in the beginning of a line. It is better to treate comment # as a command because there are some quirks prohibited you from writing comment anywhere. (i.e. no # inside the switch command.)

4.2 Condition

Condition control may select a command to execute according to the value of a variable.

  • if command will execute truebody when the boolean-expression is true, otherwise it will execute elsebody. Its body is a group of commands. The else and elseif keyword are optional for if command. boolexpr is an expression returned a boolean value. You can use a variable to represent its value without using the $ substitution.
if {boolexpr} {
	truebody
} else {
	elsebody
}
  • The format of a switch command is:
switch option value pattern body ...

switch command compares a value to pattern. If one of them is matched then program executes the related body. The first argument of the switch command is an option. The '-exact' attribute will match the value to the pattern exactly; '-glob' attribute will use glob pattern matching; and '-regexp' will match with regular expression pattern. '--' represents the end of the option. The last pattern 'default' will execute its body if no patterns are matched before.

4.3 Loop

Loop commands execute a group of commands in iteration. The iteration may terminate after all the elements are traversed or a condition expression becomes true.

  • foreach command repeatly executes its body until all the elements in a list have been traversed. Its form is,
foreach var list body

The var is the current loop variable that is assigned an element from the list one after another. foreach traverses all the elements in the alist. You can also declare two or more loop variables. The variables will orderly sample the elements in the list until all of them are traversed. Below example shows that varaiable (v1 v2) pair is assigned the value (a b) respectively, and then the value (c d), and so on.

foreach {v1 v2} {a b c d e f} {
	puts "($v1 $v2)"

}

To loop over multiple lists, you may organize arguments in variable and list pair order. The variable var may also be multiple variables. A loop variable will be set to empty element {} when its list has finished traveser but the entire loop did not terminated.

foreach v {a b c d} {v1 v2} {1 2 3 4 5 6} {
	puts "($v) ($v1 $v2)"
}
  • while command evaluates the boolean-expression, if it is true then execute the body, and then evaluates the boolean-expression again until the boolean-expression is not true. Its syntax is like while statement of C language.
while boolexpr body 
  • The for command format is,
for initial boolexpr increment body

At first it evaluates the initail argument and then evaluates the boolexpr. If the boolexpr is true it executes the body and increment. Repeatlly, it evaluates the boolexpr again and continues the loop until boolexpr returns a false value.

4.4 Return

return command comes back from a procedure with a value. break command exits from a loop. And continue command will goto the start of a loop to execute the next iteration. For example,


4.5 Exception

Exceptions raise abnormal conditions during the execution of commands.



  • catch command caught the exceptions of a command during its execution. Its format is,
catch {command args ... } result

catch command sets trap to the command in the curly braces. When there is an exception during the command execution, the exception message is assigned to the variable 'result', otherwise the 'result' gets the return value of the command. catch command returns zero when no exception is raised, otherwise returns non-zero.

  • error command generates an error code. Its first argument is a string that indicates the reason of an error.
catch {...} errmsg
error $errmsg