Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

"While" loops

Hi, all!

With Harlowe, how can I exit a "for" loop based on a certain condition, thus simulating a "while" loop?

Thanks in advance.

Comments

  • When asking a question you need to state the name and version of the Story Format you are using, as answers can be different for each one. I will assume you are using v2.0.0 because the 1.x series does not have a (for:) macro.

    Harlowe 2.x's implementation of a for loop is designed to iterate through a collection, and I could not find anything in it's documentation to indicate that you could stop that iteration.

    You can however use a variable to control if anything is done during that each loop, as the following demonstrates.
    []<output|
    
    (set: $list to (array: "one", "two", "three", "four", "five"))
    (set: $continue to true)
    
    (for: each _item, ...$list)[
    	<!-- your condition check goes here. -->
    	(if: _item is "three")[
    		(set: $continue to false)
    	]
    
    	(if: $continue)[
    		<!-- the code applied on each iteration goes here. -->
    		(append: ?output)[<br>_item]
    	]
    ]
    
    warning: For reasons unknown, changing the $continue variable in above to be a temporary variable results in the (if:) condition always being true.
  • You're right, greyelf; I'll make a point of mentioning the version of Harlowe that I'm using, which is v2.
    For the loop itself, now: that's what I needed.
    Many thanks.
Sign In or Register to comment.