Conditional Constructs

Conditional Constructs

Consider a situation where you need to execute certain statements in a script on the basis of certain condition. For example, you want to check whether a given number is even or odd and display a message accordingly. For this, you need to make decisions in the script code and execute a different set of statements depending upon the decision taken. 

You can do this by using conditional constructs . These constructs allow you to execute a selective statement or a block of statements based on the result of the expression being evaluated. 

The two conditional constructs in JavaScript are:

  • The if.. else construct
  • The switch...case construct
http://affiliate.flipkart.com/install-app?affid=kartikbag
The if...else Construct

The if statement in the if...else conditional constructs is followed by a logical expression in parenthesis. This condition is evaluated and a decision is made on the basis of the result.

The following statements depict the syntax of the if...else statement:

if (exp)
{
// Statements;
}
else
{
// Statements;
}

In the preceding syntax,  the expression exp, is evaluated. If the result is true, the statement inside the if construct are executed. If the result is false, The statement inside the else construct are executed.

Consider the example of a game where you need to validate the age of a player. If the age is grater than 12,  the player is allowed to play the game . Otherwise, an appropriate message need to be displayed. 

The following code shows the usage of the if...else construct:

var age=5;
if(age<12)
{
alert(' Sorry! This Game is for children above 12 years');
}
else
{
alert('Play the Game');
}

The preceding code snippet checks whether the age of a player is less than 12. The condition in the if statement checks the age of the player. If the condition evaluates to true, the message, Sorry! This Game is for children above 12 years , is displayed . If the condition evaluates to false, the message Play a Game is displayed.

The switch...case Construct

Another conditional construct available in JavaScript is a switch ...case construct. It is used when you need to evaluate a variable for multiple values. 

The following code depicts the syntax for the switch...case construct:

switch (VariableName)
{
case ConstantExpression_1:
//statements;
break;
case ConstantExpression_2:
//statemets;
break;
case ConstantExpression_n:
//statements;
break;
}

While the switch statement is executed, the variable passed as a parameter to the switch statement is evaluated and individually compared  with each constant expression specified with each case statement. If one of the constant expression is equal to the value of the variable given in the switch statement , the control is passed to the statement following the matched case statement.
A break statement is used to exit the switch statement . This prevents execution of the remaining case structures by ending the execution of the switch...case construct.

Consider the following code where you want to display the name of the day of the week depending on the value of a variable:

<!DOCTYPE HTML>
<HTML>
<HEAD>
<BODY>
<SCRIPT type="text/javascript">
var day="5";
switch(day)
{
case "1":
alert("Day is Monday");
break;
case "2":
alert("Day is Tuesday");
break;
case "3":
alert("Day is Wednesday");
break;
case"4":
alert("Day is Thursday");
break;
case "5":
alert(" Day is Friday");
break;
case"6":
alert(" Day is Saturday");
beak;
case"7":
alert("Day is Sunday");
break;
default:
alert("Not a Valid Number");
break;
}
</SCRIPT>
</HEAD>
</BODY>
<HTML>

About Codingwelove

Codingwelove
Recommended Posts × +

0 comments:

Post a Comment