Home   News   Product   Order   Forum   Feedback   Partner   Profile © Neatware 2000
Neatware Control
Sequence, Condition, Loop
Home : Last Update: Thu Jul 06 16:05:44 2000
  1. Sequence

    Sequence control organizes the command flow in sequence. Each command is executed one after another. Following paragraphs introduce primitive sequence commands.

    • set

      set command assigns a value to a variable. Its first argument is a variable and the second argument is an expression. You can use a variable anywhere without declare its type. To access a variable uses the dollar sign $.

      	set v "a string value"
      	set u $v

    • incr

      The first argument of incr command is a variable name. The second optional argument is increment integer. incr command increases variable value by increment. Negative increment will decrease a variable. The default value of increment is 1.

      	incr x                 # x increase 1
      	incr x -1              # x decrease 1
      	incr x [expr {2*3}]    # x increment

    • expr

      expr command evaluates math expression with C expression syntax. It concatenates all the arguments to form a input string. The expression may be integer, floating point, and boolean. The return value is a numeric string. Many basic math functions in the standard C math library has been built in.

      	expr {2/3}                         # return 0    (surprise?) 
      	expr {2.0/3.0}                     # return 0.666666666667
      	expr {asin(1.0)*round(sqrt(2))}    # return 1.579079632679 

    • # comment

      MCL comment starts with # in the start of a line. It is better to treate comment # as a command. There are some quirks prohibited you from writing comment anywhere. (i.e. no # inside the switch command.)

      	set x [expr 2*3];	# comment here.
      	# comment start 

    • puts

      puts command outputs its argument string to stdout. It has only one argument.

      	puts "output a $string and a [compute $value]" 

  2. Condition

    Condition control may select a command to execute according to the variable value. It branches the command flow.

    • if

      if command will execute truebody when the 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. Following codes are several forms of if command:

      	if {expression} {truebody}
      	if {expression} {truebody} else {elsebody}
      	if {expression1} {
      	} elseif {expression2} {
      	} else {

    • switch

      The syntax of switch command is:

      	switch option value pattern body ... 
      	switch option value {pattern body ...}
      switch command compares a value with patterns. 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.

      In the following example, since pattern and body pairs are grouped into an argument, there is no substitution inside the pattern/body pairs.

      	switch -exact -- $val {
      	  first {doFirst}
      	  second {doSecond}
      	  third {doThird}
      	  default {doDefault}
      following example can substitute its patterns
      	switch -glob -- $val  $v1 do_v1  $v2 do_v2  $v3 do_v3

  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 become true.

    • foreach

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

      	foreach var alist ... body
      The var is the current loop variable that is assigned an element from the alist one after another. foreach will traverse all the elements in the alist. This command is a compact expression of iteration.
      	foreach v {a b c d e} {
      	  puts $v
      You can declare two or more loop variables. The variables will orderly sample the elements in the list until all of them are traversed. Following 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 var/list pair order. The variable var may also be multiple variables. A loop variable will be set to empty {} 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

      while command evaluates the expression, if it is true then execute the body, and then evaluates the expression again until the expression is not true. Its syntax is like while statement of C language.

      	while expression body 
      An example of while command is,
      	set count 7 
      	while {$count > 0} { 
      	  puts "2*$count" 
      	  incr count -1 

    • for

      The for command syntax is,

      	for initial expression increment body
      At first it evaluates the initail argument and then evaluates the expression. If the expression is true it executes the body and increment. Repeatlly, it evaluates the expression again and continues the loop until expression returns a false value.
      	set len 7
      	for {set count 0} {$count < $len} {incr count 1} {
      	  puts "2*$count"

  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.

    	set b 6
    	set c 5
    	set len 7
    	for {set count 0} {$count < $len} {incr count 1} {
    	  if {$count == $b} {
    	  } elseif {$count == $c} {
    	return $c 

  5. Exception

    Exceptions raise abnormal conditions during the execution of commands.

    • Catch

      catch command caught the exceptions of a command during its execution. Its syntax 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.
      	if [catch {test $exception} result] {
      	  puts "Exception: $result"
      	} else {
      	  puts "OK: $result"

    • Error

      error command generates an error code. Its first argument is a string that indicates the reason of an error.

      	catch {...} errmsg
      	error $errmsg

    << Syntax         Type >>

Home : Last Update: Thu Jul 06 16:05:44 2000
Sequence, Condition, Loop
Neatware Control
Home   News   Product   Order   Forum   Feedback   Partner   Profile © Neatware 2000