JavaScript while បញ្ចប់កម្មវិធីមួយដង
- 上一页 var
- 下一页 break
- 返回上一层 Manwal ng Statement ng JavaScript
ការអង្គុយនិងការប្រើប្រាស់
while បញ្ចប់កម្មវិធីមួយដង បន្ទាប់មកនៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ ខ្លួនឯងចង់នៅក្នុងរង្វង់នេះ
បើសិនជាជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ រង្វង់នេះនឹងបន្តដំណើរការ និងអត់នៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាមិនត្រឹមត្រូវ
JavaScript គាំទ្របញ្ចប់កម្មវិធីមួយដង បន្ទាប់មកនៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ ខ្លួនឯងចង់នៅក្នុងរង្វង់នេះ
- for - បញ្ចប់កម្មវិធីមួយដង បន្ទាប់មកនៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ ខ្លួនឯងចង់នៅក្នុងរង្វង់នេះ
- for/in - បញ្ចប់កម្មវិធីមួយដង បន្ទាប់មកនៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ ខ្លួនឯងចង់នៅក្នុងរង្វង់នេះ
- for/of - បញ្ចប់កម្មវិធីមួយដង បន្ទាប់មកនៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ ខ្លួនឯងចង់នៅក្នុងរង្វង់នេះ
- while - បញ្ចប់កម្មវិធីមួយដង បន្ទាប់មកនៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ ខ្លួនឯងចង់នៅក្នុងរង្វង់នេះ
- do/while - បញ្ចប់កម្មវិធីមួយដង បន្ទាប់មកនៅពេលជម្លោះត្រូវបានគេចាត់ទុកជាត្រឹមត្រូវ ខ្លួនឯងចង់នៅក្នុងរង្វង់នេះ
Tip:Use the break statement to exit the loop, and use the continue statement to skip a value in the loop.
Example
The loop code block will be executed as long as the variable (i) is less than 5:
var text = ""; var i = 0; while (i < 5) { text += "<br>The number is " + i; i++; }
More TIY examples are available at the bottom of the page.
Syntax
while (condition) { code block to be executed }
Parameter Value
Parameter | Description |
---|---|
condition |
Required. Defines the condition for running the loop (code block). If it returns true, the loop will restart, and if it returns false, the loop will end. Note:If the condition is always true, the loop will never end. This will crash your browser. Note:If you use a variable with a condition, initialize it before the loop and increment it within the loop. If you forget to increase the variable, the loop will never end. This will also crash your browser. |
Technical Details
JavaScript Version: | ECMAScript 1 |
---|
More Examples
Example
Loop through the array index to collect car names from the cars array:
var cars = ["BMW", "Volvo", "Saab", "Ford"]; var text = ""; var i = 0; while (i < cars.length) { text += cars[i] + "<br>"; i++; }
Example Explanation:
- First, we set a variable before the loop starts (var i = 0;)
- Then, we define the condition for the loop to run. As long as the variable is less than the length of the array (i.e., 4), the loop will continue
- The variable is incremented by one (i++) each time the loop is executed
- Once the variable is no longer less than 4 (the length of the array), the condition is false, and the loop ends
Example
Loop through array index backward:
var cars = ["BMW", "Volvo", "Saab", "Ford"]; var text = ""; var len = cars.length; while (len--) { text += cars[len] + "<br>"; }
Example
Use break statement - loop code block, but exit the loop when the variable i equals "3":
var text = ""; var i = 0; while (i < 5) { text += "<br>The number is " + i; i++; if (i == 3) { break; } }
Example
Use continue statement - loop code block, but skip the value "3":
var text = ""; var i = 0; while (i < 5) { i++; if (i == 3) { continue; } text += "<br>The number is " + i; }
Browser Support
Statement | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
while | Support | Support | Support | Support | Support |
Related Pages
JavaScript Tutorial:JavaScript While 循环
JavaScript 参考手册:JavaScript do ... while 语句
JavaScript 参考手册:JavaScript for 语句
JavaScript 参考手册:JavaScript break 语句
JavaScript 参考手册:JavaScript continue 语句
- 上一页 var
- 下一页 break
- 返回上一层 Manwal ng Statement ng JavaScript