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 (condition) { // code to execute if the condition is true }- "condition" can be any logical expresion.
var num:Number = 8; if(num > 7) { trace('Condition true'); } // Condition trueThe value of "num" variable is greater than 7, so (num > 7) is true and the script executes the trace() function within curly brackets.
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.
var day:String = 'Monday'; if(day=='Sunday') { trace('Stay home'); } else { trace('Go to work'); } // Go to workThe 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").
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.
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 grandparentsThe 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.
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.
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.
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".
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 gardenIn 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.
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'.
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".
(logical-expression) ? functionTrue() : functionFalse();Example:
(var1 == var2) ? fTrue() : fFalse();- If "var1" equal to "var2", calls the fTrue() function, otherwise calls the fFalse() function.
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);