Loops/Do-while: Difference between revisions
Content deleted Content added
→{{header|Minimal BASIC}}: Added |
m Moved FutureBasic entry out of the BASIC group |
||
(48 intermediate revisions by 26 users not shown) | |||
Line 34:
{{trans|Python}}
<
L
val++
print(val)
I val % 6 == 0
L.break</
=={{header|360 Assembly}}==
;Basic
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
Line 70:
WTOLEN DC H'2' fixed WTO length of two
WTOTXT DC CL2' '
END DOWHILE </
;Structured Macros
Although specified at the beginning (DO UNTIL), the test is done at the end of the loop (ENDDO).
Structured macros (DO ENDDO) weren't in the 1963 standard of Assembler 360, but there are part of it since since 1998.
<
DOWHILE CSECT
USING DOWHILE,12 set base register
Line 94:
DC H'0' must be zero
WTOTXT DS C one char
END DOWHILE</
=={{header|6502 Assembly}}==
Code is called as a subroutine (i.e. JSR DoWhileSub). Specific OS/hardware routines for printing are left unimplemented.
<
TYA
PHA ;push accumulator and Y register onto stack
Line 115:
TAY
PLA ;restore Y register and accumulator from stack
RTS ;return from subroutine</
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopdowhile64.s */
Line 173:
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<
byte A
Line 186:
Until A Mod 6=0
Od
Return</
=={{header|ActionScript}}==
<
do
{
trace(++val);
} while (val % 6);</
=={{header|Ada}}==
<
Value := Value + 1;
Put (Value);
exit when Value mod 6 = 0;
end loop;</
Here is an alternative version:
<
Put (Value);
exit when Value mod 6 = 0;
end loop;</
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<
local i := 0;
do
Line 215:
print( i )
as ( i % 6 ) <> 0
epocs</
=={{header|Aime}}==
<
a = 0;
Line 225:
o_integer(a);
o_byte('\n');
} while (a % 6 != 0);</
=={{header|ALGOL 60}}==
{{works with|ALGOL 60|OS/360}}
No structured control instructions in Algol 60 to perform this task. Use of 2 harmful GOTOs. I agree Edsger Dijkstra communication "Go To Statement Considered Harmful", ACM 1968.
<
'INTEGER' I;
I:=0;
Line 239:
'GOTO' LOOP;
ENDLOOP:
'END'</
{{out}}
<pre>
Line 245:
</pre>
===
{{works with|A60}}
While a judicious "goto" may
<
begin
Line 255:
i := 0;
another := true;
for i := i
begin
i := i + 1;
outinteger(1,i);
comment -
if i = (i div 6) * 6 then another := false;
end;
end
</syntaxhighlight>
{{out}}
<pre>
Line 270 ⟶ 271:
=={{header|ALGOL 68}}==
<
print(value);
# WHILE # value MOD 6 /= 0 DO
SKIP
OD</
=={{header|ALGOL W}}==
<
integer i;
i := 0;
Line 287 ⟶ 288:
end
do begin end
end.</
=={{header|AmigaE}}==
<
DEF i = 0
REPEAT
Line 296 ⟶ 297:
WriteF('\d\n', i)
UNTIL Mod(i, 6) = 0
ENDPROC</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 442 ⟶ 443:
pop {r4, lr}
bx lr
</syntaxhighlight>
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">
on printConsole(x)
return x as string
Line 456 ⟶ 457:
printConsole(table)
end repeat
</syntaxhighlight>
{{out}}
<pre>
Line 470 ⟶ 471:
=={{header|Arturo}}==
<
until [
value: value + 1
print value
] [ 0 = value%6 ]</
{{out}}
Line 484 ⟶ 485:
5
6</pre>
=={{header|Asymptote}}==
Asymptote's control structures are similar to those in C, C++, or Java
<syntaxhighlight lang="asymptote">int i = 0;
do {
++i;
write(" ", i, suffix=none);
} while (i % 6 != 0);</syntaxhighlight>
=={{header|AutoHotkey}}==
<
output = %output%`n%A_Index%
MsgBox % output</
=={{header|AWK}}==
<
val = 0
do {
Line 497 ⟶ 506:
print val
} while( val % 6 != 0)
}</
=={{header|Axe}}==
While Axe does not have explicit do-while loops, they can be easily emulated using an infinite loop with a conditional terminator:
<
While 1
A++
Disp A▶Dec,i
End!If A^6</
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Commodore BASIC}}
<syntaxhighlight lang="gwbasic"> 0 REMADE FOR DO WHILE
1 DEF FN MOD6(N) = N - INT (N / 6) * 6
2 LET V4LUE = 0
3 FOR DO = 0 TO 1
10 LET V4LUE = V4LUE + 1
20 PRINT V4LUE" ";
30 WHILE = FN MOD6(V4LUE) < > 0:DO = NOT WHILE: NEXT</syntaxhighlight>
==={{header|ASIC}}===
ASIC does not have a <code>do .. while</code> construct. Equivalent using <code>WHILE</code>:
<
REM Loops/Do-while
Line 532 ⟶ 544:
END
</syntaxhighlight>
{{out}}
<pre>
Line 543 ⟶ 555:
</pre>
Equivalent using conditional jump:
<
REM Loops/Do-while
Line 554 ⟶ 566:
END
</syntaxhighlight>
{{out}}
As above
==={{header|BaCon}}===
<
a=0
REPEAT
Line 565 ⟶ 577:
PRINT a
UNTIL MOD(a,6) == 0
</syntaxhighlight>
==={{header|BASIC256}}===
<
do
Line 575 ⟶ 587:
until i mod 6 = 0
print
end</
==={{header|BBC BASIC}}===
<
REPEAT
a = a + 1
PRINT a
UNTIL a MOD 6 = 0</
==={{header|Chipmunk Basic}}===
In ''Chipmunk Basic Man Page'', the words <code>do</code>, <code>loop</code>, and <code>until</code> are mentioned as reserved, but the <code>do .. loop until</code> statement is not described, probably because of uncorrected abnormal behavior of the interpreter. In case of such behavior you may use equivalents (e.g. with <code>while .. wend</code>).
<syntaxhighlight lang="basic">
100 rem Loops/Do-while
110 i = 0
120 do
130 i = i+1
140 print i
150 loop until i mod 6 = 0
160 end
</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="basic">50 rem does not have do-while simultate using for-next
100 x=0
120 for b=-1 to 0 step 0
130 x=x+1
140 print x
150 b=x/6<>int(x/6)
160 next x
</syntaxhighlight>
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05. 0 Win64
Dim i As Integer = 0
Do
i += 1
Print i; " ";
Loop While i Mod 6 <> 0
Print
Sleep</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
</pre>
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=57e91eab60baf7e39df9b6d16a0deddd Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siCount As Short
Repeat
Inc siCount
Print siCount;;
Until siCount Mod 6 = 0
End</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
</pre>
==={{header|GW-BASIC}}===
GW-BASIC does not have a <code>do .. while</code> construct.
Equivalent using <code>WHILE</code>:
{{works with|BASICA}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="gwbasic">
10 LET I% = 0
20 ' first iteration - before the WHILE
30 LET I% = I% + 1
40 PRINT I%
50 WHILE I% MOD 6 <> 0
60 LET I% = I% + 1
70 PRINT I%
80 WEND
</syntaxhighlight>
Equivalent using <code>GOTO</code>:
{{works with|BASICA}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="gwbasic">
10 LET I% = 0
20 LET I% = I% + 1
30 PRINT I%
40 IF I% MOD 6 <> 0 THEN GOTO 20
</syntaxhighlight>
==={{header|IS-BASIC}}===
<
110 DO
120 LET I=I+1
130 PRINT I
140 LOOP UNTIL MOD(I,6)=0</
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
a = 0
do
a = a + 1
print a
loop until (a mod 6) = 0
</syntaxhighlight>
See also [[#QBasic|QBasic]].
==={{header|Microsoft Small Basic}}===
Microsoft Small Basic does not have a <code>do .. while</code> construct.
Equivalent using <code>While</code>:
<syntaxhighlight lang="microsoftsmallbasic">
i = 0
' first iteration - before the While
i = i + 1
TextWindow.WriteLine(i)
While Math.Remainder(i, 6) <> 0
i = i + 1
TextWindow.WriteLine(i)
EndWhile
</syntaxhighlight>
Equivalent using <code>Goto</code>:
<syntaxhighlight lang="microsoftsmallbasic">
i = 0
loopStart:
i = i + 1
TextWindow.WriteLine(i)
If Math.Remainder(i, 6) <> 0 Then
Goto loopStart
EndIf
</syntaxhighlight>
==={{header|Minimal BASIC}}===
Minimal BASIC does not have a <code>do .. while</code> construct. Equivalent using conditional jump:
<
10 REM Loops/Do-while
20 LET I=0
Line 600 ⟶ 734:
50 IF INT(I/6)*6 <> I THEN 30
60 END
</syntaxhighlight>
==={{header|MSX Basic}}===
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes.
==={{header|NS-HUBASIC}}===
<syntaxhighlight lang="ns-hubasic">10 PRINT "NO,"A" ISN'T A MULTIPLE OF 6."
20 A=A+1
30 IF A-(A/6)*6<>0 THEN GOTO 10
40 PRINT "YES, 6 IS A MULTIPLE OF 6."</syntaxhighlight>
==={{header|PureBasic}}===
{{works with|PureBasic|4.41}}
<syntaxhighlight lang="purebasic">x=0
Repeat
x+1
Debug x
Until x%6=0</syntaxhighlight>
==={{header|QB64}}===
''CBTJD'': 2020/03/14
<syntaxhighlight lang="qbasic">DO
PRINT n
n = n + 1
LOOP WHILE n MOD 6 <> 0</syntaxhighlight>
<syntaxhighlight lang="qb64">
'Another demo of DO loops
Dim As Integer Counter
Print "First loop DO..LOOP UNTIL"
Counter = 0
Do
Print Counter
Counter = Counter + 1
Loop Until Counter Mod 6 = 0
Print "Counter Mod 6 = "; Counter Mod 6
Print "First loop DO WHILE..LOOP"
Counter = 1
Do While Counter Mod 6 <> 0
Print Counter
Counter = Counter + 1
Loop
Print "Counter Mod 6 = "; Counter Mod 6
End
</syntaxhighlight>
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Just BASIC}}
<syntaxhighlight lang="qbasic">a = 0
DO
a = a + 1
PRINT a;
LOOP WHILE a MOD 6 <> 0</syntaxhighlight>
==={{header|Quite BASIC}}===
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes.
==={{header|Run BASIC}}===
Run Basic does not have a <code>do .. while</code> construct. Equivalent using conditional jump:
<syntaxhighlight lang="lb">i = 0
[start]
i = i +1
print i; " ";
if i mod 6 <> 0 then [start]</syntaxhighlight>
==={{header|Sinclair ZX81 BASIC}}===
<
20 LET X=X+1
30 PRINT X
40 IF X/6<>INT (X/6) THEN GOTO 20</
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
repeat
v++
print v
until(v mod 6 == 0)
</syntaxhighlight>
==={{Header|Tiny BASIC}}===
Tiny Basic does not have a <code>do .. while</code> construct. Equivalent using conditional jump:
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">10 REM Loops/Do-while
20 LET I = 0
30 LET I = I + 1
40 PRINT I
50 IF (I / 6) * 6 <> I THEN GOTO 30
60 END</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
==={{header|True BASIC}}===
<
DO
Line 617 ⟶ 842:
LOOP WHILE REMAINDER(i, 6) <> 0
PRINT
END</syntaxhighlight>
==={{header|VBA}}===
<syntaxhighlight lang="vb">Public Sub LoopDoWhile()
Dim value As Integer
value = 0
Do
value = value + 1
Debug.Print value;
Loop While value Mod 6 <> 0
End Sub</syntaxhighlight>{{out}}<pre> 1 2 3 4 5 6 </pre>
==={{header|Visual Basic .NET}}===
<syntaxhighlight lang="vbnet">Dim i = 0
Do
i += 1
Console.WriteLine(i)
Loop Until i Mod 6 = 0</syntaxhighlight>
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
PROGRAM "dowhile"
DECLARE FUNCTION Entry()
FUNCTION Entry()
val% = 0
DO
INC val%
PRINT val%
LOOP WHILE val% MOD 6 <> 0 ' or LOOP UNTIL val% MOD 6 = 0
END FUNCTION
END PROGRAM
</syntaxhighlight>
==={{header|Yabasic}}===
<
repeat
Line 627 ⟶ 885:
print i, " ";
until mod(i, 6) = 0
print</
=={{header|bc}}==
<
for (;;) {
++i /* increments then prints i */
if (i % 6 == 0) break
}
quit</
=={{header|Befunge}}==
<
|%6: <
@</
=={{header|C}}==
<
do{
val++;
printf("%d\n",val);
}while(val % 6 != 0);</
=={{header|C sharp|C#}}==
<
do
Line 657 ⟶ 915:
a += 1;
Console.WriteLine(a);
} while (a % 6 != 0);</
=={{header|C++}}==
<
do{
val++;
std::cout << val << std::endl;
}while(val % 6 != 0);</
=={{header|C3}}==
In this example we use default zero initialization of locals in C3.
<syntaxhighlight lang="c3">int val;
do
{
io::printn(++val);
}
while (val % 6 != 0);</syntaxhighlight>
=={{header|Chapel}}==
<
do {
val += 1;
writeln(val);
} while val % 6 > 0;</
=={{header|ChucK}}==
<syntaxhighlight lang="text">
0 => int value;
do
Line 682 ⟶ 949:
}
while(value % 6 != 0);
</syntaxhighlight>
=={{header|Clipper}}==
<
DO WHILE .T.
? ++n
Line 691 ⟶ 958:
EXIT
ENDIF
ENDDO</
=={{header|Clojure}}==
<
(let [i* (inc i)]
(println i*)
(when-not (zero? (mod i* 6))
(recur i*))))</
=={{header|COBOL}}==
The COBOL equivalent of a do-while loop is <code>PERFORM WITH TEST AFTER UNTIL some-condition</code>.
<
PROGRAM-ID. loop-do-while.
Line 716 ⟶ 983:
GOBACK
.</
=={{header|Coco}}==
Do-while loops are a JavaScript feature removed in CoffeeScript but re-added in Coco.
<
do
console.log ++v
while v % 6</
=={{header|CoffeeScript}}==
CoffeeScript doesn't have <code>do {} while ()</code> loop, but it can be emulated using <code>loop</code> statement and <code>break unless</code> statement.
<
loop
console.log ++val
break unless val % 6</
=={{header|ColdFusion}}==
<
value = 0;
do
Line 741 ⟶ 1,008:
writeOutput( value );
} while( value % 6 != 0 );
</cfscript></
=={{header|Common Lisp}}==
<
(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
<
do (print (incf val))
until (= 0 (mod val 6)))</
=== Using DO ===
<
(do* ((a 0) ; Initialize to 0
(b (incf a) (incf b))) ; Set first increment and increment on every loop
((zerop (mod b 6)) (print b)) ; Break condition and print last value `6' (right?)
(print b)) ; On every loop print value
</syntaxhighlight>
{{out}}
Line 775 ⟶ 1,042:
=={{header|D}}==
<
void main() {
Line 783 ⟶ 1,050:
write(val, " ");
} while (val % 6 != 0);
}</
{{out}}
<pre>1 2 3 4 5 6 </pre>
=={{header|Dart}}==
<syntaxhighlight lang="dart">void main() {
int val = 0;
do {
val++;
print(val);
} while (val % 6 != 0);
}
</syntaxhighlight>
=={{header|dc}}==
{{trans|bc}}
<
[2Q]sA [A = code to break loop]sz
[
Line 796 ⟶ 1,073:
6 % 0 =A [call A if 0 == it % 6]sz
0 0 =B [continue loop]sz
]sB 0 0 =B</
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 814 ⟶ 1,091:
Writeln;
Readln;
end.</
=={{header|Draco}}==
<
byte i;
i := 0;
Line 825 ⟶ 1,102:
i % 6 ~= 0
do od
corp</
{{out}}
<pre> 1 2 3 4 5 6</pre>
Line 831 ⟶ 1,108:
=={{header|Dragon}}==
<
do{
val++
showln val
}while(val % 6 != 0)</
=={{header|DUP}}==
Line 842 ⟶ 1,119:
A do-while loop is technically nothing more than executing the block once before running an ordinary while loop, so we simply define an operator or function that contains the block (comments in curly braces):
<
[1+$.' ,]a: {function definition}</
and put the defined block in front of the while loop, and inside the while loop itself:
Line 849 ⟶ 1,126:
If the block was defined as an operator, the whole program would look like this (comments in curly braces):
<
0 A[$6/%][A]# {PUSH 0, execute operator A, [DUP, PUSH 6, MOD/DIV, POP][execute operator A]#}</
And if the block is defined as a named function:
<
0 a;![$6/%][a;!]#</
Result:
<syntaxhighlight lang
=={{header|DWScript}}==
<syntaxhighlight lang="delphi">
var i := 0;
Line 869 ⟶ 1,146:
PrintLn(i);
until i mod 6 = 0;
</syntaxhighlight>
'''Bold text'''
=={{header|Dyalect}}==
<
do
Line 880 ⟶ 1,157:
x += 1
print(x)
} while x % 6 != 0</
=={{header|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.
<
__loop(fn {
x += 1
println(x)
x % 6 != 0 # this is the return value of the function
})</
<!-- XXX we should have an example of lambda-args sugar here -->
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
value = 0
repeat
value += 1
print value
until value mod 6 = 0
.
</syntaxhighlight>
=={{header|Ela}}==
<
loop n | n % 6 == 0 = do return ()
Line 902 ⟶ 1,189:
loop (n+1)
_ = loop 10 ::: IO</
=={{header|Elixir}}==
<
def do_while(n) do
n1 = n + 1
Line 914 ⟶ 1,201:
end
Loops.do_while(0)</
=={{header|Emacs Lisp}}==
The condition form for <code>while</code> can be a <code>progn</code> to evaluate arbitrary code before the loop condition. The body of a <code>while</code> can be empty.
<
(while (progn
(setq val (1+ val))
(message "%d" val)
(/= 0 (mod val 6)))))</
Alternatively, the loop can be rewritten to check for the exit condition and fulfill it at the end of the loop body.
<
(while (not done)
(setq val (1+ val))
(message "%d" val)
(setq done (zerop (mod val 6)))))</
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
do() ->
do(0).
Line 947 ⟶ 1,234:
io:fwrite( "~p ", [N] ),
do(N+1).
</syntaxhighlight>
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
A=0
REPEAT
Line 956 ⟶ 1,243:
PRINT(A)
UNTIL A MOD 6=0 !UNTIL A-6*INT(A/6)=0 for C-64
</syntaxhighlight>
=={{header|Euphoria}}==
{{works with|Open Euphoria}}
<
include std/console.e
include std/math.e
Line 973 ⟶ 1,260:
if getc(0) then end if
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
If you must have a loop then this is acceptable F#
<
let rec loop n =
printfn "%d " n
if (n+1)%6 > 0 then loop (n+1)
loop 0
</syntaxhighlight>
But I prefer this way:
<
Seq.initInfinite id |> Seq.takeWhile(fun n->n=0 || n%6>0) |> Seq.iter (fun n-> printfn "%d" n)
</syntaxhighlight>
Either produces:
Line 1,002 ⟶ 1,289:
Many of the solutions to this task show no output in spite of it being required in the task dexcription, so who knows what they do? Of some that have output they think it should be 1 to 6, who can tell from the task description? The following produces 1..6.
<
// Loops/Do-while. Nigel Galloway: February 14th., 2022
Seq.unfold(fun n->match n with Some n->let n=n+1 in Some(n,if n%6=0 then None else Some(n)) |_->None)(Some 0)|>Seq.iter(printfn "%d")
</syntaxhighlight>
{{out}}
<pre>
Line 1,018 ⟶ 1,305:
=={{header|Factor}}==
<
=={{header|Fantom}}==
Line 1,024 ⟶ 1,311:
There is no do-while statement in Fantom, so instead use an infinite while loop with a break statement:
<
class Main
{
Line 1,038 ⟶ 1,325:
}
}
</syntaxhighlight>
=={{header|Forth}}==
<
0
begin 1+
Line 1,047 ⟶ 1,334:
dup 6 mod 0=
until
drop ;</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
DO
i = i + 1
WRITE(*, *) i
IF (MOD(i, 6) == 0) EXIT
END DO</
{{works with|Fortran|77 and later}}
<
C Initialize modulus and value.
INTEGER MODLUS, IVALUE
Line 1,073 ⟶ 1,360:
STOP
END</
{{works with|Fortran|IV and later}}
<
10 CONTINUE
IVALUE=IVALUE+1
Line 1,082 ⟶ 1,369:
301 FORMAT(I5)
IF(MOD(IVALUE,6).NE.0) GOTO 10
END</
{{works with|Fortran|I and later}}
<
10 IVALUE=IVALUE+1
WRITE 301,IVALUE
Line 1,091 ⟶ 1,378:
IF(IVALUE-IVALUE/6*6) 10,20,10
20 STOP
END</
=={{header|Fortress}}==
Line 1,097 ⟶ 1,384:
Due to the way that Fortress works, you have to use a label to escape a loop upon a specified condition being met. There is no traditional <code>break</code> equivalent.
<
component loops_do_while
export Executable
Line 1,113 ⟶ 1,400:
end loop
end
</syntaxhighlight>
{{out}}
Line 1,123 ⟶ 1,410:
5
6
</pre>
=={{header|Frink}}==
<
do
{
n = n + 1
println[n]
} while n mod 6 != 0</
=={{header|FutureBasic}}==
<
dim as long i
Line 1,159 ⟶ 1,429:
print i
until ( i mod 6 == 0 )
HandleEvents</syntaxhighlight>
{{out}}
<pre>
1
Line 1,168 ⟶ 1,439:
5
6
</pre>
=={{header|GAP}}==
<
repeat
n := n + 1;
Print(n, "\n");
until RemInt(n, 6) = 0;</
=={{header|GML}}==
<
do
{
Line 1,200 ⟶ 1,455:
show_message(string(i))
}
until (i mod 6 = 0)</
=={{header|Go}}==
There is no explicit do-while in Go, but it can be simulated with a range-based for loop and the break statement.
<
import "fmt"
Line 1,217 ⟶ 1,472:
}
}
}</
{{out}}
<pre>1
Line 1,226 ⟶ 1,481:
6</pre>
It can also be simulated ''without'' using a break statement as follows:
<
import "fmt"
Line 1,236 ⟶ 1,491:
fmt.Println(value)
}
}</
{{out}}
Line 1,242 ⟶ 1,497:
Same as before.
</pre>
::<
import "fmt"
Line 1,268 ⟶ 1,523:
}
fmt.Println(n3) // prt 8
}</
=={{header|Groovy}}==
For Groovy 3.0.0 and later.
<
do {
i++
println i
} while (i % 6 != 0)</
Previous versions of Groovy did not have a bottom-checking loop construct. Workaround is to use an "infinite" while loop with a conditional break as the last statement.
<
while (true) {
i++
println i
if (i % 6 == 0) break
}</
{{out}}
Line 1,292 ⟶ 1,547:
5
6</pre>
=={{header|Harbour}}==
<
DO WHILE .T.
Line 1,324 ⟶ 1,556:
EXIT
ENDIF
ENDDO</
=={{header|Haskell}}==
<syntaxhighlight lang="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:
<
0
1
Line 1,340 ⟶ 1,571:
3
4
5</
The standard Prelude also includes, without further import or definition, an '''until''' function, which takes three arguments – a predicate function, a transformation function, and an initial value.
<
main =
mapM_ print . reverse $
Line 1,350 ⟶ 1,581:
(\(x:_) -> (x > 0) && (mod x 6 == 0))
(\xs@(x:_) -> succ x : xs)
[0]</
{{Out}}
Line 1,363 ⟶ 1,594:
=== With mutable references ===
Using iterateWhile from monad-loops package
<
import Control.Monad.Loops
Line 1,372 ⟶ 1,603:
val <- readIORef x
print val
return val</
=={{header|Haxe}}==
<
do {
val++;
Sys.println(val);
} while( val % 6 != 0);</
=={{header|HolyC}}==
<
do {
i++;
Print("%d\n", i);
} while (i % 6 != 0);</
=={{header|Icon}} and {{header|Unicon}}==
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 [[Icon%2BUnicon/Intro#Looping_Controls|Introduction to Icon and Unicon/Looping Controls]] for more information.)
<
i := 0
Line 1,398 ⟶ 1,629:
if i % 6 = 0 then break
}
end</
=={{header|J}}==
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
<syntaxhighlight lang=J> ,.
0
1
2
3
4
5
</syntaxhighlight>
This could also be accomplished using [[j:Vocabulary/zcapco|Z:]] to provide early termination from a [[j:Vocabulary/fcap|fold]]:
<syntaxhighlight lang=J> 0#]F.(>: [ echo [ _2 Z: * * 0=6|]) 0
0
1
2
3
4
5
</syntaxhighlight>
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).
<
NB. The 'st' in 'whilst' stands for 'skip test'
Line 1,416 ⟶ 1,666:
i.0 0
)</
Though it's rare to see J code like this.
Line 1,422 ⟶ 1,672:
=={{header|Java}}==
<
do{
val++;
System.out.println(val);
}while(val % 6 != 0);</
=={{header|JavaScript}}==
===Javascript: Imperative===
<
do {
print(++val);
} while (val % 6);</
===Javascript: Functional===
Line 1,444 ⟶ 1,694:
:#and a conditional While function.
<
'use strict';
var d = fnBody(varValue); // a transformed value
Line 1,462 ⟶ 1,712:
}
).join('\n')
);</
Output:
<syntaxhighlight lang="javascript">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.
Line 1,476 ⟶ 1,726:
So, for example, something like:
<
'use strict';
return Array.apply(null, Array(n - m + 1)).map(
Line 1,503 ⟶ 1,753:
}
).join('\n')
);</
Output:
<syntaxhighlight lang="javascript">1
2
3
4
5</
====ES6====
A process or value of this kind might be better expressed (in functionally composed JavaScript) with an '''unfold''' or '''until''' function, returning a list.
<
'use strict';
Line 1,562 ⟶ 1,811:
return [result1, result2];
})();
</syntaxhighlight>
<
ES6 is a superset of Javascript so the Javascript and ES5 solution is valid. An example of a do-while loop in a generator follows that produces correct output:
<syntaxhighlight lang="javascript">
// generator with the do while loop
function* getValue(stop) {
Line 1,590 ⟶ 1,838:
printVal(gen, gen.next());
})();
</syntaxhighlight>
<syntaxhighlight lang="javascript">
1
2
Line 1,599 ⟶ 1,847:
5
6
</syntaxhighlight>
=={{header|jq}}==
Line 1,605 ⟶ 1,853:
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:
<
def do_while( action; condition ):
def w: action | if (condition | not) then empty else ., w end;
w;</
'''The task:'''
<
{{out}}
1
Line 1,621 ⟶ 1,869:
Julia has no do-while construct. Here is one of several ways to implement do-while behavior.
<syntaxhighlight lang="julia">
julia> i = 0
0
Line 1,636 ⟶ 1,884:
4
5
</syntaxhighlight>
Using a macro that mimics the classic C style do-while.
Line 1,642 ⟶ 1,890:
Notice that the symbol <code>while</code> cannot be used as it is a keyword, which is why <code>when</code> is used instead, also the macro definition is wrapped in a <code>@eval</code> macro invocation since <code>do</code> is also a keyword, but in Julia macro calls are prefixed by <code>@</code> so this is only an issue during the macro definition, not when invoked, ie. <code>@do block when condition</code>).
<syntaxhighlight lang="julia">
julia> @eval macro $(:do)(block, when::Symbol, condition)
when ≠ :when && error("@do expected `when` got `$s`")
Line 1,669 ⟶ 1,917:
i = 4
i = 5
</syntaxhighlight>
Here is mostly the same macro, but with the conditional clause used first, which is arguably more readable.
<syntaxhighlight lang="julia">
julia> macro do_while(condition, block)
quote
Line 1,699 ⟶ 1,947:
i = 4
i = 5
</syntaxhighlight>
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 1,710 ⟶ 1,958:
}
while (value % 6 != 0)
}</
{{out}}
Line 1,726 ⟶ 1,974:
=={{header|Lasso}}==
<
while(#x % 6 > 0 || #x == 0) => {^
++#x
'\r' // for formatting
^}</
=={{header|Lambdatalk}}==
<
{def do_while
{def do_while.r
Line 1,746 ⟶ 1,994:
{do_while 0}
-> 0 1 2 3 4 5 6 (end of loop)
</syntaxhighlight>
=={{header|
Lang does not have a do-while loop. A simple loop like the example below can be used.
<syntaxhighlight lang="lang">
$i = 0
loop {
$i += 1
fn.println($i)
if(!($i % 6)) {
con.break
}
}
</syntaxhighlight>
=={{header|Lingo}}==
Lingo has no do..while, but here how this behavior can be implemented:
<
repeat while TRUE
i = i+1
put i
if i mod 6 = 0 then exit repeat
end</
=={{header|Lisaac}}==
<
{
val := val + 1;
Line 1,773 ⟶ 2,027:
'\n'.print;
val % 6 != 0
}.while_do { };</
=={{header|LiveCode}}==
<
add 1 to n
put n
end repeat</
=={{header|Logo}}==
<
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]
Line 1,791 ⟶ 2,045:
if notequal? 0 modulo :n 6 [my.loop :n]
end
my.loop 0</
=={{header|Lua}}==
Line 1,797 ⟶ 2,051:
Lua doesn't have a <code>do .. while</code> construct.
<
i=0
repeat
Line 1,803 ⟶ 2,057:
print(i)
until i%6 == 0
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module checkit {
x=0
\\ Do or Repeat
Do
x++
print x,
when x mod 6>0
print
// or we can use Until x mod 6 = 0
// and we can use block if we like it
x=0
Do {
x++
}
print
x=0
{
Line 1,821 ⟶ 2,083:
if x mod 6<>0 Then loop ' set loop flag of current block to true
\\ when block end check Loop flag and if true execute block again
}
print
}
Checkit
module Old_Style {
10 REM Loops/Do-while
20 LET I=0
30 LET I=I+1
40 PRINT I
50 IF INT(I/6)*6 <> I THEN 30
60 END
}
Old_Style
// modern style, using high order functions
module generic_iterator {
do_while = lambda (f, p)->{
{
if p(f()) then loop
}
}
funcA=lambda (start_from, do_what) -> {
=lambda i=start_from, do_what ->{
call do_what(i)
=i
i++
}
}
funcPrint=lambda ->{
print number
}
call do_while(funcA(1, funcPrint), lambda->number mod 6 <>0)
}
generic_iterator
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1
2
Line 1,844 ⟶ 2,139:
=={{header|Maple}}==
<
do
val := 1 + val;
Line 1,851 ⟶ 2,146:
break
end if;
end do:</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Because everything is an expression in Mathematica, <code>While[body;condition]</code> tests <code>condition</code> after <code>body</code> has been executed at least once.
<
While[
value++;
Print[value];
Mod[value,6]!=0
]</
=={{header|MATLAB}} / {{header|Octave}}==
<
while (1)
a = a+1;
disp(a);
if (~mod(a,6)) break; end;
end; </
=={{header|Maxima}}==
<
=={{header|MAXScript}}==
<
do
(
Line 1,881 ⟶ 2,176:
a += 1
)
while mod a 6 != 0</
=={{header|Metafont}}==
Line 1,887 ⟶ 2,182:
Metafont has no a do-while construct; the same thing can be done using a forever loop and exitif.
<
forever: show a; a := a + 1; exitif a mod 6 = 0; endfor
end</
=={{header|min}}==
{{works with|min|0.19.6}}
<
=={{header|MIPS Assembly}}==
<syntaxhighlight lang="mips">
.text
main: li $s0, 0 # start at 0.
Line 1,935 ⟶ 2,205:
li $v0, 10
syscall # syscall to end the program
</syntaxhighlight>
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">0 П4 КИП4 ИП4 6 / {x} x=0 02 С/П</
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 1,954 ⟶ 2,224:
InOut.WriteLn;
UNTIL i MOD 6 = 0;
END DoWhile.</
=={{header|Modula-3}}==
This is very similar to the [[Modula-2]] code above.
<
i := i + 1;
IO.Put(Fmt.Int(i));
UNTIL i MOD 6 = 0;</
=={{header|Monicelli}}==
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
</syntaxhighlight>
=={{header|MUMPS}}==
{{works with|Caché ObjectScript}}
<
set val = 0
do {
Line 1,980 ⟶ 2,250:
} while ((val # 6) '= 0)
quit</
{{out}}<pre>
Line 1,993 ⟶ 2,263:
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
Loops/Do-while in Neko
Tectonics:
Line 2,004 ⟶ 2,274:
index += 1;
$print(index, "\n");
} while (index % 6) != 0</
{{out}}
Line 2,017 ⟶ 2,287:
=={{header|Nemerle}}==
<
do
{
x++;
WriteLine($"$x");
} while (x % 6 != 0)</
=={{header|NetRexx}}==
In NetRexx the '''do–while''' construct is implemented via the <code>until ''expru''</code> conditional clause of the <code>loop</code> instruction. The expression ''expru'' in the <code>until ''expru''</code> clause is evaluated at the end of the loop, guaranteeing that the loop will be executed at least once.
<
options replace format comments java crossref savelog symbols nobinary
Line 2,037 ⟶ 2,307:
say i_
end
</syntaxhighlight>
=={{header|NewLISP}}==
<
(do-until (= 0 (% i 6))
(println (++ i))))</
=={{header|Nim}}==
Nim does not have a do-while loop. The standard way to simulate it consists in an infinite loop with a break statement:
<
while true:
inc val
echo val
if val mod 6 == 0: break</
It's also easy to write your own doWhile construction (but be aware that the instructions will be duplicated):
<
b
while a:
Line 2,062 ⟶ 2,332:
doWhile val mod 6 != 0:
inc val
echo val</
=={{header|
<syntaxhighlight lang="nu">
mut n = 0
while true {
$n += 1
print $n
if $n mod 6 == 0 {break}
}
</syntaxhighlight>
=={{header|Oberon-2}}==
Works with oo2c Version 2
<
MODULE LoopDoWhile;
IMPORT
Line 2,091 ⟶ 2,365:
Do
END LoopDoWhile.
</syntaxhighlight>
=={{header|Objeck}}==
<
i := 0;
do {
Line 2,101 ⟶ 2,375:
}
while (i % 6 <> 0);
</syntaxhighlight>
=={{header|OCaml}}==
OCaml doesn't have a do-while loop, so we can just make a local loop:
<
let i = succ i in
Printf.printf "%d\n" i;
Line 2,111 ⟶ 2,385:
loop i
in
loop 0</
or implementing a generic do-while iterator with higher order function:
<
let rec loop() =
f();
Line 2,121 ⟶ 2,395:
in
loop()
(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *)</
<
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 rec loop v =
let v = f v in
Line 2,140 ⟶ 2,414:
(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:
<
exception Exit_loop
try while true do
Line 2,151 ⟶ 2,425:
raise Exit_loop;
done
with Exit_loop -> ()</
=={{header|Octave}}==
The do-while can be changed into a do-until, just negating the condition of the while.
<
do
val++;
disp(val)
until( mod(val, 6) == 0 )</
=={{header|Oforth}}==
<
=={{header|OpenEdge/Progress}}==
<
DO WHILE ii MODULO 6 <> 0 OR ii = 0:
ii = ii + 1.
MESSAGE ii VIEW-AS ALERT-BOX.
END.</
=={{header|Oz}}==
Normal Oz variables are single-assignment only. So we use a "cell", which is a one-element mutable container.
<
I = {NewCell 0}
in
Line 2,181 ⟶ 2,455:
I := @I + 1
{Show @I}
end</
=={{header|PARI/GP}}==
The generic Pari loops (<code>while</code>, <code>until</code>) test at the beginning, so just use an infinite loop with a break.
<
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.
<
while (print(x++) || x % 6, )</
The condition in <code>while</code> and <code>until</code> is an expression, not a sequence, so <code>;</code> for multiple statements cannot be used there.
=={{header|Pascal}}==
<
var
Line 2,210 ⟶ 2,484:
writeln(i)
until i mod 6 = 0
end.</
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
begin
var a := 0;
repeat
a += 1;
Print(a);
until a mod 6 = 0;
end.
</syntaxhighlight>
=={{header|Perl}}==
<
do {
$val++;
print "$val\n";
} while ($val % 6);</
<code>do ... until (''condition'')</code> is equivalent to <code>do ... while (not ''condition'')</code>.
<
do {
$val++;
print "$val\n";
} until ($val % 6 == 0);</
=={{header|Phix}}==
<!--<
<span style="color: #004080;">integer</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
Line 2,233 ⟶ 2,520:
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/Do-while
by Galileo, 11/2022 #/
include ..\Utilitys.pmt
0
true while
1 +
dup ?
dup 6 mod
endwhile</syntaxhighlight>
=={{header|PHL}}==
<
do {
i = i::inc;
printf("%i\n", i);
} while (i%6 != 0);</
=={{header|PHP}}==
<
do {
$val++;
print "$val\n";
} while ($val % 6 != 0);</
=={{header|Picat}}==
===do while loop===
<syntaxhighlight lang="picat">go =>
N = 0,
do
N := N+1,
println(N)
while (N mod 6 != 0).</syntaxhighlight>
===Recursion===
<syntaxhighlight lang="picat">go2 =>
do_while(0).
do_while(N) :-
N1 = N + 1,
println(N1),
N1 mod 6 != 0,
do_while(N1).</syntaxhighlight>
Both outputs the same.
{{out}}
<pre>1
2
3
4
5
6</pre>
=={{header|PicoLisp}}==
Literally:
<
(loop
(println (inc 'Val))
(T (=0 (% Val 6))) ) )</
Shorter:
<
(until (=0 (% (println (inc 'Val)) 6))) )</
or:
<
=={{header|Pike}}==
<
int value = 0;
do {
Line 2,269 ⟶ 2,598:
write(value + "\n");
} while (value % 6);
}</
=={{header|PL/0}}==
PL/0 does not have a <code>do .. while</code> construct. Equivalent using <code>while</code>:
<syntaxhighlight lang="pascal">
var i;
begin
i := 0;
i := i + 1;
! i;
while (i / 6) * 6 <> i do
begin
i := i + 1;
! i
end;
end.
</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
=={{header|PL/I}}==
<
dcl value fixed bin (31) init (0);
do forever;
Line 2,282 ⟶ 2,636:
put list (value);
end;
</syntaxhighlight>
or shorter:
<
dcl value fixed bin(31) init(0);
do Until(value=6);
value+=1;
put Skip list(value);
end;</
{{out}}
<pre>
Line 2,302 ⟶ 2,656:
=={{header|Plain English}}==
Plain English has one kind of loop: an infinite loop with (hopefully) a conditional break/exit. So if you want a do-while, put the conditional break/exit at the end of the loop.
<
Start up.
Demonstrate do-while.
Line 2,313 ⟶ 2,667:
Write the string on the console.
If the counter is evenly divisible by 6, exit.
Repeat.</
=={{header|Pop11}}==
<
while true do
val + 1 -> val;
printf(val, '%p\n');
quitif(val rem 6 = 0);
endwhile;</
=={{header|PowerShell}}==
<
do {
$n++
$n
} while ($n % 6 -ne 0)</
=={{header|Prolog}}==
<
% initial condition
do(0):- write(0),nl,do(1).
Line 2,347 ⟶ 2,701:
do(0).
</syntaxhighlight>
=={{header|Python}}==
Python doesn't have a do-while loop.
<
while True:
val +=1
print val
if val % 6 == 0: break</
or repeat the body of the loop before a standard while.
<
print val
while val % 6 != 0:
val += 1
print val</
=={{header|Quackery}}==
Quackery's control flow words are mix-and-match. To satisfy this task, we can check for the exit condition at the end of the loop. <code>until</code> means <tt>jump to <code>[</code> if ToS is false</tt>.
<
dup 6 mod 0 = until ] drop</
=={{header|R}}==
<
repeat
{
Line 2,390 ⟶ 2,729:
print(i)
if(i %% 6 == 0) break
}</
=={{header|Racket}}==
Idiomatic Racket code is functional:
<
#lang racket
(let loop ([n 0])
Line 2,401 ⟶ 2,739:
(displayln n)
(unless (zero? (modulo n 6)) (loop n))))
</syntaxhighlight>
But an imperative version is easy to do too:
<
#lang racket
(define n 0)
Line 2,411 ⟶ 2,749:
(displayln n)
(unless (zero? (modulo n 6)) (loop)))
</syntaxhighlight>
=={{header|Raku}}==
Line 2,417 ⟶ 2,755:
{{works with|Rakudo Star|2010.08}}
<syntaxhighlight lang="raku"
repeat {
say ++$val;
} while $val % 6;</
<code>repeat ... until ''condition''</code> is equivalent to <code>do ... while not ''condition''</code>.
<syntaxhighlight lang="raku"
repeat {
say ++$val;
} until $val %% 6;</
(Here we've used <code>%%</code>, the "divisible-by" operator.)
<p>
You can also put the condition before the block, without changing the order of evaluation.
<syntaxhighlight lang="raku"
repeat while $val % 6 {
say ++$val;
}</
=={{header|REBOL}}==
<
Title: "Loop/While"
URL: http://rosettacode.org/wiki/Loop/Do_While
Line 2,452 ⟶ 2,790:
0 = mod value 6
]</
{{out}}
Line 2,463 ⟶ 2,801:
=={{header|Red}}==
<
i: 0
until [
Line 2,470 ⟶ 2,808:
i % 6 = 0 ;; loop , until this is true...
]
</
<pre>i: 0
i: 1
Line 2,488 ⟶ 2,826:
<br>This necessitates the use of '''DO UNTIL''' instead of '''DO WHILE'''.
===version 1===
<
v=0
do until v//6==0 /*REXX // is the ÷ remainder.*/
Line 2,494 ⟶ 2,832:
say v
end
/*stick a fork in it, we're done.*/</
{{out}}
<pre>
Line 2,506 ⟶ 2,844:
===version 2===
<
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 1<sup>st</sup> version.<br>
=={{header|Ring}}==
<
n = 0
While True
Line 2,521 ⟶ 2,859:
if n % 6 = 0 exit ok
end
</syntaxhighlight>
=={{header|RPL}}==
To ensure at least one loop, <code>DO</code>..<code>UNTIL</code>..<code>END</code> must be used rather than <code>WHILE</code>..<code>REPEAT</code>..<code>END</code>. To actually print (on paper) instead of pushing in the stack successive results, the <code>DUP</code> instruction inside the loop shall be replaced by <code>PR1</code>
≪ 0
'''DO'''
1 + DUP
'''UNTIL''' DUP 6 MOD 0 == '''END'''
DROP
≫
=={{header|Ruby}}==
Line 2,530 ⟶ 2,877:
! until
|-
| <
begin
val += 1
puts val
end while val % 6 != 0</
| <
begin
val += 1
puts val
end until val % 6 == 0</
|}
During November 2005, Yukihiro Matsumoto, the creator of Ruby, [https://web.archive.org/web/20220322235407/http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6741 regretted this loop feature] and [https://web.archive.org/web/20220322235418/http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6745 suggested using Kernel#loop].
{| class="wikitable"
Line 2,548 ⟶ 2,895:
! break if
|-
| <
loop do
val += 1
puts val
break unless val %6 != 0
end</
| <
loop do
val += 1
puts val
break if val %6 == 0
end</
|}
Line 2,568 ⟶ 2,915:
Rust does not have a <tt>do...while</tt> loop. Instead, the keyword <tt>loop</tt> is used with a termination condition.
<
loop {
Line 2,575 ⟶ 2,922:
if x % 6 == 0 { break; }
}</
=={{header|Salmon}}==
<
do
{
Line 2,584 ⟶ 2,931:
x!
}
while (x % 6 != 0);</
=={{header|SAS}}==
<
data _null_;
n=0;
Line 2,594 ⟶ 2,941:
put n;
end;
run;</
=={{header|Sather}}==
{{trans|C}}
<
main is
val ::= 0;
Line 2,607 ⟶ 2,954:
end;
end;
end;</
=={{header|S-BASIC}}==
S-BASIC's REPEAT..UNTIL control structure is the equivalent of a
DO..WHILE statement with the terminating test inverted.
<syntaxhighlight lang="BASIC">
rem - return n mod m
function mod(n, m = integer) = integer
end = n - (n / m) * m
var i = integer
i = 0
repeat
begin
i = i + 1
print i;
end
until mod(i, 6) = 0
end
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
</pre>
=={{header|Scala}}==
Line 2,613 ⟶ 2,984:
===Imperative===
<
var (x, l) = (0, List[Int]())
do {
Line 2,621 ⟶ 2,992:
l
}.foreach(println(_))
</syntaxhighlight>
===Tail recursive===
<
val succ = iter + 1
val temp = accu :+ succ
if (cond(succ)) loop(succ, cond, temp) else temp
}
println(loop(0, (_ % 6 != 0), Nil))</
===Stream===
<
val succ = i + 1;
succ #:: (if (cond(succ)) loop(succ, cond) else Stream.empty)
}
loop(0, (_ % 6 != 0)).foreach(println(_))</
=={{header|Scheme}}==
<
(display i)
(if (positive? (modulo i 6))
(loop (+ i 1))))</
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">v=0
while %T
v=v+1
Line 2,652 ⟶ 3,023:
if modulo(v,6)==0 then break; end
end
printf("\n")</
{{out}}
<pre> 1 2 3 4 5 6 </pre>
=={{header|Seed7}}==
<
const proc: main is func
Line 2,667 ⟶ 3,038:
writeln(number)
until number rem 6 = 0
end func;</
=={{header|Sidef}}==
<
do {
say ++value;
} while (value % 6);</
=={{header|Slate}}==
<
val: 0.
[val: val + 1.
print: val.
val \\ 6 ~= 0] whileTrue
] do.</
=={{header|Smalltalk}}==
<
val := 0.
[
val := val + 1.
val displayNl.
] doWhile: [ (val rem: 6) ~= 0 ]</
<
val := 0.
[
val := val + 1.
val displayNl.
] doUntil: [ (val rem: 6) == 0 ]</
To simulate the do-while construct, we can use the
<tt>whileTrue:</tt> method of a block with a void while block.
<
val := 0.
[
Line 2,707 ⟶ 3,078:
val displayNl.
(val rem: 6) ~= 0
] whileTrue: [ ]</
Or send the loop block a <tt>whileTrue</tt> message (without argument).
<
val := 0.
[
Line 2,716 ⟶ 3,087:
val displayNl.
(val rem: 6) ~= 0
] whileTrue</
Corresponding false-checking messages are <tt>whileFalse:</tt> and <tt>whileFalse</tt> (without argument)
=={{header|Sparkling}}==
<
do {
print(++i);
} while (i % 6 != 0);</
=={{header|Spin}}==
Line 2,731 ⟶ 3,102:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 2,750 ⟶ 3,121:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</
{{out}}
<pre>1 2 3 4 5 6 </pre>
=={{header|SPL}}==
<
>
n += 1
#.output(n)
< n%6</
{{out}}
<pre>
Line 2,775 ⟶ 3,146:
Use a flag to force the first loop. It's changed in the loop so that it will have no effect after the first loop.
<
local q 1
while `q' | mod(`n',6) {
local q 0
di `++n'
}</
Use an infinite while loop and do the test with an ''[https://www.stata.com/help.cgi?if if]''' at the end of the loop.
<
while 1 {
di `++n'
if mod(`n',6)==0 continue, break
}</
=== Mata ===
Mata has a '''[https://www.stata.com/help.cgi?m2_do do/while]''' loop:
<syntaxhighlight lang="text">mata
n=0
do {
printf("%f\n",++n)
} while (mod(n,6))
end</
=={{header|Suneido}}==
<
do
{
Print(++val)
} while (val % 6 isnt 0)</
{{out}}
Line 2,817 ⟶ 3,188:
=={{header|Swift}}==
{{works with|Swift|3.x+}}
<
repeat {
val += 1
print(val)
} while val % 6 != 0</
{{works with|Swift|2.x}}
<
repeat {
val++
print(val)
} while val % 6 != 0</
{{works with|Swift|1.x}}
<
do {
val++
println(val)
} while val % 6 != 0</
=={{header|Tailspin}}==
In Tailspin you can loop by sending a value back to the matchers (by "-> #"). Depending on how you set that up, you create different loops.
<
templates doWhile
0 -> #
Line 2,844 ⟶ 3,215:
$val -> \(<?($ mod 6 <~=0>)> $!\) -> #
end doWhile
</syntaxhighlight>
=={{header|Tcl}}==
Tcl does not have a built-in <code>do...while</code> construct. This example demonstrates the ease of creating new looping contructs in plain Tcl. <code>do</code> procedure taken from [http://wiki.tcl.tk/3603 Tcler's wiki]
<
if {$keyword eq "while"} {
set expression "!($expression)"
Line 2,865 ⟶ 3,236:
set i 0
do {puts [incr i]} while {$i % 6 != 0}</
{{tcllib|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 <code>while</code> as:
<
while true {
puts [incr i]
if {$i % 6 == 0} break
}</
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
var=0
Line 2,887 ⟶ 3,258:
IF (rest==0) EXIT
ENDLOOP
</syntaxhighlight>
{{out}}
<pre>
Line 2,902 ⟶ 3,273:
{{works with|pdksh}}
{{works with|zsh}}
<
while true; do
echo $((++val))
[ $((val%6)) -eq 0 ] && break
done</
{{works with|Bourne Shell}}
<
while true; do
val=`expr $val + 1`
echo $val
expr $val % 6 = 0 >/dev/null && break
done</
{{works with|zsh}}
<
print $val
(( val % 6 )) || break
}</
=={{header|Vala}}==
<
do {
Line 2,930 ⟶ 3,301:
}
while ( a % 6 != 0);
</syntaxhighlight>
=={{header|Vedit macro language}}==
<
do {
#1++
Num_Type(#1)
} while (#1 % 6 != 0);</
=={{header|Verbexx}}==
<
@LOOP init:{@VAR n = 0} until:(n % 6 == 0)
Line 2,956 ⟶ 3,317:
n++;
@SAY n;
};</
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
module main;
integer i;
Line 2,975 ⟶ 3,335:
end
endmodule
</syntaxhighlight>
=={{header|V (Vlang)}}==
{{trans|go}}
There is no explicit do-while in Vlang, but it can be simulated with a range-based for loop and the break statement.
<syntaxhighlight lang="go">fn main() {
mut value := 0
for {
value++
println(value)
if value%6 != 0 {
break
}
}
}</syntaxhighlight>
{{out}}
<pre>1
2
3
4
5
6</pre>
It can also be simulated ''without'' using a break statement as follows:
<syntaxhighlight lang="vlang">fn main() {
mut value := 0
for ok := true; ok; ok = value%6 != 0 {
value++
println(value)
}
}</syntaxhighlight>
{{out}}
<pre>
Same as before.
</pre>
::<syntaxhighlight lang="vlang">fn main() {
// do-while loop 1
mut n1 := 2
for n1 < 6 {
n1 *= 2
}
println(n1) // prt 8
// do-while loop 2
mut n2 := 2
for ok := true; ok; ok = n2%8 != 0 {
n2 *= 2
}
println(n2) // prt 8
// do-while loop 3
mut n3 := 2
for {
n3 *= 2
if n3 >= 6 {
break
}
}
println(n3) // prt 8
}</syntaxhighlight>
=={{header|Wren}}==
Wren doesn't have a ''do/while'' loop as such but we can simulate it using an infinite loop with a final conditional break.
<
while (true) {
v = v + 1
System.print(v)
if (v%6 == 0) break
}</
{{out}}
<pre>
Line 3,004 ⟶ 3,412:
</pre>
It can also be simulated ''without'' using a break statement as follows:
<
var ok = true
while (ok) {
Line 3,010 ⟶ 3,418:
System.print(value)
ok = value%6 != 0
}</
{{out}}
Line 3,020 ⟶ 3,428:
{{works with|nasm}}
{{works with|windows}}
<
extern _printf
Line 3,046 ⟶ 3,454:
xor eax, eax
ret
</syntaxhighlight>
=={{header|XPL0}}==
<
int V;
[V:= 0;
Line 3,072 ⟶ 3,463:
IntOut(0, V); CrLf(0);
until rem(V/6) = 0;
]</
=={{header|Yorick}}==
<
do {
val++;
write, val;
} while(val % 6 != 0);</
=={{header|zkl}}==
{{trans|Yorick}}
<
do {
val+=1;
val.print(" ");
} while(val % 6 != 0);</
{{out}}
<pre>
1 2 3 4 5 6
</pre>
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
pub fn main() !void {
var a: u8 = 0;
// no do-while in syntax, trust the optimizer to do
// correct Loop inversion https://en.wikipedia.org/wiki/Loop_inversion
// If the variable `alive` is independent to other variables and not in
// diverging control flow, then the optimization is possible in general.
var alive = true;
while (alive == true or a % 6 != 0) {
alive = false;
a += 1;
try std.io.getStdOut().writer().print("{d}\n", .{a});
}
}</syntaxhighlight>
{{omit from|GUISS}}
|