话说前端29-JavaScript之循环

2022年10月22日23:12:18 科技 1307

话说前端29-JavaScript之循环 - 天天要闻

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是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!

话说前端29-JavaScript之循环 - 天天要闻

翻译:Google翻译

本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。

文字&排版|李仕阳

审核|李焕

科技分类资讯推荐

坐稳“世界第一”,海信100吋大屏重新定义观赛体验 - 天天要闻

坐稳“世界第一”,海信100吋大屏重新定义观赛体验

2025年FIFA世俱杯即将于6月15日在美国拉开战幕,梅西领衔的迈阿密国际首战对决开罗国民,全球32支顶级俱乐部竞逐10亿美金史诗级奖金。在这场足球狂欢中,作为本届世俱杯首个全球官方合作伙伴,海信将以“100吋电视世界第一”之姿亮相国际舞台,向全球球迷展示中国显示技术的顶尖实力。百吋王者的全球登顶之路根据权威机构O...
vivo X Fold5官宣6月25日发布:4.Xmm超薄机身搭载6000mAh电池+潜望长焦,重新定义折叠屏旗舰 - 天天要闻

vivo X Fold5官宣6月25日发布:4.Xmm超薄机身搭载6000mAh电池+潜望长焦,重新定义折叠屏旗舰

作者 / 丛杰出品 / 科技Nice(Tech-Nice)vivo近日正式宣布,旗下新一代折叠屏旗舰vivo X Fold5将于6月25日19:00全球发布。凭借4.Xmm展开厚度、6000mAh超大电池及5000万像素蔡司潜望长焦镜头,该机有望刷新“全球最轻薄折叠屏手机”纪录,并首次实现“轻薄机身+顶级配置”的平衡。超薄机身+6000mAh电池:打破行业技
OPPO失速,产品线“左右互搏”,尚未站稳高端化市场 - 天天要闻

OPPO失速,产品线“左右互搏”,尚未站稳高端化市场

证券之星 李若菡近日,调研机构Counterpoint公布了一季度欧洲折叠屏手机销量份额情况,OPPO销量下滑态势尤为明显,相较去年同期出现了63%的跌幅。证券之星注意到,除欧洲市场外,OPPO中国市场及全球市场战绩不佳,一季度出货量出现下
联想拯救者R27qe 200Hz显示器:2K高刷小金刚,游戏办公两不误 - 天天要闻

联想拯救者R27qe 200Hz显示器:2K高刷小金刚,游戏办公两不误

前言对于大多数游戏党来说,2K显示器是最优选择,相较于1080P分辨率,2K分辨率能够呈现出更细腻、更清晰的画面,许多3A大作和新推出的游戏在开发时,就充分考虑了2K甚至更高分辨率的显示效果。虽然4K显示器分辨率更高画面更细腻,但对硬件尤其是显卡要求太高,需要花费高额成本升级电脑配置,所以现在接2K高刷分辨率是更好...
值得推荐的助听器有哪些?町石、创维、未来客助听器这三个怎么样? - 天天要闻

值得推荐的助听器有哪些?町石、创维、未来客助听器这三个怎么样?

在老龄化加速与听力健康需求激增的背景下,骨传导助听器作为非侵入式听力解决方案,正以技术迭代重构市场格局。2025 年国内首个助听器验配管理国家标准(GB/T 44994-2024)的实施,标志着行业进入规范化竞争阶段。在此浪潮中,未来客 MJK-B6、创维 P1 悦享版、町石 TC1600 三款产品凭借差异化技术路径,成为消费级市场的焦...
非IP全光纤架构!MediaComm美凯助力和田市城市运行管理服务指挥中心智慧建设 - 天天要闻

非IP全光纤架构!MediaComm美凯助力和田市城市运行管理服务指挥中心智慧建设

在和田市治理体系的核心地带,一座“城市大脑”正日夜不息地运转——城市运行管理服务指挥中心。这里汇聚城市脉搏的每一次跳动,以多维数据整合分析能力,重塑城市治理的精度与速度,成为智慧和田最坚实的神经中枢。 如此高规格的神经中枢,MediaComm美凯的非IP全光纤架构的会议室集群解决方案,如同为城市数据铺设了一条无...
团簇离子抛光项目亮相“创立方”路演获资本青睐 - 天天要闻

团簇离子抛光项目亮相“创立方”路演获资本青睐

极目新闻通讯员李婧 曾笑天6月12日,在由湖北省人才事业发展中心主办的创立方·2025年第四期博士后和留学回国人才创业路演活动上,武汉大学团簇离子束团队带来的“用于EUV光学元件原子级制造的团簇离子抛光技术研发及产业化”项目成为全场焦点。该项目凭借在半导体关键设备领域的突破性创新,吸引了点亮投资等10余家投资机...