From d67c5418249c2fbccab30200c0086c61e08b30b1 Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 15 Sep 2025 03:34:49 +0200 Subject: [PATCH] Added nested loop example and some more comment in other examples. --- examples/arrays.fun | 13 +++++++ examples/arrays_advanced.fun | 19 +++++++++ examples/arrays_iter.fun | 25 ++++++++++++ examples/loops_break_continue.fun | 16 ++++++++ examples/nested_loops.fun | 64 +++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 examples/nested_loops.fun diff --git a/examples/arrays.fun b/examples/arrays.fun index 5986119..96a70f3 100755 --- a/examples/arrays.fun +++ b/examples/arrays.fun @@ -45,3 +45,16 @@ fun pair(x, y) p = pair(7, 9) print(p) // -> [7, 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 +*/ diff --git a/examples/arrays_advanced.fun b/examples/arrays_advanced.fun index f48a717..365fbc2 100755 --- a/examples/arrays_advanced.fun +++ b/examples/arrays_advanced.fun @@ -40,3 +40,22 @@ print(s2) // -> [1, 7, 3] tail = [10, 20] combined = arr + tail 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] +*/ diff --git a/examples/arrays_iter.fun b/examples/arrays_iter.fun index d04f0b4..e69a4c0 100644 --- a/examples/arrays_iter.fun +++ b/examples/arrays_iter.fun @@ -28,3 +28,28 @@ fun make() for v in make() 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 +*/ diff --git a/examples/loops_break_continue.fun b/examples/loops_break_continue.fun index 7f41b04..bde2f48 100755 --- a/examples/loops_break_continue.fun +++ b/examples/loops_break_continue.fun @@ -29,3 +29,19 @@ for x in arr print(x) // -> 1, 2, 4 if x == 4 break + +/* Expected output: +1 +3 +5 +7 +0 +1 +2 +4 +5 +6 +1 +2 +4 +*/ diff --git a/examples/nested_loops.fun b/examples/nested_loops.fun new file mode 100644 index 0000000..bacd73b --- /dev/null +++ b/examples/nested_loops.fun @@ -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 +*/