How to increament a count using a for loop

Hey Guys

How to increment a count using a for loop? I basically want to increment the count until it reaches not sure how to do this simple action in uipath.

@bobby

Declare an integer variable
Use an assign activity to increment the value inside the for loop.

Variable = variable+1

1 Like

Where do i get the for loop from. There is only a for each loop. In this case, I am not using an array. So how do I do that. Or how do I set an array to size of 10?

If you just want to loop up until a condition - use a Do While or While loop

For a Do While - Your condition will be intCounter < 10
and increment your counter at the end of your sequence.

1 Like

@bobby,

You can declare an array and in the variables panel you can initialize it default/known size of the array.

 New String(10) {}
1 Like

@bobby
In case of you are using a for each activity:

A bultin feature is doing this for you:

Output

  • Index - A zero-based index that specifies which element of the current collection is being iterated, stored in an Int32 variable.

But maybe you are in process of a different scenario. So just tell us more about it

1 Like

Hi @bobby

you can also have a look at the Snippet section, there are also some cool stuff to look at. :slight_smile:

1 Like

I tried that and it did not work. The count does not increament. I basically added a do while activity. Outside created an assign with count =0. Inside the do while activity. I placed the activity I want it to process and then after increament the count by doing count = count +1. In the condition I did do this until count = 10. I printed the count and it appears blank

Ah that’s cool. I did not know that was there. I will try that. Thanks. :slight_smile:

so your sequence should look like this:

Assign Counter (int) = 0
Do While Counter < 10
   Log Message = Counter.Tostring
   Assign Counter = Counter + 1
1 Like

Hey TimK, that works. The thing I did wrong was made the count =10, not less than 10. Why is equal to 10 not right? Sorry, I am confused.

1 Like

both are correct it depends on what you want the loop to do :slight_smile:

the operator < is required to make the condition true once it reaches 10, = will not work because you are looping only while counter is = 10 (which it doesnt so will exit)

So the first time you loop - your counter will = 1 as you have incremented it so the condition is false and breaks the loop.

Hope thats helps :slight_smile:

1 Like

yes it does. Thank you.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.