Home > Web Front-end > JS Tutorial > JavaScript For Loops and While Loops

JavaScript For Loops and While Loops

不言
Release: 2018-04-02 17:05:36
Original
2460 people have browsed it

This article will share with you about JavaScript For loop and While loop. Friends who need help can refer to it

1. For loop

A for loop will be executed repeatedly. Until the specified loop condition is fasle. The for loop in JavaScript is very similar to the for loop in Java and C.

for ([initialExpression]; [condition]; [incrementExpression])
statement

1.1 Execution method

(1) If there is an initialization expression initialExpression, which will be executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of arbitrary complexity. This expression can also declare variables.

(2) Calculate the value of condition expression. If the value of condition is true, the statement in the loop will be executed. If the value of condition is false, the for loop terminates . If the entire condition expression is omitted, the value of condition will be considered true.

(3) The statement in the loop is executed. If you need to execute multiple statements, you can use blocks ({ ... }) to wrap these statements.

(4) If there is an update expression incrementExpression, execute it, and then the process returns to step (2).

2. Example

2.1 Output 1-100

JavaScript For Loops and While Loops

2.2 Sum 1+2+.. .+99+100

JavaScript For Loops and While Loops

2.3 Sum 1-100 odd numbers

JavaScript For Loops and While Loops

2.4 Sum the even numbers 1-100

JavaScript For Loops and While Loops

##2.5 Output the numbers 1-100 that are divisible by 3 or divisible by 5

JavaScript For Loops and While Loops

2.6 Output the multiplication table

JavaScript For Loops and While Loops

JavaScript For Loops and While Loops

3. Traversing the array

JavaScript For Loops and While Loops##4. While loop

4.1 Syntax

while (condition) {

statement
}

4.2 Description

(1) condition

conditional expression, which is evaluated before each loop . If it evaluates to

true

, the statement will be executed. If the evaluation is false, will jump out of the while loop and execute the subsequent statement . (2)statement

As long as the conditional expression evaluates to

true

, the statement will always be executed. To execute multiple statements in a loop, you can use block statements ({ ... }) to wrap multiple statements. 4.3 Note

Use the

break

statement to stop the loop before the condition evaluates to true. 5. Example

5.1 Output the number from 1-100 that is divisible by 7 or has the number 7

JavaScript For Loops and While Loops Read more

Reference article Learn more about for loops in JavaScript


The above is the detailed content of JavaScript For Loops and While Loops. For more information, please follow other related articles on the PHP Chinese website!

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