Begin Web Programming with PHP & MySQL

Advertisements



JavaScript Conditional Statements

What are decision control statements?

Decision control statements are defined in programming languages in such a way that they can decide which lines of code are to be evaluated and how many times to evaluate them. Two types of decision control statements are conditional statements and looping statements.
Conditional statements are decision making statements. It allows programmers to write different codes for different conditions in a program to execute, when the corresponding condition is met.

  • Numbers are considered as true if not equal to 0; 0 is taken as false
  • Strings are considered as true if greater than 0 characters in length, otherwise they are considered as false
  • Undefined and null are taken as false

Conditional Statements

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of JavaScript code to be executed if a condition is true.

Syntax if(condition)
{
// block of code to be executed if the condition is true
}
Eg:
<script>
var x = 5;
if(x<10)
{
alert("x is less than 10."); /* This will be executed. */
}
</script>

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax: if(condition)
{
// block of code to be executed if the condition is true
}
else
{
// block of code to be executed if the condition is false
}
Eg:
<script>
var fruit_name = "Orange";
if(fruit_name == "Apple")
{
alert("Fruit name is Apple");
}
else
{
alert("Fruit name is not Apple"); /* This will be executed. */
}
</script>

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax:
if(condition1)
{
// block of code to be executed if condition1 is true
}
else if(condition2)
{
// block of code to be executed if the condition1 is false and condition2 is true
}
else
{
// block of code to be executed if the condition1 is false and condition2 is false
}

Eg:
<script>
var x = 10;
if(x < 10)
{
alert("x is less than 10");
}
if(x == 10)
{
alert("x is equal to 10"); /* This will be executed. */
}
else
{
alert("x is greater than 10");
}
</script>

Switch Statement

Switch selects one of many blocks of code to be executed. The switch statement is used to avoid long blocks of if..elseif..else code.

switch (n)
{
case 1:
code to be executed if n=1;
break;
case 2:
code to be executed if n=2;
break;
case 3:
code to be executed if n=3;
break;
...
default:
code to be executed if n is different from all case labels;
}

Eg:
<script>
var fruit = "apple";
switch (fruit)
{
case "orange":
alert("Your fruit is orange!");
break;
case "apple":
alert("Your fruit is apple!"); // this will be executed
break;
case "banana":
alert("Your fruit is banana!");
break;
default:
alert("Your fruit is not orange, apple or banana!");
}
</script>