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 renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
Conditional Statements if, else, switch

Last accessed pages

  1. Living Skillfully and Essence of Skillfulness (170)
  2. PHP MySQL - WHERE and LIKE (29318)
  3. Paint Bucket and Eyedropper (2115)
  4. Convert XML to JSON in PHP (12408)
  5. Adobe Flash Courses ActionScript 3 Tutorials (6560)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (220)
  2. Read Excel file data in PHP - PhpExcelReader (81)
  3. PHP Unzipper - Extract Zip, Rar Archives (71)
  4. The Four Agreements (69)
  5. The Mastery of Love (58)
Chat
Chat or leave a message for the other users
Full screenInchide