Loops/Infinite: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 2: Line 2:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<actionscript>
<lang actionscript>
while (true) {
while (true) {
trace("SPAM");
trace("SPAM");
}
}
</lang>
</actionscript>


=={{header|Ada}}==
=={{header|Ada}}==
<ada>loop
<lang ada>loop
Put_Line("SPAM");
Put_Line("SPAM");
end loop;</ada>
end loop;</lang>
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
DO
DO
Line 24: Line 24:
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
Old-fashioned syntax:
Old-fashioned syntax:
<qbasic>while 1
<lang qbasic>while 1
print "SPAM"
print "SPAM"
wend</qbasic>
wend</lang>


Standard BASIC:
Standard BASIC:
<qbasic>do
<lang qbasic>do
print "SPAM"
print "SPAM"
loop</qbasic>
loop</lang>


Also
Also
<qbasic>for i = 1 to 10 step 0
<lang qbasic>for i = 1 to 10 step 0
print "SPAM"
print "SPAM"
next i</qbasic>
next i</lang>


With classic (minimal) BASIC, the standard way to make an infinite loop would be:
With classic (minimal) BASIC, the standard way to make an infinite loop would be:
Line 53: Line 53:


=={{header|C}}==
=={{header|C}}==
<c>while(1) puts("SPAM\n");</c>
<lang c>while(1) puts("SPAM\n");</lang>
or
or
<c> for(;;) puts("SPAM\n");</c>
<lang c> for(;;) puts("SPAM\n");</lang>
or
or
<c>do { puts("SPAM\n"); } while(1);</c>
<lang c>do { puts("SPAM\n"); } while(1);</lang>


=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
Line 75: Line 75:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lisp>(loop (write-line "SPAM"))</lisp>
<lang lisp>(loop (write-line "SPAM"))</lang>
=={{header|D}}==
=={{header|D}}==
<d>while(true) writefln("SPAM") ;</d>
<lang d>while(true) writefln("SPAM") ;</lang>
<d>for(;;) writefln("SPAM") ;</d>
<lang d>for(;;) writefln("SPAM") ;</lang>


=={{header|Erlang}}==
=={{header|Erlang}}==
Line 114: Line 114:


=={{header|Java}}==
=={{header|Java}}==
<java>while(true){
<lang java>while(true){
System.out.println("SPAM");
System.out.println("SPAM");
}</java>
}</lang>


<java>for(;;){
<lang java>for(;;){
System.out.println("SPAM");
System.out.println("SPAM");
}</java>
}</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 145: Line 145:


=={{header|OCaml}}==
=={{header|OCaml}}==
<ocaml>while true do
<lang ocaml>while true do
print_endline "SPAM"
print_endline "SPAM"
done</ocaml>
done</lang>


or
or


<ocaml>let rec inf_loop() =
<lang ocaml>let rec inf_loop() =
print_endline "SPAM";
print_endline "SPAM";
inf_loop()
inf_loop()
in
in
inf_loop()</ocaml>
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 usefull to provide data to the next loop without using mutable variable.
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.


=={{header|Pascal}}==
=={{header|Pascal}}==
<pascal>
<lang pascal>
while true do
while true do
writeln('SPAM');
writeln('SPAM');
</pascal>
</lang>
Alternatively:
Alternatively:
<pascal>
<lang pascal>
repeat
repeat
writeln('SPAM')
writeln('SPAM')
until false;
until false;
</pascal>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<perl>print "SPAM\n" while 1;</perl>
<lang perl>print "SPAM\n" while 1;</lang>


=={{header|PHP}}==
=={{header|PHP}}==
<php>while(1)
<lang php>while(1)
echo "SPAM\n";</php>
echo "SPAM\n";</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 189: Line 189:


=={{header|Python}}==
=={{header|Python}}==
<python>while 1:
<lang python>while 1:
print "SPAM"</python>
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"
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"


=={{header|Ruby}}==
=={{header|Ruby}}==
<ruby>loop do
<lang ruby>loop do
puts "SPAM"
puts "SPAM"
end</ruby>
end</lang>
=={{header|Scheme}}==
=={{header|Scheme}}==
<scheme>(do ()
<lang scheme>(do ()
(#f)
(#f)
(display "SPAM")
(display "SPAM")
(newline))</scheme>
(newline))</lang>


=={{header|SNUSP}}==
=={{header|SNUSP}}==

Revision as of 15:38, 3 February 2009

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.

ActionScript

<lang actionscript> while (true) {

   trace("SPAM");

} </lang>

Ada

<lang ada>loop

  Put_Line("SPAM");

end loop;</lang>

ALGOL 68

DO
  printf($"SPAM"l$)
OD

Or the classic "dynamic halt":

loop x:
   printf($"SPAM"l$);
loop x

BASIC

Works with: QuickBasic version 4.5

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 @.

55+"MAPS",,,,,

Brainf***

++++++++++[->++++++>++++++++>+<<<]>+++++>
[+++.---.<.>---.+++>.<]

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>

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

<lang lisp>(loop (write-line "SPAM"))</lang>

D

<lang d>while(true) writefln("SPAM") ;</lang> <lang d>for(;;) writefln("SPAM") ;</lang>

Erlang

-module (main).
-export ([main/1]).

main(Any) ->
  io:fwrite("SPAM~n",[]),
  main(Any)

Forth

: email   begin ." SPAM" cr again ;

Fortran

Works with: Fortran version 90 and later
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")

Icon

procedure main()
   every write(|"SPAM")
end

Java

<lang java>while(true){

  System.out.println("SPAM");

}</lang>

<lang java>for(;;){

  System.out.println("SPAM");

}</lang>

JavaScript

for (;;) print("SPAM");
while (true) print("SPAM");

forever [print "SPAM]

Make

spam:
   @echo SPAM
   $(MAKE)

MAXScript

while true do print "SPAM\n"

Modula-3

LOOP
  IO.Put("SPAM\n");
END;

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 usefull to provide data to the next loop without using mutable variable.

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>

PHP

<lang php>while(1)

   echo "SPAM\n";</lang>

Pop11

while true do
    printf('SPAM', '%p\n');
endwhile;

Prolog

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

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"

Ruby

<lang ruby>loop do

  puts "SPAM"

end</lang>

Scheme

<lang scheme>(do ()

   (#f)
   (display "SPAM")
   (newline))</lang>

SNUSP

@\>@\>@\>@\>++++++++++===!/ < < < < \
 |  |  |  \M=@@@@+@+++++# \.>.>.>.>./
 |  |  \A=@@+@@@@+++#
 |  \P=@@+@@+@@+++#
 \S=@@+@+@@@+++#

UnixPipes

yes SPAM

Unlambda

``ci``s``s`kr``s``s``s``s`k.S`k.P`k.A`k.Mii

V

true [
   'SPAM' puts
] while

Visual Basic

       Do
           Console.WriteLine("SPAM")
       Loop