Css Practical 2
Css Practical 2
DEPARTMENT
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.
Looping Statement:
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-
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-
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-
4) SWITCH syntax:-
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
eg-
Page | 3
Program:
1) IF
<html>
<head>
<title>IF Statments!!!</title>
<script type="text/javascript">
if(age>=18)
if(age<18)
</script>
</head>
<body>
</body>
</html>
Output:
Page | 4
2) IF ELSEE
<html>
<head>
<title>If...Else Statments!!!</title>
<script type="text/javascript">
if(hours<12)
else
Page | 5
document.write("Good Afternoon!!!<br />");
</script>
</head>
<body>
</body>
</html>
Output:-
3) ELSE IF STATEMENT:-
<html>
<head>
<script type="text/javascript">
one = parseInt(x);
two = parseInt(y);
if (x == y)
Page | 6
else if (one<two)
else
</script>
</head>
<body>
</body>
</html>
Output:-
Page | 7
4) SWITCH CASE :-
<html>
<body>
<script type="text/Javascript">
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>
var i;
Page | 9
for(i = 0; i < 10; i++) {
document.write("<br />");
document.write("Loop stopped!");
</script>
</body>
</html>
Output:-
2) WHILE LOOP
<html>
Page | 10
<body>
var i = 0;
i++;
</script>
</body>
</html>
Output:-
Page | 11
3) DO WHILE LOOP
<html>
<body>
var p = 0;
do {
p++;
</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:-
Page | 13
Page | 14