<< Click to Display Table of Contents >> Expression elements syntax |
In scripting expressions, you may need to use different elements depending on what the expression is intended to do. For example, you may need to evaluate different scenarios through conditional expressions, or to iterate over an object to perform a search, or to evaluate each of the its elements. This article explains the syntax to some of these expressions that may be useful when coding in Bizagi.
Expressions can be divided into different groups. For example:
•Conditional expressions
•Loop expressions
Conditional expressions
Expressions used to make a decision depending on the value of one or more variables.
If expression
Used to evaluate one condition. A condition is an expression that must return a Boolean. For example, a comparison between two values.
if (Condition)
{
Instruction block when the statement is true
}
Example:
if (<Employee.extraHours> > 0)
{
<Employee.monthlySalary> = <Employee.salary> + <Employee.extraHours> * <extraHoursFee>
}
If-Else expression
Used to evaluate one condition among several paths. If none of the conditions are met, the expression goes through the else path.
if (Condition)
{
Instruction block when the statement is true
}
Else if (Condition2)
{
Instruction block when the statement is true
}
Else
{
Instruction block when none of the statements above was true
}
Example:
if (<Employee.salesGoal> > 1)
{
<Employee.monthlySalary> = <Employee.salary> + <Employee.bonusForGoals> * 1.5
}
Else if (<Employee.salesGoal> > 0.5)
{
<Employee.monthlySalary> = <Employee.salary> + <Employee.bonusForGoals>
}
Else
{
<Employee.monthlySalary> = <Employee.salary>
}
Switch expression
used to perform different actions depending on a variable defined (<Candidate.yearsOfExperience>), or the result of an expression(<Employees>.length). The expression in the Switch is evaluated once and then, each block is executed if the case is met. If none of the conditions are met, the default path is executed.
switch (Expression)
{
case case1:
Instruction block when the case is met
case case2:
Instruction block when the case is met
case case3:
Instruction block when the case is met
default:
Instruction block when none of the cases above was matched
}
Example:
switch (<Employee.role>)
{
case "Director":
<Employee.promotionTime> = "5 years"
case "Manager":
<Employee.promotionTime> = "3 years"
case "Analyst":
<Employee.promotionTime> = "2 years"
default:
<Employee.promotionTime> = "1 year"
}
If you want to break from the switch when one of the conditions is met (so that it does not go to the other blocks), you must use the break statement.
switch (Expression)
{
case case1:
Instruction block when the case is true
break;
case case2:
Instruction block when the case is true
break;
case case3:
Instruction block when the case is true
break;
default:
Instruction block when none of the cases above was matched
}
Example:
switch (<Employee.role>)
{
case "Director":
<Employee.promotionTime> = "5 years"
break
case "Manager":
<Employee.promotionTime> = "3 years"
break
case "Analyst":
<Employee.promotionTime> = "2 years"
break
default:
<Employee.promotionTime> = "1 year"
}
Loop expressions
Expressions used to iterate over an object with several elements (array, arraylist, collection, etc.). These can be used to search for a particular value, or to perform an action over each of the elements of the object.
For expression
Executes a cycle a certain number of times. It is necessary to define a starting point for the object to iterate (for example, if you want to start at the beginning, the starting point is 0), the condition for the cycle to run (for example, if you want to go over the whole object, or just over a part of it), and how the variable defined in the starting point increases.
for (var i = starting point; i < number of desired iterations; i++)
{
Instruction block executed while the running condition is met.
}
Example:
var finalGrade = 0
for (var i = 0; i < grades.size(); i++)
{
finalGrade = finalGrade + grades.get(i)
}
finalGrade = finalGrade / grades.size()
Keep in mind that the way to iterate over an object depends on what type of object it is. Refer to Iterating using expressions. |
While expression
Executes a cycle as long as a condition is met. A condition is an expression that must return a Boolean.
while (Condition)
{
Instruction block executed while the condition is met.
}
Example:
var tasksCompleted = 0
var i = 0
while (tasksCompleted < tasks.size())
{
var currentTask = task.get(i)
curretTask.setXPath("isCompleted", true)
tasksCompleted = tasksCompleted + 1
i = i+1
}
Do expression
Variant to the while expression. This one executes a coding block at least once, and then, it is executed as long as a condition is met.
do
{
Instruction block executed at least once, and then while the condition is met.
}
while (condition);
Example:
var tasksCompleted = 0
var i = 0
do
{
var currentTask = task.get(i)
curretTask.setXPath("isCompleted", true)
tasksCompleted = tasksCompleted + 1
i = i+1
}
while (tasksCompleted < tasks.size());
Breaking from a loop
There are cases when you might need to get out from a loop before the specific condition is met, or before the number of iterations is reached. To do this, you need to include in your code a break statement.
while (Condition)
{
Instruction block executed while the condition is met.
if (condition2)
{
Instruction block executed if the inner condition is met.
break;
}
}
Example:
var totalEvaluators = 0
var evaluators = Me.getXPath("Evaluation.evaluators")
var i = 0
while (totalEvaluators < evaluators.size())
{
var currentEvaluator = evaluators.get(i)
currentEvaluator.setXPath("selectedToEvaluate", true)
totalEvaluators = totalEvaluators + 1
i = i+1
if (totalEvaluators == <Evaluation.minimumEvaluators>)
{
break;
}
}
In the code above, if the internal condition (the one associated to the if expression) is met, the break statement will be executed and the loop will be automatically ended.
Last Updated 12/12/2024 12:06:08 PM