JavaScript Loop : For Loop in JavaScript (Conditional Statement)
Note :
- For Single Line of Code - Opening and Closing braces are not needed.
- There can Exist For Loop without body.
- Initialization , Incrementation and Condition steps are on same Line.
- Like While loop , For Loop is Entry Controlled Loop.[i.e conditions are checked if found true then and then only code is executed ]
For Loop Syntax :
for(initialization ; conditional ; increment) { -------- -------- //body -------- -------- }
Live Example :
<html> <body> <script type="text/javascript"> var i=0; for (i=0;i<=5;i++) { document.write("The number is " + i); document.write("<br />"); } </script> </body> </html>
Output :
The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5