Language Basics
Variables, Conditionals, Comments, For loops, While loops, Structs
This is a collection of basic syntax elements with examples. It includes an introduction to Variables, Conditional Statements, Loops, and Structs.
Variables
Local variables Can be defined using the let
, const
and var
keywords; their identifying rules are the same as contextual variables.
let
declares a local variable (or a parameter) with a lexical block scope. The variable can only be accessed within its definition block and any nested sub-block. This also forbids variable redeclaration within that scope.const
behaves aslet
but will prevent the variable from being reassigned by any side effect operator.var
declares a variable whose scope is the whole script and allows redefinition. This behavior is altered by theJexlFeature#setLexical(true)
that will enforce a lexical scope for all variables akin tolet
declaration.Basic declaration:
let x;
Declaration with assignment:
const theAnswer = 42;
Invalid declaration:
var x.y;
Local variables they take precedence in resolution over contextual variables. When scripts are created with named parameters, those behave as local variables. Local variables can not use ant-style
naming, only one identifier.
To learn more about the different variable types visit Primitive Types.
Conditional Expressions
Conditional statements allow you to make decisions in your code based on specific conditions. The most fundamental form of a conditional statement is the if-else
statement, which enables you to execute different blocks of code depending on whether a condition is true
or false
.
Syntax
Usage
The
if
keyword is followed by a condition enclosed in parentheses.If the condition is
true
, the code block inside the first set of curly braces is executed.If the condition is
false
, the code block inside theelse
set of curly braces is executed.
Examples
Example 1: Checking for Empty Values
In this example, we use the empty
function to check if a value is empty. If the value is empty, we display a message; otherwise, we display a different message.
Example 2: Comparing String Sizes
Here, we use the size
function to determine the length of a string and compare it to a specific size. Depending on the size of the string, different actions are taken.
These examples illustrate how if-else
statements can be used to make decisions in your code based on conditions. You can adapt this concept to handle various scenarios and create more complex logic as needed.
Comments
Just like most modern languages, B2Win Suite Scripting supports single-line and multi-line comments.
Loops
for, while, do/while, continue, break
for loops
Loop through items of an Array, Collection, Map, Iterator or Enumeration, e.g.
The following syntax using a context variable is also supported:
Finally, the conventional syntax using a local variable, initial value, loop condition and loop step is supported.
Example
Note: foreach(item in list)
is unsupported.
while loops
Loop until a condition is satisfied, e.g.
do/while loops
Continue: Within loops (do/while/for), allows to skip to the next iteration.
Break: Allows to break from a loop (do/while/for) unconditionally.
Example: Inventory Replenishment with a While Loop
Structs
Structs are fundamental data structures, providing a convenient way to define custom data types. A struct groups related data fields under a single name, allowing you to create complex data structures tailored to your specific needs. This section will introduce you to defining and using structs.
Defining a Struct
A struct is defined using a syntax similar to the following:
structExample
: The name of the struct."field1"
,"field2"
, and so on: The field names within the struct. They must be in quotation marks.value1
,value2
, and so on: The initial values for each field. You can assign fields empty values as well.
You can define as many fields as needed within a struct, and each field can have a different data type.
Example
Let's create a simple struct to represent a Point in a 2D space:
In this example, we've defined a Point
struct with two fields, x
and y
, both initialized to zero.
Accessing Struct Fields
You can access the fields of a struct using dot notation:
Another way you can access fields of a struct is using the square brackets:
Modifying Struct Fields
You can also modify the values of struct fields:
Examples
We'll create a struct to represent employee information:
Last updated