User Tools

Site Tools

한국어

wikiblog:cublocstydy8:index

For...Next

For…Next will loop the commands within itself for a set number of times.

    For Variable = StartingValue To EndingValue [Incremental Step] 
        Commands
        [Exit For]
    Next

In the below example, an Incremental Step is not set. By default each loop increments by 1.

Dim K As Long
For K=0 To 10
   Debug Dp(K),CR
Next
 
For K=10 To 0 Step1 ' Negative Step, step from 10 to 0.
   Debug Dp(K),CR
Next

An Exit For command can be used within the For…Next loop to exit at any time.

For K=0 To 10
  Debug Dp(K),CR
  If K=8 Then Exit For ' If K equals 8 exit the For...Next loop.
Next

When choosing a variable to use for the For…Next loop, please make sure the chosen variable is able to cover the desired range. Byte variables can cover 0 to 255. For larger values, a variable with a larger range must be chosen.

Dim K As Byte
For K=0 To 255
  Debug Dp(K),CR
Next

When using a negative Step, please choose a Long variable type if the loop will go below 0.

Dim LK As Long
For LK=255 To 0 Step1 ' This will reach -1 as last step
  Debug Dp(LK),CR
Next

Demo program

Const Device = CB280
Dim A As Integer
For A=1 To 9
  Debug "3 * "
  Debug Dec A
  Debug " = "
  Debug Dec 3*A,Cr
Next

Const Device = CB280
Dim A As Integer, B As Integer
For A=2 To 9
  For B=1 To 9
    Debug Dec A," * "
    Debug Dec B
    Debug " = "
    Debug Dec A*B,Cr
  Next
  Debug Cr
Next

Charles's Blog

wikiblog/cublocstydy8/index.txt · Last modified: 2022/06/23 06:01 by COMFILE Technology