You are on page 1of 6

Somaiya Vidyavihar University

K. J. Somaiya College of Engineering, Mumbai-77

TITLE: Perform Virtual Lab on JavaScript


Batch: B3 Roll No.: 16010120123

AIM: Perform Virtual Lab on JavaScript Experiment No. 05

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 {

// block of code to be executed if the condition is false

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

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

1.2.Example:

If statement

if (hour < 18) {

greeting = "Good day";

Department of Computer Engineering

Page No WPL Sem IV / Jan - May 2022


Somaiya Vidyavihar University

K. J. Somaiya College of Engineering, Mumbai-77

If else statement

if (hour < 18) {

greeting = "Good day";

} else {

greeting = "Good evening";

} If else if statement

if (time < 10) {

greeting = "Good morning";

} else if (time < 20) {

greeting = "Good day";

} else {

greeting = "Good evening";

Switch Case 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 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;
....

Department of Computer Engineering

Page No WPL Sem IV / Jan - May 2022


Somaiya Vidyavihar University

K. J. Somaiya College of Engineering, Mumbai-77

....
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

number will be selected.

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)

Department of Computer Engineering

Page No WPL Sem IV / Jan - May 2022


Somaiya Vidyavihar University

K. J. Somaiya College of Engineering, Mumbai-77

document.getElementById("demo").innerHTML = num + " is a POSITIVE number";


else if(num < 0)
document.getElementById("demo").innerHTML = num + " is a NEGATIVE
number";
else
document.getElementById("demo").innerHTML = 0 + " - ZERO";
</script>
</html >
For second problem
<html>
<head>
<title>Conditional Statements in Javascript</title>
</head>
<body>
<h1> SWITCH CASE Statement </h1><br/>
SUNDAY <input type="radio" id="r1"><br/>
MONDAY <input type="radio" id="r2"><br/>
TUESDAY <input type="radio" id="r3"><br/>
WEDNESDAY <input type="radio" id="r4"><br/>
THURSDAY <input type="radio" id="r5"><br/>
FRIDAY <input type="radio" id="r6"><br/>
SATURDAY <input type="radio" id="r7"><br/>
</body >
<script>
var day = prompt("Enter a number between 1 to 7 : ");
switch(day) {
case 1:
document.getElementById("r1").checked = true;
break;
case 2:
document.getElementById("r2").checked = true;
break;
case 3:
document.getElementById("r3").checked = true;
break;
case 4:
document.getElementById("r4").checked = true;
break;
case 5:
document.getElementById("r5").checked = true;
break;
case 6:
document.getElementById("r6").checked = true;
break;
case 7:
document.getElementById("r7").checked = true;
break;

Department of Computer Engineering

Page No WPL Sem IV / Jan - May 2022


Somaiya Vidyavihar University

K. J. Somaiya College of Engineering, Mumbai-77

}
</script>
</html >
OUTPUT:

QUIZ:

Department of Computer Engineering

Page No WPL Sem IV / Jan - May 2022


Somaiya Vidyavihar University

K. J. Somaiya College of Engineering, Mumbai-77

Conclusion:
With the help of virtual labs, we cound understand and implement the concepts of if-else statements and switch case.

Date: Signature of faculty in-charge

Department of Computer Engineering

Page No WPL Sem IV / Jan - May 2022

You might also like