1 WHILE statement
The statements inside the while are executed as long as they evaluate to true. The value of the expression is checked each time at the beginning of the loop, and execution will not stop until the iteration ends.
It has the following syntax:
<while> <expr> ! <cond> ! </expr> <do> ! <instructions> + <while.exit /> ?f <while.continue /> ? </do> </while>
while (condition) { // code block to be executed }
In the next example, the while condition evaluates a variable which increments units of one by one until four.
<xsql-script> <body> <set name='m_i'>0</set> <while> <expr> <lt><m_i/>4</lt> </expr> <do> <println><string>Process: </string><m_i/></println> <set name='m_i'> <add><m_i/>1</add> </set> </do> </while> </body> </xsql-script>
Process: 0
Process: 1
Process: 2
Process: 3
Warning
If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser.
var i = 0; while (i < 4) { console.log(`Process: ${i}`); i++; }
Process: 0
Process: 1
Process: 2
Process: 3
Warning
If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser.
2 FOR statement
'For' is used to repeat instructions at specified number of times. While in XSQL script the used attributes are start, end and step, in server-side script 3 instructions must be followed:
- Set a variable before the cycle starts
- The condition for the cycle be executed
- Increase or decrease a value every time the code block is executed in the loop
Loops are useful if you want to run the same code over and over again. Often this is the case when work with arrays.
It has the following syntax:
<for name='name' start='start' end='end' end-lt='end-lt' step='step' > <do> ! <action> ! <for.exit /> * </do> </for>
for (statement 1; statement 2; statement 3) { // code block to be executed }
The next example shows the use of the for statment, when a counter is used from zero (0) to four (4), increasted one by one.
<xsql-script name='for_sample'> <body> <println>-------------------------------------</println> <println> Case: start='0' end='4' </println> <println>-------------------------------------</println> <for name='i' start='0' end='4'> <do> <println>Process: <i/></println> </do> </for> <println>-------------------------------------</println> <println> Case: start='0' end='6' step='2' </println> <println>-------------------------------------</println> <for name='i' start='0' end='6' step='2'> <do> <println>Process: <i/></println> </do> </for> <println>-------------------------------------</println> <println> Case: start='0' end-lt='6' step='2' </println> <println>-------------------------------------</println> <for name='i' start='0' end-lt='6' step='2'> <do> <println>Process: <i/></println> </do> </for> </body> </xsql-script>
-------------------------------------
Case: start='0' end='4'
-------------------------------------
Process: 0
Process: 1
Process: 2
Process: 3
Process: 4
-------------------------------------
Case: start='0' end='6' step='2'
-------------------------------------
Process: 0
Process: 2
Process: 4
Process: 6
-------------------------------------
Case: start='0' end-lt='6' step='2'
-------------------------------------
Process: 0
Process: 2
Process: 4
console.log(`\ ----------------------------------- Case: idx = 0; idx <= 4; idx++ -----------------------------------`); for (var idx = 0; idx <= 4; idx++) { console.log(`Process ${idx}`); } console.log(`\ ----------------------------------- Case: idx = 0; idx <= 6; idx+=2 -----------------------------------`); for (var idx = 0; idx <= 6; idx+=2) { console.log(`Process ${idx}`); } console.log(`\ ----------------------------------- Case: idx = 0; idx < 6; idx+=2 -----------------------------------`); for (var idx = 0; idx < 6; idx+=2) { console.log(`Process ${idx}`); }
-----------------------------------
Case: idx = 0; idx <= 4; idx++
-----------------------------------
Process 0
Process 1
Process 2
Process 3
Process 4
-----------------------------------
Case: idx = 0; idx <= 6; idx+=2
-----------------------------------
Process 0
Process 2
Process 4
Process 6
-----------------------------------
Case: idx = 0; idx < 6; idx+=2
-----------------------------------
Process 0
Process 2
Process 4
3 FOREACH statement
The 'FOREACH' loop is used to iterate all the elements of a data structure that could be a an object, an array between others. To follow that commitment in server-side script can use three types of statement :
- foreach...
- for...in
- for...of
The first one, has his equals in XSQL and it has the follow syntax :
<foreach autocommit='true|false'> <union prefix='prefix'> ? <select prefix='prefix'> ? <select> ! </select> </union> <nativesql /> ? <in prefix='prefix'> ? <value> ! </in> <before> ? <instructions> ! </before> <before-group of='of'> * <instructions> ! </before-group> <after> ? <instructions> ! </after> <after-group of='of'> * <instructions> ! </after-group> <do> ! <instructions> ! <foreach.exit /> * <foreach.continue /> * </do> </foreach>
arr.forEach(function callback(currentValue, index, array) { // tu iterador }[, thisArg]);
Warning
Must remenber, that in this type of foreach statement in server-side script doesn't work break and continue.
<xsql-script name='foreach_code1'> <body> <foreach> <select prefix='m_'> <columns>*</columns> <from table='systables' /> <where> tabid IN (1,4,7) </where> </select> <do> <println>TABNAME: <m_tabname />, NRO COLUMNAS: <m_ncols /></println> </do> </foreach> </body> </xsql-script>
TABNAME: systables, NRO COLUMNAS: 26
TABNAME: systabauth, NRO COLUMNAS: 4
TABNAME: sysusers, NRO COLUMNAS: 5
var mSqlCond = `tabid IN (1,4,7)`; var mRsSystable = Ax.db.executeQuery(` <select> <columns> * </columns> <from table='systables' /> <where> ${mSqlCond} </where> </select> `); mRsSystable.forEach(function(mRow){ console.log("TABNAME: " + mRow.tabname + ", NRO COLUMNAS: " + mRow.ncols); });
TABNAME: systables, NRO COLUMNAS: 26
TABNAME: systabauth, NRO COLUMNAS: 4
TABNAME: sysusers, NRO COLUMNAS: 5
However this statement is not often to use that why invite you to implement the follow statement for...in, and to get a better understanding let's do an example:
var mArrCities = ['Barcelona', 'Paris', 'Rome', 'Amsterdam']; /* Iterating over the array of cities */ for (let mIntIndex in mArrCities) console.log(mArrCities[mIntIndex])
Barcelona
Paris
Rome
Amsterdam
The for...in statement distinguish that at the moment to cross the array also iterating object properties. To the previous example lets add a property and see what have to result.
var mArrCities = ['Barcelona', 'Paris', 'Rome', 'Amsterdam']; /* Adding a property to the array */ mArrCities.continent = 'Europe' /* Iterating over the array of cities */ for (let mIntIndex in mArrCities) console.log(mArrCities[mIntIndex])
Barcelona
Paris
Rome
Amsterdam
Europe
When use the statement <<for...in>> and <<for...of>> can add to your code break or continue without a problem.
On the other hand, have the for...of statement on each iteration a value of a different property is assigned to variable.
var mArrCities = ['Barcelona', 'Paris', 'Rome', 'Amsterdam']; /* Iterating over the array of cities */ for (let mStrCity of mArrCities) console.log(mStrCity)
Barcelona
Paris
Rome
Amsterdam
In resume, have seen three type of foreach statements that we can use so it will depend what looking for at the time to choose.
4 Break and Continue
4.1 Break
The break statement exits a switch statement or a loop (for, for ... in, while, do ... while).
When the break statement is used in a loop, it breaks the loop and continues executing the code after the loop (if any).
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<for.exit /> | break | The break statement "jumps out" of a loop |
<xsql-script name='for_sample'> <body> <for name='i' start='0' end='9'> <do> <if> <expr> <eq> <i/> <string>3</string> </eq> </expr> <then> <for.exit /> </then> </if> <println>The number is <i/></println> </do> </for> </body> </xsql-script>
The number is 0
The number is 1
The number is 2
for (i = 0; i < 10; i++) { if (i == 3) { break; } console.log("The number is " + i); }
The number is 0
The number is 1
The number is 2
4.2 Continue
The continue statement breaks one iteration (in the loop) if a specified condition occurs, and continues with the next iteration in the loop.
Operator XSQL | Equivalent server-side script | Description |
---|---|---|
<for.continue /> | continue | The continue statement "jumps over" one iteration in the loop |
This example skips the value 3 of the counter.
<xsql-script> <body> <for name='i' start='0' end='9'> <do> <if> <expr> <eq> <i/> <string>3</string> </eq> </expr> <then> <for.continue /> </then> </if> <println>The number is <i/></println> </do> </for> </body> </xsql-script>
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
for (i = 0; i < 10; i++) { if (i == 3) { continue; } console.log("The number is " + i); }
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
It is possible to use the continue statement with a label reference, to skip a value in a nested for loop.
var mStringText; Loop1: // The first for loop is labeled "Loop1" for (i = 0; i < 3; i++) { mStringText = "i = " + i + ", j = "; Loop2: // The second for loop is labeled "Loop2" for (j = 10; j < 15; j++) { if (j == 12) { continue Loop2; } console.log (mStringText + j); } }
i = 0, j = 10
i = 0, j = 11
i = 0, j = 13
i = 0, j = 14
i = 1, j = 10
i = 1, j = 11
i = 1, j = 13
i = 1, j = 14
i = 2, j = 10
i = 2, j = 11
i = 2, j = 13
i = 2, j = 14