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 loop Example in JavaScript:
The syntax of do…while is as follows:
do.. while Example in JavaScript:
Programming: Computer Programming for Beginners: Learn the Basics of Java, SQL & C++ (Coding, C Programming, Java Programming, SQL Programming, JavaScript, Python, PHP)
The syntax of While loop is as follows:
while (variable<=end_value)
{
//Code to be executed writes here
}
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 |
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)
No comments:
Post a Comment