Home > Web Front-end > JS Tutorial > Analysis of the difference between javascript while statement and do while statement_javascript skills

Analysis of the difference between javascript while statement and do while statement_javascript skills

WBOY
Release: 2016-05-16 19:07:22
Original
1491 people have browsed it
while statement:
Copy code The code is as follows:

var i = 1;
while(i<10)
{
document.write(i);
i ;
}

do while statement:
Copy code The code is as follows:

var i = 1;
do
{
document.write(i);
i ;

}while(i<10);
From the above example, let’s analyze their two points Difference:

1. Because the while statement always detects the loop expression first, its loop body may not be executed once; while the do/while statement detects the loop expression at the bottom of the loop, so its loop The body will be executed at least once.
2. A semicolon should be added at the end of the do/while statement. This is because it ends with a loop condition instead of simply using curly braces to mark the end of the loop body.​
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template