Loops/Do-while

From Rosetta Code
Task
Loops/Do-while
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.

Reference

360 Assembly[edit]

The WTO macro is in SYS1.MACLIB, which needs to be in the SYSLIB concatenation at assembly.

 
DOWHILE CSECT , -This program's control section
BAKR 14,0 -Caller's registers to linkage stack
LR 12,15 -load entry point address into Reg 12
USING DOWHILE,12 -tell assembler we use Reg 12 as base
XR 9,9 -clear Reg 9 - divident value
LA 6,6 -load divisor value 6 in Reg 6
LA 8,WTOLEN -address of WTO area in Reg 8
LOOP DS 0H
LA 9,1(,9) -add 1 to divident Reg 9
ST 9,FW2 -store it
LM 4,5,FDOUBLE -load into even/odd register pair
STH 9,WTOTXT -store divident in text area
MVI WTOTXT,X'F0' -first of two bytes zero
OI WTOTXT+1,X'F0' -make second byte printable
WTO TEXT=(8) -print it (Write To Operator macro)
DR 4,6 -divide Reg pair 4,5 by Reg 6
LTR 5,5 -test quotient (remainder in Reg 4)
BNZ RETURN -if one: 6 iterations, exit loop.
B LOOP -if zero: loop again.
RETURN PR , -return to caller.
FDOUBLE DC 0FD
DC F'0'
FW2 DC F'0'
WTOLEN DC H'2' -fixed WTO length of two
WTOTXT DC CL2' '
END DOWHILE
 

6502 Assembly[edit]

Code is called as a subroutine (i.e. JSR DoWhileSub). Specific OS/hardware routines for printing are left unimplemented.

DoWhileSub:	PHA
TYA
PHA ;push accumulator and Y register onto stack
 
LDY #0
DoWhileLoop: INY
JSR DisplayValue ;routine not implemented
TYA
SEC
Modulus: SBC #6
BCS Modulus
ADC #6
BNE DoWhileLoop
 
PLA
TAY
PLA ;restore Y register and accumulator from stack
RTS ;return from subroutine

ActionScript[edit]

var val:int = 0;
do
{
trace(++val);
} while (val % 6);

Ada[edit]

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;

Aime[edit]

integer a;
 
a = 0;
do {
a += 1;
o_integer(a);
o_byte('\n');
} while (a % 6 != 0);

ALGOL 68[edit]

FOR value WHILE
print(value);
# WHILE # value MOD 6 /= 0 DO
SKIP
OD

ALGOL W[edit]

begin
integer i;
i := 0;
while
begin
i := i + 1;
write( i );
( i rem 6 ) not = 0
end
do begin end
end.

AmigaE[edit]

PROC main()
DEF i = 0
REPEAT
i := i + 1
WriteF('\d\n', i)
UNTIL Mod(i, 6) = 0
ENDPROC

AutoHotkey[edit]

While mod(A_Index, 6) ;comment:everything but 0 is considered true
output = %output%`n%A_Index%
MsgBox % output

AWK[edit]

BEGIN {
val = 0
do {
val++
print val
} while( val % 6 != 0)
}

Axe[edit]

While Axe does not have explicit do-while loops, they can be easily emulated using an infinite loop with a conditional terminator:

0→A
While 1
A++
Disp A▶Dec,i
End!If A^6

BASIC[edit]

Works with: QuickBasic version 4.5
a = 0
DO
a = a + 1
PRINT a
LOOP WHILE a MOD 6 <> 0

BBC BASIC[edit]

a = 0
REPEAT
a = a + 1
PRINT a
UNTIL a MOD 6 = 0

bc[edit]

i = 0
for (;;) {
++i /* increments then prints i */
if (i % 6 == 0) break
}
quit

Befunge[edit]

0>1+:.v
|%6: <
@

C[edit]

int val = 0;
do{
val++;
printf("%d\n",val);
}while(val % 6 != 0);

ChucK[edit]

 
0 => int value;
do
{
value++;
<<<value>>>;
}
while(value % 6 != 0);
 

C++[edit]

int val = 0;
do{
val++;
cout << val << endl;
}while(val % 6 != 0);

C#[edit]

int a = 0;
 
do
{
a += 1;
Console.WriteLine(a);
} while (a % 6 != 0);

Chapel[edit]

var val = 0;
do {
val += 1;
writeln(val);
} while val % 6 > 0;

Clipper[edit]

   Local n := 0
DO WHILE .T.
 ? ++n
IF n % 6 == 0
EXIT
ENDIF
ENDDO

COBOL[edit]

The COBOL equivalent of a do-while loop is PERFORM WITH TEST AFTER UNTIL some-condition.

       IDENTIFICATION DIVISION.
PROGRAM-ID. loop-do-while.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 i PIC 99 VALUE 0.
 
PROCEDURE DIVISION.
PERFORM WITH TEST AFTER UNTIL FUNCTION MOD(i, 6) = 0
ADD 1 TO i
DISPLAY i
END-PERFORM
 
GOBACK
.

Coco[edit]

Do-while loops are a JavaScript feature removed in CoffeeScript but re-added in Coco.

v = 0
do
console.log ++v
while v % 6

CoffeeScript[edit]

CoffeeScript doesn't have do {} while () loop, but it can be emulated using loop statement and break unless statement.

val = 0
loop
console.log ++val
break unless val % 6

ColdFusion[edit]

<cfscript>
value = 0;
do
{
value += 1;
writeOutput( value );
} while( value % 6 != 0 );
</cfscript>

Common Lisp[edit]

(let ((val 0))
(loop do
(incf val)
(print val)
while (/= 0 (mod val 6))))

loop can set up temporary values, and incf returns a value, so it's also possible to do

(loop with val = 0
do (print (incf val))
until (= 0 (mod val 6)))

Clojure[edit]

(loop [i 0]
(let [i* (inc i)]
(println i*)
(when-not (zero? (mod i* 6))
(recur i*))))

D[edit]

import std.stdio;
 
void main() {
int val;
do {
val++;
write(val, " ");
} while (val % 6 != 0);
}
Output:
1 2 3 4 5 6 

dc[edit]

Translation of: bc
0 si		[i = 0]sz
[2Q]sA [A = code to break loop]sz
[
li 1 + p [print it = i + 1]sz
d si [i = it, leave it on stack]sz
6 % 0 =A [call A if 0 == it % 6]sz
0 0 =B [continue loop]sz
]sB 0 0 =B

Delphi[edit]

program Loop;
 
{$APPTYPE CONSOLE}
 
var
I: Integer;
 
begin
I:= 0;
repeat
Inc(I);
Write(I:2);
until I mod 6 = 0;
Writeln;
Readln;
end.

DWScript[edit]

 
var i := 0;
 
repeat
Inc(i);
PrintLn(i);
until i mod 6 = 0;
 

E[edit]

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
})


Ela[edit]

open monad io
 
loop n | n % 6 == 0 = do return ()
| else = do
putStrLn (show n)
loop (n+1)
 
_ = loop 10 ::: IO

Elixir[edit]

defmodule Loops do
def do_while(n) do
n1 = n + 1
IO.puts n1
if rem(n1, 6) == 0, do: :ok,
else: do_while(n1)
end
end
 
Loops.do_while(0)

Emacs Lisp[edit]

The condition form for while can be a progn to evaluate arbitrary code before the loop condition. The body of a while can be empty.

(let ((val 0))
(while (progn
(setq val (1+ val))
(message "%d" val)
(/= 0 (mod val 6)))))

Erlang[edit]

 
do() ->
do(0).
 
do(0) ->
io:fwrite( "0 " ),
do( 1 );
do(N) when N rem 6 =:= 0 ->
io:format("~w~n", [N]);
do(N) ->
io:fwrite( "~p ", [N] ),
do(N+1).
 

ERRE[edit]

 
A=0
REPEAT
A=A+1
PRINT(A)
UNTIL A MOD 6=0  !UNTIL A-6*INT(A/6)=0 for C-64
 

Euphoria[edit]

Works with: Open Euphoria
 
include std/console.e
include std/math.e
 
atom x = 0
 
loop do
x += 1
?x
until(mod(x,6)) = 0
end loop
 
if getc(0) then end if
 

Factor[edit]

0 [ dup 6 mod 0 = not ] [ [ . ] [ 1 + ] bi ] do while drop

Fantom[edit]

There is no do-while statement in Fantom, so instead use an infinite while loop with a break statement:

 
class Main
{
public static Void main ()
{
i := 0
while (true)
{
i += 1
echo (i)
if (i % 6 == 0) break // end loop on condition
}
}
}
 

Forth[edit]

: do-until
0
begin 1+
dup .
dup 6 mod 0=
until
drop ;

Fortran[edit]

Works with: Fortran version 90 and later
INTEGER :: i = 0
DO
i = i + 1
WRITE(*, *) i
IF (MOD(i, 6) == 0) EXIT
END DO
Works with: Fortran version 77 and later
      PROGRAM DOWHILE
C Initialize modulus and value.
INTEGER MODLUS, IVALUE
PARAMETER (MODLUS = 6)
IVALUE = 0
 
C FORTRAN 77 has no do-while structure -- not semantically. It is not
C difficult to simulate it using GOTO, however:
10 CONTINUE
IVALUE = IVALUE + 1
WRITE (*,*) IVALUE
IF (.NOT. (MOD(IVALUE, MODLUS) .EQ. 0)) GOTO 10
 
STOP
END

Frink[edit]

n = 0
do
{
n = n + 1
println[n]
} while n mod 6 != 0

GAP[edit]

n := 0;
repeat
n := n + 1;
Print(n, "\n");
until RemInt(n, 6) = 0;

Go[edit]

No do-while in Go.

package main
 
import "fmt"
 
func main() {
for value := 0;; {
value++
fmt.Println(value)
if value % 6 == 0 {
break
}
}
}
Output:
1
2
3
4
5
6

GML[edit]

i = 0
do
{
i += 1
show_message(string(i))
}
until (i mod 6 = 0)

Groovy[edit]

Groovy does not have a bottom-checking loop construct! So use an "infinite" while loop with a conditional break as the last statement

def i = 0
while (true) {
i++
println i
if ( i % 6 == 0) break
}
Output:
1
2
3
4
5
6

Harbour[edit]

LOCAL n := 0
 
DO WHILE .T.
? ++n
IF n % 6 == 0
EXIT
ENDIF
ENDDO

Haskell[edit]

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

Icon and Unicon[edit]

Icon and Unicon do not have a do-while looping control with end of loop checking. There are four looping controls 'every', 'repeat', 'until', and 'while' (see Introduction to Icon and Unicon/Looping Controls for more information.)

procedure main()
 
i := 0
repeat {
write(i +:= 1)
if i % 6 = 0 then break
}
end

J[edit]

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.

Java[edit]

int val = 0;
do{
val++;
System.out.println(val);
}while(val % 6 != 0);

JavaScript[edit]

Javascript: Imperative[edit]

var val = 0;
do {
print(++val);
} while (val % 6);

Javascript: Functional[edit]

In a functional idiom of JavaScript we cannot use a Do While statement, as it returns no value and is not a composable expression. We can, however achieve the same effect with a composable doWhile function, which takes three arguments, and returns the output series as a value.

  1. An initial value,
  2. a Do function which transforms that value repetitively, corresponding to the body of the loop,
  3. and a conditional While function.
function doWhile(varValue, fnBody, fnTest) {
'use strict';
var d = fnBody(varValue); // a transformed value
 
return fnTest(d) ? [d].concat(
doWhile(d, fnBody, fnTest)
) : [d];
}
 
console.log(
doWhile(0, // initial value
function (x) { // Do body, returning transformed value
return x + 1;
},
function (x) { // While condition
return x % 6;
}
).join('\n')
);

Output:

1
2
3
4
5
6

Alternatively, if we assume instead that the unstated problem was not to produce repetitive computation, but to derive the membership of a set we could interpret the task as a request for a JavaScript implementation of the takeWhile function – a familiar staple of functional list processing.

So, for example, something like:

function range(m, n) {
'use strict';
return Array.apply(null, Array(n - m + 1)).map(
function (x, i) {
return m + i;
}
);
}
 
function takeWhile(lst, fnTest) {
'use strict';
var varHead = lst.length ? lst[0] : null;
 
return varHead ? (
fnTest(varHead) ? [varHead].concat(
takeWhile(lst.slice(1), fnTest)
) : []
) : []
}
 
console.log(
takeWhile(
range(1, 100),
function (x) {
return x % 6;
}
).join('\n')
);

Output:

1
2
3
4
5

jq[edit]

Works with: jq version 1.4

In jq 1.4, the "recurse" built-in always emits the input value, and so to accomplish the task specified here, we shall define a control structure: "do_while(action; condition)" as follows:

# Perform the action, then check the condition, etc
def do_while( action; condition ):
def w: action | if (condition | not) then empty else ., w end;
w;

The task:

0 | do_while( .+1; . % 6 != 0 )
Output:
1
2
3
4
5

Julia[edit]

Julia has no do-while construct. Here is one of several ways to implement do-while behavior.

 
i = 0
while true
println(i)
i += 1
if i%6 == 0
break
end
end
 
Output:
0
1
2
3
4
5

LabVIEW[edit]

This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.
LabVIEW Loops Do-while.png

Lasso[edit]

local(x = 0)
while(#x % 6 > 0 || #x == 0) => {^
++#x
'\r' // for formatting
^}

Liberty BASIC[edit]

 
a = 0
do
a =a +1
print a
loop until ( a mod 6) = 0
 

Lisaac[edit]

+ val : INTEGER;
{
val := val + 1;
val.print;
'\n'.print;
val % 6 != 0
}.while_do { };

LiveCode[edit]

repeat while n mod 6 is not 0 or n is 0
add 1 to n
put n
end repeat

Lua[edit]

Lua doesn't have a do .. while construct.

 
i=0
repeat
i=i+1
print(i)
until i%6 == 0
 

[edit]

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

Maple[edit]

val := 0:
do
val := 1 + val;
print( val );
if irem( val, 6 ) = 0 then
break
end if;
end do:

Mathematica[edit]

value = 5;
NestWhile[
# + 1 &
,
value
, (Print[#]; Mod[#, 6] != 0) &
];

gives back:

5
6

If the starting value is 6, only 6 is returned.

MATLAB / Octave[edit]

   a=0; 
while (1)
a = a+1;
disp(a);
if (~mod(a,6)) break; end;
end;

Maxima[edit]

block([n: 0], do (ldisp(n: n + 1), if mod(n, 6) = 0 then return('done)))$

MAXScript[edit]

a = 0
do
(
print a
a += 1
)
while mod a 6 != 0

Metafont[edit]

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

МК-61/52[edit]

0	П4	КИП4	ИП4	6	/	{x}	x=0	02	С/П

Modula-2[edit]

MODULE DoWhile;
IMPORT InOut;
 
VAR
i: INTEGER;
 
BEGIN
i := 0
REPEAT
InOut.WriteInt(i, 1);
InOut.WriteLn;
INC(i)
UNTIL i MOD 6 = 0;
END DoWhile.

Modula-3[edit]

This is very similar to the Modula-2 code above.

REPEAT
i := i + 1;
IO.Put(Fmt.Int(i));
UNTIL i MOD 6 = 0;

Monicelli[edit]

The do-while loop is the only kind of loop available in Monicelli

 
stuzzica
... # loop body
e brematura anche, se <expr> # exit if <expr> is false
 


Nemerle[edit]

mutable x = 0;
do
{
x++;
WriteLine($"$x");
} while (x % 6 != 0)

NetRexx[edit]

In NetRexx the do–while construct is implemented via the until expru conditional clause of the loop instruction. The expression expru in the until expru clause is evaluated at the end of the loop, guaranteeing that the loop will be executed at least once.

/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
say
say 'Loops/Do-while'
 
i_ = 0
loop until i_ // 6 = 0
i_ = i_ + 1
say i_
end
 

NewLISP[edit]

(let ((i 0))
(do-until (= 0 (% i 6))
(println (++ i))))

Nim[edit]

Nim does not have a do-while loop, but it's easy to write your own:

template doWhile(a: expr, b: stmt): stmt =
b
while a:
b
 
var val = 1
doWhile val mod 6 != 0:
val += 1
echo val

Oberon-2[edit]

Works with oo2c Version 2

 
MODULE LoopDoWhile;
IMPORT
Out;
 
PROCEDURE Do();
VAR
i: INTEGER;
BEGIN
i := 0;
REPEAT
Out.LongInt(i,0);Out.Ln;
INC(i)
UNTIL (i MOD 6 = 0);
END Do;
 
BEGIN
Do
END LoopDoWhile.
 

OCaml[edit]

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 -> ()

Objeck[edit]

 
i := 0;
do {
i += 1;
i->PrintLine();
}
while (i % 6 <> 0);
 

Octave[edit]

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 )

Oforth[edit]

0 doWhile: [ 1 + dup println dup 6 rem 0 <> ] drop

OpenEdge/Progress[edit]

DEFINE VARIABLE ii AS INTEGER.
 
DO WHILE ii MODULO 6 <> 0 OR ii = 0:
ii = ii + 1.
MESSAGE ii VIEW-AS ALERT-BOX.
END.

Oz[edit]

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

PARI/GP[edit]

The generic Pari loops (while, until) test at the beginning, so just use an infinite loop with a break.

x = 0;
while(1,
print(x++);
if(x % 6 == 0, break)
)

If the loop body is something simple then it might be worked into the loop condition. This is obscure but compact.

x = 0;
while (print(x++) || x % 6, )

The condition in while and until is an expression, not a sequence, so ; for multiple statements cannot be used there.

Pascal[edit]

program countto6(output);
 
var
i: integer;
 
begin
i := 0;
repeat
i := i + 1;
writeln(i)
until i mod 6 = 0
end.

Perl[edit]

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);

Perl 6[edit]

Works with: Rakudo Star version 2010.08
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;

(Here we've used %%, the "divisible-by" operator.)

You can also put the condition before the block, without changing the order of evaluation.

my $val = 0;
repeat while $val % 6 {
say ++$val;
}

Phix[edit]

integer x = 0
while 1 do
x += 1
 ?x
if mod(x,6)=0 then exit end if
end while

PHL[edit]

var i = 0;
do {
i = i::inc;
printf("%i\n", i);
} while (i%6 != 0);

PHP[edit]

$val = 0;
do {
$val++;
print "$val\n";
} while ($val % 6 != 0);

PicoLisp[edit]

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))))

Pike[edit]

int main(){
int value = 0;
do {
value++;
write(value + "\n");
} while (value % 6);
}

PL/I[edit]

 
dcl value fixed bin (31) init (0);
do forever;
value = value + 1;
 
if mod(value, 6) = 0 then
leave;
 
put list (value);
end;
 

or shorter:

 
dcl value fixed bin(31) init(0);
do Until(value=6);
value+=1;
put Skip list(value);
end;
Output:
             1
             2
             3
             4
             5
             6     

Pop11[edit]

lvars val = 0;
while true do
val + 1 -> val;
printf(val, '%p\n');
quitif(val rem 6 = 0);
endwhile;

PowerShell[edit]

$n = 0
do {
$n++
$n
} while ($n % 6 -ne 0)

Prolog[edit]

 
% initial condition
do(0):- write(0),nl,do(1).
 
% control condition
do(V):- 0 is mod(V,6), !, fail.
 
% loop
do(V) :-
write(V),nl,
Y is V + 1,
do(Y).
 
wloop :-
do(0).
 
 

PureBasic[edit]

Works with: PureBasic version 4.41
x=0
Repeat
x+1
Debug x
Until x%6=0

Python[edit]

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

R[edit]

i <- 0
repeat
{
i <- i + 1
print(i)
if(i %% 6 == 0) break
}

Racket[edit]

Idiomatic Racket code is functional:

 
#lang racket
(let loop ([n 0])
(let ([n (add1 n)])
(displayln n)
(unless (zero? (modulo n 6)) (loop n))))
 

But an imperative version is easy to do too:

 
#lang racket
(define n 0)
(let loop ()
(set! n (add1 n))
(displayln n)
(unless (zero? (modulo n 6)) (loop)))
 

REBOL[edit]

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

REXX[edit]

In the DO UNTIL construct, the expression is evaluated at the end of the DO loop,
even though it is written at the beginning.
This insures that the DO UNTIL loop will execute at least once (as coded below).

In contrast, a DO WHILE construct, the expression would be evaluated at the beginning of the DO loop, and
may cause the DO WHILE loop to not execute at all.
This necessitates the use of DO UNTIL instead of DO WHILE.

version 1[edit]

/*REXX program demonstrates a     DO  UNTIL     construction.           */
v=0
do until v//6==0 /*REXX // is the ÷ remainder.*/
v=v+1
say v
end
/*stick a fork in it, we're done.*/
Output:
1
2
3
4
5
6

version 2[edit]

/*REXX program demonstrates a     DO  UNTIL     construction.           */
 
do v=1 until v//6==0 /*REXX // is the ÷ remainder.*/
say v
end
/*stick a fork in it, we're done.*/

output is the same as the 1st version.

Ruby[edit]

The while statement modifier normally checks the condition before entering the loop. But if the while statement modifier is on a begin ... end statement, then it loops at least once. Same with the until statement modifier.

while until
val = 0
begin
val += 1
puts val
end while val % 6 != 0
val = 0
begin
val += 1
puts val
end until val % 6 == 0

During November 2005, Yukihiro Matsumoto, the creator of Ruby, regretted this loop feature and suggested using Kernel#loop.

break unless break if
val = 0
loop do
val += 1
puts val
break unless val %6 != 0
end
val = 0
loop do
val += 1
puts val
break if val %6 == 0
end

All four of these examples print the numbers 1, 2, 3, 4, 5, 6.

Rust[edit]

Rust does not have a do...while loop. Instead, the keyword loop is used with a termination condition.

let mut x = 0;
 
loop {
x += 1;
println!("{}", x);
 
if x % 6 == 0 { break; }
}

Salmon[edit]

variable x := 0;
do
{
++x;
x!
}
while (x % 6 != 0);

SAS[edit]

/* using DO UNTIL so that the loop executes at least once */
data _null_;
n=0;
do until(mod(n,6)=0);
n=n+1;
put n;
end;
run;

Sather[edit]

Translation of: C
class MAIN is
main is
val ::= 0;
loop
val := val + 1;
#OUT + val + "\n";
while!(val % 6 /= 0)
end;
end;
end;

Scala[edit]

Library: Scala

Imperative[edit]

  {
var (x, l) = (0, List[Int]())
do {
x += 1
l :+= x // A new copy of this list with List(x) appended.
} while (x % 6 != 0)
l
}.foreach(println(_))
 

Tail recursive[edit]

	def loop(iter: Int, cond: (Int) => Boolean, accu: List[Int]): List[Int] = {
val succ = iter + 1
val temp = accu :+ succ
if (cond(succ)) loop(succ, cond, temp) else temp
}
println(loop(0, (_ % 6 != 0), Nil))

Stream[edit]

  def loop(i: Int, cond: (Int) => Boolean): Stream[Int] = {
val succ = i + 1;
succ #:: (if (cond(succ)) loop(succ, cond) else Stream.empty)
}
loop(0, (_ % 6 != 0)).foreach(println(_))

Scheme[edit]

(let loop ((i 1))
(display i)
(if (positive? (modulo i 6))
(loop (+ i 1))))

Scilab[edit]

Works with: Scilab version 5.5.1
v=0
while %T
v=v+1
printf("%2d ",v)
if modulo(v,6)==0 then break; end
end
printf("\n")
Output:
 1  2  3  4  5  6 

Seed7[edit]

$ 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;

Sidef[edit]

var value = 0;
do {
say ++value;
} while (value % 6);

Slate[edit]

[| val |
val: 0.
[val: val + 1.
print: val.
val \\ 6 ~= 0] whileTrue
] do.

Smalltalk[edit]

Works with: Smalltalk/X
|val|
val := 0.
[
val := val + 1.
val displayNl.
] doWhile: [ (val rem: 6) ~= 0 ]
|val|
val := 0.
[
val := val + 1.
val displayNl.
] doUntil: [ (val rem: 6) == 0 ]
Works with: GNU 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: [ ]

Sparkling[edit]

var i = 0;
do {
print(++i);
} while (i % 6 != 0);

Suneido[edit]

val = 0
do
{
Print(++val)
} while (val % 6 isnt 0)
Output:
1
2
3
4
5
6

Swift[edit]

Works with: Swift version 2.x+
var val = 0
repeat {
val++
print(val)
} while val % 6 != 0
Works with: Swift version 1.x
var val = 0
do {
val++
println(val)
} while val % 6 != 0

Tcl[edit]

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}
Library: Tcllib (Package: control)
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
}

TUSCRIPT[edit]

 
$$ MODE TUSCRIPT
var=0
LOOP
var=var+1, rest=var%6
PRINT var
IF (rest==0) EXIT
ENDLOOP
 
Output:
1
2
3
4
5
6

UNIX Shell[edit]

Works with: bash
Works with: pdksh
Works with: zsh
val=0
while true; do
echo $((++val))
[ $((val%6)) -eq 0 ] && break
done
Works with: Bourne Shell
val=0
while true; do
val=`expr $val + 1`
echo $val
expr $val % 6 = 0 >/dev/null && break
done

Vedit macro language[edit]

#1 = 0
do {
#1++
Num_Type(#1)
} while (#1 % 6 != 0);

Visual Basic .NET[edit]

Dim i = 0
Do
i += 1
Console.WriteLine(i)
Loop Until i Mod 6 = 0

XPL0[edit]

code CrLf=9, IntOut=11;
int V;
[V:= 0;
repeat V:= V+1;
IntOut(0, V); CrLf(0);
until rem(V/6) = 0;
]

Yorick[edit]

val = 0;
do {
val++;
write, val;
} while(val % 6 != 0);

zkl[edit]

Translation of: Yorick
val := 0;
do {
val+=1;
val.print(" ");
} while(val % 6 != 0);
Output:
1 2 3 4 5 6