Repetitive

Repetitive statements enable one or more statements to repeat. The Firewing compiler has three repetitive statements: while…end while, do…loop until and for…next.

The While…End While Loop

 while expression
   {statements}
 end while

A while…end while loop will execute one or more statements if the expression evaluates to true. When an expression becomes false, the while loop terminates. The condition can be any boolean expression or a single boolean variable. The following example shows how a while…end while loop can be used to count from 0 to 9, outputting each value in turn,

While index < 10
   Console.Write(CStr(index), 13, 10)
   index += 1
End While

The while…end while loop is can be useful for delaying program execution until a certain event has occurred. For example, if we write

Dim pinIsHigh As PORTB.Booleans(0)
While pinIsHigh
End While

then the code following end while is not executed until PORTB.0 becomes equal to false (0).

The Do…Loop Until Loop

 do
   {statements}
 loop until expression

A do…loop will execute one or more statements if the expression evaluates to false. When an expression becomes true, the loop terminates. The condition can be any boolean expression or a single boolean variable. Unlike a while…end while loop, any statements in a do…loop will be executed at least once, since the conditional test is evaluated at the bottom of the loop. For example,

Dim index As Byte = 0
Do
   Console.Write(CStr(index), 13, 10)
   index += 1
Loop Until index > 9  

Note the conditional termination logic of a do…loop expression is the opposite of a while…end while loop. That is, a while…end while will loop while some condition is true but a do…loop will loop while some condition is false.

The For…Next Loop

 for variable [as type] = expression to expression [step expression]
   {statements}
 next [variable]

The for…next loop will execute one or more statements a predetermined number of times. Unlike while…end while or do…loop until, the for…next loop does not use a boolean expression for termination control but a control variable. For example,

Dim index As Byte
For index = 0 To 9
   Console.Write(CStr(index), 13, 10)
Next

In this example, the control variable is index. The control variable must be an integer type, such as byte. It cannot be a non-integer, such as floating point. The control variable is initialized to zero when the loop begins and terminates when the control variable is greater than nine. In other words, the for…next iterates through its loop ten times (0 to 9). If no step value is given, the control variable is incremented by one each iteration. Note that with a for…next loop, the control variable can be declared within the for…next block. For example,

For index As Byte = 0 To 9
   Console.Write(CStr(index), 13, 10)
Next

You can change the default increment value of the control variable by specifying a step value. For example,

For index As Byte = 0 To 9 Step 3
   Console.Write(CStr(index), 13, 10)
Next

will output 0, 3, 6 and 9. If the start expression is larger than the end expression (that is, you want to count downwards) then a negative step value must always be specified. For example,

For Index As Byte = 9 To 0 Step -3
   Console.Write(CStr(Index), 13, 10)
Next

will output 9, 6, 3 and 0.

Short Circuit Boolean Expressions

Statements such as if…then, if…goto, while…end while and do…loop until depend on the evaluation of a boolean expression to determine program flow or to control loop iterations. The Firewing compiler uses short circuit evaluation, which can make these statements execute more quickly. For example,

If a < b And c = d And e > f Then
   ' execute statements
End If   

The if…then statement block will only be executed if all of the three conditions are true. That is, if a is less than b and c is equal to d and e is greater than f. However, if a is NOT less than b, then there is no point testing any of the other conditions, because the final result will always be false. In short, the Firewing compiler will immediately stop evaluating any boolean expression, if a certain outcome becomes known in advance. The expression is said to short circuit, causing the program to skip over the rest of the evaluation code.

Exit Statement

Calling exit from within a while…end while, do…loop until or for…next will force your program to exit that particular code block. For example,

Imports USART
Sub Main()
   Dim index As UShort = 0
   While index < 1000
      If USART.DataAvailable Then
         Exit While
      End If
      USART.Write(CStr(index), 13, 10)
      DelayMS(100)
      index += 1
   End While
End Sub

If the DataAvailable flag is false, the while…end while loop will iterate normally. However, if the hardware USART in this example receives data, the DataAvailable flag becomes true and the loop terminates.

Using exit too often can result in multiple exit points in a block of code, which can make your program difficult to debug and harder to read. When possible, it is better programming practice to allow your looping construct to control all exit conditions. For example, the previous code sample code could be written as,

While index< 1000 And Not USART.DataAvailable
   USART.Write(CStr(index), 13, 10)
   DelayMS(100)
   index += 1
End While

If you find yourself using exit inside a for…next loop, it may be an indication that a while…end while or do…loop until statement is a more appropriate construct to use. Note that exit can also be used to exit subroutine or function block. For example,

Sub MySub()
   For index As Byte = 0 To 10 
      Console.Write(CStr(index),13,10)
      If index = 5 Then
         Exit Sub ' exit subroutine
      End If   
   Next
End Sub

Continue Statement

Calling continue from inside a while…end while, do…loop until or for…next loop will force your program to begin the next iteration of the currently executing loop. For example,

Sub Main()
   For Index As Byte = 0 To 5 
      If Index = 3 Then
         Continue For
      End If   
      Console.Write(CStr(index), 13, 10)
   Next
End Sub

will output 0, 1, 2, 4 and 5. This is because when the control variable Index reaches 3, the program immediately begins another iteration of the for…next loop, skipping the call to Write().

With Statement

 with item {, item}
   {statements}
 end with

A with statement is a shorthand for referencing the fields of a record or an imported module. For example,

Structure Date
   Dim dayAs Byte
   Dim monthAs Byte
   Dim yearAs UShort
End Structure

Sub Main()
   Dim myDate As Date
   With myDate
      .day = 23
      .month = 3
      .year = 2007
   End With
End Sub

This is the same as writing

myDate.day = 23
myDate.month = 3
myDate.year = 2007

A with statement can have multiple items. For example,

Sub Main()
   Dim myDate As Date
   With Console, myDate
      .day = 23
      .month = 3
      .year = 2007
      .Write(.day, .month, .year)
   End With
End Sub

Each reference name in a with statement is interpreted as a member of the specified structure or module. If there is another variable or procedure call of the same name that you want to access from the with statement, you need to prepend it with a qualifier. For example,

 
Sub Main()
   Dim myDate As Date
   With Console, myDate
      .day = 23
      .month = 3
      .year = 2007
      .Write(.day, .month, .year)     ' write to usart
       SD.Write(.day, .month, .year)  ' write to SD card
   End With
End Sub