Conditional Execution - If, else if, else
Description
The 'If' construct in deluge is the same as that of other languages.
It conditionally executes a set of statements depending on the value of
the boolean expression.
Syntax
if(<if-boolean-expression>)
{
<if statements>
}
else if(<elseif-boolean-expression-1>)
{
<elseif statements-1>
}
else if(<elseif-boolean-expression-2>)
{
<elseif statements-2>
}
..
..
..
..
else if(<elseif-boolean-expression-n>)
{
<elseif statements-n>
}
else
{
<else statements>
}
|
An If construct should adhere to the following rules:
- It should have an 'If' condition
- It can have zero or more 'else if' conditions
- It can have an optional 'else' part
When an <If Construct> is encountered during script execution,
the condition specified by the <if-boolean_expression>
is evaluated. If the condition is met, the statement in the <if
statements> block are executed, but if the condition was not
met, then the program flow skips the statements in the <if statements>
block and searches for any else or else if keywords.
When an else if part is encountered, the condition specified by <boolean_expression-x>
is evaluated and if the condition evaluates to true, the statements
in <elseif statements-x> block is executed. If the <if
condition> and all the <else if conditions> fails
then the presence of <else> is searched and the statements
in <else statements> block is executed.
Example - Free flow scripting
Let us take the example of a recruitment
application. The deluge code for the on success block of the
New_Applicant form is given below:
on add { on success { opening = New_Opening [Position_Name == input.Applied_For]; if (opening.Status == "Closed") { sendmail ( To : input.Email_ID From : zoho.adminuserid Subject : "Reg application for job posted at recruitment.zohocreator.com" Message : "The job profile " + input.Applied_For + " for which you have applied is not currently open " ) } else if (opening.Experience == input.Experience) { sendmail ( To : "manager-recruitment@adventnet.com" From : zoho.adminuserid Subject : "Applicants resume matches job profile" Message : input.Applicant_Name + " matches the job profile<br> Contach Info: " + input.Email_ID ) } else { sendmail ( To : input.Email_ID From : zoho.adminuserid Subject : "Reg application for job posted at recruitment.zohocreator.com" Message : "The experience required for the job profile " + input.Applied_For + "does not match yours" ) } } }
|
Code Explantion:
- In the on success block, we first fetch the record from the New_Opening
form, whose Position_Name matches with the position applied
for.
- if status of the position is "Closed" the statements
inside the if block is executed.
- if experience for the position is same as the experience specified,
the statements inside the else if block is executed.
- if the above two conditions fail, the statements inside the else
block is executed.
|