Loops
Loops are used for repeating execution of a common set of code, iterating through an array etc. Loops are of multiple types viz For loop and While loop.
How to use Loop Nodes?
- The loop will span from its start node to its
loop end
node. - Examples of loop start nodes:
for start
andwhile start
nodes. - The
loop end
node can be used to mark the end of loops started byfor start
as well aswhile start
node. - Each loop start node should have its loop end node.
- A loop always runs sequentially.
- Loop can have branches (which can run parallel to and independent of it).
- Merges aren't allowed in the loop.
- Merges are only allowed in a loop when used for sequentialising
if node
's output flows at single point.
While using an if node
in a loop node, the if node
's all output flows, from each branch, should be sequentialised by ending the flows at 1 single point within the loop. They should be sequentialised at or before the loop end
node of the loop. Check example here.
:::
For Loop
To create a for loop:
- Take a
for start
node. - Open its properties
- Set the
initial value
ofi
. Eg.0
- Set the
condition
to run the loop till. Eg.i < 10
will run the loop 10 times when i = 0 uptill i = 9. - Set an
incrementor/decrementor
eg.i + 1
which will increment i at the end of each loop iteration. (NOTE: We are yet to supporti++
/i--
) - Add the next flow after the
for start
node which should run inside the loop. - Take a loop end node and place it at the end of the loop.
- Make sure there is a connected chain of nodes (1 path) from the
for start
node andloop end
node. - You can make use of
i
anywhere inside the for loop and its branches.
Optionally, i
can be renamed in the outputs section.
:::
While Loop
Just like you created a for loop, you can use a while loop.
The while loop
node only requires a condition for which its loop will keep running eg. count < 0
.
In case of a while loop, the iteration variable needs to be initialised and incremented/decremented manually in separate nodes as shown in the diagram.
Infinite Loop
Create an infinitely running loop by using a while start
node and keeping its condition as 1
.
Loop Nodes
Along with the loop start and end nodes, Smow has other nodes to help you.
1. Break Node
Use a break node
to stop a running loop's (immediate loop) execution from any part of the loop. Once loop is stopped, the next code after its loop end
node, will begin to run, if any.
2. Continue Node
Use a continue node
to skip a running loop's 1 iteration from anywhere in the loop.
Nested loops
Above is an example of a nested loop inside another loop.
Nested loops help iterating a new dimension inside the parent loop.
If node inside a loop
When inside a loop, the if node
's all output flows, from each branch, should be sequentialised by ending the flows at 1 single point within the loop as shown above.
Create and Share Projects/Libraries
Create new libraries and projects using Smowcode and share it on the project central and with you friends on Linkedin/FB/Insta. Get a certificate by emailing your libraries at hello@smowcode.com.