Loops/Infinite

From Rosetta Code

Jump to: navigation, search
Task
Loops/Infinite
You are encouraged to solve this task according to the task description, using any language you may know.
Specifically print out "SPAM" followed by a newline in an infinite loop.

Contents

[edit] 4DOS Batch

@echo off
do forever
echo SPAM
enddo

[edit] 6502 Assembly

Specific OS/hardware routines for printing are left unimplemented.

InfiniteLoop	LDX #0
PrintLoop: LDA MSG,x
JSR PrintAccumulator ;routine not implemented
INX
CPX #5
BNE PrintLoop
JMP InfiniteLoop
 
MSG .byte "SPAM", $0A

[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] Batch File

Using goto:

@echo off
:loop
echo SPAM
goto loop

Another variant which uses Windows NT's for statement:

Works with: Windows NT version 4 or later

for /l %%x in (1,0,2) do @echo SPAM

This essentially is a counted loop which starts at 1, increments by 0 and stops when the counter reaches 2.

[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" << std::endl;

or

for (;;)
std::cout << "SPAM" << std::endl;

or

do
std::cout << "SPAM" << std::endl;
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] F#

 
while true do
printfn "SPAM"
done
 
let rec forever () : unit =
printfn "SPAM" ; forever ()
 

[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] HicEst

DO i = 1, 1E20 ! for i with 16 or more digits:  i == i + 1 == loop infinite
WRITE() "SPAM"
ENDDO

[edit] Icon and Unicon

[edit] Icon

There are several ways to write infinite loops in Icon. The most straightforward would be with repeat.

procedure main()
repeat write("SPAM")
end

Alternately one could use one of these:

until &fail do write("SPAM")   # always fails, needs succeed to break
...
while write("SPAM") # always succeeds, needs failure to break
...
every write(|"SPAM") # generator always succeeds, needs failure to break
...
while write(|"SPAM") # this is a common mistake that results in an endless loop
...
while write(1 to 5) # a clearer version of the same mistake that generates endless 1's

[edit] Unicon

This Icon solution works in Unicon.

[edit] IDL

while 1 do print,'SPAM'

[edit] Io

loop("SPAM" println)

[edit] J

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

Alternatively,

smoutput bind 'SPAM'^:1e99 ''

This second 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] MUMPS

 
FOR WRITE "SPAM",!
 

[edit] Objeck

 
while(true) {
"SPAM"->PrintLine();
};
 

[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 Star version 2010.08

loop {
say 'SPAM';
}

In addition, there are various ways of writing lazy, infinite lists in Perl 6:

print "SPAM\n" xx *;      # repetition operator
print "SPAM\n", ~* ... *; # series operator
map {say "SPAM"}, ^Inf; # upto operator<lang>
 
=={{header|PHP}}==
<lang 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] PostScript

{}loop

[edit] PowerShell

for () {
"SPAM"
}

[edit] Prolog

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

[edit] PureBasic

[edit] Repeat/Forever

Repeat 
PrintN("SPAM")
ForEver

[edit] Goto

PrintIt:
PrintN("SPAM")
Goto PrintIt

[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". However, according to Python Wiki, for Python versions 2.3+ this variant is optimized by the interpreter and thus is the fastest.

[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] Sather

class MAIN is
main is
loop
#OUT + "Spam\n";
end;
end;
end;

[edit] Scala

while (true)
println("SPAM")

[edit] Scheme

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

[edit] Slate

[inform: 'SPAM'] loop

[edit] Smalltalk

[ true ] whileTrue: [ 'SPAM' displayNl ]

[edit] SNOBOL4

loop output = "SPAM" :(loop)
end

[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] SystemVerilog

program main;
initial forever $display("SPAM");
endprogram
 

[edit] Transact-SQL

WHILE 1=1 BEGIN
PRINT "SPAM"
END

[edit] Tcl

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

[edit] TI-83 BASIC

There are a few ways to achieve this in TI-83 BASIC

 
 :Goto 1
 :Lbl 1
 :Disp "SPAM"
 :Goto 1
 

Another way is by using a While loop

 
 :1→A
 :While A = 1
 :Disp "SPAM"
 

[edit] TI-89 BASIC

Loop
Disp "SPAM"
EndLoop

[edit] Trith

["SPAM" print] loop

[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
Support