Actionscript Course

Conditional statements enable you to program a script to make decisions based on one ore more conditions.
All conditional statements deal with Boolean values (true and false). They can execute certain instructions if a condition is true, and other instructions if it is false.
This tutorial explains the following conditionals:

if() statements

The syntax to create if() statements is:
if (condition) {
  // code to execute if the condition is true
}
- "condition" can be any logical expresion.
- If "condition" is true, executes the code within curly brackets. If the condition is false, the code ignores the statements within the curly brackets.
Example:
var num:Number = 8;
if(num > 7) {
 trace('Condition true');
}

// Condition true
The value of "num" variable is greater than 7, so (num > 7) is true and the script executes the trace() function within curly brackets.

if() ... else statement

By adding the else keyword, you can add a block of code that will be executed only if the condition is not met.
So, the "if() ... else" statement is used to execute some piece of code if the condition is true and other block of code if the condition is false.
Syntax to crete "if ... else" statements
if (condition) {
  // code to execute if the condition is true
}
else  {
  // code to execute if the condition is false
}
- "condition" can be any logical expresion.
Example:
var day:String = 'Monday';

if(day=='Sunday') {
  trace('Stay home');
}
else {
  trace('Go to work');
}

// Go to work
The script evaluates the condition within pharantesses (day=='Sunday'), which returns false becouse the value of the "day" variable is 'Monday'. Then, the script will executes the code of the "else" statement (in Output displays "Go to work").

else if() clause

You can use the "else if()" clause to add and check another clause if the first "if()" statement isn't true.
  - Syntax:
if (condition1) {
  // code to execute if the condition1 is true
}
else if (condition2) {
  // code to execute if the condition1 is false and condition2 is true
}
else if (condition3) {
  // code to execute if the condition1 and condition2 are false and condition3 is true
}
else  {
  // code to execute if all these conditions are false
}
So, with "else if" you can handle several different conditions.
You can use as many "else if" clauses as you want.
  - Example:
var day:String = 'Monday';

if(day=='duminica') {
  trace('Stay home');
}
else if(day=='Monday') {
  trace('Go to grandparents');
}
else if(day=='Saturday') {
  trace('Sport');
}
else {
  trace('Go to work');
}

// Go to grandparents
The script checks if the first "if()" condition is true, if returns false, it evaluates the next "else if" clause, if returns true executes its code and ignores the next "else if" statements, if it returns false, will check the next "else if()" clause, and so on till the "else" statement.
The script above outputs "Go to grandparents", becouse the value of "day" variable is 'Monday'.

switch statement

This conditional selects one of many blocks of code to be executed, by comparing the value of a single variable against several possible values.
The switch() statement makes a good alternative when you have multiple conditions to check, best used in place of a long "if-else if-else" conditional.
Syntax to create switch() statements:
switch (variable) {
  case value1:
    code to execute if variable = value1
    break;
  case value2:
    code to execute if variable = value2
    break;
  case value3:
    code to execute if variable = value3
    break;
  default :
    code to execute if none of the other cases match
}
The "switch()" statement checks the actual value of the "variable", compares it to the list of options (case), and determines the appropiate code block to execute.
The "default" is used when none of the other cases match. It's not required, but it is useful if you want to define a case that executes if none of the other cases in the switch statement match.

Notice that break statements are used after each "case". The break statement tells the conditional statement (or loop) to cancel the rest of its operation without going any further.
If you not add the "break" instruction, the code continues to the next cases.

- Example:
var day:String = 'Tuesday';

switch(day) {
  case 'Sunday':
    trace('Stay home');
    break;
  case 'Monday':
    trace('Go to grandparents');
    break;
  case 'Tuesday':
    trace('A walk in the garden');
    break;
  case 'Saturday':
    trace('Sport');
    break;
  default:
    trace('Go to work');
}
- This code will output "A walk in the garden".

Logical operators and conditionals

The logical operators are: && (AND), and || (OR). They are used to evaluate multiple conditions in a conditional statement, and return true or false.   - Example:
var day:String = 'Saturday';
var num:Number = 8;

if(day=='duminica' && num==8) {
  trace('Stay home');
}
else if(day=='Saturday' || num>8) {
  trace('A walk in the garden');
}
else {
  trace('Go to work');
}

// A walk in the garden
In the Output panel displays "A walk in the garden", becouse the condition "(day=='duminica' && num==8)" returns false (not both expressions true), but the condition "(day=='Saturday' || num>8)" returns true.

The conditional operator ( ? : )

Another method that you can use to check the truth of an expression is the conditional operator ( ? : ).
This opoerator can be used to assign a value to a variable based on a condition.
- Syntax:
variable = (condition) ? val1_true : val2_false;
- It evaluates the condition, if it's True then the variable takes the value 'val1_true', otherwise, takes the value 'val2_false'.
Example:
var num:Number = 8.3;

// set "str" variable depending on the value of "num"
var str:String = (num > 5) ? 'marplo.net' : 'coursesweb.net';
trace(str);        // marplo.net

/* Equivalent with:
   if(num > 5) { var str:String = 'marplo.net'; }
   else { var str:String = 'coursesweb.net'; }
*/
- Will set "str" to "marplo.net" or "coursesweb.net" depending on the value of "num". The Output is "marplo.net".

The "? :" conditional operator can also be used to determine the execution of a function or another, depending on the result of a logical expression:
(logical-expression) ? functionTrue() : functionFalse();
Example:
(var1 == var2) ? fTrue() : fFalse();
- If "var1" equal to "var2", calls the fTrue() function, otherwise calls the fFalse() function.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Conditional Statements if, else, switch

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)