Practical No.2 Develop Javascript To Use Decision Making and Looping Statements
Practical No.2 Develop Javascript To Use Decision Making and Looping Statements
2
Develop Javascript To Use Decision Making And Looping
Statements
If-Else Statement:-
<html>
<body>
<script type="text/javascript">
var a
a=11
if(a%2==0)
document.write(a+ " is even number")
else
document.write(a+ " is odd number")
</script>
</body>
</html>
Output:-
If-Else Ladder:-
<html>
<body>
<script type="text/javascript">
var num = -12
if (num>0)
document.write(num+" is Positive number")
else if (num<0)
document.write(num+" is Negative number")
else
document.write(num+" number is zero")
</script>
</body>
</html>
Output:-
Break:-
<html>
<body>
<script type ="text/javascript">
var i;
for (i=0;i<=10;i++)
{
if (i==6)
{
break
}
document.write("<br>"+i)
}
</script>
</body>
</html>
Output:-
Continue:-
<html>
<body>
<script type ="text/javascript">
var a;
for (a=1;a<=7;a++)
{
if (a==4)
{
continue
}
document.write("<br>"+a)
}
</script>
</body>
</html>
Output:-
For Loop:-
<html>
<body>
<script type=" text/javascript">
var n
for(n=1;n<=10;n++)
document.write(n+ "<br>")
</script>
</body>
</html>
Output:-
While loop :-
<html>
<body>
<script type ="text/javascript">
var i=1
while(i<=10)
{
if(i%2==0)
document.write(i+ "<br>")
i++;
}
</script>
</body>
</html>
Output:-
Do-while Loop:-
<html>
<body>
<script type ="text/javascript">
var i=1
var n=10
do
{
document.write(i+"<br>")
i++;
}
while(i<=n)
</script>
</body>
</html>
Output:-
Switch Case:-
<html>
<body>
<script type= "text/javascript">
var num = 5
switch(num)
{
case 1:
document.write("One")
break;
case 2:
document.write("Two")
break;
case 3:
document.write("Three")
break;
case 4:
document.write("Four")
break;
case 5:
document.write("Five")
break;
}
</script>
</body>
</html>
Output:-