Back
| Next
Lesson
UnConditional Statements
Last lesson (#008 - Conditional Program Flow),
we discussed conditional statements and their affect on the program's flow by
first evaluating expressions. UnConditional
statements allows you to direct the program's
flow to another part of your program without evaluating conditions.
Most unconditional statements requires a programmer defined keyword called a
label.
A label is like a bookmark in your program. A label is identified by either
a 1 or 2 colons (:) suffixed.
Note:
Do not confuse these colons used as suffixes with the colons used as prefixes when calling sigmac programs (Lesson #006 - Calling All Sigmacs).
Goto
The goto statement redirects the program's flow to the specified label (suffixed by a single colon) without any expectation of returning to the goto statement.
Example #1:
006 goto skip
007 . . . .
. . . .
022 . . . .
023 skip:
024 . . . .
Lines #007-022 will never execute. The goto statement on line #006 redirects the program's flow to line #023.
Call/Subroutine/Return
The call statement redirects the program's flow to a subroutine. A subroutine is a subset of code, often called more than once by the main body of the program (one subroutine may be called from multiple call statements). All subroutines reside at the end of the written program, after the last executing statement in the main body. The order of individual subroutines do not matter.
The power of subroutines is in "writing the code once". This reduces logic errors, increases maintainability and is easier to debug.
Example #2 - Call Subroutine
004 . . . .
005 isq=4
006 call calc_sq
007 /* isq now equals 16
008 . . . .
. . . .
021 . . . .
022 exit
023 /** Subroutines begin here **
024 cal_sq::
025 isq = (isq * isq)
026 return
Line #006 calls calc_sq:: on line #024. The subroutine executes its subcode. The return on line #026 will return the program's control to line #007. Now lines #007 thru #022 will execute.
A subroutine begins with a label suffixed by double colons and ends with the return statement. The return statement redirects the program's flow back to the statement following (#007) the originating call statement, making call/return bi-directional.
A subroutine may have more than one return statement, or the subroutine may call other subroutines, or the program may terminate in a subroutine.
Example #3 - Call Subroutine with several exit points
004 . . . .
005 isq=4
006 call calc_sq
007 /* isq now equals 16
008 . . . .
. . . .
021 . . . .
022 exit
023 /** Subroutines begin here **
024 cal_sq::
025 if isq == 0
026 exit
027 elseif isq < 0
028 isq = 0
029 $error(0,'Negative Numbers')
030 return
031 else
032 isq = (isq * isq)
033 endif
034 return
Line #006 calls calc_sq:: on line #024. The subroutine executes its subcode. Either return on line #030 or 034 will return the program's control to line #007. Now lines #007 thru #022 will execute.
Exit
The exit statement terminates the program's flow cleanly. $ferror() will abort your program with a fatal error message.
That's all for now,
Steve
This lesson was brought to you by SCG consulting. All written
materials related to these Sigmac lessons are copyrighted by SCG and
intended for personal use only. Any commercial or non-commercial reproduction
for public use is prohibited without written consent from SCG.
Back
| Next
Lesson