JavaScript conditional statements and loop statements

Source: Internet
Author: User

1. Conditional statements

(1). If...else

if (judging condition) {

Statement 1

}else{

Statement 2

}

(2). If...else If....else

if (judging condition) {

Statement 1

}else if (judging condition) {

Statement 2

}else{

Statement 3

}

(3). If

if (judging condition) {

}

If...else If....else and If...else execute only one statement at a time, and then jump out of the statement after execution.

2. Looping statements

Loop statements are commonly used with do...while and for and while with for in.

All loop statements are executed until the condition is met, as long as the loop condition is met.

(1). Do...while statements

Grammar:

Do

{

Statement 1

}while (cyclic condition)

var v = 0;

var a = 0;

Do

{

A+=v;

v++;

}while (i<=100)

Note: If the Do loop is not added with v++, then this is a dead loop, so the do...while easily forms a dead loop.

(2). While statement

Grammar:

while (loop condition)

{

}

While statements are similar to Do...while, the only difference is that while executing the statement, the statement in the while loop executes at least 0 times and Do...while executes at least once.

(3). For statement

Grammar:

for (initial expression; loop condition; action expression)

{

Statement 1

}

var v = 0;

for (i=0;i>100;i++)

{

V+=i;

}

Note: The For statement is not easy to form a dead loop because it has an action expression.

(4). For in

Note: For in can only loop arrays and objects, and for in does not form a dead loop.

Grammar:

for (variable name in array)

{

Statement 1

}

JavaScript conditional statements and loop statements

Related Article

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.