[R] "Goto" loop

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Oct 31 11:59:45 CET 2008


2008/10/31 megh <megh700004 at yahoo.com>:
>
> Is there anything like "goto" loop, which exists in most computer programs?

Really? Not since 1968, I think:

http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF

> e.g. I am looking for this kind of stuff :
>
> if(i < 6) goto "step-02"
>
> Any idea?

 Thinking you need a 'goto' is a massive red flag in your program.
This red flag is waving to warn you of danger ahead. As well as
Dijkstra's somewhat technical reasons for abhoring such a language
construct, it will also make your program very difficult to
understand.

 R has for-loops, repeat-loops, while loops, and conditional
(if-then-else) structures. You can break out of loops with 'break',
and jump to the next iteration with 'next'.  There is no "goto":

 > help.search("goto")
 No help files found with alias or concept or title matching 'goto'
 using regular expression matching.

If you have been given a bolognese full of spaghetti code (which is
what we call anything with a generous sprinkling of goto statements)
and you want to rewrite it in R, you'll have some thinking to do. Or
just write it in Fortran which, since it dates from well before
Dijkstra's famous note, has GOTO statements you can use.

Barry



More information about the R-help mailing list