Condition Statements
Condition statements are used
to specify one or more conditions for the program to evaluate,
along with the statements to execute if the condition is
true and, optionally, the statements to execute if it is
false.
Silk treats any non-zero and non-null value as true. A value of zero or null is treated as false.
Silk provides the following conditional statements:
if statementAn
if statement consists of a Boolean expression followed by one or more statements. Curly braces are used to group the 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, the block of
code inside the 'if' statement is executed. If the
Boolean expression evaluates to false, execution continues with the first line after
the end of the 'if' block (after the closing curly brace).
if...else statement
An
if statement can be followed by an optional
else statement, which runs 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) //condition 1
{
print("i=0");
}
else if(i==10) //condition 2
{
print("i=10");
}
else if(i==100) //condition 3
{
print("i=100");
}
else //all the above conditions are evaluated to false, then execute the following statement
{
print("not found");
}
//continue to execute the following statement
print("continue");