Funge-98 in Depth

Repeating Instructions with Iterate

How the Funge-98 iterate instruction repeats the next instruction a given number of times, with a traced example, edge cases and where it shortens code.

On this page
  1. What the instruction does
  2. A traced example
  3. Details worth knowing
  4. Where it helps
  5. Where it does not help
  6. Testing and debugging

What the instruction does

The iterate instruction is written k. It pops a number from the stack and then runs the next instruction that many times. Only one instruction is repeated: the one after the k. When the repetitions are finished, the pointer carries on past that instruction as normal.

This is a small convenience, but it removes a surprising amount of clutter. In Befunge-93, repeating a stack operation five times meant either writing it five times or building a loop that counted down. With iterate, the same thing is the count, the k, and the instruction.

A traced example

Here is a one-row program that prints five asterisks:

'*4k:5k,@

Reading it from left to right:

  1. The apostrophe pushes the character in the next cell, so the asterisk (value 42) goes on the stack, and the pointer skips over that cell.
  2. The digit 4 is pushed.
  3. The k pops the 4 and repeats the next instruction, the duplicate :, four times. The stack now holds five copies of 42.
  4. The digit 5 is pushed.
  5. The next k pops the 5 and repeats the output-character instruction , five times, printing one asterisk each time and using up all five copies.
  6. The @ ends the program.

The output is five asterisks. A common slip is to ask for as many duplications as copies you want in total, forgetting that the original already counts. Four extra copies plus the original is five.

Details worth knowing

  • Zero skips. If the count is zero, the next instruction is not run at all, and the pointer moves beyond it. This turns k into a conditional skip, which is occasionally handy.
  • Negative counts. What happens with a negative count is a place where behaviors may vary, and the specification treats it as a case to avoid or to check in your interpreter. Do not depend on it.
  • Spaces and markers between. The instruction k looks for the next real instruction, so spaces and sections skipped with the semicolon marker generally do not count as the target. That keeps spacing flexible, but it also means the repeated instruction can be further away than it looks.
  • Which instruction is repeated. It repeats an instruction, not a cell’s worth of movement. The pointer does not travel to execute each repetition; the same instruction simply runs again in place.
  • Repeating a direction change. Repeating an instruction that moves or turns the pointer is allowed, but the results are usually not worth the confusion. Repeating a turn instruction twice in a row, for instance, is the same as turning around.
  • Repeating k itself. An iterate whose target is another iterate is legal in principle, but is a good candidate for being clever at the expense of clarity.

Where it helps

Building up the stack. As in the example, a few duplicates or pushes are quick to write. Pushing a run of zeros as padding, or clearing several values with a repeated pop, takes a handful of characters.

Printing repeated characters. Lines of dashes, indentation or borders in text art need the same character many times. A count in front of the print instruction is clearer than a separate loop.

Applying an operation across a block. If several values sit on the stack and each should be treated the same way, repeating one instruction can be enough. Be careful here: many stack operations act on the top elements only, so repetition must be designed to rotate or consume values in a useful order.

Compact programs. For code golf and other size-limited puzzles, iterate is one of the more useful additions, since it can replace several characters of repeated instructions with a few.

Where it does not help

A count must be on the stack when k runs. If the number of repetitions is not known until later, or depends on a comparison, a loop is still the better tool. The iterate instruction cannot stop early, and it cannot do different things in different rounds. Anything that needs a test between steps belongs in a loop built with conditional branching, either as a geometric cycle or as a counter that decrements.

It also pays to think about readability. A program full of k becomes dense, and the pointer-path pictures that make grid programs pleasant to follow lose their value when the real work happens inside one cell. Use it where it replaces plain repetition, and use ordinary loops where there is real logic.

Testing and debugging

If a repeated instruction misbehaves, check these in order:

  1. Is the count on top of the stack at the moment k runs, rather than buried beneath another value?
  2. Is the instruction after k really the one you meant, given spaces and skipped sections?
  3. Does the repeated instruction need more values on the stack than the first repetition leaves for the second? Running out may produce zeros silently, and that can look like a logic bug elsewhere.

Tracing the first two repetitions by hand, writing the stack after each, almost always shows the issue. Those silent zeros are the same family of problem described under stack underflow, and the habit of watching stack depth carries over directly.

The complete guide to funge-98 in depth

Funge-98 Features in Depth
Read the guide

Keep exploring

Guides from other topics

All topics →