Professional Documents
Culture Documents
Experiment No: 2
Title of Experiment: Developed JavaScript 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.
• 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
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.
• do/while - also loops through a block of code while a specified condition is true
syntax:-
• IF syntax :-
if (condition) {
eg-
• IF else syntax:-
if (condition) {
} else {
eg-
} else {
• ELSE IF syntax:-
if (condition1) {
} else {
eg-
} else {
• SWITCH syntax:-
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
Eg-
case 0:
day = "Sunday";
break;
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";
}
Program:
• IF
<html>
<head>
<title>IF Statments!!!</title>
<script type="text/javascript">
if(age>=18)
if(age<18)
</head>
<body>
</body>
</html>
Output:
• IF ELSE
<html>
<head>
<title>If...Else Statments!!!</title>
<script type="text/javascript">
if(hours<12)
else
</script>
</head>
<body>
</body>
</html>
Output:-
• ELSE IF STATEMENT:-
<html>
<head>
<script type="text/javascript">
two =
parseInt(y); if (x
== y)
else
</script>
</head>
<body>
</body>
</html>
Output:
• 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>
Output:
LOOPS:-
• FOR LOOP:-
<html>
<body>
document.write("Current
Count : " + i );
document.write("<br />");
document.write("Loop stopped!");
</script>
</body>
</html>
Output:-
• While loop
<html>
<body>
var i = 0;
document.write("Starting
Loop ");
while (i < 10) {
</script>
</body>
</html>
• DO WHILE LOOP
<html>
<body>
<script type =
"text/javascript"> var p
= 0;
document.write("Starting Loop" +
"<br />"); do {
</script>
</body>
</html>
Output:
• FOR IN LOOP
<html>
<body>
<script
type="text/Javascript"
> var
languages={first:"c",
second:"P
yathon",
third:"java
",
fourth:
"php"}; for(i in
languages)
document.write(languages[i]+"<br>");
</script
</body>
</html>
Output:-
Grade and C (4M) P (4M) A (2M) Total ( 10 M) Dated Sign
Dated
Signature
of
Teacher