Loops/Infinite: Difference between revisions
Added Hare |
m →{{header|Hare}}: fixed <lang parameter> |
||
Line 1,052: | Line 1,052: | ||
=={{header|Hare}}== |
=={{header|Hare}}== |
||
<lang |
<lang hare>use fmt; |
||
export fn main() void = { |
export fn main() void = { |
Revision as of 00:52, 11 August 2022
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Print out SPAM followed by a newline in an infinite loop.
- Related tasks
- Loop over multiple arrays simultaneously
- Loops/Break
- Loops/Continue
- Loops/Do-while
- Loops/Downward for
- Loops/For
- Loops/For with a specified step
- Loops/Foreach
- Loops/Increment loop index within loop body
- Loops/Infinite
- Loops/N plus one half
- Loops/Nested
- Loops/While
- Loops/with multiple ranges
- Loops/Wrong ranges
11l
<lang 11l>L
print(‘SPAM’)</lang>
360 Assembly
This for sure will result in a severe WTO buffer shortage. <lang 360 Assembly> INFINITE CSECT , this PGM control section INFINITE AMODE 31 addressing mode 31 bit INFINITE RMODE ANY loader can load either 24 or 31
BAKR 14,0 stack caller's register contents LR 12,15 establish base LA 13,0 no savearea USING INFINITE,12 base to assembler LA 10,1 1 in reg 10 LA 11,2 2 in reg 11
LOOP EQU *
CR 10,11 1==2? BE RETURN Yes, exit. WTO 'SPAM',ROUTCDE=11 print SPAM to syslog B LOOP No, check again.
RETURN PR , return to caller
END INFINITE
</lang>
4DOS Batch
<lang 4dos>@echo off do forever
echo SPAM
enddo</lang>
6502 Assembly
Specific OS/hardware routines for printing are left unimplemented. <lang 6502asm>InfiniteLoop LDX #0 PrintLoop: LDA MSG,x JSR PrintAccumulator ;routine not implemented INX CPX #5 BNE PrintLoop BEQ InfiniteLoop
MSG .byte "SPAM", $0A</lang>
6800 Assembly
<lang> .cr 6800
.tf spam6800.obj,AP1 .lf spam6800
- =====================================================;
- Infinite SPAM loop for the Motorola 6800 ;
- by barrym 2013-04-10 ;
- -----------------------------------------------------;
- Prints the message "SPAM" repeatedly to an ascii ;
- terminal (console) connected to a 1970s vintage ;
- SWTPC 6800 system, which is the target device for ;
- this assembly. ;
- Many thanks to
- ;
- swtpc.com for hosting Michael Holley's documents! ;
- sbprojects.com for a very nice assembler! ;
- swtpcemu.com for a very capable emulator! ;
- reg x is the string pointer ;
- reg a holds the ascii char to be output ;
- -----------------------------------------------------;
outeee = $e1d1 ;ROM: console putchar routine
.or $0f00
- -----------------------------------------------------;
main ldx #string ;Point to the string
bra puts ; and print it
outs jsr outeee ;Emit a as ascii
inx ;Advance the string pointer
puts ldaa ,x ;Load a string character
bne outs ;Print it if non-null bra main ;else restart
- =====================================================;
string .as "SPAM",#13,#10,#0
.en</lang>
68000 Assembly
Hardware-specific routines for I/O are left unimplemented and just displayed as a subroutine, as this is not the focus of the task. <lang 68000devpac>doSPAM: LEA Message,A0 JSR PrintString JMP doSPAM
Message: DC.B "SPAM",13,10,0 EVEN</lang>
8086 Assembly
Loading Immediates
<lang asm>Spam: mov ah,02h mov dl,'S' ;VASM replaces a character in single quotes with its ascii equivalent int 21h ;Print Char routine
mov dl,'P' int 21h
mov dl, 'A' int 21h
mov dl, 'M' int 21h
mov dl,13 ;Carriage Return int 21h
mov dl,10 ;New Line int 21h
jmp Spam</lang>
Loading From A Data Source
<lang asm>mov ah, 02h ;prep int 21h for printing to screen mov ax, seg SpamMessage ;load into ax whatever segment the address of our message is in. mov ds, ax ;segment registers on the original 8086 must be loaded from a register
cld ;clear the direction flag, this makes commands like "lodsb" auto-increment
SpamOuter: mov si, offset SpamMessage ;load the address of SpamMessage into the source index SpamInner: lodsb ;mov al,[ds:si] and increment si by 1. cmp al,0 ;is this the terminator? jz SpamOuter ;point si to the beginning of the message again mov dl,al ;the DOS interrupt for printing requires the desired character to be in DL int 21h ;print the chosen character to the screen jmp SpamInner
SpamMessage db "SPAM",13,10,0</lang>
8th
One way: <lang forth>
- inf "SPAM\n" . recurse ;
</lang> Another way: <lang forth>
- inf repeat "SPAM\n" . again ;
</lang>
AArch64 Assembly
<lang AArch64 Assembly> /* ARM assembly AARCH64 Raspberry PI 3B */ /* program infinite64.s */
/*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessage: .asciz "SPAM\n" /*********************************/ /* code section */ /*********************************/ .text .global main
main:
loop:
ldr x0,qAdrszMessage bl affichageMess b loop
qAdrszMessage: .quad szMessage
/********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" </lang>
ACL2
<lang Lisp>(defun spam ()
(declare (xargs :mode :program)) (if nil nil (prog2$ (cw "SPAM~%") (spam))))</lang>
Action!
<lang Action!>PROC Main()
DO PrintE("SPAM") OD
RETURN</lang>
- Output:
Screenshot from Atari 8-bit computer
SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM ...
ActionScript
<lang actionscript>while (true) {
trace("SPAM");
}</lang>
Ada
<lang ada>loop
Put_Line("SPAM");
end loop;</lang>
Agena
Tested with Agena 2.9.5 Win32 <lang agena>do
print( "SPAM" )
od</lang>
Aime
<lang aime>while (1) {
o_text("SPAM\n");
}</lang>
ALGOL 60
Based on the 1962 Revised Repport on ALGOL:
begin integer i; for i:=1 step 0 until 2 do outtext("spam") end
<lang algol60>'BEGIN' 'COMMENT' Loops/Infinite - Algol60 - 23/06/2018;
'INTEGER' I; 'FOR' I := 1 'STEP' 0 'UNTIL' 2 'DO' OUTSTRING(1,'('SPAM')')
'END'</lang>
ALGOL 68
<lang algol68>DO
printf($"SPAM"l$)
OD</lang> Or the classic "dynamic halt": <lang algol68>loop x:
printf($"SPAM"l$);
loop x</lang>
ALGOL W
<lang algolw>begin
for i := 1 step 0 until 2 do write( "SPAM" )
end.</lang>
AmigaE
<lang amigae>PROC main()
LOOP WriteF('SPAM') ENDLOOP
ENDPROC</lang>
AppleScript
<lang applescript>repeat
log "SPAM"
end repeat</lang>
ARM Assembly
<lang ARM_Assembly> .global main
main:
loop:
ldr r0, =message bl printf b loop
message:
.asciz "SPAM\n"
</lang>
ArnoldC
<lang ArnoldC>IT'S SHOWTIME STICK AROUND @NO PROBLEMO TALK TO THE HAND "SPAM" CHILL YOU HAVE BEEN TERMINATED</lang>
Arturo
<lang rebol>while [true] [ print "SPAM" ]</lang>
AutoHotkey
<lang autohotkey>Loop
MsgBox SPAM `n</lang>
AWK
<lang awk>BEGIN {
while(1) { print "SPAM" }
}</lang>
Axe
Warning: running this program will cause you to need to reset your calculator, thereby losing any user data stored in RAM.
<lang axe>While 1
Disp "SPAM",i
End</lang>
BASIC
Old-fashioned syntax: <lang qbasic>while 1
print "SPAM"
wend</lang>
Standard BASIC: <lang qbasic>do
print "SPAM"
loop</lang>
Also <lang qbasic>for i = 1 to 10 step 0
print "SPAM"
next i</lang>
The most intuitive method is to use the GOTO
statement.
<lang gwbasic>10 print "SPAM" 20 goto 10</lang>
Generally, using GOSUB
in place of GOTO
is incorrect. Some programming bugs come about when a GOSUB
causes a potentially infinite loop, however, eventually stack memory will fill up and cause a terminating error as shown in this Commodore BASIC example:
ready. new ready. 10 print "spam! ";:gosub 10 run spam! spam! spam! spam! spam! spam! spam ! spam! spam! spam! spam! spam! spam! sp am! spam! spam! spam! spam! spam! spam! spam! spam! spam! spam! spam! spam! ?out of memory error in 10 ready. █
The solution is to keep the stack empty, however, this will also clear all variables used and prevent the use of RETURN
ing from the "subroutine". This is accomplished with the CLEAR
(or CLR
in Commodore BASIC) placed at the start of the loop.
<lang gwbasic>10 clr:print "Commodore Spam! ";:gosub 10</lang> <lang gwbasic>10 clear : print "Apple Spam! ";: gosub 10</lang>
Rather than a GOTO
, instead we can use a FOR... NEXT
statement:
<lang gwbasic>10 for i = 1 to 10 step 0 : rem A zero step makes the loop infinite 20 print "SPAM"; 30 next i</lang>
In most cases, we can also call the RUN
command from within the program.
<lang gwbasic>10 print "Spam! "; 20 run</lang>
IF... THEN
has an implied GOTO
on some BASICs...
<lang gwbasic>10 print "SPAM SPAM! ";:if 1 then 10</lang>
Applesoft BASIC
<lang ApplesoftBasic>FOR I = 0 TO 1 STEP 0 : PRINT "SPAM" : NEXT</lang>
BASIC256
<lang BASIC256>while true
print "SPAM"
end while</lang>
Commodore BASIC
In addition to the general examples listed above for BASIC, there is a trick to get a Commodore BASIC program to endlessly loop its listing. All of the lines of code are a linked list in RAM. The trick is accomplished by modifying the pointer to the next line, which is recorded at the very start of each tokenized BASIC line. Instead of it pointing to the next line, you can make it point to a previous line, or itself. This will affect execution when any GOTO
or GOSUB
needs to reference any line number after the affected line, since the line search will be corrupted (and endless...)
For example, on the Commodore 64, BASIC program storage begins at $0800 (2048) with a NULL byte, the first line begins at $0801 with the little-endian pointer to the memory address that begins the next line. After entering the short program, POKE
a 1 into the low byte portion of the pointer (location $0801) causing complete pointer value to be $0801... pointing to itself. Then run or list the program for endless looping fun.
Other similarly structured BASICs based on the early Microsoft BASIC (where the LIST
routine follows the linked list pointers) can be manipulated in the same manner if it is known where BASIC program memory starts.
ready. 10 rem there is way too much spam in this program! 20 print "spam!!";:goto 10 poke 2049,1 ready. run spam!!spam!!spam!!spam!!spam!!spam!!spam!!spam!! spam!!spam!!spam!!spam!!spam!!spam!!spam!!spam!! spam!!spam!!spam!!spam!!spam!!spam!!spam!!spam!! spam!!spam!!spam!!spam!!spam!!spam!!spam!!spam!! spam!!spam!!spam!!spam!!spam!!spam!!spam!!spam!! break in 10 ready. list 10 rem there is way too much spam in this program! 10 rem there is way too much spam in this program! 10 rem there is way too much spam in this program! 10 rem there is way too much spam in this program! 10 rem there is way too much spam in this program! 10 rem there is way too much spam in this program! break ready. █
IS-BASIC
<lang IS-BASIC>100 DO 110 PRINT "SPAM" 120 LOOP</lang>
QB64
<lang QB64>'Using Do loop Do 'Alternatively this could have a conditional, "Do While 1"
Print "SPAM"
Loop
'Using While loop While 1
Print "SPAM"
Wend</lang>
True BASIC
<lang qbasic>DO
PRINT "SPAM"
LOOP END</lang>
Yabasic
<lang Yabasic>do
print "SPAM"
loop</lang>
O también <lang Yabasic>while true
print "SPAM"
wend</lang>
Batch File
Using goto
:
<lang dos>@echo off
- loop
echo SPAM
goto loop</lang>
Another variant which uses Windows NT's for
statement:
<lang dos>for /l %%x in (1,0,2) do @echo SPAM</lang>
This essentially is a counted loop which starts at 1
, increments by 0
and stops when the counter reaches 2
.
BBC BASIC
<lang bbcbasic> REPEAT
PRINT "SPAM" UNTIL FALSE</lang>
bc
<lang bc>while (1) "SPAM "</lang>
BCPL
<lang BCPL>get "libhdr"
let start() be writes("SPAM*N") repeat</lang>
beeswax
<lang beeswax>_>`SPA`p
bN`M`<</lang>
Befunge
Because the 2-D code space is toroidal, all loops are infinite unless explicitly stopped with @. <lang befunge>55+"MAPS",,,,,</lang>
blz
<lang blz>while true
print("SPAM")
end</lang>
bootBASIC
Using goto
:
<lang bootBASIC>10 print "SPAM"
20 goto 10</lang>
Using run
:
<lang bootBASIC>10 print "SPAM"
20 run</lang>
BQN
The main way of performing an infinite loop in BQN is using recursion.
<lang BQN>{𝕊 •Out 𝕩}"SPAM"</lang>
will likely end in a stack overflow.
Bracmat
<lang bracmat>whl'out$SPAM</lang>
Brainf***
Optimized for code size: <lang bf>++++++++++[->++++++>++++++++>+<<<]>+++++> [+++.---.<.>---.+++>.<]</lang>
Optimized for execution speed: <lang bf>10++++++++++ [-> 8++++++++ > 8++++++++ > 6++++++ > 8++++++++ > 1+ <<<<<]> 83+++ > 80 > 65+++++ > 77--- <<< [.>.>.>.>.<<<<]</lang>
Brat
<lang brat>loop { p "SPAM" }</lang>
C
<lang c>while(1) puts("SPAM");</lang> or <lang c> for(;;) puts("SPAM");</lang> or <lang c>do { puts("SPAM"); } while(1);</lang> or <lang c>while(puts("SPAM"));</lang> or <lang c> spam: puts("SPAM"); goto spam; </lang>
C#
<lang csharp>while (true) {
Console.WriteLine("SPAM");
}</lang>
C++
<lang cpp>while (true)
std::cout << "SPAM\n";</lang>
or <lang cpp>for (;;)
std::cout << "SPAM\n";</lang>
or <lang cpp>do
std::cout << "SPAM\n";
while (true);</lang>
Chapel
<lang chapel>while true do writeln("SPAM");</lang>
ChucK
<lang> while(true) <<<"SPAM">>>; </lang>
Clojure
<lang lisp>(loop [] (println "SPAM") (recur))</lang>
COBOL
<lang cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. Spam.
PROCEDURE DIVISION. PERFORM UNTIL 1 <> 1 DISPLAY "SPAM" END-PERFORM
GOBACK .</lang>
OpenCOBOL supports a FOREVER
clause for PERFORM
which will have the same effect.
CoffeeScript
<lang coffeescript>loop
console.log 'SPAM'
</lang>
ColdFusion
This will result in a JRun Servlet Error and heap dump.
With tags: <lang cfm><cfloop condition = "true NEQ false">
SPAM
</cfloop></lang> With script: <lang cfm><cfscript>
while( true != false ) { writeOutput( "SPAM" ); }
</cfscript></lang>
Comal
<lang Comal>LOOP
PRINT "SPAM"
ENDLOOP</lang>
Common Lisp
<lang lisp>(loop (write-line "SPAM"))</lang>
Using DO
<lang lisp> (do () ; Not initialization
(nil) ; Not break condition (print "SPAM")) ; On every loop as requested
</lang>
- Output:
"SPAM" ...
Corescript
<lang Corescript>
- top
print Spam! goto top </lang>
Cowgol
<lang cowgol>include "cowgol.coh";
loop
print("Spam\n");
end loop;</lang>
Crystal
<lang crystal>loop do
puts "SPAM"
end</lang>
Using while
/until
:
<lang crystal>while true
puts "SPAM"
end</lang> <lang crystal>until false
puts "SPAM"
end</lang>
Using an infinite range: <lang crystal>(0..).each do
puts "SPAM"
end</lang>
D
Some common ways to create an infinite printing loop: <lang d>import std.stdio;
void main() {
while (true) writeln("SPAM");
}</lang>
<lang d>import std.stdio;
void main() {
do writeln("SPAM"); while (true);
}</lang>
<lang d>import std.stdio;
void main() {
for ( ; ; ) writeln("SPAM");
}</lang>
<lang d>import std.stdio;
void main() {
LOOP: writeln("SPAM"); goto LOOP;
}</lang>
Dart
<lang> main() {
while(true) { print("SPAM"); }
} </lang>
dc
<lang dc>[[SPAM ]P dx]dx</lang>
This loop is a tail-recursive function. The program pushes the function on the stack, the outer dx makes the first call, and the inner dx makes each recursive call.
DCL
<lang DCL>$ loop: $ write sys$output "SPAM" $ goto loop</lang>
Delphi
See Pascal
Draco
<lang draco>proc nonrec main() void:
while true do writeln("SPAM") od
corp</lang>
DWScript
<lang Delphi>while True do
PrintLn('SPAM');</lang>
Dyalect
<lang Dyalect>while true {
print("SPAM")
}</lang>
Déjà Vu
<lang dejavu>while true: !print "SPAM"</lang> Infinite recursion thanks to tail calls: <lang dejavu>labda: !print "SPAM" recurse call</lang>
E
<lang e>while (true) {
println("SPAM")
}</lang>
<lang e>def f() {
println("SPAM") f <- ()
} f <- ()</lang>
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.
EDSAC order code
The EDSAC instruction set does not include an unconditional jump: it is necessary to synthesize it by using either an E
"branch on accumulator sign bit clear" or F
"branch on accumulator sign bit set" order, in circumstances where the condition is guaranteed to be met. For this specific task, guaranteeing it is trivial: printing characters does not change the contents of the accumulator at all. The solution presented here, however, is more general. We use a T
"transfer and clear" order to store the accumulator's contents in storage address θ+17, then jump back to the beginning of the loop and reload the accumulator with an A
"add" order. Note that the storage address used as a temporary variable should be set to zero on entry to the loop.
<lang edsac>[ Infinite loop
=============
A program for the EDSAC
Works with Initial Orders 2 ]
T56K GK
O10@ [ letter shift ]
[ 1 ] A17@ [ a += C(17@) ]
O11@ O12@ O13@ O14@ O15@ O16@ T17@ [ C(17@) = a; a = 0 ] E1@ [ if a >= 0 goto 1@ ]
[ 10 ] *F [ 11 ] SF [ 12 ] PF [ 13 ] AF [ 14 ] MF [ 15 ] @F [ carriage return ] [ 16 ] &F [ line feed ]
[ 17 ] PF
EZPF</lang>
Ela
Direct Approach
<lang ela>open monad io
loop () = do
putStrLn "SPAM" loop ()
loop () ::: IO</lang>
Non-strict version
<lang ela>open monad io
xs = "SPAM"::xs
takeit 0 _ = do return () takeit num (x::xs) = do
putStrLn x takeit (num - 1) xs
_ = takeit 10 xs ::: IO</lang>
Elena
ELENA 4.x: <lang elena>public program() {
while (true) { console.writeLine:"spam" }
}</lang>
Elixir
<lang elixir>defmodule Loops do
def infinite do IO.puts "SPAM" infinite end
end
Loops.infinite</lang> or <lang elixir>Stream.cycle(["SPAM"]) |> Enum.each(&IO.puts &1)</lang>
Emacs Lisp
<lang lisp>(while t
(message "SPAM"))</lang>
Erlang
<lang erlang> -module (main). -export ([main/0]).
main() ->
io:fwrite( "SPAM~n" ), main().
</lang>
ERRE
<lang ERRE> LOOP
PRINT("SPAM")
END LOOP </lang> You can use also WHILE TRUE..END WHILE or REPEAT...UNTIL FALSE loops.
Euphoria
<lang Euphoria> while 1 do
puts(1, "SPAM\n")
end while </lang>
F#
<lang fsharp> // Imperative Solution while true do
printfn "SPAM"
// Functional solution let rec forever () : unit =
printfn "SPAM" forever ()
</lang>
Factor
Tail recursion: <lang factor>: spam ( -- ) "SPAM" print spam ;</lang> Looping combinators: <lang factor>[ "SPAM" print t ] loop</lang> <lang factor>USE: combinators.extras [ "SPAM" print ] forever</lang>
FALSE
<lang false>[1]["SPAM "]#</lang>
Fantom
<lang fantom> class Main {
public static Void main () { while (true) { echo ("SPAM") } }
} </lang>
Fermat
<lang fermat>while 1 do !!'SPAM'; od</lang>
Fish
<lang fish>a"MAPS"ooooo</lang>
Forth
<lang forth>: email begin ." SPAM" cr again ;</lang>
Fortran
FORTRAN 77 <lang fortran>
10 WRITE(*,*) 'SPAM' GO TO 10 END
</lang>
Fortran 90 <lang fortran> program spam
implicit none do write(*,*) 'SPAM' end do
end program spam </lang>
Fortress
<lang fortress> component loops_infinite
export Executable run() = while true do println("SPAM") end
end </lang>
FreeBASIC
<lang freebasic>' FB 1.05.0
Do
Print "SPAM"
Loop</lang>
Frink
<lang frink> while true
println["SPAM"]
</lang>
FutureBasic
Loop de loop -- whose great idea was this? <lang futurebasic>include "NSLog.incl"
dispatchglobal
while 1 NSLog(@"SPAM") wend
dispatchend
HandleEvents</lang>
Gambas
Click this link to run this code <lang gambas>Public Sub Main()
Do
Print "SPAM"
Loop
End</lang>
GAP
<lang gap>while true do
Print("SPAM\n");
od;</lang>
GB BASIC
<lang GB BASIC>10 print "SPAM" 20 goto10</lang>
GlovePIE
GlovePIE does not natively support multiple lines of output. As such, this code continuously changes the single line of output to SPAM. The below code does this without specifying an infinite loop because all GlovePIE scripts loop indefinitely until the program is stopped. <lang GlovePIE>debug = "SPAM"</lang>
GML
<lang GML>while(1)
show_message("SPAM")</lang>
Go
<lang go>package main
import "fmt"
func main() { for { fmt.Printf("SPAM\n") } }</lang>
Groovy
<lang groovy>while (true) {
println 'SPAM'
}</lang>
Halon
<lang halon>forever {
echo "SPAM";
}</lang> or (due to optimizations, these are equally fast) <lang halon>while (true) {
echo "SPAM";
}</lang>
Hare
<lang hare>use fmt;
export fn main() void = { for (true) { fmt::println("SPAM")!; }; };</lang>
Haskell
<lang haskell>forever (putStrLn "SPAM")</lang> or <lang haskell>import Control.Monad.Fix (fix) fix (putStrLn "SPAM" >>) </lang>
Haxe
<lang haxe>while (true)
Sys.println("SPAM");</lang>
hexiscript
<lang hexiscript>while true; println "SPAM"; endwhile</lang>
HicEst
<lang hicest>DO i = 1, 1E20 ! for i with 16 or more digits: i == i + 1 == loop infinite
WRITE() "SPAM"
ENDDO</lang>
HolyC
<lang holyc>while(1) Print("SPAM\n");</lang>
Icon and Unicon
There are several ways to write infinite loops in Icon. The most straightforward would be with repeat. <lang icon>procedure main()
repeat write("SPAM")
end</lang>
Alternately one could use one of these: <lang icon>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</lang>
IDL
<lang IDL>while 1 do print,'SPAM'</lang>
Intercal
Assuming Turing Text I/O with 8-bit ASCII-compatible character set, using COME FROM:
<lang intercal> NOTE THIS IS INTERCAL
PLEASE ,1 <- #5 DO ,1 SUB #1 <- #54 DO ,1 SUB #2 <- #192 DO ,1 SUB #3 <- #136 PLEASE ,1 SUB #4 <- #208 DO ,1 SUB #5 <- #98 DO COME FROM (1) DO READ OUT ,1
(2) DO ,1 SUB #1 <- #134 (1) PLEASE ABSTAIN FROM (2)</lang>
Io
<lang io>loop("SPAM" println)</lang>
J
<lang j>(-[smoutput bind 'SPAM')^:_(1)</lang>
Alternatively,
<lang j>smoutput bind 'SPAM'^:1e99 </lang>
This second implementation relies on numeric inaccuracies in IEEE floating point notation. For example, 1+1e98 is exactly equal to 1e98. That said, 1e98 iterations would still be significantly longer than the practical life of any machine anyone would care to dedicate to this task.
Java
<lang java>while (true) {
System.out.println("SPAM");
}</lang>
<lang java>for (;;) {
System.out.println("SPAM");
}</lang>
JavaScript
<lang javascript>for (;;) console.log("SPAM");</lang> <lang javascript>while (true) console.log("SPAM");</lang>
Joy
<lang joy>DEFINE loop == [true []] dip while.
["SPAM\n" putchars] loop.</lang>
jq
<lang jq>recurse("SPAM")</lang>
- Output:
"SPAM" "SPAM" ...
To suppress the quotation marks, invoke jq with the -r option.
Jsish
<lang javascript>for (;;) puts('SPAM');</lang>
Julia
<lang Julia> while true
println("SPAM")
end </lang>
- Output:
SPAM SPAM SPAM SPAM SPAM SPAM SPAM
and so on until ^C
K
<lang K> while[1; `0:"SPAM\n"]</lang>
Kotlin
<lang scala>// version 1.0.6
fun main(args: Array<String>) {
while (true) println("SPAM")
}</lang>
LabVIEW
This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.
Lambdatalk
<lang scheme> {def loops_infinite
{lambda {} {if true then SPAM{br} {loops_infinite} else never}}}
-> loops_infinite
{loops_infinite} -> SPAM forever... </lang>
Lang5
<lang lang5>do "SPAM\n" . loop</lang>
Lasso
<lang Lasso>// not wise to run this! while(1 > 0) => {^ 'SPAM\r' ^}</lang>
Liberty BASIC
<CTRL><Break> is used to terminate such loops. <lang lb> while 1
print "SPAM"
wend end </lang>
Lily
<lang lily> while 1: print("SPAM") </lang>
Lingo
<lang lingo>repeat while TRUE
put "SPAM"
end repeat</lang>
Lisaac
{ "SPAM\n".print; }.endless_loop;
LiveCode
<lang LiveCode>repeat forever
put "SPAM" & return
end repeat</lang>
Logo
<lang logo>forever [print "SPAM]</lang>
LOLCODE
<lang lolcode>HAI
CAN HAS STDIO? IM IN YR LOOP VISIBLE "SPAM" IM OUTTA YR LOOP
KTHXBYE</lang>
Lua
<lang lua> while true do
print("SPAM")
end
--Another solution repeat
print("SPAM")
until false </lang>
M2000 Interpreter
All loops can stop using Esc or Ctrl+C or Break (the last two open dialog box to stop or continue). Using Escape Off we make Esc not work for breaking execution. If Esc works then Ctrl + Y (and other letters except C, A, Z, X, N, M. F, L), open Control form, which we can do: Next Step, Slow Flow, Stop, and we can show code,current stack, variables, or execute immediate statements. This works only in console, not in M2000 forms.
<lang M2000 Interpreter> Module CheckIt {
Print "SPAM" loop
} Checkit </lang> Using a Repeat (or Do) - Always block <lang M2000 Interpreter> Module CheckIt {
Repeat { Print "SPAM" } Always
} Checkit </lang>
Printing text rendering using Report. <lang M2000 Interpreter> Module CheckIt {
\\ stop in every 2/3 of cosole lines \\ press spacebar or mouse button to continue Report Format$("Spam\n") Loop
} Checkit \\ using multiline string, replace report from module above Report {SPAM
}
</lang>
M4
<lang M4>define(`spam',`SPAM spam') spam</lang>
Make
<lang make>spam:
@echo SPAM $(MAKE)</lang>
Malbolge
<lang malbolge> bP&A@?>=<;:9876543210/.-,+*)('&%$T"!~}|;]yxwvutslUSRQ.yx+i)J9edFb4`_^]\yxwRQ)(TSRQ]m!G0KJIyxFvDa%_@?"=<5:98765.-2+*/.-,+*)('&%$#"!~}|utyrqvutsrqjonmPkjihgfedc\DDYAA\>>Y;;V886L5322G//D,,G))>&&A##!7~5:{y7xvuu,10/.-,+*)('&%$#"yb}|{zyxwvutmVqSohmOOjihafeHcEa`YAA\[ZYRW:U7SLKP3NMLK-I,GFED&%%@?>=6;|9y70/4u210/o-n+k)"!gg$#"!x}`{zyxZvYtsrqSoRmlkjLhKfedcEaD_^]\>Z=XWVU7S6QPON0LKDI,GFEDCBA#?"=};438y6543s1r/o-&%*k('&%e#d!~}|^z]xwvuWsVqponPlOjihgIeHcba`B^A\[ZY;W:UTSR4PI2MLKJ,,AFE(&B;:?"~<}{zz165v3s+*/pn,mk)jh&ge#db~a_{^\xwvoXsrqpRnmfkjMKg`_GG\aDB^A?[><X;9U86R53ONM0KJC,+FEDC&A@?!!6||3876w4-tr*/.-&+*)('&%$e"!~}|utyxwvutWlkponmlOjchg`edGba`_XW\?ZYRQVOT7RQPINML/JIHAFEDC&A@?>!<;{98yw5.-ss*/pn,+lj(!~ff{"ca}`^z][wZXtWUqTRnQOkNLhgfIdcFaZ_^A\[Z<XW:U8SRQPOHML/JIHG*ED=%%:?>=~;:{876w43210/(-,+*)('h%$d"ca}|_z\rqYYnsVTpoRPledLLafIGcbE`BXW??TY<:V97S64P31M0.J-+G*(DCB%@?"=<;|98765.3210p.-n+$)i'h%${"!~}|{zyxwvuXVlkpSQmlOjLbafIGcbE`BXW??TY<:V97S64P31M0.J-+G*(D'%A@?"=<}:98y6543,1r/.o,+*)j'&%eez!~a|^tsx[YutWUqjinQOkjMhJ`_dGEaDB^A?[><X;9U86R53O20LKJ-HG*ED'BA@?>7~;:{y7x5.3210q.-n+*)jh&%$#"c~}`{z]rwvutWrkpohmPkjihafI^cba`_^A\[>YXW:UTS5QP3NM0KJ-HGF?D'BA:?>=~;:z8765v32s0/.-nl$#(ig%fd"ca}|_]yrqvYWsVTpSQmPNjMKgJHdGEa`_B]\?ZY<WVUTMR5PO20LK.IHA))>CB%#?87}}49zx6wu3tr0qo-nl*ki'hf$ec!~}`{^yxwvotsrUponQlkMihKIe^]EEZ_B@\?=Y<:V97S64P31M0.J-+GFE(C&A@?8=<;:{876w43s10qo-&%kk"'hf$ec!b`|_]y\ZvYWsVTpSQmlkNiLgf_dcba`C^]\?ZY;WV97SLK33HM0.J-+G*(D'%A$">!};|z8yw543t1r/(-,+*)(i&%fd"!~}|_t]xwvutslqTonmPkjLhKIeHFbEC_^A?[TSX;9UT7R4JIN1/K.,H+)E(&B%#?"~<}{987x/4ussr).o,+l)(h&ge#db~a_{^\x[YutWrTjinQOkNLhgJeG]\aDB^]@[=SRW:877LQP3N0FEJ-+**?DC&A#98=~|:98yx/4u21rp(',mk)(ig%|{"ca}`^z][wZXtWUqTRnQOkNLhKIedcFE`YB@@?ZYRW:UTS6QPO11F..CHGF)(CB;@#>!~;XzV7gwu-QrrqMoJIkZF'WC$#AbQ`_{^L9wI64"VDConzl+j);JJ%qGFEZ~}]{ygwRuc8aSq44"H1Y.iV,e*RQ </lang>
Maple
<lang Maple> > do print(SPAM) end; </lang>
Mathematica / Wolfram Language
<lang mathematica>While[True,
Print@"SPAM"; ]</lang>
MATLAB / Octave
<lang Matlab>while true
fprintf('SPAM\n')
end</lang>
Maxima
<lang maxima>do(disp("SPAM"));</lang>
MAXScript
<lang maxscript>while true do print "SPAM\n"</lang>
MelonBasic
Using Goto:1
:
<lang MelonBasic>Say:"SPAM"
Goto:1</lang>
Using Goto:start
:
<lang MelonBasic>Say:"SPAM"
Goto:start</lang>
Metafont
<lang metafont>forever: message "SPAM"; endfor end</lang>
Microsoft Small Basic
With While
.
<lang microsoftsmallbasic>
While "True"
TextWindow.WriteLine("SPAM")
EndWhile
</lang>
With Goto
.
<lang microsoftsmallbasic>
loopStart:
TextWindow.WriteLine("SPAM")
Goto loopStart
</lang>
min
<lang min>(true) ("SPAM" puts!) while</lang>
MIPS Assembly
Thanks to Chibialiens.com for the header/footer, bitmap font, and print routines. <lang mips>.include "\SrcAll\Header.asm" .include "\SrcAll\BasicMacros.asm" .include "\SrcPSX\MemoryMap.asm" .include "\SrcN64\MemoryMap.asm"
CursorX equ 0x100 CursorY equ 0x101
main: la a0,MyString jal PrintString nop jal NewLine nop j main nop
MyString: .byte "SPAM",255,0,0,0 ;the 3 zeroes are padding to ensure proper alignment.
MyFont: .ifdef buildn64 .incbin "\ResN64\ChibiAkumas.fnt" .endif .ifdef buildPSX .incbin "\ResPSX\ChibiAkumas.fnt" .endif
.include "\SrcALL\graphics.asm"
.include "..\\SrcAll\monitor.asm" .include "\SrcN64\Footer.asm"</lang>
- Output:
Screenshot of Nintendo 64 emulator
МК-61/52
<lang>1 2 3 4 С/П БП 00</lang>
Note: because this device has no text output instead of "SPAM" was used the number (1234).
Modula-2
<lang modula2>LOOP
InOut.WriteString ("SPAM"); InOut.WriteLn
END;</lang>
Modula-3
<lang modula3>LOOP
IO.Put("SPAM\n");
END;</lang>
Monte
<lang Monte> while (true):
traceln("SPAM")
</lang>
MontiLang
<lang MontiLang>WHILE TRUE
|SPAM| PRINT .
ENDWHILE</lang>
Note that TRUE
is simply a variable equal to 1. WHILE 1
, any number larger than 0 or any string with a length more than 0 would also work
MOO
<lang moo>while (1)
player:tell("SPAM");
endwhile</lang>
MUMPS
<lang MUMPS>
FOR WRITE "SPAM",!
</lang>
Nanoquery
<lang Nanoquery>while true
println "SPAM"
end</lang>
Nemerle
<lang Nemerle>while (true) WriteLine("SPAM");</lang> Or, using recursion: <lang Nemerle>def loop() : void {
WriteLine("SPAM"); loop();
}</lang>
NetRexx
<lang NetRexx>/* NetRexx */ options replace format comments java crossref savelog symbols nobinary
say say 'Loops/Infinite'
loop label spam forever say 'SPAM' end spam
</lang>
NewLISP
<lang NewLISP>(while (println "SPAM"))</lang>
Nim
<lang nim>while true:
echo "SPAM"</lang>
NS-HUBASIC
Using FOR
:
<lang NS-HUBASIC>10 FOR I=0 TO 1 STEP 0
20 PRINT "SPAM"
30 NEXT</lang>
Using GOTO
:
<lang NS-HUBASIC>10 PRINT "SPAM"
20 GOTO 10</lang>
Using RUN
:
<lang NS-HUBASIC>10 PRINT "SPAM"
20 RUN</lang>
Oberon-2
<lang oberon2> MODULE InfiniteLoop; IMPORT
Out;
BEGIN
LOOP Out.String("SPAM");Out.Ln END
END InfiniteLoop. </lang>
Objeck
<lang objeck> while(true) {
"SPAM"->PrintLine();
}; </lang>
OCaml
<lang ocaml>while true do
print_endline "SPAM"
done</lang>
or
<lang ocaml>let rec inf_loop() =
print_endline "SPAM"; inf_loop()
in inf_loop()</lang>
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.
Occam
<lang occam>#USE "course.lib" PROC main (CHAN BYTE screen!)
WHILE TRUE out.string("SPAM*c*n", 0, screen)
- </lang>
Octave
<lang octave>while(1)
disp("SPAM")
endwhile</lang>
Oforth
<lang Oforth>begin "SPAM" . again</lang>
Ol
<lang scheme> (let loop ()
(display "SPAM") (loop))
</lang>
OPL
<lang opl>PROC main:
LOCAL loop% loop%=1 while loop%=1 PRINT "SPAM" ENDWH
ENDP</lang>
Oz
<lang oz>for do
{Show 'SPAM'}
end</lang>
PARI/GP
<lang parigp>while(1,
print("SPAM")
);</lang>
For a shorter version, note that print
returns gnil
which is evaluated as false
.
A 'cheating' solution might use print(SPAM)
on the hope that the variable SPAM is uninitialized and hence prints as the monomial in itself.
But with the '
operator that evaluation can be forced, regardless of the current value (if any) of that variable:
<lang parigp>until(print('SPAM),)</lang>
Pascal
<lang pascal>while true do
writeln('SPAM');</lang>
Alternatively: <lang pascal>repeat
writeln('SPAM')
until false;</lang>
Perl
<lang perl>while(1){
print "SPAM\n";
}</lang>
or equivalently
<lang perl>print "SPAM\n" while 1;</lang>
Phix
while true do puts(1,"SPAM\n") end while
PHP
<lang php>while(1)
echo "SPAM\n";</lang>
PicoLisp
<lang PicoLisp>(loop (prinl "SPAM"))</lang>
Pike
<lang pike> while(1)
write("SPAM\n");
</lang>
PILOT
<lang pilot>*TypeSpam type:SPAM jump:*TypeSpam</lang>
Pixilang
<lang Pixilang>start: fputs("SPAM\n") go start</lang>
PL/I
<lang PL/I> do forever;
put list ('SPAM'); put skip;
end;</lang>
Plain English
When Repeat.
appears by itself, execution proceeds from the beginning of the routine. Normally you would include a conditional statement to break or exit when a condition is met, but not in this case.
<lang plainenglish>To run:
Start up.
Write SPAM forever.
Shut down.
To write SPAM forever: Write "SPAM" to the console. Repeat.</lang>
Plain TeX
Compile in console mode, with, e.g. "pdftex <file name>". <lang tex>\newlinechar`\^^J \def\spam{\message{SPAM^^J}\spam}% \spam</lang>
Pop11
<lang pop11>while true do
printf('SPAM', '%p\n');
endwhile;</lang>
PostScript
simple infinite loop: <lang postscript>{}loop</lang>
A bit more complex infinite loop: <lang postscript>/go {
/spam { (SPAM\n) print flush } bind def % bind and define spam
{ spam } % procedure that will be executed by loop and will call spam to print loop % the loop
}
%start spamming! go</lang>
PowerShell
<lang powershell>for () {
"SPAM"
}</lang>
Prolog
<lang prolog>repeat, write('SPAM'), nl, fail.</lang>
Pure Data
Screenshot: https://i.imgur.com/IrwaafZ.png
<lang Pure Data>#N canvas 426 88 450 300 10;
- X obj 17 75 print;
- X msg 17 55 SPAM;
- X obj 17 35 metro 1;
- X msg 17 15 1;
- X connect 1 0 0 0;
- X connect 2 0 1 0;
- X connect 3 0 2 0;</lang>
Notes: the loop is started by clicking the |1(, a [loadbang] could additionally be used. An [until] object, sent a bang, will loop forever, but will hang Pure Data, whereas a high-speed metro will function perfectly.
PureBasic
Repeat/Forever
<lang PureBasic>Repeat
PrintN("SPAM")
ForEver</lang>
Goto
<lang PureBasic>PrintIt: PrintN("SPAM") Goto PrintIt</lang>
Python
In Python 2: <lang python>while 1:
print "SPAM"</lang>
In python 3: <lang python>while 1:
print("SPAM")</lang>
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.
Quackery
<lang Quackery>[ say "SPAM" cr again ]</lang>
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.
<lang R>repeat print("SPAM")</lang>
Racket
<lang racket>
- lang racket
- Using recursion
(define (loop)
(displayln "SPAM") (loop))
(loop)
- Using a for loop
(for ([i (in-naturals)])
(displayln "SPAM"))
</lang>
Raku
(formerly Perl 6)
<lang perl6>loop {
say 'SPAM';
}</lang> In addition, there are various ways of writing lazy, infinite lists in Raku: <lang perl6>print "SPAM\n" xx *; # repetition operator print "SPAM\n", ~* ... *; # sequence operator map {say "SPAM"}, ^Inf; # upto operator</lang>
Rapira
<lang Rapira>while 1 do
output: "SPAM"
od</lang>
REBOL
<lang REBOL>forever [print "SPAM"]</lang>
Red
<lang Red>forever [ print "SPAM" ]</lang>
ReScript
<lang ReScript>while true {
Js.log("SPAM")
}</lang>
or
<lang ReScript>let rec inf_loop = () => {
Js.log("SPAM") inf_loop()
}</lang>
Retro
<lang Retro>[ "SPAM\n" puts -1 ] while</lang>
REXX
simple
<lang rexx>/*REXX program displays the word SPAM forever. */
do forever say 'SPAM' end /*DO forever*/ /*control will never reach here. */ /*don't stick a fork in it. */</lang>
esoteric
<lang rexx>/*REXX program displays the word SPAM forever. */
do while 1==1 /*esoteric "forever" clause. */ say 'SPAM' end /*DO while 1==1*/ /*control will never reach here. */ /*don't stick a fork in it. */</lang>
GO TO version
<lang rexx>/*REXX program displays the word SPAM forever. */
tell_it: say 'SPAM' signal tell_it /*REXX's version of a GO TO */
/*control will never reach here. */ /*don't stick a fork in it. */</lang>
too clever by half
<lang rexx>/*REXX program displays the word SPAM forever. */
do until 0>1 /*too-clever-by-half forever loop*/ say 'SPAM' end /*DO until 0>1*/ /*control will never reach here. */ /*don't stick a fork in it. */</lang>
Ring
<lang ring> while true
see "Spam"
end </lang>
Robotic
This will display the word SPAM at the bottom of the screen indefinitely: <lang robotic>
- "infinite_loop"
- "SPAM"
goto "infinite_loop" </lang>
Ruby
<lang ruby>loop {puts "SPAM"} </lang>
Run BASIC
<lang runbasic>[loop] print "Spam" :goto [loop]
while 1 print "Spam" wend</lang>
Rust
<lang rust>fn main() {
loop { println!("SPAM"); }
}</lang>
S-lang
<lang S-lang>forever print("SPAM");</lang>
Salmon
<lang Salmon>while (true)
"SPAM"!;</lang>
Sather
<lang sather>class MAIN is
main is loop #OUT + "Spam\n"; end; end;
end;</lang>
Scala
<lang scala>while (true)
println("SPAM")</lang>
Scheme
<lang scheme>((lambda (x) (display "SPAM") (newline) (x x))
(lambda (x) (display "SPAM") (newline) (x x)))
</lang>
or, less Schemishly but with less redundancy:
<lang scheme>(do () (#f) (display "SPAM") (newline))</lang>
Scilab
<lang>while %T
printf("SPAM\n")
end</lang>
- Output:
SPAM SPAM SPAM SPAM ...
sed
<lang sed>:loop s/.*/SPAM/ p t loop</lang> Sed requires at least one line of input to execute, so run as follows:
echo | sed ':loop;s/.*/SPAM/;p;t loop'
Seed7
<lang seed7>$ include "seed7_05.s7i";
const proc: main is func
begin while TRUE do writeln("SPAM"); end while; end func;</lang>
Self
<lang self>['SPAM' printLine] loop</lang>
Sidef
<lang ruby>loop { say "SPAM!" };</lang>
Slate
<lang slate>[inform: 'SPAM'] loop</lang>
Smalltalk
<lang smalltalk>[
Transcript showCR:'boring stuff'.
] loop
[true] whileTrue:[
Transcript showCR:'also borinh'.
]
[
Transcript showCR:'poor cpu'.
] doUntil:[false]
[
Transcript showCR:'please press CTRL-c!'.
] doWhile:[true]</lang>
SNOBOL4
<lang snobol>loop output = "SPAM" :(loop) end</lang>
SNUSP
<lang snusp>@\>@\>@\>@\>++++++++++===!/ < < < < \
| | | \M=@@@@+@+++++# \.>.>.>.>./ | | \A=@@+@@@@+++# | \P=@@+@@+@@+++# \S=@@+@+@@@+++#</lang>
Sparkling
<lang sparkling>while true {
print("SPAM");
}</lang>
or
<lang sparkling>do {
print("SPAM");
} while true;</lang>
or
<lang sparkling>for var b = true; b; b = true {
printf("SPAM\n");
}</lang>
etc.
Spin
<lang spin>con
_clkmode = xtal1 + pll16x _clkfreq = 80_000_000
obj
ser : "FullDuplexSerial.spin"
pub main
ser.start(31, 30, 0, 115200)
repeat ser.str(string("SPAM",13,10))
waitcnt(_clkfreq + cnt) ser.stop cogstop(0)</lang>
SPL
<lang spl>>
#.output("SPAM")
<</lang>
SQL PL
version 9.7 or higher.
With SQL PL: <lang sql pl> --#SET TERMINATOR @
SET SERVEROUTPUT ON@
BEGIN
DECLARE I SMALLINT DEFAULT 1; WHILE (I = I) DO CALL DBMS_OUTPUT.PUT_LINE('SPAM'); END WHILE;
END @ </lang> Output:
db2 -td@ db2 => SET SERVEROUTPUT ON@ db2 => BEGIN ... db2 (cont.) => END @ DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL20511N There is not enough available space in the "DBMS_OUTPUT" message buffer. SQLSTATE=54035 SPAM SPAM SPAM SPAM ...
Standard ML
<lang sml>while true do
print "SPAM\n";</lang>
or
<lang sml>let
fun inf_loop () = ( print "SPAM\n"; inf_loop () )
in
inf_loop ()
end</lang>
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.
Stata
<lang stata>while 1 {
display "SPAM"
}</lang>
Mata
<lang stata>while (1) printf("SPAM\n")</lang>
Also possible with a for loop, but unlike C, the middle expression is not optional:
<lang stata>for (;1;) printf("SPAM\n")</lang>
Swift
<lang swift>while true {
println("SPAM")
}</lang>
SystemVerilog
<lang SystemVerilog>program main;
initial forever $display("SPAM");
endprogram </lang>
Tailspin
<lang tailspin> 1 -> \(
<> 'SPAM$#10;' -> !OUT::write 1 -> #
\) -> !VOID </lang>
Tcl
<lang tcl>while true {
puts SPAM
}
- or
for {} 1 {} {
puts SPAM
}</lang>
TI-83 BASIC
There are a few ways to achieve this in TI-83 BASIC
<lang ti83b>
:Lbl 1 :Disp "SPAM :Goto 1
</lang>
Another way is by using a While loop
<lang ti83b>
:While 1 :Disp "SPAM :End
</lang>
TI-89 BASIC
<lang ti89b>Loop
Disp "SPAM"
EndLoop</lang>
TorqueScript
<lang Torque>While(1)
echo("SPAM");</lang>
Transact-SQL
<lang sql>WHILE 1=1 BEGIN
PRINT "SPAM"
END</lang>
Trith
<lang trith>["SPAM" print] loop</lang>
TUSCRIPT
TUSCRIPT has no infinite loop. 999999999 loops are the limit. <lang tuscript> $$ MODE TUSCRIPT LOOP/999999999 print "spam" ENDLOOP </lang>
UNIX Shell
Use any of these loops: <lang bash>while :; do echo SPAM; done</lang>
<lang bash>while true; do echo "SPAM"; done</lang>
<lang bash>until false; do echo "SPAM"; done</lang>
<lang bash>for ((;;)); do echo "SPAM"; done</lang>
C Shell
<lang bash>while (1) echo SPAM end</lang>
es
<lang es>forever {echo SPAM}</lang>
UnixPipes
<lang bash>yes SPAM</lang>
Unlambda
<lang unlambda> ``ci``s``s`kr``s``s``s``s`k.S`k.P`k.A`k.Mii</lang>
Ursa
<lang ursa>while true out "SPAM" endl console end while</lang>
V
<lang v>true [
'SPAM' puts
] while</lang>
Vala
<lang vala>for(;;) stdout.printf("SPAM\n");</lang> <lang vala>while(true) stdout.printf("SPAM\n");</lang> <lang vala>do stdout.printf("SPAM\n"); while(true);</lang>
VAX Assembly
<lang VAX Assembly> 0000 0000 1 .entry main,0
4D415053 8F DD 0002 2 pushl #^a"SPAM" ;string on stack 5E DD 0008 3 pushl sp ;reference to string 04 DD 000A 4 pushl #4 ;+length = descriptor 000C 5 loop: 5E DD 000C 6 pushl sp ;descriptor by reference 00000000'GF 01 FB 000E 7 calls #1, g^lib$put_output ;show message F5 11 0015 8 brb loop ;forever 0017 9 0017 10 .end main</lang>
VBA
<lang vb>Do
Debug.Print "SPAM"
Loop</lang>
VBScript
<lang vb>Do
WScript.Echo("SPAM")
Loop</lang>
Vedit macro language
<lang vedit>while (1) {
Message("Spam\n")
}</lang> or: <lang vedit>do {
Message("Spam\n")
} while (1)</lang> or: <lang vedit>for (;1;) {
Message("Spam\n")
}</lang> "Nearly infinite" loop can be done by using constant ALL (=1073741824) as repeat count: <lang vedit>Repeat (ALL) {
Message("Spam\n")
}</lang>
Visual Basic
<lang vb>Do
Debug.Print("SPAM")
Loop</lang>
Visual Basic .NET
Platform: .NET
<lang vbnet>Do
Console.WriteLine("SPAM")
Loop</lang>
Vlang
<lang vlang>fn main() { for { print("SPAM\n") } }</lang>
Wart
<lang wart>repeat :forever
prn "spam"</lang>
Wee Basic
<lang Wee Basic>let loop=1 while loop=1 print 1 "SPAM" wend end</lang>
Whenever
<lang whenever> 1 print("SPAM"); 1; </lang>
Wren
<lang ecmascript>while (true) System.print("SPAM")</lang>
X86 Assembly
<lang asm> section .text global _start
_start: mov edx, len mov ecx, msg mov ebx, 1 mov eax, 4 int 0x80 jmp _start
section .data msg db "SPAM",0xa len equ $-msg </lang>
XLISP
It is of course possible to use a WHILE
loop with a condition that will always evaluate to true:
<lang lisp>(defun keep-printing-spam ()
(while t (display "SPAM") (newline) ) )</lang>
Although this idiom is very common (in many programming languages), however, it feels a bit like a misuse of a looping construct that is meant to be conditional. If an unconditional jump is really what we want, then that is what we have goto for; or rather, in XLISP we do not have goto (in so many words) but we can achieve the effect of it using tail recursion. <lang lisp>(defun keep-printing-spam ()
(display "SPAM") (newline) (keep-printing-spam) )</lang>
XPL0
<lang XPL0>code Text=12; loop Text(0, "SPAM ")</lang>
Z80 Assembly
Using the Amstrad CPC firmware:
<lang z80>org $4000
txt_output: equ $bb5a
start: ld hl,spam
print: ld a,(hl) cp 0 jr z,start call txt_output inc hl jr print
spam: defm "SPAM\r\n\0"</lang>
Zig
<lang Zig> const std = @import("std"); pub fn main() !void {
const stdout_wr = std.io.getStdOut().writer(); while (true) try stdout_wr.writeAll("SPAM\n");
} </lang>
zkl
<lang zkl>while(1) { println("SPAM") } while(True){ println("SPAM") } foreach _ in ([0..]){ println("SPAM") } [0..].pump(Console.println,T(Void,"SPAM")); [0..].pump(fcn{ println("SPAM") }); fcn{ println("SPAM"); return(self.fcn()) }(); // tail recursive lambda</lang>
Zoomscript
For typing: <lang Zoomscript>var loop loop = 1 while ne loop 0 print "SPAM" println endwhile</lang> For importing:
¶0¶var loop¶0¶loop = 1¶0¶while ne loop 0¶1¶print "SPAM"¶1¶println¶0¶endwhile
- Programming Tasks
- Iteration
- Simple
- 11l
- 360 Assembly
- 4DOS Batch
- 6502 Assembly
- 6800 Assembly
- 68000 Assembly
- 8086 Assembly
- 8th
- AArch64 Assembly
- ACL2
- Action!
- ActionScript
- Ada
- Agena
- Aime
- ALGOL 60
- ALGOL 68
- ALGOL W
- AmigaE
- AppleScript
- ARM Assembly
- ArnoldC
- Arturo
- AutoHotkey
- AWK
- Axe
- BASIC
- Applesoft BASIC
- BASIC256
- Commodore BASIC
- IS-BASIC
- QB64
- True BASIC
- Yabasic
- Batch File
- BBC BASIC
- Bc
- BCPL
- Beeswax
- Befunge
- Blz
- BootBASIC
- BQN
- Bracmat
- Brainf***
- Brat
- C
- C sharp
- C++
- Chapel
- ChucK
- Clojure
- COBOL
- CoffeeScript
- ColdFusion
- Comal
- Common Lisp
- Corescript
- Cowgol
- Crystal
- D
- Dart
- Dc
- DCL
- Delphi
- Draco
- DWScript
- Dyalect
- Déjà Vu
- E
- EDSAC order code
- Ela
- Elena
- Elixir
- Emacs Lisp
- Erlang
- ERRE
- Euphoria
- F Sharp
- Factor
- FALSE
- Fantom
- Fermat
- Fish
- Forth
- Fortran
- Fortress
- FreeBASIC
- Frink
- FutureBasic
- Gambas
- GAP
- GB BASIC
- GlovePIE
- GML
- Go
- Groovy
- Halon
- Hare
- Haskell
- Haxe
- Hexiscript
- HicEst
- HolyC
- Icon
- Unicon
- IDL
- Intercal
- Io
- J
- Java
- JavaScript
- Joy
- Jq
- Jsish
- Julia
- K
- Kotlin
- LabVIEW
- Lambdatalk
- Lang5
- Lasso
- Liberty BASIC
- Lily
- Lingo
- Lisaac
- LiveCode
- Logo
- LOLCODE
- Lua
- M2000 Interpreter
- M4
- Make
- Malbolge
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Maxima
- MAXScript
- MelonBasic
- Metafont
- Microsoft Small Basic
- Min
- MIPS Assembly
- МК-61/52
- Modula-2
- Modula-3
- Monte
- MontiLang
- MOO
- MUMPS
- Nanoquery
- Nemerle
- NetRexx
- NewLISP
- Nim
- NS-HUBASIC
- Oberon-2
- Objeck
- OCaml
- Occam
- Oforth
- Ol
- OPL
- Oz
- PARI/GP
- Pascal
- Perl
- Phix
- PHP
- PicoLisp
- Pike
- PILOT
- Pixilang
- PL/I
- Plain English
- PlainTeX
- Pop11
- PostScript
- PowerShell
- Prolog
- Pure Data
- PureBasic
- Python
- Quackery
- R
- Racket
- Raku
- Rapira
- REBOL
- Red
- ReScript
- Retro
- REXX
- Ring
- Robotic
- Ruby
- Run BASIC
- Rust
- S-lang
- Salmon
- Sather
- Scala
- Scheme
- Scilab
- Sed
- Seed7
- Self
- Sidef
- Slate
- Smalltalk
- SNOBOL4
- SNUSP
- Sparkling
- Spin
- SPL
- SQL PL
- Standard ML
- Stata
- Swift
- SystemVerilog
- Tailspin
- Tcl
- TI-83 BASIC
- TI-89 BASIC
- TorqueScript
- Transact-SQL
- Trith
- TUSCRIPT
- UNIX Shell
- C Shell
- Es
- UnixPipes
- Unlambda
- Ursa
- V
- Vala
- VAX Assembly
- VBA
- VBScript
- Vedit macro language
- Visual Basic
- Visual Basic .NET
- Vlang
- Wart
- Wee Basic
- Whenever
- Wren
- X86 Assembly
- XLISP
- XPL0
- Z80 Assembly
- Zig
- Zkl
- GUISS/Omit
- Zoomscript