save up to 40%

Conditional statements

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

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 if statement: The if statement is a simple decision-making statement that is used to execute a statement or a group of statements based on some condition. If a condition is true, the statement in the if block is executed; otherwise, the statement following the if block is executed.

The syntax of the if statement is as follows:

if(condition)
{
   //statements
}


The following example will demonstrate the use of the if statement:

var a;
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 if-else statement: The if-else statement is used to execute a statement or a group of statements based on some condition. If the condition is true, the statement in the if block is executed. If the condition is false, the statement in the else block is executed.

The syntax for using the if-else statement is as follows:

if(condition)
{
   //statements
}
else
{
   //statements
}


The following example will demonstrate the use of the if else statement:

var a;
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 if-else if statement: The if-else if statement is used to check several conditions. If any of the condition is true, the statement of that block is executed; otherwise, the value in the else block is executed.

The syntax of using the if-else if statement is as follows:

if(condition)
{
   //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 a;
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 conditional statement: The conditional statement can be used in the same way as the if else statement. The conditional statement is a single line statement that can be used in place of the if-else statement.

The syntax of using the conditional statement is as follows:

variablename=condition ? value1 : value2

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 a;
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.

The switch statement:


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.