Some of Sunday's edits have been lost. The edits from Saturday that were reverted have been restored. Site is now hosted on prgmr.com. Thank you for your patience. This notice will be removed one week from posting. --Michael Mol 18:12, 7 March 2010 (UTC)

Loops/Infinite

From Rosetta Code

Jump to: navigation, search
Loops/Infinite is a programming task. Visitors like you are encouraged to solve it according to the task description, using any language they may happen to know.
Add to BlogMarksAdd to del.icio.usAdd to diggAdd to NewsvineAdd to redditAdd to Slashdot
Specifically print out "SPAM" followed by a newline in an infinite loop.

Contents

[edit] ActionScript

while (true) {
trace("SPAM");
}

[edit] Ada

loop
Put_Line("SPAM");
end loop;

[edit] ALGOL 68

DO
printf($"SPAM"l$)
OD

Or the classic "dynamic halt":

loop x:
printf($"SPAM"l$);
loop x

[edit] AmigaE

PROC main()
LOOP
WriteF('SPAM')
ENDLOOP
ENDPROC

[edit] AutoHotkey

Loop
MsgBox SPAM `n

[edit] AWK

BEGIN {
while(1) {
print "SPAM"
}
}

[edit] BASIC

Works with: QuickBasic version 4.5 Old-fashioned syntax:

WHILE 1
PRINT "SPAM"
WEND

Standard BASIC:

DO
PRINT "SPAM"
LOOP

Also

FOR i = 1 TO 10 STEP 0
PRINT "SPAM"
NEXT i

With classic (minimal) BASIC, the standard way to make an infinite loop would be:

10 PRINT "SPAM"
20 GOTO 10

[edit] Befunge

Because the 2-D code space is toroidal, all loops are infinite unless explicitly stopped with @.

55+"MAPS",,,,,

[edit] Brainf***

++++++++++[->++++++>++++++++>+<<<]>+++++>
[+++.---.<.>---.+++>.<]

[edit] C

while(1) puts("SPAM\n");

or

 for(;;) puts("SPAM\n");

or

do { puts("SPAM\n"); } while(1);

[edit] C++

Translation of: C

while (true)
std::cout << "SPAM\n";

or

for (;;)
std::cout << "SPAM\n";

or

do
std::cout << "SPAM\n";
while (true);

[edit] C#

while (true)
{
Console.WriteLine("SPAM");
}

[edit] ColdFusion

This will result in a JRun Servlet Error and heap dump.

With tags:

<cfloop condition = "true NEQ false">
SPAM
</cfloop>

With script:

<cfscript>
while( true != false )
{
writeOutput( "SPAM" );
}
</cfscript>

[edit] Clojure

(loop [] (println "SPAM") (recur))

[edit] Common Lisp

(loop (write-line "SPAM"))

[edit] D

while(true) writefln("SPAM") ;
for(;;) writefln("SPAM") ;
l: writefln("SPAM"); goto l;

[edit] E

while (true) {
println("SPAM")
}
def f() {
println("SPAM")
f <- ()
}
f <- ()

The difference between these is that in the second, other activities can be interleaved with the loop; in the first, no other processing will occur in this vat.

[edit] Erlang

-module (main).
-export ([main/1]).
 
main(Any) ->
io:fwrite("SPAM~n",[]),
main(Any)

[edit] Factor

: spam ( -- ) "SPAM" print spam ;
: spam ( -- ) [ "SPAM" print t ] loop ;

[edit] FALSE

[1]["SPAM
"]#

[edit] Forth

: email   begin ." SPAM" cr again ;

[edit] Fortran

Works with: Fortran version 90 and later

DO 
WRITE(*,*) "SPAM"
END DO

Although deprecated GOTO is still available

10 WRITE(*,*) "SPAM"
GOTO 10

[edit] Go

package main
import "fmt"
func main() {
for {
fmt.Printf("SPAM\n")
}
}

[edit] Groovy

while (true) {
println 'SPAM'
}

[edit] Haskell

forever (putStrLn "SPAM")

[edit] Icon

procedure main()
every write(|"SPAM")
end

[edit] IDL

while 1 do print,'SPAM'

[edit] J

-@:(][ 1!:2&2@('SPAM'"_)) (^:_) 1

Alternatively,

smoutput bind 'SPAM'^:1e99 ''

This implementation relies on numeric inaccuracies in IEEE floating point notation. For example, 1+1e98 is exactly equal to 1e98. That said, 1e99 iterations would still be significantly longer than the practical life of any machine anyone would care to dedicate to this task.

[edit] Java

while(true){
System.out.println("SPAM");
}
for(;;){
System.out.println("SPAM");
}

[edit] JavaScript

for (;;) print("SPAM");
while (true) print("SPAM");

[edit] Joy

DEFINE loop == [1] swap while.
 
["SPAM\n" putchars] loop.

[edit] Lisaac

The "lisaac" compiler apparently doesn't like infinite loops. Using a static slot (variable) seems to be the only way to get away with "Recursivity without end (call_slot)." error.

- i : INTEGER;
i := 1;
{ i = 1 }.while_do {
"SPAM\n".print;
};

[edit] Logo

forever [print "SPAM]

[edit] Lua

 
while 1 do
print("SPAM")
end
 

[edit] M4

define(`spam',`SPAM
spam')
spam

[edit] Make

spam:
@echo SPAM
$(MAKE)

[edit] MAXScript

while true do print "SPAM\n"

[edit] Metafont

forever: message "SPAM"; endfor end

[edit] Modula-3

LOOP
IO.Put("SPAM\n");
END;

[edit] MOO

while (1)
player:tell("SPAM");
endwhile

[edit] OCaml

while true do
print_endline "SPAM"
done

or

let rec inf_loop() =    
print_endline "SPAM";
inf_loop()
in
inf_loop()

Seen like this it looks like the "too much functional" danger when a "while" loop looks far simpler, but the functional loop may be useful to provide data to the next loop without using mutable variable.

[edit] Octave

while(1)
disp("SPAM")
endwhile

[edit] Oz

for do
{Show 'SPAM'}
end

[edit] Pascal

while true do
writeln('SPAM');

Alternatively:

repeat
writeln('SPAM')
until false;

[edit] Perl

print "SPAM\n" while 1;

[edit] Perl 6

Works with: Rakudo version #21 "Seattle"

loop {
say 'SPAM';
}

[edit] PHP

while(1)
echo "SPAM\n";

[edit] PicoLisp

(loop (prinl "SPAM"))

[edit] Pike

int main(){
while(1) write("SPAM\n");
}

[edit] PL/I

 
do forever;
put list ('SPAM'); put skip;
end;
 

[edit] Pop11

while true do
printf('SPAM', '%p\n');
endwhile;

[edit] PowerShell

for () {
"SPAM"
}

[edit] Prolog

repeat, write('SPAM'), nl, fail.

[edit] PureBasic

Works with: PureBasic version 4.41

Repeat 
PrintN("SPAM")
ForEver

[edit] Python

while 1:
print "SPAM"

Note: one can also use: "True" or any other non-false value. In Python the following values are false: 0, "" (empty string), (,) and {} and [] (empty tuples, dictionaries or lists), None (the special object), and the False object. Any non-empty collection or string or non-zero numeric value is considered "True"

[edit] R

Note that the default R Gui buffers outputs before pushing them to the screen. To see this run either run in terminal mode, right click on the GUI window and deselect "Buffered Output" prior to execution, or add a call to flush.console() in the loop.

repeat print("SPAM")

[edit] REBOL

forever [print "SPAM"]


[edit] REXX

do forever
say "SPAM"
end

[edit] Ruby

loop do
puts "SPAM"
end

[edit] Scheme

(do ()
(#f)
(display "SPAM")
(newline))

[edit] Slate

[inform: 'SPAM'] loop

[edit] Smalltalk

[ true ] whileTrue: [ 'SPAM' displayNl ]

[edit] SNUSP

@\>@\>@\>@\>++++++++++===!/ < < < < \
| | | \M=@@@@+@+++++# \.>.>.>.>./
| | \A=@@+@@@@+++#
| \P=@@+@@+@@+++#
\S=@@+@+@@@+++#

[edit] Standard ML

while true do
print "SPAM\n";

or

let 
fun inf_loop () = (
print "SPAM\n";
inf_loop ()
)
in
inf_loop ()
end

Seen like this it looks like the "too much functional" danger when a "while" loop looks far simpler, but the functional loop may be useful to provide data to the next loop without using mutable variable.

[edit] Transact-SQL

WHILE 1=1 BEGIN
PRINT "SPAM"
END

[edit] Tcl

while true {
puts SPAM
}
# or
for {} 1 {} {
puts SPAM
}

[edit] TI-89 BASIC

Loop
Disp "SPAM"
EndLoop

[edit] UNIX Shell

Works with: Bourne Shell

Works with: Korn Shell

Works with: Public Domain Korn SHell

Works with: Almquist SHell

Works with: Debian Almquist SHell

Works with: Bourne Again SHell

Works with: Z Shell

while :; do echo SPAM; done

[edit] UnixPipes

 yes SPAM

[edit] Unlambda

 ``ci``s``s`kr``s``s``s``s`k.S`k.P`k.A`k.Mii

[edit] V

true [
'SPAM' puts
] while

[edit] Vedit macro language

while (1) {
Message("Spam\n")
}

or:

do {
Message("Spam\n")
} while (1)

or:

for (;1;) {
Message("Spam\n")
}

"Nearly infinite" loop can be done by using constant ALL (=1073741824) as repeat count:

Repeat (ALL) {
Message("Spam\n")
}

[edit] Visual Basic

Do
Debug.Print("SPAM")
Loop

[edit] Visual Basic .NET

Platform: .NET Works with: Visual Basic .NET version 9.0+

Do
Console.WriteLine("SPAM")
Loop
Personal tools
Google AdSense