0% found this document useful (0 votes)
10 views

ControlStructure&Looping

Uploaded by

vinothchintam12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

ControlStructure&Looping

Uploaded by

vinothchintam12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

if(con1)

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");
}
}
} */

//x=parseInt(prompt("Enter number 1 to 3"));


x=prompt("Enter a character to find vowel or not");
switch(x)
{
case 'a':
alert("Hi");
break;
case 'e':
alert("Bye");
break;
case 'i':
alert("Welcome");
break;
case 'o':
alert("xyz");
break;
case 'u':
alert("Welcome");
break;
default:
alert("Invalid case Try again");
}

//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

expr1 :- Initialization (Loop start from where)


expr2 :- How many times execute(End to where)
expr3 :- Upgradation(increment or decrement)

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>

<!-- Example to get& show input dynamic -->


<html>
<script>
names=[];
ages=[];
sals=[];
document.write("Enter 5 employee details");
for(i=0;i<5;i++)
{
names[i]=prompt("enter name");
ages[i]=parseInt(prompt("Enter age"));
sals[i]=parseFloat(prompt("Enter salary"));
}

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>

You might also like