Hướng dẫn how for loop is executed in javascript? - vòng lặp for được thực thi như thế nào trong javascript?

  • " Trước
  • Tiếp theo "

Các vòng lặp cung cấp một cách nhanh chóng và dễ dàng để làm một cái gì đó nhiều lần. Chương này của Hướng dẫn JavaScript giới thiệu các câu lệnh lặp khác nhau có sẵn cho JavaScript.

Show

Bạn có thể nghĩ về một vòng lặp như một phiên bản máy tính của trò chơi nơi bạn bảo ai đó thực hiện X Bước theo một hướng, sau đó y các bước khác. Ví dụ, ý tưởng "đi năm bước về phía đông" có thể được thể hiện theo cách này như một vòng lặp:

for (let step = 0; step < 5; step++) {
  // Runs 5 times, with values of step 0 through 4.
  console.log('Walking east one step');
}

Có nhiều loại vòng lặp khác nhau, nhưng tất cả chúng đều làm điều tương tự: họ lặp lại một hành động một số lần. (Lưu ý rằng có thể số đó có thể bằng không!)

Các cơ chế vòng lặp khác nhau cung cấp các cách khác nhau để xác định điểm bắt đầu và điểm cuối của vòng lặp. Có nhiều tình huống dễ dàng được phục vụ hơn bởi một loại vòng lặp trên các loại khác.

Các câu lệnh cho các vòng lặp được cung cấp trong JavaScript là:

  • cho tuyên bố
  • làm ... trong khi tuyên bố
  • trong khi tuyên bố
  • Tuyên bố được dán nhãn
  • Tuyên bố phá vỡ
  • Tiếp tục tuyên bố
  • cho ... trong tuyên bố
  • cho ... tuyên bố

cho tuyên bố

làm ... trong khi tuyên bố

trong khi tuyên bố

Tuyên bố được dán nhãn

Tuyên bố phá vỡ

  1. Tiếp tục tuyên bố
  2. cho ... trong tuyên bố
  3. cho ... tuyên bố
  4. Vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0 lặp lại cho đến khi một điều kiện được chỉ định đánh giá là sai. Vòng lặp JavaScript
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0 tương tự như vòng lặp Java và C
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0.
  5. Một câu lệnh
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0 trông như sau:

for ([initialExpression]; [conditionExpression]; [incrementExpression]) statement

Khi vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 thực thi, điều này xảy ra sau:

Biểu thức khởi tạo
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
5, nếu có, được thực thi. Biểu thức này thường khởi tạo một hoặc nhiều bộ đếm vòng lặp, nhưng cú pháp cho phép biểu hiện bất kỳ mức độ phức tạp nào. Biểu thức này cũng có thể khai báo các biến.

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>

Biểu thức
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
6 được đánh giá. Nếu giá trị của
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
6 là đúng, các câu lệnh LOOP thực thi. Nếu không, vòng lặp
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 chấm dứt. (Nếu biểu thức
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
6 bị bỏ qua hoàn toàn, điều kiện được coi là đúng.)

function howMany(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${howMany(musicTypes)} option(s).`);
});
0 thực thi. Để thực thi nhiều câu lệnh, hãy sử dụng câu lệnh BLOCK (
function howMany(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${howMany(musicTypes)} option(s).`);
});
1) để nhóm các câu lệnh đó.

function howMany(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${howMany(musicTypes)} option(s).`);
});

làm ... trong khi tuyên bố

trong khi tuyên bố

Tuyên bố được dán nhãn

do
  statement
while (condition);

Tuyên bố phá vỡ

Tiếp tục tuyên bố

for ([initialExpression]; [conditionExpression]; [incrementExpression]) statement

Khi vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 thực thi, điều này xảy ra sau:

let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);

trong khi tuyên bố

Tuyên bố được dán nhãn

while (condition)
  statement

Tuyên bố phá vỡ

Tiếp tục tuyên bố

cho ... trong tuyên bố

cho ... tuyên bố

Vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 lặp lại cho đến khi một điều kiện được chỉ định đánh giá là sai. Vòng lặp JavaScript
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 tương tự như vòng lặp Java và C
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0.

let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}

Một câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 trông như sau:

  • for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
  • Khi vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0 thực thi, điều này xảy ra sau:
  • Biểu thức khởi tạo
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    5, nếu có, được thực thi. Biểu thức này thường khởi tạo một hoặc nhiều bộ đếm vòng lặp, nhưng cú pháp cho phép biểu hiện bất kỳ mức độ phức tạp nào. Biểu thức này cũng có thể khai báo các biến.

Biểu thức

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
6 được đánh giá. Nếu giá trị của
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
6 là đúng, các câu lệnh LOOP thực thi. Nếu không, vòng lặp
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 chấm dứt. (Nếu biểu thức
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
6 bị bỏ qua hoàn toàn, điều kiện được coi là đúng.)

function howMany(selectObject) { let numberSelected = 0; for (let i = 0; i < selectObject.options.length; i++) { if (selectObject.options[i].selected) { numberSelected++; } } return numberSelected; } const btn = document.getElementById('btn'); btn.addEventListener('click', () => { const musicTypes = document.selectForm.musicTypes; console.log(`You have selected ${howMany(musicTypes)} option(s).`); }); 0 thực thi. Để thực thi nhiều câu lệnh, hãy sử dụng câu lệnh BLOCK (function howMany(selectObject) { let numberSelected = 0; for (let i = 0; i < selectObject.options.length; i++) { if (selectObject.options[i].selected) { numberSelected++; } } return numberSelected; } const btn = document.getElementById('btn'); btn.addEventListener('click', () => { const musicTypes = document.selectForm.musicTypes; console.log(`You have selected ${howMany(musicTypes)} option(s).`); }); 1) để nhóm các câu lệnh đó.

Nếu có, biểu thức cập nhật

function howMany(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${howMany(musicTypes)} option(s).`);
});
2 được thực thi.

// Infinite loops are bad!
while (true) {
  console.log('Hello, world!');
}

Tuyên bố được dán nhãn

Một

markLoop:
while (theMark) {
  doSomething();
}
0 cung cấp một câu lệnh với một định danh cho phép bạn tham khảo nó ở nơi khác trong chương trình của bạn. Ví dụ: bạn có thể sử dụng nhãn để xác định một vòng lặp và sau đó sử dụng các câu lệnh
markLoop:
while (theMark) {
  doSomething();
}
1 hoặc
markLoop:
while (theMark) {
  doSomething();
}
2 để cho biết liệu một chương trình có nên làm gián đoạn vòng lặp hay tiếp tục thực thi hay không.

Cú pháp của câu lệnh được dán nhãn trông giống như sau:

Giá trị của

markLoop:
while (theMark) {
  doSomething();
}
0 có thể là bất kỳ định danh JavaScript nào không phải là một từ dành riêng.
function howMany(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${howMany(musicTypes)} option(s).`);
});
0 mà bạn xác định với nhãn có thể là bất kỳ tuyên bố nào.

Thí dụ

Trong ví dụ này, nhãn

markLoop:
while (theMark) {
  doSomething();
}
5 xác định vòng lặp
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3.

markLoop:
while (theMark) {
  doSomething();
}

Tuyên bố phá vỡ

Sử dụng câu lệnh

markLoop:
while (theMark) {
  doSomething();
}
1 để chấm dứt một vòng lặp,
markLoop:
while (theMark) {
  doSomething();
}
8 hoặc kết hợp với một câu lệnh được dán nhãn.

  • Khi bạn sử dụng
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    1 mà không có nhãn, nó sẽ chấm dứt việc bao quanh bên trong
    let i = 0;
    do {
      i += 1;
      console.log(i);
    } while (i < 5);
    
    3,
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    01,
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0 hoặc
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    8 ngay lập tức và chuyển kiểm soát sang câu lệnh sau.
  • Khi bạn sử dụng
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    1 với nhãn, nó sẽ chấm dứt câu lệnh được ghi nhãn được chỉ định.

Cú pháp của câu lệnh

markLoop:
while (theMark) {
  doSomething();
}
1 trông như thế này:

  1. Hình thức đầu tiên của cú pháp chấm dứt vòng lặp kín trong cùng hoặc
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    8.
  2. Hình thức thứ hai của cú pháp chấm dứt câu lệnh được chỉ định được chỉ định.

ví dụ 1

Ví dụ sau đây lặp qua các phần tử trong một mảng cho đến khi tìm thấy chỉ mục của một phần tử có giá trị là

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
07:

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
0

Ví dụ 2: Phá vỡ nhãn

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
1

Tiếp tục tuyên bố

Tuyên bố

markLoop:
while (theMark) {
  doSomething();
}
2 có thể được sử dụng để khởi động lại câu lệnh
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
01,
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 hoặc
markLoop:
while (theMark) {
  doSomething();
}
0.

  • Khi bạn sử dụng
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    2 mà không có nhãn, nó sẽ chấm dứt lần lặp hiện tại của câu lệnh
    let i = 0;
    do {
      i += 1;
      console.log(i);
    } while (i < 5);
    
    3,
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    01 hoặc
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0 và tiếp tục thực hiện vòng lặp với lần lặp tiếp theo. Trái ngược với tuyên bố
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    1,
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    2 không chấm dứt hoàn toàn việc thực hiện vòng lặp. Trong vòng lặp
    let i = 0;
    do {
      i += 1;
      console.log(i);
    } while (i < 5);
    
    3, nó nhảy trở lại điều kiện. Trong vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    0, nó nhảy đến
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    21.
  • Khi bạn sử dụng
    markLoop:
    while (theMark) {
      doSomething();
    }
    
    2 với nhãn, nó sẽ áp dụng cho câu lệnh Looping được xác định với nhãn đó.

Cú pháp của câu lệnh

markLoop:
while (theMark) {
  doSomething();
}
2 trông giống như sau:

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
2

ví dụ 1

Ví dụ sau đây lặp qua các phần tử trong một mảng cho đến khi tìm thấy chỉ mục của một phần tử có giá trị là

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
07:

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
3

Ví dụ 2: Phá vỡ nhãn

Tiếp tục tuyên bố

Tuyên bố

markLoop:
while (theMark) {
  doSomething();
}
2 có thể được sử dụng để khởi động lại câu lệnh
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
01,
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 hoặc
markLoop:
while (theMark) {
  doSomething();
}
0.

Khi bạn sử dụng

markLoop:
while (theMark) {
  doSomething();
}
2 mà không có nhãn, nó sẽ chấm dứt lần lặp hiện tại của câu lệnh
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
01 hoặc
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 và tiếp tục thực hiện vòng lặp với lần lặp tiếp theo. Trái ngược với tuyên bố
markLoop:
while (theMark) {
  doSomething();
}
1,
markLoop:
while (theMark) {
  doSomething();
}
2 không chấm dứt hoàn toàn việc thực hiện vòng lặp. Trong vòng lặp
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3, nó nhảy trở lại điều kiện. Trong vòng lặp
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0, nó nhảy đến
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
21.

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
4

Khi bạn sử dụng markLoop: while (theMark) { doSomething(); } 2 với nhãn, nó sẽ áp dụng cho câu lệnh Looping được xác định với nhãn đó.

Cú pháp của câu lệnh

markLoop:
while (theMark) {
  doSomething();
}
2 trông giống như sau:

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
5

Thí dụ

Trong ví dụ này, nhãn

markLoop:
while (theMark) {
  doSomething();
}
5 xác định vòng lặp
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3.

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
6

Tuyên bố phá vỡ

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
7

Sử dụng câu lệnh markLoop: while (theMark) { doSomething(); } 1 để chấm dứt một vòng lặp, markLoop: while (theMark) { doSomething(); } 8 hoặc kết hợp với một câu lệnh được dán nhãn.

Khi bạn sử dụng

markLoop:
while (theMark) {
  doSomething();
}
1 mà không có nhãn, nó sẽ chấm dứt việc bao quanh bên trong
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
01,
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0 hoặc
markLoop:
while (theMark) {
  doSomething();
}
8 ngay lập tức và chuyển kiểm soát sang câu lệnh sau.

Khi bạn sử dụng

markLoop:
while (theMark) {
  doSomething();
}
1 với nhãn, nó sẽ chấm dứt câu lệnh được ghi nhãn được chỉ định.

Cú pháp của câu lệnh markLoop: while (theMark) { doSomething(); } 1 trông như thế này:

Hình thức đầu tiên của cú pháp chấm dứt vòng lặp kín trong cùng hoặc

markLoop:
while (theMark) {
  doSomething();
}
8.

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
8

Ví dụ sau đây cho thấy sự khác biệt giữa vòng lặp

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
61 và vòng lặp
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
51. Trong khi
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
51 lặp lại tên thuộc tính,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
61 lặp lại các giá trị thuộc tính:

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
9

  • " Trước
  • Tiếp theo "

Làm thế nào để một vòng lặp hoạt động trong javascript?

A cho vòng lặp lặp lại cho đến khi một điều kiện được chỉ định đánh giá là sai. JavaScript cho vòng lặp tương tự như Java và C cho vòng lặp. Khi A For Loop thực thi, điều sau đây xảy ra: Biểu thức khởi tạo khởi động, nếu có, được thực thi.repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. When a for loop executes, the following occurs: The initializing expression initialExpression , if any, is executed.

3 phần của A For Loop trong JavaScript là gì?

Đối với vòng lặp bao gồm ba phần: khởi tạo, điều kiện và lặp.initialization, condition and iteration.

Những gì cho trong vòng lặp trong JavaScript với ví dụ?

Vòng lặp for-in trong JavaScript được sử dụng để lặp lại các thuộc tính của một đối tượng.Nó có thể là một công cụ gỡ lỗi tuyệt vời nếu chúng ta muốn hiển thị nội dung của một đối tượng.Vòng lặp for-in chỉ lặp lại trên các phím của một đối tượng có tài sản có thể phản cảm của chúng thành True True.used to iterate over the properties of an object. It can be a great debugging tool if we want to show the contents of an object. The for-in loop iterates only over those keys of an object which have their enumerable property set to “true”.

Làm thế nào để một vòng lặp hoạt động?

Vòng lặp "cho" được sử dụng để lặp lại một khối mã cụ thể một số lần đã biết.Ví dụ: nếu chúng tôi muốn kiểm tra lớp của mỗi học sinh trong lớp, chúng tôi lặp từ 1 đến số đó.Khi số lần không được biết đến trước khi tay, chúng tôi sử dụng vòng lặp "trong khi".used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.