ooPIC Logo

For Loop Construct

Main Index
Language Idx
Back to top of pageDescription:
 A For Loop Construct executes a group of statements a specified number of times. An optional keyword can be used to prematurely exit the loop before the specified number of times.
Back to top of pageSyntax:
 

The following shows the format of the For Loop Construct syntax.
Visual Basic & BASICC & Java
For <counter> = <start> to <end> [Step <step>]
  <statements>
  [Exit For]
  [Continue]
  <statements>
Next <counter>
For (<init>; <test>; <inc>){
  <statements>
  [break;]
  [continue;]
  <statements>
}

The following table lists the elements of the For Loop Construct syntax.
ElementDescription
BasicC & Java
ForforFor Loop Construct beginning keyword.
<counter> Numeric variable or Object used as a loop counter.
<start> Initial value of <counter>.
<end> Final value of <counter>.
<statements> One or more statements executed the specified number of times.
<step> The step value that is added to <counter> upon finishing the execution of <statements>. If not specified, the step value defaults to 1.
 <init>Loop Initialization statement.
 <test>Loop Test expression.
 <inc>Loop Incrementing statement.
Exit ForbreakLoop Termination keyword(s). Can occur any number of times within the <statements>
ContinuecontinueLoop Continuation keyword. Can occur any number of times within the <statements>
Next While Loop Construct ending keyword.

Back to top of pageOperation:
 Basic Syntax:
When a For Loop Construct is encountered during the program's execution, the value of the identifier specified by the <counter> element is set to the value specified by the <start> element. A comparison is then made between identifier's value and the value specified by the <end> element.

If the identifier's value has not exceeded the <end> value, then the statements within the <statements> element are executed, after which, the value specified by the <step> element is added to the identifier's value. The program flow then returns to the comparison at the beginning of the For Loop Construct and the process repeats.

If the identifier's value has exceeded the <end> value, or the optional Exit For keywords are encountered anywhere within the <statements>, then the program's flow is transferred to the statement following the For Loop Construct at which point the program's execution resumes.

C and Java Syntax:
When a For Loop Construct is encountered during the program's execution, the statement specified by the <init> element is executed. This statement should be an assignment of a beginning value to an identifier. A comparison is then made by executing the expression specified by the <test> element. This expression should be an evaluation of whether or not the value of the identifier has exceeded an ending value.

If the result of the comparison is false, then the statements within the <statements> element are executed, after which, the statement specified by the <inc> element is executed. The program flow then returns to the comparison at the beginning of the For Loop Construct and the process repeats.

If the result of the comparison is true, or the optional break keyword is encountered anywhere within the <statements>, then the program's flow is transferred to the statement following the For Loop Construct at which point the program's execution resumes.

If the optional continue keyword is encountered anywhere within the <statements>, then statement specified by the <inc> element is executed and the program flow then returns to the comparison at the beginning of the For Loop Construct and the process repeats.

Back to top of pageRemarks:
 

The value of the identifier, used as a loop counter, is not returned to its previous value if the evaluation fails, which leaves the value of the identifier at 1 increment past the ending value when the For Loop Construct ends. See the Caution at the end of this page:

NOTE:
The ooPIC use of Basic, C and Java syntax does not include all of the nuances for all of these languages. Only the most commonly used modes. If you try to "stump the chump" you will find some for/next constucts that don't work. Don't try to be clever, just write code that you can understand.

Back to top of pageExample:
 

In the following example, a For Loop Construct is shown that executes "BlinkLED" the number of time specified by the value of A.
Visual Basic & BASICC & Java
For I = 1 To A
  Call BlinkLED
Next I
For (I = 1; I < A; I++){
  Call BlinkLED;
}

In the following example, a For Loop Construct is shown that sets the value of the first 12 elements within an array named leg, to 64.
Visual Basic & BASICC & Java
For I= 1 To 12
  Leg(I).Value = 64
Next I
For (I = 1; I < 12; I++){
  Leg[I].Value = 64;
}

In the following example, a For Loop Construct is shown that sets the value of the even elements within the first 12 elements of an array named leg, to 64.
Visual Basic & BASICC & Java
For I = 2 To 12 Step 2
  Leg(I).Value = 64
Next I
For (I=2; I<12; I=I+2){
  Leg[I].Value = 64;
}

In the following example, a For Loop Construct is shown that executes the procedure "DoStuff" 10 times, unless the value of A gets set to 1, at which time the loop is exited.
Visual Basic & BASICC & Java
For J = 1 To 10
  Call BlinkLED
  If A = 1 Then Exit For
Next J
For (J = 1; J < 10; J++){
  Call BlinkLED;
  If (A==1) Break;
}

In the following example, 3 For Loop Construct are shown that are nested by placing one For Loop Construct within another. Given that each loop loops 10 times, the statement in <statements> will execute 1000 times.
Visual Basic & BASICC & Java
For I = 1 To 10
  For J = 1 To 10
    For K = 1 To 10
      '<statements>
    Next K
  Next J
Next I
For (I=1; I<10; I++){
  For (J=1; J<10; J++){
    For (K=1; K<10; K++){
      <statements>
    }
  }
}

In the following example, a For Loop Construct is shown that executes procedure "BlinkLED" indefinably.
NOTE: The Do Loop Construct is better suited for the conditions in this example.
Visual Basic & BASICC & Java
For I = 0 To 1 Step 0
  Call BlinkLED
Next I
For (;1;){
  Call BlinkLED;
}

Back to top of pageCaution:
 A For Loop Construct that loops the entire span of a variable will never end.

I.E.... Looping from 0 to 255 using a oByte Object:
When the oByte Object is at its highest possible value of 255 and the Next keyword is encountered, the loop will first increase the value of the oByte Object by the <step> value, Since the oByte Object can only hold 8-bits, and a value over 255 takes more than 8-bits, the high-order bits will be lost, which results in the value of the oByte Object rolling over to a value of less than 255. After the increment has been done, the For Loop Construct continues on normally, and the value of the oByte Object is then tested to see if it has passed the <end> value (which is set to 255). Since the value of the oByte Object is now less than 255, the For Loop Construct continues looping, unaware that the value of the oByte Object value has rolled over.

In the following Code, the value of B will never be set to 1.

Dim A As New oByte
Dim B As New oBit

Sub Main()
  For A.Value = 0 To 255
  Next A.Value
  B.Value = 1
End Sub

Back to top of pageRelated Items:

 Do Loop Construct, While Loop Construct
Back to top of pageVersion History and Bug List:
 Firmware Ver A1: Introduced.

Bugs: No known bugs.


ooPIC Compiler Ver 6.0 (c) Copyright 1997 - 2007 Savage Innovations, LLC.