Loops/Infinite: Difference between revisions
(Vedit macro language added) |
m (Highlighting, whitespace) |
||
Line 150: | Line 150: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth>: email begin ." SPAM" cr again ;</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{Works with|Fortran|90 and later}} |
{{Works with|Fortran|90 and later}} |
||
<lang fortran>DO |
|||
DO |
|||
WRITE(*,*) "SPAM" |
|||
END DO</lang> |
|||
Although deprecated GOTO is still available |
Although deprecated GOTO is still available |
||
<lang fortran>10 WRITE(*,*) "SPAM" |
|||
GOTO 10</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
<lang groovy>while (true) { |
|||
println 'SPAM' |
|||
}</lang> |
|||
} |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell>forever (putStrLn "SPAM")</lang> |
|||
=={{header|Icon}}== |
=={{header|Icon}}== |
||
<lang icon>procedure main() |
|||
every write(|"SPAM") |
|||
end</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 184: | Line 184: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<lang javascript>for (;;) print("SPAM"); |
|||
while (true) print("SPAM");</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<lang logo>forever [print "SPAM]</lang> |
|||
=={{header|Make}}== |
=={{header|Make}}== |
||
<lang make>spam: |
|||
@echo SPAM |
|||
$(MAKE)</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<lang maxscript>while true do print "SPAM\n"</lang> |
|||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
Line 324: | Line 324: | ||
=={{header|V}}== |
=={{header|V}}== |
||
<lang v>true [ |
|||
'SPAM' puts |
|||
] while</lang> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Line 354: | Line 354: | ||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
||
<lang vb>Do |
|||
⚫ | |||
Do |
|||
Loop</lang> |
|||
⚫ | |||
Loop |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
'''Platform:''' [[.NET]] |
'''Platform:''' [[.NET]] |
||
{{works with|Visual Basic .NET|9.0+}} |
{{works with|Visual Basic .NET|9.0+}} |
||
<lang vbnet>Do |
<lang vbnet>Do |
||
Console.WriteLine("SPAM") |
Console.WriteLine("SPAM") |
Revision as of 14:57, 13 July 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
DO printf($"SPAM"l$) OD
Or the classic "dynamic halt":
loop x: printf($"SPAM"l$); loop x
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 @.
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>
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:
<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> <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
-module (main). -export ([main/1]). main(Any) -> io:fwrite("SPAM~n",[]), main(Any)
FALSE
[1]["SPAM "]#
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>
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>
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>
Logo
<lang logo>forever [print "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>
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>
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>
Slate
<lang slate>[inform: 'SPAM'] loop</lang>
Smalltalk
<lang smalltalk>[ true ] whileTrue: [ 'SPAM' displayNl ]</lang>
SNUSP
@\>@\>@\>@\>++++++++++===!/ < < < < \ | | | \M=@@@@+@+++++# \.>.>.>.>./ | | \A=@@+@@@@+++# | \P=@@+@@+@@+++# \S=@@+@+@@@+++#
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 tsql>WHILE 1=1 BEGIN
PRINT "SPAM"
END</lang>
Tcl
<lang tcl>while true {
puts SPAM
}
- or
for {} 1 {} {
puts SPAM
}</lang>
UnixPipes
yes SPAM
Unlambda
``ci``s``s`kr``s``s``s``s`k.S`k.P`k.A`k.Mii
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
Console.WriteLine("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
- Common Lisp
- D
- E
- Erlang
- FALSE
- Forth
- Fortran
- Groovy
- Haskell
- Icon
- Java
- JavaScript
- Logo
- Make
- MAXScript
- Metafont
- Modula-3
- OCaml
- Octave
- Pascal
- Perl
- PHP
- Pop11
- Prolog
- Python
- Ruby
- Scheme
- Slate
- Smalltalk
- SNUSP
- Standard ML
- Transact-SQL
- Tcl
- UnixPipes
- Unlambda
- V
- Vedit macro language
- Visual Basic
- Visual Basic .NET