2

I need to break a for loop in smartpy, but break does not work. Is there any other solution I could use?

2

Michelson is a functional language. break statements have no (clean) sense.

LOOP_LEFT is an approximation for break statements, but in smartpy you can do the following:

import smartpy as sp

class MyContract(sp.Contract):
  def __init__(self, **kargs):
    self.init(**kargs)

  @sp.entry_point
  def ep(self):
    stop = sp.local("stop", False)
    sp.while ~ stop.value:
      sp.if self.data.value > 5:
        stop.value = True
      sp.else:
        self.data.value += 1
        
    
@sp.add_test(name = "Example")
def test():
  scenario = sp.test_scenario()
  c1 = MyContract(value = 0)
  scenario += c1
  c1.ep()
3
  • Thanks for your fast answer! I am trying to cut the keys list of a map into chunks (first 50 entries, then next fifty entries, till the end). That's why I needed a break statement. Unfortunately a while loop won't help me since I need to iterate through my keys list. Is there any other way to do that? Jun 16 at 19:49
  • 1
    That is not supported in Michelson, the instruction is tezos.gitlab.io/michelson-reference/#instr-ITER, and it iterates over all elements. You can have an IF that ignores elements when some condition is met. Jun 16 at 21:04
  • You could first filter the lists of keys, splitting the keys into multiple lists depending on what you are trying to do and then iterate over all of them. Jun 16 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.