Loop/Infinite
From Rosetta Code
Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.
Contents |
[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] 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] 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] Common Lisp
(loop (write-line "SPAM"))
[edit] D
while(true) writefln("SPAM") ;
for(;;) writefln("SPAM") ;
[edit] Erlang
-module (main).
-export ([main/1]).
main(Any) ->
io:fwrite("SPAM~n",[]),
main(Any)
[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] Groovy
while (true) {
println 'SPAM'
}
[edit] Haskell
forever (putStrLn "SPAM")
[edit] Java
while(true){ System.out.println("SPAM"); }
for(;;){ System.out.println("SPAM"); }
[edit] JavaScript
for (;;) print("SPAM");
while (true) print("SPAM");
[edit] Logo
forever [print "SPAM]
[edit] Make
spam: @echo SPAM $(MAKE)
[edit] MAXScript
while true do print "SPAM\n"
[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 usefull to provide data to the next loop without using mutable variable.
[edit] Pascal
while true do writeln('SPAM');
Alternatively:
repeat writeln('SPAM') until false;
[edit] Perl
while(1){print"SPAM\n"}
[edit] Pop11
while true do
printf('SPAM', '%p\n');
endwhile;
[edit] Prolog
repeat, write('SPAM'), nl, fail.
[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] Ruby
loop do puts "SPAM" end
[edit] Scheme
(do () (#f) (display "SPAM") (newline))
[edit] SNUSP
@\>@\>@\>@\>++++++++++===!/ < < < < \ | | | \M=@@@@+@+++++# \.>.>.>.>./ | | \A=@@+@@@@+++# | \P=@@+@@+@@+++# \S=@@+@+@@@+++#
[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
Categories: Programming Tasks | Iteration | Ada | ALGOL 68 | BASIC | Befunge | Brainf*** | C | ColdFusion | Common Lisp | D | Erlang | Forth | Fortran | Groovy | Haskell | Java | JavaScript | Logo | Make | MAXScript | OCaml | Pascal | Perl | Pop11 | Prolog | Python | Ruby | Scheme | SNUSP | UnixPipes | Unlambda | V

