For Loop Over a Range

The current For Each activity is used to loop over a collection of items. This is definitely useful, but the standard for loop in uses a counter and a range and iterates over an integer value. See this documentation: For...Next Statement - Visual Basic | Microsoft Learn

I am requesting that a new activity be added that can take a counter variable, start and end numbers, as well as a step amount.

This would be very helpful for creating an idiomatic means of performing a given bit of code multiple times. The current workarounds are clunky, such as using a while loop with an assign activity to assign a variable being used as the index to itself with the step or attempting to create collections variables with the number of elements to represent the total amount of times that one wishes to iterate.

1 Like

You can use a For Each activity with i as the loop variable and Enumerable.Range(1,20) to loop from i=1 through i = 20. This isn’t capable of stepping more than one but it does iterate over an integer value fairly easily.


Hello Daniel,

I wasn’t aware of that, thank you. That mostly fixes the issue I was having. I was getting frustrated trying to create a collection that had initialized with a certain number of indexes and it wasn’t working.

I think what I initially suggested would still have value, not only for the skip but to have it be more idiomatically shown using the Workflow foundation setup. However, you’re right, for the most part, what you’re suggesting works.

1 Like

@dmccammond @DanielMitchell which TypeArgument need to be set within Properties in order to get dedicated result from your example? Thanks in advance

It’s Integer, so Int32.

You could also figure it out by mousing over the method as it will tell you what the output variable types would be.

What should I do if I want to loop the cycle for a int32 variable times?

For example

var1(int32) = 35

I am writing it like the below. But it doesn’t work. Could you pls comment on that?


Take off the ToArray()

1 Like

This solved it. Thanks.

Hello Daniel,

In my loop, the for each loop or Do While never stops :slight_smile:
For example var1:11

It continues to cycle for 12, 13,14,15 and so on.

How can I make it stop at 11?

I found the problem. The for each loop never stops looping :slight_smile:
I understand why.

I even inserted an if block and put the condition as iteration number is higher than 11, use the “Break” activity.

But it tries to loop but break but still tries to loop. I don’t understand why :slight_smile:

Are you sure you are compating the right values? Can you share a screenshot?
For me it worked as expected:
image image

1 Like

The problem was that my For Each Loop was in a Do While Loop. So For Each Loop was stopping but Do While forcing it to continue.

I created a new loop without in any sequence then I understood.

1 Like

I wouldn’t be able to say right which version introduced it (it was a while ago), but the Repeat Number of Times activity offers this exact functionality:

I’m only digging out this old topic because I noticed it is regularly getting a lot of views so it is fair to share this information.

1 Like