Summary of JavaScript syntax for looping statements

Source: Internet
Author: User

If conditional statement syntax:

if (condition) {

statements;

}

Understanding: In parentheses are the conditional parameters, the statements executed in curly braces.

Example code:if (1>2) {

Alert ("The world has gone mad!")

}

While loop statement syntax:

Initialize:

while (condition) {

statements;

}

Understanding: While loops are commonly known as loops, syntax is consistent with if, and different points are. When the condition evaluates to true, the statement in the curly braces repeats the loop. So the statement in the while Loop has a statement that affects the loop condition.

Example code:var count=1;

while (count<11) {

alert (count);

count++; Count is added, Loop 10 times after the condition evaluates to False, the loop terminates.

}

Do...while Loop statement Syntax:

Initialize:

do{

statements;

}while (condition);

Understanding: The difference between the Do...while loop and the while loop is that the statement in the curly braces is executed once, regardless of whether the condition evaluates to TRUE or FALSE.

Example code:var count=1

do{

alert (count);

count++

}while (COUNT<11); After looping 10 times, even if the value of Count is 11, the value of the conditional statement is false, and the statement in the curly braces is still executed once.

For Loop statement syntax:

For (initial condition; test condition; alter condition) {

Statements:

}

Understanding: The For loop differs from the above loop; It writes all the loop-related statements in parentheses. The benefit of this is to make the loop control structure clearer.

Example code: For(var count=1; count<11; count++) {

Alert (count)

}

Summary of JavaScript syntax for looping statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.