"Goto" loop

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

"Goto" loop

Megh
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,
Reply | Threaded
Open this post in threaded view
|

Re: "Goto" loop

barry rowlingson
2008/10/31 megh <[hidden email]>:
>
> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "Goto" loop

Peter Dalgaard
In reply to this post by Megh
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.

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "Goto" loop

barry rowlingson
2008/10/31 Peter Dalgaard <[hidden email]>:

> 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 .)

 Also see also the always wonderful xkcd comic on the subject:

http://xkcd.com/292/

Barry

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "Goto" loop

Patrick Burns
In reply to this post by Peter Dalgaard
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.


Patrick Burns
[hidden email]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "Goto" loop

Wacek Kusnierczyk
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])

vQ

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: "Goto" loop

Peter Dalgaard
In reply to this post by Patrick Burns
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.
>

Hmm, no, I don't think so. The switch expression belongs with structured
programming paradigms like if, while, for. (Also, by the way, the fact
that it can be implemented as a function call in R is a nice
illustration of what lazy evaluation means).

The other ones, say "break", cover situations where goto might be used
like this

while ( cond ){
    if(...) goto exit;
    ....
}
exit:
...


which can be implemented using plain if and while, but becomes rather
painful:

while (cond & !done){
    if (....) {
        done <- TRUE
    } else [
        .....
    }
}

Similarly, "continue" jumps to a virtual label at the end of (but
inside) the loop and return() corresponds to a jump to the end of a
function.




--
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.