Looping Statement
Looping Statement in
Set of instructions given to the compiler to execute set of statements until condition becomes false is called loops. The basic purpose of loop is code repetition.
The way of the repetition will be forms a circle that's why repetition statements are called loops. Some loops are available In JavaScript which are given below.
- while loop
- for loop
- do-while
while loop
When we are working with while loop always pre-checking process will be occurred. Pre-checking process means before evolution of statement block condition part will be executed. While loop will be repeats in clock wise direction.
Syntax
while (condition) { code block to be executed }
Example of while loop
<script> var i=10; while (i<=13) { document.write(i + "<br/>"); i++; } </script>
Result
10 11 12 13
do-while loop
In implementation when we need to repeat the statement block at least 1 then go for do-while. In do-while loop post checking of the statement block condition part will be executed.
syntax
do { code to be executed increment/decrement } while (condition);
Example of do-while loop
Example
<script> var i=11; do{ document.write(i + "<br/>"); i++; }while (i<=15); </script>
Result
11 12 13 14 15
for Loop
For loop is a simplest loop first we initialized the value then check condition and then increment and decrements occurred.
Steps of for loop
Syntax
for (initialization; condition; increment/decrement) { code block to be executed }
Example of for loop
Example
<script> for (i=1; i<=5; i++) { document.write(i + "<br/>") } </script>
Result
1 2 3 4 5