forked from GitbookIO/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request GitbookIO#76 from sumn2u/master
an example to show do while loop
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Do...While Loop | ||
|
||
The do...while statement creates a loop that executes a specified statement until the test condition evaluates to be false. The condition is evaluated after executing the statement. | ||
Syntax for do... while is | ||
|
||
```javascript | ||
do{ | ||
// statement | ||
} | ||
while(expression) ; | ||
``` | ||
|
||
Lets for example see how to print numbers less than 10 using `do...while` loop: | ||
|
||
``` | ||
var i = 0; | ||
do { | ||
document.write(i + " "); | ||
i++; // incrementing i by 1 | ||
} while (i < 10); | ||
``` | ||
|
||
>***Note***: `i = i + 1` can be written `i++`. | ||
|
||
{% exercise %} | ||
Using a do...while-loop, print numbers between less than 5. | ||
{% initial %} | ||
var i = 0; | ||
{% solution %} | ||
var i = 0; | ||
do { | ||
i++; // incrementing i by 1 | ||
} while (i < 5); | ||
{% endexercise %} |