分享興趣,傳播快樂,增長見聞,留下美好!
親愛的您,這裡是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新學苑原創,部分圖片文字來自網路,如有侵權請聯繫。
文字&排版|李仕陽
審核|李煥