go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlark/testdata/while.star (about)

     1  # Tests of Starlark while statement.
     2  
     3  # This is a "chunked" file: each "---" effectively starts a new file.
     4  
     5  # option:while
     6  
     7  load("assert.star", "assert")
     8  
     9  def sum(n):
    10  	r = 0
    11  	while n > 0:
    12  		r += n
    13  		n -= 1
    14  	return r
    15  
    16  def while_break(n):
    17  	r = 0
    18  	while n > 0:
    19  		if n == 5:
    20  			break
    21  		r += n
    22  		n -= 1
    23  	return r
    24  
    25  def while_continue(n):
    26  	r = 0
    27  	while n > 0:
    28  		if n % 2 == 0:
    29  			n -= 1
    30  			continue
    31  		r += n
    32  		n -= 1
    33  	return r
    34  
    35  assert.eq(sum(5), 5+4+3+2+1)
    36  assert.eq(while_break(10), 40)
    37  assert.eq(while_continue(10), 25)