Some of Sunday's edits have been lost. The edits from Saturday that were reverted have been restored. Site is now hosted on prgmr.com. Thank you for your patience. This notice will be removed one week from posting. --Michael Mol 18:12, 7 March 2010 (UTC)
Loops/Do-while
From Rosetta Code
[edit] ActionScript
var val:int = 0;
do
{
trace(++val);
} while (val % 6);
[edit] Ada
loop
Value := Value + 1;
Put (Value);
exit when Value mod 6 = 0;
end loop;
Here is an alternative version:
for Value in 0..Integer'Last loop
Put (Value);
exit when Value mod 6 = 0;
end loop;
[edit] ALGOL 68
FOR value WHILE
print(value);
# WHILE # value MOD 6 /= 0 DO
SKIP
OD
[edit] AmigaE
PROC main()
DEF i = 0
REPEAT
i := i + 1
WriteF('\d\n', i)
UNTIL Mod(i, 6) = 0
ENDPROC
[edit] AutoHotkey
While mod(A_Index, 6) ;comment:everything but 0 is considered true
output = %output%`n%A_Index%
MsgBox % output
[edit] AWK
BEGIN {
val = 0
do {
val++
print val
} while( val % 6 != 0)
}
[edit] BASIC
Works with: QuickBasic version 4.5
a = 0
DO
a = a + 1
PRINT a
LOOP WHILE a MOD 6 <> 0
[edit] Befunge
0>1+:.v
|%6: <
@
[edit] C
int val = 0;
do{
val++;
printf("%d\n",val);
}while(val % 6 != 0);
[edit] C++
int val = 0;
do{
val++;
cout << val << endl;
}while(val % 6 != 0);
[edit] C#
int a = 0;
do
{
Console.WriteLine(a);
a += 1;
} while (a % 6 != 0);
[edit] ColdFusion
<cfscript>
value = 0;
do
{
value += 1;
writeOutput( value );
} while( value % 6 != 0 );
</cfscript>
[edit] Common Lisp
(setq val 0)
(loop do
(incf val)
(print val)
while (/= 0 (mod val 6)))
[edit] D
int val = 0;
do{
val++;
writefln(val);
}while(val % 6 != 0);
[edit] 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.
var x := 0
__loop(fn {
x += 1
println(x)
x % 6 != 0 # this is the return value of the function
})
[edit] Factor
0 [ dup 6 mod 0 = not ] [ [ . ] [ 1 + ] bi ] do while drop
[edit] Forth
: do-until
0
begin 1+
dup .
dup 6 mod 0=
until
drop ;
[edit] Fortran
Works with: Fortran version 90 and later
INTEGER :: i = 0
DO
i = i + 1
WRITE(*, *) i
IF (MOD(i, 6) == 0) EXIT
END DO
[edit] Haskell
import Data.List
import Control.Monad
import Control.Arrow
doWhile p f n = (n:) $ takeWhile p $ unfoldr (Just.(id &&& f)) $ succ n
Example executed in GHCi:
*Main> mapM_ print $ doWhile ((/=0).(`mod`6)) succ 0
0
1
2
3
4
5
[edit] 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).
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
)
Though it's rare to see J code like this.
[edit] Java
int val = 0;
do{
val++;
System.out.println(val);
}while(val % 6 != 0);
[edit] JavaScript
var val = 0;
do {
print(++val);
} while (val % 6);
[edit] Lua
Lua doesn't have a do .. while construct.
i=0
repeat
i=i+1
print(i)
until i%6 == 0
[edit] Lisaac
+ val : INTEGER;
{
val := val + 1;
val.print;
'\n'.print;
val % 6 != 0
}.while_do { };
[edit] 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
[edit] Mathematica
value = 5;
NestWhile[
# + 1 &
,
value
, (Print[#]; Mod[#, 6] != 0) &
];
gives back:
5
6
If the starting value is 6, only 6 is returned.
[edit] MAXScript
a = 0
do
(
print a
a += 1
)
while mod a 6 != 0
[edit] Metafont
Metafont has no a do-while construct; the same thing can be done using a forever loop and exitif.
a := 0;
forever: show a; a := a + 1; exitif a mod 6 = 0; endfor
end
[edit] OCaml
OCaml doesn't have a do-while loop, so we can just make a local loop:
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
or implementing a generic do-while iterator with higher order function:
let do_while f p =
let rec loop() =
f();
if p() then loop()
in
loop()
(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *)
let v = ref 0 in
do_while (fun () -> incr v; Printf.printf "%d\n" !v)
(fun () -> !v mod 6 <> 0)
The example above is the an imperative form, below is its functional counterpart:
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
Or in a very poor OCaml style, we can use an exception to exit a while loop:
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 -> ()
[edit] Octave
The do-while can be changed into a do-until, just negating the condition of the while.
val = 0;
do
val++;
disp(val)
until( mod(val, 6) == 0 )
[edit] Oz
Normal Oz variables are single-assignment only. So we use a "cell", which is a one-element mutable container.
declare
I = {NewCell 0}
in
for until:@I mod 6 == 0 do
I := @I + 1
{Show @I}
end
[edit] Pascal
program countto6(output);
var
i: integer;
begin
i := 0;
repeat
i := i + 1;
writeln(i)
until i mod 6 = 0
end.
[edit] Perl
my $val = 0;
do {
$val++;
print "$val\n";
} while ($val % 6);
do ... until (condition) is equivalent to do ... while (not condition).
my $val = 0;
do {
$val++;
print "$val\n";
} until ($val % 6 == 0);
[edit] Perl 6
Works with: Rakudo version #21 "Seattle"
my $val = 0;
repeat {
say ++$val;
} while $val % 6;
repeat ... until condition is equivalent to do ... while not condition.
my $val = 0;
repeat {
say ++$val;
} until $val % 6 == 0;
You can also put the condition before the block, without changing the order of evaluation.
my $val = 0;
repeat while $val % 6 {
say ++$val;
}
[edit] PHP
$val = 0;
do {
$val++;
print "$val\n";
} while ($val % 6 != 0);
[edit] PicoLisp
Literally:
(let Val 0
(loop
(println (inc 'Val))
(T (=0 (% Val 6))) ) )
Shorter:
(let Val 0
(until (=0 (% (println (inc 'Val)) 6))) )
or:
(for (Val 0 (n0 (% (println (inc 'Val)) 6))))
[edit] Pike
int main(){
int value = 0;
do {
value++;
write(value + "\n");
} while (value % 6);
}
[edit] PL/I
value = 0;
do value = 0, value+1 while (mod(value,6) ^= 0);
put list (value);
end;
[edit] Pop11
lvars val = 0;
while true do
val + 1 -> val;
printf(val, '%p\n');
quitif(val rem 6 = 0);
endwhile;
[edit] PowerShell
$n = 0
do {
$n++
$n
} while ($n % 6 -ne 0)
[edit] PureBasic
Works with: PureBasic version 4.41
x=0
Repeat
x+1
Debug x
Until x%6=0
[edit] Python
Python doesn't have a do-while loop.
val = 0
while True:
val +=1
print val
if val % 6 == 0: break
or repeat the body of the loop before a standard while.
val = 1
print val
while val % 6 != 0:
val += 1
print val
[edit] R
i <- 0
repeat
{
i <- i + 1
print(i)
if(i %% 6 == 0) break
}
[edit] 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
]
Output:
1 2 3 4 5 6
[edit] REXX
In the do until construct, the expression is evaluated at the end, even though it is written at the beginning.
v = 0
do until v//6 = 0
v = v + 1
say v
end
[edit] Ruby
val = 0
begin
val += 1
puts val
end while val % 6 != 0
begin ... end until condition is equivalent to begin ... end while not condition.
val = 0
begin
val += 1
puts val
end until val % 6 == 0
[edit] Scheme
(let loop ((i 1))
(display i)
(if (positive? (modulo i 6))
(loop (+ i 1))))
[edit] 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;
[edit] Slate
[| val |
val: 0.
[val: val + 1.
print: val.
val \\ 6 ~= 0] whileTrue
] do.
[edit] Smalltalk
To simulate the do-while construct, we can use the whileTrue: method of a block with a void while block.
|val|
val := 0.
[
val := val + 1.
val displayNl.
(val rem: 6) ~= 0
] whileTrue: [ ]
[edit] Suneido
val = 0
do
{
Print(++val)
} while (val % 6 isnt 0)
Output:
1
2
3
4
5
6
[edit] 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
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}
The control package of Library: tcllib has this:
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}
Mind you, it is also normal to write this task using a normal while as:
set i 0
while true {
puts [incr i]
if {$i % 6 == 0} break
}
[edit] Vedit macro language
#1 = 0
do {
#1++
Num_Type(#1)
} while (#1 % 6 != 0);
[edit] Visual Basic .NET
Dim i = 0
Do
i += 1
Console.WriteLine(i)
Loop Until i Mod 6 = 0







