Added nested loop example and some more comment in other examples.
This commit is contained in:
parent
0b48aa3e0c
commit
d67c541824
5 changed files with 137 additions and 0 deletions
|
|
@ -45,3 +45,16 @@ fun pair(x, y)
|
||||||
p = pair(7, 9)
|
p = pair(7, 9)
|
||||||
print(p) // -> [7, 9]
|
print(p) // -> [7, 9]
|
||||||
print(p[1]) // -> 9
|
print(p[1]) // -> 9
|
||||||
|
|
||||||
|
/* Expected output:
|
||||||
|
[1, 2, 3]
|
||||||
|
3
|
||||||
|
[1, 2, 13]
|
||||||
|
16
|
||||||
|
[0, 1, 4, 9, 16]
|
||||||
|
[[1, 2], [3, 4], [5, 6]]
|
||||||
|
3
|
||||||
|
[10, 20, 30]
|
||||||
|
[7, 9]
|
||||||
|
9
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -40,3 +40,22 @@ print(s2) // -> [1, 7, 3]
|
||||||
tail = [10, 20]
|
tail = [10, 20]
|
||||||
combined = arr + tail
|
combined = arr + tail
|
||||||
print(combined) // -> [1, 7, 3, 10, 20]
|
print(combined) // -> [1, 7, 3, 10, 20]
|
||||||
|
|
||||||
|
/* Expected output:
|
||||||
|
[1, 2, 3]
|
||||||
|
3
|
||||||
|
5
|
||||||
|
4
|
||||||
|
[1, 2, 3, 99]
|
||||||
|
99
|
||||||
|
[1, 2, 3]
|
||||||
|
42
|
||||||
|
[1, 42, 3]
|
||||||
|
4
|
||||||
|
[1, 7, 42, 3]
|
||||||
|
42
|
||||||
|
[1, 7, 3]
|
||||||
|
[7, 3]
|
||||||
|
[1, 7, 3]
|
||||||
|
[1, 7, 3, 10, 20]
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -28,3 +28,28 @@ fun make()
|
||||||
|
|
||||||
for v in make()
|
for v in make()
|
||||||
print(v) // prints: 7, 9, 11
|
print(v) // prints: 7, 9, 11
|
||||||
|
|
||||||
|
/* Expected output:
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
10
|
||||||
|
20
|
||||||
|
30
|
||||||
|
[1, 2]
|
||||||
|
1
|
||||||
|
2
|
||||||
|
[3, 4]
|
||||||
|
3
|
||||||
|
4
|
||||||
|
10
|
||||||
|
20
|
||||||
|
30
|
||||||
|
40
|
||||||
|
50
|
||||||
|
20
|
||||||
|
30
|
||||||
|
7
|
||||||
|
9
|
||||||
|
11
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -29,3 +29,19 @@ for x in arr
|
||||||
print(x) // -> 1, 2, 4
|
print(x) // -> 1, 2, 4
|
||||||
if x == 4
|
if x == 4
|
||||||
break
|
break
|
||||||
|
|
||||||
|
/* Expected output:
|
||||||
|
1
|
||||||
|
3
|
||||||
|
5
|
||||||
|
7
|
||||||
|
0
|
||||||
|
1
|
||||||
|
2
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
1
|
||||||
|
2
|
||||||
|
4
|
||||||
|
*/
|
||||||
|
|
|
||||||
64
examples/nested_loops.fun
Normal file
64
examples/nested_loops.fun
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
// Nested loops: break and continue behavior
|
||||||
|
|
||||||
|
// 1) Nested for range: inner continue on j==2, inner break on j==4, outer break on i==3
|
||||||
|
for i in range(0, 5)
|
||||||
|
for j in range(0, 5)
|
||||||
|
if j == 2
|
||||||
|
continue // skip j==2
|
||||||
|
if j == 4
|
||||||
|
break // stop inner loop at j==4
|
||||||
|
print(i * 10 + j) // e.g., i=0 -> 0, 1, 3
|
||||||
|
if i == 3
|
||||||
|
break // stop outer loop when i==3
|
||||||
|
|
||||||
|
// 2) Nested for-in over arrays: skip even values, break inner when hitting 7
|
||||||
|
rows = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
|
||||||
|
for row in rows
|
||||||
|
for v in row
|
||||||
|
if v % 2 == 0
|
||||||
|
continue // skip even numbers
|
||||||
|
print(v) // prints odd numbers in each row
|
||||||
|
if v == 7
|
||||||
|
break // stop inner loop when reaching 7
|
||||||
|
|
||||||
|
// 3) Nested while loops: inner continue/break and regular increments
|
||||||
|
number i = 0
|
||||||
|
while i < 3
|
||||||
|
number k = 0
|
||||||
|
while k < 5
|
||||||
|
if k == 1
|
||||||
|
k = k + 1
|
||||||
|
continue // skip k==1
|
||||||
|
print(i * 100 + k)
|
||||||
|
if k == 3
|
||||||
|
break // exit inner loop at k==3
|
||||||
|
k = k + 1
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
/* Expected output:
|
||||||
|
0
|
||||||
|
1
|
||||||
|
3
|
||||||
|
10
|
||||||
|
11
|
||||||
|
13
|
||||||
|
20
|
||||||
|
21
|
||||||
|
23
|
||||||
|
30
|
||||||
|
31
|
||||||
|
33
|
||||||
|
1
|
||||||
|
3
|
||||||
|
5
|
||||||
|
7
|
||||||
|
0
|
||||||
|
2
|
||||||
|
3
|
||||||
|
100
|
||||||
|
102
|
||||||
|
103
|
||||||
|
200
|
||||||
|
202
|
||||||
|
203
|
||||||
|
*/
|
||||||
Loading…
Add table
Add a link
Reference in a new issue