Loops/Do-while: Difference between revisions
Line 423: | Line 423: | ||
<lang prolog> |
<lang prolog> |
||
% initial condition |
% initial condition |
||
do(0):- |
do(0):- write(0),nl,do(1). |
||
write(0),nl, |
|||
do(1). |
|||
% control condition |
% control condition |
||
⚫ | |||
do(V):- |
|||
⚫ | |||
X = 0. |
|||
% loop |
% loop |
Revision as of 13:18, 1 April 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once.
ActionScript
<lang actionscript>var val:int = 0; do {
trace(++val);
} while (val % 6);</lang>
Ada
<lang ada>loop
Value := Value + 1; Put (Value); exit when Value mod 6 = 0;
end loop;</lang> Here is an alternative version: <lang ada>for Value in 0..Integer'Last loop
Put (Value); exit when Value mod 6 = 0;
end loop;</lang>
ALGOL 68
<lang algol68>FOR value WHILE
print(value);
- WHILE # value MOD 6 /= 0 DO
SKIP
OD</lang>
AmigaE
<lang amigae>PROC main()
DEF i = 0 REPEAT i := i + 1 WriteF('\d\n', i) UNTIL Mod(i, 6) = 0
ENDPROC</lang>
AutoHotkey
<lang AutoHotkey>While mod(A_Index, 6) ;comment:everything but 0 is considered true
output = %output%`n%A_Index%
MsgBox % output</lang>
AWK
<lang awk>BEGIN {
val = 0 do { val++ print val } while( val % 6 != 0)
}</lang>
BASIC
<lang qbasic>a = 0 do
a = a + 1 print a
loop while a mod 6 <> 0</lang>
Befunge
<lang befunge>0>1+:.v
|%6: < @</lang>
C
<lang c>int val = 0; do{
val++; printf("%d\n",val);
}while(val % 6 != 0);</lang>
C++
<lang cpp>int val = 0; do{
val++; cout << val << endl;
}while(val % 6 != 0);</lang>
C#
<lang csharp>int a = 0;
do {
Console.WriteLine(a); a += 1;
} while (a % 6 != 0);</lang>
ColdFusion
<lang cfm><cfscript>
value = 0; do { value += 1; writeOutput( value ); } while( value % 6 != 0 );
</cfscript></lang>
Common Lisp
<lang lisp>(setq val 0) (loop do
(incf val) (print val) while (/= 0 (mod val 6)))</lang>
D
<lang d>int val = 0; do{
val++; writefln(val);
}while(val % 6 != 0);</lang>
E
E does not have an official do-while construct, but the primitive which loops are built out of (which calls a function which returns a boolean indicating whether it should be called again) can be used to construct a do-while. <lang e>var x := 0 __loop(fn {
x += 1 println(x) x % 6 != 0 # this is the return value of the function
})</lang>
Factor
<lang factor>0 [ dup 6 mod 0 = not ] [ [ . ] [ 1 + ] bi ] do while drop</lang>
Forth
<lang forth>: do-until
0 begin 1+ dup . dup 6 mod 0= until drop ;</lang>
Fortran
<lang fortran>INTEGER :: i = 0 DO
i = i + 1 WRITE(*, *) i IF (MOD(i, 6) == 0) EXIT
END DO</lang>
Haskell
<lang haskell>import Data.List import Control.Monad import Control.Arrow
doWhile p f n = (n:) $ takeWhile p $ unfoldr (Just.(id &&& f)) $ succ n</lang> Example executed in GHCi: <lang haskell>*Main> mapM_ print $ doWhile ((/=0).(`mod`6)) succ 0 0 1 2 3 4 5</lang>
J
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
,. ([^:(0=6|])>:)^:a: 0
J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided). <lang j>3 : 0 ] 0
NB. The 'st' in 'whilst' stands for 'skip test'
whilst. 0 ~: 6 | y do. y 1!:2 ]2 y =. y+1 end.
i.0 0 )</lang>
Though it's rare to see J code like this.
Java
<lang java>int val = 0; do{
val++; System.out.println(val);
}while(val % 6 != 0);</lang>
JavaScript
<lang javascript>var val = 0; do {
print(++val);
} while (val % 6);</lang>
Lua
Lua doesn't have a do .. while
construct.
<lang lua> i=0 repeat
i=i+1 print(i)
until i%6 == 0 </lang>
Lisaac
<lang Lisaac>+ val : INTEGER; {
val := val + 1; val.print; '\n'.print; val % 6 != 0
}.while_do { };</lang>
Logo
<lang logo>make "val 0 do.while [make "val :val + 1 print :val] [notequal? 0 modulo :val 6] do.until [make "val :val + 1 print :val] [equal? 0 modulo :val 6]
to my.loop :n
make "n :n + 1 print :n if notequal? 0 modulo :n 6 [my.loop :n]
end my.loop 0</lang>
Mathematica
<lang Mathematica>value = 5; NestWhile[
# + 1 & , value , (Print[#]; Mod[#, 6] != 0) & ];</lang>
gives back: <lang Mathematica>5 6</lang> If the starting value is 6, only 6 is returned.
MAXScript
<lang maxscript>a = 0 do (
print a a += 1
) while mod a 6 != 0</lang>
Metafont
Metafont has no a do-while construct; the same thing can be done using a forever loop and exitif.
<lang metafont>a := 0; forever: show a; a := a + 1; exitif a mod 6 = 0; endfor end</lang>
OCaml
OCaml doesn't have a do-while loop, so we can just make a local loop: <lang ocaml>let rec loop i =
let i = succ i in Printf.printf "%d\n" i; if i mod 6 <> 0 then loop i in loop 0</lang>
or implementing a generic do-while iterator with higher order function:
<lang ocaml>let do_while f p =
let rec loop() = f(); if p() then loop() in loop()
(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *)</lang>
<lang ocaml>let v = ref 0 in do_while (fun () -> incr v; Printf.printf "%d\n" !v)
(fun () -> !v mod 6 <> 0)</lang>
The example above is the an imperative form, below is its functional counterpart: <lang ocaml>let do_while f p ~init =
let rec loop v = let v = f v in if p v then loop v in loop init
do_while (fun v ->
let v = succ v in Printf.printf "%d\n" v; (v)) (fun v -> v mod 6 <> 0) ~init:0</lang>
Or in a very poor OCaml style, we can use an exception to exit a while loop: <lang ocaml>let v = ref 0 exception Exit_loop try while true do
incr v; Printf.printf "%d\n" !v; if not(!v mod 6 <> 0) then raise Exit_loop;
done with Exit_loop -> ()</lang>
Octave
The do-while can be changed into a do-until, just negating the condition of the while. <lang octave>val = 0; do
val++; disp(val)
until( mod(val, 6) == 0 )</lang>
Oz
Normal Oz variables are single-assignment only. So we use a "cell", which is a one-element mutable container. <lang oz>declare
I = {NewCell 0}
in
for until:@I mod 6 == 0 do I := @I + 1 {Show @I} end</lang>
Pascal
<lang pascal>program countto6(output);
var
i: integer;
begin
i := 0; repeat i := i + 1; writeln(i) until i mod 6 = 0
end.</lang>
Perl
<lang perl>my $val = 0; do {
$val++; print "$val\n";
} while ($val % 6);</lang>
do ... until (condition)
is equivalent to do ... while (not condition)
.
<lang perl>my $val = 0;
do {
$val++; print "$val\n";
} until ($val % 6 == 0);</lang>
Perl 6
<lang perl6>my $val = 0; repeat {
say ++$val;
} while $val % 6;</lang>
repeat ... until condition
is equivalent to do ... while not condition
.
<lang perl6>my $val = 0; repeat {
say ++$val;
} until $val % 6 == 0;</lang>
You can also put the condition before the block, without changing the order of evaluation.
<lang perl6>my $val = 0; repeat while $val % 6 {
say ++$val;
}</lang>
PHP
<lang php>$val = 0; do {
$val++; print "$val\n";
} while ($val % 6 != 0);</lang>
PicoLisp
Literally: <lang PicoLisp>(let Val 0
(loop (println (inc 'Val)) (T (=0 (% Val 6))) ) )</lang>
Shorter: <lang PicoLisp>(let Val 0
(until (=0 (% (println (inc 'Val)) 6))) )</lang>
or: <lang PicoLisp>(for (Val 0 (n0 (% (println (inc 'Val)) 6))))</lang>
Pike
<lang pike>int main(){
int value = 0; do { value++; write(value + "\n"); } while (value % 6);
}</lang>
PL/I
<lang pli> value = 0; do value = 0, value+1 while (mod(value,6) ^= 0);
put list (value);
end; </lang>
Pop11
<lang pop11>lvars val = 0; while true do
val + 1 -> val; printf(val, '%p\n'); quitif(val rem 6 = 0);
endwhile;</lang>
PowerShell
<lang powershell>$n = 0 do {
$n++ $n
} while ($n % 6 -ne 0)</lang>
Prolog
<lang prolog> % initial condition do(0):- write(0),nl,do(1).
% control condition do(V):- X is mod(V,6),X = 0,!,fail.
% loop do(V) :-
write(V),nl, Y is V + 1, do(Y).
wloop :-
do(0).
</lang>
PureBasic
<lang PureBasic>x=0 Repeat
x+1 Debug x
Until x%6=0 </lang>
Python
Python doesn't have a do-while loop. <lang python>val = 0 while True:
val +=1 print val if val % 6 == 0: break</lang>
or repeat the body of the loop before a standard while. <lang python>val = 1 print val while val % 6 != 0:
val += 1 print val</lang>
R
<lang R>i <- 0 repeat {
i <- i + 1 print(i) if(i %% 6 == 0) break
}</lang>
REBOL
<lang REBOL>REBOL [ Title: "Loop/While" Author: oofoe Date: 2009-12-19 URL: http://rosettacode.org/wiki/Loop/Do_While ]
- REBOL doesn't have a specific 'do/while' construct, but 'until' can
- be used to provide the same effect.
value: 0 until [ value: value + 1 print value
0 = mod value 6 ]</lang>
Output:
1 2 3 4 5 6
REXX
In the do until construct, the expression is evaluated at the end, even though it is written at the beginning. <lang rexx>v = 0 do until v//6 = 0
v = v + 1 say v
end</lang>
Ruby
<lang ruby>val = 0 begin
val += 1 puts val
end while val % 6 != 0</lang>
begin ... end until condition
is equivalent to begin ... end while not condition
.
<lang ruby>val = 0
begin
val += 1 puts val
end until val % 6 == 0</lang>
Scheme
<lang scheme>(let loop ((i 1))
(display i) (if (positive? (modulo i 6)) (loop (+ i 1))))</lang>
Seed7
<lang seed7>$ include "seed7_05.s7i";
const proc: main is func
local var integer: number is 0; begin repeat incr(number); writeln(number) until number rem 6 = 0 end func;</lang>
Slate
<lang slate>[| val |
val: 0. [val: val + 1. print: val. val \\ 6 ~= 0] whileTrue
] do.</lang>
Smalltalk
To simulate the do-while construct, we can use the whileTrue: method of a block with a void while block. <lang smalltalk>|val| val := 0. [
val := val + 1. val displayNl. (val rem: 6) ~= 0
] whileTrue: [ ]</lang>
Suneido
<lang Suneido>val = 0 do
{ Print(++val) } while (val % 6 isnt 0)</lang>
Output:
<lang Suneido>1 2 3 4 5 6</lang>
Tcl
Tcl does not have a built-in do...while
construct. This example demonstrates the ease of creating new looping contructs in plain Tcl. do
procedure taken from Tcler's wiki
<lang tcl>proc do {body keyword expression} {
if {$keyword eq "while"} { set expression "!($expression)" } elseif {$keyword ne "until"} { return -code error "unknown keyword \"$keyword\": must be until or while" } set condition [list expr $expression] while 1 { uplevel 1 $body if {[uplevel 1 $condition]} { break } } return
}
set i 0 do {puts [incr i]} while {$i % 6 != 0}</lang>
The control
package of
has this:
<lang tcl>package require control set i 0; control::do {puts [incr i]} while {$i % 6 != 0} set i 0; control::do {puts [incr i]} until {$i % 6 == 0}</lang>
Mind you, it is also normal to write this task using a normal while
as:
<lang tcl>set i 0
while true {
puts [incr i] if {$i % 6 == 0} break
}</lang>
Vedit macro language
<lang vedit>#1 = 0 do {
#1++ Num_Type(#1)
} while (#1 % 6 != 0);</lang>
Visual Basic .NET
<lang vbnet>Dim i = 0 Do
i += 1 Console.WriteLine(i)
Loop Until i Mod 6 = 0</lang>
- Programming Tasks
- Iteration
- ActionScript
- Ada
- ALGOL 68
- AmigaE
- AutoHotkey
- AWK
- BASIC
- Befunge
- C
- C++
- C sharp
- ColdFusion
- Common Lisp
- D
- E
- Factor
- Forth
- Fortran
- Haskell
- J
- Java
- JavaScript
- Lua
- Lisaac
- Logo
- Mathematica
- MAXScript
- Metafont
- OCaml
- Octave
- Oz
- Pascal
- Perl
- Perl 6
- PHP
- PicoLisp
- Pike
- PL/I
- Pop11
- PowerShell
- Prolog
- PureBasic
- Python
- R
- REBOL
- REXX
- Ruby
- Scheme
- Seed7
- Slate
- Smalltalk
- Suneido
- Tcl
- Tcllib
- Vedit macro language
- Visual Basic .NET