Do While loop in Javascript

JS - Do While loop
Do While loop भी while loop की तरह ही होता है| बस यह first Time बिना condition check किए block of statement को execute करता है, और बाद में हर बार condition check करता है, यदि condition true होता है, तो do block के statements को execute करता है, और यदि condition false होता है तो loop stop हो जाता है|

Syntax :-


do{
	// do Statement(s);
	}while(condition);

Q :- Write a JavaScript program to print natural numbers using do while loop.


Source Code :

<!DOCTYPE html>
<html>
<head>
<title>Natural Numbers</title>
</head>
<body>
<script type="text/javascript">
	
var c=0, n=0;

n=window.prompt("Enter a Value",5);

n=parseInt(n);

document.write("N : "+n+"<br>");

do{
	c=c+1;
	document.write(c+" ");
}while(c<n);

</script>
</body>
</html>

Output :-

N : 5
1 2 3 4 5 

Q :- Write a javascript program to print even number with the help of do while loop.


Source Code :

<!DOCTYPE html>
<html>
<head>
<title>Even Numbers</title>
</head>
<body>
<script type="text/javascript">
	
var c=0, n=0 , e=0;

n=window.prompt("Enter a Value",5);

n=parseInt(n);

document.write("N : "+n+"<br>");

do{
	c=c+1;
	e=2*c;
	document.write(e+" ");
}while(c<n);

</script>
</body>
</html>

Output :-

N : 5
2 4 6 8 10 

Q :- Write a javascript program to print odd number with the help of do while loop.


Source Code :

<!DOCTYPE html>
<html>
<head>
<title>Odd Numbers</title>
</head>
<body>
<script type="text/javascript">
	
var c=0, n=0 , o=0;

n=window.prompt("Enter a Value",5);

n=parseInt(n);

document.write("N : "+n+"<br>");

do{
	c=c+1;
	o=(2*c)-1;
	document.write(o+" ");
}while(c<n);

</script>
</body>
</html>

Output :-

N : 5
1 3 5 7 9 

Q :- Write a JavaScript program to print the natural numbers in reverse order using do while loop.


Source Code :

<!DOCTYPE html>
<html>
<head>
<title>Natural Numbers in reverse order</title>
</head>
<body>
<script type="text/javascript">
	
var n=0;

n=window.prompt("Enter a Value",5);

n=parseInt(n);

document.write("N : "+n+"<br>");

do{
	document.write(n+" ");
	n=n-1;
}while(n>0);

</script>
</body>
</html>

Output :-

N : 5
5 4 3 2 1 

Q : Write a JavaScript program to print the even numbers in reverse order using do while loop.


Source Code :

<!DOCTYPE html>
<html>
<head>
<title>even numbers in reverse order</title>
</head>
<body>
<script type="text/javascript">
	
var n=0,e=0;

n=window.prompt("Enter a Value",5);

n=parseInt(n);

document.write("N : "+n+"<br>");

do{
	e=2*n;
	document.write(e+" ");
	n=n-1;
}while(n>0);

</script>
</body>
</html>

Output :-

N : 5
10 8 6 4 2 

Q : Write a JavaScript program to print the odd numbers in reverse order using do while loop.


Source Code :

<!DOCTYPE html>
<html>
<head>
<title>odd numbers in reverse order</title>
</head>
<body>
<script type="text/javascript">
	
var n=0,o=0;

n=window.prompt("Enter a Value",5);

n=parseInt(n);

document.write("N : "+n+"<br>");

do{
	o=(2*n)-1;
	document.write(o+" ");
	n=n-1;
}while(n>0);

</script>
</body>
</html>

Output :-

N : 5
9 7 5 3 1 

Q :- Write a JavaScript program to print and sum a square series of natural numbers using while loop.


Source Code :

<!DOCTYPE html>
<html>
<head>
<title>square series of natural numbers</title>
</head>
<body>
<script type="text/javascript">
	
var c=0, n=0, p=0, s=0;

n=window.prompt("Enter a Value",5);
n=parseInt(n);

document.write("N : "+n+"<br>");

do{
	c=c+1;
	p=c*c;
	s=s+p;
	document.write(p+" ");
}while(c<n);
document.write("<br>"+"Sum = "+s);

</script>
</body>
</html>

Output :-

N : 5
1 4 9 16 25 
Sum = 55 

Post a Comment

If you have any doubts,Please let me know

close