Hướng dẫn can we write for loop in javascript? - chúng ta có thể viết vòng lặp for trong javascript không?

  • " 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.

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 
  Choose some music types, then click the button below:
  
    R&B
    Jazz
    Blues
    New Age
    Classical
    Opera
  
  How many are selected?

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

  Choose some music types, then click the button below:
  
    R&B
    Jazz
    Blues
    New Age
    Classical
    Opera
  
  How many are selected?

0 tương tự như vòng lặp Java và C

  Choose some music types, then click the button below:
  
    R&B
    Jazz
    Blues
    New Age
    Classical
    Opera
  
  How many are selected?

0.
  • Một câu lệnh
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0 trông như sau:
  • for [[initialExpression]; [conditionExpression]; [incrementExpression]]
      statement
    

    Khi vòng lặp

    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0 thực thi, điều này xảy ra sau:

    Biểu thức khởi tạo
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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.

    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    

    Biểu thức
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    6 được đánh giá. Nếu giá trị của
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    6 là đúng, các câu lệnh LOOP thực thi. Nếu không, vòng lặp
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0 chấm dứt. [Nếu biểu thức
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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  {
      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  {
      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  {
      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

    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0 thực thi, điều này xảy ra sau:

    let i = 0;
    do {
      i += 1;
      console.log[i];
    } while [i 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0 tương tự như vòng lặp Java và C
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0.

    let n = 0;
    let x = 0;
    while [n 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0 trông như sau:

    • for [[initialExpression]; [conditionExpression]; [incrementExpression]]
        statement
      
    • Khi vòng lặp
      
        Choose some music types, then click the button below:
        
          R&B
          Jazz
          Blues
          New Age
          Classical
          Opera
        
        How many are selected?
      
      
      0 thực thi, điều này xảy ra sau:
    • Biểu thức khởi tạo
      
        Choose some music types, then click the button below:
        
          R&B
          Jazz
          Blues
          New Age
          Classical
          Opera
        
        How many are selected?
      
      
      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

    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    6 được đánh giá. Nếu giá trị của
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    6 là đúng, các câu lệnh LOOP thực thi. Nếu không, vòng lặp
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    0 chấm dứt. [Nếu biểu thức
    
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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  {
      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  {
      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  {
      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  {
      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 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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 
        Choose some music types, then click the button below:
        
          R&B
          Jazz
          Blues
          New Age
          Classical
          Opera
        
        How many are selected?
      
      
      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 
        Choose some music types, then click the button below:
        
          R&B
          Jazz
          Blues
          New Age
          Classical
          Opera
        
        How many are selected?
      
      
      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 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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 
      Choose some music types, then click the button below:
      
        R&B
        Jazz
        Blues
        New Age
        Classical
        Opera
      
      How many are selected?
    
    
    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 "

    Chúng ta có thể sử dụng cho trong vòng lặp cho mảng trong JavaScript không?

    Một vòng lặp cho vòng lặp có thể được sử dụng để truy cập vào mọi phần tử của một mảng.Mảng bắt đầu ở mức 0 và độ dài thuộc tính mảng được sử dụng để đặt đầu vòng lặp.. The array begins at zero, and the array property length is used to set the loop end.

    Cái nào cho vòng lặp là tốt nhất trong JavaScript?

    Vòng lặp nhanh nhất là một vòng lặp, cả có và không có độ dài bộ nhớ đệm mang lại hiệu suất thực sự tương tự.....
    Vòng lặp trong khi giảm chậm hơn khoảng 1,5 lần so với vòng lặp ..
    Một vòng lặp sử dụng hàm gọi lại [như foreach tiêu chuẩn], chậm hơn khoảng 10 lần so với vòng lặp ..

    Bạn có thể làm tổ cho các vòng trong JavaScript không?

    JavaScript hỗ trợ tính năng vòng lặp lồng nhau, trong đó một vòng lặp có mặt bên trong một vòng lặp khác.Một vòng lặp có thể có một hoặc số và/hoặc n mức các vòng lặp được xác định bên trong một vòng lặp khác.Đối với mỗi vòng ngoài, vòng lặp bên trong được thực thi., where a loop is present inside another loop. A loop can have one or number and/or n nested level of loops defined inside another loop. For each outer loop, the inner loop gets to execute.

    Bài Viết Liên Quan

    Chủ Đề