1.5 JavaScript loop statement

1.5 JavaScript loop statement

The loop statement in JavaScript C++is similar to that in and also contains for, whileloop.

forcycle

for (let i = 0; i < 10; i++) {
    
    
    console.log(i);
}

When enumerating objects or arrays you can use:

  • for-inLoop, you can enumerate the subscripts in the array, and the subscripts in the objectkey
  • for-ofLoop, you can enumerate the values ​​in the array, and the values ​​in the objectvalue

whilecycle

let i = 0;
while (i < 10) {
    
    
    console.log(i);
    i++;
}

practise

  1. Find the sum of the cubes of all the numbers from 1 to 100.
  2. Find the nth term of the Fibonacci sequence. f(1) = 1, f(2) = 1, f(3) = 2, f(n) = f(n-1) + f(n-2).
  3. Print all prime numbers from 1 to 100.

Guess you like

Origin blog.csdn.net/qq_42465670/article/details/130397088