Improve for-loop scripts.

This commit is contained in:
Stephen Chung 2021-06-07 14:52:45 +08:00
parent 65b7135324
commit c4b3ad7c7b
2 changed files with 10 additions and 6 deletions

View File

@ -1,10 +1,12 @@
// This script runs for-loops // This script runs for-loops
let arr = [1, 2, 3, 4]; let arr = [1, true, 123.456, "hello", 3, 42];
for a in arr { for (a, i) in arr {
for b in [10, 20] { for (b, j) in ['x', 42, (), 123, 99, 0.5] {
print(`${a}, ${b}`); if b > 100 { continue; }
print(`(${i}, ${j}) = (${a}, ${b})`);
} }
if a == 3 { break; } if a == 3 { break; }
@ -12,6 +14,6 @@ for a in arr {
//print(a); // <- if you uncomment this line, the script will fail to run //print(a); // <- if you uncomment this line, the script will fail to run
// because 'a' is not defined here // because 'a' is not defined here
for i in range(0, 5) { // runs through a range from 0 to 4 for i in range(5, 0, -1) { // runs from 5 down to 1
print(i); print(i);
} }

View File

@ -12,6 +12,8 @@ for i in range(0, MAX) {
list.push(i); list.push(i);
} }
print(`Time = ${now.elapsed} seconds...`);
let sum = 0; let sum = 0;
for i in list { for i in list {
@ -19,4 +21,4 @@ for i in list {
} }
print(`Sum = ${sum}`); print(`Sum = ${sum}`);
print(`Finished. Run time = ${now.elapsed} seconds.`); print(`Finished. Total run time = ${now.elapsed} seconds.`);