Loops/N plus one half: Difference between revisions

m
prod
(Added EasyLang implementation)
m (prod)
 
(28 intermediate revisions by 15 users not shown)
Line 3:
 
Quite often one needs loops which, in the last iteration, execute only part of the loop body.
 
 
;Goal:
Demonstrate the best way to do this.
 
 
;Task:
Line 14 ⟶ 12:
using separate output statements for the number
and the comma from within the body of the loop.
 
 
;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]]
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">L(i) 1..10
L(i) 1..10
print(i, end' ‘’)
I !L.last_iteration
print(‘, ’, end' ‘’)</syntaxhighlight>
</syntaxhighlight>
 
=={{header|360 Assembly}}==
Line 73 ⟶ 71:
=={{header|68000 Assembly}}==
Sega Genesis cartridge header and hardware print routines/bitmap font are omitted here but do work as intended.
<syntaxhighlight lang="68000devpac"> moveq #0,d0
moveq #0,d0
move.w #1,d1 ;ABCD can't use an immediate operand
move.w #10-1,d2
Line 96 ⟶ 95:
; include "X:\SrcGEN\testModule.asm"
jmp *</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>01, 02, 03, 04, 05, 06, 07, 08, 09, 10</pre>
Line 103:
Since the output of the last element is different than the rest, the easiest way to accomplish this is by "breaking out" of the loop with a comparison to 10.
 
<syntaxhighlight lang="asm"> .model small
.model small
.stack 1024
 
Line 156 ⟶ 157:
pop ax
ret
end start ;EOF</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 239 ⟶ 241:
ACL2 does not have loops, but this is close:
 
<syntaxhighlight lang="lisp">(defun print-list (xs)
(defun print-list (xs)
(progn$ (cw "~x0" (first xs))
(if (endp (rest xs))
(cw (coerce '(#\Newline) 'string))
(progn$ (cw ", ")
(print-list (rest xs))))))</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Action!}}==
Line 258 ⟶ 262:
i==+1
OD
RETURN</syntaxhighlight>
</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/N_plus_one_half.png Screenshot from Atari 8-bit computer]
Line 266 ⟶ 271:
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Text_IO;
 
procedure LoopsAndHalf is
Line 276 ⟶ 282:
end loop;
Ada.Text_IO.new_line;
end LoopsAndHalf;</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">integer i;
integer i;
 
i = 0;
Line 290 ⟶ 298:
o_text(", ");
}
o_text("\n");</syntaxhighlight>
</syntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|ALGOL 60|OS/360}}
<syntaxhighlight lang="algol60">'BEGIN'
'BEGIN'
'COMMENT' Loops N plus one half - Algol60 - 20/06/2018;
'INTEGER' I;
Line 301 ⟶ 311:
'IF' I 'NOTEQUAL' 10 'THEN' OUTSTRING(1,'(', ')')
'END'
'END'</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 313 ⟶ 324:
There are three common ways of achieving n+½ loops:
{|border="1" style="border-collapse: collapse;"
||<syntaxhighlight lang="algol68"> FOR i WHILE
FOR i WHILE
print(whole(i, -2));
# WHILE # i < 10 DO
Line 320 ⟶ 332:
OD;
 
print(new line)</syntaxhighlight>
||</syntaxhighlight lang="algol68">FOR i TO 10 DO
||<syntaxhighlight lang="algol68">
FOR i TO 10 DO
print(whole(i, -2));
IF i < 10 THEN
Line 328 ⟶ 342:
OD;
 
print(new line)</syntaxhighlight>
||</syntaxhighlight lang="algol68">FOR i DO
||<syntaxhighlight lang="algol68">
FOR i DO
print(whole(i, -2));
IF i >= 10 THEN GO TO done FI;
Line 336 ⟶ 352:
OD;
done:
print(new line)</syntaxhighlight>
</syntaxhighlight>
|}
Output for all cases above:
Line 344 ⟶ 361:
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
begin
integer i;
i := 0;
Line 357 ⟶ 375:
writeon( "," )
end
end.
end.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
Six ways to do this task in Hopper flavour "Jambo":
<syntaxhighlight lang="c">
#include <jambo.h>
 
Main
i=1
Loop if ' Less equal (i,10)'
Set 'i', Print if ( #(i<10), ",","")
++i
Back
Prnl
Loop for ( i=1, #(i<=10), ++i )
Set 'i', Print only if ( #(10-i), ",")
Next
Prnl
i=1
Loop
Set 'i', Print only if ( #(10-i), ",")
++i
While ' #(i<=10) '
Prnl
i=1
Loop
Set 'i', Print only if ( #(10-i), ",")
++i
Until ' #(i>10) '
Prnl
i=1
Loop
Set 'i', and print it
Break if '#( !(10-i) )'
Set '","'
++i
Back
Prnl
 
/* assembler Hopper */
i=1
loop single:
{i,10,i} sub, do{{","}}, print
++i, {i,11}, jneq(loop single)
puts("\n")
End
</syntaxhighlight>
{{out}}
<pre>
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
</pre>
 
=={{header|AmigaE}}==
<syntaxhighlight lang="amigae">PROC main()
PROC main()
DEF i
FOR i := 1 TO 10
Line 367 ⟶ 442:
WriteF(', ')
ENDFOR
ENDPROC</syntaxhighlight>
</syntaxhighlight>
 
=={{header|ARM Assembly}}==
Line 503 ⟶ 579:
 
=={{header|ArnoldC}}==
<syntaxhighlight lang="arnoldc">IT'S SHOWTIME
IT'S SHOWTIME
HEY CHRISTMAS TREE n
YOU SET US UP @NO PROBLEMO
Line 522 ⟶ 599:
ENOUGH TALK
CHILL
YOU HAVE BEEN TERMINATED</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print join.with:", " map 1..10 => [to :string]</syntaxhighlight>
print join.with:", " map 1..10 => [to :string]
</syntaxhighlight>
 
{{out}}
 
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
=={{header|Asymptote}}==
<syntaxhighlight lang="Asymptote">
for(int i = 1; i <= 10; ++i) {
write(i, suffix=none);
if(i < 10) write(", ", suffix=none);
}
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Loop, 9 ; loop 9 times
Loop, 9 ; loop 9 times
{
output .= A_Index ; append the index of the current loop to the output var
Line 539 ⟶ 630:
output .= ", " ; append ", " to the output var
}
MsgBox, %output%</syntaxhighlight>
</syntaxhighlight>
 
=={{header|AutoIt}}==
<syntaxhighlight lang="autoit">#cs ----------------------------------------------------------------------------
#cs ----------------------------------------------------------------------------
 
AutoIt Version: 3.3.8.1
Line 567 ⟶ 660:
EndFunc
 
main()</syntaxhighlight>
</syntaxhighlight>
 
=={{header|AWK}}==
'''One-liner:'''
<syntaxhighlight lang="awk">$ awk 'BEGIN{for(i=1;i<=10;i++){printf i;if(i<10)printf ", "};print}'</syntaxhighlight>
$ awk 'BEGIN{for(i=1;i<=10;i++){printf i;if(i<10)printf ", "};print}'
</syntaxhighlight>
{{out}}
<pre>
Line 585 ⟶ 681:
}
print
}
}</syntaxhighlight>
</syntaxhighlight>
Same output.
 
=={{header|Axe}}==
<syntaxhighlight lang="axe">For(I,1,10)
For(I,1,10)
Disp I▶Dec
If I=10
Line 596 ⟶ 694:
Disp ","
End
End
End</syntaxhighlight>
</syntaxhighlight>
 
=={{header|BASIC}}==
{{works with|FreeBASIC}}
{{works with|QBasic|1.1}}
 
{{works with|QuickBasic|4.5}}
{{works with|RapidQ}}
<syntaxhighlight lang="qbasic">DIM i AS Integer
DIM i AS Integer
 
FOR i=1 TO 10
PRINT USING "##"; i;
IF i=10 THEN EXIT FOR
PRINT ", ";
NEXT i</syntaxhighlight>
</syntaxhighlight>
 
==={{header|Applesoft BASIC}}===
{{works with|Commodore BASIC}}
The [[#ZX_Spectrum_Basic|ZX Spectrum Basic]] code will work just fine in Applesoft BASIC. The following is a more structured approach which avoids the use of GOTO.
<syntaxhighlight lang="applesoftbasic">
 
<syntaxhighlight lang="applesoftbasic">10 FOR I = 1 TO 10
20 PRINT I;
30 IF I < 10 THEN PRINT ", "; : NEXT I</syntaxhighlight>
</syntaxhighlight>
 
==={{header|IS-BASICASIC}}===
If equality checking affect the efficiency or if the last value were treated in different manner, one can extract the last iteration.
<syntaxhighlight lang="is-basic">100 FOR I=1 TO 10
110 PRINT I;
120 IF I=10 THEN EXIT FOR
130 PRINT ",";
140 NEXT</syntaxhighlight>
 
ASIC prints integer numbers as six-char strings. If shorter ones are needed, one can use the <code>STR$</code> and <code>LTRIM$</code> functions.
==={{header|Sinclair ZX81 BASIC}}===
<syntaxhighlight lang="basic">
The [[#ZX_Spectrum_Basic|ZX Spectrum Basic]] program will work on the ZX81. Depending on the context, the programmer's intention may be clearer if we do it all with <code>GOTO</code>s instead of a <code>FOR</code> loop.
REM Loops/N plus one half
<syntaxhighlight lang="basic">10 LET I=1
FOR I = 1 TO 9
20 PRINT I;
PRINT I;
30 IF I=10 THEN GOTO 70
40 PRINT ", ";
NEXT I
50 LET I=I+1
PRINT 10
60 GOTO 20</syntaxhighlight>
END
 
</syntaxhighlight>
==={{header|ZX Spectrum Basic}}===
{{out}}
{{works with|Applesoft BASIC}}
<pre>
{{works with|Commodore BASIC}}
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
To terminate a loop on the ZX Spectrum, set the loop counter to a value that will exit the loop, before jumping to the NEXT statement.
</pre>
 
<syntaxhighlight lang="zxbasic">10 FOR i=1 TO 10
20 PRINT i;
30 IF i=10 THEN GOTO 50
40 PRINT ", ";
50 NEXT i</syntaxhighlight>
 
==={{header|Basic09}}===
Line 656 ⟶ 750:
PRINT
</syntaxhighlight>
 
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">for i = 1 to 10
for i = 1 to 10
print i;
if i < 10 then print ", ";
next i
 
end
end</syntaxhighlight>
</syntaxhighlight>
 
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> FOR i% = 1 TO 10
FOR i% = 1 TO 10
PRINT ; i% ;
IF i% <> 10 PRINT ", ";
NEXT
PRINT</syntaxhighlight>
</syntaxhighlight>
 
==={{header|bootBASIC}}===
There are no for/next or do/while loops in bootBASIC. Only goto.
<syntaxhighlight lang="BASIC">
10 a=1
20 print a ;
30 if a-10 goto 100
40 goto 130
100 print ", ";
110 a=a+1
120 goto 20
130 print
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">
10 rem Loops/N plus one half
20 c$ = ""
30 for i = 1 to 10
40 print c$;str$(i);
50 c$ = ", "
60 next i
</syntaxhighlight>
 
==={{header|FBSL}}===
<syntaxhighlight lang="qbasic">
#APPTYPE CONSOLE
For Dim i = 1 To 10
PRINT i;
IF i < 10 THEN PRINT ", ";
Next
PAUSE
</syntaxhighlight>
Output
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Press any key to continue...</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">
' FB 1.05.0 Win64
 
For i As Integer = 1 To 10
Print Str(i);
If i < 10 Then Print ", ";
Next
 
Print
Sleep
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
====Alternative====
This makes the important point that for many loops of this kind the partial iteration could easily be the ''first'' one.
<syntaxhighlight lang="freebasic">
dim as string cm = ""
for i as ubyte = 1 to 10
print cm;str(i);
cm = ", "
next i
</syntaxhighlight>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">
window 1
 
long i, num = 10
 
for i = 1 to num
print i;
if i = num then break
print @", ";
next i
 
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=c43cc581e5f93e70c5dc82733f609a7e Click this link to run this code]'''
<syntaxhighlight lang="gambas">
Public Sub Main()
Dim siLoop As Short
 
For siLoop = 1 To 10
Print siLoop;
If siLoop <> 10 Then Print ", ";
Next
 
End
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
==={{header|GW-BASIC}}===
<syntaxhighlight lang="gwbasic">
10 C$ = ""
20 FOR I = 1 TO 10
30 PRINT C$;STR$(I);
40 C$=", "
50 NEXT I
</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">
100 FOR I=1 TO 10
110 PRINT I;
120 IF I=10 THEN EXIT FOR
130 PRINT ",";
140 NEXT
</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
Keyword 'exit' allows the termination.
<syntaxhighlight lang="lb">
for i =1 to 10
print i;
if i =10 then exit for
print ", ";
next i
end
</syntaxhighlight>
 
==={{header|Microsoft Small Basic}}===
<syntaxhighlight lang="smallbasic">
For i = 1 To 10
TextWindow.Write(i)
If i <> 10 Then
TextWindow.Write(", ")
EndIf
EndFor
TextWindow.WriteLine("")
</syntaxhighlight>
 
==={{header|NS-HUBASIC}}===
<syntaxhighlight lang="ns-hubasic">
10 FOR I=1 TO 10
20 PRINT I;
30 IF I=10 THEN GOTO 50
40 PRINT ",";
50 NEXT
</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">
x=1
Repeat
Print(Str(x))
x+1
If x>10: Break: EndIf
Print(", ")
ForEver
</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">
FOR i = 1 TO 10
PRINT USING "##"; i;
IF i=10 THEN EXIT FOR
PRINT ", ";
NEXT i
</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">
FOR i = 1 TO 10
PRINT cma$;i;
cma$ = ", "
NEXT i
</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
The [[#ZX_Spectrum_Basic|ZX Spectrum Basic]] program will work on the ZX81. Depending on the context, the programmer's intention may be clearer if we do it all with <code>GOTO</code>s instead of a <code>FOR</code> loop.
<syntaxhighlight lang="basic">
10 LET I=1
20 PRINT I;
30 IF I=10 THEN GOTO 70
40 PRINT ", ";
50 LET I=I+1
60 GOTO 20
</syntaxhighlight>
 
==={{header|TI-89 BASIC}}===
There is no horizontal cursor position on the program IO screen, so we concatenate strings instead.
<syntaxhighlight lang="ti89b">
Local str
"" → str
For i,1,10
str & string(i) → str
If i < 10 Then
str & "," → str
EndIf
EndFor
Disp str
</syntaxhighlight>
 
==={{header|Tiny BASIC}}===
Tiny BASIC does not support string concatenation so each number is on a separate line but this is at least in the spirit of the task description.
<syntaxhighlight lang="tinybasic">
LET I = 1
10 IF I = 10 THEN PRINT I
IF I < 10 THEN PRINT I,", "
IF I = 10 THEN END
LET I = I + 1
GOTO 10
</syntaxhighlight>
 
A solution for the dialects of Tiny BASIC that support string concatenation.
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">
10 REM Loops/N plus one half
20 LET I = 1
30 IF I = 10 THEN PRINT I
40 IF I < 10 THEN PRINT I; ", ";
50 IF I = 10 THEN END
60 LET I = I + 1
70 GOTO 30
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">LET cm$ = ""
LET cm$ = ""
FOR i = 1 to 10
PRINT cm$; str$(i);
LET cm$ = ", "
NEXT i
END
END</syntaxhighlight>
</syntaxhighlight>
 
==={{header|VBA}}===
<syntaxhighlight lang="vb">
Public Sub WriteACommaSeparatedList()
Dim i As Integer
Dim a(1 To 10) As String
For i = 1 To 10
a(i) = CStr(i)
Next i
Debug.Print Join(a, ", ")
End Sub
</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
<syntaxhighlight lang="vbnet">
For i = 1 To 10
Console.Write(i)
If i = 10 Then Exit For
Console.Write(", ")
Next
</syntaxhighlight>
 
==={{header|Wee Basic}}===
print 1 "" ensures the end of program text is separate from the list of numbers.
<syntaxhighlight lang="wee basic">
print 1 ""
for numbers=1 to 10
print 1 at numbers*3-2,0 numbers
if numbers<>10
print 1 at numbers*3-1,0 ","
endif
end
</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
PROGRAM "n_plus_one_half"
 
DECLARE FUNCTION Entry()
 
FUNCTION Entry()
FOR i% = 1 TO 10
PRINT i%;
IF i% = 10 THEN EXIT FOR
PRINT ",";
NEXT i%
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
If equality checking affect the efficiency or if the last value were treated in different manner, one can extract the last iteration.
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
PROGRAM "n_plus_one_half"
 
DECLARE FUNCTION Entry()
 
FUNCTION Entry()
FOR i% = 1 TO 9
PRINT i%; ",";
NEXT i%
PRINT 10
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">
for i = 1 to 10
print i;
if i < 10 print ", ";
next i
print
end
</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
{{works with|Applesoft BASIC}}
{{works with|Commodore BASIC}}
To terminate a loop on the ZX Spectrum, set the loop counter to a value that will exit the loop, before jumping to the NEXT statement.
<syntaxhighlight lang="zxbasic">
10 FOR i=1 TO 10
20 PRINT i;
30 IF i=10 THEN GOTO 50
40 PRINT ", ";
50 NEXT i
</syntaxhighlight>
 
=={{header|bc}}==
{{Works with|GNU bc}}
The <code>print</code> extension is necessary to get the required output.
<syntaxhighlight lang="bc">while (1) {
while (1) {
print ++i
if (i == 10) {
Line 693 ⟶ 1,123:
}
print ", "
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Befunge}}==
<syntaxhighlight lang="befunge">1+>::.9`#@_" ,",,</syntaxhighlight>
1+>::.9`#@_" ,",,
</syntaxhighlight>
This code is a good answer. However, most Befunge implementations print a " " after using . (output number), so this program prints "1 , 2 , 3 ..." with extra spaces. A bypass for this is possible, by adding 48 and printing the ascii character, but does not work with 10::
<syntaxhighlight lang="befunge">1+>::68*+,8`#v_" ,",,
1+>::68*+,8`#v_" ,",,
@,,,,", 10"<</syntaxhighlight>
@,,,,", 10"<
</syntaxhighlight>
 
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat"> 1:?i
1:?i
& whl
' ( put$!i
& !i+1:~>10:?i
& put$", "
)</syntaxhighlight>
</syntaxhighlight>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdio.h>
 
int main()
Line 721 ⟶ 1,159:
}
return 0;
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System;
 
class Program
Line 738 ⟶ 1,178:
Console.WriteLine();
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <iostream>
int main()
Line 756 ⟶ 1,198:
 
=={{header|Chapel}}==
<syntaxhighlight lang="chapel">for i in 1..10 do
for i in 1..10 do
write(i, if i % 10 > 0 then ", " else "\n")</syntaxhighlight>
write(i, if i % 10 > 0 then ", " else "\n")
</syntaxhighlight>
 
=={{header|Clojure}}==
Line 775 ⟶ 1,219:
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. Loop-N-And-Half.
 
Line 801 ⟶ 1,246:
 
GOBACK
.</syntaxhighlight>
</syntaxhighlight>
Free-form, 'List'-free version, using DISPLAY NO ADVANCING.
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. LOOP-1p5-NOADV.
DATA DIVISION.
Line 820 ⟶ 1,267:
END-PERFORM.
STOP RUN.
END-PROGRAM.</syntaxhighlight>
</syntaxhighlight>
Free-form, GO TO, 88-level. Paragraphs in PROCEDURE DIVISION.
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. LOOP-1p5-NOADV-GOTO.
DATA DIVISION.
Line 839 ⟶ 1,288:
02-DONE.
STOP RUN.
END-PROGRAM.</syntaxhighlight>
</syntaxhighlight>
Using 'PERFORM VARYING'
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. LOOP-1p5-NOADV-VARY.
DATA DIVISION.
Line 857 ⟶ 1,308:
END-PERFORM.
STOP RUN.
END-PROGRAM.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|CoffeeScript}}==
Line 878 ⟶ 1,330:
=={{header|ColdFusion}}==
With tags:
<syntaxhighlight lang="cfm"><cfloop index = "i" from = "1" to = "10">
<cfloop index = "i" from = "1" to = "10">
#i#
<cfif i EQ 10>
Line 884 ⟶ 1,337:
</cfif>
,
</cfloop></syntaxhighlight>
</syntaxhighlight>
 
With script:
<syntaxhighlight lang="cfm"><cfscript>
<cfscript>
for( i = 1; i <= 10; i++ ) //note: the ++ notation works only on version 8 up, otherwise use i=i+1
{
Line 898 ⟶ 1,353:
writeOutput( ", " );
}
</cfscript></syntaxhighlight>
</syntaxhighlight>
 
=={{header|Common Lisp}}==
Line 909 ⟶ 1,365:
or
 
<syntaxhighlight lang="lisp">(loop for i from 1 upto 10 do
(loop for i from 1 upto 10 do
(princ i)
(if (= i 10) (return))
(princ ", "))</syntaxhighlight>
</syntaxhighlight>
 
but for such simple tasks we can use format's powers:
Line 944 ⟶ 1,402:
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "cowgol.coh";
 
var i: uint8 := 1;
Line 953 ⟶ 1,412:
i := i + 1;
end loop;
print_nl();</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 961 ⟶ 1,421:
=={{header|D}}==
===Iterative===
<syntaxhighlight lang="d">import std.stdio;
import std.stdio;
void main() {
Line 972 ⟶ 1,433:
 
writeln();
}
}</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
===Functional Style===
<syntaxhighlight lang="d">void main() {
void main() {
import std.stdio, std.range, std.algorithm, std.conv, std.string;
iota(1, 11).map!text.join(", ").writeln;
Line 982 ⟶ 1,445:
// A simpler solution:
writefln("%(%d, %)", iota(1, 11));
}
}</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Line 988 ⟶ 1,452:
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">String loopPlusHalf(start, end) {
String loopPlusHalf(start, end) {
var result = '';
for(int i = start; i <= end; i++) {
Line 1,002 ⟶ 1,467:
void main() {
print(loopPlusHalf(1, 10));
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Delphi}}==
 
<syntaxhighlight lang="delphi">program LoopsNPlusOneHalf;
program LoopsNPlusOneHalf;
 
{$APPTYPE CONSOLE}
Line 1,022 ⟶ 1,489:
end;
Writeln;
end.
end.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec main() void:
proc nonrec main() void:
byte i;
i := 1;
Line 1,035 ⟶ 1,504:
write(", ")
od
corp
corp</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
=={{header|DWScript}}==
 
<syntaxhighlight lang="delphi">var i : Integer;
var i : Integer;
 
for i := 1 to 10 do begin
Line 1,046 ⟶ 1,517:
if i < 10 then
Print(', ');
end;
end;</syntaxhighlight>
</syntaxhighlight>
 
=={{header|E}}==
 
A typical loop+break solution:
<syntaxhighlight lang="e">var i := 1
var i := 1
while (true) {
print(i)
Line 1,057 ⟶ 1,530:
print(", ")
i += 1
}
}</syntaxhighlight>
</syntaxhighlight>
 
Using the loop primitive in a semi-functional style:
 
<syntaxhighlight lang="e">var i := 1
var i := 1
__loop(fn {
print(i)
Line 1,071 ⟶ 1,546:
true
}
})
})</syntaxhighlight>
</syntaxhighlight>
 
=={{header|EasyLAngEasyLang}}==
<syntaxhighlight lang="easylang">
for i = 1 to 10
list$ &=write i
if i =< 10
breakwrite 1", "
.
list$ &= ", "
.
print list$
</syntaxhighlight>
 
Line 1,099 ⟶ 1,573:
 
=={{header|EDSAC order code}}==
<syntaxhighlight lang="edsac">[ N and a half times loop
[ N and a half times loop
=======================
 
Line 1,139 ⟶ 1,614:
[ 25 ] PF
 
EZPF</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
===Alternative===
In the original EDSAC solution, the last number is printed outside the loop. My understanding of the task is that all numbers should be printed inside the loop, and control should then pass out of the loop without printing a comma after the last number.
 
To avoid dragging in a number-printing subroutine just to print 10, the output from the subroutines below has been changed to the single digits 0..9.
 
The first subroutine sticks to the task description: "in the last iteration, execute only part of the loop body". Two conditional jumps are encountered each time round the loop. The second subroutine skips the first part of the first iteration. This may not exactly fit the task description, but it has the advantage of requiring only one conditional jump within the loop. Cf. the S-lang solution and the comment by its author.
<syntaxhighlight lang="edsac">
[Loop-and-a-half for Rosetta Code.
EDSAC program, Initial Orders 2.]
T64K [load at location 64 (arbitrary choice)]
GK [set @ (theta) parameter]
[Constants]
[0] OF [digit 9]
[1] JF [to inc digit after subtracting 9]
[2] #F [figures mode]
[3] !F [space]
[4] NF [comma (in figures mode)]
[5] @F [carriage return]
[6] &F [line feed]
[Variable]
[7] PF [current digit]
 
[First subroutine to print digits 0..9]
[8] A3F T20@ [plant return link as usual]
T7@ [digit := 0]
[11] O7@ [loop: print digit]
A7@ S@ [is digit 9?]
E20@ [if so, jump to exit]
O4@ O3@ [print comma and space]
A1@ [inc digit in acc]
T7@ [update digit in store]
E11@ [always loop back]
[20] ZF [(planted) return to caller with acc = 0]
 
[Second subroutine to print digits 0..9
Instead of saying "print a comma after each digit except the last"
it says "print a comma before each digit except the first".]
[21] A3F T33@ [plant return link as usual]
T7@ [digit := 0]
E29@ [jump into middle of loop]
[25] A1@ [loop: inc digit in acc]
T7@ [update digit in store]
O4@ O3@ [print comma and space]
[29] O7@ [print digit]
A7@ S@ [is digit 9?]
G25@ [if not, loop back]
[33] ZF [(planted) return to caller with acc = 0]
 
[Enter with acc = 0]
[34] O2@ [set teleprinter to figures]
A35@ G8@ [call first subroutine]
O5@ O6@ [print CR, LF]
A39@ G21@ [call second subroutine]
O5@ O6@ [print CR, LF]
O2@ [print dummy character to flush teleprinter buffer]
ZF [stop]
E34Z [define entry point]
PF [value of acc on entry (here = 0)]
[end]
</syntaxhighlight>
{{out}}
<pre>
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Loops do
defmodule Loops do
def n_plus_one_half([]), do: IO.puts ""
def n_plus_one_half([x]), do: IO.puts x
Line 1,153 ⟶ 1,696:
end
 
Enum.to_list(1..10) |> Loops.n_plus_one_half</syntaxhighlight>
</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
int LAST_ITERATION = 10
for int i = 1; i <= LAST_ITERATION ; ++i
write(i)
if i == LAST_ITERATION do break end
write(", ")
end
writeLine()
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">%% Implemented by Arjun Sunel
%% Implemented by Arjun Sunel
-module(loop).
-export([main/0]).
Line 1,209 ⟶ 1,769:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">: print-comma-list ( n -- )
: print-comma-list ( n -- )
[ [1,b] ] keep '[
[ number>string write ]
[ _ = [ ", " write ] unless ] bi
] each nl ;</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Falcon}}==
<syntaxhighlight lang="falcon">for value = 1 to 10
for value = 1 to 10
formiddle
>> value
Line 1,222 ⟶ 1,785:
end
forlast: > value
end
end</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 1,229 ⟶ 1,793:
 
=={{header|FALSE}}==
<syntaxhighlight lang="false">1[$9>~][$.", "1+]#.</syntaxhighlight>
1[$9>~][$.", "1+]#.
</syntaxhighlight>
 
=={{header|Fantom}}==
Line 1,248 ⟶ 1,814:
}
</syntaxhighlight>
 
=={{header|FBSL}}==
<syntaxhighlight lang="qbasic">
#APPTYPE CONSOLE
For Dim i = 1 To 10
PRINT i;
IF i < 10 THEN PRINT ", ";
Next
PAUSE</syntaxhighlight>
Output
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Press any key to continue...</pre>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: comma-list ( n -- )
: comma-list ( n -- )
dup 1 ?do i 1 .r ." , " loop
. ;</syntaxhighlight>
</syntaxhighlight>
 
<syntaxhighlight lang="forth">: comma-list ( n -- )
: comma-list ( n -- )
dup 1+ 1 do
i 1 .r
dup i = if leave then \ or DROP UNLOOP EXIT to exit loop and the function
[char] , emit space
loop drop ;</syntaxhighlight>
</syntaxhighlight>
 
<syntaxhighlight lang="forth">: comma-list ( n -- )
: comma-list ( n -- )
1
begin dup 1 .r
2dup <>
while ." , " 1+
repeat 2drop ;</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Fortran}}==
{{works with|FORTRAN|IV and later}}
<syntaxhighlight lang="fortran">C Loops N plus one half - Fortran IV (1964)
C Loops N plus one half - Fortran IV (1964)
INTEGER I
WRITE(6,301) (I,I=1,10)
301 FORMAT((I3,','))
END</syntaxhighlight>
</syntaxhighlight>
 
{{works with|Fortran|77 and later}}
 
<syntaxhighlight lang="fortran">C WARNING: This program is not valid ANSI FORTRAN 77 code. It uses
C WARNING: This program is not valid ANSI FORTRAN 77 code. It uses
C two nonstandard characters on the lines labelled 5001 and 5002.
C Many F77 compilers should be okay with it, but it is *not*
Line 1,338 ⟶ 1,901:
C5001 FORMAT (T3, ADVANCE='NO')
C5001 FORMAT (A, ADVANCE='NO')
END</syntaxhighlight>
</syntaxhighlight>
 
{{Works with|Fortran|90 and later}}
 
<syntaxhighlight lang="fortran">i = 1
i = 1
do
write(*, '(I0)', advance='no') i
Line 1,349 ⟶ 1,914:
i = i + 1
end do
write(*,*)</syntaxhighlight>
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
For i As Integer = 1 To 10
Print Str(i);
If i < 10 Then Print ", ";
Next
 
Print
Sleep</syntaxhighlight>
 
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
===Alternative===
This makes the important point that for many loops of this kind the partial iteration could easily be the ''first'' one.
 
<syntaxhighlight lang="freebasic">dim as string cm = ""
for i as ubyte = 1 to 10
print cm;str(i);
cm = ", "
next i</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1
 
long i, num = 10
 
for i = 1 to num
print i;
if i = num then break
print @", ";
next i
 
HandleEvents</syntaxhighlight>
Output:
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=c43cc581e5f93e70c5dc82733f609a7e Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siLoop As Short
 
For siLoop = 1 To 10
Print siLoop;
If siLoop <> 10 Then Print ", ";
Next
 
End</syntaxhighlight>
Output:
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|GAP}}==
<syntaxhighlight lang="gap">n := 10;
n := 10;
for i in [1 .. n] do
Print(i);
Line 1,418 ⟶ 1,927:
Print("\n");
fi;
od;
od;</syntaxhighlight>
</syntaxhighlight>
 
=={{header|GML}}==
<syntaxhighlight lang="gml">str = ""
str = ""
for(i = 1; i <= 10; i += 1)
{
Line 1,428 ⟶ 1,939:
str += ", "
}
show_message(str)</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
package main
 
import "fmt"
Line 1,444 ⟶ 1,957:
fmt.Print(", ")
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Gosu}}==
<syntaxhighlight lang="java">var out = System.out
var out = System.out
for(i in 1..10) {
if(i > 1) out.print(", ")
out.print(i)
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<syntaxhighlight lang="groovy">for(i in (1..10)) {
for(i in (1..10)) {
print i
if (i == 10) break
print ', '
}
}</syntaxhighlight>
</syntaxhighlight>
 
Output:
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">10 C$ = ""
20 FOR I = 1 TO 10
30 PRINT C$;STR$(I);
40 C$=", "
50 NEXT I</syntaxhighlight>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">main :: IO ()
main :: IO ()
main = forM_ [1 .. 10] $ \n -> do
putStr $ show n
putStr $ if n == 10 then "\n" else ", "</syntaxhighlight>
</syntaxhighlight>
 
You can also use intersperse :: a -> [a] -> [a]
<syntaxhighlight lang="haskell">intercalate ", " (map show [1..10])</syntaxhighlight>
intercalate ", " (map show [1..10])
</syntaxhighlight>
 
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">for (i in 1...11)
for (i in 1...11)
Sys.print('$i${i == 10 ? '\n' : ', '}');</syntaxhighlight>
Sys.print('$i${i == 10 ? '\n' : ', '}');
</syntaxhighlight>
 
=={{header|hexiscript}}==
<syntaxhighlight lang="hexiscript">for let i 1; i <= 10; i++
for let i 1; i <= 10; i++
print i
if i = 10; break; endif
print ", "
endfor
println ""</syntaxhighlight>
</syntaxhighlight>
 
=={{header|HicEst}}==
<syntaxhighlight lang="hicest">DO i = 1, 10
DO i = 1, 10
WRITE(APPend) i
IF(i < 10) WRITE(APPend) ", "
ENDDO</syntaxhighlight>
</syntaxhighlight>
 
=={{header|HolyC}}==
<syntaxhighlight lang="holyc">U8 i, max = 10;
U8 i, max = 10;
for (i = 1; i <= max; i++) {
Print("%d", i);
Line 1,505 ⟶ 2,027:
Print(", ");
}
Print("\n");</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<syntaxhighlight lang="icon">procedure main()
procedure main()
every writes(i := 1 to 10) do
if i = 10 then break write()
else writes(", ")
end
end</syntaxhighlight>
</syntaxhighlight>
 
The above can be written more succinctly as:
<syntaxhighlight lang="icon">every writes(c := "",1 to 10) do c := ","
every writes(c := "",1 to 10) do c := ","
</syntaxhighlight>
 
Line 1,522 ⟶ 2,048:
Nobody would ever use a loop in IDL to output a vector of numbers - the requisite output would be generated something like this:
 
<syntaxhighlight lang="idl">print,indgen(10)+1,format='(10(i,:,","))'</syntaxhighlight>
print,indgen(10)+1,format='(10(i,:,","))'
</syntaxhighlight>
 
However if a loop had to be used it could be done like this:
 
<syntaxhighlight lang="idl">for i=1,10 do begin
for i=1,10 do begin
print,i,format='($,i)'
if i lt 10 then print,",",format='($,a)'
endfor</syntaxhighlight>
</syntaxhighlight>
 
(which merely suppresses the printing of the comma in the last iteration);
Line 1,535 ⟶ 2,065:
or like this:
 
<syntaxhighlight lang="idl">for i=1,10 do begin
for i=1,10 do begin
print,i,format='($,i)'
if i eq 10 then break
print,",",format='($,a)'
end
end</syntaxhighlight>
</syntaxhighlight>
 
(which terminates the loop early if the last element is reached).
 
=={{header|J}}==
<syntaxhighlight lang="j">output=: verb define
output=: verb define
buffer=: buffer,y
)
Line 1,556 ⟶ 2,089:
end.
end.
smoutputecho buffer
)
)</syntaxhighlight>
</syntaxhighlight>
 
Example use:
<syntaxhighlight lang="j"> loopy 0
loopy 0
1, 2, 3, 4, 5, 6, 7, 8, 9, 10</syntaxhighlight>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
 
That said, note that neither loops nor output statements are necessary:
 
<syntaxhighlight lang="j"> ;}.,(', ' ; ":)&> 1+i.10
1, 2, 3, 4;}., 5(', 6,' 7,; 8, 9,":)&> 1+i.10</syntaxhighlight>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
 
And, note also that this sort of data driven approach can also deal with more complex issues:
 
<syntaxhighlight lang="j"> commaAnd=: ":&.> ;@,. -@# {. (<;._1 '/ and /') ,~ (<', ') #~ #
commaAnd=: ":&.> ;@,. -@# {. (<;._1 '/ and /') ,~ (<', ') #~ #
commaAnd i.5
0, 1, 2, 3 and 4</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">public static void main(String[] args) {
public static void main(String[] args) {
for (int i = 1; ; i++) {
System.out.print(i);
Line 1,583 ⟶ 2,124:
}
System.out.println();
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">function loop_plus_half(start, end) {
function loop_plus_half(start, end) {
var str = '',
i;
Line 1,599 ⟶ 2,142:
}
alert(loop_plus_half(1, 10));</syntaxhighlight>
</syntaxhighlight>
 
Alternatively, if we stand back for a moment from imperative assumptions about the nature and structure of computing tasks, it becomes clear that the problem of special transitional cases as a pattern terminates has no necessary connection with loops. (See the comments accompanying the ACL2, Haskell, IDL, J and R examples above and below, and see also some of the approaches taken in languages like Clojure and Scala.
Line 1,605 ⟶ 2,149:
If a JavaScript expression evaluates to an array [1 .. 10] of integers, for example, we can map that array directly to a comma-delimited string by using the '''Array.join()''' function, writing something like:
 
<syntaxhighlight lang="javascript">function range(m, n) {
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(
function (x, i) {
Line 1,615 ⟶ 2,160:
console.log(
range(1, 10).join(', ')
);
);</syntaxhighlight>
</syntaxhighlight>
 
Output:
<syntaxhighlight lang="javascript">1, 2, 3, 4, 5, 6, 7, 8, 9, 10</syntaxhighlight>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
 
Otherwise, any special transitional case at the end of a pattern can be handled by defining conditional values for one or more sub-expressions:
 
<syntaxhighlight lang="javascript">function range(m, n) {
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
Line 1,642 ⟶ 2,191:
)
})(1, 10)
);
);</syntaxhighlight>
</syntaxhighlight>
 
Output:
<syntaxhighlight lang="javascript">1, 2, 3, 4, 5, 6, 7, 8, 9, 10</syntaxhighlight>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
==== Otherwise ====
<syntaxhighlight lang="javascript">var s=1, e=10
var s=1, e=10
for (var i=s; i<=e; i+=1) {
document.write( i==s ? '' : ', ', i )
}
}</syntaxhighlight>
</syntaxhighlight>
or
<syntaxhighlight lang="javascript">var s=1, e=10
var s=1, e=10
for (;; s+=1) {
document.write( s )
if (s==e) break
document.write( ', ' )
}
}</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 1,665 ⟶ 2,221:
=={{header|jq}}==
In jq, it is idiomatic to view a range of integers with boundaries m and n as [m, n), i.e. including m but excluding n.
<syntaxhighlight lang="jq">One approach is to construct the answer incrementally:
One approach is to construct the answer incrementally:
def loop_plus_half(m;n):
if m<n then reduce range(m+1;n) as $i (m|tostring; . + ", " + ($i|tostring))
Line 1,673 ⟶ 2,230:
# An alternative that is shorter and perhaps closer to the task description because it uses range(m;n) is as follows:
def loop_plus_half2(m;n):
[range(m;n) | if . == m then . else ", ", . end | tostring] | join("");</syntaxhighlight>
</syntaxhighlight>
{{Out}}
loop_plus_half2(1;11)
Line 1,693 ⟶ 2,251:
 
=={{header|K}}==
<syntaxhighlight lang="k"> p:{`0:$x} / output
p:{`0:$x} / output
i:1;do[10;p[i];p[:[i<10;", "]];i+:1];p@"\n"
1, 2, 3, 4, 5, 6, 7, 8, 9, 10</syntaxhighlight>
</syntaxhighlight>
 
Alternative solutions:
<syntaxhighlight lang="k"> 10 {p@x;p@:[x<10;", ";"\n"];x+1}\1;
10 {p@x;p@:[x<10;", ";"\n"];x+1}'\1+!10; /variant</syntaxhighlight>
{p@x;p@:[x<10;", ";"\n"];x+1}'1+!10; /variant
</syntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
// version 1.0.6
 
fun main(args: Array<String>) {
Line 1,709 ⟶ 2,272:
if (i < 10) print(", ")
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 1,733 ⟶ 2,297:
6, 7, 8, 9, 10. (end of loop with a dot)
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
# Loop
$i = 1
loop {
fn.print($i)
if($i === 10) {
con.break
}
fn.print(\,\s)
$i += 1
}
fn.println()
 
# Array generate from and join
fn.println(fn.join(\,\s, fn.arrayGenerateFrom(fn.inc, 10)))
</syntaxhighlight>
 
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|Lang5}}==
<syntaxhighlight lang="lang5">: , dup ", " 2 compress "" join ;
: , dup ", " 2 compress "" join ;
1 do dup 10 != if dup , . 1 + else . break then loop</syntaxhighlight>
1 do dup 10 != if dup , . 1 + else . break then loop
</syntaxhighlight>
Word: [[Loops/For#Lang5]]
<syntaxhighlight lang="lang5">: 2string 2 compress "" join ;
: 2string 2 compress "" join ;
: , dup 10 != if ", " 2string then ;
1 10 "dup , . 1+" times</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">local(out) = ''
local(out) = ''
loop(10) => {
#out->append(loop_count)
Line 1,749 ⟶ 2,344:
#out->append(', ')
}
#out
#out</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Lhogho}}==
<code>type</code> doesn't output a newline. The <code>print</code> outputs one.
<syntaxhighlight lang="logo">for "i [1 10]
for "i [1 10]
[
type :i
Line 1,761 ⟶ 2,358:
]
]
print</syntaxhighlight>
</syntaxhighlight>
 
A more list-y way of doing it
 
<syntaxhighlight lang="logo">to join :lst :sep
to join :lst :sep
if list? :lst
[
Line 1,780 ⟶ 2,379:
 
make "aList [1 2 3 4 5 6 7 8 9 10]
print join :aList "|, |</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
Keyword 'exit' allows the termination.
<syntaxhighlight lang="lb">
for i =1 to 10
print i;
if i =10 then exit for
print ", ";
next i
end
</syntaxhighlight>
 
=={{header|Lisaac}}==
<syntaxhighlight lang="lisaac">Section Header
Section Header
 
+ name := LOOP_AND_HALF;
Line 1,812 ⟶ 2,402:
};
'\n'.print;
);
);</syntaxhighlight>
</syntaxhighlight>
 
=={{header|LiveCode}}==
<syntaxhighlight lang="livecode">repeat with n = 1 to 10
repeat with n = 1 to 10
put n after loopn
if n is not 10 then put comma after loopn
end repeat
put loopn</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Logo}}==
<syntaxhighlight lang="logo">to comma.list :n
to comma.list :n
repeat :n-1 [type repcount type "|, |]
print :n
end
 
comma.list 10</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Lua}}==
Translation of C:
<syntaxhighlight lang="lua">for i = 1, 10 do
for i = 1, 10 do
io.write(i)
if i == 10 then break end
io.write", "
end
end</syntaxhighlight>
</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 1,869 ⟶ 2,466:
 
=={{header|M4}}==
<syntaxhighlight lang="m4">define(`break',
define(`break',
`define(`ulim',llim)')
define(`for',
Line 1,878 ⟶ 2,476:
 
for(`x',`1',`',
`x`'ifelse(x,10,`break',`, ')')</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Make}}==
<syntaxhighlight lang="make">NEXT=`expr $* + 1`
NEXT=`expr $* + 1`
MAX=10
RES=1
Line 1,891 ⟶ 2,491:
 
%-n:
@-make -f loop.mk $(NEXT)-n MAX=$(MAX) RES=$(RES),$(NEXT)</syntaxhighlight>
</syntaxhighlight>
 
Invoking it
Line 1,898 ⟶ 2,499:
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">> for i to 10 do printf( "%d%s", i, `if`( i = 10, "\n", ", " ) ) end:
> for i to 10 do printf( "%d%s", i, `if`( i = 10, "\n", ", " ) ) end:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10</syntaxhighlight>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">i = 1; s = "";
i = 1; s = "";
While[True,
s = s <> ToString@i;
Line 1,909 ⟶ 2,513:
i++;
]
s
s</syntaxhighlight>
</syntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
Vectorized form:
<syntaxhighlight lang="matlab"> printf('%i, ',1:9); printf('%i\n',10);</syntaxhighlight>
printf('%i, ',1:9); printf('%i\n',10);
</syntaxhighlight>
 
Explicite loop:
<syntaxhighlight lang="matlab"> for k=1:10,
for k=1:10,
printf('%i', k);
if k==10, break; end;
printf(', ');
end;
printf('\n');</syntaxhighlight>
</syntaxhighlight>
 
Output:
Line 1,927 ⟶ 2,536:
 
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">for i in 1 to 10 do
for i in 1 to 10 do
(
format "%" i
if i == 10 then exit
format "%" ", "
)
)</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Metafont}}==
Line 1,939 ⟶ 2,550:
and then we output it.
 
<syntaxhighlight lang="metafont">last := 10;
last := 10;
string s; s := "";
for i = 1 upto last:
Line 1,946 ⟶ 2,558:
endfor
message s;
end
end</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
<syntaxhighlight lang="smallbasic">For i = 1 To 10
TextWindow.Write(i)
If i <> 10 Then
TextWindow.Write(", ")
EndIf
EndFor
TextWindow.WriteLine("")</syntaxhighlight>
 
=={{header|min}}==
min's <code>linrec</code> combinator is flexible enough to easily accomodate this task, due to taking a quotation to execute at the end of the loop (the second one).
<syntaxhighlight lang="min">1 (dup 10 ==) 'puts! (print succ ", " print!) () linrec</syntaxhighlight>
1 (dup 10 ==) 'puts! (print succ ", " print!) () linrec
</syntaxhighlight>
 
=={{header|Modula-3}}==
<syntaxhighlight lang="modula3">MODULE Loop EXPORTS Main;
MODULE Loop EXPORTS Main;
 
IMPORT IO, Fmt;
Line 1,976 ⟶ 2,583:
END;
IO.Put("\n");
END Loop.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|MUMPS}}==
<syntaxhighlight lang="mumps">LOOPHALF
LOOPHALF
NEW I
FOR I=1:1:10 DO
Line 1,987 ⟶ 2,596:
;Alternate
NEW I FOR I=1:1:10 WRITE I WRITE:I'=10 ", "
KILL I QUIT</syntaxhighlight>
</syntaxhighlight>
Output:<pre>USER>D LOOPHALF^ROSETTA
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Line 1,995 ⟶ 2,605:
 
=={{header|Nemerle}}==
<syntaxhighlight lang="nemerle">foreach (i in [1 .. 10])
foreach (i in [1 .. 10])
{
Write(i);
unless (i == 10) Write(", ");
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|NetRexx}}==
<syntaxhighlight lang="netrexx">/* NetRexx */
/* NetRexx */
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
Line 2,018 ⟶ 2,631:
end
end i_
say rs.strip()</syntaxhighlight>
</syntaxhighlight>
'''Output'''
<pre>Loops/N plus one half
Line 2,024 ⟶ 2,638:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">(for (i 0 10)
(for (i 0 10)
(print i)
(unless (= i 10)
(print ", ")))</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">var s = ""
var s = ""
for i in 1..10:
s.add $i
if i == 10: break
s.add ", "
echo s</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
=={{header|NS-HUBASICNu}}==
<syntaxhighlight lang="ns-hubasicnu">10 FOR I=1 TO 10
for i in 1.. {
20 PRINT I;
print -n $i
30 IF I=10 THEN GOTO 50
if $i == 10 {
40 PRINT ",";
print ""
50 NEXT</syntaxhighlight>
break
}
print -n ", "
}
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
 
=={{header|Oberon-07}}==
===Using a FOR loop===
Should also work with Oberon-2.
<syntaxhighlight lang="modula2">
MODULE LoopsNPlusOneHalf1;
IMPORT Out;
 
VAR i :INTEGER;
 
BEGIN
FOR i := 1 TO 10 DO
Out.Int( i, 0 );
IF i < 10 THEN Out.String( ", " ) END
END
END LoopsNPlusOneHalf1.
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
===Using an extended WHILE loop===
Whilst probably not the best way of handling the task, this example demonstrates the extended WHILE loop available in Oberon-07.<br/>
The WHILE loop can have multiple conditions and DO pats. The conditions following WHILE and ELSIF are evaluated in order and the DO part corresponding to the first TRUE one is executed.<br/>
The loop terminates when all the conditions evaluate to FALSE
<syntaxhighlight lang="modula2">
MODULE LoopsNPlusOneHalf;
IMPORT Out;
 
VAR i :INTEGER;
 
BEGIN
i := 0;
WHILE i < 9 DO
i := i + 1;
Out.Int( i, 0 );
Out.String( ", " )
ELSIF i < 10 DO
i := i + 1;
Out.Int( i, 0 )
END
END LoopsNPlusOneHalf.
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|Objeck}}==
Line 2,066 ⟶ 2,742:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let last = 10 in
let last = 10 in
for i = 1 to last do
print_int i;
Line 2,072 ⟶ 2,749:
print_string ", ";
done;
print_newline();</syntaxhighlight>
</syntaxhighlight>
 
<syntaxhighlight lang="ocaml">let ints = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] in
let ints = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] in
let str_ints = List.map string_of_int ints in
print_endline (String.concat ", " str_ints);</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">: loopn
: loopn
| i |
10 loop: i [ i dup print 10 ifEq: [ break ] "," . ] printcr ;</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Oz}}==
Line 2,091 ⟶ 2,773:
if N == 10 then {Break} end
{System.printInfo ", "}
end
end</syntaxhighlight>
</syntaxhighlight>
 
However, it seems more natural to use a left fold:
<syntaxhighlight lang="oz">declare
declare
fun {CommaSep Xs}
case Xs of nil then nil
Line 2,104 ⟶ 2,788:
end
in
{System.showInfo {CommaSep {List.number 1 10 1}}}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Panda}}==
Panda is stream based. To know if there is no more values you need to know it's the last. You can only do that if you get all the values. So this is functional style; We accumulate all the values from the stream. Then join them together as strings with a comma.
<syntaxhighlight lang="panda">array{{1..10}}.join(',')</syntaxhighlight>
array{{1..10}}.join(',')
</syntaxhighlight>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">n=0;
n=0;
while(1,
print1(n++);
if(n>9, break);
print1(", ")
);
);</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">program numlist(output);
program numlist(output);
 
const MAXNUM: integer = 10;
Line 2,138 ⟶ 2,828:
write(i, ', ');
writeln(MAXNUM);
end.
end.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Peloton}}==
<syntaxhighlight lang="sgml"><@ FORLITLIT>10|<@ SAYPOSFOR>...</@><@ ABF>,</@></@></syntaxhighlight>
<@ FORLITLIT>10|<@ SAYPOSFOR>...</@><@ ABF>,</@></@>
</syntaxhighlight>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">for my $i(1..10) {
for my $i(1..10) {
print $i;
last if $i == 10;
print ', ';
}
print "\n";</syntaxhighlight>
</syntaxhighlight>
 
In perl one would solve the task via <code>join</code>.
<syntaxhighlight lang="perl">print join(', ', 1..10), "\n";</syntaxhighlight>
print join(', ', 1..10), "\n";
</syntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">-->
-->
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
Line 2,161 ⟶ 2,859:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--
<!--</syntaxhighlight>-->
</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/N_plus_one_half
/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/N_plus_one_half
by Galileo, 11/2022 #/
 
10 for dup print 10 == not if ", " print endif endfor</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Line 2,173 ⟶ 2,874:
 
=={{header|PHP}}==
<syntaxhighlight lang="php">for ($i = 1; $i <= 11; $i++) {
for ($i = 1; $i <= 11; $i++) {
echo $i;
if ($i == 10)
Line 2,179 ⟶ 2,881:
echo ', ';
}
echo "\n";</syntaxhighlight>
</syntaxhighlight>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(for N 10
(for N 10
(prin N)
(T (= N 10))
(prin ", ") )</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Pike}}==
Line 2,198 ⟶ 2,903:
}
write("\n");
}
}</syntaxhighlight>
</syntaxhighlight>
 
The most idiomatic way of inserting delimiters in Pike is the multiplication operator and it's symmetry with division of strings.
<syntaxhighlight lang="text">> "1, 2, 3"/", ";
> "1, 2, 3"/", ";
Result: ({ "1", "2", "3" })
> ({ "1", "2", "3" })*", ";
Result: "1, 2, 3"</syntaxhighlight>
</syntaxhighlight>
 
So achieving the result of this task with the method more suited to
Pike would be:
<syntaxhighlight lang="text">array a = (array(string))enumerate(10, 1, 1);
array a = (array(string))enumerate(10, 1, 1);
write(a * ", " + "\n");</syntaxhighlight>
write(a * ", " + "\n");
</syntaxhighlight>
 
=={{header|PL/I}}==
Line 2,220 ⟶ 2,930:
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To run:
To run:
Start up.
Write the numbers up to 10 on the console.
Line 2,231 ⟶ 2,942:
Write the string on the console without advancing.
If the counter is less than the number, write ", " on the console without advancing.
Repeat.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Pop11}}==
<syntaxhighlight lang="pop11">lvars i;
lvars i;
for i from 1 to 10 do
printf(i, '%p');
Line 2,240 ⟶ 2,953:
printf(', ', '%p');
endfor;
printf('\n', '%p');</syntaxhighlight>
</syntaxhighlight>
 
=={{header|PowerShell}}==
{{trans|C}}
<syntaxhighlight lang="powershell">for ($i = 1; $i -le 10; $i++) {
for ($i = 1; $i -le 10; $i++) {
Write-Host -NoNewLine $i
if ($i -eq 10) {
Line 2,251 ⟶ 2,966:
}
Write-Host -NoNewLine ", "
}
}</syntaxhighlight>
</syntaxhighlight>
An interesting alternative solution, although not ''strictly'' a loop, even though <code>switch</code> certainly loops over the given range.
<syntaxhighlight lang="powershell">switch (1..10) {
switch (1..10) {
{ $true } { Write-Host -NoNewLine $_ }
{ $_ -lt 10 } { Write-Host -NoNewLine ", " }
{ $_ -eq 10 } { Write-Host }
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">example :-
example :-
between(1,10,Val), write(Val), Val<10, write(', '), fail.
example.</syntaxhighlight>
</syntaxhighlight>
<pre>?- example.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
true.</pre>
 
=={{header|PureBasic}}==
 
<syntaxhighlight lang="purebasic">x=1
Repeat
Print(Str(x))
x+1
If x>10: Break: EndIf
Print(", ")
ForEver</syntaxhighlight>
 
=={{header|Python}}==
The particular pattern and example chosen in the task description is recognised by the Python language and there are more idiomatic ways to achieve the result that don't even require an explicit conditional test such as:
<syntaxhighlight lang="python">print ( ', '.join(str(i+1) for i in range(10)) )</syntaxhighlight>
print ( ', '.join(str(i+1) for i in range(10)) )
</syntaxhighlight>
But the [http://academicearth.org/lectures/the-loop-and-half-problem named pattern] is shown by code such as the following:
<syntaxhighlight lang="python">>>> from sys import stdout
>>> from sys import stdout
>>> write = stdout.write
>>> n, i = 10, 1
Line 2,293 ⟶ 3,006:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
>>>
>>></syntaxhighlight>
</syntaxhighlight>
 
List comprehension one-liner
<syntaxhighlight lang="python">
[print(str(i+1) + ", ",end='') if i < 9 else print(i+1) for i in range(10)]
</syntaxhighlight>
 
{{works with|Python|3.x}}
<syntaxhighlight lang="python">
n, i = 10, 1
while True:
print(i, end="")
i += 1
if i > n:
break
print(", ", end="")
</syntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> 10 times
10 times
[ i^ 1+ echo
i 0 = iff
conclude done
say ", " ]</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 2,314 ⟶ 3,041:
=={{header|R}}==
The natural way to solve this task in R is:
<syntaxhighlight lang="r">paste(1:10, collapse=", ")</syntaxhighlight>
paste(1:10, collapse=", ")
</syntaxhighlight>
The task specifies that we should use a loop however, so this more verbose code is needed.
<syntaxhighlight lang="r">for(i in 1:10)
for(i in 1:10)
{
cat(i)
Line 2,325 ⟶ 3,055:
}
cat(", ")
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
#lang racket
(for ((i (in-range 1 15)))
(display i)
#:break (= 10 i)
(display ", "))</syntaxhighlight>
</syntaxhighlight>
 
Gives the desired output.
Line 2,344 ⟶ 3,077:
}
 
print "\n";</syntaxhighlight>
</syntaxhighlight>
 
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">REBOL [
REBOL [
Title: "Loop Plus Half"
URL: http://rosettacode.org/wiki/Loop/n_plus_one_half
Line 2,357 ⟶ 3,092:
prin ", "
]
print ""</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red[]
Red[]
 
repeat i 10 [
Line 2,367 ⟶ 3,104:
prin ", "
]
print ""</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Relation}}==
Line 2,385 ⟶ 3,123:
=={{header|REXX}}==
===two CHAROUTs===
<syntaxhighlight lang="rexx">/*REXX program displays: 1,2,3,4,5,6,7,8,9,10 */
/*REXX program displays: 1,2,3,4,5,6,7,8,9,10 */
 
do j=1 to 10
Line 2,391 ⟶ 3,130:
if j<10 then call charout ,"," /*append a comma for one-digit numbers.*/
end /*j*/
/*stick a fork in it, we're all done. */</syntaxhighlight>
</syntaxhighlight>
'''output'''
<pre>
Line 2,398 ⟶ 3,138:
 
===one CHAROUT===
<syntaxhighlight lang="rexx">/*REXX program displays: 1,2,3,4,5,6,7,8,9,10 */
/*REXX program displays: 1,2,3,4,5,6,7,8,9,10 */
 
do j=1 for 10 /*using FOR is faster than TO. */
call charout ,j || left(',',j<10) /*display J, maybe append a comma (,).*/
end /*j*/
/*stick a fork in it, we're all done. */</syntaxhighlight>
</syntaxhighlight>
'''output'''
<pre>
Line 2,410 ⟶ 3,152:
 
===version 3 if the number of items is not known===
<syntaxhighlight lang="rexx">list='aa bb cc dd'
list='aa bb cc dd'
sep=', '
Do i=1 By 1 While list<>''
Line 2,416 ⟶ 3,159:
Parse Var list item list
Call charout ,item
End</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>aa, bb, cc, dd</pre>
Line 2,424 ⟶ 3,168:
for x = 1 to 10 see x if x=10 exit ok see ", " next see nl
</syntaxhighlight>
 
=={{header|RPL}}==
There are basically 2 possibilities, based on an overarching <code>FOR..NEXT</code> or <code>DO..UNTIL</code> / <code>WHILE..REPEAT</code> loop structure, in which an <code>IF..THEN..ELSE</code> decides which parts of the loop must be executed. RPL does not have any instruction to just exit a loop.
The <code>DO..UNTIL</code> / <code>WHILE..REPEAT</code> approach requires to use a flag and to manage a counter, which weighs down the code, as it can be seen in the implementations below.
≪ ""
1 10 '''FOR''' j
j →STR +
'''IF''' j 10 < '''THEN''' ", " + '''END'''
'''NEXT'''
≫ 'S1→10' STO
17 words
 
≪ 1 CF "" 0
'''DO'''
1 + SWAP OVER →STR +
'''IF''' OVER 10 < '''THEN''' ", " + '''ELSE''' 1 SF '''END'''
SWAP
'''UNTIL''' 1 FS? '''END'''
DROP
≫ 'S1→10' STO
28 words + 1 flag
 
=={{header|Ruby}}==
Line 2,432 ⟶ 3,197:
print ", "
end
puts
puts</syntaxhighlight>
</syntaxhighlight>
More idiomatic Ruby to obtain the same result is:
<syntaxhighlight lang="ruby">
puts (1..10).join(", ")</syntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">
FOR i = 1 TO 10
PRINT cma$;i;
cma$ = " , "
NEXT i
</syntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn main() {
fn main() {
for i in 1..=10 {
print!("{}{}", i, if i < 10 { ", " } else { "\n" });
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
More like the problem description:
<syntaxhighlight lang="rust">fn main() {
fn main() {
for i in 1..=10 {
print!("{}", i);
Line 2,462 ⟶ 3,224:
}
println!();
}
}</syntaxhighlight>
</syntaxhighlight>
 
Alternative solution using <code>join</code>.
<syntaxhighlight lang="rust">fn main() {
fn main() {
println!(
"{}",
Line 2,473 ⟶ 3,237:
.join(", ")
);
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|S-lang}}==
Line 2,480 ⟶ 3,245:
part from the bottom to the top of the loop, then NOT include it on the
FIRST pass:
<syntaxhighlight lang="s-lang">variable more = 0, i;
variable more = 0, i;
foreach i ([1:10]) {
if (more) () = printf(", ");
printf("%d", i);
more = 1;
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Salmon}}==
<syntaxhighlight lang="salmon">iterate (x; [1...10])
iterate (x; [1...10])
{
print(x);
Line 2,495 ⟶ 3,263:
print(", ");
};
print("\n");</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<syntaxhighlight lang="scala">var i = 1
var i = 1
while ({
print(i)
Line 2,507 ⟶ 3,277:
i += 1
}
println()</syntaxhighlight>
<syntaxhighlight lang="scala">println((1 to 10).mkString(", "))</syntaxhighlight>
<syntaxhighlight lang="scala">
println((1 to 10).mkString(", "))
</syntaxhighlight>
 
=={{header|Scheme}}==
It is possible to use continuations:
<syntaxhighlight lang="scheme">(call-with-current-continuation
(call-with-current-continuation
(lambda (esc)
(do ((i 1 (+ 1 i))) (#f)
(display i)
(if (= i 10) (esc (newline)))
(display ", "))))</syntaxhighlight>
</syntaxhighlight>
 
But usually making the tail recursion explicit is enough:
<syntaxhighlight lang="scheme">(let loop ((i 0))
(let loop ((i 0))
(display i)
(if (= i 10)
Line 2,526 ⟶ 3,302:
(begin
(display ", ")
(loop (+ 1 i)))))</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">for i=1:10
for i=1:10
printf("%2d ",i)
if i<10 then printf(", "); end
end
printf("\n")</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre> 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 </pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,552 ⟶ 3,332:
end for;
writeln;
end func;</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">for (1..10) { |i|
for (1..10) { |i|
print i;
i == 10 && break;
Line 2,561 ⟶ 3,343:
}
 
print "\n";</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Smalltalk}}==
<syntaxhighlight lang="smalltalk">1 to: 10 do: [:n |
1 to: 10 do: [:n |
Transcript show: n asString.
n < 10 ifTrue: [ Transcript show: ', ' ]
]
]</syntaxhighlight>
</syntaxhighlight>
Smalltalk/X and Pharo/Squeak have special enumeration messages for this in their Collection class, which executes another action in-between iterated elements:
{{works with|Smalltalk/X}}{{works with|Pharo}}
<syntaxhighlight lang="smalltalk">(1 to: 10) do: [:n |
(1 to: 10) do: [:n |
Transcript show: n asString.
] separatedBy:[
Transcript show: ', '
]
]</syntaxhighlight>
</syntaxhighlight>
That is a bit different from the task's specification, but does what is needed here, and is more general in working with any collection (in the above case: a range aka "Interval"). It does not depend on the collection being addressed by an integer index and/or keeping a count inside the loop.
 
Line 2,582 ⟶ 3,369:
line output, and to use the same statement for loop control and the comma.
 
<syntaxhighlight lang="snobol4">loop str = str lt(i,10) (i = i + 1) :f(out)
loop str = str lt(i,10) (i = i + 1) :f(out)
str = str ne(i,10) ',' :s(loop)
out output = str
end
end</syntaxhighlight>
</syntaxhighlight>
 
{{works with|Macro Spitbol}}
Line 2,593 ⟶ 3,382:
 
This example also breaks the loop explicitly:
<syntaxhighlight lang="snobol4"> output('out',1,'-[-r1]')
output('out',1,'-[-r1]')
loop i = lt(i,10) i + 1 :f(end)
out = i
eq(i,10) :s(end)
out = ',' :(loop)
end
end</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 2,604 ⟶ 3,395:
 
=={{header|SNUSP}}==
<syntaxhighlight lang="snusp">@\>@\>@\>+++++++++<!/+. >-?\# digit and loop test
@\>@\>@\>+++++++++<!/+. >-?\# digit and loop test
| | \@@@+@+++++# \>>.<.<</ comma and space
| \@@+@@+++++#
\@@@@=++++#</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Spin}}==
Line 2,614 ⟶ 3,407:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<syntaxhighlight lang="spin">con
con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 2,632 ⟶ 3,426:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 2,639 ⟶ 3,434:
 
=={{header|Stata}}==
<syntaxhighlight lang="stata">forv i=1/10 {
forv i=1/10 {
di `i' _continue
if `i'<10 {
Line 2,647 ⟶ 3,443:
di
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
=== Mata ===
<syntaxhighlight lang="stata">mata
mata
for (i=1; i<=10; i++) {
printf("%f",i)
Line 2,659 ⟶ 3,457:
}
}
end
end</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Swift}}==
{{works with|Swift|1.x}}
<syntaxhighlight lang="swift">for var i = 1; ; i++ {
for var i = 1; ; i++ {
print(i)
if i == 10 {
Line 2,670 ⟶ 3,470:
}
print(", ")
}
}</syntaxhighlight>
</syntaxhighlight>
{{works with|Swift|2}} The usual way to do this with Swift 2 is:
<syntaxhighlight lang="swift">
Line 2,679 ⟶ 3,480:
</syntaxhighlight>
To satisfy the specification, we have to do something similar to Swift 1.x and other C-like languages:
<syntaxhighlight lang="swift">for var i = 1; ; i++ {
for var i = 1; ; i++ {
print(i, terminator: "")
if i == 10 {
Line 2,686 ⟶ 3,488:
}
print(", ", terminator: "")
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">for {set i 1; set end 10} true {incr i} {
for {set i 1; set end 10} true {incr i} {
puts -nonewline $i
if {$i >= $end} break
puts -nonewline ", "
}
puts ""</syntaxhighlight>
</syntaxhighlight>
However, that's not how the specific task (printing 1..10 with comma separators) would normally be done. (Note, the solution below is ''not'' a solution to the half-looping problem.)
<syntaxhighlight lang="tcl">proc range {from to} {
proc range {from to} {
set result {}
for {set i $from} {$i <= $to} {incr i} {
Line 2,704 ⟶ 3,510:
}
puts [join [range 1 10] ", "] ;# ==> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10</syntaxhighlight>
</syntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
There is no horizontal cursor position on the program IO screen, so we concatenate strings instead.
 
<syntaxhighlight lang="ti89b">Local str
"" → str
For i,1,10
str & string(i) → str
If i < 10 Then
str & "," → str
EndIf
EndFor
Disp str</syntaxhighlight>
 
=={{header|Tiny BASIC}}==
Tiny BASIC does not support string concatenation so each number is on a separate line but this is at least in the spirit of the task description.
<syntaxhighlight lang="tinybasic"> LET I = 1
10 IF I = 10 THEN PRINT I
IF I < 10 THEN PRINT I,", "
IF I = 10 THEN END
LET I = I + 1
GOTO 10</syntaxhighlight>
 
=={{header|TUSCRIPT}}==
Line 2,745 ⟶ 3,529:
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="bash">for(( Z=1; Z<=10; Z++ )); do
for(( Z=1; Z<=10; Z++ )); do
echo -e "$Z\c"
if (( Z != 10 )); then
echo -e ", \c"
fi
done
done</syntaxhighlight>
</syntaxhighlight>
 
{{works with|Bash}}
<syntaxhighlight lang="bash">for ((i=1;i<=$((last=10));i++)); do
for ((i=1;i<=$((last=10));i++)); do
echo -n $i
[ $i -eq $last ] && break
echo -n ", "
done
done</syntaxhighlight>
</syntaxhighlight>
 
=={{header|UnixPipes}}==
The last iteration is handled automatically for us
when there is no element in one of the pipes.
<syntaxhighlight lang="bash">yes \ | cat -n | head -n 10 | paste -d\ - <(yes , | head -n 9) | xargs echo</syntaxhighlight>
yes \ | cat -n | head -n 10 | paste -d\ - <(yes , | head -n 9) | xargs echo
</syntaxhighlight>
 
=={{header|Ursa}}==
{{trans|PHP}}
<syntaxhighlight lang="ursa">decl int i
decl int i
for (set i 1) (< i 11) (inc i)
out i console
Line 2,774 ⟶ 3,565:
out ", " console
end for
out endl console</syntaxhighlight>
</syntaxhighlight>
 
=={{header|V}}==
<syntaxhighlight lang="v">[loop
[loop
[ [10 =] [puts]
[true] [dup put ',' put succ loop]
] when].</syntaxhighlight>
</syntaxhighlight>
Using it
|1 loop
Line 2,787 ⟶ 3,581:
=={{header|Vala}}==
{{trans|C}}
<syntaxhighlight lang="vala">void main() {
void main() {
for (int i = 1; i <= 10; i++)
{
Line 2,793 ⟶ 3,588:
stdout.printf(i == 10 ? "\n" : ", ");
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Public Sub WriteACommaSeparatedList()
Dim i As Integer
Dim a(1 To 10) As String
For i = 1 To 10
a(i) = CStr(i)
Next i
Debug.Print Join(a, ", ")
End Sub</syntaxhighlight>
 
=={{header|Vedit macro language}}==
This example writes the output into current edit buffer.
<syntaxhighlight lang="vedit">for (#1 = 1; 1; #1++) {
for (#1 = 1; 1; #1++) {
Num_Ins(#1, LEFT+NOCR)
if (#1 == 10) { Break }
Ins_Text(", ")
}
Ins_Newline</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Verilog}}==
<syntaxhighlight lang="Verilog">
module main;
integer i;
initial begin
for(i = 1; i <= 10; i = i + 1) begin
$write(i);
if (i < 10 ) $write(", ");
end
$display("");
$finish ;
end
endmodule
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
 
=={{header|Vim Script}}==
<syntaxhighlight lang="vim">for i in range(1, 10)
for i in range(1, 10)
echon i
if (i != 10)
echon ", "
endif
endfor</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang="vbnet">For i = 1 To 10
Console.Write(i)
If i = 10 Then Exit For
Console.Write(", ")
Next</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="go">fn main() {
fn main() {
for i := 1; ; i++ {
print(i)
Line 2,838 ⟶ 3,641:
print(", ")
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Wart}}==
<syntaxhighlight lang="wart">for i 1 (i <= 10) ++i
for i 1 (i <= 10) ++i
pr i
if (i < 10)
pr ", "
(prn)</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Wee Basic}}==
print 1 "" ensures the end of program text is separate from the list of numbers.
<syntaxhighlight lang="wee basic">print 1 ""
for numbers=1 to 10
print 1 at numbers*3-2,0 numbers
if numbers<>10
print 1 at numbers*3-1,0 ","
endif
end</syntaxhighlight>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">for (i in 1..10) {
for (i in 1..10) {
System.write(i)
System.write((i < 10) ? ", " : "\n")
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
PROGRAM "n_plus_one_half"
 
DECLARE FUNCTION Entry()
 
FUNCTION Entry()
FOR i% = 1 TO 10
PRINT i%;
IF i% = 10 THEN EXIT FOR
PRINT ",";
NEXT i%
PRINT
' If equality checking affected the efficiency
' or if the last value were treated in different manner,
' one would extract the last iteration:
FOR i% = 1 TO 9
PRINT i%; ",";
NEXT i%
PRINT 10
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">codes CrLf=9, IntOut=11, Text=12;
codes CrLf=9, IntOut=11, Text=12;
int N;
[for N:= 1 to 10 do \best way to do this
Line 2,912 ⟶ 3,680:
];
CrLf(0);
]
]</syntaxhighlight>
</syntaxhighlight>
 
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">for i = 1 to 10
print i;
if i < 10 then print ", "; : endif
next i
print
 
end</syntaxhighlight>
 
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">foreach n in ([1..10]){ print(n); if (n!=10) print(",") }</syntaxhighlight>
foreach n in ([1..10]){ print(n); if (n!=10) print(",") }
</syntaxhighlight>
Or, using a state machine:
<syntaxhighlight lang="zkl">[1..10].pump(Console.print, Void.Drop, T(Void.Write,",",Void.Drop));</syntaxhighlight>
[1..10].pump(Console.print, Void.Drop, T(Void.Write,",",Void.Drop));
</syntaxhighlight>
where pump is (sink, action, action ...). The first Drop writes the
first object from the source (1) to the sink and drops out (and that
Line 2,934 ⟶ 3,696:
collects things to write to the sink: a comma and the number, eg ",2".
Or:
<syntaxhighlight lang="zkl">[1..10].pump(Console.print, Void.Drop, fcn(n){ String(",",n) });</syntaxhighlight>
[1..10].pump(Console.print, Void.Drop, fcn(n){ String(",",n) });
</syntaxhighlight>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
const std = @import("std");
pub fn main() !void {
const stdout_wr = std.io.getStdOut().writer();
Line 2,949 ⟶ 3,714:
}
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{omit from|PL/0}}
3,038

edits