Back
| Next
Lesson
Conditional Statements
Last lesson (#007 - Evaluating Data), we discussed
how to compare expressions for trueness. Conditional
statements allow you to design program
flow or logic into your program. By evaluating certain conditions, you
can dictate which subcode sets to execute, how many times, and when.
All conditional statements begin and end with Sigmac keywords.
All keywords are lowercase.
If/Endif
The if/endif
conditional statement is a switch type
statement - if #true, execute the subcode - if not, skip it.
Example #1 - Single Switch:
006
if i1 == 100
007 /* subcode . . . .
008 endif
Example #2 - Multiple Switch Option:
006 if i1 <= 100
007 /* subcode . . . .
008 elseif i1 <= 200
009 /* subcode . . . .
010 elseif i1 <= 300
011 /* subcode . . . .
012 endif
You can place as many elseif statements as required. ARRIS will evaluate these conditional statements in the same order as you write them. This is important because once any if/elseif statement is evaluated to be #true, the appropriate subcode is executed. The program's control is directed to the matching endif statement and the remaining elseif statements are never evaluated nor executed.
Tip: On multiple switches, it may be desirable to order evaluations from specific conditions to general condidtions.
Example #3 - Adding a Default Action
006 if i1 <= 100
007 /* subcode . . . .
008 elseif i1 <= 200
009 /* subcode . . . .
010 else
011 /* subcode . . . .
012 endif
If i1 is greater than 200, the subcode listed on line #011 will execute by default. You can only place one else statement after all if/elseif statements and prior to the endif statement.
Loopwhile/Endloop
The loopwhile/endloop set is a program loop with the condition evaluated at the beginning. This means the subcode may never be executed (main difference with repeat/until).
Once the stated condition returns #true, the loop will release the program's control.
Example #4 - Simple Loop
006 loopwhile (ik < 10)
007 /* subcode . . . .
008 endloop
Assuming ik is being incremented in the subcode, this loop will execute the same subcode x number of times and exit at the endloop statement (#008).
Example #5 - Add a Loop Counter
006 loop ik=0 while ik < 10
007 /* subcode . . . .
008 end ik=(ik+1) loop
Now ik is being initiated in the loopwhile statement (#006) to zero and is being incremented (or possibly decremented) in the endloop statement (#008), this loop will execute the same subcode 10 times and exit at the endloop statement.
Repeat/Until
The repeat/until set is a program loop with the condition evaluated at the end. This means the subcode will always be executed at least once (main difference with loopwhile/endloop).
Example #6 - Simple Loop
006 repeat
007 /* subcode . . . .
008 until ik == 10
Once the stated condition returns #false, the loop will release the program's control. Assuming ik is being incremented in the subcode, this loop will execute the same subcode x number of times and exit at the until statement (#008).
There is no builtin initialization or incrementor/decrementor in the repeat/until statements.
Nesting
Nesting is when you place one of these statement groups within another. You can place a if/endif inside a repeat/until loop or vice-versa. Each statement group within another is considered a level. You may nest as many levels as required by your program.
Example of Nesting (3 levels):
006 if ik < 10
007 repeat
008 if ik > 5
009 /* subcode . . . .
010 endif
011 ik=(ik+1)
012 until ik == 10
013 . . . .
014 endif
The only rule concerning nesting is that each level terminates before its parent level terminates.
Example of Illegal Nesting:
006 repeat
007 if ik == 10
008 until ik == 100
009 endif
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