Condition statements
Condition statements are used
to specify one or more conditions to be evaluated by the program,
along with a statement or statements to be executed if the condition is
determined to be true, and optionally, other statements to be executed
if the condition is determined to be false.
Silk assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
There are the following condiction statements in Silk:
if statementAn
if statement consists of a Boolean expression followed by one or more statements. The curly brace is used to contain statements.
if example:
i=1;
if(i>0) //Boolean expression
{
// if Boolean expression is true, then will execute the following statement
print("i>0");
}
//continue to execute the following statement
print("continue");
If the Boolean expression evaluates to true, then the block of
code(statements) inside the 'if' statement will be executed. If the
Boolean expression evaluates to false, then the first set of code after
the end of the 'if' statement (after the closing curly brace) will be
executed.
if...else statement
An
if statement can be followed by an optional
else statement, which executes when the Boolean expression is false.
if...else example:
i=0;
if(i>0) //Boolean expression
{
// if Boolean expression is true, then will execute the following statement
print("i>0");
}
else
{
// if Boolean expression is false, then will execute the following statement
print("i<0");
}
//continue to execute the following statement
print("continue");
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed.
if...else if...else statements
You can nest if-else statements, which means you can use one
if or
else if statement inside another
if or
else if statement(s).
When you use if...else if...else statements, please follow the following rules:
- one if can be followed by one else or nothing, else should always follow after all else if
- one if can be followed by multiple else if or nothing, else if should be always placed before else
- once one else if evaluates to true, other else if and else will not be evaluated.
if...else if...else sample:
i=0;
if(i==0) //condiction 1
{
print("i=0");
}
else if(i==10) //condiction 2
{
print("i=10");
}
else if(i==100) //condiction 3
{
print("i=100");
}
else //all the above condictions are evaluated to false, then execute the following statement
{
print("not found");
}
//continue to execute the following statement
print("continue");