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) ? 'www.marplo.net' : 'coursesweb.net';
trace(str);        // www.marplo.net

/* Equivalent with:
   if(num > 5) { var str:String = 'www.marplo.net'; }
   else { var str:String = 'coursesweb.net'; }
*/
- Will set "str" to "www.marplo.net" or "coursesweb.net" depending on the value of "num". The Output is "www.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 create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Conditional Statements if, else, switch

Last accessed pages

  1. Zuma Honey (1371)
  2. SHA512 Encrypt hash in JavaScript (5059)
  3. Backgammon (1291)
  4. Strategy Games (3243)
  5. Sweet Home Decoration (45)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (47785)
  2. Read Excel file data in PHP - PhpExcelReader (35922)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (35547)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (32047)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (29323)