The most interesting, but difficult, in writing a script is designing it so that it can take decision while is executed.
Using conditional instructions can make programs to test various conditions and then decide how to manipulate the datas.

JavaScript uses the following conditional instructions:

1. The "if" statement

The general form of this instruction is the following:
if (condition) {
  The code will be executed if condition is true
}
- 'condition' can be any logical expresion.
- If 'ondition' return TRUE, executes the code within curly braces, otherwise, when the condition returns false, it passes over the code.
Here's an example. The following script prints "Hello" if the hour is greater than 11.
- We use the Date object, will be explained in another lesson.
<script type="text/javascript"><!--
// if the hour > 11, prints Hello!
var d = new Date();
var time = d.getHours();
if (time>11) {
 document.write("<b>Hellow!</b>")
}
--></script>
- We defined the variable 'd', whose value is an object with the current date, then the variable 'time' takes only the hour from the variable 'd'. The condition of 'if' statement checks if 'time' is greater than 11 and if True executes the command of the braces, which displays the message.
If the hour is less than 11, the script will do nothing.

2. The "if ... else" statement

In the previous example we saw that it displays "Hello!" if "time>11" and if not, nothing happens.
Using the instruction "if ... else" we can set commands to be executed when the condition of "if" instruction" is FALSE.
The general form of the "if ... else" instruction is:
if (condition) {
  The code will be executed if condition is True
}
else {
  The code will be executed if condition is False
}
- 'condition' can be any logical expresion.
If 'ondition' return TRUE it is executed the code inside the first braces (that belongs to "if"), otherwise, when the condition returns false it is executed the code of the second group of braces (after else).
Here again the previous example, this time using the "if ... else" statement.
The script displays "Hello!" if the hour is greater than 11, otherwise display "It's ... o`clock".
<script type="text/javascript"><!--
// if 'time' > 11, prints Hello!
// Otherwise "It's ... o`clock"
var d = new Date()
var time = d.getHours()
if (time>11) {
  document.write("<b>Hello!</b>")
}
else {
  document.write("<b>It`s " +time+ " o`clock</b>")
}
--></script>
- If 'time' is less than 11 the script will execute the code of second braces group, from "else".

3. switch statement

This instruction is used to compare a value with others from a list.

The syntax of switch instruction is:
switch (expresion) {
case value1:
    code executed if expresion = value1
    break
case value2:
    code executed if expresion = value2
    break
case value3:
    code executed if expresion = value3
    break
default :
    code executed if expresion is other then value1, valoare2 or valoare3
}
- First we have a single expression between parentheses (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
Here is a sample script that displays a message depending on weekdays:
<script type="text/javascript"><!--
var d = new Date();
var day = d.getDay();

switch(day) {
 case 5:
   document.write("Today is friday");
 case 6:
   document.write("Today is saturday");
 case 0:
   document.write("Today is sunday");
 default:
   document.write("There is a lot until saturday");
}
--></script>
- The variable 'day' takes the number of the week-day from the variable 'd' (Sunday = 0, Monday = 1, ...).
- The switch statement compares the value of "day" with the value of each "case". If there is a match, the block of code associated with that 'case' is executed.
- If none of the values of 'case' match the variable 'day', will execute the code associated with 'default'.

Here is another example where the "case", uses string values.
<script type="text/javascript"><!--
var nume = "Marius";
switch (nume) {
 case "Cristi":
   document.write("Friend");
   break
 case "Marius":
   document.write("Brother");
   break
 case "Maria":
   document.write("Sister");
   break
 default:
   document.write("Somebody else");
}
--></script>
- Will return Brother.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Conditional statements if, else, switch

Last accessed pages

  1. Validate radio and checkbox buttons (4204)
  2. Mystic India Pop (4909)
  3. Butterfly Kyodai (13386)
  4. Bike Mania2 (346)
  5. Auto Moto Games (3272)

Top accessed pages

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