ControlStructure&Looping
ControlStructure&Looping
statememt
else
statement
if(con1)
{
---
---
---
statements;
}
else
{
statements
}
*****************************
if - elseif -else
if(con1)
{
statement
}
else if(con2)
{
statement
}
else if(con3)
statement
else
statement
****************************
if(con1)
if(con2)
statement
else
statement
else
statement
switch-case
***********
expr
switch(expr)
{
case 1:
statements
break;
case 2:
statements;
break;
case 3:
statements;
break;
default:
statements
}
Example:-
********
<html>
<head>
<script>
/*cty=prompt("enter your city");
age=parseInt(prompt("Enter age"));
if(cty=="mdu" && age>18)
alert("Valid");
else
alert("Invalid");*/
/*
x=1000;
y=200;
z=950;
if(x>y && x>z)
alert("X is greater");
else if(y>z)
alert("Y is greater");
else
alert("Z is greater"); */
/*x="mdu";
y="aaa";
z=25;
if(x=="mdu")
if(y=="aaa")
{
if(z>20)
{
alert("Valid");
}
else
{
alert("Invalid");
}
}
} */
//names=["raja","kumar","mani","ram","ratha"];
</script>
</head>
<body>
</body>
</html>
//Looping in javascript
A task or set of task execute again and again
Types of loop:-
**************
for loop
while loop
do-while loop
for-each loop (enhanced loop)
Syntax:-
Each loop meet 3 expression
for loop
********
for(expr1;expr2;expr3)
{
-----
-----
-----
}
while loop
**********
expr1;
while(expr2)
{
------
------
------
expr3;
}
expr1;
do
{
-----
-----
-----
expr3;
}while(expr2);
for(x in obj)
{
print(x)
}
<script>
names=["raja","kumar","mani","ram","ratha"]; //array
/*
document.write("<font color='green'>"+names+ "</font><br>")
for(i=0;i<3;i++)
{
document.write("<font color='green'>"+names[i]+ "</font><br>")
}
for(x in names)
{
document.write("<font color='green'>"+names[x]+ "</font><br>")
}
*/
/*
i=0;
while(i<5)
{
document.write(names[i]+"<br>");
i++;
}
*/
i=0;
do
{
alert(names[i]);
i++;
}while(i<5);
</script>
i=0;
document.write("<table border=1>");
while(i<5)
{
document.write("<tr>");
document.write("<td>"+names[i]+"</td><td>"+ages[i]+"</td><td>"+sals[i]+"</td></
tr>");
i++;
}
</script>
document.write("</table>");
<body>
</body>
</html>