Last active
March 7, 2016 20:35
-
-
Save csdear/21df6f3984fd4a158891 to your computer and use it in GitHub Desktop.
While Loop and Do
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| The while statement performs a simple loop. If the expression is falsy, then the loop will break. | |
| While the expression is truthy, the block will be executed. | |
| */ | |
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <p>This loops until i is less than 20</p> | |
| <button onclick="myWhileFunction()">Try it</button> | |
| <p id="demo"></p> | |
| <script> | |
| function myWhileFunction() { | |
| var text = ""; | |
| var i = 0; | |
| while (i < 20) { | |
| text += "<br> The number is " + i; | |
| i++; | |
| } | |
| document.getElementById("demo").innerHTML = text; | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| // Variant, do loop, code block will always be executed at least once | |
| <script> | |
| function myFunction() { | |
| var text = "" | |
| var i = 0; | |
| do { | |
| text += "<br>The number is " + i; | |
| i++; | |
| } | |
| while (i < 10) | |
| document.getElementById("demo").innerHTML = text; | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment