-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathadvanced_loops.js
91 lines (76 loc) · 1.87 KB
/
advanced_loops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
if (typeof output === 'undefined') output = console.log;
let counter = 5;
function countdown() {
return counter--;
}
function resetCounter() {
counter = 5;
}
//
// While loops
//
while (countdown()) {
output("inside while loop body");
}
resetCounter()
while (output("inside while loop header"), output("still inside while loop header"), countdown()) {
output("inside while loop body");
}
resetCounter();
while (output("inside while loop header"), counter) {
output("inside while loop body");
countdown();
}
resetCounter();
while ((function() { let c = countdown(); output("inside temporary function, c = " + c); return c; })()) {
output("inside while loop body");
}
resetCounter();
//
// Do-While loops
//
do {
output("inside do-while loop body");
} while (countdown())
resetCounter()
do {
output("inside do-while loop body");
} while (output("inside do-while loop header"), output("still inside do-while loop header"), countdown())
resetCounter();
do {
output("inside do-while loop body");
countdown();
} while (output("inside do-while loop header"), counter)
resetCounter();
do {
output("inside do-while loop body");
} while ((function() { let c = countdown(); output("inside temporary function, c = " + c); return c; })())
resetCounter();
//
// For loops
//
for (;;) {
if (!counter--) {
break;
}
output("inside for loop body");
continue;
output("should not be reached");
}
resetCounter();
for (let i = 0, j = 10; i < j; i++, j--) {
output("inside for loop body, i: " + i + " j: " + j);
}
for (; countdown();) {
output("inside for loop body");
}
resetCounter();
for (let i = 0; ; i++) {
output("inside for loop body");
if (i >= 5) break;
}
for (output("inside for loop initializer"); output("inside for loop condition"), true; output("inside for loop afterthought")) {
output("inside for loop body");
if (!countdown()) break;
}
resetCounter();