Sunday, 3 May 2015

html session 13

Html tutorial theory :

if-else if statement:

The if-else if statements allow you to check multiple conditions and specify a different block to be executed for each condition. The flow of these statements begins with the if statement followed by multiple else if statements and finally by an optional else block. The entry point of execution in these statements begins with the if statement. If the condition in the if statement is false, the condition in the immediate else if statement is evaluated.
The if-else if statements are also referred to as the if-else if ladder.
The syntax to use the if-else if statements are as follows:
Syntax:
if (condition)
{
// one or more statements;
}
else if (condition)
{
// one or more statements;
}
else
{
// one or more statements;
}
Code Snippet 15 displays the grades according to the percentage value entered by the user using the if-else if statements.


<script>
var percentage = prompt(‘Enter percentage:’,0);
if (percentage >= 60)
{
alert (‘You have obtained the A grade.’);
}
else if (percentage >= 35 && percentage < 60)

{
alert (‘You have obtained the B class.’);
}
else
{
alert (‘You have failed’);
}
</script>    
   

The code accepts the percentage value from the user and stores it in the percentage variable. The if statement checks whether the value of the percentage variable is greater than or equal to 60. If this is true, the user has obtained the A grade. If the condition is false, the execution control is passed to the else if block. Here, the value of the percentage variable is checked as to whether it is greater than or equal to 35 and less than 60. If this is true, the user has obtained the B grade. If the condition is false, the else block is executed.
13.4.4 Nested-if Statement
The nested-if statements comprises multiple if statements within an if statement. The flow of the nested-if statements starts with the if statement, which is referred to as the outer if statement. This outer if statement consists of multiple if statements, which are referred to as the inner if statements.
The inner if statements are executed only if the condition in the outer if statement is true. Further, each of the inner if statements is executed, but only if the condition in its previous inner if statement is true.
The syntax to use the nested if statements.
Syntax:
if (condition)
{
// one or more statements;
if (condition)
{
// one or more statements;
if (condition)
{
 Concepts Session 13 Operators and Statements V 1.1 © zetutorials Limited
// one or more statements;
}
}
}
Code Snippet 16 validates the username and password using the nested-if statements.

<SCRIPT>
var username = prompt(‘Enter Username:’);
var password = prompt(‘Enter Password:’);
if (username != “” && password != “”)
{
if (username == “admin” && password == “admin123”)
{
alert(‘Login Successful’);
}
else
{
alert (‘Login Failed’);
}
}
</SCRIPT>    

The code accepts the username and password and stores them in the username and password variables. The if statement checks whether the values of both the variables are not empty. If they are not empty, the inner if statement is executed. The inner if statement checks whether the value of the username variable is admin and the value of the password variable is admin123. If this condition is true, the Login Successful message is displayed to the user. If the condition is false, the else block is executed.
13.4.5 switch-case Statement
A program becomes quite difficult to understand when there are multiple if statements. To simplify coding and to avoid using multiple if statements, switch-case statement can be used as a different approach to code the same logic. The switch-case statement allows comparing a variable or expression with multiple values.

The syntax to use the switch-case statement is as follows:
Syntax:
switch(expression/variable)
{
case value1:
// statements;
break;
case value2:
// statements;
break;
. . .
case valueN:
// statements;
break;
default:
// default statement
}
where,
switch: Executes a specific case statement that holds the value of the expression or the variable.
case: A value and a colon follow the case keyword. The block of a specific case statement is executed when the value of switch expression and the case value are the same. Each case block must end with the break keyword.
break: Passes the execution control to the statement existing immediately out of the switch-case statement. If there is no break statement, the next case statement is executed.
default: The execution control passes to the default block when none of the case values matches with the switch expression. The default block is the same as the else block of the if-else if statements.
Code Snippet 17 displays the salary of an employee according to the designation by using the switch-case statement.

<SCRIPT>
var designation = prompt(‘Enter designation:’);
switch (designation)
{
case ‘Manager’:
alert (‘Salary: $21000’);
break;
case ‘Developer’:
alert (‘Salary: $16000’);
break;
default:
alert (‘Enter proper designation.’);
break;
}
</SCRIPT>    
The code uses the variable designation to store the designation of an employee, which is accepted from the user. The switch statement takes the value of the designation variable and this value is matched with the different case statements. If the value matches, the particular case block is executed, which displays the respective salary. If none of the case values matches with the switch variable, the default block is executed.

No comments:

Post a Comment