Loops/Infinite: Difference between revisions
(→{{header|C}}: puts() already adds a newline) |
(→{{header|C}}: for(;;)) |
||
Line 42: | Line 42: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<c>while(1) puts("SPAM");</c> |
<c>while(1) puts("SPAM");</c> |
||
or |
|||
<c> for(;;) puts("SPAM");</c> |
|||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
Revision as of 22:59, 26 September 2008
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.
Ada
<ada>loop
Put_Line("SPAM");
end loop;</ada>
ALGOL 68
DO printf($"SPAM"l$) OD
BASIC
Old-fashioned syntax: <qbasic>while 1
print "SPAM"
wend</qbasic>
Standard BASIC: <qbasic>do
print "SPAM"
loop</qbasic>
Also <qbasic>for i = 1 to 10 step 0
print "SPAM"
next i</qbasic>
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 @.
55+"MAPS",,,,,
Brainf***
++++++++++[->++++++>++++++++>+<<<]>+++++> [+++.---.<.>---.+++>.<]
C
<c>while(1) puts("SPAM");</c> or <c> for(;;) puts("SPAM");</c>
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>
Common Lisp
<lisp>(loop (write-line "SPAM"))</lisp>
D
<d>while(true) writefln("SPAM") ;</d> <d>for(;;) writefln("SPAM") ;</d>
Erlang
-module (main). -export ([main/1]). main(Any) -> io:fwrite("SPAM~n",[]), main(Any)
Forth
: email begin ." SPAM" cr again ;
Fortran
DO WRITE(*,*) "SPAM" END DO
Although deprecated GOTO is still available
10 WRITE(*,*) "SPAM" GOTO 10
Groovy
while (true) { println 'SPAM' }
Haskell
forever (putStrLn "SPAM")
Java
<java>while(true){
System.out.println("SPAM");
}</java>
<java>for(;;){
System.out.println("SPAM");
}</java>
JavaScript
for (;;) print("SPAM"); while (true) print("SPAM");
Logo
forever [print "SPAM]
Make
spam: @echo SPAM $(MAKE)
MAXScript
while true do print "SPAM\n"
OCaml
<ocaml>while true do
print_endline "SPAM"
done</ocaml>
or
<ocaml>let rec inf_loop() =
print_endline "SPAM"; inf_loop()
in inf_loop()</ocaml>
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.
Pascal
<pascal> while true do
writeln('SPAM');
</pascal>
Perl
<perl>while(1){print"SPAM\n"}</perl>
Pop11
while true do printf('SPAM', '%p\n'); endwhile;
Prolog
repeat, write('SPAM'), nl, fail.
Python
<python>while 1:
print "SPAM"</python>
Ruby
loop do
puts "SPAM"
end
Scheme
<scheme>(do ()
(#f) (display "SPAM") (newline))</scheme>
SNUSP
@\>@\>@\>@\>++++++++++===!/ < < < < \ | | | \M=@@@@+@+++++# \.>.>.>.>./ | | \A=@@+@@@@+++# | \P=@@+@@+@@+++# \S=@@+@+@@@+++#
UnixPipes
yes SPAM
V
true [ 'SPAM' puts ] while