分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来话说前端29-JavaScript之循环,欢迎您的访问。
Share interests, spread happiness, increase knowledge, and leave a good legacy!
Dear you, this is The LearningYard Academy.
Today Xiaobian brings you Knowledge set| Flexsim modeling and simulation of warehousing systems, welcome to your visit.
为什么我们要是用循环:
在编写代码时,我们常常希望反复执行同一段代码。这个时候我们就可以使用循环来完成这个功能,这样就用不着重复地写若干行相同的代码。
例如:我想输出 100行 我爱你。(在这里就是一直重复输出我爱你,但是我们复制粘贴的话就太麻烦了)
这个时候我们就可以利用循环,帮我们做这种重复的事情。
Why do we use loops:
When writing code, we often want to execute the same code repeatedly. At this time, we can use the loop to complete this function, so that we don't have to write several lines of the same code repeatedly.
For example: I want to output 100 lines. I love you. (Here, I just keep repeating that I love you, but it's too much trouble if we copy and paste it.)
At this time, we can make use of the cycle to help us do this repetitive thing.
js的循环分为三种:while循环、do-while循环、for循环
循环要素:初始化变量、条件表达式、执行代码、自身改变。
while循环常用于那种不知道循环次数是多少的情况,比如让用户循环输入一个整数,直到输入某个特殊的字符为止,你根本没法直到这个循环会进行的次数。
语法:
while(条件表达式){
当条件表达式为布尔值true时要执行的语句块
}
There are three kinds of loops of s: while loop, do-while loop and for loop.
Loop elements: initialization variables, conditional expressions, execution codes, and self-changes.
While loop is often used in situations where you don't know the number of loops. For example, you can't know the number of times this loop will be performed until a special character is entered.
Grammar:
While (conditional expression) {
A statement block to be executed when the conditional expression is boolean true.
}
do-while与while循环的不同之处在于:它先执行循环中的语句,然后再判断表达式是否为真, 如果为真则继续循环;如果为假, 则终止循环。因此, do-while循环至少要执行一次循环语句。
语法:
do{
条件表达式为true时执行的语句块
}while(条件表达式)
Do-while loop differs from while loop in that it first executes the statements in the loop, then determines whether the expression is true, and if it is true, it continues the loop; If false, the loop is terminated. Therefore, the do-while loop must execute the loop statement at least once.
Grammar:
do{
Statement block executed when the expression is true
}while (conditional expression)
for循环多用于循环次数比较明确的情况,就是那种一眼就能看出来要循环几次,比较直观,for循环的第一句包含变量的初始化 结束循环的条件以及每次更新的值,循环体内部才执行真正要做的事情。比如for(n=1;n<10;n++),一般用于遍历,是一种比较使用的循环方式。
语法:
for(变量初始化;条件表达式; 变量更新){
条件表达式为true时执行语句块
}
For loop is mostly used when the number of loops is clear, that is, it can be seen at a glance how many times to loop, which is more intuitive. The first sentence of the for loop contains the conditions for the initialization of variables to end the loop and the value updated each time, so that what really needs to be done is executed inside the loop body. For example, for(n=1; n<10; ++), which is generally used for traversal, is a circular way of comparative use.
Grammar:
For (variable initialization; Conditional expression; Update) {
Execute the statement block when the expression is true.
}
循环嵌套是指在一个循环语句中再定义一个循环语句的语法结构,例如在for循环语句中,可以再嵌套一个for循环,这样的for循环语句我们称之为双重for循环。
外层循环一次,内层for循环全部执行完成再进行外层的一次循环;
注意:双重for循环就是在一个for循环里在嵌套另一个for循环,即两个循环嵌套,还有多重循环嵌套的情况,但用的比较少,一般常用的是两个循环嵌套,很少使用多重循环,因为for循环多了会导致代码执行的效率低,而且容易死机,多循环中的总循环次数是相乘的,嵌套几次后几何级数增长,因此通常情况下在循环多次嵌套时,我们是不推荐使用使用的。
Loop nesting refers to defining the syntax structure of another loop statement in a loop statement. for example, in a for loop statement, another for loop can be nested. Such a for loop statement is called double for loop.
The outer layer is circulated once, and the inner layer for circulation is completely executed, and then the outer layer is circulated once;
Note: Double for loop is to nest another for loop in one for loop, that is, two loops are nested, and multiple loop is nested. However, it is seldom used. Generally, two loops are nested, and multiple loop is seldom used, because too many for loops will lead to low efficiency of code execution, and it is easy to crash. The total number of loops in multiple loops is multiplied, and geometric progression increases after nesting several times. Therefore, we usually do not recommend using it when loops are nested many times.
今天的分享就到这里了。如果您对今天的文章有什么独特的想法,欢迎评论留言,让我们相约明天,祝您今天过得开心快乐!
That's it for today's sharing. If you have any unique ideas for today's article, please leave a comment, let us meet tomorrow, I wish you a happy day!
翻译:Google翻译
本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。
文字&排版|李仕阳
审核|李焕