COMPUTER ENGINEERING
DEPARTMENT
Subject: Client Side Scripting Subject Code: 22519
th
Semester: 5 Course: Computer Engineering
Laboratory No: L001B Name of Subject Teacher: Prof Pokharkar M.S.
Name of Student: Roll Id: 17202C0009
Experiment No: 2
Title of Experiment Developed JavaScript t use decision making and looping statements.
Theory:
Conditional statements are used to perform different actions based on different conditions.
Conditional Statements
Very often when you write code, you want to perform different actions for different
decisions.
You can use conditional statements in your code to do this.
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 elseto specify a block of code to be executed, if the same condition is false to
Use elsespecify
if a new condition to test, if the first condition is false
Use switch
to specify many alternative blocks of code to be executed
Looping Statement:
Loops can execute a block of code a number of times.
JavaScript Loops
Loops are handy, if you want to run the same code over and over again, each time with a
different value.
Page | 1
JavaScript supports different kinds of loops:
- loops
for through a block of code a number of times
- loops through the properties of an object
for/in
while- loops through a block of code while a specified condition is true
do/while- also loops through a block of code while a specified condition is true
syntax:-
1) IF syntax :-
if (condition) {
// block of code to be executed if the condition is true
}
eg-
if (hour < 18) {
greeting = "Good day";
}
2) IF else syntax:-
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
}
eg-
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
3) ELSE IF syntax:-
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
}
Page | 2
eg-
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
4) SWITCH syntax:-
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
eg-
switch (new Date().getDay()) {
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";
}
Page | 3
Program:
1) IF
<html>
<head>
<title>IF Statments!!!</title>
<script type="text/javascript">
var age = prompt("Please enter your age");
if(age>=18)
document.write("You are an adult <br />");
if(age<18)
document.write("You are NOT an adult <br />");
</script>
</head>
<body>
</body>
</html>
Output:
Page | 4
2) IF ELSEE
<html>
<head>
<title>If...Else Statments!!!</title>
<script type="text/javascript">
// Get the current hours
var hours = new Date().getHours();
if(hours<12)
document.write("Good Morning!!!<br />");
else
Page | 5
document.write("Good Afternoon!!!<br />");
</script>
</head>
<body>
</body>
</html>
Output:-
3) ELSE IF STATEMENT:-
<html>
<head>
<script type="text/javascript">
var x = prompt("Enter the first number");
var y = prompt("Enter the second number");
one = parseInt(x);
two = parseInt(y);
if (x == y)
document.write(x + " is equal to " + y + ".");
Page | 6
else if (one<two)
document.write(x + " is less than " + y + ".");
else
document.write(x + " is greater than " + y + ".");
</script>
</head>
<body>
</body>
</html>
Output:-
Page | 7
4) SWITCH CASE :-
<html>
<body>
<script type="text/Javascript">
var day = prompt ("Eneter number between 1 to 7","");
day = parseInt(day);
switch(day)
case 1: document.write("Monady");break;
case 2: document.write("Tuesday");break;
case 3: document.write("Wednusday");break;
case 4: document.write("Thuersday");break;
case 5: document.write("Friday");break;
case 6: document.write("Saturady");break;
case 7: document.write("Sunday");break;
</script>
</body>
</html>
Page | 8
Output:-
LOOPS:-
1) FOR LOOP:-
<html>
<body>
<script type = "text/javascript">
var i;
document.write("Starting Loop" + "<br />");
Page | 9
for(i = 0; i < 10; i++) {
document.write("Current Count : " + i );
document.write("<br />");
document.write("Loop stopped!");
</script>
</body>
</html>
Output:-
2) WHILE LOOP
<html>
Page | 10
<body>
<script type = "text/javascript">
var i = 0;
document.write("Starting Loop ");
while (i < 10) {
document.write("Current Count : " + i + "<br />");
i++;
</script>
</body>
</html>
Output:-
Page | 11
3) DO WHILE LOOP
<html>
<body>
<script type = "text/javascript">
var p = 0;
document.write("Starting Loop" + "<br />");
do {
document.write("Current Count : " + p + "<br />");
p++;
while (p < 5);
document.write ("Loop stopped!");
</script>
</body>
</html>
Output:-
Page | 12
4) FOR IN LOOP
<html>
<body>
<script type="text/Javascript">
var languages={first:"c",
second:"Pyathon",
third:"java",
fourth:"php"};
for(i in languages)
document.write(languages[i]+"<br>");
</script
</body>
</html>
Output:-
Grade and P1 (35M) P2 (15 M) Total ( 50 M) Dated Sign
Dated
Signature of
Teacher
Page | 13
Page | 14