Loop for with variable

When programming software, we can use the command for($i = $initial; $i<= $end; $i++).
I would like to know how I can make a loop in knime defining the beginning and end of the loop through variables previously received, in a table for example.

Hi @JeymssonOpovo and welcome to the Knime Community.

Just like you have different loops in programming languages, Knime also offers different loops.

In any programming language, a for loop is usually used when we know how many iteration we want the loop to run for. Therefore, the equivalent of this in Knime would be the Counting Loop Start:

The number of iterations can be set as a hardcoded value, or can be set dynamically via a flow variable (like any other node)

And you would close the loop with a Loop End:

EDIT: Just wanted to add: Of course, I’ve only answered your question about what loop to use as an equivalent to a for loop.

However, if you can expand a bit on what you are trying to do, there might be alternatives. For example, if you want to loop as many times as the number of records in a table, you can use the Chunk Loop Start, or if you are loop via a list of files in a folder, you can use the Table Row To Variable Loop Start.

Choosing the right loop will depend on your use case. It could also be a case where you do not need to use a loop - there’s a bit of confusion among beginners that they need to manually process 1 row at a time, and they implement a loop for that, but in Knime you do not need to do this. Whatever manipulation or operation to apply to a row of a table will be applied to all the rows automatically by Knime.

4 Likes

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