Conditional

Reference.Conditional History

Hide minor edits - Show changes to output

Changed line 28 from:
!![[#select]]The Select…Case Statement
to:
!![[#Select]]The Select…Case Statement
Changed lines 37-38 from:
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 statement. For example,
to:
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,
=firewing [=
Changed lines 40-42 from:
   Dim MenuChoice As Char = "c"
  Dim Message As String
  Select MenuChoice
to:
   Dim menuChoice As Char = "c"
  Dim message As String
  Select menuChoice
Changed line 44 from:
         Message = "Quit"
to:
         message = "Quit"
Changed line 46 from:
         Message = "Main Menu"
to:
         message = "Main Menu"
Changed line 48 from:
         Message = "Option A"
to:
         message = "Option A"
Changed line 50 from:
         Message = "Option B"
to:
         message = "Option B"
Changed line 52 from:
         Message = "Option C"
to:
         message = "Option C"
Changed line 54 from:
         Message = "Error"
to:
         message = "Error"
Changed lines 57-59 from:

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,
to:
=]
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,
=firewing [=
Changed lines 61-66 from:

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
to:
=]
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,
=firewing [=
Select value
* 2
Changed line 66 from:
     Result = 1
to:
     result = 1
Changed line 68 from:
     Result = 2
to:
     result = 2
Changed line 70 from:
     Result = 0
to:
     result = 0
Changed lines 72-80 from:

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.

14.3 - 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,
to:
=]
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.

!![[#Jump]]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,
=firewing [=
Changed lines 82-84 from:
   Dim Value As Byte = 0
  Dim LED As PORTB.0 
If Value <> 0 Then GoTo SkipCode
to:
   Dim value As Byte = 0
  Dim Led As PORTB.0 
If value <> 0 Then GoTo SkipCode
Changed line 89 from:
   Low(LED)
to:
   Low(Led)
Changed lines 91-94 from:

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
to:
=]
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'''.
Added lines 1-96:
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'''.

!![[#IfThen]]The If…Then Statement

 '''if''' expression '''then'''
  statements
 ['''elseif''' expression '''then''']
  {statements}
 ['''else''']
  {statements}
 '''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,
=firewing [=
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"
  Else
      message = "Error"
  End If
End Sub
=]

!![[#select]]The Select…Case Statement

 '''select''' expression
  '''case''' condition {, condition}
      {statements}
  …
  ['''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 statement. 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.

14.3 - 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
  High(LED)
  DelayMS(500)

SkipCode:
  Low(LED)
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