Conditional Statement

Conditional Statement:- JavaScript supports three conditional statement
If statement,If-else and switch statement.

If statement

If statement supports decision making and control the sequence of execution of statement.

Syntax of the If statement is

If (condition)
 statement;

or

If (condition)
 {
 statement 1;
 statement 2;
 }

In the If statement, a condition is evaluated first.If this condition evaluates to TRUE, then the statement associated with if block are executed. If the condition false then the statement are not executed.

If-else statement

The syntax of If-else statement is

If (condition)
{
statement 1;
statement 2;
}
else
{
statement 3;
statement 4;
}

The If-else statement, this statement specifies two block. If the tested condition is TRUE, the statement in If block are executed.And the if condition is FALSE, the statement in else block are executed.

Switch statement

The syntax of Switch statement is
 
Switch (switch variable)
 {
 case value 1;
 statement 1;
 break;
 case value 2;
 statement 2
 break;
 default;
 statement 3;
 }

The Switch statement stats by comparing the switch variable with the value of case 1. If they match statement 1 is executed, and the switch statement is terminated by the break statement. If they do not match, testing continues unit a break statement is reached or until the program reach the default statement.

Leave a Reply