A Guide to Using For Loops in Xano

A Loop in computer programming is a sequence of instructions that is continually repeated until a certain condition is reached.[1]  For Loops are particularly important because they allow developers to iterate through arrays, generate a sequence of numbers or repeat a set of statements. To put it simply, we are performing a set of instructions over and over again for as many iterations as we want.

The goal of this tutorial is to provide you with some For Loop use cases and examples of how to implement this in Xano. 

Introduction

Imagine you have a list of 10 items that you would like to make changes to. Without a for-loop you would need to manually map every single ID of each of the records that you need to make updates to. 

Under each, you would need to repeat the same set of instructions, or if you have placed them in one reusable function, you would need to call the function multiple times in different sections as opposed to just once when using a loop. 

This manual approach will be very clunky because the code cannot scale and you will end up with a longer, possibly slower, function stack. 

Syntax

In traditional development, the syntax of a for loop would contain 3 parts as seen below. 

These sections can be broken down as Initialization, Condition and Iteration.


The Initialization Section refers to the first part of the for loop (int i = 0). This is where our loop control variable is defined. 

The Condition Section refers to the second part of the for loop (i < number.length;). This is where we define the condition that needs to remain true for this loop to continue running. 

The Iteration Section refers to the third part of the for loop (i++)statement that increments the loop control variable so that it can continue running. 

In Xano, we do most of the heavy lifting for you so you would only need 2 sections, as seen on the left side below.

The # Of Loop Iterations coincides with our Condition Section because this is where we define our array’s length. 


The Loop Index Variable coincides with the Iteration Section because it will start at 0 and then increment until the index is equal to the array’s length. 

Common Use Cases and Examples

  1. Iterating through lists

  2. Repeating a set of statements

  3. Processing data elements

  4. Generating a number sequence

Break Statement

At some point, you may have a use case whereby you are looking for a certain value inside an array/list and once found, you would like to stop the loop from iterating any further. This is where you would use the Break statement, which ends the loop. 

Continue Statement

In another scenario, you could be dealing with an array that may have empty values. In order for you to allow your loop to continue running without a possible disturbance, you could add a Continue statement to skip to the next item in the array. 

There we have it! Hopefully this was helpful for you. If there is anything you would like me to cover, let me know in the comments.

Further Reading

2
1 reply