Home Web Front-end JS Tutorial Differences in usage of javascript loop statements while, do-while, for-in, and for_javascript skills

Differences in usage of javascript loop statements while, do-while, for-in, and for_javascript skills

May 16, 2016 pm 05:55 PM
loop statement

The only difference between the first two is that the order of looping and judgment is different. do-while loops one more time than while, so I won’t give an example.
I believe everyone is familiar with the for loop. Let’s look at the sentence for-in.
This is actually for arrays. The initialization of arrays in js is also quite strange. For example, we write it in the script node: (Also note the initialization of the array, using square brackets)


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

for in Example 2

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
javascrpt for
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Explanation:
The step value of the for loop starts from i=0.
As long as i is less than or equal to 5, the loop will continue to run.
Every time the loop loops, i will accumulate by 1.

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Explanation:
i is equal to 0.
The loop will run first.
Each time through the loop, i will accumulate by 1. javascrpt while


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Explanation:
i is equal to 0. The loop will continue running when i is less than or equal to 5. Every time the loop runs, i will accumulate by 1. Javascript sample code explanation: This Javascript sample uses the do...while loop statement. The loop statement allows one or several lines of code to be repeatedly executed. do is followed by the code for repeated execution, and while is followed by the condition for terminating the loop. In this Javascript example, let a variable be i, the initial value of i is 0, i means that the value of i will be increased by 1 after each repeated execution, and the loop termination condition is while (i <= 5), that is to say, once i If the value is greater than 5, the loop will be terminated. In this example, the statement that repeats the loop is the document.write statement in the while loop.

From the above examples we can see the difference between js for, for in, while, do while.
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of loop statements in C++ Detailed explanation of loop statements in C++ Aug 22, 2023 am 11:52 AM

C++ is an efficient programming language with powerful functions and a wide range of applications. Loop statements are one of the most important parts of C++. C++ provides several loop statements to make it easier for programmers to iterate data. This article will introduce the loop statement in C++ in detail. 1. for loop A for loop is an iterative statement that allows programmers to easily perform a specified operation repeatedly. The basic syntax of a for loop is as follows: for(initialization;condi

How to solve Python loop statement error? How to solve Python loop statement error? Jun 25, 2023 am 10:15 AM

The Python loop statement is very basic but also very important. You can use it to repeatedly execute a piece of code until a certain condition is met. Commonly used loop statements in Python include for loops and while loops. Even though loop statements are very simple, you can still encounter various loop errors while writing code. In this article, I will discuss several common loop errors and their solutions. 1. Language Error Check for spelling errors and symbol errors such as missing colons and parentheses. In Python, these errors

What loop statements are there in java? What loop statements are there in java? Jun 21, 2023 pm 03:42 PM

Java's loop statements: 1. while loop, the syntax is "while (Boolean expression) {loop body;}", the loop body is executed when the condition is true; 2. do-while loop, the syntax is "do{loop body; }while (Boolean expression);", even if the condition is not met, it will be executed at least once; 3. for loop, the syntax is "for (expression 1; expression 2; expression 3) {loop body;}"; expression Expression 1 is to assign a value to a variable, expression 2 is a loop condition, and expression 3 is to change the value of a variable.

How to use loop statement function in C++? How to use loop statement function in C++? Nov 18, 2023 pm 05:13 PM

How to use loop statement function in C++? C++ is an object-oriented programming language with powerful loop functions that help developers perform repetitive tasks more efficiently. The loop statement function can create a loop in the code to execute the same piece of code multiple times, thereby simplifying the programming process and improving the readability of the code. In C++, we use loop statement functions to perform different types of loops, including for loops, while loops, and do-while loops. different loop classes

How to use loop statements in Python? How to use loop statements in Python? Jun 03, 2023 pm 08:51 PM

Python is a high-level programming language that is widely used in fields such as data science, artificial intelligence, web development, and automation. The loop statement is one of the most basic control structures in Python programming, which allows a program to repeatedly execute a block of code until a termination condition is met. In this article we will introduce two types of loop statements in Python - for loop and while loop, and provide some examples to demonstrate their usage. 1. for loop statement The for loop statement in Python is used to traverse a

How to use loop structure in PHP programming? How to use loop structure in PHP programming? Jun 12, 2023 pm 01:09 PM

In PHP programming, the loop structure is a very important control flow structure. Through the loop structure, we can make the code repeatedly execute a specific block of code, which can be a statement or a group of statements. The loop structure allows us to solve some repetitive tasks with less code and can reduce the occurrence of code logic errors. This article will introduce how to use loop structures in PHP programming. for loop The for loop is one of the most used loop structures in PHP programming. As the name suggests, the for loop loops within a certain range.

Learn to use conditionals and loop statements in Golang Learn to use conditionals and loop statements in Golang Dec 23, 2023 pm 01:19 PM

To master conditional statements and loop statements in Golang, you need specific code examples. In Golang, conditional statements and loop statements are a very important part of the program. Conditional statements are used to determine the execution flow of the program, while loop statements are used to repeatedly execute a section of code. This article will introduce conditional statements and loop statements in Golang in detail and provide specific code examples. Conditional Statements Conditional statements are used to execute different blocks of code depending on whether a condition is true or false. In Golang, conditional statements include if statements, if-els

Mastering Python conditionals and loops is a key programming skill Mastering Python conditionals and loops is a key programming skill Jan 20, 2024 am 09:12 AM

Essential programming skills: Master conditional statements and loop statements in Python In today's information age, programming has become an increasingly important skill. Whether in fields such as scientific research, data analysis, software development, or artificial intelligence, programming plays a vital role. Among many programming languages, Python is becoming more and more popular among developers for its simplicity, ease of learning and powerful functions. In Python, conditional statements and loop statements are indispensable basic tools when writing programs. This article will introduce these two in detail.

See all articles