29 August 2013

What are the difference between the while and do…while loop in computer programming?

To better understand the difference of while loop & do..While loop, you have to know the syntax of this loop.

The syntax of While loop is as follows:
while (variable<=end_value)

{

//Code to be executed writes here

}


while loop
While Loop

While loop Example in JavaScript:
1
2
3
4
5
6
7
8
<script type="text/javascript">
var1=0;
while (var1<=5)
{
document.write("The number is " + var1+"<br />");
var1++;
}
</script>

do while loop
Do While Loop

The syntax of do…while is as follows:
1
2
3
4
5
6
7
8
9
do

{

//code to be executed writes here

}

while (variable<=end_value);

do.. while Example in JavaScript:
1
2
3
4
5
6
7
8
9
<script type="text/javascript">
var1 = 0;
do
{
document.write("The number is " + var1+ "<br />");
var1++;
}
while (var1 <= 5)
</script>

  • So, In the case of the while loop, the condition is checked first, if the condition is false, the block will not be executed. 
  • On the other hand, In case of the do…while loop, the condition is checked after the block is executed; therefore the block is always executed at least once.

Programming: Computer Programming for Beginners: Learn the Basics of Java, SQL & C++ (Coding, C Programming, Java Programming, SQL Programming, JavaScript, Python, PHP)

Featured Post

How to Write PHP code and HTML code within a same file

A PHP file can have both PHP code and HTML code together. Any HTML code written outside of PHP <?php //php code here… ?> Code is ig...

Popular Posts