Repetitive

Reference.Repetitive History

Hide minor edits - Show changes to output

Changed line 102 from:
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 be written as,
to:
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,
Changed line 36 from:
Loop Until index> 9 
to:
Loop Until index > 9 
Changed lines 3-4 from:
!![#While]]The While…End While Loop
to:
!![[#While]]The While…End While Loop
Changed line 24 from:
!![#Do]]The Do…Loop Until Loop
to:
!![[#Do]]The Do…Loop Until Loop
Changed lines 121-124 from:
15.6 - Continue StatementType

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,
to:

!![[#Continue]]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,
=firewing [=
Changed line 131 from:
     Console.Write(CStr(Index), 13, 10)
to:
     Console.Write(CStr(index), 13, 10)
Changed lines 134-139 from:

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().

16 - With Statement

with item {, item}
to:
=]
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]]With Statement

 '''with''' item {, item}
Changed lines 141-142 from:
end with
to:
 '''end with'''
Changed lines 144-148 from:

Structure TDate
 
  Dim Day As Byte
  Dim Month As Byte
  Dim Year As UShort
to:
=firewing [=
Structure Date
   Dim dayAs Byte
  Dim monthAs Byte
  Dim yearAs UShort
Deleted line 150:
 
Changed lines 152-156 from:
   Dim Date As TDate
  With Date
      .Day = 23
      .Month = 3
      .Year = 2007
to:
   Dim myDate As Date
  With myDate
      .day = 23
      .month = 3
      .year = 2007
Changed line 159 from:
 
to:
=]
Changed lines 161-165 from:
 
Date.Day = 23
Date.Month = 3
Date.Year = 2007
 
to:
=firewing [=
myDate
.day = 23
myDate.month = 3
myDate.year = 2007
=]
Changed line 167 from:
 
to:
=firewing [=
Changed lines 169-174 from:
   Dim Date As TDate
  With Console, Date
      .Day = 23
      .Month = 3
      .Year = 2007
      .Write(.Day, .Month, .Year)
to:
   Dim myDate As Date
  With Console, myDate
      .day = 23
      .month = 3
      .year = 2007
      .Write(.day, .month, .year)
Changed line 177 from:
 
to:
=]
Changed line 179 from:
 
to:
=firewing [=
Changed lines 181-187 from:
   Dim Date As TDate
  With Console, Date
      .Day = 23
      .Month = 3
      .Year = 2007
      .Write(.Day, .Month, .Year)        ' write to usart
       EE.Write(0, .Day, .Month, .Year)  ' write to eeprom
to:
   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
Added line 190:
=]
Added lines 1-189:
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'''.

!![#While]]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,
=firewing [=
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
=firewing [=
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).

!![#Do]]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,
=firewing [=
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.

!![[#ForNext]]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,
=firewing [=
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,
=firewing [=
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,
=firewing [=
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,
=firewing [=
For Index As Byte = 9 To 0 Step -3
  Console.Write(CStr(Index), 13, 10)
Next
=]
will output 9, 6, 3 and 0.

!![[#ShortCircuit]]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,
=firewing [=
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]]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,
=firewing [=
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 be written as,
=firewing [=
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,
=firewing [=
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
=]
15.6 - Continue StatementType

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().

16 - 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 TDate
  Dim Day As Byte
  Dim Month As Byte
  Dim Year As UShort
End Structure

 
Sub Main()
  Dim Date As TDate
  With Date
      .Day = 23
      .Month = 3
      .Year = 2007
  End With
End Sub
 
This is the same as writing
 
Date.Day = 23
Date.Month = 3
Date.Year = 2007
 
A with statement can have multiple items. For example,
 
Sub Main()
  Dim Date As TDate
  With Console, Date
      .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 Date As TDate
  With Console, Date
      .Day = 23
      .Month = 3
      .Year = 2007
      .Write(.Day, .Month, .Year)        ' write to usart
      EE.Write(0, .Day, .Month, .Year)  ' write to eeprom
  End With
End Sub