Loops/Infinite: Difference between revisions
m (→{{header|R}}: changed 'while' to 'repeat') |
m (Fixed lang tags.) |
||
Line 2: | Line 2: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
<lang actionscript> |
<lang actionscript>while (true) { |
||
⚫ | |||
trace("SPAM"); |
trace("SPAM"); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 13: | Line 11: | ||
end loop;</lang> |
end loop;</lang> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
<lang algol68>DO |
|||
DO |
|||
printf($"SPAM"l$) |
|||
⚫ | |||
OD |
|||
Or the classic "dynamic halt": |
Or the classic "dynamic halt": |
||
<lang algol68>loop x: |
|||
printf($"SPAM"l$); |
|||
loop x</lang> |
|||
=={{header|AmigaE}}== |
=={{header|AmigaE}}== |
||
Line 63: | Line 61: | ||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
Because the 2-D code space is toroidal, all loops are infinite unless explicitly stopped with '''@'''. |
Because the 2-D code space is toroidal, all loops are infinite unless explicitly stopped with '''@'''. |
||
<lang befunge>55+"MAPS",,,,,</lang> |
|||
=={{header|Brainf***}}== |
=={{header|Brainf***}}== |
||
<lang bf>++++++++++[->++++++>++++++++>+<<<]>+++++> |
|||
[+++.---.<.>---.+++>.<]</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 78: | Line 76: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<lang cpp> |
<lang cpp>while (true) |
||
⚫ | |||
while (true) |
|||
⚫ | |||
⚫ | |||
or |
or |
||
<lang cpp> |
<lang cpp>for (;;) |
||
⚫ | |||
for (;;) |
|||
⚫ | |||
⚫ | |||
or |
or |
||
<lang cpp> |
<lang cpp>do |
||
do |
|||
std::cout << "SPAM\n"; |
std::cout << "SPAM\n"; |
||
while (true); |
while (true);</lang> |
||
⚫ | |||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 105: | Line 97: | ||
With tags: |
With tags: |
||
<lang cfm><cfloop condition = "true NEQ false"> |
|||
SPAM |
|||
</cfloop></lang> |
|||
With script: |
With script: |
||
<lang cfm><cfscript> |
|||
while( true != false ) |
|||
{ |
|||
writeOutput( "SPAM" ); |
|||
} |
|||
</cfscript></lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<lang |
<lang lisp>(loop [] (println "SPAM") (recur))</lang> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 142: | Line 134: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<lang erlang>-module (main). |
|||
-export ([main/1]). |
|||
main(Any) -> |
|||
io:fwrite("SPAM~n",[]), |
|||
main(Any)</lang> |
|||
=={{header|FALSE}}== |
=={{header|FALSE}}== |
||
<lang false>[1]["SPAM |
|||
"]#</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 214: | Line 206: | ||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
<lang joy> |
<lang joy>DEFINE loop == [1] swap while. |
||
DEFINE loop == [1] swap while. |
|||
["SPAM\n" putchars] loop. |
["SPAM\n" putchars] loop.</lang> |
||
⚫ | |||
=={{header|Lisaac}}== |
=={{header|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. |
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. |
||
<lang Lisaac> |
<lang Lisaac>- i : INTEGER; |
||
- i : INTEGER; |
|||
i := 1; |
i := 1; |
||
{ i = 1 }.while_do { |
{ i = 1 }.while_do { |
||
"SPAM\n".print; |
"SPAM\n".print; |
||
}; |
};</lang> |
||
</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 234: | Line 222: | ||
=={{header|M4}}== |
=={{header|M4}}== |
||
<lang M4> |
<lang M4>define(`spam',`SPAM |
||
define(`spam',`SPAM |
|||
spam') |
spam') |
||
spam |
spam</lang> |
||
</lang> |
|||
=={{header|Make}}== |
=={{header|Make}}== |
||
Line 305: | Line 291: | ||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
⚫ | |||
<pre> |
|||
while true do |
|||
printf('SPAM', '%p\n'); |
printf('SPAM', '%p\n'); |
||
endwhile; |
endwhile;</lang> |
||
</pre> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 317: | Line 301: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
<lang prolog>repeat, write('SPAM'), nl, fail.</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 328: | Line 312: | ||
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. |
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> |
<lang R>repeat print("SPAM")</lang> |
||
repeat print("SPAM") |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 349: | Line 331: | ||
=={{header|SNUSP}}== |
=={{header|SNUSP}}== |
||
<lang snusp>@\>@\>@\>@\>++++++++++===!/ < < < < \ |
|||
| | | \M=@@@@+@+++++# \.>.>.>.>./ |
|||
| | \A=@@+@@@@+++# |
|||
| \P=@@+@@+@@+++# |
|||
\S=@@+@+@@@+++#</lang> |
|||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<lang |
<lang sml>while true do |
||
print "SPAM\n";</lang> |
print "SPAM\n";</lang> |
||
or |
or |
||
<lang |
<lang sml>let |
||
fun inf_loop () = ( |
fun inf_loop () = ( |
||
print "SPAM\n"; |
print "SPAM\n"; |
||
Line 374: | Line 356: | ||
=={{header|Transact-SQL}}== |
=={{header|Transact-SQL}}== |
||
<lang |
<lang sql>WHILE 1=1 BEGIN |
||
PRINT "SPAM" |
PRINT "SPAM" |
||
END</lang> |
END</lang> |
||
Line 389: | Line 371: | ||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |
||
<lang ti89b>Loop |
|||
<pre style="font-family:'TI Uni'">Loop |
|||
Disp "SPAM" |
Disp "SPAM" |
||
EndLoop</ |
EndLoop</lang> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 411: | Line 393: | ||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
||
<lang |
<lang bash> yes SPAM</lang> |
||
=={{header|Unlambda}}== |
=={{header|Unlambda}}== |
||
Line 422: | Line 404: | ||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
<lang vedit> |
<lang vedit>while (1) { |
||
while (1) { |
|||
Message("Spam\n") |
Message("Spam\n") |
||
⚫ | |||
} |
|||
</lang> |
|||
or: |
or: |
||
<lang vedit> |
<lang vedit>do { |
||
do { |
|||
Message("Spam\n") |
Message("Spam\n") |
||
} while (1) |
} while (1)</lang> |
||
</lang> |
|||
or: |
or: |
||
<lang vedit> |
<lang vedit>for (;1;) { |
||
for (;1;) { |
|||
Message("Spam\n") |
Message("Spam\n") |
||
⚫ | |||
} |
|||
</lang> |
|||
"Nearly infinite" loop can be done by using constant ALL (=1073741824) as repeat count: |
"Nearly infinite" loop can be done by using constant ALL (=1073741824) as repeat count: |
||
<lang vedit> |
<lang vedit>Repeat (ALL) { |
||
Repeat (ALL) { |
|||
Message("Spam\n") |
Message("Spam\n") |
||
⚫ | |||
} |
|||
</lang> |
|||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
Revision as of 22:50, 20 November 2009
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.
ActionScript
<lang actionscript>while (true) {
trace("SPAM");
}</lang>
Ada
<lang ada>loop
Put_Line("SPAM");
end loop;</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>
AmigaE
<lang amigae>PROC main()
LOOP WriteF('SPAM') ENDLOOP
ENDPROC</lang>
AutoHotkey
<lang autohotkey>Loop
MsgBox SPAM `n</lang>
AWK
<lang awk>BEGIN {
while(1) { print "SPAM" }
}</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>
With classic (minimal) BASIC, the standard way to make an infinite loop would be:
10 PRINT "SPAM" 20 GOTO 10
Befunge
Because the 2-D code space is toroidal, all loops are infinite unless explicitly stopped with @. <lang befunge>55+"MAPS",,,,,</lang>
Brainf***
<lang bf>++++++++++[->++++++>++++++++>+<<<]>+++++> [+++.---.<.>---.+++>.<]</lang>
C
<lang c>while(1) puts("SPAM\n");</lang> or <lang c> for(;;) puts("SPAM\n");</lang> or <lang c>do { puts("SPAM\n"); } while(1);</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>
C#
<lang csharp>while (true) {
Console.WriteLine("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>
Clojure
<lang lisp>(loop [] (println "SPAM") (recur))</lang>
Common Lisp
<lang lisp>(loop (write-line "SPAM"))</lang>
D
<lang d>while(true) writefln("SPAM") ;</lang> <lang d>for(;;) writefln("SPAM") ;</lang> <lang d>l: writefln("SPAM"); goto l;</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.
Erlang
<lang erlang>-module (main). -export ([main/1]).
main(Any) ->
io:fwrite("SPAM~n",[]), main(Any)</lang>
FALSE
<lang false>[1]["SPAM "]#</lang>
Forth
<lang forth>: email begin ." SPAM" cr again ;</lang>
Fortran
<lang fortran>DO
WRITE(*,*) "SPAM"
END DO</lang> Although deprecated GOTO is still available <lang fortran>10 WRITE(*,*) "SPAM"
GOTO 10</lang>
Go
<lang go>package main import "fmt" func main() { for { fmt.Printf("SPAM\n") } }</lang>
Groovy
<lang groovy>while (true) {
println 'SPAM'
}</lang>
Haskell
<lang haskell>forever (putStrLn "SPAM")</lang>
Icon
<lang icon>procedure main()
every write(|"SPAM")
end</lang>
IDL
<lang IDL>while 1 do print,'SPAM'</lang>
J
<lang j>-@:(][ 1!:2&2@('SPAM'"_)) (^:_) 1</lang>
Alternatively,
<lang j>smoutput bind 'SPAM'^:1e99 </lang>
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.
Java
<lang java>while(true){
System.out.println("SPAM");
}</lang>
<lang java>for(;;){
System.out.println("SPAM");
}</lang>
JavaScript
<lang javascript>for (;;) print("SPAM"); while (true) print("SPAM");</lang>
Joy
<lang joy>DEFINE loop == [1] swap while.
["SPAM\n" putchars] loop.</lang>
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. <lang Lisaac>- i : INTEGER; i := 1; { i = 1 }.while_do {
"SPAM\n".print;
};</lang>
Logo
<lang logo>forever [print "SPAM]</lang>
M4
<lang M4>define(`spam',`SPAM spam') spam</lang>
Make
<lang make>spam:
@echo SPAM $(MAKE)</lang>
MAXScript
<lang maxscript>while true do print "SPAM\n"</lang>
Metafont
<lang metafont>forever: message "SPAM"; endfor end</lang>
Modula-3
<lang modula3>LOOP
IO.Put("SPAM\n");
END;</lang>
MOO
<lang moo>while (1)
player:tell("SPAM");
endwhile</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.
Octave
<lang octave>while(1)
disp("SPAM")
endwhile</lang>
Pascal
<lang pascal>while true do
writeln('SPAM');</lang>
Alternatively: <lang pascal>repeat
writeln('SPAM')
until false;</lang>
Perl
<lang perl>print "SPAM\n" while 1;</lang>
Perl 6
<lang perl6>loop {
say 'SPAM';
}</lang>
PHP
<lang php>while(1)
echo "SPAM\n";</lang>
Pop11
<lang pop11>while true do
printf('SPAM', '%p\n');
endwhile;</lang>
PowerShell
<lang powershell>for () {
"SPAM"
}</lang>
Prolog
<lang prolog>repeat, write('SPAM'), nl, fail.</lang>
Python
<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"
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>
Ruby
<lang ruby>loop do
puts "SPAM"
end</lang>
Scheme
<lang scheme>(do ()
(#f) (display "SPAM") (newline))</lang>
Slate
<lang slate>[inform: 'SPAM'] loop</lang>
Smalltalk
<lang smalltalk>[ true ] whileTrue: [ 'SPAM' displayNl ]</lang>
SNUSP
<lang snusp>@\>@\>@\>@\>++++++++++===!/ < < < < \
| | | \M=@@@@+@+++++# \.>.>.>.>./ | | \A=@@+@@@@+++# | \P=@@+@@+@@+++# \S=@@+@+@@@+++#</lang>
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.
Transact-SQL
<lang sql>WHILE 1=1 BEGIN
PRINT "SPAM"
END</lang>
Tcl
<lang tcl>while true {
puts SPAM
}
- or
for {} 1 {} {
puts SPAM
}</lang>
TI-89 BASIC
<lang ti89b>Loop
Disp "SPAM"
EndLoop</lang>
UNIX Shell
<lang bash>while :; do echo SPAM; done</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>
V
<lang v>true [
'SPAM' puts
] while</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>
- Programming Tasks
- Iteration
- ActionScript
- Ada
- ALGOL 68
- AmigaE
- AutoHotkey
- AWK
- BASIC
- Befunge
- Brainf***
- C
- C++
- C sharp
- ColdFusion
- Clojure
- Common Lisp
- D
- E
- Erlang
- FALSE
- Forth
- Fortran
- Go
- Groovy
- Haskell
- Icon
- IDL
- J
- Java
- JavaScript
- Joy
- Lisaac
- Logo
- M4
- Make
- MAXScript
- Metafont
- Modula-3
- MOO
- OCaml
- Octave
- Pascal
- Perl
- Perl 6
- PHP
- Pop11
- PowerShell
- Prolog
- Python
- R
- Ruby
- Scheme
- Slate
- Smalltalk
- SNUSP
- Standard ML
- Transact-SQL
- Tcl
- TI-89 BASIC
- UNIX Shell
- UnixPipes
- Unlambda
- V
- Vedit macro language
- Visual Basic
- Visual Basic .NET