While Loops in Deluge

Last Updated: 2020-12-18

Introduction

If you are creating a workflow or custom function on a Zoho app, you may have noticed that Zoho's language, Deluge, does not have while loops. However, there is a way around this limitation.

In This Article

You will learn how to create a while loop using a Zoho function. This article will include:


Code

In Deluge, you can mimic the functionality of a while loop by creating a function that creates a list of a variable length. The code for this is:

 list Utility.getListOfLength(int length) 
{
   return if(length < 1,list(),"".leftpad(length - 1).toList(""));
}

The function above will create a list with a variable number of empty elements, specified by the length parameter. If the parameter provided is less than 1, the list will be empty and non-iterable:

 info thisapp.Utility.getListOfLength(5).size(); // 5 
info thisapp.Utility.getListOfLength(0).size(); // 0
info thisapp.Utility.getListOfLength(-5).size(); // 0

Usage

Basic

You can use the code above in conjunction with a for loop to get the functionality of a while loop:

 for each index i in thisapp.Utility.getListOfLength(100) 
{
   // for loop will iterate 100 times (specified by parameter)
   // your code goes here
}

Early Exits

In cases where you don't always need to iterate the specified amount of time, you can add a break; line within an if-block to exit the loop early:

 for each index i in thisapp.Utility.getListOfLength(100) 
{
   if(<BOOLEAN>)
   {
      break;
   }
   else
   {
      // your code goes here
   }
}

Note: with Deluge, a break must be placed inside the if block. If placed in the else block, you will receive the following error message: break statement cannot be used without loop.

Functionless

In applications outside of Zoho Creator, functions can't call other functions. The approach here is to inline the functionality:

 length = 10; 
for each index i in if(length < 1,list(),"".leftpad(length - 1).toList(""))
{
   // your code goes here
}

Closing

Though Zoho deluge doesn't offer native support for while loops, there are ways to make a for loops behave like one. While not usually needed, this is an important tool to know (and one that we often use).


Was this helpful?

Share this article with the people you know: