Javascript trong khi ngắt vòng lặp

Ví dụ. xuất hàng hóa từ danh sách này sang danh sách khác hoặc chỉ chạy cùng một mã với 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

Nội dung chính

  • 1. Vòng lặp trong khi
  • 2. Vòng lặp do … while
  • 3. Các vòng lặp với for
    • 3. 1 Khai báo biến nội tuyến
    • 3. 2 Remove the part
  • 4. Phá vỡ vòng lặp
  • 5. Continue to the next phase
  • 6. Nhãn để ngắt/tiếp tục
  • 7. Tóm tắt

1. Vòng lặp trong khi

Các vòng lặp

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
5 có cú pháp sau

while (condition) {
  // code
  // so-called "loop body"
}

Trong khi

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
6 là true, phần thân
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
7trong vòng lặp được thực thi

Không giới hạn, vòng lặp bên dưới xuất ra

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
8 cho tới
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
9

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}

Cứ một lần thực thi vòng lặp được gọi là phép lặp. Vòng lặp trong ví dụ được tạo ra ba lần lặp

Nếu

let i = 3;
while (i) alert(i--);
0bị thiếu trong ví dụ trên, vòng lặp sẽ lặp lại (về lý thuyết) mãi mãi. Trong thực tế, trình duyệt cung cấp các cách để ngăn chặn các vòng lặp như vậy trong JavaScript ở phía máy chủ, chúng ta có thể tiêu diệt tiến trình này

Bất kỳ biểu thức hoặc biến nào cũng có thể là điều kiện vòng lặp, không chỉ là so sánh. điều kiện được ước tính và chuyển đổi thành boolean bởi

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
5

không hạn chế, một cách viết ngắn hơn

let i = 3;
while (i) alert(i--);
2là
let i = 3;
while (i) alert(i--);
3

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}

Nếu vòng lặp thân có một đơn lệnh, chúng ta có thể bỏ qua dấu trích dẫn

let i = 3;
while (i) alert(i--);
4

let i = 3;
while (i) alert(i--);

2. Vòng lặp do … while

Điều kiện kiểm tra có thể được chuyển vào bên trong thân vòng lặp bằng cách sử dụng

let i = 3;
while (i) alert(i--);
5cú pháp

while (condition) {
  // code
  // so-called "loop body"
}
5

Vòng trước tiên sẽ thực hiện phần thân, sau đó kiểm tra điều kiện và khi điều kiện là đúng, thì thực hiện vòng lặp 1 lần. Cứ như vậy cho tới khi điều kiện là sai

Ví dụ

while (condition) {
  // code
  // so-called "loop body"
}
6

Cú pháp đột biến 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 là sai. Thông thường, các hình thức khác nhau được ưa thích.

let i = 3;
while (i) alert(i--);
6

3. Các vòng lặp với for

Các vòng lặp

let i = 3;
while (i) alert(i--);
7 phức tạp hơn, nhưng nó cũng là vòng lặp thông thường được sử dụng nhiều nhất

Nó trông giống như thế này

while (condition) {
  // code
  // so-called "loop body"
}
9

Hãy tìm hiểu ý nghĩa của những phần này bằng ví dụ. Vòng bên dưới chạy

let i = 3;
while (i) alert(i--);
8cho
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
8từ
while (condition) {
  // code
  // so-called "loop body"
}
50lên đến (nhưng không bao gồm)
while (condition) {
  // code
  // so-called "loop body"
}
51

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
4

Vui lòng xem xét từng vòng

let i = 3;
while (i) alert(i--);
7 của câu lệnh

Vòng bắt đầu
while (condition) {
  // code
  // so-called "loop body"
}
53 Thực hiện một lần khi vào vòng. status
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
9Kiểm tra trước mỗi lần lặp. Nếu sai, vòng lặp dừng lại. thân______78Chạy đi chạy lại trong khi điều kiện là đúng. step
let i = 3;
while (i) alert(i--);
0 Thực hiện sau mỗi lần lặp phần thân

Thuật toán vòng lặp hoạt động chung như thế này

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
0

Đó là,

while (condition) {
  // code
  // so-called "loop body"
}
57 thực thi một lần, và sau đó lặp lại -> sau mỗi lần kiểm tra
while (condition) {
  // code
  // so-called "loop body"
}
58và
while (condition) {
  // code
  // so-called "loop body"
}
59

Nếu bạn chưa quen với các vòng lặp, mô tả và tái tạo cách nó chạy từng bước trên một tờ giấy

Điều này chính xác là những gì xảy ra trong trường hợp của chúng tôi

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
4

3. 1 Khai báo biến nội tuyến

Tại đây, biến đối số

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
8được khai báo ngay trong vòng lặp. Điều này được gọi là một biến báo nội tuyến. Các biến như vậy chỉ được nhìn thấy bên trong vòng lặp

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
0

Thay vì khai báo một biến, chúng ta có thể sử dụng một biến hiện có

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
1

3. 2 Remove the part

Bất kỳ phần nào của

let i = 3;
while (i) alert(i--);
7có thể bị bỏ qua

Ví dụ. chúng ta có thể bỏ qua

while (condition) {
  // code
  // so-called "loop body"
}
57 nếu chúng ta không cần làm gì khi bắt đầu vòng lặp

Giống như ở đây

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
2

Chúng ta cũng có thể loại bỏ

while (condition) {
  // code
  // so-called "loop body"
}
59một phần

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
3

Điều này làm cho vòng lặp giống như ngoại thất

while (condition) {
  // code
  // so-called "loop body"
}
64

Chú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 = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
4

Xin lưu ý rằng hai

let i = 3;
while (i) alert(i--);
7dấu chấm hài
while (condition) {
  // code
  // so-called "loop body"
}
66 phải có mặt. Nếu không, sẽ có một cú pháp lỗi

4. Phá vỡ vòng lặp

Thông thường, một vòng lặp thoát khi trạng thái của nó trở nên sai

Nhưng chúng ta có thể buộc phải thoát ra bất cứ lúc nào bằng cách sử dụng

while (condition) {
  // code
  // so-called "loop body"
}
67chỉ thị đặc biệt

Ví dụ. vòng bên dưới yêu cầu người dùng cung cấp một chuỗi số, ngắt khi không nhập số

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
5

Lệnh

while (condition) {
  // code
  // so-called "loop body"
}
67 được kích hoạt tại dòng
while (condition) {
  // code
  // so-called "loop body"
}
69 nếu người dùng nhập vào một dòng trống hoặc hủy bỏ đầu vào. 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à
while (condition) {
  // code
  // so-called "loop body"
}
90

Vòng lặp vô hạn của tổ hợp +

while (condition) {
  // code
  // so-called "loop body"
}
67khi cần thiết, rất phù hợp cho các vấn đề khi phải kiểm tra điều kiện của vòng lặp không phải ở đầu hoặc cuối vòng mà ở giữa hoặc thậm chí chí ở một số vị trí trên cơ

5. Continue to the next phase

Lệnh

while (condition) {
  // code
  // so-called "loop body"
}
92 này là một phiên bản nhẹ hơn của
while (condition) {
  // code
  // so-called "loop body"
}
67. Nó không dừng toàn bộ vòng lặp. Thay vào đó, nó dừng việc lặp lại hiện tại và buộc vòng lặp bắt đầu một vòng lặp mới (nếu điều kiện cho phép)

Chúng ta có thể sử dụng nó nếu chúng ta đã hoàn thành với lần lặp hiện tại và muốn chuyển sang lần lặp tiếp theo

Vòng bên dưới chỉ sử dụng

while (condition) {
  // code
  // so-called "loop body"
}
92 để xuất các giá trị đơn lẻ

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
6

Đối với các giá trị buổi tối của

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
8, lệnh
while (condition) {
  // code
  // so-called "loop body"
}
92dừng thực hiện phần thân và chuyển điều khiển sang lần lặp tiếp theo của
let i = 3;
while (i) alert(i--);
7(với số tiếp theo). Vì vậy,
while (condition) {
  // code
  // so-called "loop body"
}
90chỉ được gọi cho các giá trị đơn lẻ

Một vòng lặp hiển thị các giá trị kỳ lạ có thể trông giống như thế này

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
7

Từ quan điểm kỹ thuật, điều này giống với ví dụ trên. Chắc chắn, chúng ta chỉ có thể bọc mã trong một khối ______199 thay vì sử dụng

while (condition) {
  // code
  // so-called "loop body"
}
92

Nhưng với tư cách là một hiệu ứng phụ, điều này tạo ra thêm một mức độ lồng nhau (gọi

while (condition) {
  // code
  // so-called "loop body"
}
90 bên trong các dấu ngoặc kép). Nếu mã bên trong
while (condition) {
  // code
  // so-called "loop body"
}
99dài hơn một vài dòng, điều đó có thể làm giảm khả năng đọc mã

Không

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
43ở bên phải của ‘?’

Xin lưu ý rằng các cấu trúc cú pháp không phải là biểu thức có thể được sử dụng với toán tử ternary

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
44. Cụ thể, các chỉ thị như
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
43không được phép ở đó

Ví dụ. if we we get this code

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
8

Viết và viết lại bằng dấu chấm hỏi

let i = 0;
while (i < 3) { // shows 0, then 1, then 2
  alert( i );
  i++;
}
9

Unable to continue active. cú pháp error

Đây chỉ là một lý do khác để không sử dụng toán tử dấu hỏi

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
44thay vì ________ vì199

6. Nhãn để phá vỡ / /* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam @author cafedevn Contact: [email protected] Fanpage: https://www.facebook.com/cafedevn Instagram: https://instagram.com/cafedevn Twitter: https://twitter.com/CafedeVn Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/ */ let i = 3; while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops alert( i ); i--; }48

Đôi khi chúng ta cần thoát ra khỏi nhiều vòng lặp cùng một lúc

Ví dụ. trong đoạn mã bên dưới, chúng ta lặp lại

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
8và
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
00nhắc nhắc chế độ
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
01 từ
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
02 đến
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
03

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
0

Chúng ta cần một cách để dừng quá trình nếu người dùng hủy bỏ đầu vào

Bình thường

while (condition) {
  // code
  // so-called "loop body"
}
67sau
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
05 sẽ chỉ phá vỡ vòng lặp bên trong, nhưng không đủ

Một nhãn là một danh sách được định nghĩa với một dấu hai chấm trước một vòng lặp

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
1

Câu lệnh

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
06 trong vòng bên dưới sẽ thoát khỏi nhãn

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
2

Trong đoạn mã trên, với

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
07ta nhìn lên trên sẽ thấy nhãn có tên
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
08và nó sẽ thoát ra khỏi vòng lặp tại cái nhãn đó

Vì vậy, luồng xử lý đi thẳng từ

while (condition) {
  // code
  // so-called "loop body"
}
69 đến
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
40

Chúng ta cũng có thể chuyển nhãn lên một dòng riêng biệt

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
3

Lệnh

while (condition) {
  // code
  // so-called "loop body"
}
92này cũng có thể được sử dụng với nhãn. Trong trường hợp này, thực thi mã nhảy đến lần lặp tiếp theo của vòng lặp đã được gắn nhãn. Nhãn không cho phép nhảy bất cứ nơi nào

Nhãn không cho phép chúng ta nhảy vào một vị trí tùy chọn trong mã

Ví dụ, could not doing this thing

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
4

Một lệnh gọi đến

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
43chỉ có thể từ bên trong một vòng lặp và nhãn phải ở đâu phía trên chỉ thị

7. Tóm tắt

Chúng ta bao gồm 3 loại vòng

  • /*
    Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
    @author cafedevn
    Contact: [email protected]
    Fanpage: https://www.facebook.com/cafedevn
    Instagram: https://instagram.com/cafedevn
    Twitter: https://twitter.com/CafedeVn
    Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
    */
    
    let i = 3;
    while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
      alert( i );
      i--;
    }
    5 – Điều kiện được kiểm tra trước mỗi lần bật
  • let i = 3;
    while (i) alert(i--);
    5 – Điều kiện được kiểm tra sau mỗi lần bật
  • /*
    Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
    @author cafedevn
    Contact: [email protected]
    Fanpage: https://www.facebook.com/cafedevn
    Instagram: https://instagram.com/cafedevn
    Twitter: https://twitter.com/CafedeVn
    Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
    */
    
    let i = 3;
    while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
      alert( i );
      i--;
    }
    45 – Điều kiện được kiểm tra trước mỗi lần lặp và một số điều kiện đi kèm

To create an a loop of the user, cấu trúc thông thường được sử dụng là

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
46. 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ể bị chặn lại bằng lệnh
while (condition) {
  // code
  // so-called "loop body"
}
67

Nếu chúng ta không muốn làm bất cứ điều gì trong lần lặp lại hiện tại và muốn chuyển sang lần tiếp theo, chúng ta có thể sử dụng lệnh

while (condition) {
  // code
  // so-called "loop body"
}
92

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
43với nhãn hỗ trợ trước vòng lặp. Một nhãn thì duy nhất.
/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

let i = 3;
while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
  alert( i );
  i--;
}
43dùng để thoát khỏi một vòng lặp lồng nhau để đi đến một vòng ngoài