Loops/N plus one half: Difference between revisions

m
prod
m (prod)
 
(33 intermediate revisions by 19 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' ‘’)</lang>
</syntaxhighlight>
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">
* Loops/N plus one half 13/08/2015
LOOPHALF CSECT USING LOOPHALF,R12
Line 65 ⟶ 63:
YREGS
END LOOPHALF
</syntaxhighlight>
</lang>
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{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
move.w #1,d1 ;ABCD can't use an immediate operand
move.w #10-1,d2
loop:
abcd d1,d0
move.l d0,-(sp)
jsr printhex
move.l (sp)+,d0
cmp.b #$10,d0 ;we use hex 10 since this is binary-coded decimal
beq exitEarly
move.l d0,-(sp)
move.b #',',D0 ;PrintChar uses D0 as input
jsr PrintChar
move.b #' ',D0
jsr PrintChar
move.l (sp)+,d0
DBRA d2,loop
exitEarly:
; include "X:\SrcGEN\testModule.asm"
jmp *
</syntaxhighlight>
{{out}}
<pre>01, 02, 03, 04, 05, 06, 07, 08, 09, 10</pre>
 
=={{header|8086 Assembly}}==
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 127 ⟶ 157:
pop ax
ret
end start ;EOF</lang>
</syntaxhighlight>
 
{{out}}
Line 136 ⟶ 167:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopnplusone64.s */
Line 202 ⟶ 233:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 210 ⟶ 241:
ACL2 does not have loops, but this is close:
 
<syntaxhighlight lang="lisp">
<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))))))</lang>
</syntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
BYTE i=[1]
Line 229 ⟶ 262:
i==+1
OD
RETURN</lang>
</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/N_plus_one_half.png Screenshot from Atari 8-bit computer]
Line 237 ⟶ 271:
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
<lang ada>with Ada.Text_IO;
with Ada.Text_IO;
 
procedure LoopsAndHalf is
Line 247 ⟶ 282:
end loop;
Ada.Text_IO.new_line;
end LoopsAndHalf;</lang>
</syntaxhighlight>
 
=={{header|Aime}}==
<syntaxhighlight lang ="aime">integer i;
integer i;
 
i = 0;
Line 261 ⟶ 298:
o_text(", ");
}
o_text("\n");</lang>
</syntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|ALGOL 60|OS/360}}
<langsyntaxhighlight lang="algol60">'BEGIN'
'BEGIN'
'COMMENT' Loops N plus one half - Algol60 - 20/06/2018;
'INTEGER' I;
Line 272 ⟶ 311:
'IF' I 'NOTEQUAL' 10 'THEN' OUTSTRING(1,'(', ')')
'END'
'END'</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 284 ⟶ 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 291 ⟶ 332:
OD;
 
print(new line)</lang>
</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 299 ⟶ 342:
OD;
 
print(new line)</lang>
</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 307 ⟶ 352:
OD;
done:
print(new line)</lang>
</syntaxhighlight>
|}
Output for all cases above:
Line 315 ⟶ 361:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
begin
integer i;
i := 0;
Line 328 ⟶ 375:
writeon( "," )
end
end.</lang>
</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 338 ⟶ 442:
WriteF(', ')
ENDFOR
ENDPROC</lang>
</syntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program loopnplusone.s */
Line 471 ⟶ 576:
bx lr @ return
 
</syntaxhighlight>
</lang>
 
=={{header|ArnoldC}}==
<syntaxhighlight lang ="arnoldc">IT'S SHOWTIME
IT'S SHOWTIME
HEY CHRISTMAS TREE n
YOU SET US UP @NO PROBLEMO
Line 493 ⟶ 599:
ENOUGH TALK
CHILL
YOU HAVE BEEN TERMINATED</lang>
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">
<lang rebol>print join.with:", " map 1..10 => [to :string]</lang>
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">
<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 510 ⟶ 630:
output .= ", " ; append ", " to the output var
}
MsgBox, %output%</lang>
</syntaxhighlight>
 
=={{header|AutoIt}}==
<syntaxhighlight lang="autoit">
<lang autoit>#cs ----------------------------------------------------------------------------
#cs ----------------------------------------------------------------------------
 
AutoIt Version: 3.3.8.1
Line 538 ⟶ 660:
EndFunc
 
main()</lang>
</syntaxhighlight>
 
=={{header|AWK}}==
'''One-liner:'''
<syntaxhighlight lang="awk">
<lang awk>$ awk 'BEGIN{for(i=1;i<=10;i++){printf i;if(i<10)printf ", "};print}'</lang>
$ awk 'BEGIN{for(i=1;i<=10;i++){printf i;if(i<10)printf ", "};print}'
</syntaxhighlight>
{{out}}
<pre>
Line 548 ⟶ 673:
 
'''Readable version:'''
<langsyntaxhighlight lang="awk">
BEGIN {
n=10
Line 556 ⟶ 681:
}
print
}
}</lang>
</syntaxhighlight>
Same output.
 
=={{header|Axe}}==
<syntaxhighlight lang="axe">
<lang axe>For(I,1,10)
For(I,1,10)
Disp I▶Dec
If I=10
Line 567 ⟶ 694:
Disp ","
End
End</lang>
</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</lang>
</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">
 
<lang ApplesoftBasic>10 FOR I = 1 TO 10
20 PRINT I;
30 IF I < 10 THEN PRINT ", "; : NEXT I</lang>
</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.
<lang IS-BASIC>100 FOR I=1 TO 10
110 PRINT I;
120 IF I=10 THEN EXIT FOR
130 PRINT ",";
140 NEXT</lang>
 
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
<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</lang>
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>
 
<lang zxbasic>10 FOR i=1 TO 10
20 PRINT i;
30 IF i=10 THEN GOTO 50
40 PRINT ", ";
50 NEXT i</lang>
 
==={{header|Basic09}}===
<langsyntaxhighlight lang="basic09">
PROCEDURE nAndAHalf
DIM i:INTEGER
Line 626 ⟶ 749:
NEXT i
PRINT
</syntaxhighlight>
</lang>
 
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">
<lang BASIC256>for i = 1 to 10
for i = 1 to 10
print i;
if i < 10 then print ", ";
next i
 
end</lang>
</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</lang>
</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">
<lang qbasic>LET cm$ = ""
LET cm$ = ""
FOR i = 1 to 10
PRINT cm$; str$(i);
LET cm$ = ", "
NEXT i
END</lang>
</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 664 ⟶ 1,123:
}
print ", "
}
}</lang>
</syntaxhighlight>
 
=={{header|Befunge}}==
<syntaxhighlight lang ="befunge>1+>::.9`#@_" ,",,</lang>
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"<</lang>
@,,,,", 10"<
</syntaxhighlight>
 
=={{header|Bracmat}}==
<syntaxhighlight lang ="bracmat"> 1:?i
1:?i
& whl
' ( put$!i
& !i+1:~>10:?i
& put$", "
)</lang>
</syntaxhighlight>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">
<lang c>#include <stdio.h>
#include <stdio.h>
 
int main()
Line 692 ⟶ 1,159:
}
return 0;
}
}</lang>
</syntaxhighlight>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang ="csharp">using System;
using System;
 
class Program
Line 709 ⟶ 1,178:
Console.WriteLine();
}
}
}</lang>
</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
<lang cpp>#include <iostream>
#include <iostream>
int main()
Line 724 ⟶ 1,195:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{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")</lang>
write(i, if i % 10 > 0 then ", " else "\n")
</syntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">
; Functional version
(apply str (interpose ", " (range 1 11)))
Line 742 ⟶ 1,215:
(print ", ")
(recur (inc n)))))
</syntaxhighlight>
</lang>
 
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<syntaxhighlight lang="cobol">
<lang cobol> IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. Loop-N-And-Half.
 
Line 772 ⟶ 1,246:
 
GOBACK
.</lang>
</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 791 ⟶ 1,267:
END-PERFORM.
STOP RUN.
END-PROGRAM.</lang>
</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 810 ⟶ 1,288:
02-DONE.
STOP RUN.
END-PROGRAM.</lang>
</syntaxhighlight>
Using 'PERFORM VARYING'
<syntaxhighlight lang ="cobol">IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. LOOP-1p5-NOADV-VARY.
DATA DIVISION.
Line 828 ⟶ 1,308:
END-PERFORM.
STOP RUN.
END-PROGRAM.</lang>
</syntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
# Loop plus half. This code shows how to break out of a loop early
# on the last iteration. For the contrived example, there are better
Line 845 ⟶ 1,326:
s += ', '
console.log s
</syntaxhighlight>
</lang>
 
=={{header|ColdFusion}}==
With tags:
<syntaxhighlight lang="cfm">
<lang cfm><cfloop index = "i" from = "1" to = "10">
<cfloop index = "i" from = "1" to = "10">
#i#
<cfif i EQ 10>
Line 855 ⟶ 1,337:
</cfif>
,
</cfloop></lang>
</syntaxhighlight>
 
With script:
<syntaxhighlight lang="cfm">
<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 869 ⟶ 1,353:
writeOutput( ", " );
}
</cfscript></lang>
</syntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(loop for i from 1 below 10 do
(princ i) (princ ", ")
finally (princ i))
</syntaxhighlight>
</lang>
 
or
 
<syntaxhighlight lang="lisp">
<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 ", "))</lang>
</syntaxhighlight>
 
but for such simple tasks we can use format's powers:
 
<langsyntaxhighlight lang="lisp">
(format t "~{~a~^, ~}" (loop for i from 1 to 10 collect i))
</syntaxhighlight>
</lang>
 
=== Using DO ===
<langsyntaxhighlight lang="lisp">
(do ((i 1 (incf i))) ; Initialize to 1 and increment on every loop
((> i 10)) ; Break condition
Line 899 ⟶ 1,386:
(princ ", ") ; Printing the comma is jumped by the go statement
end) ; The tag
</syntaxhighlight>
</lang>
or
<langsyntaxhighlight lang="lisp">
(do ; Not exactly separate statements for the number and the comma
((i 1 (incf i))) ; Initialize to 1 and increment on every loop
Line 907 ⟶ 1,394:
(princ i) ; Print number statement
(princ ", ")) ; Print comma statement
</syntaxhighlight>
</lang>
 
{{out}}
Line 915 ⟶ 1,402:
 
=={{header|Cowgol}}==
<syntaxhighlight lang cowgol>include ="cowgol.coh";>
include "cowgol.coh";
 
var i: uint8 := 1;
Line 924 ⟶ 1,412:
i := i + 1;
end loop;
print_nl();</lang>
</syntaxhighlight>
 
{{out}}
Line 932 ⟶ 1,421:
=={{header|D}}==
===Iterative===
<syntaxhighlight lang="d">
<lang d>import std.stdio;
import std.stdio;
void main() {
Line 943 ⟶ 1,433:
 
writeln();
}
}</lang>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
===Functional Style===
<syntaxhighlight lang="d">
<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 953 ⟶ 1,445:
// A simpler solution:
writefln("%(%d, %)", iota(1, 11));
}
}</lang>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Line 959 ⟶ 1,452:
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">
<lang Dart>String loopPlusHalf(start, end) {
String loopPlusHalf(start, end) {
var result = '';
for(int i = start; i <= end; i++) {
Line 973 ⟶ 1,467:
void main() {
print(loopPlusHalf(1, 10));
}
}</lang>
</syntaxhighlight>
 
=={{header|Delphi}}==
 
<syntaxhighlight lang="delphi">
<lang Delphi>program LoopsNPlusOneHalf;
program LoopsNPlusOneHalf;
 
{$APPTYPE CONSOLE}
Line 993 ⟶ 1,489:
end;
Writeln;
end.</lang>
</syntaxhighlight>
 
=={{header|Draco}}==
<syntaxhighlight lang ="draco">proc nonrec main() void:
proc nonrec main() void:
byte i;
i := 1;
Line 1,006 ⟶ 1,504:
write(", ")
od
corp</lang>
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
=={{header|DWScript}}==
 
<syntaxhighlight lang="delphi">
<lang Delphi>var i : Integer;
var i : Integer;
 
for i := 1 to 10 do begin
Line 1,017 ⟶ 1,517:
if i < 10 then
Print(', ');
end;</lang>
</syntaxhighlight>
 
=={{header|E}}==
 
A typical loop+break solution:
<syntaxhighlight lang="e">
<lang e>var i := 1
var i := 1
while (true) {
print(i)
Line 1,028 ⟶ 1,530:
print(", ")
i += 1
}
}</lang>
</syntaxhighlight>
 
Using the loop primitive in a semi-functional style:
 
<syntaxhighlight lang="e">
<lang e>var i := 1
var i := 1
__loop(fn {
print(i)
Line 1,042 ⟶ 1,546:
true
}
})</lang>
</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
for i = 1 to 10
write i
if i < 10
write ", "
.
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="lisp">
(string-delimiter "")
 
Line 1,055 ⟶ 1,570:
(string-join (range 1 11) ",")
→ 1,2,3,4,5,6,7,8,9,10
</syntaxhighlight>
</lang>
 
=={{header|EDSAC order code}}==
<syntaxhighlight lang="edsac">
<lang edsac>[ N and a half times loop
[ N and a half times loop
=======================
 
Line 1,098 ⟶ 1,614:
[ 25 ] PF
 
EZPF</lang>
</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,112 ⟶ 1,696:
end
 
Enum.to_list(1..10) |> Loops.n_plus_one_half</lang>
</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">
<lang erlang>%% Implemented by Arjun Sunel
%% Implemented by Arjun Sunel
-module(loop).
-export([main/0]).
Line 1,129 ⟶ 1,730:
io:format("~p\n",[N])
end.
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
FOR I=1 TO 10 DO
PRINT(I;)
Line 1,138 ⟶ 1,739:
PRINT(", ";)
END FOR
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
<lang Euphoria>
for i = 1 to 10 do
printf(1, "%g", {i})
Line 1,148 ⟶ 1,749:
end if
end for
</syntaxhighlight>
</lang>
 
While, yes, use of <code>exit</code> would also work here, it is slightly faster to code it this way, if only the last iteration has something different.
Line 1,154 ⟶ 1,755:
=={{header|F Sharp|F#}}==
Functional version that works for lists of any length
<langsyntaxhighlight lang="fsharp">
let rec print (lst : int list) =
match lst with
Line 1,165 ⟶ 1,766:
 
print [1..10]
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<syntaxhighlight lang ="factor">: print-comma-list ( n -- )
: print-comma-list ( n -- )
[ [1,b] ] keep '[
[ number>string write ]
[ _ = [ ", " write ] unless ] bi
] each nl ;</lang>
</syntaxhighlight>
 
=={{header|Falcon}}==
<syntaxhighlight lang ="falcon">for value = 1 to 10
for value = 1 to 10
formiddle
>> value
Line 1,181 ⟶ 1,785:
end
forlast: > value
end</lang>
</syntaxhighlight>
 
{{out}}
Line 1,188 ⟶ 1,793:
 
=={{header|FALSE}}==
<syntaxhighlight lang ="false>1[$9>~][$.", "1+]#.</lang>
1[$9>~][$.", "1+]#.
</syntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 1,206 ⟶ 1,813:
}
}
</syntaxhighlight>
</lang>
 
=={{header|FBSL}}==
<lang qbasic>
#APPTYPE CONSOLE
For Dim i = 1 To 10
PRINT i;
IF i < 10 THEN PRINT ", ";
Next
PAUSE</lang>
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
. ;</lang>
</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 ;</lang>
</syntaxhighlight>
 
<syntaxhighlight lang ="forth">: comma-list ( n -- )
: comma-list ( n -- )
1
begin dup 1 .r
2dup <>
while ." , " 1+
repeat 2drop ;</lang>
</syntaxhighlight>
 
=={{header|Fortran}}==
{{works with|FORTRAN|IV and later}}
<syntaxhighlight lang="fortran">
<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</lang>
</syntaxhighlight>
 
{{works with|Fortran|77 and later}}
 
<syntaxhighlight lang="fortran">
<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,297 ⟶ 1,901:
C5001 FORMAT (T3, ADVANCE='NO')
C5001 FORMAT (A, ADVANCE='NO')
END</lang>
</syntaxhighlight>
 
{{Works with|Fortran|90 and later}}
 
<syntaxhighlight lang ="fortran">i = 1
i = 1
do
write(*, '(I0)', advance='no') i
Line 1,308 ⟶ 1,914:
i = i + 1
end do
write(*,*)</lang>
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<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</lang>
 
{{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.
 
<lang freebasic>dim as string cm = ""
for i as ubyte = 1 to 10
print cm;str(i);
cm = ", "
next i</lang>
 
=={{header|FutureBasic}}==
<lang futurebasic>window 1
 
long i, num = 10
 
for i = 1 to num
print i;
if i = num then break
print @", ";
next i
 
HandleEvents</lang>
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]'''
<lang gambas>Public Sub Main()
Dim siLoop As Short
 
For siLoop = 1 To 10
Print siLoop;
If siLoop <> 10 Then Print ", ";
Next
 
End</lang>
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,377 ⟶ 1,927:
Print("\n");
fi;
od;</lang>
</syntaxhighlight>
 
=={{header|GML}}==
<syntaxhighlight lang GML>str = "gml">
str = ""
for(i = 1; i <= 10; i += 1)
{
Line 1,387 ⟶ 1,939:
str += ", "
}
show_message(str)</lang>
</syntaxhighlight>
 
=={{header|Go}}==
<syntaxhighlight lang ="go">package main
package main
 
import "fmt"
Line 1,403 ⟶ 1,957:
fmt.Print(", ")
}
}
}</lang>
</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)
}
}</lang>
</syntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<syntaxhighlight lang ="groovy">for(i in (1..10)) {
for(i in (1..10)) {
print i
if (i == 10) break
print ', '
}
}</lang>
</syntaxhighlight>
 
Output:
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
=={{header|GW-BASIC}}==
<lang gwbasic>10 C$ = ""
20 FOR I = 1 TO 10
30 PRINT C$;STR$(I);
40 C$=", "
50 NEXT I</lang>
 
=={{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 ", "</lang>
</syntaxhighlight>
 
You can also use intersperse :: a -> [a] -> [a]
<syntaxhighlight lang="haskell">
<lang haskell>intercalate ", " (map show [1..10])</lang>
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' : ', '}');</lang>
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 ""</lang>
</syntaxhighlight>
 
=={{header|HicEst}}==
<syntaxhighlight lang ="hicest">DO i = 1, 10
DO i = 1, 10
WRITE(APPend) i
IF(i < 10) WRITE(APPend) ", "
ENDDO</lang>
</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,464 ⟶ 2,027:
Print(", ");
}
Print("\n");</lang>
</syntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<syntaxhighlight lang="icon">
<lang Icon>procedure main()
procedure main()
every writes(i := 1 to 10) do
if i = 10 then break write()
else writes(", ")
end</lang>
</syntaxhighlight>
 
The above can be written more succinctly as:
<syntaxhighlight lang="icon">
<lang Icon>every writes(c := "",1 to 10) do c := ","
every writes(c := "",1 to 10) do c := ","
</lang>
</syntaxhighlight>
 
=={{header|IDL}}==
Line 1,481 ⟶ 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">
<lang idl>print,indgen(10)+1,format='(10(i,:,","))'</lang>
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">
<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</lang>
</syntaxhighlight>
 
(which merely suppresses the printing of the comma in the last iteration);
Line 1,494 ⟶ 2,065:
or like this:
 
<syntaxhighlight lang="idl">
<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</lang>
</syntaxhighlight>
 
(which terminates the loop early if the last element is reached).
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang j>output=: verb define
output=: verb define
buffer=: buffer,y
)
Line 1,515 ⟶ 2,089:
end.
end.
smoutputecho buffer
)
)</lang>
</syntaxhighlight>
 
Example use:
<syntaxhighlight lang="j">
<lang j> loopy 0
loopy 0
1, 2, 3, 4, 5, 6, 7, 8, 9, 10</lang>
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">
<lang j> ;}.,(', ' ; ":)&> 1+i.10
1, 2, 3, 4;}., 5(', 6,' 7,; 8, 9,":)&> 1+i.10</lang>
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">
<lang j> commaAnd=: ":&.> ;@,. -@# {. (<;._1 '/ and /') ,~ (<', ') #~ #
commaAnd=: ":&.> ;@,. -@# {. (<;._1 '/ and /') ,~ (<', ') #~ #
commaAnd i.5
0, 1, 2, 3 and 4</lang>
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
<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,542 ⟶ 2,124:
}
System.out.println();
}
}</lang>
</syntaxhighlight>
 
=={{header|JavaScript}}==
<syntaxhighlight lang ="javascript">function loop_plus_half(start, end) {
function loop_plus_half(start, end) {
var str = '',
i;
Line 1,558 ⟶ 2,142:
}
alert(loop_plus_half(1, 10));</lang>
</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,564 ⟶ 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">
<lang JavaScript>function range(m, n) {
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(
function (x, i) {
Line 1,574 ⟶ 2,160:
console.log(
range(1, 10).join(', ')
);</lang>
</syntaxhighlight>
 
Output:
<syntaxhighlight lang="javascript">
<lang JavaScript>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</lang>
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">
<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,601 ⟶ 2,191:
)
})(1, 10)
);</lang>
</syntaxhighlight>
 
Output:
<syntaxhighlight lang="javascript">
<lang JavaScript>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</lang>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
==== Otherwise ====
<syntaxhighlight lang="javascript">
<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 )
}
}</lang>
</syntaxhighlight>
or
<syntaxhighlight lang="javascript">
<lang JavaScript>var s=1, e=10
var s=1, e=10
for (;; s+=1) {
document.write( s )
if (s==e) break
document.write( ', ' )
}
}</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 1,624 ⟶ 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">
<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,632 ⟶ 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("");</lang>
</syntaxhighlight>
{{Out}}
loop_plus_half2(1;11)
Line 1,639 ⟶ 2,238:
=={{header|Julia}}==
The short-circuiting && is idiomatic to Julia - the second expression will only be evaluated if the first one is true.
<syntaxhighlight lang="julia">
<lang Julia>
for i = 1:10
print(i)
Line 1,645 ⟶ 2,244:
print(", ")
end
</syntaxhighlight>
</lang>
Output:
<syntaxhighlight lang="julia">
<lang Julia>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
</lang>
 
=={{header|K}}==
<syntaxhighlight lang="k">
<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</lang>
</syntaxhighlight>
 
Alternative solutions:
<syntaxhighlight lang="k">
<lang K> 10 {p@x;p@:[x<10;", ";"\n"];x+1}\1;
10 {p@x;p@:[x<10;", ";"\n"];x+1}'\1+!10; /variant</lang>
{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,668 ⟶ 2,272:
if (i < 10) print(", ")
}
}
}</lang>
</syntaxhighlight>
 
{{out}}
Line 1,679 ⟶ 2,284:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def loops_N_plus_one_half
{lambda {:i :n}
Line 1,691 ⟶ 2,296:
-> 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10. (end of loop with a dot)
</syntaxhighlight>
</lang>
 
=={{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">
<lang lang5>: , dup ", " 2 compress "" join ;
1: do, dup 10 != if dup ", . 1 +" else2 .compress break"" thenjoin loop</lang>;
1 do dup 10 != if dup , . 1 + else . break then loop
</syntaxhighlight>
Word: [[Loops/For#Lang5]]
<syntaxhighlight lang="lang5">
<lang lang5>: 2string 2 compress "" join ;
: 2string 2 compress "" join ;
: , dup 10 != if ", " 2string then ;
1 10 "dup , . 1+" times</lang>
</syntaxhighlight>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">
<lang Lasso>local(out) = ''
local(out) = ''
loop(10) => {
#out->append(loop_count)
Line 1,708 ⟶ 2,344:
#out->append(', ')
}
#out</lang>
</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,720 ⟶ 2,358:
]
]
print</lang>
</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,739 ⟶ 2,379:
 
make "aList [1 2 3 4 5 6 7 8 9 10]
print join :aList "|, |</lang>
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
Keyword 'exit' allows the termination.
<lang lb>
for i =1 to 10
print i;
if i =10 then exit for
print ", ";
next i
end
</lang>
 
=={{header|Lisaac}}==
<syntaxhighlight lang="lisaac">
<lang Lisaac>Section Header
Section Header
 
+ name := LOOP_AND_HALF;
Line 1,771 ⟶ 2,402:
};
'\n'.print;
);</lang>
</syntaxhighlight>
 
=={{header|LiveCode}}==
<syntaxhighlight lang="livecode">
<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</lang>
</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</lang>
</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</lang>
</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
\\ old type loop
Line 1,825 ⟶ 2,463:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<syntaxhighlight lang="m4">
<lang M4>define(`break',
define(`break',
`define(`ulim',llim)')
define(`for',
Line 1,837 ⟶ 2,476:
 
for(`x',`1',`',
`x`'ifelse(x,10,`break',`, ')')</lang>
</syntaxhighlight>
 
=={{header|Make}}==
<syntaxhighlight lang ="make">NEXT=`expr $* + 1`
NEXT=`expr $* + 1`
MAX=10
RES=1
Line 1,850 ⟶ 2,491:
 
%-n:
@-make -f loop.mk $(NEXT)-n MAX=$(MAX) RES=$(RES),$(NEXT)</lang>
</syntaxhighlight>
 
Invoking it
Line 1,857 ⟶ 2,499:
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
<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</lang>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
<lang Mathematica>i = 1; s = "";
i = 1; s = "";
While[True,
s = s <> ToString@i;
Line 1,868 ⟶ 2,513:
i++;
]
s
s</lang>
</syntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
Vectorized form:
<syntaxhighlight lang="matlab">
<lang MATLAB> printf('%i, ',1:9); printf('%i\n',10);</lang>
printf('%i, ',1:9); printf('%i\n',10);
</syntaxhighlight>
 
Explicite loop:
<syntaxhighlight lang="matlab">
<lang Matlab> for k=1:10,
for k=1:10,
printf('%i', k);
if k==10, break; end;
printf(', ');
end;
printf('\n');</lang>
</syntaxhighlight>
 
Output:
Line 1,886 ⟶ 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 "%" ", "
)
)</lang>
</syntaxhighlight>
 
=={{header|Metafont}}==
Line 1,898 ⟶ 2,550:
and then we output it.
 
<syntaxhighlight lang ="metafont">last := 10;
last := 10;
string s; s := "";
for i = 1 upto last:
Line 1,905 ⟶ 2,558:
endfor
message s;
end</lang>
</syntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
<lang smallbasic>For i = 1 To 10
TextWindow.Write(i)
If i <> 10 Then
TextWindow.Write(", ")
EndIf
EndFor
TextWindow.WriteLine("")</lang>
 
=={{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">
<lang min>1 (dup 10 ==) 'puts! (print succ ", " print!) () linrec</lang>
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,935 ⟶ 2,583:
END;
IO.Put("\n");
END Loop.</lang>
</syntaxhighlight>
 
=={{header|MUMPS}}==
<syntaxhighlight lang="mumps">
<lang MUMPS>LOOPHALF
LOOPHALF
NEW I
FOR I=1:1:10 DO
Line 1,946 ⟶ 2,596:
;Alternate
NEW I FOR I=1:1:10 WRITE I WRITE:I'=10 ", "
KILL I QUIT</lang>
</syntaxhighlight>
Output:<pre>USER>D LOOPHALF^ROSETTA
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Line 1,954 ⟶ 2,605:
 
=={{header|Nemerle}}==
<syntaxhighlight lang="nemerle">
<lang Nemerle>foreach (i in [1 .. 10])
foreach (i in [1 .. 10])
{
Write(i);
unless (i == 10) Write(", ");
}
}</lang>
</syntaxhighlight>
 
=={{header|NetRexx}}==
<syntaxhighlight lang="netrexx">
<lang NetRexx>/* NetRexx */
/* NetRexx */
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
Line 1,977 ⟶ 2,631:
end
end i_
say rs.strip()</lang>
</syntaxhighlight>
'''Output'''
<pre>Loops/N plus one half
Line 1,983 ⟶ 2,638:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang NewLISP>(for (i 0 10)
(for (i 0 10)
(print i)
(unless (= i 10)
(print ", ")))</lang>
</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<lang nim>var s = ""
var s = ""
for i in 1..10:
s.add $i
if i == 10: break
s.add ", "
echo s</lang>
</syntaxhighlight>
 
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</pre>
 
=={{header|NS-HUBASICNu}}==
<syntaxhighlight lang="nu">
<lang NS-HUBASIC>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</lang>
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}}==
<langsyntaxhighlight lang="objeck">
bundle Default {
class Hello {
Line 2,022 ⟶ 2,739:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
<syntaxhighlight lang ="ocaml">let last = 10 in
let last = 10 in
for i = 1 to last do
print_int i;
Line 2,031 ⟶ 2,749:
print_string ", ";
done;
print_newline();</lang>
</syntaxhighlight>
 
<syntaxhighlight lang="ocaml">
<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);</lang>
</syntaxhighlight>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">
<lang Oforth>: loopn
: loopn
| i |
10 loop: i [ i dup print 10 ifEq: [ break ] "," . ] printcr ;</lang>
</syntaxhighlight>
 
=={{header|Oz}}==
Using a for-loop:
<syntaxhighlight lang="oz">
<lang oz>
for N in {List.number 1 10 1} break:Break do
{System.printInfo N}
if N == 10 then {Break} end
{System.printInfo ", "}
end</lang>
</syntaxhighlight>
 
However, it seems more natural to use a left fold:
<syntaxhighlight lang="oz">
<lang oz>declare
declare
fun {CommaSep Xs}
case Xs of nil then nil
Line 2,063 ⟶ 2,788:
end
in
{System.showInfo {CommaSep {List.number 1 10 1}}}</lang>
</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">
<lang panda>array{{1..10}}.join(',')</lang>
array{{1..10}}.join(',')
</syntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">n=0;
n=0;
while(1,
print1(n++);
if(n>9, break);
print1(", ")
);</lang>
</syntaxhighlight>
 
=={{header|Pascal}}==
<syntaxhighlight lang ="pascal">program numlist(output);
program numlist(output);
 
const MAXNUM: integer = 10;
Line 2,097 ⟶ 2,828:
write(i, ', ');
writeln(MAXNUM);
end.</lang>
</syntaxhighlight>
 
=={{header|Peloton}}==
<syntaxhighlight lang="sgml">
<lang sgml><@ FORLITLIT>10|<@ SAYPOSFOR>...</@><@ ABF>,</@></@></lang>
<@ 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";</lang>
</syntaxhighlight>
 
In perl one would solve the task via <code>join</code>.
<syntaxhighlight lang="perl">
<lang perl>print join(', ', 1..10), "\n";</lang>
print join(', ', 1..10), "\n";
</syntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phix>--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,120 ⟶ 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>
<!--</lang>-->
</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">
/# 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>
{{out}}
<pre>1, 2, 3, 4, 5, 6, 7, 8, 9, 10
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
<syntaxhighlight lang="php">
<lang php>for ($i = 1; $i <= 11; $i++) {
for ($i = 1; $i <= 11; $i++) {
echo $i;
if ($i == 10)
Line 2,129 ⟶ 2,881:
echo ', ';
}
echo "\n";</lang>
</syntaxhighlight>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">
<lang PicoLisp>(for N 10
(for N 10
(prin N)
(T (= N 10))
(prin ", ") )</lang>
</syntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">
int main(){
for(int i = 1; i <= 11; i++){
Line 2,148 ⟶ 2,903:
}
write("\n");
}
}</lang>
</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">
<lang>> "1, 2, 3"/", ";
> "1, 2, 3"/", ";
Result: ({ "1", "2", "3" })
> ({ "1", "2", "3" })*", ";
Result: "1, 2, 3"</lang>
</syntaxhighlight>
 
So achieving the result of this task with the method more suited to
Pike would be:
<syntaxhighlight lang="text">
<lang>array a = (array(string))enumerate(10, 1, 1);
array a = (array(string))enumerate(10, 1, 1);
write(a * ", " + "\n");</lang>
write(a * ", " + "\n");
</syntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
do i = 1 to 10;
put edit (trim(i)) (a);
if i < 10 then put edit (', ') (a);
end;
</syntaxhighlight>
</lang>
 
=={{header|Plain English}}==
<syntaxhighlight lang ="plainenglish">To run:
To run:
Start up.
Write the numbers up to 10 on the console.
Line 2,181 ⟶ 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.</lang>
</syntaxhighlight>
 
=={{header|Pop11}}==
<syntaxhighlight lang ="pop11">lvars i;
lvars i;
for i from 1 to 10 do
printf(i, '%p');
Line 2,190 ⟶ 2,953:
printf(', ', '%p');
endfor;
printf('\n', '%p');</lang>
</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,201 ⟶ 2,966:
}
Write-Host -NoNewLine ", "
}
}</lang>
</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 }
}
}</lang>
</syntaxhighlight>
 
=={{header|Prolog}}==
<syntaxhighlight lang ="prolog">example :-
example :-
between(1,10,Val), write(Val), Val<10, write(', '), fail.
example.</lang>
</syntaxhighlight>
<pre>?- example.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
true.</pre>
 
=={{header|PureBasic}}==
 
<lang PureBasic>x=1
Repeat
Print(Str(x))
x+1
If x>10: Break: EndIf
Print(", ")
ForEver</lang>
 
=={{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">
<lang python>print ( ', '.join(str(i+1) for i in range(10)) )</lang>
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,243 ⟶ 3,006:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
>>></lang>
</syntaxhighlight>
 
List comprehension one-liner
<langsyntaxhighlight lang="python">
[print(str(i+1) + ", ",end='') if i < 9 else print(i+1) for i in range(10)]
</syntaxhighlight>
</lang>
 
{{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">
<lang Quackery> 10 times
10 times
[ i^ 1+ echo
i 0 = iff
conclude done
say ", " ]</lang>
</syntaxhighlight>
 
{{out}}
Line 2,264 ⟶ 3,041:
=={{header|R}}==
The natural way to solve this task in R is:
<syntaxhighlight lang="r">
<lang R>paste(1:10, collapse=", ")</lang>
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">
<lang R>for(i in 1:10)
for(i in 1:10)
{
cat(i)
Line 2,275 ⟶ 3,055:
}
cat(", ")
}
}</lang>
</syntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight Racket>#lang ="racket">
#lang racket
(for ((i (in-range 1 15)))
(display i)
#:break (= 10 i)
(display ", "))</lang>
</syntaxhighlight>
 
Gives the desired output.
Line 2,288 ⟶ 3,071:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>for 1 .. 10 {
.print;
last when 10;
Line 2,294 ⟶ 3,077:
}
 
print "\n";</lang>
</syntaxhighlight>
 
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">
<lang REBOL>REBOL [
REBOL [
Title: "Loop Plus Half"
URL: http://rosettacode.org/wiki/Loop/n_plus_one_half
Line 2,307 ⟶ 3,092:
prin ", "
]
print ""</lang>
</syntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
Red[]
 
repeat i 10 [
Line 2,317 ⟶ 3,104:
prin ", "
]
print ""</lang>
</syntaxhighlight>
 
=={{header|Relation}}==
<syntaxhighlight lang="relation">
<lang Relation>
set i = 1
set result = ""
Line 2,331 ⟶ 3,119:
end while
echo result
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
===two CHAROUTs===
<syntaxhighlight lang="rexx">
<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,341 ⟶ 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. */</lang>
</syntaxhighlight>
'''output'''
<pre>
Line 2,348 ⟶ 3,138:
 
===one CHAROUT===
<syntaxhighlight lang="rexx">
<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. */</lang>
</syntaxhighlight>
'''output'''
<pre>
Line 2,360 ⟶ 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,366 ⟶ 3,159:
Parse Var list item list
Call charout ,item
End</lang>
</syntaxhighlight>
{{out}}
<pre>aa, bb, cc, dd</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
for x = 1 to 10 see x if x=10 exit ok see ", " next see nl
</syntaxhighlight>
</lang>
 
=={{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}}==
<langsyntaxhighlight lang="ruby">
(1..10).each do |i|
print i
Line 2,382 ⟶ 3,197:
print ", "
end
puts</lang>
</syntaxhighlight>
More idiomatic Ruby to obtain the same result is:
<langsyntaxhighlight lang="ruby">
puts (1..10).join(", ")</lang>
</syntaxhighlight>
 
=={{header|Run BASIC}}==
<lang runbasic>
FOR i = 1 TO 10
PRINT cma$;i;
cma$ = " , "
NEXT i
</lang>
 
=={{header|Rust}}==
<syntaxhighlight lang ="rust">fn main() {
fn main() {
for i in 1..=10 {
print!("{}{}", i, if i < 10 { ", " } else { "\n" });
}
}
}</lang>
</syntaxhighlight>
 
More like the problem description:
<syntaxhighlight lang ="rust">fn main() {
fn main() {
for i in 1..=10 {
print!("{}", i);
Line 2,412 ⟶ 3,224:
}
println!();
}
}</lang>
</syntaxhighlight>
 
Alternative solution using <code>join</code>.
<syntaxhighlight lang ="rust">fn main() {
fn main() {
println!(
"{}",
Line 2,423 ⟶ 3,237:
.join(", ")
);
}
}</lang>
</syntaxhighlight>
 
=={{header|S-lang}}==
Line 2,430 ⟶ 3,245:
part from the bottom to the top of the loop, then NOT include it on the
FIRST pass:
<syntaxhighlight lang S="s-lang">variable more = 0, i;
variable more = 0, i;
foreach i ([1:10]) {
if (more) () = printf(", ");
printf("%d", i);
more = 1;
}
}</lang>
</syntaxhighlight>
 
=={{header|Salmon}}==
<syntaxhighlight lang="salmon">
<lang Salmon>iterate (x; [1...10])
iterate (x; [1...10])
{
print(x);
Line 2,445 ⟶ 3,263:
print(", ");
};
print("\n");</lang>
</syntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<syntaxhighlight lang="scala">
<lang Scala>var i = 1
var i = 1
while ({
print(i)
Line 2,457 ⟶ 3,277:
i += 1
}
println()</lang>
</syntaxhighlight>
<lang Scala>println((1 to 10).mkString(", "))</lang>
<syntaxhighlight lang="scala">
println((1 to 10).mkString(", "))
</syntaxhighlight>
 
=={{header|Scheme}}==
It is possible to use continuations:
<syntaxhighlight lang="scheme">
<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 ", "))))</lang>
</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,476 ⟶ 3,302:
(begin
(display ", ")
(loop (+ 1 i)))))</lang>
</syntaxhighlight>
 
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">
<lang>for i=1:10
for i=1:10
printf("%2d ",i)
if i<10 then printf(", "); end
end
printf("\n")</lang>
</syntaxhighlight>
{{out}}
<pre> 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 </pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">
<lang seed7>$ include "seed7_05.s7i";
$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,502 ⟶ 3,332:
end for;
writeln;
end func;</lang>
</syntaxhighlight>
 
=={{header|Sidef}}==
<syntaxhighlight lang ="ruby">for (1..10) { |i|
for (1..10) { |i|
print i;
i == 10 && break;
Line 2,511 ⟶ 3,343:
}
 
print "\n";</lang>
</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: ', ' ]
]
]</lang>
</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: ', '
]
]</lang>
</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,532 ⟶ 3,369:
line output, and to use the same statement for loop control and the comma.
 
<syntaxhighlight lang="snobol4">
<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</lang>
</syntaxhighlight>
 
{{works with|Macro Spitbol}}
Line 2,543 ⟶ 3,382:
 
This example also breaks the loop explicitly:
<syntaxhighlight lang="snobol4">
<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</lang>
</syntaxhighlight>
 
{{out}}
Line 2,554 ⟶ 3,395:
 
=={{header|SNUSP}}==
<syntaxhighlight lang="snusp">
<lang snusp>@\>@\>@\>+++++++++<!/+. >-?\# digit and loop test
@\>@\>@\>+++++++++<!/+. >-?\# digit and loop test
| | \@@@+@+++++# \>>.<.<</ comma and space
| \@@+@@+++++#
\@@@@=++++#</lang>
</syntaxhighlight>
 
=={{header|Spin}}==
Line 2,564 ⟶ 3,407:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<langsyntaxhighlight lang="spin">con
con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 2,582 ⟶ 3,426:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 2,589 ⟶ 3,434:
 
=={{header|Stata}}==
<syntaxhighlight lang ="stata">forv i=1/10 {
forv i=1/10 {
di `i' _continue
if `i'<10 {
Line 2,597 ⟶ 3,443:
di
}
}
}</lang>
</syntaxhighlight>
 
=== Mata ===
<langsyntaxhighlight lang="stata">mata
mata
for (i=1; i<=10; i++) {
printf("%f",i)
Line 2,609 ⟶ 3,457:
}
}
end</lang>
</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,620 ⟶ 3,470:
}
print(", ")
}
}</lang>
</syntaxhighlight>
{{works with|Swift|2}} The usual way to do this with Swift 2 is:
<langsyntaxhighlight lang="swift">
for i in 1...10 {
print(i, terminator: i == 10 ? "\n" : ", ")
}
</syntaxhighlight>
</lang>
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,636 ⟶ 3,488:
}
print(", ", terminator: "")
}
}</lang>
</syntaxhighlight>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
<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 ""</lang>
</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">
<lang tcl>proc range {from to} {
proc range {from to} {
set result {}
for {set i $from} {$i <= $to} {incr i} {
Line 2,654 ⟶ 3,510:
}
puts [join [range 1 10] ", "] ;# ==> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10</lang>
</syntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
There is no horizontal cursor position on the program IO screen, so we concatenate strings instead.
 
<lang ti89b>Local str
"" → str
For i,1,10
str & string(i) → str
If i < 10 Then
str & "," → str
EndIf
EndFor
Disp str</lang>
 
=={{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.
<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</lang>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
line=""
Line 2,688 ⟶ 3,522:
ENDLOOP
PRINT line
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,695 ⟶ 3,529:
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="bash">
<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</lang>
</syntaxhighlight>
 
{{works with|Bash}}
<syntaxhighlight lang="bash">
<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</lang>
</syntaxhighlight>
 
=={{header|UnixPipes}}==
The last iteration is handled automatically for us
when there is no element in one of the pipes.
<syntaxhighlight lang="bash">
<lang bash>yes \ | cat -n | head -n 10 | paste -d\ - <(yes , | head -n 9) | xargs echo</lang>
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,724 ⟶ 3,565:
out ", " console
end for
out endl console</lang>
</syntaxhighlight>
 
=={{header|V}}==
<syntaxhighlight lang ="v">[loop
[loop
[ [10 =] [puts]
[true] [dup put ',' put succ loop]
] when].</lang>
</syntaxhighlight>
Using it
|1 loop
Line 2,737 ⟶ 3,581:
=={{header|Vala}}==
{{trans|C}}
<syntaxhighlight lang ="vala">void main() {
void main() {
for (int i = 1; i <= 10; i++)
{
Line 2,743 ⟶ 3,588:
stdout.printf(i == 10 ? "\n" : ", ");
}
}
}</lang>
</syntaxhighlight>
 
=={{header|VBA}}==
<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</lang>
 
=={{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</lang>
</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">
<lang vim>for i in range(1, 10)
for i in range(1, 10)
echon i
if (i != 10)
echon ", "
endif
endfor</lang>
</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
<lang vbnet>For i = 1 To 10
Console.Write(i)
If i = 10 Then Exit For
Console.Write(", ")
Next</lang>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang ="go">fn main() {
fn main() {
for i := 1; ; i++ {
print(i)
Line 2,788 ⟶ 3,641:
print(", ")
}
}
}</lang>
</syntaxhighlight>
 
=={{header|Wart}}==
<syntaxhighlight lang="wart">
<lang wart>for i 1 (i <= 10) ++i
for i 1 (i <= 10) ++i
pr i
if (i < 10)
pr ", "
(prn)</lang>
</syntaxhighlight>
 
=={{header|Wee Basic}}==
print 1 "" ensures the end of program text is separate from the list of numbers.
<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</lang>
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">
<lang ecmascript>for (i in 1..10) {
for (i in 1..10) {
System.write(i)
System.write((i < 10) ? ", " : "\n")
}
}</lang>
</syntaxhighlight>
 
{{out}}
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</pre>
 
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<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
</lang>
{{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">
<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,862 ⟶ 3,680:
];
CrLf(0);
]
]</lang>
</syntaxhighlight>
 
 
=={{header|Yabasic}}==
<lang Yabasic>for i = 1 to 10
print i;
if i < 10 then print ", "; : endif
next i
print
 
end</lang>
 
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">
<lang zkl>foreach n in ([1..10]){ print(n); if (n!=10) print(",") }</lang>
foreach n in ([1..10]){ print(n); if (n!=10) print(",") }
</syntaxhighlight>
Or, using a state machine:
<syntaxhighlight lang="zkl">
<lang zkl>[1..10].pump(Console.print, Void.Drop, T(Void.Write,",",Void.Drop));</lang>
[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,884 ⟶ 3,696:
collects things to write to the sink: a comma and the number, eg ",2".
Or:
<syntaxhighlight lang="zkl">
<lang zkl>[1..10].pump(Console.print, Void.Drop, fcn(n){ String(",",n) });</lang>
[1..10].pump(Console.print, Void.Drop, fcn(n){ String(",",n) });
</syntaxhighlight>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
<lang zig>const std = @import("std");
const std = @import("std");
pub fn main() !void {
const stdout_wr = std.io.getStdOut().writer();
Line 2,899 ⟶ 3,714:
}
}
}
}</lang>
</syntaxhighlight>
 
{{omit from|PL/0}}
3,038

edits