Statement
The control statements of MQL: if, case, switch, for, while and repeat loops with break and continue, try and exception for errors, eval for expressions, plus parallel and synchronized blocks for concurrent work. They are the building blocks of every script.
evalparallelifif forcecaseswitchmodeforwhilerepeatsynchronizedbreakcontinueexceptiontry

eval <mql>


Description

    Evaluate an MQL expression

Parameters

    mql:   The MQL source code - string - required
admin
eval "log trace OK!;";
mentdb
1
Used in use cases (22 recipes)

    MQL developer › Catalogue of math and physics constants with eval
    Data validation developer › Validate an API payload against its contract
    Script developer › Patch a script with its generated update request
    Execution modes › sub_include: build include requests with current values
    Configuration & connections › Generate the code that deploys your parameters to PROD
    Versioning & deployment › Rollback: restore the previous version of a script
    and 16 more in the Use cases
Often used with

    script delete  ·  execute  ·  string replace  ·  atom get  ·  atom size  ·  string regexp_extract  ·  script create  ·  type is_double

parallel <mql1> <mqlN>


Description

    Execute several commands at the same time

Parameters

    mql1:   The first MQL command - String - required
    mqlN:   The Nth MQL command - String - required
admin
parallel { log trace "1"; } { log trace "2"; } { log trace "3"; };
mentdb
[ { "result": "1", "status": "OK" }, { "result": "1", "status": "OK" }, { "result": "1", "status": "OK" } ]
Used in use cases (6 recipes)

    Backend & web service developer › execute, call and include: which environment?
    Execution modes › JSON results computed in parallel
    Control flow › Ask three suppliers at once with parallel
    The MQL language in depth › Loops: continue, break, retry with backoff and parallel
    Execution modes › Lost updates and synchronized
    Control flow › Reserve stock without overselling: synchronized
Often used with

    wait  ·  execute  ·  exception  ·  script create  ·  script delete  ·  string regexp_extract  ·  synchronized  ·  type is_enum

if <condition> <trueAction> <falseAction>


Description

    IF statement

Parameters

    condition:   The condition - string - required
    trueAction:   The true action - boolean - required
    falseAction:   The false action - boolean - not required
admin
if (true) { log trace OK! } { log trace KO! };
mentdb
1
admin
if (true) { log trace hello! };
mentdb
1
Used in use cases (353 recipes)

    MQL beginner › Shopping cart total: variables and arithmetic
    MQL developer › Catalogue of math and physics constants with eval
    Backend & web service developer › Publish a typed web service with its parameter and stack options
    Date & time developer › Run log header and execution timer
    Data validation developer › Supplier onboarding validation report
    JSON & document store developer › Invoice numbers from a sequence
    and 347 more in the Use cases
Often used with

    json doc  ·  atom get  ·  script create  ·  script delete  ·  execute  ·  string regexp_extract  ·  file delete  ·  and

if force <condition> <trueAction> <falseAction>


Description

    IF statement (force true and false execution)

Parameters

    condition:   The condition - string - required
    trueAction:   The true action - boolean - required
    falseAction:   The false action - boolean - required
admin
if force (true) { log trace OK! } { log trace KO! };
mentdb
1
Used in use cases (1 recipe)

    Control flow › Quote a B2B order: if, else-if chain and if force
Often used with

    bd*  ·  bd+  ·  bd-  ·  bd/  ·  is empty

case <condition1> <action1> <conditionN> <actionN> <elseAction>


Description

    CASE statement

Parameters

    condition1:   The condition 1 - boolean - required
    action1:   The action 1 - string - required
    conditionN:   The condition N - boolean - required
    actionN:   The action N - string - required
    elseAction:   The else action - string - not required
admin
case (true) {A} (false) {B} {C} ;
mentdb
A
Used in use cases (20 recipes)

    MQL beginner › Grade a class: for loop, list and case
    MQL developer › Catalogue of math and physics constants with eval
    Date & time developer › GDPR retention: keep, anonymise or delete customers
    JSON & document store developer › Validate a payload against field rules and answer 422
    Script developer › A script with typed parameters rejects bad calls
    Web service developer › DELETE: idempotent delete with an audit trail
    and 14 more in the Use cases
Often used with

    script create  ·  script delete  ·  atom get  ·  atom size  ·  execute  ·  string regexp_extract  ·  type is_enum  ·  and

switch <valueToSearch> <condition1> <action1> <conditionN> <actionN> <elseAction>


Description

    SWITCH statement

Parameters

    valueToSearch:   The value to search - boolean - required
    condition1:   The condition 1 - boolean - required
    action1:   The action 1 - string - required
    conditionN:   The condition N - boolean - required
    actionN:   The action N - string - required
    elseAction:   The else action - string - not required
admin
switch (A) (A) {A} (B) {B} {C} ;
mentdb
A
Used in use cases (12 recipes)

    MQL beginner › Your first web service, called with execute
    MQL developer › Run user formulas safely: eval, try and error codes
    Data validation developer › Cross-field rules of a hotel booking
    Script developer › Optional parameters and default values
    Execution modes › Errors propagate through nested executes
    Web service developer › PUT: update a status along a workflow
    and 6 more in the Use cases
Often used with

    atom get  ·  script create  ·  script delete  ·  execute  ·  string regexp_extract  ·  try  ·  atom size  ·  exception

mode <paramMode> <condition1> <action1> <conditionN> <actionN> <elseAction>


Description

    SWITCH statement

Parameters

    paramMode:   The parameter mode - string - required
    condition1:   The condition 1 - boolean - required
    action1:   The action 1 - string - required
    conditionN:   The condition N - boolean - required
    actionN:   The action N - string - required
    elseAction:   The else action - string - not required
admin
mode SERVER_MODE (PROD) {A} (DEV) {B} {C} ;
mentdb
A
Used in use cases (2 recipes)

    Backend & web service developer › Read server parameters and trace a run in the logs
    Control flow › Same code for DEV, UAT and PROD with mode
Often used with

    parameter exist  ·  parameter get value  ·  date sysdate  ·  log archive path  ·  log archive size  ·  log current id  ·  log retention day  ·  log search

for <init> <condition> <increment> <action>


Description

    FOR statement

Parameters

    init:   The initialization MQL source code - string - required
    condition:   The condition - boolean - required
    increment:   The increment - string - required
    action:   The action - string - required
admin
for (-> "[i]" 0) (< [i] 45) (++ "[i]") { log trace [i]; };
mentdb
Used in use cases (285 recipes)

    MQL beginner › Grade a class: for loop, list and case
    MQL developer › Catalogue of math and physics constants with eval
    Backend & web service developer › Inspect a script and answer with a JSON object
    Date & time developer › Run log header and execution timer
    String & encoding developer › SQL table and API names from form labels
    Data validation developer › Supplier onboarding validation report
    and 279 more in the Use cases
Often used with

    atom get  ·  json doc  ·  json iarray  ·  atom size  ·  script delete  ·  script create  ·  string regexp_replace  ·  string regexp_extract

while <condition> <action>


Description

    WHILE statement

Parameters

    condition:   The condition - boolean - required
    action:   The action - string - required
admin
-> "[i]" 0; while (< [i] 45) { log trace [i]; ++ "[i]"; };
mentdb
Used in use cases (44 recipes)

    MQL beginner › Savings plan: while, repeat, break and continue
    Date & time developer › French public holidays and a delivery date in working days
    JSON & document store developer › Merge the pages of a paginated API without duplicates
    Execution modes › Stack run postponed until a gate opens
    Async developer › Push a script to the stack and watch it close
    Scheduler developer › Prove a job ran with the log table
    and 38 more in the Use cases
Often used with

    and  ·  wait  ·  script create  ·  script delete  ·  date now  ·  sequence add  ·  sequence remove  ·  sequence get current

repeat <condition> <action>


Description

    REPEAT statement

Parameters

    condition:   The condition - boolean - required
    action:   The action - string - required
admin
-> "[i]" 0; repeat (< [i] 45) { log trace [i]; ++ "[i]"; };
mentdb
Used in use cases (5 recipes)

    MQL beginner › Savings plan: while, repeat, break and continue
    Algorithms in MQL › Bubble sort and insertion sort against json sort
    Control flow › Drain a print queue with repeat (do-while)
    The MQL language in depth › Loops: continue, break, retry with backoff and parallel
    Algorithms in MQL › Euclid's GCD, fractions and exact decimals
Often used with

    while  ·  break  ·  continue  ·  not  ·  and  ·  arith gcd  ·  arith irem  ·  arith lcm

synchronized <key> <action>


Description

    Synchronized MQL block

Parameters

    key:   The synchronized key - string - required
    action:   The action - string - required
admin
synchronized ("KEY") { log trace "OK"; }
mentdb
Used in use cases (4 recipes)

    MQL developer › Run user formulas safely: eval, try and error codes
    Execution modes › Lost updates and synchronized
    Event developer › Refuse a file received twice under two names
    Control flow › Reserve stock without overselling: synchronized
Often used with

    wait  ·  eval  ·  exception  ·  node create  ·  node delete  ·  node iobject  ·  node select  ·  parallel

break


Description

    Break a FOR/WHILE/REPEAT/SQL_PARSE/CSV_PARSE/HTML_PARSE/JSON_PARSE_OBJ/JSON_PARSE_ARRAY statement

admin
break
mentdb
BREAK
Used in use cases (5 recipes)

    MQL beginner › Savings plan: while, repeat, break and continue
    Web service developer › Search with sort and pagination
    Algorithms in MQL › Binary search against a linear scan
    Control flow › First free meeting slot: nested loops, break and continue
    The MQL language in depth › Loops: continue, break, retry with backoff and parallel
Often used with

    continue  ·  atom get  ·  atom size  ·  while  ·  and  ·  date is_weekend  ·  json iarray  ·  repeat

continue


Description

    Stop the current iteration and continue the loop: FOR/WHILE/REPEAT/SQL_PARSE/CSV_PARSE/HTML_PARSE/JSON_PARSE_OBJ/JSON_PARSE_ARRAY statement

admin
continue
mentdb
CONTINUE
Used in use cases (8 recipes)

    MQL beginner › Savings plan: while, repeat, break and continue
    Date & time developer › French public holidays and a delivery date in working days
    JSON & document store developer › Merge the pages of a paginated API without duplicates
    Web service developer › Search with sort and pagination
    Control flow › First free meeting slot: nested loops, break and continue
    The MQL language in depth › Loops: continue, break, retry with backoff and parallel
    and 2 more in the Use cases
Often used with

    atom get  ·  atom size  ·  break  ·  not  ·  while  ·  atom find  ·  date is_weekend  ·  json doc

exception <id> <message>


Description

    EXCEPTION statement

Parameters

    id:   The error message id - string - required
    message:   The error message - string - required
admin
exception (1) ("your message ...");
mentdb
1: your message ...
Used in use cases (31 recipes)

    MQL beginner › Your first try and exception: a bank withdrawal
    MQL developer › Run user formulas safely: eval, try and error codes
    Backend & web service developer › Signed webhook receiver with replay protection
    Execution modes › Errors propagate through nested executes
    Web service developer › POST: create an order with validation
    Async developer › A run fails, the API comes back, replay it
    and 25 more in the Use cases
Often used with

    string regexp_extract  ·  try  ·  script create  ·  script delete  ·  execute  ·  atom get  ·  not  ·  wait

try <action> <errorAction> <idException>


Description

    TRY statement

Parameters

    action:   The main action - string - required
    errorAction:   The action to execute if an error is raised - string - required
    idException:   The variable that receives the error message - string - required
admin
try { exception (1) ("your messffage ..."); } { log trace [err]; } "[err]";
mentdb
1
Used in use cases (41 recipes)

    MQL beginner › Your first try and exception: a bank withdrawal
    MQL developer › Run user formulas safely: eval, try and error codes
    Backend & web service developer › Signed webhook receiver with replay protection
    Script developer › A script with typed parameters rejects bad calls
    Execution modes › call shares the caller's variables
    Web service developer › POST: create an order with validation
    and 35 more in the Use cases
Often used with

    string regexp_extract  ·  script create  ·  script delete  ·  execute  ·  exception  ·  atom get  ·  type is_integer  ·  atom size