Toán tử gia số [
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
09] gia tăng [thêm một vào] toán hạng của nó và trả về giá trị trước hoặc sau gia số, tùy thuộc vào vị trí đặt toán tửThử nó
cú pháp
x++
++x
Sự miêu tả
Nếu sử dụng hậu tố, với toán tử đứng sau toán hạng [ví dụ:
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
10], toán tử gia tăng sẽ tăng dần và trả về giá trị trước khi tăng dầnNếu sử dụng tiền tố, với toán tử đứng trước toán hạng [ví dụ:
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
11], toán tử gia tăng sẽ tăng dần và trả về giá trị sau khi tăng dầnToán tử gia tăng chỉ có thể được áp dụng trên toán hạng là tham chiếu [biến và thuộc tính đối tượng; i. e. mục tiêu phân công hợp lệ]. Bản thân
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
11 đánh giá thành một giá trị, không phải tham chiếu, vì vậy bạn không thể xâu chuỗi nhiều toán tử gia số lại với nhau Ví dụ xuất hàng lần lượt theo danh sách hoặc chỉ chạy mã giống nhau cho từng số từ 1 đến 10
Vòng lặp là một cách để lặp lại cùng một mã nhiều lần
Vòng lặp for…of và for…in
Một thông báo nhỏ cho độc giả nâng cao
Bài viết này chỉ bao gồm các vòng lặp cơ bản.
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
6, let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
7 và let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
8Nếu bạn đến bài viết này để tìm kiếm các loại vòng lặp khác, thì đây là các gợi ý
- Xem for…in để lặp qua các thuộc tính đối tượng
- Xem for…of và iterables để lặp qua các mảng và đối tượng có thể lặp lại
Nếu không, hãy đọc tiếp
Vòng lặp “trong khi”
Vòng lặp
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
6 có cú pháp như sauwhile [condition] {
// code
// so-called "loop body"
}
Trong khi
let i = 3;
while [i] alert[i--];
0 là true, thì let i = 3;
while [i] alert[i--];
1 từ thân vòng lặp được thực thiChẳng hạn, vòng lặp bên dưới xuất ra
let i = 3;
while [i] alert[i--];
2 trong khi let i = 3;
while [i] alert[i--];
3let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
0Một lần thực hiện thân vòng lặp được gọi là một phép lặp. Vòng lặp trong ví dụ trên thực hiện ba lần lặp
Nếu thiếu
let i = 3;
while [i] alert[i--];
4 trong ví dụ trên, vòng lặp sẽ lặp lại [theo lý thuyết] mãi mãi. Trong thực tế, trình duyệt cung cấp các cách để dừng các vòng lặp như vậy và trong JavaScript phía máy chủ, chúng tôi có thể tắt quy trìnhBất kỳ biểu thức hoặc biến nào cũng có thể là điều kiện lặp, không chỉ là phép so sánh. điều kiện được đánh giá và chuyển đổi thành boolean bởi
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
6Chẳng hạn, một cách ngắn hơn để viết
let i = 3;
while [i] alert[i--];
6 là let i = 3;
while [i] alert[i--];
7let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
Dấu ngoặc nhọn không cần thiết cho phần thân một dòng
Nếu thân vòng lặp chỉ có một câu lệnh, chúng ta có thể bỏ dấu ngoặc nhọn
let i = 3;
while [i] alert[i--];
8let i = 3;
while [i] alert[i--];
Vòng lặp “do…while”
Kiểm tra điều kiện có thể được di chuyển bên dưới thân vòng lặp bằng cú pháp
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
7let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
9Đầu tiên, vòng lặp sẽ thực thi phần thân, sau đó kiểm tra điều kiện và, trong khi nó đúng, thực hiện lặp đi lặp lại
Ví dụ
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
0Dạng cú pháp này chỉ nên được sử dụng khi bạn muốn phần thân của vòng lặp thực thi ít nhất một lần bất kể điều kiện có đúng hay không. Thông thường, hình thức khác được ưa thích.
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
90Vòng lặp “cho”
Vòng lặp
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
91 phức tạp hơn nhưng nó cũng là vòng lặp được sử dụng phổ biến nhấtNó trông như thế này
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
3Hãy tìm hiểu ý nghĩa của những phần này bằng ví dụ. Vòng lặp bên dưới chạy
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
92 cho let i = 3;
while [i] alert[i--];
2 từ let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
94 đến [nhưng không bao gồm] let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
95let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
8Hãy xem xét từng phần câu lệnh
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
91partbeginlet i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
97Thực hiện một lần khi vào vòng lặp. điều kiệnlet i = 3;
while [i] alert[i--];
3Được kiểm tra trước mỗi lần lặp lại vòng lặp. Nếu sai, vòng lặp dừng lại. cơ thểlet i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
92Chạy đi chạy lại trong khi điều kiện là trung thực. steplet i = 3;
while [i] alert[i--];
4Thực hiện sau phần thân trên mỗi lần lặpThuật toán vòng lặp chung hoạt động như thế này
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
4Nghĩa là,
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
01 thực hiện một lần, sau đó nó lặp lại. sau mỗi thử nghiệm let i = 3;
while [i] alert[i--];
0, let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
03 và let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
04 được thực thiNếu bạn chưa quen với các vòng lặp, bạn có thể quay lại ví dụ và mô phỏng lại cách nó chạy từng bước trên một tờ giấy
Đây chính xác là những gì xảy ra trong trường hợp của chúng tôi
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
9Khai báo biến nội tuyến
Tại đây, biến “bộ đếm”
let i = 3;
while [i] alert[i--];
2 được khai báo ngay trong vòng lặp. Đây được gọi là khai báo biến “nội tuyến”. Các biến như vậy chỉ hiển thị bên trong vòng lặplet i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
00Thay vì xác định một biến, chúng ta có thể sử dụng một biến hiện có
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
01Bỏ qua các phần
Có thể bỏ qua bất kỳ phần nào của
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
91Ví dụ: chúng ta có thể bỏ qua
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
01 nếu chúng ta không cần làm gì khi bắt đầu vòng lặpthích ở đây
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
02Chúng tôi cũng có thể loại bỏ phần
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
04let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
03Điều này làm cho vòng lặp giống hệt với
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
09Chúng ta thực sự có thể loại bỏ mọi thứ, tạo ra một vòng lặp vô hạn
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
04Xin lưu ý rằng phải có hai dấu chấm phẩy
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
91 let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
31. Nếu không, sẽ có một lỗi cú phápPhá vỡ vòng lặp
Thông thường, một vòng lặp thoát khi điều kiện của nó trở nên sai
Nhưng chúng tôi có thể buộc thoát bất cứ lúc nào bằng cách sử dụng chỉ thị đặc biệt
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32Ví dụ: vòng lặp bên dưới yêu cầu người dùng nhập một dãy số, "phá vỡ" khi không có số nào được nhập
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
05Chỉ thị
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32 được kích hoạt tại dòng let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
34 nếu người dùng nhập một dòng trống hoặc hủy nhập. Nó dừng vòng lặp ngay lập tức, chuyển điều khiển đến dòng đầu tiên sau vòng lặp. Cụ thể là, let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
35Sự kết hợp “vòng lặp vô hạn +
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32 khi cần thiết” là tuyệt vời cho các tình huống khi điều kiện của vòng lặp không được kiểm tra ở đầu hoặc cuối vòng lặp, mà ở giữa hoặc thậm chí ở một số vị trí trên cơ thể của nóTiếp tục đến lần lặp tiếp theo
Chỉ thị
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
37 là “phiên bản nhẹ hơn” của let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32. Nó không dừng vòng lặp while. Thay vào đó, nó dừng vòng lặp hiện tại và buộc vòng lặp bắt đầu vòng lặp mới [nếu điều kiện cho phép]Chúng tôi có thể sử dụng nó nếu chúng tôi đã hoàn thành với lần lặp lại hiện tại và muốn chuyển sang bước tiếp theo
Vòng lặp bên dưới sử dụng
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
37 để chỉ xuất các giá trị lẻlet i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
06Đối với các giá trị chẵn của
let i = 3;
while [i] alert[i--];
2, lệnh let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
37 dừng thực thi phần thân và chuyển quyền điều khiển sang lần lặp tiếp theo của let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
91 [với số tiếp theo]. Vì vậy, let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
35 chỉ được gọi cho các giá trị lẻChỉ thị
37 giúp giảm việc làm tổlet i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
Một vòng lặp hiển thị các giá trị lẻ có thể trông như thế này
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
07Từ quan điểm kỹ thuật, điều này giống hệt với ví dụ trên. Chắc chắn, chúng ta chỉ có thể bọc mã trong một khối
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
85 thay vì sử dụng let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
37Nhưng như một tác dụng phụ, điều này đã tạo thêm một cấp độ lồng nhau [cuộc gọi
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
35 bên trong dấu ngoặc nhọn]. Nếu mã bên trong của let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
85 dài hơn một vài dòng, điều đó có thể làm giảm khả năng đọc tổng thểKhông có
89 ở bên phải của ‘?’let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
Xin lưu ý rằng các cấu trúc cú pháp không phải là biểu thức không thể được sử dụng với toán tử bậc ba
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
40. Cụ thể, các lệnh như let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
89 không được phép ở đóVí dụ: nếu chúng ta lấy mã này
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
08…và viết lại bằng dấu chấm hỏi
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
09…nó ngừng hoạt động. có một lỗi cú pháp
Đây chỉ là một lý do khác để không sử dụng toán tử dấu chấm hỏi
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
40 thay vì let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
85Nhãn để ngắt/tiếp tục
Đôi khi chúng ta cần thoát ra khỏi nhiều vòng lặp lồng nhau cùng một lúc
Ví dụ: trong mã bên dưới, chúng tôi lặp lại
let i = 3;
while [i] alert[i--];
2 và let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
45, nhắc tọa độ let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
46 từ let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
47 đến let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
48let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
0Chúng tôi cần một cách để dừng quá trình nếu người dùng hủy đầu vào
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32 thông thường sau let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
90 sẽ chỉ phá vỡ vòng lặp bên trong. Điều đó là không đủ – nhãn, đến giải cứuNhãn là một mã định danh có dấu hai chấm trước vòng lặp
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
1Câu lệnh
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
91 trong vòng lặp bên dưới thoát ra nhãnlet i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
2Trong đoạn mã trên,
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
92 tìm kiếm nhãn có tên let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
93 và thoát ra khỏi vòng lặp đóVì vậy, điều khiển đi thẳng từ
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
34 đến let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
95Chúng tôi cũng có thể di chuyển nhãn vào một dòng riêng biệt
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
3Chỉ thị
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
37 cũng có thể được sử dụng với nhãn. Trong trường hợp này, việc thực thi mã sẽ chuyển sang lần lặp tiếp theo của vòng lặp được gắn nhãnNhãn không cho phép "nhảy" bất cứ nơi nào
Nhãn không cho phép chúng tôi nhảy vào một vị trí tùy ý trong mã
Ví dụ, không thể làm điều này
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
4Chỉ thị
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32 phải nằm trong khối mã. Về mặt kỹ thuật, bất kỳ khối mã được dán nhãn nào cũng được, e. glet i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
5…Mặc dù, 99. 9% thời gian
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32 được sử dụng bên trong các vòng lặp, như chúng ta đã thấy trong các ví dụ trênChỉ có thể có một
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
37 từ bên trong một vòng lặpTóm lược
Chúng tôi đã đề cập đến 3 loại vòng lặp
6 – Điều kiện được kiểm tra trước mỗi lần lặplet i = 3; while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops alert[ i ]; i--; }
7 – Điều kiện được kiểm tra sau mỗi lần lặplet i = 3; while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops alert[ i ]; i--; }
002 – Điều kiện được kiểm tra trước mỗi lần lặp lại, có sẵn các cài đặt bổ sunglet i = 3; while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops alert[ i ]; i--; }
Để tạo một vòng lặp “vô hạn”, cấu trúc
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
003 thường được sử dụng. Một vòng lặp như vậy, giống như bất kỳ vòng lặp nào khác, có thể được dừng lại bằng chỉ thị let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
32Nếu chúng tôi không muốn làm bất cứ điều gì trong lần lặp hiện tại và muốn chuyển tiếp sang lần tiếp theo, chúng tôi có thể sử dụng chỉ thị
let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
37let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
89 nhãn hỗ trợ trước vòng lặp. Nhãn là cách duy nhất để let i = 3;
while [i] { // when i becomes 0, the condition becomes falsy, and the loop stops
alert[ i ];
i--;
}
89 thoát khỏi vòng lặp lồng nhau để đi đến vòng lặp bên ngoài