Loops/For: Difference between revisions

15,634 bytes added ,  1 month ago
Zig sorted alphabetically before zkn
(Zig sorted alphabetically before zkn)
 
(38 intermediate revisions by 26 users not shown)
Line 45:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">L(i) 1..5
L 1..i
print(‘*’, end' ‘’)
print()</langsyntaxhighlight>
 
Pythonic solution:
<langsyntaxhighlight lang="11l">L(i) 1..5
print(‘*’ * i)</langsyntaxhighlight>
 
=={{header|360 Assembly}}==
;Basic - Algol style
The opcode BXH uses 3 registers, one for index, one for step and one for limit.
<langsyntaxhighlight lang="360asm">
* Loops/For - BXH Algol 27/07/2015
LOOPFOR CSECT
Line 80:
YREGS
END LOOPFOR
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 91:
;Structured Macros
Structured and without BXH, only one register used by loop.
<langsyntaxhighlight lang="360asm">
* Loops/For - struct 29/06/2016
LOOPFOR CSECT
Line 112:
YREGS
END LOOPFOR
</langsyntaxhighlight>
{{out}}
Same as above
 
=={{header|68000 Assembly}}==
Although it's more natural for the language to loop downward rather than forward, both are possible.
<syntaxhighlight lang="68000devpac">main:
MOVEQ #1,D1 ;counter for how many times to print *, this is also the loop counter
.outerloop:
MOVE.W D1,D2
SUBQ.W #1,D2
.innerloop:
MOVE.B #'*',D0
JSR PrintChar ;hardware-dependent printing routine
DBRA D2,.innerloop ;DBRA loops until wraparound to $FFFF, which is why we subtracted 1 from D2 earlier.
JSR NewLine ;hardware-dependent newline routine
ADDQ.W #1,D1
CMP.W #6,D1 ;are we done yet?
BCS .outerloop ;if not, go back to the top
RTS</syntaxhighlight>
 
=={{header|8086 Assembly}}==
The following works with MS-DOS. Called as a subroutine (i.e. "call StarSub")
<langsyntaxhighlight lang="asm">StarSub:
 
mov ah,02h ;needed to prime the interrupt command for printing to screen
Line 141 ⟶ 158:
jnz outer_loop
 
ret</langsyntaxhighlight>
 
=={{header|8th}}==
This illustrates two kinds of 'for' loop. The first kind is "loop", which iterates from the low to the high value, and passes the current loop index as a parameter to the inner word. The second is 'times', which takes a count and repeats the word that many times.
 
<langsyntaxhighlight lang="forth">
( ( '* putc ) swap times cr ) 1 5 loop
</syntaxhighlight>
</lang>
=={{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 loop64.s */
Line 205 ⟶ 222:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">Proc Main()
byte I,J
 
Line 220 ⟶ 237:
Od
 
Return</langsyntaxhighlight>
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">var str:String = "";
for (var i:int = 1; i <= 5; i++) {
for (var j:int = 1; j <= i; j++)
Line 229 ⟶ 246:
trace(str);
str = "";
}</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">for I in 1..5 loop
for J in 1..I loop
Put("*");
end loop;
New_Line;
end loop;</langsyntaxhighlight>
 
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<langsyntaxhighlight lang="agena">for i to 5 do
for j to i do
write( "*" )
od;
print()
od</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
<langsyntaxhighlight lang="algol60">INTEGER I,J;
FOR I:=1 STEP 1 UNTIL 5 DO
BEGIN
Line 256 ⟶ 273:
OUTLINE
END
</syntaxhighlight>
</lang>
 
=={{header|ALGOL 68}}==
Line 264 ⟶ 281:
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
<langsyntaxhighlight lang="algol68">FOR i TO 5 DO
TO i DO
print("*")
OD;
print(new line)
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 281 ⟶ 298:
=={{header|ALGOL W}}==
In Algol W, write starts a new line, writeon continues it.
<langsyntaxhighlight lang="algolw">begin
for i := 1 until 5 do
begin
Line 290 ⟶ 307:
end j
end i
end.</langsyntaxhighlight>
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algol">BEGIN
INTEGER I, J;
FOR I := 1 STEP 1 UNTIL 5 DO
Line 301 ⟶ 318:
WRITEON( "*" );
END;
END</langsyntaxhighlight>
{{out}}
<pre>*
Line 311 ⟶ 328:
=={{header|Alore}}==
 
<langsyntaxhighlight Alorelang="alore">for i in 0 to 6
for j in 0 to i
Write('*')
Line 317 ⟶ 334:
WriteLn()
end
</syntaxhighlight>
</lang>
 
=={{header|AmigaE}}==
<langsyntaxhighlight lang="amigae">PROC main()
DEF i, j
FOR i := 1 TO 5
Line 326 ⟶ 343:
WriteF('\n')
ENDFOR
ENDPROC</langsyntaxhighlight>
 
=={{header|Apex}}==
<langsyntaxhighlight lang="java">for (Integer i = 0; i < 5; i++) {
String line = '';
 
Line 349 ⟶ 366:
for (String line : lines) {
System.debug(line);
}</langsyntaxhighlight>
 
=={{header|APL}}==
TheFor most purposes, the APL analogue of a for loop is the '''each''' operator <tt>¨</tt>. The most natural way to accomplish this task doesn't use a nested '''each''', but the '''repeat''' operator <tt>/</tt> inside a single '''each''':
 
<syntaxhighlight lang="apl">stars ← { ⍵ 1 ⍴ {⍵/'*'}¨⍳⍵ }</syntaxhighlight>
<lang APL>{⎕←⍵/'*'}¨⍳5</lang>
 
However, toTo stick to the letter of the task description, we can nest an '''each''' inside another one., This becomes less portable asbut it relies on '''⎕ucs'''s toa generatelittle asilly. newline,Plus assumesnot thatall newlinedialects issupport characterniladic 10dfns, andso requiresthe supportinnermost for"return multipleone statements inside an anonymousstar" function (dfn,has <tt>{</tt>to ...take <tt>}</tt>),a which e.g. GNU APL does notdummy haveargument:
 
<syntaxhighlight lang="apl">stars ← { ⍵ 1 ⍴ { {1⌷'*',⍵} ¨ ⍳⍵ } ¨ ⍳⍵ }</syntaxhighlight>
Additionally, Dyalog and some other dialects support the more traditional structured programming controls inside a named function definition (tradfn):
{{works with|Dyalog APL}}
 
<syntaxhighlight lang="apl">∇result ← stars count; i; j; vec
<lang APL>{_←{⍞←'*'}¨⍳⍵ ⋄ ⍞←⎕ucs 10}¨⍳5</lang>
vec ← ⍬
:for i :in ⍳ count
vec ,← ⊂''
:for j :in ⍳ i
vec[i],←'*'
:endfor
:endfor
result ← count 1 ⍴ vec
∇</syntaxhighlight>
 
{{Out}}
 
The result of all three implementations of '''stars''' is a column vector, which is displayed like this:
The output of both versions is the same:
 
<pre>* stars 5
*
**
***
Line 373 ⟶ 403:
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">set x to linefeed
repeat with i from 1 to 5
repeat with j from 1 to i
Line 380 ⟶ 410:
set x to x & linefeed
end repeat
return x</langsyntaxhighlight>
{{out}}
<pre>*
Line 390 ⟶ 420:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program loop1.s */
Line 458 ⟶ 488:
bx lr /* return */
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">loop 0..5 'x [
loop 0..x 'y [
prints "*"
]
print ""
]</langsyntaxhighlight>
{{out}}
<pre>*
Line 474 ⟶ 504:
*****
******</pre>
 
=={{header|Asymptote}}==
Asymptote's control structures are similar to those in C, C++, or Java
<syntaxhighlight lang="asymptote">for(int i = 0; i < 6; ++i) {
for(int j = 0; j < i; ++j) {
write("*", suffix=none);
}
write("");
}</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Gui, Add, Edit, vOutput r5 w100 -VScroll ; Create an Edit-Control
Gui, Show ; Show the window
Loop, 5 ; loop 5 times
Line 488 ⟶ 527:
Output .= (A_Index = 5) ? "" : "`n" ; append a new line to the output if A_Index is not "5"
}
Return ; End of auto-execution section</langsyntaxhighlight>
 
=={{header|Avail}}==
<langsyntaxhighlight Availlang="avail">For each row from 1 to 5 do
[
For each length from 1 to row do [Print: "*";];
Print: "\n";
];</langsyntaxhighlight>
Since the inner loop index is unneeded, it may be more natural to use the <code>From_to_do_</code> loop format:
<langsyntaxhighlight Availlang="avail">For each row from 1 to 5 do
[
From 1 to row do [Print: "*";];
Print: "\n";
];</langsyntaxhighlight>
 
=={{header|AWK}}==
 
<langsyntaxhighlight lang="awk">BEGIN {
for(i=1; i < 6; i++) {
for(j=1; j <= i; j++ ) {
Line 512 ⟶ 551:
print
}
}</langsyntaxhighlight>
 
=={{header|Axe}}==
In this example, the Axe code is nearly identical to the [[#TI-83_BASIC|TI-83 BASIC]] version. However, note the swapped order of the I and J in the Output() statement. Also, unlike TI-83 BASIC, Axe does not support an increment value other than 1.
<langsyntaxhighlight lang="axe">ClrHome
For(I,1,5)
For(J,1,I)
Output(J,I,"*")
End
End</langsyntaxhighlight>
 
=={{header|Babel}}==
 
<langsyntaxhighlight lang="babel">((main { 10 star_triangle ! })
 
(star_triangle {
Line 535 ⟶ 574:
"\n" << }
->
times }))</langsyntaxhighlight>
 
{{out}}
Line 551 ⟶ 590:
The key operator here is 'iter' which gives the current iteration of the loop body it
resides in. When used with the 'times' operator, it generates a countdown.
 
=={{header|Bait}}==
<syntaxhighlight lang="bait">
const ROWS := 5
 
fun main() {
for i := 1; i <= ROWS; i += 1 {
for j := 1; j <= i; j += 1 {
print('*')
}
println('')
}
}
</syntaxhighlight>
 
 
=={{header|bash}}==
<langsyntaxhighlight lang="bash">
for i in {1..5}
do
Line 562 ⟶ 616:
echo
done
</syntaxhighlight>
</lang>
 
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight lang="qbasic">for i = 1 to 5
for j = 1 to i
print "*";
next j
print
next i</langsyntaxhighlight>
 
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">FOR I = 1 TO 5 : FOR J = 1 TO I : PRINT "*"; : NEXT J : PRINT : NEXT</langsyntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">for i = 1 to 5
for j = 1 to i
print "*";
Line 583 ⟶ 637:
print
next i
end</langsyntaxhighlight>
 
==={{header|BaCon}}===
<langsyntaxhighlight lang="freebasic">
FOR i = 1 TO 5
FOR j = 1 TO i
Line 593 ⟶ 647:
PRINT
NEXT
</syntaxhighlight>
</lang>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="text">
FOR I% = 1 TO 5
FOR J% = 1 TO I%
Line 604 ⟶ 658:
PRINT
NEXT
</syntaxhighlight>
</lang>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 for i = 1 to 5
20 for j = 1 to i
30 print "*";
40 next j
50 print
60 next i</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
<langsyntaxhighlight lang="qbasic">10 FOR I = 1 TO 5
20 : FOR J = 1 TO I
30 : PRINT "*";
40 : NEXT J
50 : PRINT
60 NEXT I</langsyntaxhighlight>
 
==={{header|Creative Basic}}===
<syntaxhighlight lang="creative basic">
<lang Creative Basic>
OPENCONSOLE
 
Line 637 ⟶ 700:
 
END
</syntaxhighlight>
</lang>
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="qbasic">10 FOR I = 1 TO 5
20 FOR J = 1 TO I
30 PRINT "*";
Line 646 ⟶ 709:
50 PRINT
60 NEXT I
</syntaxhighlight>
</lang>
 
==={{header|FBSL}}===
<langsyntaxhighlight lang="qbasic">
#APPTYPE CONSOLE
FOR dim i = 1 TO 5
Line 657 ⟶ 720:
PRINT
NEXT i
Pause</langsyntaxhighlight>
{{out}}
<pre>
Line 668 ⟶ 731:
 
==={{header|FUZE BASIC}}===
<langsyntaxhighlight lang="qbasic">FOR n = 1 to 5 CYCLE
FOR k = 1 to n CYCLE
print "*";
Line 674 ⟶ 737:
PRINT
REPEAT
END</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 FOR I=1 TO 5
110 FOR J=1 TO I
120 PRINT "*";
130 NEXT
140 PRINT
150 NEXT</langsyntaxhighlight>
 
==={{header|IWBASIC}}===
<syntaxhighlight lang="iwbasic">
<lang IWBASIC>
OPENCONSOLE
 
Line 705 ⟶ 768:
 
'Could also have been written the same way as the Creative Basic example, with no LOCATE command.
</syntaxhighlight>
</lang>
 
==={{header|Liberty BASIC}}===
Unlike some BASICs, Liberty BASIC does not require that the counter variable be specified with 'next'.
<langsyntaxhighlight lang="lb">for i = 1 to 5
for j = 1 to i
print "*";
next
print
next</syntaxhighlight>
next
</lang>
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Microsoft Small Basic}}===
<langsyntaxhighlight lang="microsoftsmallbasic">For i = 1 To 5
For j = 1 To i
TextWindow.Write("*")
EndFor
TextWindow.WriteLine("")
EndFor</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()
Define i, j
For i=1 To 5
Line 737 ⟶ 801:
Print(#LFCR$+"Press ENTER to quit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
 
==={{header|QBasic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
Also
The [[#BASIC256|BASIC256]] solution works without any changes.
 
==={{header|Quite BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Run BASIC}}===
<langsyntaxhighlight lang="runbasic">FOR i = 1 TO 5
FOR i = 1 TO 5
FOR j = 1 TO i
PRINT "*";
NEXT j
PRINT
NEXT i</syntaxhighlight>
</lang>
 
==={{header|smart BASIC}}===
While some versions of BASIC allow for NEXT without a variable, smart BASIC requires the variable designation.
 
<langsyntaxhighlight lang="qbasic">for n = 1 to 5
for m = 1 to n
print "*";
next m
print
next n</langsyntaxhighlight>
 
 
==={{header|True BASIC}}===
<syntaxhighlight lang="basic">FOR i = 1 TO 5
<lang basic>
FOR i = 1 TO 5
FOR j = 1 TO i
PRINT "*";
Line 768 ⟶ 836:
PRINT
NEXT i
END</syntaxhighlight>
END
</lang>
 
 
==={{header|Visual Basic}}===
'''Works with:''' VB6
<langsyntaxhighlight lang="vb">Public OutConsole As Scripting.TextStream
For i = 0 To 4
For j = 0 To i
Line 780 ⟶ 846:
Next j
OutConsole.WriteLine
Next i</langsyntaxhighlight>
 
==={{header|Visual Basic .NET}}===
{{works with|Visual Basic .Net 2002}}
<langsyntaxhighlight lang="vbnet">For x As Integer = 0 To 4
For y As Integer = 0 To x
Console.Write("*")
Next
Console.WriteLine()
Next</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight Yabasiclang="yabasic">for i = 1 to 5
for j = 1 to i
print "*";
Line 798 ⟶ 864:
print
next i
end</langsyntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
 
On the ZX Spectrum, we need line numbers:
 
<syntaxhighlight lang="basic">10 FOR i = 1 TO 5
<lang basic>
10 FOR i = 1 TO 5
20 FOR j = 1 TO i
30 PRINT "*";
40 NEXT j
50 PRINT
60 NEXT i</syntaxhighlight>
</lang>
 
=={{header|Batch File}}==
<syntaxhighlight lang="text">@ECHO OFF
 
<lang>@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
 
Line 826 ⟶ 888:
)
 
ENDLOCAL</langsyntaxhighlight>
 
=={{header|bc}}==
<langsyntaxhighlight lang="bc">for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) "*"
"
"
}
quit</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let start() be
Line 843 ⟶ 905:
$( for j = 1 to i do wrch('**')
wrch('*N')
$)</langsyntaxhighlight>
 
=={{header|Befunge}}==
<langsyntaxhighlight lang="befunge">1>:5`#@_:>"*",v
| :-1<
^+1,+5+5<</langsyntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
The following 22-byte BLC program is generated from https://github.com/tromp/AIT/blob/master/rosetta/forloops.lam :
 
<pre>18 18 11 50 73 9c e7 40 b3 df cb df 38 1c bd a3 88 05 bb 00 2a 0a</pre>
 
=={{header|blz}}==
<langsyntaxhighlight lang="blz">for i = 1; i <= 5; i++
line = ""
for (j = 1; j <= i; j++)
Line 857 ⟶ 925:
end
print(line)
end</langsyntaxhighlight>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat"> 0:?i
& whl
' ( !i+1:~>5:?i
Line 868 ⟶ 936:
)
&
);</langsyntaxhighlight>
 
=={{header|Brainf***}}==
<langsyntaxhighlight lang="bf">>>+++++++[>++++++[>+<-]<-] place * in cell 3
+++++[>++[>>+<<-]<-]<< place \n in cell 4
+++++[ set outer loop count
Line 878 ⟶ 946:
>[>>.<<-]>>>.<<< print line
<<-] end inner loop
] end outer loop</langsyntaxhighlight>
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">1.to 5, { i |
1.to i, { j |
print "*"
}
print "\n"
}</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
putchar('*');
puts("");
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
class Program {
Line 911 ⟶ 979:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
for(int i = 0; i < 5; ++i) {
for(int j = 0; j < i; ++j)
Line 920 ⟶ 988:
 
std::cout.put('\n');
}</langsyntaxhighlight>
 
=={{header|C3}}==
<syntaxhighlight lang="c3">
for (int i = 0; i < 5; i++)
{
for (int j = 1; j <= i; j++) io::print("*");
io::printn("*");
}</syntaxhighlight>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
for(i in 1..5) {
Line 931 ⟶ 1,007:
print("");
}
}</langsyntaxhighlight>
 
=={{header|Chapel}}==
<langsyntaxhighlight lang="chapel">for i in 1..5 {
for 1..i do write('*');
writeln();
}</langsyntaxhighlight>
 
=={{header|Chef}}==
 
<langsyntaxhighlight lang="chef">Asterisks Omelette.
 
This recipe prints a triangle of asterisks.
Line 969 ⟶ 1,045:
Pour contents of the mixing bowl into the baking dish.
 
Serves 1.</langsyntaxhighlight>
 
=={{header|Clojure}}==
 
<langsyntaxhighlight lang="clojure">(doseq [i (range 5), j (range (inc i))]
(print "*")
(if (= i j) (println)))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">start_up = proc ()
po: stream := stream$primary_output()
Line 987 ⟶ 1,063:
stream$putl(po, "")
end
end start_up</langsyntaxhighlight>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Display-Triangle.
 
Line 1,011 ⟶ 1,087:
GOBACK
.
</syntaxhighlight>
</lang>
 
=={{header|ColdFusion}}==
Line 1,017 ⟶ 1,093:
 
With tags:
<langsyntaxhighlight lang="cfm"><cfloop index = "i" from = "1" to = "5">
<cfloop index = "j" from = "1" to = "#i#">
*
</cfloop>
< br />
</cfloop></langsyntaxhighlight>
With script:
<langsyntaxhighlight lang="cfm"><cfscript>
for( i = 1; i <= 5; i++ )
{
Line 1,033 ⟶ 1,109:
writeOutput( "< br />" );
}
</cfscript></langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(loop for i from 1 upto 5 do
(loop for j from 1 upto i do
(write-char #\*))
(terpri))</langsyntaxhighlight>
or
<langsyntaxhighlight lang="lisp">(dotimes (i 5)
(dotimes (j (+ i 1))
(write-char #\*))
(terpri))</langsyntaxhighlight>
or
<langsyntaxhighlight lang="lisp">(do ((i 1 (+ i 1)))
((> i 5))
(do ((j 1 (+ j 1)))
((> j i))
(write-char #\*))
(terpri))</langsyntaxhighlight>
or
<langsyntaxhighlight lang="lisp">(use-package :iterate)
(iter
(for i from 1 to 5)
Line 1,059 ⟶ 1,135:
(for j from 1 to i)
(princ #\*))
(terpri))</langsyntaxhighlight>
 
=={{header|Coq}}==
 
<langsyntaxhighlight lang="coq">Section FOR.
Variable T : Type.
Variable body : nat -> T -> T.
Line 1,086 ⟶ 1,162:
)
0 5 nil.
</syntaxhighlight>
</lang>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
var i: uint8 := 1;
Line 1,100 ⟶ 1,176:
print_nl();
i := i + 1;
end loop;</langsyntaxhighlight>
 
=={{header|Crystal}}==
 
<langsyntaxhighlight lang="crystal">
1.upto(5) do |i|
1.upto(i) do |j|
Line 1,111 ⟶ 1,187:
puts
end
</syntaxhighlight>
</lang>
 
Or another way, more succinctly put:
 
<langsyntaxhighlight lang="crystal">
puts (1..5).map { |i| "*" * i }.join("\n")
</syntaxhighlight>
</lang>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio: write, writeln;
 
void main() {
Line 1,135 ⟶ 1,211:
writeln();
}
}</langsyntaxhighlight>
{{out}}
<pre>*
Line 1,150 ⟶ 1,226:
 
=={{header|Dao}}==
<langsyntaxhighlight lang="dao">for( i = 1 : 5 ){
for( j = 1 : i ) io.write( '*' )
io.writeln()
}</langsyntaxhighlight>
 
=={{header|Dart}}==
<langsyntaxhighlight lang="dart">main() {
for (var i = 0; i < 5; i++)
for (var j = 0; j < i + 1; j++)
print("*");
print("\n");
}</langsyntaxhighlight>
 
=={{header|dc}}==
Line 1,167 ⟶ 1,243:
 
{{trans|bc}}
<langsyntaxhighlight lang="dc">[
[*]P [print asterisk]sz
lj 1 + d sj [increment j, leave it on stack]sz
Line 1,181 ⟶ 1,257:
]sB
1 d si [i = 1, leave it on stack]sz
5 !<B [enter loop B if 5 >= i]sz</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program LoopFor;
 
{$APPTYPE CONSOLE}
Line 1,197 ⟶ 1,273:
Writeln;
end;
end.</langsyntaxhighlight>
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">set_ns(rosettacode);
 
add_var({str},s)_value();
add_var({str},output);
 
add_for({int},i)_from(1)_uptoand(5)_inc()
with_var(s)_v();
add_for({int},j)_from(0)_upto([i])_inc()
with_var(s)_calc([s]&="*");
;
with_var(output)_calc(&=[s]&\n);
;
 
me_msg([output]);
 
reset_ns[];</syntaxhighlight>
 
=={{header|DMS}}==
<langsyntaxhighlight DMSlang="dms">number i, j
for (i = 1; i <= 5; i++)
{
Line 1,208 ⟶ 1,302:
}
Result( "\n" )
}</langsyntaxhighlight>
 
=={{header|dodo0}}==
<langsyntaxhighlight lang="dodo0">fun for -> var, test, body, return # define a for loop using recursion
(
test(var) -> continue
Line 1,244 ⟶ 1,338:
)
| result
exit()</langsyntaxhighlight>
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">proc nonrec main() void:
byte i,j;
for i from 1 upto 5 do
Line 1,255 ⟶ 1,349:
writeln()
od
corp</langsyntaxhighlight>
{{out}}
<pre>*
Line 1,264 ⟶ 1,358:
 
=={{header|Dragon}}==
<langsyntaxhighlight lang="dragon">for (i = 0, i < 5, i++) {
for (j = 0, j <= i, j++) {
show "*"
}
showln ""
}</langsyntaxhighlight>
 
=={{header|DWScript}}==
<langsyntaxhighlight Delphilang="delphi">var i, j : Integer;
 
for i := 1 to 5 do begin
Line 1,278 ⟶ 1,372:
Print('*');
PrintLn('');
end;</langsyntaxhighlight>
 
=={{header|Dyalect}}==
{{trans|Swift}}
 
<langsyntaxhighlight Dyalectlang="dyalect">for i in 1..5 {
for _ in 1..i {
print("*", terminator: "")
}
print()
}</langsyntaxhighlight>
 
Output:
Line 1,300 ⟶ 1,394:
=={{header|E}}==
 
<langsyntaxhighlight lang="e">for width in 1..5 {
for _ in 1..width {
print("*")
}
println()
}</langsyntaxhighlight>
 
This loop is a combination of <code>for ... in ...</code> which iterates over something and <code>a..b</code> which is a range object that is iteratable. (Also, writing <code>a..!b</code> excludes the value b.)
Line 1,311 ⟶ 1,405:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">
<lang>for i range 5
for i for= j1 rangeto i5
for j = 1 to i
write "*"
.
print ""
.
.</lang>
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
Line 1,324 ⟶ 1,420:
 
Characters are encoded in five-bit form, with each code point producing a different character depending on whether the machine is in 'letter' or 'figure' mode: this is why it is necessary to output a 'figure shift' control character at the beginning of the job.
<langsyntaxhighlight lang="edsac">[ Loops
=====
Line 1,373 ⟶ 1,469:
[ 24 ] P0F [ used to clear a ]
EZPF [ begin execution ]</langsyntaxhighlight>
{{out}}
<pre>+
Line 1,382 ⟶ 1,478:
 
=={{header|EGL}}==
<langsyntaxhighlight EGLlang="egl">str string;
for ( i int to 5 )
str = "";
Line 1,389 ⟶ 1,485:
end
SysLib.writeStdout(str);
end</langsyntaxhighlight>
 
=={{header|Ela}}==
 
<langsyntaxhighlight lang="ela">open monad io
 
loop m n | n < m = do
Line 1,405 ⟶ 1,501:
| else = do return ()
 
_ = loop 10 1 ::: IO</langsyntaxhighlight>
 
 
Output:
 
<langsyntaxhighlight lang="ela">**
***
****
Line 1,418 ⟶ 1,514:
********
*********
**********</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x :
<langsyntaxhighlight lang="elena">import extensions;
public program()
{
for(int i := 0,; i < 5,; i += 1)
{
for(int j := 0,; j <= i,; j += 1)
{ console.write:("*") };
console.writeLine()
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,444 ⟶ 1,540:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Loops do
def loops_for(n) do
Enum.each(1..n, fn i ->
Line 1,453 ⟶ 1,549:
end
 
Loops.loops_for(5)</langsyntaxhighlight>
 
one line (Comprehensions)
<langsyntaxhighlight lang="elixir">for i <- 1..5, do: IO.puts (for j <- 1..i, do: "*")</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">;; Lisp implementation of c-for is like:
;; (let ((i nil))
;; (while (progn (setq i (if (not i) 0 (1+ i) )) ;; if value of i is nil, initialize its value to 0, if else, add 1
;; (< i 10)) ;; end loop when i > 10
;; (... body ...) ) ) ;; loop body
 
(let ((i nil) (str ""))
(while (progn (setq i (if (not i) 0 (1+ i) ))
(< i 5))
(setq str (concat str "*"))
(message str) ) )</syntaxhighlight>
 
output logged in buffer *Messages*:
 
<pre>
*
**
***
****
*****
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
for int i = 0; i < 5; ++i
for int j = 0; j <= i; ++j do write("*") end
writeLine()
end
</syntaxhighlight>
{{out}}
<pre>
*
**
***
****
*****
</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">%% Implemented by Arjun Sunel
-module(nested_loops).
-export([main/0, inner_loop/0]).
Line 1,483 ⟶ 1,618:
io:format("~n"),
inner_loop(N).
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
FOR I=1 TO 5 DO
FOR J=1 TO I DO
Line 1,493 ⟶ 1,628:
PRINT
END FOR
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
 
<syntaxhighlight lang="euphoria">
<lang Euphoria>
for i = 1 to 5 do
for j = 1 to i do
Line 1,504 ⟶ 1,639:
puts(1, "\n") -- Same as "puts(1, {'\n'})"
end for
</syntaxhighlight>
</lang>
 
<code>puts()</code> is a function that takes two arguments; an <code>integer</code> and a <code>sequence</code>. Strings are simply <code>sequence</code>s; there is no string type.
Line 1,511 ⟶ 1,646:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">#light
[<EntryPoint>]
let main args =
Line 1,518 ⟶ 1,653:
printf "*"
printfn ""
0</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">5 [1,b] [ [ "*" write ] times nl ] each</langsyntaxhighlight>
 
=={{header|FALSE}}==
<langsyntaxhighlight lang="false">1[$6-][$[$]["*"1-]#%"
"1+]#%</langsyntaxhighlight>
 
=={{header|Fantom}}==
Line 1,531 ⟶ 1,666:
Using for loops:
 
<langsyntaxhighlight lang="fantom">
class ForLoops
{
Line 1,546 ⟶ 1,681:
}
}
</syntaxhighlight>
</lang>
 
Using range objects:
 
<langsyntaxhighlight lang="fantom">
class ForLoops
{
Line 1,565 ⟶ 1,700:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Fennel}}==
<langsyntaxhighlight lang="fennel">(for [i 1 4]
(for [j 1 i]
(io.write "*"))
(print))
</syntaxhighlight>
</lang>
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">
for i = 1 to 5 do for j = 1 to i do !'*'; od; !; od
</syntaxhighlight>
</lang>
 
=={{header|FOCAL}}==
When the program exits the outer loop, the control variable <tt>I</tt> is set to 4 + 1 = 5; we can therefore permit execution to fall through into the inner loop for one more iteration.
<langsyntaxhighlight lang="focal">01.10 FOR I=1,4; DO 2.0
 
02.10 FOR J=1,I; TYPE "*"
02.20 TYPE !</langsyntaxhighlight>
{{out}}
<pre>
Line 1,594 ⟶ 1,729:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: triangle ( n -- )
1+ 1 do
cr i 0 do [char] * emit loop
loop ;
5 triangle</langsyntaxhighlight>
One more:
<langsyntaxhighlight lang="forth">
: limit_example
15 1 do r> r@ dup rot >r drop \ Bring limit on stack
Line 1,606 ⟶ 1,741:
loop ;
\ Gforth and JSForth all work, SP-Forth brakes (different 'for' implementation?)
</syntaxhighlight>
</lang>
 
=={{header|Fortran}}==
{{works with|Fortran|77 and later}}
<langsyntaxhighlight lang="fortran">C WARNING: This program is not valid ANSI FORTRAN 77 code. It uses
C one nonstandard character on the line labelled 5001. Many F77
C compilers should be okay with it, but it is *not* standard.
Line 1,642 ⟶ 1,777:
5001 FORMAT (A, $)
C5001 FORMAT (A, ADVANCE='NO')
END</langsyntaxhighlight>
 
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">DO i = 1, 5
DO j = 1, i
WRITE(*, "(A)", ADVANCE="NO") "*"
END DO
WRITE(*,*)
END DO</langsyntaxhighlight>
 
Fortran 95 (and later) has also a loop structure that can be used only when the result is independent from real order of execution of the loop.
 
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">integer :: i
integer, dimension(10) :: v
 
forall (i=1:size(v)) v(i) = i</langsyntaxhighlight>
 
But if one accepts that a do-loop can be expressed without the actual word "do" (or "for"), then
<syntaxhighlight lang="fortran">
<lang Fortran>
DO 1 I = 1,5
1 WRITE (6,*) ("*", J = 1,I)
END
</syntaxhighlight>
</lang>
That is a complete programme, though a more polite source file would have INTEGER I,J. It uses the old-style DO ''label'' etc. style of DO-loop to save on having to specify an END DO. The WRITE statement's output list is generated by an "implied" DO-loop having much of the form of DO J = 1,I and is indeed a proper loop. The output item is a text literal, which in earlier Fortran was unknown, however the result can still be achieved:
<syntaxhighlight lang="fortran">
<lang Fortran>
DO 1 I = 1,5
1 WRITE (6,2) (666, J = 1,I)
2 FORMAT(5I1)
END
</syntaxhighlight>
</lang>
This works because if a value cannot be fitted into its output field, the field is filled with asterisks. Which, is what is wanted! Just allow one digit for output (I1), and present a large integer.
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
For i As Integer = 1 To 5
Line 1,685 ⟶ 1,820:
Next
 
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,697 ⟶ 1,832:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
for n = 1 to 5
{
Line 1,705 ⟶ 1,840:
println[]
}
</syntaxhighlight>
</lang>
 
=={{header|Futhark}}==
Line 1,713 ⟶ 1,848:
inner loop.
 
<syntaxhighlight lang="futhark">
<lang Futhark>
fun main(n: int): [n]int =
loop (a = replicate n 0) = for i < n do
Line 1,721 ⟶ 1,856:
in a)
in a
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">window 1
 
long i, j
Line 1,735 ⟶ 1,870:
next
 
HandleEvents</langsyntaxhighlight>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=9be4a2a6e69b2bf2921279a057df18c5 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim i, j As Integer
 
Line 1,749 ⟶ 1,884:
Next
 
End</langsyntaxhighlight>
<pre>
*
Line 1,759 ⟶ 1,894:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">for i in [1 .. 5] do
for j in [1 .. i] do
Print("*");
Line 1,770 ⟶ 1,905:
# ***
# ****
# *****</langsyntaxhighlight>
 
=={{header|GML}}==
<langsyntaxhighlight GMLlang="gml">pattern = ""
for(i = 1; i <= 5; i += 1)
{
Line 1,782 ⟶ 1,917:
pattern += "#"
}
show_message(pattern)</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,796 ⟶ 1,931:
fmt.Printf("\n")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,808 ⟶ 1,943:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">for(i in (1..6)) {
for(j in (1..i)) {
print '*'
}
println ()
}</langsyntaxhighlight>
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="qbasic">100 FOR I=1 TO 5
110 FOR J=1 TO I
120 PRINT "*";
Line 1,822 ⟶ 1,957:
140 PRINT
150 NEXT I
160 END</langsyntaxhighlight>
 
{{out}}
Line 1,834 ⟶ 1,969:
 
=={{header|Hack}}==
<langsyntaxhighlight lang="hack">for($i = 0; $i < 5; $i++) {
for($j = 0; $j <= $i; $j++) {
echo '*';
Line 1,840 ⟶ 1,975:
echo '\n';
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad
 
main = do
Line 1,849 ⟶ 1,984:
forM_ [1..i] $ \j -> do
putChar '*'
putChar '\n'</langsyntaxhighlight>
 
But it's more Haskellish to do this without loops:
 
<langsyntaxhighlight lang="haskell">import Data.List (inits)
 
main = mapM_ putStrLn $ tail $ inits $ replicate 5 '*'</langsyntaxhighlight>
 
Or, with a list comprehension:
 
<langsyntaxhighlight lang="haskell">putStrLn $ unlines [replicate n '*' | n <- [1..5]]</langsyntaxhighlight>
 
Taking from an infinite stream of increasing length lines:
 
<langsyntaxhighlight lang="haskell">putStrLn . unlines . take 5 $ iterate ('*':) "*"</langsyntaxhighlight>
 
=={{header|Haxe}}==
 
<langsyntaxhighlight Haxelang="haxe">for (i in 1...6) {
for(j in 0...i) {
Sys.print('*');
}
Sys.println('');
}</langsyntaxhighlight>
 
=={{header|hexiscript}}==
<langsyntaxhighlight lang="hexiscript">for let i 1; i <= 5; i++
for let j 1; j <= i; j++
print "*"
endfor
println ""
endfor</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">DO i = 1, 5
DO j = 1, i
WRITE(APPend) "*"
ENDDO
WRITE() ' '
ENDDO</langsyntaxhighlight>
 
=={{header|HolyC}}==
<langsyntaxhighlight lang="holyc">U8 i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
Print("*");
Print("\n");
}</langsyntaxhighlight>
 
== Icon and Unicon ==
==={{header|Icon}}===
<langsyntaxhighlight Iconlang="icon">procedure main()
every i := 1 to 5 do {
every 1 to i do
Line 1,906 ⟶ 2,041:
write()
}
end</langsyntaxhighlight>
==={{header|Unicon}}===
The Icon solution works in Unicon.
 
=={{header|Inform 7}}==
<langsyntaxhighlight lang="inform7">repeat with length running from 1 to 5:
repeat with N running from 1 to length:
say "*";
say line break;</langsyntaxhighlight>
 
=={{header|J}}==
J is array-oriented, so there is very little need for loops. For example, except for the requirement for loops, one could satisfy this task this way:
 
<syntaxhighlight lang=J>
]\ '*****'
*
**
***
****
*****
</syntaxhighlight>
 
J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided).
<syntaxhighlight lang ="j">3 : 0{{
for_i. 1 + i. y do.
z =. ''
for. i. i do.
 
forz=. 1 + i. i do.z,'*'
end.
z=. z,'*'
echo end. z
end.
 
EMPTY
z 1!:2 ] 2
}}0
end.
</syntaxhighlight>
 
i.0 0
)</lang>
 
But you would almost never see J code like this.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">var i, j;
for (i = 1; i <= 5; i += 1) {
s = '';
Line 1,953 ⟶ 2,093:
s += '*';
document.write(s + '<br>');
}</langsyntaxhighlight>
 
 
Line 1,959 ⟶ 2,099:
or a simple range function which generates a range:
 
<langsyntaxhighlight JavaScriptlang="javascript">function range(i) {
return i ? range(i - 1).concat(i) : [];
}
 
range(5) --> [1, 2, 3, 4, 5]</langsyntaxhighlight>
 
We could write something like:
 
<langsyntaxhighlight JavaScriptlang="javascript">var s = '';
 
range(5).forEach(
Line 1,978 ⟶ 2,118:
);
 
console.log(s);</langsyntaxhighlight>
 
but it might be more natural in JavaScript, if we are going to use built-in Array functions, to simplify a little with '''Array.reduce()''', writing:
 
<langsyntaxhighlight JavaScriptlang="javascript">console.log(
range(5).reduce(
function (a, n) {
Line 1,988 ⟶ 2,128:
}, ''
)
);</langsyntaxhighlight>
 
in which the inner ''n'' refers to the Array value visited at the next level out, and the triangle is returned as a single expression, rather than as a series of variable mutations.
Line 1,994 ⟶ 2,134:
Finally, in contexts where an expression composes better than a statement, the effect of a loop can often be expressed as a map.
 
<langsyntaxhighlight JavaScriptlang="javascript">console.log(
range(5).map(function(a) {
return Array(a + 1).join('*');
}).join('\n')
);</langsyntaxhighlight>
 
=={{header|Jinja}}==
Variable usage inside a loop,
before version 2.10 :
<langsyntaxhighlight Jinjalang="jinja">print(Template("""{% set sum = 0 %}
{% for i in range(6) %}
{{ sum }}{% set sum = sum + i %}
{%- endfor %}""").render())
</syntaxhighlight>
</lang>
Since 2.10 :
<langsyntaxhighlight Jinjalang="jinja">print(Template("""{% set sum = namespace(value=0) %}
{% for i in range(6) %}
{{ sum.value }}{% set sum.value = sum.value + i %}
{%- endfor %}""").render())
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq"># Single-string version using explicit nested loops:
def demo(m):
reduce range(0;m) as $i
Line 2,028 ⟶ 2,168:
 
# Variation of demo2 using an implicit inner loop:
def demo3(m): range(1;m) | "*" * . ;</langsyntaxhighlight>
'''Example using demo(6)'''
{{Out}}
Line 2,040 ⟶ 2,180:
=={{header|Jsish}}==
Code from Javascript entry.
<langsyntaxhighlight lang="javascript">var i, j, s;
for (i = 1; i <= 5; i += 1) {
s = '';
for (j = 0; j < i; j += 1) s += '*';
puts(s);
}</langsyntaxhighlight>
 
{{out}}
Line 2,056 ⟶ 2,196:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
for i in 1:5
for j in 1:i
Line 2,063 ⟶ 2,203:
println()
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,073 ⟶ 2,213:
 
=={{header|Klong}}==
<syntaxhighlight lang="k">
<lang K>
:" x{p}:*y means repeat {p} x times starting at y "
 
Line 2,084 ⟶ 2,224:
 
{.p(x:^0c*)}'1+!5
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight Kotlinlang="kotlin">fun main(args: Array<String>) {
(1..5).forEach {
(1..it).forEach { print('*') }
println()
}
}</langsyntaxhighlight>
 
=={{header|LabVIEW}}==
Line 2,099 ⟶ 2,239:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def loops_for
{lambda {:i :n}
Line 2,128 ⟶ 2,268:
* * * *
* * * * *
</syntaxhighlight>
</lang>
 
=={{header|Lang5}}==
<langsyntaxhighlight lang="lang5">: cr "\n" . ; : dip swap '_ set execute _ ;
: nip swap drop ; : last -1 extract nip ;
: times
Line 2,139 ⟶ 2,279:
 
: concat "" join ;
'* 1 5 "2dup reshape concat . cr 1 +" times</langsyntaxhighlight>
 
=={{header|langur}}==
<langsyntaxhighlight lang="langur">for .i = 0; .i < 5; .i += 1 {
for .j = 0; .j <= .i; .j += 1 {
write "*"
}
writeln()
}</langsyntaxhighlight>
 
A for of loop iterates over keys (when used with an array, string, or hash) and a for in loop iterates over values.
 
<langsyntaxhighlight lang="langur">for .i of 5 {
for of .i {
write "*"
}
writeln()
}</langsyntaxhighlight>
 
Or, with one for loop...
<langsyntaxhighlight lang="langur">for .i of 5 {
writeln "*" x* .i
}</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">loop(5) => {^
loop(loop_count) => {^ '*' ^}
'\r'
^}</langsyntaxhighlight>
 
=={{header|LC3 Assembly}}==
<langsyntaxhighlight lang="lc3asm"> .ORIG 0x3000
 
AND R1,R1,0
Line 2,200 ⟶ 2,340:
LF .FILL 0x0A
 
.END</langsyntaxhighlight>
Output:
<pre>*
Line 2,207 ⟶ 2,347:
****
*****</pre>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
i is number
j is number
 
procedure:
for i from 1 to 6 step 1 do
for j from 0 to i step 1 do
display "*"
repeat
display lf
repeat
</syntaxhighlight>
{{out}}
<pre>
*
**
***
****
*****
</pre>
 
=={{header|LIL}}==
In LIL '''for''' takes a before loop code block for init, a conditional expression (true to enter loop step, false to exit loop), an after each loop step code block for value reassignment, followed by the code for the loop.
 
<langsyntaxhighlight lang="tcl">for {set i 1} {$i <= 5} {inc i} {
for {set j 1} {$j <= $i} {inc j} {
write "*"
}
print
}</langsyntaxhighlight>
 
{{out}}
Line 2,229 ⟶ 2,391:
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">repeat with i = 1 to 5
str = ""
repeat with j = 1 to i
Line 2,235 ⟶ 2,397:
end repeat
put str
end repeat</langsyntaxhighlight>
 
=={{header|Lisaac}}==
<langsyntaxhighlight Lisaaclang="lisaac">1.to 5 do { i : INTEGER;
1.to i do { dummy : INTEGER;
'*'.print;
};
'\n'.print;
};</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">put 0 into n
repeat for 5 times
add 1 to n
Line 2,253 ⟶ 2,415:
end repeat
put return
end repeat</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">for [i 1 5] [repeat :i [type "*] (print)]
repeat 5 [repeat repcount [type "*] (print)]</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">
for i=1,5 do
for j=1,i do
Line 2,267 ⟶ 2,429:
io.write("\n")
end
</syntaxhighlight>
</lang>
single loop
<langsyntaxhighlight lang="lua">for i = 1, 5 do
print(string.rep("*", i))
end</langsyntaxhighlight>
or
<langsyntaxhighlight lang="lua">for i = 1, 5 do
print(("*"):rep(i))
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
By default there For loops always perform on execution of block. If end value is smaller than fist value, then step adjust to that direction. When first value is equal to second value then if we declare step negative end value after execution of block became start value minus absolute step, or if step is positive, became start value plus step. We can use a switch for interpreter to change IF's STEP to act as BASIC's, and sign of step always used, and there is situations where block can't executed.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
For i=1 to 5
For j=1 to i
Line 2,295 ⟶ 2,457:
}
Print "End2"
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`for',
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
Line 2,306 ⟶ 2,468:
`for(`y',`1',x,`1',
`*')
')</langsyntaxhighlight>
 
=={{header|make}}==
{{works with|BSD make}}
{{libheader|jot}}
<langsyntaxhighlight lang="make">all: line-5
 
ILIST != jot 5
Line 2,336 ⟶ 2,498:
 
. endfor
.endfor</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">> for i to 5 do to i do printf( "*" ) end; printf( "\n" ) end;
*
**
***
****
*****</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">n=5;
<lang Mathematica>n=5;
For[i=1,i<=5,i++,
string="";
For[j=1,j<=i,j++,string=string<>"*"];
Print[string]
]</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">for i = (1:5)
output = [];
for j = (1:i)
Line 2,361 ⟶ 2,523:
end
disp(output);
end</langsyntaxhighlight>
 
Vectorized version:
 
<langsyntaxhighlight MATLABlang="matlab">for i = (1:5)
disp(repmat('*',1,i));
end</langsyntaxhighlight>
 
=={{header|Maxima}}==
 
<langsyntaxhighlight lang="maxima">for i thru 5 do (
s: "",
thru i do s: sconcat(s, "*"),
print(s)
);</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">for i in 1 to 5 do
(
line = ""
Line 2,386 ⟶ 2,548:
)
format "%\n" line
)</langsyntaxhighlight>
 
=={{header|Mercury}}==
<syntaxhighlight lang="text">:- module loops_for.
:- interface.
 
Line 2,410 ⟶ 2,572:
 
inner_loop_body(_, !IO) :-
io.write_char('*', !IO).</langsyntaxhighlight>
 
=={{header|MiniScript}}==
Literal interpretation of the task is somewhat complicated by the fact that the standard implementation of <code>print</code> in MiniScript adds a line break:
<langsyntaxhighlight MiniScriptlang="miniscript">for i in range(1,5)
s = ""
for j in range(1, i)
Line 2,420 ⟶ 2,582:
end for
print s
end for</langsyntaxhighlight>
 
{{out}}
Line 2,431 ⟶ 2,593:
However, it is worth noting that MiniScript's string replication operator (*) makes a more natural solution possible:
 
<langsyntaxhighlight MiniScriptlang="miniscript">for i in range(1,5)
print "*" * i
end for</langsyntaxhighlight>
 
(Output same as above.)
 
=={{header|MIPS Assembly}}==
Thanks to [https://www.chibialiens.com/mips/ ChibiAliens] for the header and footer as well as print routines.
<syntaxhighlight lang="mips">.include "\SrcAll\Header.asm"
.include "\SrcAll\BasicMacros.asm"
.include "\SrcPSX\MemoryMap.asm"
.include "\SrcN64\MemoryMap.asm"
CursorX equ 0x100
CursorY equ 0x101
main:
li t3,5+1 ;outer loop counter
li t2,1 ;inner loop counter
move a2,t2 ;working copy of inner loop counter
loop:
li a1,'*'
jal PrintChar
nop ;needed on PlayStation after branches to prevent out-of-order execution.
subiu a2,1
bnez a2,loop
nop
;overhead
jal NewLine ;this doesn't use t2 so we don't care about out-of-order execution.
addiu t2,1 ;increment outer loop counter
move a2,t2 ;next time, we'll print one more * than we did last time.
bne t2,t3,loop ;are we done yet? If not, loop.
nop
HALT:
j HALT ;halt the CPU - we're done
nop
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MyFont:
.ifdef buildn64
.incbin "\ResN64\ChibiAkumas.fnt"
.endif
.ifdef buildPSX
.incbin "\ResPSX\ChibiAkumas.fnt"
.endif
 
.include "\SrcALL\graphics.asm"
.include "..\\SrcAll\monitor.asm"
.include "\SrcN64\Footer.asm"</syntaxhighlight>
 
{{out}}
<pre>*
**
***
****
*****</pre>
[https://ibb.co/LdmTpwP Screenshot of Nintendo 64 emulator]
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE For;
IMPORT InOut;
 
Line 2,451 ⟶ 2,668:
InOut.WriteLn
END
END For.</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE Stars EXPORTS Main;
 
IMPORT IO;
Line 2,465 ⟶ 2,682:
IO.Put("\n");
END;
END Stars.</langsyntaxhighlight>
 
=={{header|MOO}}==
<langsyntaxhighlight lang="moo">for i in [1..5]
s = "";
for j in [1..i]
Line 2,474 ⟶ 2,691:
endfor
player:tell(s);
endfor</langsyntaxhighlight>
 
=={{header|Morfa}}==
<langsyntaxhighlight lang="morfa">
import morfa.base;
 
Line 2,488 ⟶ 2,705:
println("");
}
</syntaxhighlight>
</lang>
 
=={{header|MUMPS}}==
=== Routine ===
<langsyntaxhighlight MUMPSlang="mumps">FORLOOP
NEW I,J
FOR I=1:1:5 DO
Line 2,498 ⟶ 2,715:
..WRITE "*"
.WRITE !
QUIT</langsyntaxhighlight>
{{out}}
<pre>
Line 2,511 ⟶ 2,728:
=== One line ===
The if statement has to follow the write, or else the if statement would control the write (5 lines with one asterisk each).
<langsyntaxhighlight MUMPSlang="mumps">FOR I=1:1:5 FOR J=1:1:I WRITE "*" IF J=I W !</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang="nanoquery">for ($i = 1) ($i <= 5) ($i = $i+1)
for ($j = 0) ($j < $i) ($j = $j+1)
print "*"
end for
println
end for</langsyntaxhighlight>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">for (int i = 0; i < 5; i++)
{
for (int j = 0; j <= i; j++)
Line 2,529 ⟶ 2,746:
}
WriteLine();
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
Line 2,544 ⟶ 2,761:
say
end i_
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang NewLISP>
(for (i 1 5)
(for(j 1 i)
(print "*"))
(print "\n"))
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight Pythonlang="python">for i in 1..5:
for j in 1..i:
stdout.write("*")
echo("")</langsyntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASUChubasuc">10 FOR I=1 TO 5
20 FOR J=1 TO I
30 PRINT "*";
40 NEXT
50 PRINT
60 NEXT</langsyntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
for i in 1..5 {
for j in 1..$i {
print -n "*"
}
print ""
}
</syntaxhighlight>
 
=={{header|Oberon-2}}==
Works with oo2c Version 2
<langsyntaxhighlight lang="oberon2">
MODULE LoopFor;
IMPORT
Line 2,585 ⟶ 2,812:
END
END LoopFor.
</syntaxhighlight>
</lang>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
bundle Default {
class For {
Line 2,605 ⟶ 2,832:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">for i = 1 to 5 do
for j = 1 to i do
print_string "*"
done;
print_newline ()
done</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">for i = 0:1:4
for j = 0:1:i
printf("*");
endfor
printf("\n");
endfor</langsyntaxhighlight>
=={{header|Odin}}==
<syntaxhighlight lang="odin">
package main
 
import "core:fmt"
 
main :: proc() {
for i := 1 ; i <= 5 ; i += 1 {
for j := 1; j <= i; j += 1 {
fmt.printf("*")
}
fmt.println()
}
}
</syntaxhighlight>
 
=={{header|Oforth}}==
<langsyntaxhighlight Oforthlang="oforth">: loopFor(n)
| i j |
n loop: i [
i loop: j [ "*" print ]
printcr ;</langsyntaxhighlight>
 
=={{header|Onyx}}==
<langsyntaxhighlight lang="onyx">1 1 5 {dup {`*'} repeat bdup bpop ncat `\n' cat print} for flush</langsyntaxhighlight>
Using repeat inside the for loop instead of nesting another for loop is shorter and more efficient.
 
=={{header|Order}}==
<langsyntaxhighlight lang="c">#include <order/interpreter.h>
 
ORDER_PP(
Line 2,644 ⟶ 2,886:
8space)),
1, 6)
)</langsyntaxhighlight>
(Order cannot print newlines, so this example just uses a space.)
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">for I in 1..5 do
for _ in 1..I do
{System.printInfo "*"}
end
{System.showInfo ""}
end</langsyntaxhighlight>
Note: we don't use the inner loop variable, so we prefer not to give it a name.
 
=={{header|Panoramic}}==
<syntaxhighlight lang="panoramic">
<lang Panoramic>
dim x,y
 
Line 2,671 ⟶ 2,913:
 
next x
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">for(a=1,5,for(b=1,a,print1("*"));print())</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program stars(output);
 
var
Line 2,689 ⟶ 2,931:
writeln
end
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">for(my $x = 1; $x <= 5; $x++) {
for(my $y = 1; $y <= $x; $y++) {
print "*";
}
print "\n";
}</langsyntaxhighlight>
<langsyntaxhighlight lang="perl">foreach (1..5) {
foreach (1..$_) {
print '*';
}
print "\n";
}</langsyntaxhighlight>
 
However, if we lift the constraint of two loops the code will be simpler:
 
<langsyntaxhighlight lang="perl">print ('*' x $_ . "\n") for 1..5;</langsyntaxhighlight>
 
or equivalently
 
<langsyntaxhighlight lang="perl">map {print '*' x $_ . "\n"} 1..5;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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;">5</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
Line 2,721 ⟶ 2,963:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/For
by Galileo, 11/2022 #/
 
include ..\Utilitys.pmt
 
5 for
for
"*" print
endfor
nl
endfor
 
5 for '*' swap repeat ? endfor</syntaxhighlight>
{{out}}
<pre>*
**
***
****
*****
*
**
***
****
*****
 
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo '*';
}
echo "\n";
}</langsyntaxhighlight>
or
<langsyntaxhighlight lang="php">foreach (range(1, 5) as $i) {
foreach (range(1, $i) as $j) {
echo '*';
}
echo "\n";
}</langsyntaxhighlight>
or
<langsyntaxhighlight lang="php">foreach (range(1, 5) as $i)
echo str_repeat('*', $i) , PHP_EOL;</langsyntaxhighlight>
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
N = 5,
foreach(I in 1..N)
Line 2,749 ⟶ 3,019:
end,
nl
end.</langsyntaxhighlight>
 
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(for N 5
(do N (prin "*"))
(prinl) )</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">int main(){
for(int i = 1; i <= 5; i++){
for(int j=1; j <= i; j++){
Line 2,765 ⟶ 3,035:
write("\n");
}
}</langsyntaxhighlight>
 
=={{header|PILOT}}==
Core PILOT does not offer any way of printing without a newline, so in the inner loop we concatenate another star onto the string variable <code>$stars</code> each time round and then print it in the outer loop.
<langsyntaxhighlight lang="pilot">C :i = 1
*OuterLoop
C :j = 0
Line 2,780 ⟶ 3,050:
C :i = i + 1
J ( i < 6 ) :*OuterLoop
END:</langsyntaxhighlight>
 
=={{header|PL/I}}==
Basic version:
<langsyntaxhighlight PLlang="pl/Ii">do i = 1 to 5;
do j = 1 to i;
put edit ('*') (a);
end;
put skip;
end;</langsyntaxhighlight>
Advanced version:
<langsyntaxhighlight PLlang="pl/Ii">do i = 1 to 5;
put skip edit (('*' do j = 1 to i)) (a);
end;</langsyntaxhighlight>
Due to the new line requirement a mono line version is not possible
<langsyntaxhighlight PLlang="pl/Ii">put edit ((('*' do j = 1 to i)do i=1 to 5))(a); /* no new line */</langsyntaxhighlight>
 
=={{header|Plain English}}==
Plain English doesn't allow the direct nesting of loops. Instead, you are encouraged to make one routine for each loop and let the routine headers describe what the loops are doing.
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Write a triangle of asterisks on the console given 5.
Line 2,813 ⟶ 3,083:
If a counter is past the size, exit.
Write a row of asterisks on the console given the counter.
Repeat.</langsyntaxhighlight>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">lvars i, j;
for i from 1 to 5 do
for j from 1 to i do
Line 2,822 ⟶ 3,092:
endfor;
printf('\n')
endfor;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">for ($i = 1; $i -le 5; $i++) {
for ($j = 1; $j -le $i; $j++) {
Write-Host -NoNewline *
}
Write-Host
}</langsyntaxhighlight>
Alternatively the same can be achieved with a slightly different way by using the range operator along with the <code>ForEach-Object</code> cmdlet:
<langsyntaxhighlight lang="powershell">1..5 | ForEach-Object {
1..$_ | ForEach-Object {
Write-Host -NoNewline *
}
Write-Host
}</langsyntaxhighlight>
while the inner loop wouldn't strictly be necessary and can be replaced with simply <code>"*" * $_</code>.
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">size( 105,120 );
 
for ( int i=20; i<=100; i+=20 )
for ( int j=10; j<=i; j+=20 )
text( "*", j,i );</langsyntaxhighlight>
 
=={{header|Prolog}}==
Prolog has a built in iterator, between(Lo,Hi,I) which binds the value of I to successive values from Lo to Hi. This is the closest thing Prolog has to a 'for' loop.
<langsyntaxhighlight lang="prolog">example :-
between(1,5,I), nl, between(1,I,_J),
write('*'), fail.
example.</langsyntaxhighlight>
<pre>?- example.
 
Line 2,863 ⟶ 3,133:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import sys
for i in xrange(5):
for j in xrange(i+1):
sys.stdout.write("*")
print</langsyntaxhighlight>
Note that we have a constraint to use two for loops, which leads to non-idiomatic Python. If that constraint is dropped we can use the following, more idiomatic Python solution:
<langsyntaxhighlight lang="python">for i in range(1,6):
print '*' * i</langsyntaxhighlight>
or
<langsyntaxhighlight lang="python">print('\n'.join('*' * i for i in range(1, 6)))</langsyntaxhighlight>
 
=={{header|QB64}}==
''CBTJD'': 2020/03/14
<langsyntaxhighlight lang="qbasic">FOR c = 1 TO 5
FOR n = 1 TO c
PRINT "*";
NEXT
PRINT
NEXT</langsyntaxhighlight>
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery">5 times [ i^ 1+ times [ say "*" ] cr ]</langsyntaxhighlight>
{{Out}}
<pre>*
Line 2,894 ⟶ 3,164:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">for(i in 0:4) {
s <- ""
for(j in 0:i) {
Line 2,900 ⟶ 3,170:
}
print(s)
}</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">(for ([i (in-range 1 6)]) (for ([j i]) (display "*")) (newline))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 2,909 ⟶ 3,179:
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<syntaxhighlight lang="raku" perl6line>for ^5 {
 
for 0..$_ {
Line 2,917 ⟶ 3,187:
print "\n";
 
}</langsyntaxhighlight>
 
or using only one for loop:
 
<syntaxhighlight lang="raku" perl6line>say '*' x $_ for 1..5;</langsyntaxhighlight>
 
or without using any loops at all:
 
<syntaxhighlight lang="raku" perl6line>([\~] "*" xx 5).join("\n").say;</langsyntaxhighlight>
 
=={{header|Rapira}}==
<syntaxhighlight lang="rapira">for N from 1 to 5 do
output: "*" * N
od</syntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">; Use 'repeat' when an index required, 'loop' when repetition suffices:
 
repeat i 5 [
Line 2,940 ⟶ 3,215:
loop i [prin "*"]
print ""
]</langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
repeat i 5 [
loop i [prin "*"]
prin newline
]</langsyntaxhighlight>
 
=={{header|ReScript}}==
<langsyntaxhighlight ReScriptlang="rescript">let s = ref("")
for i in 1 to 5 {
for _ in 1 to i {
Line 2,958 ⟶ 3,233:
s := Js.String2.concat(s.contents, "\n")
}
Js.log(s.contents)</langsyntaxhighlight>
{{output}}
<pre>
Line 2,971 ⟶ 3,246:
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">6 [ 0; cr [ '* emit ] times ] iter</langsyntaxhighlight>
 
=={{header|REXX}}==
===using concatenation===
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates an outer DO loop controlling the inner DO loop with a "FOR".*/
 
do j=1 for 5 /*this is the same as: do j=1 to 5 */
Line 2,983 ⟶ 3,258:
end /*k*/
say $ /*display character string being built.*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|:}}
<pre>
Line 2,994 ⟶ 3,269:
 
===using abutment===
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates an outer DO loop controlling the inner DO loop with a "FOR".*/
 
do j=1 for 5 /*this is the same as: do j=1 to 5 */
Line 3,002 ⟶ 3,277:
end /*k*/
say $ /*display character string being built.*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
can be done in just one line:
<langsyntaxhighlight lang="ring">
for i = 1 to 5 for x = 1 to i see "*" next see nl next
</syntaxhighlight>
</lang>
or multiple line
<langsyntaxhighlight lang="ring">
for i = 1 to 5
for x = 1 to i
Line 3,018 ⟶ 3,293:
see nl
next
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
RPL provides two types of counting loops: <code>FOR</code>..<code>NEXT</code> and <code>START</code>..<code>NEXT</code>, the latter looping without providing access to its counter.
≪ 1 5 '''FOR''' j
""
1 j '''START'''
"*" +
'''NEXT
NEXT'''
'LOOPS' STO
=={{header|Ruby}}==
One can write a <tt>for</tt> loop as <tt>for i in 1..5; ...end</tt> or as <tt>for i in 1..5 do ... end</tt> or as <tt>(1..5).each do |i| ... end</tt>. All three forms call <tt>Range#each</tt> to iterate <tt>1..5</tt>.
Line 3,028 ⟶ 3,313:
|-
| style="vertical-align: top;" |
<langsyntaxhighlight lang="ruby">for i in 1..5
for j in 1..i
print "*"
end
puts
end</langsyntaxhighlight>
| style="vertical-align: top;" |
<langsyntaxhighlight lang="ruby">(1..5).each do |i|
(1..i).each do |j|
print "*"
end
puts
end</langsyntaxhighlight>
|}
 
Line 3,051 ⟶ 3,336:
|-
| style="vertical-align: top;" |
<langsyntaxhighlight lang="ruby">1.upto(5) do |i|
1.upto(i) do |j|
print "*"
end
puts
end</langsyntaxhighlight>
| style="vertical-align: top;" |
<langsyntaxhighlight lang="ruby">5.times do |i|
# i goes from 0 to 4
(i+1).times do
Line 3,064 ⟶ 3,349:
end
puts
end</langsyntaxhighlight>
| style="vertical-align: top;" |
<langsyntaxhighlight lang="ruby">i = 1
loop do
j = 1
Line 3,075 ⟶ 3,360:
puts
break if (i += 1) > 5
end</langsyntaxhighlight>
|}
 
Or we can use String#* as the inner loop, and Enumerable#map as the outer loop. This shrinks the program to one line.
 
<langsyntaxhighlight lang="ruby">puts (1..5).map { |i| "*" * i }</langsyntaxhighlight>
 
=={{header|Rust}}==
The compiler warns when you create an unused variable; here we use _ to avoid this effect.
<langsyntaxhighlight lang="rust">fn main() {
for i in 0..5 {
for _ in 0..=i {
Line 3,092 ⟶ 3,377:
println!();
}
}</langsyntaxhighlight>
 
=={{header|Salmon}}==
<langsyntaxhighlight Salmonlang="salmon">iterate (x; [0...4])
{
iterate (y; [0...x])
print("*");;
print("\n");
};</langsyntaxhighlight>
 
or
 
<langsyntaxhighlight Salmonlang="salmon">for (x; 0; x < 5)
{
for (y; 0; y <= x)
print("*");;
print("\n");
};</langsyntaxhighlight>
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sas">data _null_;
length a $5;
do n=1 to 5;
Line 3,132 ⟶ 3,417:
put a;
end;
run;</langsyntaxhighlight>
 
=={{header|Sather}}==
Line 3,138 ⟶ 3,423:
Sather allows the definition of new iterators. Here's we define <code>for!</code> so that it resembles the known <code>for</code> in other languages, even though the <code>upto!</code> built-in can be used.
 
<langsyntaxhighlight lang="sather">class MAIN is
-- from, to, step
for!(once init:INT, once to:INT, once inc:INT):INT is
Line 3,158 ⟶ 3,443:
end;
end;
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">for (i <- 1 to 5) {
for (j <- 1 to i)
print("*")
println()
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(do ((i 1 (+ i 1)))
((> i 5))
(do ((j 1 (+ j 1)))
((> j i))
(display "*"))
(newline))</langsyntaxhighlight>
 
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">for i=1:5
s=""
for j=1:i
Line 3,183 ⟶ 3,468:
end
printf("%s\n",s)
end</langsyntaxhighlight>
{{out}}
<pre>*
Line 3,192 ⟶ 3,477:
 
=={{header|Seed7}}==
<syntaxhighlight lang="python">
<lang seed7>for I range 1 to 5 do
$ include "seed7_05.s7i";
for J range 1 to I do
 
write("*");
const proc: main is func
end for;
local
writeln;
var integer: I is 1;
end for;</lang>
var integer: J is 1;
 
begin
for I range 1 to 5 do
for J range 1 to I do
write("*");
end for;
writeln;
end for;
end func;
</syntaxhighlight>
{{out}}
<pre>
*
**
***
****
*****
</pre>
 
=={{header|SETL}}==
<langsyntaxhighlight lang="ada">for i in {1..5} loop
for j in {1..i} loop
nprint( '*' );
end loop;
print; -- new line
end loop;</langsyntaxhighlight>
 
=={{header|Sidef}}==
'''for(;;)''' loop:
<langsyntaxhighlight lang="ruby">for (var i = 1; i <= 5; i++) {
for (var j = 1; j <= i; j++) {
print '*'
}
print "\n"
}</langsyntaxhighlight>
 
'''for([])''' loop:
<langsyntaxhighlight lang="ruby">for (1..5) { |i|
for (1..i) { print '*' }
print "\n"
}</langsyntaxhighlight>
 
'''for-in''' loop:
<langsyntaxhighlight lang="ruby">for i in (1..5) {
for j in (1..i) { print '*' }
print "\n"
}</langsyntaxhighlight>
 
Idiomatic:
<langsyntaxhighlight lang="ruby">5.times { |i|
i.times { print '*' }
print "\n"
}</langsyntaxhighlight>
 
=={{header|Simula}}==
{{works with|SIMULA-67}}
<langsyntaxhighlight lang="simula">begin
integer i,j;
for i:=1 step 1 until 5 do
Line 3,245 ⟶ 3,549:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">1 to: 5 do: [| :n | inform: ($* repeatedTimes: n)].</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">1 to: 5 do: [ :aNumber |
aNumber timesRepeat: [ '*' display ].
Character nl display.
]</langsyntaxhighlight>
or:
<langsyntaxhighlight lang="smalltalk">1 to: 5 do: [ :row |
1 to: row do: [:col | '*' display ].
]</langsyntaxhighlight>
(only for demonstration of nested for-loops; as the column is not needed, the first solution is probably clearer).
 
However, streams already have some builtin repetition mechanism, so a programmer might write:
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">1 to: 5 do: [ :n |
Stdout next: n put: $*; cr
]</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
A slightly longer, "mundane" version
 
<langsyntaxhighlight lang="snobol">ol outer = ?lt(outer,5) outer + 1 :f(end)
inner = outer; stars = ""
il stars = ?gt(inner,0) stars "*" :f(disp)
inner = inner - 1 :(il)
disp output = stars; :(ol)
end</langsyntaxhighlight>
 
The "real SNOBOL4" starts here:
<langsyntaxhighlight lang="snobol">outer b = a = ?lt(a,5) a + 1 :f(end)
inner t = t ?(b = (gt(b,0) b - 1)) "*" :s(inner)
t span("*") . terminal = :(outer)
end</langsyntaxhighlight>
 
one "loop" only:
<langsyntaxhighlight lang="snobol"> a = "*****";
a a len(x = x + 1) . output :s(a)
end</langsyntaxhighlight>
 
... or just (courtesy of GEP2):
{{works with|SNOBOL4|which defaults to anchored mode}}
<langsyntaxhighlight lang="snobol"> "*****" arb $ output fail
end</langsyntaxhighlight>
 
=={{header|SNUSP}}==
Line 3,326 ⟶ 3,630:
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">for (var row = 1; row <= 5; row++) {
for (var col = 1; col <= row; col++) {
printf("*");
Line 3,332 ⟶ 3,636:
 
print();
}</langsyntaxhighlight>
 
=={{header|Spin}}==
Line 3,339 ⟶ 3,643:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<langsyntaxhighlight lang="spin">con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 3,356 ⟶ 3,660:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</langsyntaxhighlight>
{{out}}
<pre>
Line 3,367 ⟶ 3,671:
 
=={{header|SPL}}==
<langsyntaxhighlight lang="spl">> i, 1..5
> j, 1..i
#.output("*",#.rs)
<
#.output()
<</langsyntaxhighlight>
 
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">forvalues n=1/5 {
local s ""
forvalues i=1/`n' {
Line 3,382 ⟶ 3,686:
}
display "`s'"
}</langsyntaxhighlight>
 
=== Mata ===
<langsyntaxhighlight lang="stata">for (i=1; i<=5; i++) {
for (j=1; j<=i; j++) printf("*")
printf("\n")
}</langsyntaxhighlight>
 
=={{header|Suneido}}==
<langsyntaxhighlight Suneidolang="suneido">for(i = 0; i < 5; ++i)
{
str = ''
Line 3,397 ⟶ 3,701:
str $= '*'
Print(str)
}</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">for i in 1...5 {
for _ in 1...i {
print("*", terminator: "")
}
print()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,417 ⟶ 3,721:
=={{header|Tailspin}}==
Tailspin uses streams of values within streams of values rather than loops.
<langsyntaxhighlight lang="tailspin">
1..5 -> '$:1..$ -> '*';
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,431 ⟶ 3,735:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">for {set lines 1} {$lines <= 5} {incr lines} {
for {set i 1} {$i <= $lines} {incr i} {
puts -nonewline "*"
}
puts ""
}</langsyntaxhighlight>
Note that it would be more normal to produce this output with:
<langsyntaxhighlight lang="tcl">for {set i 1} {$i <= 5} {incr i} {
puts [string repeat "*" $i]
}</langsyntaxhighlight>
 
It bears noting that the three parts of the for loop do not have to consist of "initialize variable", "test value of variable" and "increment variable". This is a common way to think of it as it resembles the "for" loop in other languages, but many other things make sense. For example this for-loop will read a file line-by-line:
 
<langsyntaxhighlight lang="tcl">set line ""
for { set io [open test.txt r] } { ![eof $io] } { gets $io line } {
if { $line != "" } { ...do something here... }
}</langsyntaxhighlight>
 
(This is a somewhat awkward example; just to show what is possible)
Line 3,462 ⟶ 3,766:
 
=={{header|TI-89 BASIC}}==
<langsyntaxhighlight lang="ti89b">Local i,j
ClrIO
For i, 1, 5
Line 3,468 ⟶ 3,772:
Output i*8, j*6, "*"
EndFor
EndFor</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
 
<langsyntaxhighlight Torquelang="torque">for(%i = 0; %i < 5; %i++)
{
for(%x = %i; %x < 5; %x++)
Line 3,479 ⟶ 3,783:
echo(%string);
}
}</langsyntaxhighlight>
 
=={{header|TransFORTH}}==
<langsyntaxhighlight lang="forth">: PRINTSTARS ( ROWS -- )
1 + 1 DO
I 0 DO
PRINT " * " LOOP
CR LOOP ;
5 PRINTSTARS</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
m=""
Line 3,497 ⟶ 3,801:
PRINT m
ENDLOOP
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,508 ⟶ 3,812:
 
=={{header|TypeScript}}==
<langsyntaxhighlight JavaScriptlang="javascript">for (let i: number = 0; i < 5; ++i) {
let line: string = ""
for(let j: number = 0; j <= i; ++j) {
Line 3,515 ⟶ 3,819:
console.log(line)
}
</syntaxhighlight>
</lang>
 
=={{header|UNIX Shell}}==
Line 3,521 ⟶ 3,825:
 
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">#!/bin/sh
# Using a while control construct to emulate a for loop
 
Line 3,535 ⟶ 3,839:
echo
l=`expr "$l" + 1` # Increment the outer counter
done</langsyntaxhighlight>
 
The [[Bourne Shell]] has a for loop, but it requires a list of words to iterate.
Line 3,542 ⟶ 3,846:
{{works with|Bourne Shell}}
{{libheader|jot}}
<langsyntaxhighlight lang="bash">for i in `jot 5`; do
for j in `jot $i`; do
printf \*
done
echo
done</langsyntaxhighlight>
 
Bash has <tt>for</tt> loops that act like C. These loops are very good for this task.
 
{{works with|Bourne Again SHell|3}}
<langsyntaxhighlight lang="bash">for (( x=1; $x<=5; x=$x+1 )); do
for (( y=1; y<=$x; y=$y+1 )); do
echo -n '*'
done
echo ""
done</langsyntaxhighlight>
 
==={{header|C Shell}}===
{{libheader|jot}}
<langsyntaxhighlight lang="csh">foreach i (`jot 5`)
foreach j (`jot $i`)
echo -n \*
end
echo ""
end</langsyntaxhighlight>
 
==={{header|Korn Shellksh}}===
{{works with|Korn Shell 93ksh93}}
<langsyntaxhighlight bashlang="ksh">for ((x = 1; x <= 5; x=x += 1)); do
do
for ((y=1; y<=x; y=y+1)); do
for ((y = 0; y < x; y += 1))
echo -n '*'
do
done
print -n '*'
echo ""
done </lang>
print
done</syntaxhighlight>
 
=={{header|UnixPipes}}==
<langsyntaxhighlight lang="bash">yes \ | cat -n | (while read n ; do
[ $n -gt 5 ] && exit 0;
yes \* | head -n $n | xargs -n $n echo
done)</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">#
# for loop
#
Line 3,594 ⟶ 3,900:
end for
out endl console
end for</langsyntaxhighlight>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">int main (string[] args) {
for (var i = 1; i <= 5; i++) {
for (var j = 1; j <= i; j++) {
Line 3,605 ⟶ 3,911:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Option Explicit
Sub LoopEx()
Dim i As Long, j As Long, s As String
Line 3,618 ⟶ 3,924:
Debug.Print s
Next
End Sub</langsyntaxhighlight>
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Option Explicit
Dim i, j, s
For i = 1 To 5
Line 3,629 ⟶ 3,935:
Next
WScript.Echo s
Next</langsyntaxhighlight>
 
=={{header|Vedit macro language}}==
<langsyntaxhighlight lang="vedit">for (#1 = 1; #1 <= 5; #1++) {
for (#2 = 1; #2 <= #1; #2++) {
Type_Char('*')
}
Type_Newline
}</langsyntaxhighlight>
 
 
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
<lang Verilog>
module main;
integer i, j;
Line 3,654 ⟶ 3,960:
end
endmodule
</syntaxhighlight>
</lang>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
for i in 1..6 {
for _ in 1..i+1 {
Line 3,664 ⟶ 3,970:
print("\n")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,675 ⟶ 3,981:
 
=={{header|Wart}}==
<langsyntaxhighlight lang="wart">for i 1 (i <= 5) ++i
for j 0 (j < i) ++j
pr "*"
(prn)</langsyntaxhighlight>
 
=={{header|Wee Basic}}==
print 1 "" ensures the end of program text is separate from the asterisk characters.
<langsyntaxhighlight Weelang="wee Basicbasic">for y=0 to 4
print 1 ""
for x=0 to y
Line 3,688 ⟶ 3,994:
next
next
end</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">for (i in 1..5) {
for (j in 1..i) System.write("*")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 3,707 ⟶ 4,013:
=={{header|x86 Assembly}}==
This subroutine uses only the original 16-bit 8086 instruction set; it is written for DOS, but could be adapted to run under other operating systems.
<langsyntaxhighlight lang="x86asm">loops: mov bx, 1 ; outer loop counter
 
outerloop: mov cx, bx ; inner loop counter
Line 3,727 ⟶ 4,033:
jne outerloop
 
ret</langsyntaxhighlight>
 
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<langsyntaxhighlight lang="xbasic">
PROGRAM "for"
 
Line 3,745 ⟶ 4,051:
END FUNCTION
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|XBS}}==
<langsyntaxhighlight lang="xbs">const repChar:string = "*";
const maxIters = 5;
for(i=1;maxIters;1){
Line 3,756 ⟶ 4,062:
}
log(str);
}</langsyntaxhighlight>
{{out}}
<pre>*
Line 3,766 ⟶ 4,072:
=={{header|XLISP}}==
The equivalent of other languages' <tt>FOR</tt> or <tt>DO</tt> loops can be written using <tt>DO</tt>:
<langsyntaxhighlight lang="xlisp">(DO ((I 1 (+ I 1))) ((> I 5))
(DO ((J 0 (+ J 1))) ((= J I))
(DISPLAY "*"))
(NEWLINE))</langsyntaxhighlight>
{{out}}
<pre>*
Line 3,779 ⟶ 4,085:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code ChOut=8, CrLf=9;
int I, J;
for I:= 1 to 5 do
Line 3,785 ⟶ 4,091:
ChOut(0, ^*);
CrLf(0);
]</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
For the Amstrad CPC (should work with e.g. the built-in assembler in JavaCPC; use <tt>call &4000</tt> to start from BASIC):
<langsyntaxhighlight lang="z80">org &4000 ; put code at memory address 0x4000
wr_char equ &bb5a ; write ASCII character in register A to screen
; (jumps into CPC ROM)
Line 3,829 ⟶ 4,135:
pop de
pop bc
ret ; return to BASIC interpreter</langsyntaxhighlight>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
 
pub fn main() !void {
const stdout_wr = std.io.getStdOut().writer();
for (1..6) |n| {
for (0..n) |_| {
try stdout_wr.writeAll("*");
}
try stdout_wr.writeAll("\n");
}
}
</syntaxhighlight>
{{out}}
<pre>
*
**
***
****
*****
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">foreach i in ([1..5]){
foreach j in (i){print("*")}
println();
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,844 ⟶ 4,173:
*****
</pre>
 
=={{header|Zig}}==
<lang zig>const std = @import("std");
 
pub fn main() !void {
const stdout_wr = std.io.getStdOut().writer();
var i: u8 = 1;
while (i < 5) : (i += 1) {
var j: u8 = 1;
while (j <= i) : (j += 1)
try stdout_wr.writeAll("*");
try stdout_wr.writeAll("\n");
}
}</lang>
19

edits