Article Information
This Javascript tutorial contains and tries to cover following subjects:
- Brief explanation of Javascript Do While
- Example to Javascript Do While
Articles tries to provide answer to following questions and issues:
- How to make DO WHILE loop in javascript
- How to make a loop without giving a fixed number to condition and executing code inside loop at least once regardless condition in Javascript?
Articles pre-requisites following information:
- General knowledge of variables in Javascript
- General knowledge of functions in Javascript
- General knowledge of HTML
Brief explanation of Javascript DO WHILE
In this Javascript Tutorial, we will try to cover Javascript DO WHILE loops subject to understand how to create a DO WHILE loop, why to use DO WHILE loop in js and differences between WHILE loop and DO WHILE loops.
If we recall earlier loops, FOR loops were being used to execute a code block for fixed and pre-defined of times. WHILE loops which also we covered in earlier Javascript Tutorial, were used to create loop for executing a block of code when we do not want to provide fixed number of execution cycle.DO WHILE loops are similar to the WHILE loops. Main difference of DO WHILE is to execute code block at least once regardless condition, After DO WHILE executes code, checks a condition.
DO WHILE loops are mainly used in cases that programmer wants to make a loop and execute a block and depending on condition keep looping through or not loop.
Lets look into syntax of DO WHILE loop:
do
{
...execute some codes...
} while ("condition");
As above syntax indicates, we enclose the code between curly braces which is required to be executed. Loop stars with "DO" keyword, and keeps itself loop through till condition in parenthesis following WHILE keyword evaluates TRUE.
Example to Javascript DO WHILE
In following javascript example, we will create create a Javascript "DO WHILE" loop, and execute a code block until condition provided returns TRUE.
HTML page shows result at the right pane.