<meta charset= "UTF-8" >
<title></title>
<script type= "Text/javascript" >
/*
* Output consecutive digits to the page
*/
document.write (1);
var n=1;
document.write (n++ + "<br/>");
/*
* Loop statement:
* Repeated execution of a piece of code multiple times through a looping statement
* While loop:
* Syntax: \
* while (conditional expression) {
* Statement ...
*
* }
* While statements are executed,
* Evaluate the expression of the day sword first
* If the value is true, the loop body is executed,
* After the completion of the loop body, continue to judge the expression
* If true, resumes execution of the loop body and so on
* Terminates the loop if the value is Fasle
*/
/*
* while (true) {
* Alert (n++);
* }
* A loop like this that writes a conditional expression to true, called a dead loop
* The loop will not stop unless the browser is closed and the dead loop is used with caution in development
* You can use break to terminate the loop
*
*/
1. Create a variable to initialize
var i = 0;
2. Set a conditional expression in a loop
while (I<10) {
alert (i);
i++
3. Define an update expression, each time the initialization variable is updated
}
/*
* Do...while cycle,,
* Grammar
* do{
* Statement ...
*}while (conditional expression);
* Execution Process:
* Do...while statement executes, the loop body is executed first,
* After the completion of the loop body, the conditional expression after the while is judged
* If the result is true, continue execution of the loop body, after execution, continue to judge and so on
*
* Actually these two statements function similarly, the difference is that the while is the first to judge after the execution
* and Do...while will be executed first and then judged
* Do...while can ensure that the loop body is executed at least once
* While not
*/
do{
document.write (i++ + "<br/>");
}while (i <= 10);
//
</script>
<body>
</body>
If loop statements in JavaScript