4

I want to write this in algorithm2e pseudocode:

Do N times
   stuff that has to be done N times
end

or maybe

Repeat N times
   stuff that has to be done N times
end

Since the loop body does not depend on a loop index, introducing one (like in a typical "for" loop) seems overkill.

My current best solution is

\For{$N$ times}{
  do stuff
}

but the output, "for N times do" is unclear.

(there are Do and Repeat commands in algorithm2e, but they both use end conditions: Do...while and Repeat...until, so not what I'm looking for.)

  • According to the docs there's \Repeat, so you should have a look at the package's documentation. – TeXnician Jun 2 '18 at 17:18
  • There is indeed Repeat, but that gives "repeat ... until condition" – Wouter Jun 2 '18 at 17:21
4

It seems there is no such built-in loop. Luckily, you can create your own! In algoritm2e, there are loops "with the condition at the start", or "with the condition at the end". To create your own "at the start" loop, do:

\SetKwFor{RepTimes}{repeat}{times}{end}

Then, to use it:

\RepTimes{100}{
    foobar
}

rendered latex image

| improve this answer | |

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.