Loops/Do-while: Difference between revisions
Loops/Do-while in various BASIC dialents (QBasic, XBasic and Tiny BASIC) |
m Moved FutureBasic entry out of the BASIC group |
||
(39 intermediate revisions by 20 users not shown) | |||
Line 34: | Line 34: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V val = 0 |
||
L |
L |
||
val++ |
val++ |
||
print(val) |
print(val) |
||
I val % 6 == 0 |
I val % 6 == 0 |
||
L.break</ |
L.break</syntaxhighlight> |
||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
;Basic |
;Basic |
||
The WTO macro is in SYS1.MACLIB, which needs to be in the SYSLIB concatenation at assembly. |
The WTO macro is in SYS1.MACLIB, which needs to be in the SYSLIB concatenation at assembly. |
||
< |
<syntaxhighlight lang="360asm">* Do-While |
||
DOWHILE CSECT , This program's control section |
DOWHILE CSECT , This program's control section |
||
BAKR 14,0 Caller's registers to linkage stack |
BAKR 14,0 Caller's registers to linkage stack |
||
Line 70: | Line 70: | ||
WTOLEN DC H'2' fixed WTO length of two |
WTOLEN DC H'2' fixed WTO length of two |
||
WTOTXT DC CL2' ' |
WTOTXT DC CL2' ' |
||
END DOWHILE </ |
END DOWHILE </syntaxhighlight> |
||
;Structured Macros |
;Structured Macros |
||
Although specified at the beginning (DO UNTIL), the test is done at the end of the loop (ENDDO). |
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. |
Structured macros (DO ENDDO) weren't in the 1963 standard of Assembler 360, but there are part of it since since 1998. |
||
< |
<syntaxhighlight lang="360asm">* Do-While 27/06/2016 |
||
DOWHILE CSECT |
DOWHILE CSECT |
||
USING DOWHILE,12 set base register |
USING DOWHILE,12 set base register |
||
Line 94: | Line 94: | ||
DC H'0' must be zero |
DC H'0' must be zero |
||
WTOTXT DS C one char |
WTOTXT DS C one char |
||
END DOWHILE</ |
END DOWHILE</syntaxhighlight> |
||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
Code is called as a subroutine (i.e. JSR DoWhileSub). Specific OS/hardware routines for printing are left unimplemented. |
Code is called as a subroutine (i.e. JSR DoWhileSub). Specific OS/hardware routines for printing are left unimplemented. |
||
< |
<syntaxhighlight lang="6502asm">DoWhileSub: PHA |
||
TYA |
TYA |
||
PHA ;push accumulator and Y register onto stack |
PHA ;push accumulator and Y register onto stack |
||
Line 115: | Line 115: | ||
TAY |
TAY |
||
PLA ;restore Y register and accumulator from stack |
PLA ;restore Y register and accumulator from stack |
||
RTS ;return from subroutine</ |
RTS ;return from subroutine</syntaxhighlight> |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program loopdowhile64.s */ |
/* program loopdowhile64.s */ |
||
Line 173: | Line 173: | ||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">Proc Main() |
||
byte A |
byte A |
||
Line 186: | Line 186: | ||
Until A Mod 6=0 |
Until A Mod 6=0 |
||
Od |
Od |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">var val:int = 0; |
||
do |
do |
||
{ |
{ |
||
trace(++val); |
trace(++val); |
||
} while (val % 6);</ |
} while (val % 6);</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">loop |
||
Value := Value + 1; |
Value := Value + 1; |
||
Put (Value); |
Put (Value); |
||
exit when Value mod 6 = 0; |
exit when Value mod 6 = 0; |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
Here is an alternative version: |
Here is an alternative version: |
||
< |
<syntaxhighlight lang="ada">for Value in 0..Integer'Last loop |
||
Put (Value); |
Put (Value); |
||
exit when Value mod 6 = 0; |
exit when Value mod 6 = 0; |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
=={{header|Agena}}== |
=={{header|Agena}}== |
||
Tested with Agena 2.9.5 Win32 |
Tested with Agena 2.9.5 Win32 |
||
< |
<syntaxhighlight lang="agena">scope |
||
local i := 0; |
local i := 0; |
||
do |
do |
||
Line 215: | Line 215: | ||
print( i ) |
print( i ) |
||
as ( i % 6 ) <> 0 |
as ( i % 6 ) <> 0 |
||
epocs</ |
epocs</syntaxhighlight> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">integer a; |
||
a = 0; |
a = 0; |
||
Line 225: | Line 225: | ||
o_integer(a); |
o_integer(a); |
||
o_byte('\n'); |
o_byte('\n'); |
||
} while (a % 6 != 0);</ |
} while (a % 6 != 0);</syntaxhighlight> |
||
=={{header|ALGOL 60}}== |
=={{header|ALGOL 60}}== |
||
{{works with|ALGOL 60|OS/360}} |
{{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. |
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. |
||
< |
<syntaxhighlight lang="algol60">'BEGIN' 'COMMENT' Loops DoWhile - Algol60 - 22/06/2018; |
||
'INTEGER' I; |
'INTEGER' I; |
||
I:=0; |
I:=0; |
||
Line 239: | Line 239: | ||
'GOTO' LOOP; |
'GOTO' LOOP; |
||
ENDLOOP: |
ENDLOOP: |
||
'END'</ |
'END'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 245: | Line 245: | ||
</pre> |
</pre> |
||
=== |
===A "goto-less" approach=== |
||
{{works with|A60}} |
{{works with|A60}} |
||
While "goto" may |
While a judicious "goto" may sometimes be clearer than the alternative, it is possible to avoid it in this case by using the for-while statement and a boolean flag to implement a test-at-the-bottom loop. |
||
< |
<syntaxhighlight lang="algol60"> |
||
begin |
begin |
||
Line 255: | Line 255: | ||
i := 0; |
i := 0; |
||
another := true; |
another := true; |
||
for i := i |
for i := i while another do |
||
begin |
begin |
||
i := i + 1; |
|||
outinteger(1,i); |
outinteger(1,i); |
||
comment - |
comment - stop once i mod 6 = 0; |
||
if i = (i div 6) * 6 then another := false; |
if i = (i div 6) * 6 then another := false; |
||
end; |
end; |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 270: | Line 271: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">FOR value WHILE |
||
print(value); |
print(value); |
||
# WHILE # value MOD 6 /= 0 DO |
# WHILE # value MOD 6 /= 0 DO |
||
SKIP |
SKIP |
||
OD</ |
OD</syntaxhighlight> |
||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin |
||
integer i; |
integer i; |
||
i := 0; |
i := 0; |
||
Line 287: | Line 288: | ||
end |
end |
||
do begin end |
do begin end |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|AmigaE}}== |
=={{header|AmigaE}}== |
||
< |
<syntaxhighlight lang="amigae">PROC main() |
||
DEF i = 0 |
DEF i = 0 |
||
REPEAT |
REPEAT |
||
Line 296: | Line 297: | ||
WriteF('\d\n', i) |
WriteF('\d\n', i) |
||
UNTIL Mod(i, 6) = 0 |
UNTIL Mod(i, 6) = 0 |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 442: | Line 443: | ||
pop {r4, lr} |
pop {r4, lr} |
||
bx lr |
bx lr |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<syntaxhighlight lang="applescript"> |
|||
<lang AppleScript> |
|||
on printConsole(x) |
on printConsole(x) |
||
return x as string |
return x as string |
||
Line 456: | Line 457: | ||
printConsole(table) |
printConsole(table) |
||
end repeat |
end repeat |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 470: | Line 471: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">value: 0 |
||
until [ |
until [ |
||
value: value + 1 |
value: value + 1 |
||
print value |
print value |
||
] [ 0 = value%6 ]</ |
] [ 0 = value%6 ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 487: | Line 488: | ||
=={{header|Asymptote}}== |
=={{header|Asymptote}}== |
||
Asymptote's control structures are similar to those in C, C++, or Java |
Asymptote's control structures are similar to those in C, C++, or Java |
||
< |
<syntaxhighlight lang="asymptote">int i = 0; |
||
do { |
do { |
||
++i; |
++i; |
||
write(" ", i, suffix=none); |
write(" ", i, suffix=none); |
||
} while (i % 6 != 0);</ |
} while (i % 6 != 0);</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">While mod(A_Index, 6) ;comment:everything but 0 is considered true |
||
output = %output%`n%A_Index% |
output = %output%`n%A_Index% |
||
MsgBox % output</ |
MsgBox % output</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">BEGIN { |
||
val = 0 |
val = 0 |
||
do { |
do { |
||
Line 505: | Line 506: | ||
print val |
print val |
||
} while( val % 6 != 0) |
} while( val % 6 != 0) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Axe}}== |
=={{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 Axe does not have explicit do-while loops, they can be easily emulated using an infinite loop with a conditional terminator: |
||
< |
<syntaxhighlight lang="axe">0→A |
||
While 1 |
While 1 |
||
A++ |
A++ |
||
Disp A▶Dec,i |
Disp A▶Dec,i |
||
End!If A^6</ |
End!If A^6</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with| |
{{works with|Commodore BASIC}} |
||
<syntaxhighlight lang="gwbasic"> 0 REMADE FOR DO WHILE |
|||
{{works with|Just BASIC}} |
|||
1 DEF FN MOD6(N) = N - INT (N / 6) * 6 |
|||
<lang qbasic>a = 0 |
|||
2 LET V4LUE = 0 |
|||
do |
|||
3 FOR DO = 0 TO 1 |
|||
10 LET V4LUE = V4LUE + 1 |
|||
print a |
|||
20 PRINT V4LUE" "; |
|||
loop while a mod 6 <> 0</lang> |
|||
30 WHILE = FN MOD6(V4LUE) < > 0:DO = NOT WHILE: NEXT</syntaxhighlight> |
|||
==={{header|ASIC}}=== |
==={{header|ASIC}}=== |
||
ASIC does not have a <code>do .. while</code> construct. Equivalent using <code>WHILE</code>: |
ASIC does not have a <code>do .. while</code> construct. Equivalent using <code>WHILE</code>: |
||
< |
<syntaxhighlight lang="basic"> |
||
REM Loops/Do-while |
REM Loops/Do-while |
||
Line 542: | Line 544: | ||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 553: | Line 555: | ||
</pre> |
</pre> |
||
Equivalent using conditional jump: |
Equivalent using conditional jump: |
||
< |
<syntaxhighlight lang="basic"> |
||
REM Loops/Do-while |
REM Loops/Do-while |
||
Line 564: | Line 566: | ||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
As above |
As above |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
a=0 |
a=0 |
||
REPEAT |
REPEAT |
||
Line 575: | Line 577: | ||
PRINT a |
PRINT a |
||
UNTIL MOD(a,6) == 0 |
UNTIL MOD(a,6) == 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
< |
<syntaxhighlight lang="basic256">i = 0 |
||
do |
do |
||
Line 585: | Line 587: | ||
until i mod 6 = 0 |
until i mod 6 = 0 |
||
print |
print |
||
end</ |
end</syntaxhighlight> |
||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
< |
<syntaxhighlight lang="bbcbasic">a = 0 |
||
REPEAT |
REPEAT |
||
a = a + 1 |
a = a + 1 |
||
PRINT a |
PRINT a |
||
UNTIL a MOD 6 = 0</ |
UNTIL a MOD 6 = 0</syntaxhighlight> |
||
==={{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}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 LET I=0 |
||
110 DO |
110 DO |
||
120 LET I=I+1 |
120 LET I=I+1 |
||
130 PRINT I |
130 PRINT I |
||
140 LOOP UNTIL MOD(I,6)=0</ |
140 LOOP UNTIL MOD(I,6)=0</syntaxhighlight> |
||
==={{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}}=== |
==={{header|Minimal BASIC}}=== |
||
Minimal BASIC does not have a <code>do .. while</code> construct. Equivalent using conditional jump: |
Minimal BASIC does not have a <code>do .. while</code> construct. Equivalent using conditional jump: |
||
< |
<syntaxhighlight lang="gwbasic"> |
||
10 REM Loops/Do-while |
10 REM Loops/Do-while |
||
20 LET I=0 |
20 LET I=0 |
||
Line 610: | Line 734: | ||
50 IF INT(I/6)*6 <> I THEN 30 |
50 IF INT(I/6)*6 <> I THEN 30 |
||
60 END |
60 END |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{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}}=== |
==={{header|QBasic}}=== |
||
Line 616: | Line 784: | ||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
{{works with|Just BASIC}} |
{{works with|Just BASIC}} |
||
< |
<syntaxhighlight lang="qbasic">a = 0 |
||
DO |
DO |
||
a = a + 1 |
a = a + 1 |
||
PRINT a; |
PRINT a; |
||
LOOP WHILE a MOD 6 <> 0</ |
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}}=== |
==={{header|Sinclair ZX81 BASIC}}=== |
||
< |
<syntaxhighlight lang="basic">10 LET X=0 |
||
20 LET X=X+1 |
20 LET X=X+1 |
||
30 PRINT X |
30 PRINT X |
||
40 IF X/6<>INT (X/6) THEN GOTO 20</ |
40 IF X/6<>INT (X/6) THEN GOTO 20</syntaxhighlight> |
||
==={{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}}=== |
==={{header|True BASIC}}=== |
||
< |
<syntaxhighlight lang="basic">LET i = 0 |
||
DO |
DO |
||
Line 636: | Line 842: | ||
LOOP WHILE REMAINDER(i, 6) <> 0 |
LOOP WHILE REMAINDER(i, 6) <> 0 |
||
PRINT |
PRINT |
||
END</ |
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}}=== |
==={{header|XBasic}}=== |
||
{{works with|Windows XBasic}} |
{{works with|Windows XBasic}} |
||
<syntaxhighlight lang="xbasic"> |
|||
<lang xbasic>PROGRAM "Do/While" |
|||
PROGRAM "dowhile" |
|||
VERSION "0.0001" |
|||
DECLARE FUNCTION |
DECLARE FUNCTION Entry() |
||
FUNCTION |
FUNCTION Entry() |
||
val% = 0 |
|||
DO |
DO |
||
INC val% |
|||
PRINT |
PRINT val% |
||
LOOP WHILE |
LOOP WHILE val% MOD 6 <> 0 ' or LOOP UNTIL val% MOD 6 = 0 |
||
END FUNCTION |
END FUNCTION |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
==={{Header|Tiny BASIC}}=== |
|||
<lang Tiny BASIC> REM TinyBasic does not have a do .. while construct. Equivalent using conditional jump: |
|||
LET i = 0 |
|||
10 LET i = i + 1 |
|||
PRINT i |
|||
IF (i / 6) * 6 <> i THEN GOTO 10 |
|||
END</lang> |
|||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
< |
<syntaxhighlight lang="yabasic">i = 0 |
||
repeat |
repeat |
||
Line 671: | Line 885: | ||
print i, " "; |
print i, " "; |
||
until mod(i, 6) = 0 |
until mod(i, 6) = 0 |
||
print</ |
print</syntaxhighlight> |
||
=={{header|bc}}== |
=={{header|bc}}== |
||
< |
<syntaxhighlight lang="bc">i = 0 |
||
for (;;) { |
for (;;) { |
||
++i /* increments then prints i */ |
++i /* increments then prints i */ |
||
if (i % 6 == 0) break |
if (i % 6 == 0) break |
||
} |
} |
||
quit</ |
quit</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
< |
<syntaxhighlight lang="befunge">0>1+:.v |
||
|%6: < |
|%6: < |
||
@</ |
@</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">int val = 0; |
||
do{ |
do{ |
||
val++; |
val++; |
||
printf("%d\n",val); |
printf("%d\n",val); |
||
}while(val % 6 != 0);</ |
}while(val % 6 != 0);</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">int a = 0; |
||
do |
do |
||
Line 701: | Line 915: | ||
a += 1; |
a += 1; |
||
Console.WriteLine(a); |
Console.WriteLine(a); |
||
} while (a % 6 != 0);</ |
} while (a % 6 != 0);</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">int val = 0; |
||
do{ |
do{ |
||
val++; |
val++; |
||
std::cout << val << std::endl; |
std::cout << val << std::endl; |
||
}while(val % 6 != 0);</ |
}while(val % 6 != 0);</syntaxhighlight> |
||
=={{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}}== |
=={{header|Chapel}}== |
||
< |
<syntaxhighlight lang="chapel">var val = 0; |
||
do { |
do { |
||
val += 1; |
val += 1; |
||
writeln(val); |
writeln(val); |
||
} while val % 6 > 0;</ |
} while val % 6 > 0;</syntaxhighlight> |
||
=={{header|ChucK}}== |
=={{header|ChucK}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
0 => int value; |
0 => int value; |
||
do |
do |
||
Line 726: | Line 949: | ||
} |
} |
||
while(value % 6 != 0); |
while(value % 6 != 0); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Clipper}}== |
=={{header|Clipper}}== |
||
< |
<syntaxhighlight lang="clipper"> Local n := 0 |
||
DO WHILE .T. |
DO WHILE .T. |
||
? ++n |
? ++n |
||
Line 735: | Line 958: | ||
EXIT |
EXIT |
||
ENDIF |
ENDIF |
||
ENDDO</ |
ENDDO</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(loop [i 0] |
||
(let [i* (inc i)] |
(let [i* (inc i)] |
||
(println i*) |
(println i*) |
||
(when-not (zero? (mod i* 6)) |
(when-not (zero? (mod i* 6)) |
||
(recur i*))))</ |
(recur i*))))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
The COBOL equivalent of a do-while loop is <code>PERFORM WITH TEST AFTER UNTIL some-condition</code>. |
The COBOL equivalent of a do-while loop is <code>PERFORM WITH TEST AFTER UNTIL some-condition</code>. |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. loop-do-while. |
PROGRAM-ID. loop-do-while. |
||
Line 760: | Line 983: | ||
GOBACK |
GOBACK |
||
.</ |
.</syntaxhighlight> |
||
=={{header|Coco}}== |
=={{header|Coco}}== |
||
Do-while loops are a JavaScript feature removed in CoffeeScript but re-added in Coco. |
Do-while loops are a JavaScript feature removed in CoffeeScript but re-added in Coco. |
||
< |
<syntaxhighlight lang="coco">v = 0 |
||
do |
do |
||
console.log ++v |
console.log ++v |
||
while v % 6</ |
while v % 6</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="coffeescript">val = 0 |
||
loop |
loop |
||
console.log ++val |
console.log ++val |
||
break unless val % 6</ |
break unless val % 6</syntaxhighlight> |
||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
< |
<syntaxhighlight lang="cfm"><cfscript> |
||
value = 0; |
value = 0; |
||
do |
do |
||
Line 785: | Line 1,008: | ||
writeOutput( value ); |
writeOutput( value ); |
||
} while( value % 6 != 0 ); |
} while( value % 6 != 0 ); |
||
</cfscript></ |
</cfscript></syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(let ((val 0)) |
||
(loop do |
(loop do |
||
(incf val) |
(incf val) |
||
(print val) |
(print val) |
||
while (/= 0 (mod val 6))))</ |
while (/= 0 (mod val 6))))</syntaxhighlight> |
||
loop can set up temporary values, and incf returns a value, so it's also possible to do |
loop can set up temporary values, and incf returns a value, so it's also possible to do |
||
< |
<syntaxhighlight lang="lisp">(loop with val = 0 |
||
do (print (incf val)) |
do (print (incf val)) |
||
until (= 0 (mod val 6)))</ |
until (= 0 (mod val 6)))</syntaxhighlight> |
||
=== Using DO === |
=== Using DO === |
||
< |
<syntaxhighlight lang="lisp"> |
||
(do* ((a 0) ; Initialize to 0 |
(do* ((a 0) ; Initialize to 0 |
||
(b (incf a) (incf b))) ; Set first increment and increment on every loop |
(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?) |
((zerop (mod b 6)) (print b)) ; Break condition and print last value `6' (right?) |
||
(print b)) ; On every loop print value |
(print b)) ; On every loop print value |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 819: | Line 1,042: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
void main() { |
void main() { |
||
Line 827: | Line 1,050: | ||
write(val, " "); |
write(val, " "); |
||
} while (val % 6 != 0); |
} while (val % 6 != 0); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 3 4 5 6 </pre> |
<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}}== |
=={{header|dc}}== |
||
{{trans|bc}} |
{{trans|bc}} |
||
< |
<syntaxhighlight lang="dc">0 si [i = 0]sz |
||
[2Q]sA [A = code to break loop]sz |
[2Q]sA [A = code to break loop]sz |
||
[ |
[ |
||
Line 840: | Line 1,073: | ||
6 % 0 =A [call A if 0 == it % 6]sz |
6 % 0 =A [call A if 0 == it % 6]sz |
||
0 0 =B [continue loop]sz |
0 0 =B [continue loop]sz |
||
]sB 0 0 =B</ |
]sB 0 0 =B</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program Loop; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 858: | Line 1,091: | ||
Writeln; |
Writeln; |
||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">proc nonrec main() void: |
||
byte i; |
byte i; |
||
i := 0; |
i := 0; |
||
Line 869: | Line 1,102: | ||
i % 6 ~= 0 |
i % 6 ~= 0 |
||
do od |
do od |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 2 3 4 5 6</pre> |
<pre> 1 2 3 4 5 6</pre> |
||
Line 875: | Line 1,108: | ||
=={{header|Dragon}}== |
=={{header|Dragon}}== |
||
< |
<syntaxhighlight lang="dragon">val = 0 |
||
do{ |
do{ |
||
val++ |
val++ |
||
showln val |
showln val |
||
}while(val % 6 != 0)</ |
}while(val % 6 != 0)</syntaxhighlight> |
||
=={{header|DUP}}== |
=={{header|DUP}}== |
||
Line 886: | Line 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): |
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): |
||
< |
<syntaxhighlight lang="dup">[1+$.' ,]⇒A {operator definition: PUSH 1, ADD, DUP, print top of stack to SDTOUT, print whitespace} |
||
[1+$.' ,]a: {function definition}</ |
[1+$.' ,]a: {function definition}</syntaxhighlight> |
||
and put the defined block in front of the while loop, and inside the while loop itself: |
and put the defined block in front of the while loop, and inside the while loop itself: |
||
Line 893: | Line 1,126: | ||
If the block was defined as an operator, the whole program would look like this (comments in curly braces): |
If the block was defined as an operator, the whole program would look like this (comments in curly braces): |
||
< |
<syntaxhighlight lang="dup">[1+$.' ,]⇒A |
||
0 A[$6/%][A]# {PUSH 0, execute operator A, [DUP, PUSH 6, MOD/DIV, POP][execute operator A]#}</ |
0 A[$6/%][A]# {PUSH 0, execute operator A, [DUP, PUSH 6, MOD/DIV, POP][execute operator A]#}</syntaxhighlight> |
||
And if the block is defined as a named function: |
And if the block is defined as a named function: |
||
< |
<syntaxhighlight lang="dup">[1+$.' ,]a: |
||
0 a;![$6/%][a;!]#</ |
0 a;![$6/%][a;!]#</syntaxhighlight> |
||
Result: |
Result: |
||
<lang |
<syntaxhighlight lang="dup">1 2 3 4 5 6</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
var i := 0; |
var i := 0; |
||
Line 913: | Line 1,146: | ||
PrintLn(i); |
PrintLn(i); |
||
until i mod 6 = 0; |
until i mod 6 = 0; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Bold text''' |
'''Bold text''' |
||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
< |
<syntaxhighlight lang="dyalect">var x = 0 |
||
do |
do |
||
Line 924: | Line 1,157: | ||
x += 1 |
x += 1 |
||
print(x) |
print(x) |
||
} while x % 6 != 0</ |
} while x % 6 != 0</syntaxhighlight> |
||
=={{header|E}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="e">var x := 0 |
||
__loop(fn { |
__loop(fn { |
||
x += 1 |
x += 1 |
||
println(x) |
println(x) |
||
x % 6 != 0 # this is the return value of the function |
x % 6 != 0 # this is the return value of the function |
||
})</ |
})</syntaxhighlight> |
||
<!-- XXX we should have an example of lambda-args sugar here --> |
<!-- 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}}== |
=={{header|Ela}}== |
||
< |
<syntaxhighlight lang="ela">open monad io |
||
loop n | n % 6 == 0 = do return () |
loop n | n % 6 == 0 = do return () |
||
Line 946: | Line 1,189: | ||
loop (n+1) |
loop (n+1) |
||
_ = loop 10 ::: IO</ |
_ = loop 10 ::: IO</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Loops do |
||
def do_while(n) do |
def do_while(n) do |
||
n1 = n + 1 |
n1 = n + 1 |
||
Line 958: | Line 1,201: | ||
end |
end |
||
Loops.do_while(0)</ |
Loops.do_while(0)</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="lisp">(let ((val 0)) |
||
(while (progn |
(while (progn |
||
(setq val (1+ val)) |
(setq val (1+ val)) |
||
(message "%d" val) |
(message "%d" val) |
||
(/= 0 (mod val 6)))))</ |
(/= 0 (mod val 6)))))</syntaxhighlight> |
||
Alternatively, the loop can be rewritten to check for the exit condition and fulfill it at the end of the loop body. |
Alternatively, the loop can be rewritten to check for the exit condition and fulfill it at the end of the loop body. |
||
< |
<syntaxhighlight lang="lisp">(let ((val 0) done) |
||
(while (not done) |
(while (not done) |
||
(setq val (1+ val)) |
(setq val (1+ val)) |
||
(message "%d" val) |
(message "%d" val) |
||
(setq done (zerop (mod val 6)))))</ |
(setq done (zerop (mod val 6)))))</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
do() -> |
do() -> |
||
do(0). |
do(0). |
||
Line 991: | Line 1,234: | ||
io:fwrite( "~p ", [N] ), |
io:fwrite( "~p ", [N] ), |
||
do(N+1). |
do(N+1). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
A=0 |
A=0 |
||
REPEAT |
REPEAT |
||
Line 1,000: | Line 1,243: | ||
PRINT(A) |
PRINT(A) |
||
UNTIL A MOD 6=0 !UNTIL A-6*INT(A/6)=0 for C-64 |
UNTIL A MOD 6=0 !UNTIL A-6*INT(A/6)=0 for C-64 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
{{works with|Open Euphoria}} |
{{works with|Open Euphoria}} |
||
< |
<syntaxhighlight lang="euphoria"> |
||
include std/console.e |
include std/console.e |
||
include std/math.e |
include std/math.e |
||
Line 1,017: | Line 1,260: | ||
if getc(0) then end if |
if getc(0) then end if |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
If you must have a loop then this is acceptable F# |
If you must have a loop then this is acceptable F# |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let rec loop n = |
let rec loop n = |
||
printfn "%d " n |
printfn "%d " n |
||
if (n+1)%6 > 0 then loop (n+1) |
if (n+1)%6 > 0 then loop (n+1) |
||
loop 0 |
loop 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
But I prefer this way: |
But I prefer this way: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
Seq.initInfinite id |> Seq.takeWhile(fun n->n=0 || n%6>0) |> Seq.iter (fun n-> printfn "%d" n) |
Seq.initInfinite id |> Seq.takeWhile(fun n->n=0 || n%6>0) |> Seq.iter (fun n-> printfn "%d" n) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Either produces: |
Either produces: |
||
Line 1,046: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Loops/Do-while. Nigel Galloway: February 14th., 2022 |
// 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") |
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> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,062: | Line 1,305: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">0 [ dup 6 mod 0 = not ] [ [ . ] [ 1 + ] bi ] do while drop</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
Line 1,068: | Line 1,311: | ||
There is no do-while statement in Fantom, so instead use an infinite while loop with a break statement: |
There is no do-while statement in Fantom, so instead use an infinite while loop with a break statement: |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 1,082: | Line 1,325: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: do-until |
||
0 |
0 |
||
begin 1+ |
begin 1+ |
||
Line 1,091: | Line 1,334: | ||
dup 6 mod 0= |
dup 6 mod 0= |
||
until |
until |
||
drop ;</ |
drop ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">INTEGER :: i = 0 |
||
DO |
DO |
||
i = i + 1 |
i = i + 1 |
||
WRITE(*, *) i |
WRITE(*, *) i |
||
IF (MOD(i, 6) == 0) EXIT |
IF (MOD(i, 6) == 0) EXIT |
||
END DO</ |
END DO</syntaxhighlight> |
||
{{works with|Fortran|77 and later}} |
{{works with|Fortran|77 and later}} |
||
< |
<syntaxhighlight lang="fortran"> PROGRAM DOWHILE |
||
C Initialize modulus and value. |
C Initialize modulus and value. |
||
INTEGER MODLUS, IVALUE |
INTEGER MODLUS, IVALUE |
||
Line 1,117: | Line 1,360: | ||
STOP |
STOP |
||
END</ |
END</syntaxhighlight> |
||
{{works with|Fortran|IV and later}} |
{{works with|Fortran|IV and later}} |
||
< |
<syntaxhighlight lang="fortran"> IVALUE = 0 |
||
10 CONTINUE |
10 CONTINUE |
||
IVALUE=IVALUE+1 |
IVALUE=IVALUE+1 |
||
Line 1,126: | Line 1,369: | ||
301 FORMAT(I5) |
301 FORMAT(I5) |
||
IF(MOD(IVALUE,6).NE.0) GOTO 10 |
IF(MOD(IVALUE,6).NE.0) GOTO 10 |
||
END</ |
END</syntaxhighlight> |
||
{{works with|Fortran|I and later}} |
{{works with|Fortran|I and later}} |
||
< |
<syntaxhighlight lang="fortran"> IVALUE = 0 |
||
10 IVALUE=IVALUE+1 |
10 IVALUE=IVALUE+1 |
||
WRITE 301,IVALUE |
WRITE 301,IVALUE |
||
Line 1,135: | Line 1,378: | ||
IF(IVALUE-IVALUE/6*6) 10,20,10 |
IF(IVALUE-IVALUE/6*6) 10,20,10 |
||
20 STOP |
20 STOP |
||
END</ |
END</syntaxhighlight> |
||
=={{header|Fortress}}== |
=={{header|Fortress}}== |
||
Line 1,141: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="fortress"> |
||
component loops_do_while |
component loops_do_while |
||
export Executable |
export Executable |
||
Line 1,157: | Line 1,400: | ||
end loop |
end loop |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,167: | Line 1,410: | ||
5 |
5 |
||
6 |
6 |
||
</pre> |
|||
=={{header|FreeBASIC}}== |
|||
<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</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 2 3 4 5 6 |
|||
</pre> |
</pre> |
||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">n = 0 |
||
do |
do |
||
{ |
{ |
||
n = n + 1 |
n = n + 1 |
||
println[n] |
println[n] |
||
} while n mod 6 != 0</ |
} while n mod 6 != 0</syntaxhighlight> |
||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
< |
<syntaxhighlight lang="futurebasic">window 1 |
||
dim as long i |
dim as long i |
||
Line 1,203: | Line 1,430: | ||
until ( i mod 6 == 0 ) |
until ( i mod 6 == 0 ) |
||
HandleEvents</ |
HandleEvents</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
1 |
1 |
||
Line 1,212: | Line 1,439: | ||
5 |
5 |
||
6 |
6 |
||
</pre> |
|||
=={{header|Gambas}}== |
|||
'''[https://gambas-playground.proko.eu/?gist=57e91eab60baf7e39df9b6d16a0deddd Click this link to run this code]''' |
|||
<lang gambas>Public Sub Main() |
|||
Dim siCount As Short |
|||
Repeat |
|||
Inc siCount |
|||
Print siCount;; |
|||
Until siCount Mod 6 = 0 |
|||
End</lang> |
|||
Output: |
|||
<pre> |
|||
1 2 3 4 5 6 |
|||
</pre> |
</pre> |
||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">n := 0; |
||
repeat |
repeat |
||
n := n + 1; |
n := n + 1; |
||
Print(n, "\n"); |
Print(n, "\n"); |
||
until RemInt(n, 6) = 0;</ |
until RemInt(n, 6) = 0;</syntaxhighlight> |
||
=={{header|GML}}== |
=={{header|GML}}== |
||
< |
<syntaxhighlight lang="gml">i = 0 |
||
do |
do |
||
{ |
{ |
||
Line 1,244: | Line 1,455: | ||
show_message(string(i)) |
show_message(string(i)) |
||
} |
} |
||
until (i mod 6 = 0)</ |
until (i mod 6 = 0)</syntaxhighlight> |
||
=={{header|Go}}== |
=={{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. |
There is no explicit do-while in Go, but it can be simulated with a range-based for loop and the break statement. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,261: | Line 1,472: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 1,270: | Line 1,481: | ||
6</pre> |
6</pre> |
||
It can also be simulated ''without'' using a break statement as follows: |
It can also be simulated ''without'' using a break statement as follows: |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,280: | Line 1,491: | ||
fmt.Println(value) |
fmt.Println(value) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,286: | Line 1,497: | ||
Same as before. |
Same as before. |
||
</pre> |
</pre> |
||
::< |
::<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,312: | Line 1,523: | ||
} |
} |
||
fmt.Println(n3) // prt 8 |
fmt.Println(n3) // prt 8 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
For Groovy 3.0.0 and later. |
For Groovy 3.0.0 and later. |
||
< |
<syntaxhighlight lang="groovy">def i = 0 |
||
do { |
do { |
||
i++ |
i++ |
||
println i |
println i |
||
} while (i % 6 != 0)</ |
} while (i % 6 != 0)</syntaxhighlight> |
||
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. |
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. |
||
< |
<syntaxhighlight lang="groovy">def i = 0 |
||
while (true) { |
while (true) { |
||
i++ |
i++ |
||
println i |
println i |
||
if (i % 6 == 0) break |
if (i % 6 == 0) break |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,336: | Line 1,547: | ||
5 |
5 |
||
6</pre> |
6</pre> |
||
=={{header|GW-BASIC}}== |
|||
GW-BASIC does not have a <code>do .. while</code> construct. |
|||
Equivalent using <code>WHILE</code>: |
|||
{{works with|PC-BASIC|any}} |
|||
<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 |
|||
</lang> |
|||
Equivalent using <code>GOTO</code>: |
|||
{{works with|PC-BASIC|any}} |
|||
<lang gwbasic> |
|||
10 LET I% = 0 |
|||
20 LET I% = I% + 1 |
|||
30 PRINT I% |
|||
40 IF I% MOD 6 <> 0 THEN GOTO 20 |
|||
</lang> |
|||
=={{header|Harbour}}== |
=={{header|Harbour}}== |
||
< |
<syntaxhighlight lang="visualfoxpro">LOCAL n := 0 |
||
DO WHILE .T. |
DO WHILE .T. |
||
Line 1,368: | Line 1,556: | ||
EXIT |
EXIT |
||
ENDIF |
ENDIF |
||
ENDDO</ |
ENDDO</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang="haskell">import Data.List |
|||
<lang haskell>import Data.List |
|||
import Control.Monad |
import Control.Monad |
||
import Control.Arrow |
import Control.Arrow |
||
doWhile p f n = (n:) $ takeWhile p $ unfoldr (Just.(id &&& f)) $ succ n</ |
doWhile p f n = (n:) $ takeWhile p $ unfoldr (Just.(id &&& f)) $ succ n</syntaxhighlight> |
||
Example executed in GHCi: |
Example executed in GHCi: |
||
< |
<syntaxhighlight lang="haskell">*Main> mapM_ print $ doWhile ((/=0).(`mod`6)) succ 0 |
||
0 |
0 |
||
1 |
1 |
||
Line 1,384: | Line 1,571: | ||
3 |
3 |
||
4 |
4 |
||
5</ |
5</syntaxhighlight> |
||
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. |
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. |
||
< |
<syntaxhighlight lang="haskell">main :: IO () |
||
main = |
main = |
||
mapM_ print . reverse $ |
mapM_ print . reverse $ |
||
Line 1,394: | Line 1,581: | ||
(\(x:_) -> (x > 0) && (mod x 6 == 0)) |
(\(x:_) -> (x > 0) && (mod x 6 == 0)) |
||
(\xs@(x:_) -> succ x : xs) |
(\xs@(x:_) -> succ x : xs) |
||
[0]</ |
[0]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,407: | Line 1,594: | ||
=== With mutable references === |
=== With mutable references === |
||
Using iterateWhile from monad-loops package |
Using iterateWhile from monad-loops package |
||
< |
<syntaxhighlight lang="haskell">import Data.IORef |
||
import Control.Monad.Loops |
import Control.Monad.Loops |
||
Line 1,416: | Line 1,603: | ||
val <- readIORef x |
val <- readIORef x |
||
print val |
print val |
||
return val</ |
return val</syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
< |
<syntaxhighlight lang="haxe">var val = 0; |
||
do { |
do { |
||
val++; |
val++; |
||
Sys.println(val); |
Sys.println(val); |
||
} while( val % 6 != 0);</ |
} while( val % 6 != 0);</syntaxhighlight> |
||
=={{header|HolyC}}== |
=={{header|HolyC}}== |
||
< |
<syntaxhighlight lang="holyc">U8 i = 0; |
||
do { |
do { |
||
i++; |
i++; |
||
Print("%d\n", i); |
Print("%d\n", i); |
||
} while (i % 6 != 0);</ |
} while (i % 6 != 0);</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{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.) |
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.) |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
i := 0 |
i := 0 |
||
Line 1,442: | Line 1,629: | ||
if i % 6 = 0 then break |
if i % 6 = 0 then break |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way: |
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=6|])>:)^:a: 0 |
||
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). |
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). |
||
< |
<syntaxhighlight lang="j">3 : 0 ] 0 |
||
NB. The 'st' in 'whilst' stands for 'skip test' |
NB. The 'st' in 'whilst' stands for 'skip test' |
||
Line 1,460: | Line 1,666: | ||
i.0 0 |
i.0 0 |
||
)</ |
)</syntaxhighlight> |
||
Though it's rare to see J code like this. |
Though it's rare to see J code like this. |
||
Line 1,466: | Line 1,672: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">int val = 0; |
||
do{ |
do{ |
||
val++; |
val++; |
||
System.out.println(val); |
System.out.println(val); |
||
}while(val % 6 != 0);</ |
}while(val % 6 != 0);</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===Javascript: Imperative=== |
===Javascript: Imperative=== |
||
< |
<syntaxhighlight lang="javascript">var val = 0; |
||
do { |
do { |
||
print(++val); |
print(++val); |
||
} while (val % 6);</ |
} while (val % 6);</syntaxhighlight> |
||
===Javascript: Functional=== |
===Javascript: Functional=== |
||
Line 1,488: | Line 1,694: | ||
:#and a conditional While function. |
:#and a conditional While function. |
||
< |
<syntaxhighlight lang="javascript">function doWhile(varValue, fnBody, fnTest) { |
||
'use strict'; |
'use strict'; |
||
var d = fnBody(varValue); // a transformed value |
var d = fnBody(varValue); // a transformed value |
||
Line 1,506: | Line 1,712: | ||
} |
} |
||
).join('\n') |
).join('\n') |
||
);</ |
);</syntaxhighlight> |
||
Output: |
Output: |
||
<syntaxhighlight lang="javascript">1 |
|||
<lang JavaScript>1 |
|||
2 |
2 |
||
3 |
3 |
||
4 |
4 |
||
5 |
5 |
||
6</ |
6</syntaxhighlight> |
||
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. |
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,520: | Line 1,726: | ||
So, for example, something like: |
So, for example, something like: |
||
< |
<syntaxhighlight lang="javascript">function range(m, n) { |
||
'use strict'; |
'use strict'; |
||
return Array.apply(null, Array(n - m + 1)).map( |
return Array.apply(null, Array(n - m + 1)).map( |
||
Line 1,547: | Line 1,753: | ||
} |
} |
||
).join('\n') |
).join('\n') |
||
);</ |
);</syntaxhighlight> |
||
Output: |
Output: |
||
<syntaxhighlight lang="javascript">1 |
|||
<lang JavaScript>1 |
|||
2 |
2 |
||
3 |
3 |
||
4 |
4 |
||
5</ |
5</syntaxhighlight> |
||
====ES6==== |
====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. |
A process or value of this kind might be better expressed (in functionally composed JavaScript) with an '''unfold''' or '''until''' function, returning a list. |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,606: | Line 1,811: | ||
return [result1, result2]; |
return [result1, result2]; |
||
})(); |
})(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="javascript">[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]</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: |
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"> |
|||
<lang JavaScript> |
|||
// generator with the do while loop |
// generator with the do while loop |
||
function* getValue(stop) { |
function* getValue(stop) { |
||
Line 1,634: | Line 1,838: | ||
printVal(gen, gen.next()); |
printVal(gen, gen.next()); |
||
})(); |
})(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
1 |
1 |
||
2 |
2 |
||
Line 1,643: | Line 1,847: | ||
5 |
5 |
||
6 |
6 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 1,649: | Line 1,853: | ||
In jq 1.4, the "recurse" built-in always emits the input value, and so to accomplish the task specified here, |
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: |
we shall define a control structure: "do_while(action; condition)" as follows: |
||
< |
<syntaxhighlight lang="jq"># Perform the action, then check the condition, etc |
||
def do_while( action; condition ): |
def do_while( action; condition ): |
||
def w: action | if (condition | not) then empty else ., w end; |
def w: action | if (condition | not) then empty else ., w end; |
||
w;</ |
w;</syntaxhighlight> |
||
'''The task:''' |
'''The task:''' |
||
< |
<syntaxhighlight lang="jq">0 | do_while( .+1; . % 6 != 0 )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
1 |
1 |
||
Line 1,665: | Line 1,869: | ||
Julia has no do-while construct. Here is one of several ways to implement do-while behavior. |
Julia has no do-while construct. Here is one of several ways to implement do-while behavior. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
julia> i = 0 |
julia> i = 0 |
||
0 |
0 |
||
Line 1,680: | Line 1,884: | ||
4 |
4 |
||
5 |
5 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Using a macro that mimics the classic C style do-while. |
Using a macro that mimics the classic C style do-while. |
||
Line 1,686: | Line 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>). |
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"> |
|||
<lang Julia> |
|||
julia> @eval macro $(:do)(block, when::Symbol, condition) |
julia> @eval macro $(:do)(block, when::Symbol, condition) |
||
when ≠ :when && error("@do expected `when` got `$s`") |
when ≠ :when && error("@do expected `when` got `$s`") |
||
Line 1,713: | Line 1,917: | ||
i = 4 |
i = 4 |
||
i = 5 |
i = 5 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Here is mostly the same macro, but with the conditional clause used first, which is arguably more readable. |
Here is mostly the same macro, but with the conditional clause used first, which is arguably more readable. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
julia> macro do_while(condition, block) |
julia> macro do_while(condition, block) |
||
quote |
quote |
||
Line 1,743: | Line 1,947: | ||
i = 4 |
i = 4 |
||
i = 5 |
i = 5 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 1,754: | Line 1,958: | ||
} |
} |
||
while (value % 6 != 0) |
while (value % 6 != 0) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,770: | Line 1,974: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(x = 0) |
||
while(#x % 6 > 0 || #x == 0) => {^ |
while(#x % 6 > 0 || #x == 0) => {^ |
||
++#x |
++#x |
||
'\r' // for formatting |
'\r' // for formatting |
||
^}</ |
^}</syntaxhighlight> |
||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
{def do_while |
{def do_while |
||
{def do_while.r |
{def do_while.r |
||
Line 1,790: | Line 1,994: | ||
{do_while 0} |
{do_while 0} |
||
-> 0 1 2 3 4 5 6 (end of loop) |
-> 0 1 2 3 4 5 6 (end of loop) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header| |
=={{header|Lang}}== |
||
Lang does not have a do-while loop. A simple loop like the example below can be used. |
|||
<lang lb> |
|||
<syntaxhighlight lang="lang"> |
|||
a = 0 |
|||
$i = 0 |
|||
do |
|||
loop { |
|||
a =a +1 |
|||
$i += 1 |
|||
print a |
|||
loop until ( a mod 6) = 0 |
|||
fn.println($i) |
|||
</lang> |
|||
if(!($i % 6)) { |
|||
See also [[#BASIC|BASIC]] |
|||
con.break |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
Lingo has no do..while, but here how this behavior can be implemented: |
Lingo has no do..while, but here how this behavior can be implemented: |
||
< |
<syntaxhighlight lang="lingo">i = 0 |
||
repeat while TRUE |
repeat while TRUE |
||
i = i+1 |
i = i+1 |
||
put i |
put i |
||
if i mod 6 = 0 then exit repeat |
if i mod 6 = 0 then exit repeat |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Lisaac}}== |
=={{header|Lisaac}}== |
||
< |
<syntaxhighlight lang="lisaac">+ val : INTEGER; |
||
{ |
{ |
||
val := val + 1; |
val := val + 1; |
||
Line 1,819: | Line 2,027: | ||
'\n'.print; |
'\n'.print; |
||
val % 6 != 0 |
val % 6 != 0 |
||
}.while_do { };</ |
}.while_do { };</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang="livecode">repeat while n mod 6 is not 0 or n is 0 |
||
add 1 to n |
add 1 to n |
||
put n |
put n |
||
end repeat</ |
end repeat</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">make "val 0 |
||
do.while [make "val :val + 1 print :val] [notequal? 0 modulo :val 6] |
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] |
do.until [make "val :val + 1 print :val] [equal? 0 modulo :val 6] |
||
Line 1,837: | Line 2,045: | ||
if notequal? 0 modulo :n 6 [my.loop :n] |
if notequal? 0 modulo :n 6 [my.loop :n] |
||
end |
end |
||
my.loop 0</ |
my.loop 0</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 1,843: | Line 2,051: | ||
Lua doesn't have a <code>do .. while</code> construct. |
Lua doesn't have a <code>do .. while</code> construct. |
||
< |
<syntaxhighlight lang="lua"> |
||
i=0 |
i=0 |
||
repeat |
repeat |
||
Line 1,849: | Line 2,057: | ||
print(i) |
print(i) |
||
until i%6 == 0 |
until i%6 == 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module checkit { |
Module checkit { |
||
x=0 |
x=0 |
||
\\ Do or Repeat |
\\ 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 { |
Do { |
||
x++ |
x++ |
||
print x, |
|||
} |
} when x mod 6>0 |
||
print |
|||
x=0 |
x=0 |
||
{ |
{ |
||
Line 1,867: | Line 2,083: | ||
if x mod 6<>0 Then loop ' set loop flag of current block to true |
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 |
\\ when block end check Loop flag and if true execute block again |
||
print x, |
|||
} |
} |
||
print |
|||
} |
} |
||
Checkit |
Checkit |
||
module Old_Style { |
|||
</lang> |
|||
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}} |
{{out}} |
||
<pre> |
<pre> |
||
1 2 3 4 5 6 |
|||
1 2 3 4 5 6 |
|||
1 2 3 4 5 6 |
|||
1 |
1 |
||
2 |
2 |
||
Line 1,890: | Line 2,139: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">val := 0: |
||
do |
do |
||
val := 1 + val; |
val := 1 + val; |
||
Line 1,897: | Line 2,146: | ||
break |
break |
||
end if; |
end if; |
||
end do:</ |
end do:</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="mathematica">value = 0; |
||
While[ |
While[ |
||
value++; |
value++; |
||
Print[value]; |
Print[value]; |
||
Mod[value,6]!=0 |
Mod[value,6]!=0 |
||
]</ |
]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab"> a=0; |
||
while (1) |
while (1) |
||
a = a+1; |
a = a+1; |
||
disp(a); |
disp(a); |
||
if (~mod(a,6)) break; end; |
if (~mod(a,6)) break; end; |
||
end; </ |
end; </syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">block([n: 0], do (ldisp(n: n + 1), if mod(n, 6) = 0 then return('done)))$</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<syntaxhighlight lang="maxscript">a = 0 |
||
do |
do |
||
( |
( |
||
Line 1,927: | Line 2,176: | ||
a += 1 |
a += 1 |
||
) |
) |
||
while mod a 6 != 0</ |
while mod a 6 != 0</syntaxhighlight> |
||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
Line 1,933: | Line 2,182: | ||
Metafont has no a do-while construct; the same thing can be done using a forever loop and exitif. |
Metafont has no a do-while construct; the same thing can be done using a forever loop and exitif. |
||
< |
<syntaxhighlight lang="metafont">a := 0; |
||
forever: show a; a := a + 1; exitif a mod 6 = 0; endfor |
forever: show a; a := a + 1; exitif a mod 6 = 0; endfor |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Microsoft Small Basic}}== |
|||
Microsoft Small Basic does not have a <code>do .. while</code> construct. |
|||
Equivalent using <code>While</code>: |
|||
<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 |
|||
</lang> |
|||
Equivalent using <code>Goto</code>: |
|||
<lang microsoftsmallbasic> |
|||
i = 0 |
|||
loopStart: |
|||
i = i + 1 |
|||
TextWindow.WriteLine(i) |
|||
If Math.Remainder(i, 6) <> 0 Then |
|||
Goto loopStart |
|||
EndIf |
|||
</lang> |
|||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.6}} |
{{works with|min|0.19.6}} |
||
< |
<syntaxhighlight lang="min">0 (dup 6 mod 0 == over 0 != and) 'pop (puts succ) () linrec</syntaxhighlight> |
||
=={{header|MIPS Assembly}}== |
=={{header|MIPS Assembly}}== |
||
<syntaxhighlight lang="mips"> |
|||
<lang mips> |
|||
.text |
.text |
||
main: li $s0, 0 # start at 0. |
main: li $s0, 0 # start at 0. |
||
Line 1,981: | Line 2,205: | ||
li $v0, 10 |
li $v0, 10 |
||
syscall # syscall to end the program |
syscall # syscall to end the program |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>0 П4 КИП4 ИП4 6 / {x} x=0 02 С/П</ |
<syntaxhighlight lang="text">0 П4 КИП4 ИП4 6 / {x} x=0 02 С/П</syntaxhighlight> |
||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE DoWhile; |
||
IMPORT InOut; |
IMPORT InOut; |
||
Line 2,000: | Line 2,224: | ||
InOut.WriteLn; |
InOut.WriteLn; |
||
UNTIL i MOD 6 = 0; |
UNTIL i MOD 6 = 0; |
||
END DoWhile.</ |
END DoWhile.</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
This is very similar to the [[Modula-2]] code above. |
This is very similar to the [[Modula-2]] code above. |
||
< |
<syntaxhighlight lang="modula3">REPEAT |
||
i := i + 1; |
i := i + 1; |
||
IO.Put(Fmt.Int(i)); |
IO.Put(Fmt.Int(i)); |
||
UNTIL i MOD 6 = 0;</ |
UNTIL i MOD 6 = 0;</syntaxhighlight> |
||
=={{header|Monicelli}}== |
=={{header|Monicelli}}== |
||
The do-while loop is the only kind of loop available in Monicelli |
The do-while loop is the only kind of loop available in Monicelli |
||
< |
<syntaxhighlight lang="monicelli"> |
||
stuzzica |
stuzzica |
||
... # loop body |
... # loop body |
||
e brematura anche, se <expr> # exit if <expr> is false |
e brematura anche, se <expr> # exit if <expr> is false |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
{{works with|Caché ObjectScript}} |
{{works with|Caché ObjectScript}} |
||
< |
<syntaxhighlight lang="mumps">DOWHILELOOP |
||
set val = 0 |
set val = 0 |
||
do { |
do { |
||
Line 2,026: | Line 2,250: | ||
} while ((val # 6) '= 0) |
} while ((val # 6) '= 0) |
||
quit</ |
quit</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
Line 2,039: | Line 2,263: | ||
=={{header|Neko}}== |
=={{header|Neko}}== |
||
<syntaxhighlight lang="actionscript">/** |
|||
<lang ActionScript>/** |
|||
Loops/Do-while in Neko |
Loops/Do-while in Neko |
||
Tectonics: |
Tectonics: |
||
Line 2,050: | Line 2,274: | ||
index += 1; |
index += 1; |
||
$print(index, "\n"); |
$print(index, "\n"); |
||
} while (index % 6) != 0</ |
} while (index % 6) != 0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,063: | Line 2,287: | ||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">mutable x = 0; |
||
do |
do |
||
{ |
{ |
||
x++; |
x++; |
||
WriteLine($"$x"); |
WriteLine($"$x"); |
||
} while (x % 6 != 0)</ |
} while (x % 6 != 0)</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 2,083: | Line 2,307: | ||
say i_ |
say i_ |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(let ((i 0)) |
||
(do-until (= 0 (% i 6)) |
(do-until (= 0 (% i 6)) |
||
(println (++ i))))</ |
(println (++ i))))</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{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: |
Nim does not have a do-while loop. The standard way to simulate it consists in an infinite loop with a break statement: |
||
< |
<syntaxhighlight lang="nim">var val = 0 |
||
while true: |
while true: |
||
inc val |
inc val |
||
echo val |
echo val |
||
if val mod 6 == 0: break</ |
if val mod 6 == 0: break</syntaxhighlight> |
||
It's also easy to write your own doWhile construction (but be aware that the instructions will be duplicated): |
It's also easy to write your own doWhile construction (but be aware that the instructions will be duplicated): |
||
< |
<syntaxhighlight lang="nim">template doWhile(a, b: untyped): untyped = |
||
b |
b |
||
while a: |
while a: |
||
Line 2,108: | Line 2,332: | ||
doWhile val mod 6 != 0: |
doWhile val mod 6 != 0: |
||
inc val |
inc val |
||
echo val</ |
echo val</syntaxhighlight> |
||
=={{header| |
=={{header|Nu}}== |
||
<syntaxhighlight lang="nu"> |
|||
<lang NS-HUBASIC>10 PRINT "NO,"A" ISN'T A MULTIPLE OF 6." |
|||
mut n = 0 |
|||
20 A=A+1 |
|||
while true { |
|||
30 IF A-(A/6)*6<>0 THEN GOTO 10 |
|||
$n += 1 |
|||
40 PRINT "YES, 6 IS A MULTIPLE OF 6."</lang> |
|||
print $n |
|||
if $n mod 6 == 0 {break} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Works with oo2c Version 2 |
Works with oo2c Version 2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE LoopDoWhile; |
MODULE LoopDoWhile; |
||
IMPORT |
IMPORT |
||
Line 2,137: | Line 2,365: | ||
Do |
Do |
||
END LoopDoWhile. |
END LoopDoWhile. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
i := 0; |
i := 0; |
||
do { |
do { |
||
Line 2,147: | Line 2,375: | ||
} |
} |
||
while (i % 6 <> 0); |
while (i % 6 <> 0); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
OCaml doesn't have a do-while loop, so we can just make a local loop: |
OCaml doesn't have a do-while loop, so we can just make a local loop: |
||
< |
<syntaxhighlight lang="ocaml">let rec loop i = |
||
let i = succ i in |
let i = succ i in |
||
Printf.printf "%d\n" i; |
Printf.printf "%d\n" i; |
||
Line 2,157: | Line 2,385: | ||
loop i |
loop i |
||
in |
in |
||
loop 0</ |
loop 0</syntaxhighlight> |
||
or implementing a generic do-while iterator with higher order function: |
or implementing a generic do-while iterator with higher order function: |
||
< |
<syntaxhighlight lang="ocaml">let do_while f p = |
||
let rec loop() = |
let rec loop() = |
||
f(); |
f(); |
||
Line 2,167: | Line 2,395: | ||
in |
in |
||
loop() |
loop() |
||
(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *)</ |
(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ocaml">let v = ref 0 in |
||
do_while (fun () -> incr v; Printf.printf "%d\n" !v) |
do_while (fun () -> incr v; Printf.printf "%d\n" !v) |
||
(fun () -> !v mod 6 <> 0)</ |
(fun () -> !v mod 6 <> 0)</syntaxhighlight> |
||
The example above is the an imperative form, below is its functional counterpart: |
The example above is the an imperative form, below is its functional counterpart: |
||
< |
<syntaxhighlight lang="ocaml">let do_while f p ~init = |
||
let rec loop v = |
let rec loop v = |
||
let v = f v in |
let v = f v in |
||
Line 2,186: | Line 2,414: | ||
(v)) |
(v)) |
||
(fun v -> v mod 6 <> 0) |
(fun v -> v mod 6 <> 0) |
||
~init:0</ |
~init:0</syntaxhighlight> |
||
Or in a very poor OCaml style, we can use an exception to exit a while loop: |
Or in a very poor OCaml style, we can use an exception to exit a while loop: |
||
< |
<syntaxhighlight lang="ocaml">let v = ref 0 |
||
exception Exit_loop |
exception Exit_loop |
||
try while true do |
try while true do |
||
Line 2,197: | Line 2,425: | ||
raise Exit_loop; |
raise Exit_loop; |
||
done |
done |
||
with Exit_loop -> ()</ |
with Exit_loop -> ()</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
The do-while can be changed into a do-until, just negating the condition of the while. |
The do-while can be changed into a do-until, just negating the condition of the while. |
||
< |
<syntaxhighlight lang="octave">val = 0; |
||
do |
do |
||
val++; |
val++; |
||
disp(val) |
disp(val) |
||
until( mod(val, 6) == 0 )</ |
until( mod(val, 6) == 0 )</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">0 doWhile: [ 1+ dup . dup 6 rem 0 <> ] drop</syntaxhighlight> |
||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
< |
<syntaxhighlight lang="progress">DEFINE VARIABLE ii AS INTEGER. |
||
DO WHILE ii MODULO 6 <> 0 OR ii = 0: |
DO WHILE ii MODULO 6 <> 0 OR ii = 0: |
||
ii = ii + 1. |
ii = ii + 1. |
||
MESSAGE ii VIEW-AS ALERT-BOX. |
MESSAGE ii VIEW-AS ALERT-BOX. |
||
END.</ |
END.</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
Normal Oz variables are single-assignment only. So we use a "cell", which is a one-element mutable container. |
Normal Oz variables are single-assignment only. So we use a "cell", which is a one-element mutable container. |
||
< |
<syntaxhighlight lang="oz">declare |
||
I = {NewCell 0} |
I = {NewCell 0} |
||
in |
in |
||
Line 2,227: | Line 2,455: | ||
I := @I + 1 |
I := @I + 1 |
||
{Show @I} |
{Show @I} |
||
end</ |
end</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{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. |
The generic Pari loops (<code>while</code>, <code>until</code>) test at the beginning, so just use an infinite loop with a break. |
||
< |
<syntaxhighlight lang="parigp">x = 0; |
||
while(1, |
while(1, |
||
print(x++); |
print(x++); |
||
if(x % 6 == 0, break) |
if(x % 6 == 0, break) |
||
)</ |
)</syntaxhighlight> |
||
If the loop body is something simple then it might be worked into the loop condition. This is obscure but compact. |
If the loop body is something simple then it might be worked into the loop condition. This is obscure but compact. |
||
< |
<syntaxhighlight lang="parigp">x = 0; |
||
while (print(x++) || x % 6, )</ |
while (print(x++) || x % 6, )</syntaxhighlight> |
||
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. |
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}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program countto6(output); |
||
var |
var |
||
Line 2,256: | Line 2,484: | ||
writeln(i) |
writeln(i) |
||
until i mod 6 = 0 |
until i mod 6 = 0 |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
begin |
|||
var a := 0; |
|||
repeat |
|||
a += 1; |
|||
Print(a); |
|||
until a mod 6 = 0; |
|||
end. |
|||
</syntaxhighlight> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">my $val = 0; |
||
do { |
do { |
||
$val++; |
$val++; |
||
print "$val\n"; |
print "$val\n"; |
||
} while ($val % 6);</ |
} while ($val % 6);</syntaxhighlight> |
||
<code>do ... until (''condition'')</code> is equivalent to <code>do ... while (not ''condition'')</code>. |
<code>do ... until (''condition'')</code> is equivalent to <code>do ... while (not ''condition'')</code>. |
||
< |
<syntaxhighlight lang="perl">my $val = 0; |
||
do { |
do { |
||
$val++; |
$val++; |
||
print "$val\n"; |
print "$val\n"; |
||
} until ($val % 6 == 0);</ |
} until ($val % 6 == 0);</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="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: #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> |
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
||
Line 2,279: | Line 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;">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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{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}}== |
=={{header|PHL}}== |
||
< |
<syntaxhighlight lang="phl">var i = 0; |
||
do { |
do { |
||
i = i::inc; |
i = i::inc; |
||
printf("%i\n", i); |
printf("%i\n", i); |
||
} while (i%6 != 0);</ |
} while (i%6 != 0);</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">$val = 0; |
||
do { |
do { |
||
$val++; |
$val++; |
||
print "$val\n"; |
print "$val\n"; |
||
} while ($val % 6 != 0);</ |
} while ($val % 6 != 0);</syntaxhighlight> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
===do while loop=== |
===do while loop=== |
||
< |
<syntaxhighlight lang="picat">go => |
||
N = 0, |
N = 0, |
||
do |
do |
||
N := N+1, |
N := N+1, |
||
println(N) |
println(N) |
||
while (N mod 6 != 0).</ |
while (N mod 6 != 0).</syntaxhighlight> |
||
===Recursion=== |
===Recursion=== |
||
< |
<syntaxhighlight lang="picat">go2 => |
||
do_while(0). |
do_while(0). |
||
Line 2,313: | Line 2,567: | ||
println(N1), |
println(N1), |
||
N1 mod 6 != 0, |
N1 mod 6 != 0, |
||
do_while(N1).</ |
do_while(N1).</syntaxhighlight> |
||
Both outputs the same. |
Both outputs the same. |
||
Line 2,327: | Line 2,581: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Literally: |
Literally: |
||
< |
<syntaxhighlight lang="picolisp">(let Val 0 |
||
(loop |
(loop |
||
(println (inc 'Val)) |
(println (inc 'Val)) |
||
(T (=0 (% Val 6))) ) )</ |
(T (=0 (% Val 6))) ) )</syntaxhighlight> |
||
Shorter: |
Shorter: |
||
< |
<syntaxhighlight lang="picolisp">(let Val 0 |
||
(until (=0 (% (println (inc 'Val)) 6))) )</ |
(until (=0 (% (println (inc 'Val)) 6))) )</syntaxhighlight> |
||
or: |
or: |
||
< |
<syntaxhighlight lang="picolisp">(for (Val 0 (n0 (% (println (inc 'Val)) 6))))</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">int main(){ |
||
int value = 0; |
int value = 0; |
||
do { |
do { |
||
Line 2,344: | Line 2,598: | ||
write(value + "\n"); |
write(value + "\n"); |
||
} while (value % 6); |
} while (value % 6); |
||
}</ |
}</syntaxhighlight> |
||
=={{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}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli"> |
||
dcl value fixed bin (31) init (0); |
dcl value fixed bin (31) init (0); |
||
do forever; |
do forever; |
||
Line 2,357: | Line 2,636: | ||
put list (value); |
put list (value); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
or shorter: |
or shorter: |
||
< |
<syntaxhighlight lang="pli"> |
||
dcl value fixed bin(31) init(0); |
dcl value fixed bin(31) init(0); |
||
do Until(value=6); |
do Until(value=6); |
||
value+=1; |
value+=1; |
||
put Skip list(value); |
put Skip list(value); |
||
end;</ |
end;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,377: | Line 2,656: | ||
=={{header|Plain English}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="plainenglish">To run: |
||
Start up. |
Start up. |
||
Demonstrate do-while. |
Demonstrate do-while. |
||
Line 2,388: | Line 2,667: | ||
Write the string on the console. |
Write the string on the console. |
||
If the counter is evenly divisible by 6, exit. |
If the counter is evenly divisible by 6, exit. |
||
Repeat.</ |
Repeat.</syntaxhighlight> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
< |
<syntaxhighlight lang="pop11">lvars val = 0; |
||
while true do |
while true do |
||
val + 1 -> val; |
val + 1 -> val; |
||
printf(val, '%p\n'); |
printf(val, '%p\n'); |
||
quitif(val rem 6 = 0); |
quitif(val rem 6 = 0); |
||
endwhile;</ |
endwhile;</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">$n = 0 |
||
do { |
do { |
||
$n++ |
$n++ |
||
$n |
$n |
||
} while ($n % 6 -ne 0)</ |
} while ($n % 6 -ne 0)</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang="prolog"> |
||
% initial condition |
% initial condition |
||
do(0):- write(0),nl,do(1). |
do(0):- write(0),nl,do(1). |
||
Line 2,422: | Line 2,701: | ||
do(0). |
do(0). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
|||
{{works with|PureBasic|4.41}} |
|||
<lang PureBasic>x=0 |
|||
Repeat |
|||
x+1 |
|||
Debug x |
|||
Until x%6=0</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Python doesn't have a do-while loop. |
Python doesn't have a do-while loop. |
||
< |
<syntaxhighlight lang="python">val = 0 |
||
while True: |
while True: |
||
val +=1 |
val +=1 |
||
print val |
print val |
||
if val % 6 == 0: break</ |
if val % 6 == 0: break</syntaxhighlight> |
||
or repeat the body of the loop before a standard while. |
or repeat the body of the loop before a standard while. |
||
< |
<syntaxhighlight lang="python">val = 1 |
||
print val |
print val |
||
while val % 6 != 0: |
while val % 6 != 0: |
||
val += 1 |
val += 1 |
||
print val</ |
print val</syntaxhighlight> |
||
=={{header|QB64}}== |
|||
''CBTJD'': 2020/03/14 |
|||
<lang qbasic>DO |
|||
PRINT n |
|||
n = n + 1 |
|||
LOOP WHILE n MOD 6 <> 0</lang> |
|||
<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 |
|||
</lang> |
|||
=={{header|Quackery}}== |
=={{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>. |
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>. |
||
< |
<syntaxhighlight lang="quackery">0 [ 1+ dup echo cr |
||
dup 6 mod 0 = until ] drop</ |
dup 6 mod 0 = until ] drop</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">i <- 0 |
||
repeat |
repeat |
||
{ |
{ |
||
Line 2,485: | Line 2,729: | ||
print(i) |
print(i) |
||
if(i %% 6 == 0) break |
if(i %% 6 == 0) break |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Idiomatic Racket code is functional: |
Idiomatic Racket code is functional: |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(let loop ([n 0]) |
(let loop ([n 0]) |
||
Line 2,496: | Line 2,739: | ||
(displayln n) |
(displayln n) |
||
(unless (zero? (modulo n 6)) (loop n)))) |
(unless (zero? (modulo n 6)) (loop n)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
But an imperative version is easy to do too: |
But an imperative version is easy to do too: |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(define n 0) |
(define n 0) |
||
Line 2,506: | Line 2,749: | ||
(displayln n) |
(displayln n) |
||
(unless (zero? (modulo n 6)) (loop))) |
(unless (zero? (modulo n 6)) (loop))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,512: | Line 2,755: | ||
{{works with|Rakudo Star|2010.08}} |
{{works with|Rakudo Star|2010.08}} |
||
<lang |
<syntaxhighlight lang="raku" line>my $val = 0; |
||
repeat { |
repeat { |
||
say ++$val; |
say ++$val; |
||
} while $val % 6;</ |
} while $val % 6;</syntaxhighlight> |
||
<code>repeat ... until ''condition''</code> is equivalent to <code>do ... while not ''condition''</code>. |
<code>repeat ... until ''condition''</code> is equivalent to <code>do ... while not ''condition''</code>. |
||
<lang |
<syntaxhighlight lang="raku" line>my $val = 0; |
||
repeat { |
repeat { |
||
say ++$val; |
say ++$val; |
||
} until $val %% 6;</ |
} until $val %% 6;</syntaxhighlight> |
||
(Here we've used <code>%%</code>, the "divisible-by" operator.) |
(Here we've used <code>%%</code>, the "divisible-by" operator.) |
||
<p> |
<p> |
||
You can also put the condition before the block, without changing the order of evaluation. |
You can also put the condition before the block, without changing the order of evaluation. |
||
<lang |
<syntaxhighlight lang="raku" line>my $val = 0; |
||
repeat while $val % 6 { |
repeat while $val % 6 { |
||
say ++$val; |
say ++$val; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Loop/While" |
Title: "Loop/While" |
||
URL: http://rosettacode.org/wiki/Loop/Do_While |
URL: http://rosettacode.org/wiki/Loop/Do_While |
||
Line 2,547: | Line 2,790: | ||
0 = mod value 6 |
0 = mod value 6 |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,558: | Line 2,801: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">Red [] |
||
i: 0 |
i: 0 |
||
until [ |
until [ |
||
Line 2,565: | Line 2,808: | ||
i % 6 = 0 ;; loop , until this is true... |
i % 6 = 0 ;; loop , until this is true... |
||
] |
] |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre>i: 0 |
<pre>i: 0 |
||
i: 1 |
i: 1 |
||
Line 2,583: | Line 2,826: | ||
<br>This necessitates the use of '''DO UNTIL''' instead of '''DO WHILE'''. |
<br>This necessitates the use of '''DO UNTIL''' instead of '''DO WHILE'''. |
||
===version 1=== |
===version 1=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program demonstrates a DO UNTIL construction. */ |
||
v=0 |
v=0 |
||
do until v//6==0 /*REXX // is the ÷ remainder.*/ |
do until v//6==0 /*REXX // is the ÷ remainder.*/ |
||
Line 2,589: | Line 2,832: | ||
say v |
say v |
||
end |
end |
||
/*stick a fork in it, we're done.*/</ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,601: | Line 2,844: | ||
===version 2=== |
===version 2=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program demonstrates a DO UNTIL construction. */ |
||
do v=1 until v//6==0 /*REXX // is the ÷ remainder.*/ |
do v=1 until v//6==0 /*REXX // is the ÷ remainder.*/ |
||
say v |
say v |
||
end |
end |
||
/*stick a fork in it, we're done.*/</ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
'''output''' is the same as the 1<sup>st</sup> version.<br> |
'''output''' is the same as the 1<sup>st</sup> version.<br> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
n = 0 |
n = 0 |
||
While True |
While True |
||
Line 2,616: | Line 2,859: | ||
if n % 6 = 0 exit ok |
if n % 6 = 0 exit ok |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{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}}== |
=={{header|Ruby}}== |
||
Line 2,625: | Line 2,877: | ||
! until |
! until |
||
|- |
|- |
||
| < |
| <syntaxhighlight lang="ruby">val = 0 |
||
begin |
begin |
||
val += 1 |
val += 1 |
||
puts val |
puts val |
||
end while val % 6 != 0</ |
end while val % 6 != 0</syntaxhighlight> |
||
| < |
| <syntaxhighlight lang="ruby">val = 0 |
||
begin |
begin |
||
val += 1 |
val += 1 |
||
puts val |
puts val |
||
end until val % 6 == 0</ |
end until val % 6 == 0</syntaxhighlight> |
||
|} |
|} |
||
During November 2005, Yukihiro Matsumoto, the creator of Ruby, [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6741 regretted this loop feature] and [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6745 suggested using Kernel#loop]. |
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" |
{| class="wikitable" |
||
Line 2,643: | Line 2,895: | ||
! break if |
! break if |
||
|- |
|- |
||
| < |
| <syntaxhighlight lang="ruby">val = 0 |
||
loop do |
loop do |
||
val += 1 |
val += 1 |
||
puts val |
puts val |
||
break unless val %6 != 0 |
break unless val %6 != 0 |
||
end</ |
end</syntaxhighlight> |
||
| < |
| <syntaxhighlight lang="ruby">val = 0 |
||
loop do |
loop do |
||
val += 1 |
val += 1 |
||
puts val |
puts val |
||
break if val %6 == 0 |
break if val %6 == 0 |
||
end</ |
end</syntaxhighlight> |
||
|} |
|} |
||
Line 2,663: | Line 2,915: | ||
Rust does not have a <tt>do...while</tt> loop. Instead, the keyword <tt>loop</tt> is used with a termination condition. |
Rust does not have a <tt>do...while</tt> loop. Instead, the keyword <tt>loop</tt> is used with a termination condition. |
||
< |
<syntaxhighlight lang="rust">let mut x = 0; |
||
loop { |
loop { |
||
Line 2,670: | Line 2,922: | ||
if x % 6 == 0 { break; } |
if x % 6 == 0 { break; } |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Salmon}}== |
=={{header|Salmon}}== |
||
< |
<syntaxhighlight lang="salmon">variable x := 0; |
||
do |
do |
||
{ |
{ |
||
Line 2,679: | Line 2,931: | ||
x! |
x! |
||
} |
} |
||
while (x % 6 != 0);</ |
while (x % 6 != 0);</syntaxhighlight> |
||
=={{header|SAS}}== |
=={{header|SAS}}== |
||
< |
<syntaxhighlight lang="sas">/* using DO UNTIL so that the loop executes at least once */ |
||
data _null_; |
data _null_; |
||
n=0; |
n=0; |
||
Line 2,689: | Line 2,941: | ||
put n; |
put n; |
||
end; |
end; |
||
run;</ |
run;</syntaxhighlight> |
||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="sather">class MAIN is |
||
main is |
main is |
||
val ::= 0; |
val ::= 0; |
||
Line 2,702: | Line 2,954: | ||
end; |
end; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{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}}== |
=={{header|Scala}}== |
||
Line 2,708: | Line 2,984: | ||
===Imperative=== |
===Imperative=== |
||
< |
<syntaxhighlight lang="scala"> { |
||
var (x, l) = (0, List[Int]()) |
var (x, l) = (0, List[Int]()) |
||
do { |
do { |
||
Line 2,716: | Line 2,992: | ||
l |
l |
||
}.foreach(println(_)) |
}.foreach(println(_)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Tail recursive=== |
===Tail recursive=== |
||
< |
<syntaxhighlight lang="scala"> def loop(iter: Int, cond: (Int) => Boolean, accu: List[Int]): List[Int] = { |
||
val succ = iter + 1 |
val succ = iter + 1 |
||
val temp = accu :+ succ |
val temp = accu :+ succ |
||
if (cond(succ)) loop(succ, cond, temp) else temp |
if (cond(succ)) loop(succ, cond, temp) else temp |
||
} |
} |
||
println(loop(0, (_ % 6 != 0), Nil))</ |
println(loop(0, (_ % 6 != 0), Nil))</syntaxhighlight> |
||
===Stream=== |
===Stream=== |
||
< |
<syntaxhighlight lang="scala"> def loop(i: Int, cond: (Int) => Boolean): Stream[Int] = { |
||
val succ = i + 1; |
val succ = i + 1; |
||
succ #:: (if (cond(succ)) loop(succ, cond) else Stream.empty) |
succ #:: (if (cond(succ)) loop(succ, cond) else Stream.empty) |
||
} |
} |
||
loop(0, (_ % 6 != 0)).foreach(println(_))</ |
loop(0, (_ % 6 != 0)).foreach(println(_))</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(let loop ((i 1)) |
||
(display i) |
(display i) |
||
(if (positive? (modulo i 6)) |
(if (positive? (modulo i 6)) |
||
(loop (+ i 1))))</ |
(loop (+ i 1))))</syntaxhighlight> |
||
=={{header|Scilab}}== |
=={{header|Scilab}}== |
||
{{works with|Scilab|5.5.1}} |
{{works with|Scilab|5.5.1}} |
||
<lang>v=0 |
<syntaxhighlight lang="text">v=0 |
||
while %T |
while %T |
||
v=v+1 |
v=v+1 |
||
Line 2,747: | Line 3,023: | ||
if modulo(v,6)==0 then break; end |
if modulo(v,6)==0 then break; end |
||
end |
end |
||
printf("\n")</ |
printf("\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 2 3 4 5 6 </pre> |
<pre> 1 2 3 4 5 6 </pre> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 2,762: | Line 3,038: | ||
writeln(number) |
writeln(number) |
||
until number rem 6 = 0 |
until number rem 6 = 0 |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var value = 0; |
||
do { |
do { |
||
say ++value; |
say ++value; |
||
} while (value % 6);</ |
} while (value % 6);</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">[| val | |
||
val: 0. |
val: 0. |
||
[val: val + 1. |
[val: val + 1. |
||
print: val. |
print: val. |
||
val \\ 6 ~= 0] whileTrue |
val \\ 6 ~= 0] whileTrue |
||
] do.</ |
] do.</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang="smalltalk">|val| |
||
val := 0. |
val := 0. |
||
[ |
[ |
||
val := val + 1. |
val := val + 1. |
||
val displayNl. |
val displayNl. |
||
] doWhile: [ (val rem: 6) ~= 0 ]</ |
] doWhile: [ (val rem: 6) ~= 0 ]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="smalltalk">|val| |
||
val := 0. |
val := 0. |
||
[ |
[ |
||
val := val + 1. |
val := val + 1. |
||
val displayNl. |
val displayNl. |
||
] doUntil: [ (val rem: 6) == 0 ]</ |
] doUntil: [ (val rem: 6) == 0 ]</syntaxhighlight> |
||
To simulate the do-while construct, we can use the |
To simulate the do-while construct, we can use the |
||
<tt>whileTrue:</tt> method of a block with a void while block. |
<tt>whileTrue:</tt> method of a block with a void while block. |
||
< |
<syntaxhighlight lang="smalltalk">|val| |
||
val := 0. |
val := 0. |
||
[ |
[ |
||
Line 2,802: | Line 3,078: | ||
val displayNl. |
val displayNl. |
||
(val rem: 6) ~= 0 |
(val rem: 6) ~= 0 |
||
] whileTrue: [ ]</ |
] whileTrue: [ ]</syntaxhighlight> |
||
Or send the loop block a <tt>whileTrue</tt> message (without argument). |
Or send the loop block a <tt>whileTrue</tt> message (without argument). |
||
< |
<syntaxhighlight lang="smalltalk">|val| |
||
val := 0. |
val := 0. |
||
[ |
[ |
||
Line 2,811: | Line 3,087: | ||
val displayNl. |
val displayNl. |
||
(val rem: 6) ~= 0 |
(val rem: 6) ~= 0 |
||
] whileTrue</ |
] whileTrue</syntaxhighlight> |
||
Corresponding false-checking messages are <tt>whileFalse:</tt> and <tt>whileFalse</tt> (without argument) |
Corresponding false-checking messages are <tt>whileFalse:</tt> and <tt>whileFalse</tt> (without argument) |
||
=={{header|Sparkling}}== |
=={{header|Sparkling}}== |
||
< |
<syntaxhighlight lang="sparkling">var i = 0; |
||
do { |
do { |
||
print(++i); |
print(++i); |
||
} while (i % 6 != 0);</ |
} while (i % 6 != 0);</syntaxhighlight> |
||
=={{header|Spin}}== |
=={{header|Spin}}== |
||
Line 2,826: | Line 3,102: | ||
{{works with|HomeSpun}} |
{{works with|HomeSpun}} |
||
{{works with|OpenSpin}} |
{{works with|OpenSpin}} |
||
< |
<syntaxhighlight lang="spin">con |
||
_clkmode = xtal1 + pll16x |
_clkmode = xtal1 + pll16x |
||
_clkfreq = 80_000_000 |
_clkfreq = 80_000_000 |
||
Line 2,845: | Line 3,121: | ||
waitcnt(_clkfreq + cnt) |
waitcnt(_clkfreq + cnt) |
||
ser.stop |
ser.stop |
||
cogstop(0)</ |
cogstop(0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 3 4 5 6 </pre> |
<pre>1 2 3 4 5 6 </pre> |
||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
< |
<syntaxhighlight lang="spl">n = 0 |
||
> |
> |
||
n += 1 |
n += 1 |
||
#.output(n) |
#.output(n) |
||
< n%6</ |
< n%6</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,870: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="stata">local n 0 |
||
local q 1 |
local q 1 |
||
while `q' | mod(`n',6) { |
while `q' | mod(`n',6) { |
||
local q 0 |
local q 0 |
||
di `++n' |
di `++n' |
||
}</ |
}</syntaxhighlight> |
||
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. |
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. |
||
< |
<syntaxhighlight lang="stata">local n 0 |
||
while 1 { |
while 1 { |
||
di `++n' |
di `++n' |
||
if mod(`n',6)==0 continue, break |
if mod(`n',6)==0 continue, break |
||
}</ |
}</syntaxhighlight> |
||
=== Mata === |
=== Mata === |
||
Mata has a '''[https://www.stata.com/help.cgi?m2_do do/while]''' loop: |
Mata has a '''[https://www.stata.com/help.cgi?m2_do do/while]''' loop: |
||
<lang>mata |
<syntaxhighlight lang="text">mata |
||
n=0 |
n=0 |
||
do { |
do { |
||
printf("%f\n",++n) |
printf("%f\n",++n) |
||
} while (mod(n,6)) |
} while (mod(n,6)) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Suneido}}== |
=={{header|Suneido}}== |
||
< |
<syntaxhighlight lang="suneido">val = 0 |
||
do |
do |
||
{ |
{ |
||
Print(++val) |
Print(++val) |
||
} while (val % 6 isnt 0)</ |
} while (val % 6 isnt 0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,912: | Line 3,188: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{works with|Swift|3.x+}} |
{{works with|Swift|3.x+}} |
||
< |
<syntaxhighlight lang="swift">var val = 0 |
||
repeat { |
repeat { |
||
val += 1 |
val += 1 |
||
print(val) |
print(val) |
||
} while val % 6 != 0</ |
} while val % 6 != 0</syntaxhighlight> |
||
{{works with|Swift|2.x}} |
{{works with|Swift|2.x}} |
||
< |
<syntaxhighlight lang="swift">var val = 0 |
||
repeat { |
repeat { |
||
val++ |
val++ |
||
print(val) |
print(val) |
||
} while val % 6 != 0</ |
} while val % 6 != 0</syntaxhighlight> |
||
{{works with|Swift|1.x}} |
{{works with|Swift|1.x}} |
||
< |
<syntaxhighlight lang="swift">var val = 0 |
||
do { |
do { |
||
val++ |
val++ |
||
println(val) |
println(val) |
||
} while val % 6 != 0</ |
} while val % 6 != 0</syntaxhighlight> |
||
=={{header|Tailspin}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="tailspin"> |
||
templates doWhile |
templates doWhile |
||
0 -> # |
0 -> # |
||
Line 2,939: | Line 3,215: | ||
$val -> \(<?($ mod 6 <~=0>)> $!\) -> # |
$val -> \(<?($ mod 6 <~=0>)> $!\) -> # |
||
end doWhile |
end doWhile |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{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] |
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] |
||
< |
<syntaxhighlight lang="tcl">proc do {body keyword expression} { |
||
if {$keyword eq "while"} { |
if {$keyword eq "while"} { |
||
set expression "!($expression)" |
set expression "!($expression)" |
||
Line 2,960: | Line 3,236: | ||
set i 0 |
set i 0 |
||
do {puts [incr i]} while {$i % 6 != 0}</ |
do {puts [incr i]} while {$i % 6 != 0}</syntaxhighlight> |
||
{{tcllib|control}} |
{{tcllib|control}} |
||
< |
<syntaxhighlight 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]} while {$i % 6 != 0} |
||
set i 0; control::do {puts [incr i]} until {$i % 6 == 0}</ |
set i 0; control::do {puts [incr i]} until {$i % 6 == 0}</syntaxhighlight> |
||
Mind you, it is also normal to write this task using a normal <code>while</code> as: |
Mind you, it is also normal to write this task using a normal <code>while</code> as: |
||
< |
<syntaxhighlight lang="tcl">set i 0 |
||
while true { |
while true { |
||
puts [incr i] |
puts [incr i] |
||
if {$i % 6 == 0} break |
if {$i % 6 == 0} break |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
var=0 |
var=0 |
||
Line 2,982: | Line 3,258: | ||
IF (rest==0) EXIT |
IF (rest==0) EXIT |
||
ENDLOOP |
ENDLOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,997: | Line 3,273: | ||
{{works with|pdksh}} |
{{works with|pdksh}} |
||
{{works with|zsh}} |
{{works with|zsh}} |
||
< |
<syntaxhighlight lang="bash">val=0 |
||
while true; do |
while true; do |
||
echo $((++val)) |
echo $((++val)) |
||
[ $((val%6)) -eq 0 ] && break |
[ $((val%6)) -eq 0 ] && break |
||
done</ |
done</syntaxhighlight> |
||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">val=0 |
||
while true; do |
while true; do |
||
val=`expr $val + 1` |
val=`expr $val + 1` |
||
echo $val |
echo $val |
||
expr $val % 6 = 0 >/dev/null && break |
expr $val % 6 = 0 >/dev/null && break |
||
done</ |
done</syntaxhighlight> |
||
{{works with|zsh}} |
{{works with|zsh}} |
||
< |
<syntaxhighlight lang="bash">for ((val=1;;val++)) { |
||
print $val |
print $val |
||
(( val % 6 )) || break |
(( val % 6 )) || break |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang="vala"> int a = 0; |
||
do { |
do { |
||
Line 3,025: | Line 3,301: | ||
} |
} |
||
while ( a % 6 != 0); |
while ( a % 6 != 0); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|VBA}}== |
|||
<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</lang>{{out}}<pre> 1 2 3 4 5 6 </pre> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
< |
<syntaxhighlight lang="vedit">#1 = 0 |
||
do { |
do { |
||
#1++ |
#1++ |
||
Num_Type(#1) |
Num_Type(#1) |
||
} while (#1 % 6 != 0);</ |
} while (#1 % 6 != 0);</syntaxhighlight> |
||
=={{header|Verbexx}}== |
=={{header|Verbexx}}== |
||
< |
<syntaxhighlight lang="verbexx">// Basic @LOOP until: verb |
||
@LOOP init:{@VAR n = 0} until:(n % 6 == 0) |
@LOOP init:{@VAR n = 0} until:(n % 6 == 0) |
||
Line 3,051: | Line 3,317: | ||
n++; |
n++; |
||
@SAY n; |
@SAY n; |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Verilog}}== |
=={{header|Verilog}}== |
||
<syntaxhighlight lang="verilog"> |
|||
<lang Verilog> |
|||
module main; |
module main; |
||
integer i; |
integer i; |
||
Line 3,070: | Line 3,335: | ||
end |
end |
||
endmodule |
endmodule |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|V (Vlang)}}== |
|||
=={{header|Visual Basic .NET}}== |
|||
<lang vbnet>Dim i = 0 |
|||
Do |
|||
i += 1 |
|||
Console.WriteLine(i) |
|||
Loop Until i Mod 6 = 0</lang> |
|||
=={{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. |
|||
<lang ecmascript>var v = 0 |
|||
while (true) { |
|||
v = v + 1 |
|||
System.print(v) |
|||
if (v%6 == 0) break |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 |
|||
2 |
|||
3 |
|||
4 |
|||
5 |
|||
6 |
|||
</pre> |
|||
It can also be simulated ''without'' using a break statement as follows: |
|||
<lang ecmascript>var value = 0 |
|||
var ok = true |
|||
while (ok) { |
|||
value = value + 1 |
|||
System.print(value) |
|||
ok = value%6 != 0 |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Same as before. |
|||
</pre> |
|||
=={{header|Vlang}}== |
|||
{{trans|go}} |
{{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. |
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 |
mut value := 0 |
||
for { |
for { |
||
Line 3,124: | Line 3,349: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 3,133: | Line 3,358: | ||
6</pre> |
6</pre> |
||
It can also be simulated ''without'' using a break statement as follows: |
It can also be simulated ''without'' using a break statement as follows: |
||
< |
<syntaxhighlight lang="vlang">fn main() { |
||
mut value := 0 |
mut value := 0 |
||
for ok := true; ok; ok = value%6 != 0 { |
for ok := true; ok; ok = value%6 != 0 { |
||
Line 3,139: | Line 3,364: | ||
println(value) |
println(value) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,145: | Line 3,370: | ||
Same as before. |
Same as before. |
||
</pre> |
</pre> |
||
::< |
::<syntaxhighlight lang="vlang">fn main() { |
||
// do-while loop 1 |
// do-while loop 1 |
||
mut n1 := 2 |
mut n1 := 2 |
||
Line 3,167: | Line 3,392: | ||
} |
} |
||
println(n3) // prt 8 |
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. |
|||
<syntaxhighlight lang="wren">var v = 0 |
|||
while (true) { |
|||
v = v + 1 |
|||
System.print(v) |
|||
if (v%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="wren">var value = 0 |
|||
var ok = true |
|||
while (ok) { |
|||
value = value + 1 |
|||
System.print(value) |
|||
ok = value%6 != 0 |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Same as before. |
|||
</pre> |
|||
=={{header|X86 Assembly}}== |
=={{header|X86 Assembly}}== |
||
{{works with|nasm}} |
{{works with|nasm}} |
||
{{works with|windows}} |
{{works with|windows}} |
||
< |
<syntaxhighlight lang="asm"> |
||
extern _printf |
extern _printf |
||
Line 3,198: | Line 3,454: | ||
xor eax, eax |
xor eax, eax |
||
ret |
ret |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|XBasic}}== |
|||
{{works with|Windows XBasic}} |
|||
<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 |
|||
</lang> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code CrLf=9, IntOut=11; |
||
int V; |
int V; |
||
[V:= 0; |
[V:= 0; |
||
Line 3,224: | Line 3,463: | ||
IntOut(0, V); CrLf(0); |
IntOut(0, V); CrLf(0); |
||
until rem(V/6) = 0; |
until rem(V/6) = 0; |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Yorick}}== |
=={{header|Yorick}}== |
||
< |
<syntaxhighlight lang="yorick">val = 0; |
||
do { |
do { |
||
val++; |
val++; |
||
write, val; |
write, val; |
||
} while(val % 6 != 0);</ |
} while(val % 6 != 0);</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Yorick}} |
{{trans|Yorick}} |
||
< |
<syntaxhighlight lang="zkl">val := 0; |
||
do { |
do { |
||
val+=1; |
val+=1; |
||
val.print(" "); |
val.print(" "); |
||
} while(val % 6 != 0);</ |
} while(val % 6 != 0);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,246: | Line 3,485: | ||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
< |
<syntaxhighlight lang="zig">const std = @import("std"); |
||
pub fn main() !void { |
pub fn main() !void { |
||
Line 3,260: | Line 3,499: | ||
try std.io.getStdOut().writer().print("{d}\n", .{a}); |
try std.io.getStdOut().writer().print("{d}\n", .{a}); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{omit from|GUISS}} |
{{omit from|GUISS}} |
||
{{omit from|Commodore BASIC}} |
Latest revision as of 11:18, 18 August 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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.
- Related tasks
- Loop over multiple arrays simultaneously
- Loops/Break
- Loops/Continue
- Loops/Do-while
- Loops/Downward for
- Loops/For
- Loops/For with a specified step
- Loops/Foreach
- Loops/Increment loop index within loop body
- Loops/Infinite
- Loops/N plus one half
- Loops/Nested
- Loops/While
- Loops/with multiple ranges
- Loops/Wrong ranges
- Reference
- Do while loop Wikipedia.
11l
11l doesn't have a do-while loop.
V val = 0
L
val++
print(val)
I val % 6 == 0
L.break
360 Assembly
- Basic
The WTO macro is in SYS1.MACLIB, which needs to be in the SYSLIB concatenation at assembly.
* Do-While
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
- 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.
* Do-While 27/06/2016
DOWHILE CSECT
USING DOWHILE,12 set base register
LR 12,15 init base register
SR 6,6 v=0
LA 4,1 init reg 4
DO UNTIL=(LTR,4,Z,4) do until v mod 6=0
LA 6,1(6) v=v+1
STC 6,WTOTXT v
OI WTOTXT,X'F0' make editable
WTO MF=(E,WTOMSG) display v
LR 4,6 v
SRDA 4,32 shift dividend to reg 5
D 4,=F'6' v/6 so r4=remain & r5=quotient
ENDDO , end do
BR 14 return to caller
WTOMSG DS 0F full word alignment for wto
WTOLEN DC AL2(L'WTOTXT+4) length of WTO buffer
DC H'0' must be zero
WTOTXT DS C one char
END DOWHILE
6502 Assembly
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
AArch64 Assembly
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopdowhile64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "Counter = @ \n" // message result
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x20,0 // indice
mov x21,6
1: // begin loop
mov x0,x20
ldr x1,qAdrsZoneConv // conversion value value
bl conversion10 // decimal
ldr x0,qAdrszMessResult
ldr x1,qAdrsZoneConv // display conversion
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
add x20,x20,1 // increment counter
udiv x0,x20,x21 // divide by 6
msub x1,x0,x21,x20 // compute remainder
cbnz x1,1b // loop if remainder <> zéro
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
Action!
Proc Main()
byte A
A=0
Do
A==+1
PrintBE(A)
Until A Mod 6=0
Od
Return
ActionScript
var val:int = 0;
do
{
trace(++val);
} while (val % 6);
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;
Agena
Tested with Agena 2.9.5 Win32
scope
local i := 0;
do
inc i, 1;
print( i )
as ( i % 6 ) <> 0
epocs
Aime
integer a;
a = 0;
do {
a += 1;
o_integer(a);
o_byte('\n');
} while (a % 6 != 0);
ALGOL 60
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.
'BEGIN' 'COMMENT' Loops DoWhile - Algol60 - 22/06/2018;
'INTEGER' I;
I:=0;
LOOP:
I:=I+1;
OUTINTEGER(1,I);
'IF' I=I'/'6*6 'THEN' 'GOTO' ENDLOOP;
'GOTO' LOOP;
ENDLOOP:
'END'
- Output:
+1 +2 +3 +4 +5 +6
A "goto-less" approach
While a judicious "goto" may sometimes be clearer than the alternative, it is possible to avoid it in this case by using the for-while statement and a boolean flag to implement a test-at-the-bottom loop.
begin
integer i;
boolean another;
i := 0;
another := true;
for i := i while another do
begin
i := i + 1;
outinteger(1,i);
comment - stop once i mod 6 = 0;
if i = (i div 6) * 6 then another := false;
end;
end
- Output:
1 2 3 4 5 6
ALGOL 68
FOR value WHILE
print(value);
# WHILE # value MOD 6 /= 0 DO
SKIP
OD
ALGOL W
begin
integer i;
i := 0;
while
begin
i := i + 1;
write( i );
( i rem 6 ) not = 0
end
do begin end
end.
AmigaE
PROC main()
DEF i = 0
REPEAT
i := i + 1
WriteF('\d\n', i)
UNTIL Mod(i, 6) = 0
ENDPROC
ARM Assembly
/* ARM assembly Raspberry PI */
/* program loopdowhile.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .ascii "Counter = " @ message result
sMessValeur: .fill 12, 1, ' '
.asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
push {fp,lr} @ saves 2 registers
mov r4,#0
1: @ begin loop
mov r0,r4
ldr r1,iAdrsMessValeur @ display value
bl conversion10 @ call function with 2 parameter (r0,r1)
ldr r0,iAdrszMessResult
bl affichageMess @ display message
add r4,#1 @ increment counter
mov r0,r4
mov r1,#6 @ division conuter by 6
bl division
cmp r3,#0 @ remainder = zéro ?
bne 1b @ no ->begin loop one
100: @ standard end of the program
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrsMessValeur: .int sMessValeur
iAdrszMessResult: .int szMessResult
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
/******************************************************************/
/* Converting a register to a decimal */
/******************************************************************/
/* r0 contains value and r1 address area */
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#10
1: @ start loop
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
sub r2,#1 @ previous position
cmp r0,#0 @ stop if quotient = 0 */
bne 1b @ else loop
@ and move spaces in first on area
mov r1,#' ' @ space
2:
strb r1,[r3,r2] @ store space in area
subs r2,#1 @ @ previous position
bge 2b @ loop if r2 >= zéro
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 signé */
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
/* and http://www.hackersdelight.org/ */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10:
/* r0 contains the argument to be divided by 10 */
push {r2-r4} /* save registers */
mov r4,r0
mov r3,#0x6667 @ r3 <- magic_number lower
movt r3,#0x6666 @ r3 <- magic_number upper
smull r1, r2, r3, r0 @ r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0)
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
add r0, r2, r1 /* r0 <- r2 + r1 */
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
pop {r2-r4}
bx lr /* leave function */
/***************************************************/
/* integer division unsigned */
/***************************************************/
division:
/* r0 contains dividend */
/* r1 contains divisor */
/* r2 returns quotient */
/* r3 returns remainder */
push {r4, lr}
mov r2, #0 @ init quotient
mov r3, #0 @ init remainder
mov r4, #32 @ init counter bits
b 2f
1: @ loop
movs r0, r0, LSL #1 @ r0 <- r0 << 1 updating cpsr (sets C if 31st bit of r0 was 1)
adc r3, r3, r3 @ r3 <- r3 + r3 + C. This is equivalent to r3 ? (r3 << 1) + C
cmp r3, r1 @ compute r3 - r1 and update cpsr
subhs r3, r3, r1 @ if r3 >= r1 (C=1) then r3 ? r3 - r1
adc r2, r2, r2 @ r2 <- r2 + r2 + C. This is equivalent to r2 <- (r2 << 1) + C
2:
subs r4, r4, #1 @ r4 <- r4 - 1
bpl 1b @ if r4 >= 0 (N=0) then loop
pop {r4, lr}
bx lr
AppleScript
on printConsole(x)
return x as string
end printConsole
set {i, table} to {0, {return}}
repeat while (i mod 6 is not 0 or i is not 6)
set i to i + 1
set end of table to i & return
printConsole(table)
end repeat
- Output:
" 1 2 3 4 5 6 "
Arturo
value: 0
until [
value: value + 1
print value
] [ 0 = value%6 ]
- Output:
1 2 3 4 5 6
Asymptote
Asymptote's control structures are similar to those in C, C++, or Java
int i = 0;
do {
++i;
write(" ", i, suffix=none);
} while (i % 6 != 0);
AutoHotkey
While mod(A_Index, 6) ;comment:everything but 0 is considered true
output = %output%`n%A_Index%
MsgBox % output
AWK
BEGIN {
val = 0
do {
val++
print val
} while( val % 6 != 0)
}
Axe
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
Applesoft BASIC
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
ASIC
ASIC does not have a do .. while
construct. Equivalent using WHILE
:
REM Loops/Do-while
I = 0
REM first iteration - before the While
I = I + 1
PRINT I
IMod6 = I MOD 6
WHILE IMod6 <> 0
I = I + 1
PRINT I
IMod6 = I MOD 6
WEND
END
- Output:
1 2 3 4 5 6
Equivalent using conditional jump:
REM Loops/Do-while
I = 0
LoopStart:
I = I + 1
PRINT I
IMod6 = I MOD 6
IF IMod6 <> 0 THEN LoopStart:
END
- Output:
As above
BaCon
a=0
REPEAT
INCR a
PRINT a
UNTIL MOD(a,6) == 0
BASIC256
i = 0
do
i += 1
print i; " ";
until i mod 6 = 0
print
end
BBC BASIC
a = 0
REPEAT
a = a + 1
PRINT a
UNTIL a MOD 6 = 0
Chipmunk Basic
In Chipmunk Basic Man Page, the words do
, loop
, and until
are mentioned as reserved, but the do .. loop until
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 while .. wend
).
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
- Output:
1 2 3 4 5 6
Commodore 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
FreeBASIC
' FB 1.05. 0 Win64
Dim i As Integer = 0
Do
i += 1
Print i; " ";
Loop While i Mod 6 <> 0
Print
Sleep
- Output:
1 2 3 4 5 6
Gambas
Click this link to run this code
Public Sub Main()
Dim siCount As Short
Repeat
Inc siCount
Print siCount;;
Until siCount Mod 6 = 0
End
- Output:
1 2 3 4 5 6
GW-BASIC
GW-BASIC does not have a do .. while
construct.
Equivalent using WHILE
:
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
Equivalent using GOTO
:
10 LET I% = 0
20 LET I% = I% + 1
30 PRINT I%
40 IF I% MOD 6 <> 0 THEN GOTO 20
IS-BASIC
100 LET I=0
110 DO
120 LET I=I+1
130 PRINT I
140 LOOP UNTIL MOD(I,6)=0
Liberty BASIC
a = 0
do
a = a + 1
print a
loop until (a mod 6) = 0
See also QBasic.
Microsoft Small Basic
Microsoft Small Basic does not have a do .. while
construct.
Equivalent using While
:
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
Equivalent using Goto
:
i = 0
loopStart:
i = i + 1
TextWindow.WriteLine(i)
If Math.Remainder(i, 6) <> 0 Then
Goto loopStart
EndIf
Minimal BASIC
Minimal BASIC does not have a do .. while
construct. Equivalent using conditional jump:
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
MSX Basic
The Minimal BASIC solution works without any changes.
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."
PureBasic
x=0
Repeat
x+1
Debug x
Until x%6=0
QB64
CBTJD: 2020/03/14
DO
PRINT n
n = n + 1
LOOP WHILE n MOD 6 <> 0
'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
QBasic
a = 0
DO
a = a + 1
PRINT a;
LOOP WHILE a MOD 6 <> 0
Quite BASIC
The Minimal BASIC solution works without any changes.
Run BASIC
Run Basic does not have a do .. while
construct. Equivalent using conditional jump:
i = 0
[start]
i = i +1
print i; " ";
if i mod 6 <> 0 then [start]
Sinclair ZX81 BASIC
10 LET X=0
20 LET X=X+1
30 PRINT X
40 IF X/6<>INT (X/6) THEN GOTO 20
SmallBASIC
repeat
v++
print v
until(v mod 6 == 0)
Tiny BASIC
Tiny Basic does not have a do .. while
construct. Equivalent using conditional jump:
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
- Output:
1 2 3 4 5 6
True BASIC
LET i = 0
DO
LET i = i + 1
PRINT i; " ";
LOOP WHILE REMAINDER(i, 6) <> 0
PRINT
END
VBA
Public Sub LoopDoWhile()
Dim value As Integer
value = 0
Do
value = value + 1
Debug.Print value;
Loop While value Mod 6 <> 0
End Sub
- Output:
1 2 3 4 5 6
Visual Basic .NET
Dim i = 0
Do
i += 1
Console.WriteLine(i)
Loop Until i Mod 6 = 0
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
Yabasic
i = 0
repeat
i = i + 1
print i, " ";
until mod(i, 6) = 0
print
bc
i = 0
for (;;) {
++i /* increments then prints i */
if (i % 6 == 0) break
}
quit
Befunge
0>1+:.v
|%6: <
@
C
int val = 0;
do{
val++;
printf("%d\n",val);
}while(val % 6 != 0);
C#
int a = 0;
do
{
a += 1;
Console.WriteLine(a);
} while (a % 6 != 0);
C++
int val = 0;
do{
val++;
std::cout << val << std::endl;
}while(val % 6 != 0);
C3
In this example we use default zero initialization of locals in C3.
int val;
do
{
io::printn(++val);
}
while (val % 6 != 0);
Chapel
var val = 0;
do {
val += 1;
writeln(val);
} while val % 6 > 0;
ChucK
0 => int value;
do
{
value++;
<<<value>>>;
}
while(value % 6 != 0);
Clipper
Local n := 0
DO WHILE .T.
? ++n
IF n % 6 == 0
EXIT
ENDIF
ENDDO
Clojure
(loop [i 0]
(let [i* (inc i)]
(println i*)
(when-not (zero? (mod i* 6))
(recur i*))))
COBOL
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
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
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
<cfscript>
value = 0;
do
{
value += 1;
writeOutput( value );
} while( value % 6 != 0 );
</cfscript>
Common Lisp
(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)))
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
- Output:
1 2 3 4 5 6
D
import std.stdio;
void main() {
int val;
do {
val++;
write(val, " ");
} while (val % 6 != 0);
}
- Output:
1 2 3 4 5 6
Dart
void main() {
int val = 0;
do {
val++;
print(val);
} while (val % 6 != 0);
}
dc
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
program Loop;
{$APPTYPE CONSOLE}
var
I: Integer;
begin
I:= 0;
repeat
Inc(I);
Write(I:2);
until I mod 6 = 0;
Writeln;
Readln;
end.
Draco
proc nonrec main() void:
byte i;
i := 0;
while
i := i + 1;
write(i:2);
i % 6 ~= 0
do od
corp
- Output:
1 2 3 4 5 6
Dragon
val = 0
do{
val++
showln val
}while(val % 6 != 0)
DUP
DUP only provides a while loop in the form of [condition][block]#
, where the block is executed in a loop as long as the condition is nonzero/true.
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 {operator definition: PUSH 1, ADD, DUP, print top of stack to SDTOUT, print whitespace}
[1+$.' ,]a: {function definition}
and put the defined block in front of the while loop, and inside the while loop itself:
If the block was defined as an operator, the whole program would look like this (comments in curly braces):
[1+$.' ,]⇒A
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:
[1+$.' ,]a:
0 a;![$6/%][a;!]#
Result:
1 2 3 4 5 6
DWScript
var i := 0;
repeat
Inc(i);
PrintLn(i);
until i mod 6 = 0;
Bold text
Dyalect
var x = 0
do
{
x += 1
print(x)
} while x % 6 != 0
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
})
EasyLang
value = 0
repeat
value += 1
print value
until value mod 6 = 0
.
Ela
open monad io
loop n | n % 6 == 0 = do return ()
| else = do
putStrLn (show n)
loop (n+1)
_ = loop 10 ::: IO
Elixir
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
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)))))
Alternatively, the loop can be rewritten to check for the exit condition and fulfill it at the end of the loop body.
(let ((val 0) done)
(while (not done)
(setq val (1+ val))
(message "%d" val)
(setq done (zerop (mod val 6)))))
Erlang
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
A=0
REPEAT
A=A+1
PRINT(A)
UNTIL A MOD 6=0 !UNTIL A-6*INT(A/6)=0 for C-64
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
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
But I prefer this way:
Seq.initInfinite id |> Seq.takeWhile(fun n->n=0 || n%6>0) |> Seq.iter (fun n-> printfn "%d" n)
Either produces:
- Output:
0 1 2 3 4 5
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")
- Output:
1 2 3 4 5 6
Factor
0 [ dup 6 mod 0 = not ] [ [ . ] [ 1 + ] bi ] do while drop
Fantom
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
: do-until
0
begin 1+
dup .
dup 6 mod 0=
until
drop ;
Fortran
INTEGER :: i = 0
DO
i = i + 1
WRITE(*, *) i
IF (MOD(i, 6) == 0) EXIT
END DO
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
IVALUE = 0
10 CONTINUE
IVALUE=IVALUE+1
WRITE(6,301) IVALUE
301 FORMAT(I5)
IF(MOD(IVALUE,6).NE.0) GOTO 10
END
IVALUE = 0
10 IVALUE=IVALUE+1
WRITE 301,IVALUE
301 FORMAT(I5)
IF(IVALUE-IVALUE/6*6) 10,20,10
20 STOP
END
Fortress
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 break
equivalent.
component loops_do_while
export Executable
var x:ZZ32 = 0
run() = label loop
while true do
x += 1
println(x)
if (x MOD 6) = 0
then exit loop
end
end
end loop
end
- Output:
1 2 3 4 5 6
Frink
n = 0
do
{
n = n + 1
println[n]
} while n mod 6 != 0
FutureBasic
window 1
dim as long i
do
i++
print i
until ( i mod 6 == 0 )
HandleEvents
- Output:
1 2 3 4 5 6
GAP
n := 0;
repeat
n := n + 1;
Print(n, "\n");
until RemInt(n, 6) = 0;
GML
i = 0
do
{
i += 1
show_message(string(i))
}
until (i mod 6 = 0)
Go
There is no explicit do-while in Go, but it can be simulated with a range-based for loop and the break statement.
package main
import "fmt"
func main() {
var value int
for {
value++
fmt.Println(value)
if value%6 != 0 {
break
}
}
}
- Output:
1 2 3 4 5 6
It can also be simulated without using a break statement as follows:
package main
import "fmt"
func main() {
var value int
for ok := true; ok; ok = value%6 != 0 {
value++
fmt.Println(value)
}
}
- Output:
Same as before.
package main import "fmt" func main() { // do-while loop 1 n1 := 2 for n1 < 6 { n1 *= 2 } fmt.Println(n1) // prt 8 // do-while loop 2 n2 := 2 for ok := true; ok; ok = n2%8 != 0 { n2 *= 2 } fmt.Println(n2) // prt 8 // do-while loop 3 n3 := 2 for { n3 *= 2 if n3 >= 6 { break } } fmt.Println(n3) // prt 8 }
Groovy
For Groovy 3.0.0 and later.
def i = 0
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.
def i = 0
while (true) {
i++
println i
if (i % 6 == 0) break
}
- Output:
1 2 3 4 5 6
Harbour
LOCAL n := 0
DO WHILE .T.
? ++n
IF n % 6 == 0
EXIT
ENDIF
ENDDO
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
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 :: IO ()
main =
mapM_ print . reverse $
until
(\(x:_) -> (x > 0) && (mod x 6 == 0))
(\xs@(x:_) -> succ x : xs)
[0]
- Output:
0 1 2 3 4 5 6
With mutable references
Using iterateWhile from monad-loops package
import Data.IORef
import Control.Monad.Loops
main = do
x <- newIORef 0;
iterateWhile (\val -> val `mod` 6 /= 0 ) $ do
modifyIORef x (+1)
val <- readIORef x
print val
return val
Haxe
var val = 0;
do {
val++;
Sys.println(val);
} while( val % 6 != 0);
HolyC
U8 i = 0;
do {
i++;
Print("%d\n", i);
} while (i % 6 != 0);
Icon and 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 Introduction to Icon and Unicon/Looping Controls for more information.)
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
0
1
2
3
4
5
This could also be accomplished using Z: to provide early termination from a fold:
0#]F.(>: [ echo [ _2 Z: * * 0=6|]) 0
0
1
2
3
4
5
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
int val = 0;
do{
val++;
System.out.println(val);
}while(val % 6 != 0);
JavaScript
Javascript: Imperative
var val = 0;
do {
print(++val);
} while (val % 6);
Javascript: Functional
ES5
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.
- An initial value,
- a Do function which transforms that value repetitively, corresponding to the body of the loop,
- 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
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';
// unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
function unfoldr(mf, v) {
for (var lst = [], a = v, m;
(m = mf(a)) && m.valid;) {
lst.push(m.value), a = m.new;
}
return lst;
}
// until :: (a -> Bool) -> (a -> a) -> a -> a
function until(p, f, x) {
let v = x;
while(!p(v)) v = f(v);
return v;
}
let result1 = unfoldr(
x => {
return {
value: x,
valid: (x % 6) !== 0,
new: x + 1
}
},
1
);
let result2 = until(
m => (m.n % 6) === 0,
m => {
return {
n : m.n + 1,
xs : m.xs.concat(m.n)
};
},
{
n: 1,
xs: []
}
).xs;
return [result1, result2];
})();
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
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:
// generator with the do while loop
function* getValue(stop) {
var i = 0;
do {
yield ++i;
} while (i % stop != 0);
}
// function to print the value and invoke next
function printVal(g, v) {
if (!v.done) {
console.log(v.value);
setImmediate(printVal, g, g.next());
}
}
(() => {
var gen = getValue(6);
printVal(gen, gen.next());
})();
1
2
3
4
5
6
jq
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
Julia has no do-while construct. Here is one of several ways to implement do-while behavior.
julia> i = 0
0
julia> while true
println(i)
i += 1
i % 6 == 0 && break
end
0
1
2
3
4
5
Using a macro that mimics the classic C style do-while.
Notice that the symbol while
cannot be used as it is a keyword, which is why when
is used instead, also the macro definition is wrapped in a @eval
macro invocation since do
is also a keyword, but in Julia macro calls are prefixed by @
so this is only an issue during the macro definition, not when invoked, ie. @do block when condition
).
julia> @eval macro $(:do)(block, when::Symbol, condition)
when ≠ :when && error("@do expected `when` got `$s`")
quote
let
$block
while $condition
$block
end
end
end |> esc
end
@do (macro with 1 method)
julia> i = 0
0
julia> @do begin
@show i
i += 1
end when i % 6 ≠ 0
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
Here is mostly the same macro, but with the conditional clause used first, which is arguably more readable.
julia> macro do_while(condition, block)
quote
let
$block
while $condition
$block
end
end
end |> esc
end
@do_while (macro with 1 method)
julia> i = 0
0
julia> @do_while i % 6 ≠ 0 begin
@show i
i += 1
end
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
Kotlin
// version 1.0.6
fun main(args: Array<String>) {
var value = 0
do {
println(++value)
}
while (value % 6 != 0)
}
- Output:
1 2 3 4 5 6
LabVIEW
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.
Lasso
local(x = 0)
while(#x % 6 > 0 || #x == 0) => {^
++#x
'\r' // for formatting
^}
Lambdatalk
{def do_while
{def do_while.r
{lambda {:i}
{if {= {% :i 6} 0}
then :i (end of loop)
else :i {do_while.r {+ :i 1}}}}}
{lambda {:i}
:i {do_while.r {+ :i 1}}}}
-> do_while
{do_while 0}
-> 0 1 2 3 4 5 6 (end of loop)
Lang
Lang does not have a do-while loop. A simple loop like the example below can be used.
$i = 0
loop {
$i += 1
fn.println($i)
if(!($i % 6)) {
con.break
}
}
Lingo
Lingo has no do..while, but here how this behavior can be implemented:
i = 0
repeat while TRUE
i = i+1
put i
if i mod 6 = 0 then exit repeat
end
Lisaac
+ val : INTEGER;
{
val := val + 1;
val.print;
'\n'.print;
val % 6 != 0
}.while_do { };
LiveCode
repeat while n mod 6 is not 0 or n is 0
add 1 to n
put n
end repeat
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
Lua
Lua doesn't have a do .. while
construct.
i=0
repeat
i=i+1
print(i)
until i%6 == 0
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,
} when x mod 6>0
print
x=0
{
\\ when enter to block the loop flag change to false
x++
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 x,
}
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
- Output:
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
Maple
val := 0:
do
val := 1 + val;
print( val );
if irem( val, 6 ) = 0 then
break
end if;
end do:
Mathematica /Wolfram Language
Because everything is an expression in Mathematica, While[body;condition]
tests condition
after body
has been executed at least once.
value = 0;
While[
value++;
Print[value];
Mod[value,6]!=0
]
MATLAB / Octave
a=0;
while (1)
a = a+1;
disp(a);
if (~mod(a,6)) break; end;
end;
Maxima
block([n: 0], do (ldisp(n: n + 1), if mod(n, 6) = 0 then return('done)))$
MAXScript
a = 0
do
(
print a
a += 1
)
while mod a 6 != 0
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
min
0 (dup 6 mod 0 == over 0 != and) 'pop (puts succ) () linrec
MIPS Assembly
.text
main: li $s0, 0 # start at 0.
li $s1, 6
loop: addi $s0, $s0, 1 # add 1 to $s0
div $s0, $s1 # divide $s0 by $s1. Result is in the multiplication/division registers
mfhi $s3 # copy the remainder from the higher multiplication register to $s3
move $a0, $s0 # variable must be in $a0 to print
li $v0, 1 # 1 must be in $v0 to tell the assembler to print an integer
syscall # print the integer in $a0
bnez $s3, loop # if $s3 is not 0, jump to loop
li $v0, 10
syscall # syscall to end the program
МК-61/52
0 П4 КИП4 ИП4 6 / {x} x=0 02 С/П
Modula-2
MODULE DoWhile;
IMPORT InOut;
VAR
i: INTEGER;
BEGIN
i := 0;
REPEAT
INC(i);
InOut.WriteInt(i, 1);
InOut.WriteLn;
UNTIL i MOD 6 = 0;
END DoWhile.
Modula-3
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
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
MUMPS
DOWHILELOOP
set val = 0
do {
set val = val + 1
write val,!
} while ((val # 6) '= 0)
quit
- Output:
SAMPLES>do ^DOWHILELOOP 1 2 3 4 5 6
Neko
/**
Loops/Do-while in Neko
Tectonics:
nekoc loops-do-while.neko
neko loops-do-while
*/
var index = 0;
do {
index += 1;
$print(index, "\n");
} while (index % 6) != 0
- Output:
prompt$ nekoc loops-do-while.neko prompt$ neko loops-do-while 1 2 3 4 5 6
Nemerle
mutable x = 0;
do
{
x++;
WriteLine($"$x");
} while (x % 6 != 0)
NetRexx
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
(let ((i 0))
(do-until (= 0 (% i 6))
(println (++ i))))
Nim
Nim does not have a do-while loop. The standard way to simulate it consists in an infinite loop with a break statement:
var val = 0
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):
template doWhile(a, b: untyped): untyped =
b
while a:
b
var val = 0
doWhile val mod 6 != 0:
inc val
echo val
Nu
mut n = 0
while true {
$n += 1
print $n
if $n mod 6 == 0 {break}
}
Oberon-2
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.
Objeck
i := 0;
do {
i += 1;
i->PrintLine();
}
while (i % 6 <> 0);
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 -> ()
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 )
Oforth
0 doWhile: [ 1+ dup . dup 6 rem 0 <> ] drop
OpenEdge/Progress
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
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
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
program countto6(output);
var
i: integer;
begin
i := 0;
repeat
i := i + 1;
writeln(i)
until i mod 6 = 0
end.
PascalABC.NET
begin
var a := 0;
repeat
a += 1;
Print(a);
until a mod 6 = 0;
end.
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);
Phix
integer x = 0 while 1 do x += 1 ?x if mod(x,6)=0 then exit end if end while
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
PHL
var i = 0;
do {
i = i::inc;
printf("%i\n", i);
} while (i%6 != 0);
PHP
$val = 0;
do {
$val++;
print "$val\n";
} while ($val % 6 != 0);
Picat
do while loop
go =>
N = 0,
do
N := N+1,
println(N)
while (N mod 6 != 0).
Recursion
go2 =>
do_while(0).
do_while(N) :-
N1 = N + 1,
println(N1),
N1 mod 6 != 0,
do_while(N1).
Both outputs the same.
- Output:
1 2 3 4 5 6
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))))
Pike
int main(){
int value = 0;
do {
value++;
write(value + "\n");
} while (value % 6);
}
PL/0
PL/0 does not have a do .. while
construct. Equivalent using while
:
var i;
begin
i := 0;
i := i + 1;
! i;
while (i / 6) * 6 <> i do
begin
i := i + 1;
! i
end;
end.
- Output:
1 2 3 4 5 6
PL/I
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
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.
To run:
Start up.
Demonstrate do-while.
Wait for the escape key.
Shut down.
To demonstrate do-while:
Bump a counter.
Convert the counter to a string.
Write the string on the console.
If the counter is evenly divisible by 6, exit.
Repeat.
Pop11
lvars val = 0;
while true do
val + 1 -> val;
printf(val, '%p\n');
quitif(val rem 6 = 0);
endwhile;
PowerShell
$n = 0
do {
$n++
$n
} while ($n % 6 -ne 0)
Prolog
% 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).
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
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. until
means jump to [
if ToS is false.
0 [ 1+ dup echo cr
dup 6 mod 0 = until ] drop
R
i <- 0
repeat
{
i <- i + 1
print(i)
if(i %% 6 == 0) break
}
Racket
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)))
Raku
(formerly Perl 6)
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;
}
REBOL
REBOL [
Title: "Loop/While"
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
Red
Red []
i: 0
until [
?? i
i: i + 1
i % 6 = 0 ;; loop , until this is true...
]
- Output:
i: 0 i: 1 i: 2 i: 3 i: 4 i: 5
REXX
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
/*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
/*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.
Ring
n = 0
While True
n++ See n + nl
if n % 6 = 0 exit ok
end
RPL
To ensure at least one loop, DO
..UNTIL
..END
must be used rather than WHILE
..REPEAT
..END
. To actually print (on paper) instead of pushing in the stack successive results, the DUP
instruction inside the loop shall be replaced by PR1
≪ 0 DO 1 + DUP UNTIL DUP 6 MOD 0 == END DROP ≫
Ruby
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
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
variable x := 0;
do
{
++x;
x!
}
while (x % 6 != 0);
SAS
/* using DO UNTIL so that the loop executes at least once */
data _null_;
n=0;
do until(mod(n,6)=0);
n+1;
put n;
end;
run;
Sather
class MAIN is
main is
val ::= 0;
loop
val := val + 1;
#OUT + val + "\n";
while!(val % 6 /= 0)
end;
end;
end;
S-BASIC
S-BASIC's REPEAT..UNTIL control structure is the equivalent of a DO..WHILE statement with the terminating test inverted.
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
- Output:
1 2 3 4 5 6
Scala
Imperative
{
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
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
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
(let loop ((i 1))
(display i)
(if (positive? (modulo i 6))
(loop (+ i 1))))
Scilab
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
$ 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
var value = 0;
do {
say ++value;
} while (value % 6);
Slate
[| val |
val: 0.
[val: val + 1.
print: val.
val \\ 6 ~= 0] whileTrue
] do.
Smalltalk
|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 ]
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: [ ]
Or send the loop block a whileTrue message (without argument).
|val|
val := 0.
[
val := val + 1.
val displayNl.
(val rem: 6) ~= 0
] whileTrue
Corresponding false-checking messages are whileFalse: and whileFalse (without argument)
Sparkling
var i = 0;
do {
print(++i);
} while (i % 6 != 0);
Spin
con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
obj
ser : "FullDuplexSerial.spin"
pub main | n
ser.start(31, 30, 0, 115200)
n := 0
repeat
n += 1
ser.dec(n)
ser.tx(32)
while n // 6
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)
- Output:
1 2 3 4 5 6
SPL
n = 0
>
n += 1
#.output(n)
< n%6
- Output:
1 2 3 4 5 6
Stata
Stata macro language has no do/while loop, but it's possible to achieve this with a while loop.
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 n 0
local q 1
while `q' | mod(`n',6) {
local q 0
di `++n'
}
Use an infinite while loop and do the test with an if' at the end of the loop.
local n 0
while 1 {
di `++n'
if mod(`n',6)==0 continue, break
}
Mata
Mata has a do/while loop:
mata
n=0
do {
printf("%f\n",++n)
} while (mod(n,6))
end
Suneido
val = 0
do
{
Print(++val)
} while (val % 6 isnt 0)
- Output:
1 2 3 4 5 6
Swift
var val = 0
repeat {
val += 1
print(val)
} while val % 6 != 0
var val = 0
repeat {
val++
print(val)
} while val % 6 != 0
var val = 0
do {
val++
println(val)
} while val % 6 != 0
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 -> #
<> def val: $ + 1;
$val -> !OUT::write
$val -> \(<?($ mod 6 <~=0>)> $!\) -> #
end doWhile
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}
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
$$ 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
val=0
while true; do
echo $((++val))
[ $((val%6)) -eq 0 ] && break
done
val=0
while true; do
val=`expr $val + 1`
echo $val
expr $val % 6 = 0 >/dev/null && break
done
for ((val=1;;val++)) {
print $val
(( val % 6 )) || break
}
Vala
int a = 0;
do {
a++;
print(a.to_string() + "\n");
}
while ( a % 6 != 0);
Vedit macro language
#1 = 0
do {
#1++
Num_Type(#1)
} while (#1 % 6 != 0);
Verbexx
// Basic @LOOP until: verb
@LOOP init:{@VAR n = 0} until:(n % 6 == 0)
{
n++;
@SAY n;
};
Verilog
module main;
integer i;
initial begin
i = 1;
$write(i);
while(i % 6 != 0) begin
i = i + 1;
$write(i);
end
$finish ;
end
endmodule
V (Vlang)
There is no explicit do-while in Vlang, but it can be simulated with a range-based for loop and the break statement.
fn main() {
mut value := 0
for {
value++
println(value)
if value%6 != 0 {
break
}
}
}
- Output:
1 2 3 4 5 6
It can also be simulated without using a break statement as follows:
fn main() {
mut value := 0
for ok := true; ok; ok = value%6 != 0 {
value++
println(value)
}
}
- Output:
Same as before.
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 }
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.
var v = 0
while (true) {
v = v + 1
System.print(v)
if (v%6 == 0) break
}
- Output:
1 2 3 4 5 6
It can also be simulated without using a break statement as follows:
var value = 0
var ok = true
while (ok) {
value = value + 1
System.print(value)
ok = value%6 != 0
}
- Output:
Same as before.
X86 Assembly
extern _printf
section .data
output db 0,0
section .text
global _main
_main:
mov bl, 0
looping:
add bl, 0x31 ;0x30 to 0x39 is 0 to 9 in ASCII
mov [output], bl
sub bl, 0x30
push output
call _printf
add esp, 4
xor eax, eax
xor edx, edx
mov al, bl
mov ecx, 6
div ecx ; remainder is saved in edx
cmp edx, 0
jne looping ; if n & 6 != 0 do looping again
xor eax, eax
ret
XPL0
code CrLf=9, IntOut=11;
int V;
[V:= 0;
repeat V:= V+1;
IntOut(0, V); CrLf(0);
until rem(V/6) = 0;
]
Yorick
val = 0;
do {
val++;
write, val;
} while(val % 6 != 0);
zkl
val := 0;
do {
val+=1;
val.print(" ");
} while(val % 6 != 0);
- Output:
1 2 3 4 5 6
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});
}
}
- Programming Tasks
- Iteration
- Conditional loops
- Simple
- 11l
- 360 Assembly
- 6502 Assembly
- AArch64 Assembly
- Action!
- ActionScript
- Ada
- Agena
- Aime
- ALGOL 60
- ALGOL 68
- ALGOL W
- AmigaE
- ARM Assembly
- AppleScript
- Arturo
- Asymptote
- AutoHotkey
- AWK
- Axe
- BASIC
- Applesoft BASIC
- ASIC
- BaCon
- BASIC256
- BBC BASIC
- Chipmunk Basic
- Commodore BASIC
- FreeBASIC
- Gambas
- GW-BASIC
- IS-BASIC
- Liberty BASIC
- Microsoft Small Basic
- Minimal BASIC
- MSX Basic
- NS-HUBASIC
- PureBasic
- QB64
- QBasic
- Quite BASIC
- Run BASIC
- Sinclair ZX81 BASIC
- SmallBASIC
- Tiny BASIC
- True BASIC
- VBA
- Visual Basic .NET
- XBasic
- Yabasic
- Bc
- Befunge
- C
- C sharp
- C++
- C3
- Chapel
- ChucK
- Clipper
- Clojure
- COBOL
- Coco
- CoffeeScript
- ColdFusion
- Common Lisp
- D
- Dart
- Dc
- Delphi
- Draco
- Dragon
- DUP
- DWScript
- Dyalect
- E
- EasyLang
- Ela
- Elixir
- Emacs Lisp
- Erlang
- ERRE
- Euphoria
- F Sharp
- Factor
- Fantom
- Forth
- Fortran
- Fortress
- Frink
- FutureBasic
- GAP
- GML
- Go
- Groovy
- Harbour
- Haskell
- Haxe
- HolyC
- Icon
- Unicon
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- LabVIEW
- Lasso
- Lambdatalk
- Lang
- Lingo
- Lisaac
- LiveCode
- Logo
- Lua
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Maxima
- MAXScript
- Metafont
- Min
- MIPS Assembly
- МК-61/52
- Modula-2
- Modula-3
- Monicelli
- MUMPS
- Neko
- Nemerle
- NetRexx
- NewLISP
- Nim
- Nu
- Oberon-2
- Objeck
- OCaml
- Oforth
- OpenEdge/Progress
- Oz
- PARI/GP
- Pascal
- PascalABC.NET
- Perl
- Phix
- Phixmonti
- PHL
- PHP
- Picat
- PicoLisp
- Pike
- PL/0
- PL/I
- Plain English
- Pop11
- PowerShell
- Prolog
- Python
- Quackery
- R
- Racket
- Raku
- REBOL
- Red
- REXX
- Ring
- RPL
- Ruby
- Rust
- Salmon
- SAS
- Sather
- S-BASIC
- Scala
- Scheme
- Scilab
- Seed7
- Sidef
- Slate
- Smalltalk
- Sparkling
- Spin
- SPL
- Stata
- Suneido
- Swift
- Tailspin
- Tcl
- Tcllib
- TUSCRIPT
- UNIX Shell
- Vala
- Vedit macro language
- Verbexx
- Verilog
- V (Vlang)
- Wren
- X86 Assembly
- XPL0
- Yorick
- Zkl
- Zig
- GUISS/Omit
- Pages with too many expensive parser function calls