Home >Web Front-end >JS Tutorial >Summary of for loop usage in javascript_Basic knowledge

Summary of for loop usage in javascript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 19:11:231309browse

The most commonly used one is for (initial value of loop variable; loop condition; increment value) {statement;}
Example


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

The following are other usages
(1) Omit expression 1. At this time, the loop variable should be assigned an initial value before the for statement. Pay attention to the following; cannot be omitted.
Example:

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

(2) Omit expression 2, that is, the loop condition loop continues without termination, that is, expression 2 is always true.
Example:
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
](4) Omit expression 1, 3


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

(5) All three expressions are omitted



[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute it
]This loop will never terminate Carry out the same function of small while(true){}.
(6) Expression 1 can be the initial value of the loop variable, or it can be other expressions unrelated to the loop variable.


If you need to introduce external Js, you need to refresh to execute
]Expression 1 and expression 3 can also be a comma expression (containing more than one expression, separated by commas);
[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
逗号表达式按从左-右的顺序整个逗号表达式的值为最右边的值。

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

这个就是 


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

(7) 表达式可以是逻辑表达式,字符表达式,只要非false就可以执行循环 

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
从这些可以看到for与while循环比起来功能很强大. 
Statement:
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