From Firewing

Reference: Conditional

Conditional statements are used in a program to alter the operational flow. That is, to decide which statement or statements to execute, based on the evaluation of a single value or expression. Firewing supports three types of conditional statements: if…then, select…case and conditional jump.

The If…Then Statement

 if expression then
 [elseif expression then]
 end if

The if…then statement is used to make a program execute user code, but only when certain conditions are met. If an expression evaluates to true, then any statements following the evaluation are executed. If no expression evaluates to true, then any statements contained after an optional else are executed. For example,

Sub Main()
   Dim value As Byte = 0
   Dim message As String
   If value <= 10 Then
      message = "OK"
   ElseIf value > 10 And value < 20 Then
      message = "Warning"
      message = "Error"
   End If
End Sub

The Select…Case Statement

 select expression
   case condition {, condition}
   [else {statements}]
 end select  

Although there is nothing technically wrong with using large if…then blocks, it can sometimes be difficult to read them. The select…case statement is an alternative to using a large or multiple if…then…elseif statements. For example,

Sub Main()
   Dim menuChoice As Char = "c"
   Dim message As String
   Select menuChoice
      Case "Q", "q" 
         message = "Quit"
      Case "M", "m" 
         message = "Main Menu"
      Case "A", "a" 
         message = "Option A"
      Case "B", "b" 
         message = "Option B"
      Case "C", "c" 
         message = "Option C"
      Case Else
         message = "Error"
   End Select     
End Sub

In this example, the select part is a single char type which is tested against each successive case condition. The commas used in the case conditions are equivalent to using an if statement’s logical or operator. For example, you could write the first case as an if…then statement in the following way,

If MenuChoice = "Q" Or MenuChoice = "q" Then 

If one of the case conditions is met, then any statements following the condition are executed and the program jumps to any code immediately following end select. If no case conditions are met, statements contained in the optional else clause are executed. Case conditions can also include relational operators, or the "to" operator can be used for a range of values. For example,

Select value * 2
   Case < 10, > 100 
      result = 1
   Case 10 To 20, 50 To 100 
      result = 2 
   Case Else
      result = 0
End Select

In this example, value is multiplied by two and then tested against each case condition. If the select expression is < 10 or > 100, then result becomes equal to 1. If the select expression is in the range 10 to 20 or 50 to 100, then result becomes equal to 2. If none of the select conditions are met, result is set to 0 in the select…case else block.

Conditional Jump

 if expression then goto label

The conditional jump is a special construct that can be used with a standard goto statement. For example,

Sub Main()
   Dim value As Byte = 0
   Dim Led As PORTB.0  
If value <> 0 Then GoTo SkipCode

End Sub

Notice the difference in syntax when compared to a normal if…then statement. Firstly, no "end if" is required. You can of course use a goto inside a normal if…then statement, but the above form allows you to write the same thing more concisely.

The goto statement has a nasty reputation because of its ability to jump to almost anywhere. Some people view this lack of control as very bad. Using a goto can produce what is called spaghetti code. It gets this name because with a goto infested program, drawing a line between a goto and its destination label would look like a big plate of spaghetti. Used with care, a goto statement can be useful.

However, given the highly structured nature of the compiler language, a goto statement should be used sparingly and is best avoided.

Retrieved from
Page last modified on December 09, 2012, at 01:29 PM