Conditional statements
Conditional statements
Rating:
Conditional statement is used for decision-making. It performs an action depending on whether or not a condition is true. If the condition is true, the statements following the condition are executed. But if the condition returns false, it will disregard the enclosed actions and continue to the next statement that follows the condition block.
There are several types of conditional statements. Each are explained below:
The syntax of the if statement is as follows:
{
//statements
}
The following example will demonstrate the use of the if statement:
var b;
if(a>b)
{
trace(a);
}
The above code will execute only if the value of the variable a is greater than the value of the variable b, and the value of the variable a will be displayed on the output window.
The syntax for using the if-else statement is as follows:
{
//statements
}
else
{
//statements
}
The following example will demonstrate the use of the if else statement:
var b;
if(a>b)
{
trace(a);
}
else
{
trace(b);
}
In the above code, if the value of the variable a is greater than the value of the variable b, the value of the variable a will be displayed on the output window. If the value of the variable b is greater than the value of the variable a, the value of the variable b will be displayed on the output window.
The syntax of using the if-else if statement is as follows:
{
//statements
}
else if(condition)
{
//statements
}
else if(condition)
{
//statements
}
else
{
//statements
}
The following example will demonstrate the use of the if-else if statement:
var b;
var c;
var d;
var e;
var f=(a+b+c+d+e)*100/500
if(f<60)
{
trace("You are Failed");
}
else if(f>=60 && f<70)
{
trace("Your Grade is D");
}
else if(f>=70 && f<90)
{
trace("Your Grade is C");
}
else if(f>=80 && f<90)
{
trace("Your Grade is B");
}
else
{
trace("Your Grade is A");
}
In the above code, five conditions are given. If anyone of the condition is true, the statement following the block will be executed.
The syntax of using the conditional statement is as follows:
If the condition is true, the statement following the question mark(?) will be executed. If the condition is false, the statement after the colon(:) is executed.
The following code will demonstrate the use of the conditional expression:
var b;
var c=a>=b ? a : b
trace(c);
In the above code, if the value of the variable a is greater than or equal to the value of the variable b, the value of the variable a will be displayed on the output window. If the value of the variable b is greater than the value of the variable a, the value of the variable b will be displayed on the output window.
Rating:
Other articles
- What are embedded fonts?
- What is the use of the Info panel?
- What is motion tweening?
- How to create a graphic symbol?
- What is the delete operator?
