Professional Documents
Culture Documents
Perform any TWO virtual lab experiment from below link. Grade: AA / AB / BB / BC / CC / CD /DD
(http://vlabs.iitb.ac.in/vlabs-dev/labs/javascript/index.php)
____________________________________________________________________
Signature of the Staff In-charge with date
Expected Outcome of Experiment: Apply JavaScript for validation in client side programming.
_______________________________________________________________
Theory:
IF statement in javascript
1.1 Introduction Conditional statements are useful in scenarios which require us to perform different actions for different decisions.
In JavaScript we have the following 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 the if statement to specify a block of JavaScript code to be executed if a condition is true.
if (condition) {
// block of code to be executed if the condition is true
}
Use the else statement to specify a block of code to be executed if the condition is false.
if (condition) {
// block of code to be executed if the condition is true
} else {
Use the else if statement to specify a new condition if the first condition is false.
if (condition1) {
} else if (condition2) {
} else {
1.2.Example:
If statement
If else statement
} else {
} If else if statement
} else {
1.1 Introduction
Conditional statements are useful in scenarios which require us to perform different actions for different decisions.
In JavaScript we have the following 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 switch
statement is a multiway decision statement that tests whether the value of an expression(or variable) matches against list of case values and branches
accordingly.
The switch statement is used to execute any one block of statements out of multiple blocks based on the value of the expression provided as condition for
switch statement.
Basic Syntax
switch(expression)
{
case value-1 :
statements ;
break;
case value-2 :
statements ;
break;
case value-3 :
statements ;
break;
....
....
default :
statements;
}
The expression is an integer expression or character. If the case matches the expression value, the execution starts at that case. The 'break' is provided to avoid
the execution of next case which also causes the exit from the switch. The 'default' is an optional case. If none of the cases match, default block will be
executed. Cases and default clause can occur in any order. The 'break' statement is optional, as two or more case labels may refer to the same block of
statements.
1.2. Example :
Switch case statement
switch (day)) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}
Problem Statement:
1. Using if-else statements print whether the user input integers are positive, negative or zero.
2. Print all the days from Sunday to Sunday and then take user input for numbers between 1 to 7. The radio button against the day for the user input
CODE:
For first problem
<html>
<head>
<title>Conditional Statements in Javascript</title>
</head>
<body>
<h1> IF...ELSE Statement </h1>
<p id="demo"> </p>
</body >
<script>
var num = prompt("Enter a number : ");
if(num > 0)
}
</script>
</html >
OUTPUT:
QUIZ:
Conclusion:
With the help of virtual labs, we cound understand and implement the concepts of if-else statements and switch case.