Javascript For Loop Control Statement: Javascript Programming Programming Languages Web Development
Javascript For Loop Control Statement: Javascript Programming Programming Languages Web Development
The ‘for‘ loop is the most compact form of looping. It includes the following three important parts −
The loop initialization where we initialize our counter to a starting value. The initialization statement is
executed before the loop begins.
The test statement which will test if a given condition is true or not. If the condition is true, then the code
given inside the loop will be executed, otherwise the control will come out of the loop.
The iteration statement where you can increase or decrease your counter.
For loop is primarily preferred when the number of iterations are well known in advanced.
Syntax:
1 <html>
2 <body>
3
4 <script type="text/javascript">
5 <!--
6 var count;
7 document.write("Starting Loop" + "<br />");
8
9 for(count = 0; count < 10; count++){
10 document.write("Current Count : " + count );
11 document.write("<br />");
12 }
13
14 document.write("Loop stopped!");
15 //-->
16 </script>
17
18 <p>Set the variable to different value and then try...</p>
19 </body>
20</html>
Output –
1 Starting Loop
2 Current Count : 0
3 Current Count : 1
4 Current Count : 2
5 Current Count : 3
6 Current Count : 4
7 Current Count : 5
8 Current Count : 6
9 Current Count : 7
10Current Count : 8
11Current Count : 9
12Loop stopped!
13Set the variable to different value and then try...