話說前端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餘家投資機...