While loop and do loopYou can also see PHP while loop, ASP will loop which have more or less similar purpose and to some extent similar syntax. Here is the basic syntax for while loop in JavaScript.
Note that here the expression is checked or evaluated before starting of the loop so if the condition returned is FALSE then the loop will never be executed.
Let us try with this example which prints
The above code will print 0 to 5 with a line break after each number.
Using breakWe can use break statement inside a while loop to come out of the loop. Here it is
do While LoopDo While loop is little different than while loop. Here the condition is checked at the end of the loop. So even if the expression is FALSE then also once the statements inside the loop will be executed. This is the basic difference between do while loop and while loop. Here is an example of Do While loop in JavaScript.
In the above code condition is checked at the end of the loop only. Here also we can use break statement to come out of the loop. Here is the example
Develop a pattern like this using while loop and for loopSubscribe to our YouTube Channel here
This article is written by plus2net.com team.
|
More on Loops and Variables |