Topic: DMD0079

Help File Version: 2.9.4.37

FOR - Index Loop


The Index Loop (FOR) instruction begins a For / Next loop in which the rungs ladder logic instructions between the For instruction and the Next instruction will be executed a predefined number of times. For / Next loops can be nested, and there is no practical limit to how deep the nesting can go.

 

 

This instruction is a 'power-rail' instruction, meaning that it is displayed in the left-most column of the ladder diagram (the power rail). Creating new instances of this instructions is a bit counter-intuitive because they cannot be entered directly on the power rail in the Ladder Editor. The first step is to position the edit cursor in the output column (the far right) of the rung in the ladder logic diagram instead of positioning it against the power rail (the far left). At that point you can enter the instruction's mnemonic or double click on the instruction's name in the Instruction Toolbox. You can also drag / drop the instruction from the Toolbox anywhere on the rung and it will be created in the power rail of that rung.

 

Note: The looping instructions are unconditional so they will run any time the code-block in which they reside is enabled. The preferred method for making them conditional is to place the loop in a Task code-block and use the Enable Task (ENTASK) instruction to run the loop whenever required.

 


Parameters:

Note: Use the F9 key to open the Default Element Selection Tool (the Element Picker or the Element Browser) or use the Down-Arrow key (Auto-Complete) on any parameter field to see a complete list of the memory locations that are valid for that parameter of the instruction.

 

Index is a numeric memory location used by the For / Next loop to count loop iterations. The Index is incremented by the By Step value each iteration of the loop. This value can be any writable numeric location.

 

From is the beginning count for the For / Next loop. This value can be any integer constant, or any readable numeric location.

 

To is the ending count for the For / Next loop. The instruction only counts up, so the To value must be larger than the From Value. The logic within the loop will execute until the Index value is equal to this value. The For / Next loop will end only when the Index value exceeds the To value. This value can be any integer constant, or any readable numeric location.

 

By Step is the value the internal counter will increment by each pass through the loop. This value can be any positive integer constant except 0, or any readable numeric location containing a valid positive value.

 

Enable the Automatically create the NEXT box to automatically create the NEXT instruction required to end the loop.

  • Below this rung will create the NEXT instruction on a new rung following the FOR instruction.
  • At end of code-block will create the NEXT instruction on the last rung of this code-block.

 


Status Display:

 

The status display of the FOR instruction will also show the current .TimeSlice value of the Program or Task.

 


See Also:

BREAK - Exit Loop

 

CONTINUE - Skip to Loop End

 

FOR - Index Loop

 

NEXT - Index By Step

 

REPEAT - Loop Until Condition is Non-Zero

 

UNTIL - Repeat Until Condition is Non-Zero

 

WEND - While End

 

WHILE - Loop While Condition is Non-Zero

 


Rung Examples:

Incrementing Loop:

The following simple example uses V100 as the loop index. It will be initialized to 4000 (From) on the first pass through the loop, and will increment by 1 (By Step) on each successive pass until it gets to 4009 (To).

 

If there were values in D4000-4009, this example will compare each of them to the value in D4010 (which was set to 0 on Rung 1). Each time a value larger than what is in D4010, the value in D4010 will be replaced with this larger value. The end result is the largest value in the range D4000-4009 will be loaded to D4010.

 

 

Decrementing Loop:

This example will compare each value in D4010 to the value in each location in the range of D4009 down through D4000. Each time a larger value than what is current;y in D4010, the value in D4010 will be replaced with this larger value. The end result will be the largest value in the range D4009 - D4000 will be copied to D4010.

 

Because the For loop cannot use a negative step count to process a decrementing loop, the following example uses V100 as the loop variable and V101 as the computed comparison variable.

V100 will be initialized to 0 (From) on the first pass through the loop, and will increment by 1 (By Step) on each successive pass until it gets to 9 (To).

 

V101 will be computed by a MATH instruction that will subtract the current loop count from 4009 which will generate the range of values 4009 down to 4000, which is the range of addresses where the comparison operator will work.