Navigation

Flow Control in DI#

In DI#, flow control allow you to direct code execution based on conditions or to repeat instructions on sets of data. This article presents the main constructs such as if, else, while, and foreach, along with their syntax and examples of use in DI# scripts. Mastering these structures is essential for writing dynamic and conditional DI# scripts that adapt to the data and message context.


if

The if instruction is one of the most important keywords in any programming language. It allows the conditional execution of a code block. In DI#, if works the same way as in most languages.

if (expression)  
command ;

OR

if (expression)  
{
Command 1 ;
Command 2 ;
}

expression must return a boolean value. If it evaluates to TRUE the associated code block will be executed, if it evaluates to FALSE the commands will be ignored. 

/* In the example below, the sentence "a is larger than b" will be shown only if a is larger than b
*/
[[
int a = 1;
int b = 2;
if (a > b)
output.write("a is larger than b");
]]

You can insert if statements inside other if statements, allowing for great flexibility when it comes to selecting code execution based on a large number of parameters. 


else

You will often need to execute some commands if a particular condition is met and some other commands if it isn't. The else keyword is used after an if and provides a code block to be executed when the expression evaluates to FALSE. 

/* In the example below, the sentence "a is larger than b" will be shown only if a is larger than b, and the sentence "a is smaller or equal to b" if it isn't.
*/
[[
int a = 1;
int b = 2;
if (a > b) {
output.write("a is larger than b");
} else {
output.write("a is smaller or equal to b");
}
]]

if/else shorthand

Like in C#, it is possible to abbreviate an if-else statement by using the ?: operator. 

[[  
int a = 1;
int b = 2;
output.write((a > b) ? "a is larger than b" : "no it's not");
]]

Note: Due to limitations in the DI# compiler, make sure that you place the condition between parentheses when using this operator. 


while

The while instruction is the simplest way to implement a code loop in DI#. This command behaves the same way as in C. 

// Here is the most simple example of a while loop:
while (expression)
commande ;

OU

while (expression)
{
Commandes 1 ;
Commandes 2 ;
}
// The following example displays the numbers 1 through 10
[[
int a = 1;
while (a <= 10)
{
output.write(a);
a+=1;
}
]]

Note: Pay particular attention to the management of the variable on which the condition is based to avoid creating endless loops. As a safety measure, DI# while statements are limited to 250 iterations. 


foreach

→ Array Iterator

The foreach statement provides a simple way of going through elements of an array. 

foreach (identifier in expression) 
command ;

OU

foreach (identifier in expression) 
{
command 1 ;
command 2 ;
}

identifier is the chosen name for the variable holding the value of each individual item contained in expression as we go through the loops. This variable will be of the same data type as the values in the array.  

/* The example below shows how to display all the elements of a simple array of integer numbers:
*/  
[[
int myArray[] = { 1, 2, 3, 4, 5};
foreach(number in myArray)
{
output.write(number);
}
]]


→ Using a complex expression

Since a foreach allows iterating through any array resulting from the evaluation of an expression, it's possible to use complex expressions directly in the foreach statement. 

[[  
datasource myArray =
{
{ FirstName : "Jean", Name : "Tremblay", Age : 40 },
{ FirstName : "John", Name : "Smith", Age : 30 }
};
foreach(Namevalue in (select item.LastName
from item in myArray
order by (item.Age)))
{
output.write("Last Name :" + Namevalue);
}
]]

→ Iterating with a Counter

As it's the case in several programming languages, it is possible to maintain a counter while going through the foreach loop:
foreach (counter => identifier in expression)
command ;

counter is the name of a variable that will contain an integer number starting at 0 that will be incremented by 1 each time a new identifier is fetched from expression

[[  
int myArray[] = { 1, 2, 3, 4, 5};
foreach(itemOrder => number in myArray)
{
output.write("position : " + itemOrder);
output.write("value : " + number);
}
]]

Note: Like the while statement, a foreach loop is limited to 250 iterations 


break

/* The break command is used to terminate a foreach or while loop before all its elements
   have been processed. The example below stops the execution of the foreach after the
   number 3 has been processed.
*/
  [[
  int myArray[] = { 1, 2, 3, 4, 5};  
  foreach(number in myArray)  
    {    
            	output.write(number); 
                if (number == 3)
 		    break;
    }
  ]] 



switch

The switch statement is the equivalent of a series of if instructions based on the same expression. In some situations, you will need to test the value of a variable and provide several code blocks to be executed according to the possible values of that variable. The switch statement is built specifically for that purpose. 

Switch (expression) { case value : command; }

OR

Switch (expression)  
{
case value :
{
command 1;
command 2;
}
}
/* Both examples below are ways of achieving the same effect, one with a series of if
 statements, the other with a switch.
*/  
[[
int i = 0;
if (i == 0)
output.write("i égal 0");
if (i == 1)
output.write("i égal 1");
if (i == 2)
output.write("i égal 2");
switch (i)
{
case 0:
output.write("i égal 0");
case 1:
output.write("i égal 1");
case 2:
{
output.write("i égal 2");
output.write("i égal 2");
}
}
]]


Contrary to other programming languages, it is not necessary in DI# to use the break instruction to end the various case blocks inside your switch. Once a case is reached, the commands it contains will be executed and the compiler will exit the switch statement. There is also no default statement in the DI# version of switch, if no case match the branching expression, no code will be executed.  


continue

The continue command is used inside a foreach or while, and forces that loop to begin processing the next item immediately without executing any of the remaining commands for the current iteration. 

/* The example below will display every number contained in the array except for 3, since the continue keyword will trigger the beginning of the next loop immediately, skipping the output.write command. 
*/
[[  
Datasource myArray[] = { 1, 2, 3, 4, 5};
foreach(number in myArray)
{
if (number == 3)
continue;
output.write(number);
}
]]

return

The return command forces the current module to exit and hands back control to the part of the program that had called it, resuming code execution at the next line of the calling module.  

  • If return is called from inside a function it terminates that function immediately. 
  • If return is called from the main program, code execution is terminated completely.   

Return accepts a single optional parameter that, if present, becomes the return value of the function being terminated. 

/* In the example below, return stops the execution of checkNumber and returns a value to the main program that gets stored in the result variable.
*/
            [[ 
            	string checkNumber(int x) 
                {
                	if (x == 0) 
                    	   return "x égal 0";
                   	if (x == 1) 
                    	   return "x égal 1"; 
                        if (x == 2) 
                    	   return "x égal 2"; 
                } 
                string result = checkNumber(1); 
                output.write(result); 
                // Returns "x equal 1" 
                ]]

Note : if the return command is used at the root from the script, it will stop the execution of the script and the returned value by return can replace what the script should have returned.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.