[R] "Goto" loop

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sat Nov 1 11:22:39 CET 2008

Patrick Burns wrote:
> Peter Dalgaard wrote:
>> megh wrote:
>>> Is there anything like "goto" loop, which exists in most computer
>>> programs?
>>> e.g. I am looking for this kind of stuff :
>>> if(i < 6) goto "step-02"
>>> Any idea?
>>> Regards,
>> It doesn't exist, but it can always be replaced by if() {} else {}
>> constructs. (You don't usually see goto in the class of "functional
>> programming languages" to which R belongs.  See also
>> http://en.wikipedia.org/wiki/Edsger_W._Dijkstra .)
>> R does have break, next, and return() which cover some common cases
>> where goto might be used with some reason.
> 'switch' might well be added to this list.

you can also use callCC (call with current continuation) to perform
non-local exits; it's not exactly the same as goto jumps, but might help
you if you really need this sort of trickery. 

r's man page on callCC is not particularly informative, you'd need an
external reference to learn more about continuations.  (try, e.g., the
excellent kent dybvig's 'the scheme programming language, free online at


[1] http://www.scheme.com/tspl3/

More information about the R-help mailing list