Flash 5.5 tutorial - [ Beginners ]
Know about Conditional Statement in
Flash - If Statement
A conditional statement is a type of statement that performs an action only when a specified condition is met. The "if" statement is your everyday, all-purpose conditional. If condition has the following syntax:
if (condition)
{
// write the statements or sub-statements
}
An if statement starts, not surprisingly, with the keyword if. The condition
that must be satisfied for substatements to be executed is enclosed in
parentheses. The sub-statements are one or more ActionScript statements.
Each substatement should be on its own line and terminated with a semicolon.
The entire if statement ends with a closing curly brace, (}), without
a trailing semicolon.
The condition of our if statement can be any valid expression. When an if statement is executed, the interpreter checks the value of that expression (called the test expression or condition). If it is true, the sub-statements are executed. Otherwise, the sub-statements are not executed. Here is an example;
var a=10;
var b=5
if (a>b)
{
trace("A is Big"); // This statement
will be executed
}
Another form of "if" statement is "If-else". If the
condition is true the preceeding sub-statements will be executed. Otherwise,
control goes to the else part and executes the sub-statements.
syntax:
if (condition)
{
sub-statements-1
}
else
{
sub-statements-2
}
By considering above values of a and b, we can write
as below;
if (a>b)
{
trace("A is Big"); // This statement
will be executed
}
else
{
trace("B is Big");
// This statement will be executed
}
Else If Statement
Using if and else, we can optionally execute one of two code blocks. By
using if and else if, we can execute one or even none of an unlimited
number of code blocks.
syntax:
if (condition-1)
{
sub-statements-1
}
else if (condition-2)
{
sub-statements-2
}
else if (condition-3)
{
sub-statements-3
}
else
{
sub-statements-4 // this will be executed, if above conditions were
not met
}
where condition-1, condition-2 and condition-3 must be valid expressions.
sub-statements-1 will be executed if condition-1 is true. If condition-1
is false, sub-statements-2 will be executed if condition-2 is true. Otherwise,
condition-3 is evaluated and so on for as many else if statements are
provided. If none of the test expressions are true, the statements in
the final else will be executed.
That's all my dear friends. Hope You Enjoyed It!!!
Don't forget send mail to me.
![]()
"Fundamentals are the Route
to the Destination" - BHMK
Go Home