Loops, break and continue.
This commit is contained in:
parent
879801cbfc
commit
0b48aa3e0c
2 changed files with 134 additions and 4 deletions
31
examples/loops_break_continue.fun
Executable file
31
examples/loops_break_continue.fun
Executable file
|
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
// break and continue examples
|
||||
|
||||
// 1) while loop: print odd numbers, stop after printing 7
|
||||
number i = 0
|
||||
while i < 10
|
||||
if i % 2 == 0
|
||||
i = i + 1
|
||||
continue
|
||||
print(i) // -> 1, 3, 5, 7
|
||||
if i > 5
|
||||
break
|
||||
i = i + 1
|
||||
|
||||
// 2) for range: skip 3, stop at 6
|
||||
for i in range(0, 8)
|
||||
if i == 3
|
||||
continue
|
||||
print(i) // -> 0, 1, 2, 4, 5, 6
|
||||
if i == 6
|
||||
break
|
||||
|
||||
// 3) for-in over array: skip 3, break at 4
|
||||
arr = [1, 2, 3, 4, 5]
|
||||
for x in arr
|
||||
if x == 3
|
||||
continue
|
||||
print(x) // -> 1, 2, 4
|
||||
if x == 4
|
||||
break
|
||||
Loading…
Add table
Add a link
Reference in a new issue