File: while.star

package info (click to toggle)
golang-starlark 0.0~git20240725.42030a7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,296 kB
  • sloc: makefile: 8; sh: 8
file content (37 lines) | stat: -rw-r--r-- 516 bytes parent folder | download | duplicates (2)
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
# Tests of Starlark while statement.

# This is a "chunked" file: each "---" effectively starts a new file.

# option:while

load("assert.star", "assert")

def sum(n):
	r = 0
	while n > 0:
		r += n
		n -= 1
	return r

def while_break(n):
	r = 0
	while n > 0:
		if n == 5:
			break
		r += n
		n -= 1
	return r

def while_continue(n):
	r = 0
	while n > 0:
		if n % 2 == 0:
			n -= 1
			continue
		r += n
		n -= 1
	return r

assert.eq(sum(5), 5+4+3+2+1)
assert.eq(while_break(10), 40)
assert.eq(while_continue(10), 25)