Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript While statement

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge about how to use the JavaScript While sentence. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

The JavaScript While statement iterates through the values of iterable objects

The loop can execute a block of code all the time as long as the condition is true.

While cycle

The while loop loops the code block all the time, as long as the specified condition is true.

Grammar

While (condition) {

Block of code to be executed

}

Example

In the following example, the code in the loop will run over and over again, as long as the variable (I) is less than 10:

While (I < 10) {

Text + = "the number is" + I

ITunes +

}

If you forget to increment the variables used in the condition, the loop will never end. This can cause the browser to crash.

Do/While cycle

The do/while loop is a variant of the while loop. This loop executes the code block once before checking whether the condition is true, and then repeats the loop as long as the condition is true.

Grammar

Do {

Block of code to be executed

}

While (conditional)

Example

The following example uses a do/while loop. The loop executes at least once, even if the condition is false, because the code block executes before the conditional test:

Do {

Text + = "The number is" + I

ITunes +

}

While (I < 10)

Don't forget to increment the variables used in the condition, or the loop will never end!

Compare For with While

If you have read the previous chapter on loops, you will find that while loops are quite similar to for loops, where statements 1 and 2 can be omitted.

The loop in this example uses the for loop to extract the car brand from the cars array:

Example

Var cars = ["BMW", "Volvo", "Saab", "Ford"]

Var I = 0

Var text = ""

For (; cars [I];) {

Text + = cars [I] + "

"

ITunes +

}

The loop in this example uses the while loop to extract the car brand from the cars array:

Example

Var cars = ["BMW", "Volvo", "Saab", "Ford"]

Var I = 0

Var text = ""

While (cars [I]) {

Text + = cars [I] + "

"

ITunes +

}

These are all the contents of the article "how to use JavaScript While sentences". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report