Loops/For: Difference between revisions
Zig sorted alphabetically before zkn
m (→{{header|APL}}) |
(Zig sorted alphabetically before zkn) |
||
(27 intermediate revisions by 22 users not shown) | |||
Line 45:
{{trans|Python}}
<
L 1..i
print(‘*’, end' ‘’)
print()</
Pythonic solution:
<
print(‘*’ * i)</
=={{header|360 Assembly}}==
;Basic - Algol style
The opcode BXH uses 3 registers, one for index, one for step and one for limit.
<
* Loops/For - BXH Algol 27/07/2015
LOOPFOR CSECT
Line 80:
YREGS
END LOOPFOR
</syntaxhighlight>
{{out}}
<pre>
Line 91:
;Structured Macros
Structured and without BXH, only one register used by loop.
<
* Loops/For - struct 29/06/2016
LOOPFOR CSECT
Line 112:
YREGS
END LOOPFOR
</
{{out}}
Same as above
Line 118:
=={{header|68000 Assembly}}==
Although it's more natural for the language to loop downward rather than forward, both are possible.
<
MOVEQ #1,D1 ;counter for how many times to print *, this is also the loop counter
.outerloop:
Line 131:
CMP.W #6,D1 ;are we done yet?
BCS .outerloop ;if not, go back to the top
RTS</
=={{header|8086 Assembly}}==
The following works with MS-DOS. Called as a subroutine (i.e. "call StarSub")
<
mov ah,02h ;needed to prime the interrupt command for printing to screen
Line 158:
jnz outer_loop
ret</
=={{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.
<
( ( '* putc ) swap times cr ) 1 5 loop
</syntaxhighlight>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loop64.s */
Line 222:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<
byte I,J
Line 237:
Od
Return</
=={{header|ActionScript}}==
<
for (var i:int = 1; i <= 5; i++) {
for (var j:int = 1; j <= i; j++)
Line 246:
trace(str);
str = "";
}</
=={{header|Ada}}==
<
for J in 1..I loop
Put("*");
end loop;
New_Line;
end loop;</
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<
for j to i do
write( "*" )
od;
print()
od</
=={{header|ALGOL 60}}==
<
FOR I:=1 STEP 1 UNTIL 5 DO
BEGIN
Line 273:
OUTLINE
END
</syntaxhighlight>
=={{header|ALGOL 68}}==
Line 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]}}
<
TO i DO
print("*")
OD;
print(new line)
OD</
{{out}}
<pre>
Line 298:
=={{header|ALGOL W}}==
In Algol W, write starts a new line, writeon continues it.
<
for i := 1 until 5 do
begin
Line 307:
end j
end i
end.</
=={{header|ALGOL-M}}==
<
INTEGER I, J;
FOR I := 1 STEP 1 UNTIL 5 DO
Line 318:
WRITEON( "*" );
END;
END</
{{out}}
<pre>*
Line 328:
=={{header|Alore}}==
<
for j in 0 to i
Write('*')
Line 334:
WriteLn()
end
</syntaxhighlight>
=={{header|AmigaE}}==
<
DEF i, j
FOR i := 1 TO 5
Line 343:
WriteF('\n')
ENDFOR
ENDPROC</
=={{header|Apex}}==
<
String line = '';
Line 366:
for (String line : lines) {
System.debug(line);
}</
=={{header|APL}}==
For 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''':
<
To stick to the letter of the task description, we can nest an '''each''' inside another one, but it's a little silly. Plus not all dialects support niladic dfns, so the innermost "return one star" function has to take a dummy argument:
<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}}
<
vec ← ⍬
:for i :in ⍳ count
Line 389:
:endfor
result ← count 1 ⍴ vec
∇</
{{Out}}
The result of all three implementations of
<pre> stars 5
Line 403:
=={{header|AppleScript}}==
<
repeat with i from 1 to 5
repeat with j from 1 to i
Line 410:
set x to x & linefeed
end repeat
return x</
{{out}}
<pre>*
Line 420:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program loop1.s */
Line 488:
bx lr /* return */
</syntaxhighlight>
=={{header|Arturo}}==
<
loop 0..x 'y [
prints "*"
]
print ""
]</
{{out}}
<pre>*
Line 507:
=={{header|Asymptote}}==
Asymptote's control structures are similar to those in C, C++, or Java
<
for(int j = 0; j < i; ++j) {
write("*", suffix=none);
}
write("");
}</
=={{header|AutoHotkey}}==
<
Gui, Show ; Show the window
Loop, 5 ; loop 5 times
Line 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</
=={{header|Avail}}==
<
[
For each length from 1 to row do [Print: "*";];
Print: "\n";
];</
Since the inner loop index is unneeded, it may be more natural to use the <code>From_to_do_</code> loop format:
<
[
From 1 to row do [Print: "*";];
Print: "\n";
];</
=={{header|AWK}}==
<
for(i=1; i < 6; i++) {
for(j=1; j <= i; j++ ) {
Line 551:
print
}
}</
=={{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.
<
For(I,1,5)
For(J,1,I)
Output(J,I,"*")
End
End</
=={{header|Babel}}==
<
(star_triangle {
Line 574:
"\n" << }
->
times }))</
{{out}}
Line 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}}==
<
for i in {1..5}
do
Line 601 ⟶ 616:
echo
done
</syntaxhighlight>
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
<
for j = 1 to i
print "*";
next j
print
next i</
==={{header|Applesoft BASIC}}===
<
==={{header|BASIC256}}===
<
for j = 1 to i
print "*";
Line 622 ⟶ 637:
print
next i
end</
==={{header|BaCon}}===
<
FOR i = 1 TO 5
FOR j = 1 TO i
Line 632 ⟶ 647:
PRINT
NEXT
</syntaxhighlight>
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="text">
FOR I% = 1 TO 5
FOR J% = 1 TO I%
Line 643 ⟶ 658:
PRINT
NEXT
</syntaxhighlight>
==={{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}}===
<
20 : FOR J = 1 TO I
30 : PRINT "*";
40 : NEXT J
50 : PRINT
60 NEXT I</
==={{header|Creative Basic}}===
<syntaxhighlight lang="creative basic">
OPENCONSOLE
Line 676 ⟶ 700:
END
</syntaxhighlight>
==={{header|GW-BASIC}}===
<
20 FOR J = 1 TO I
30 PRINT "*";
Line 685 ⟶ 709:
50 PRINT
60 NEXT I
</syntaxhighlight>
==={{header|FBSL}}===
<
#APPTYPE CONSOLE
FOR dim i = 1 TO 5
Line 696 ⟶ 720:
PRINT
NEXT i
Pause</
{{out}}
<pre>
Line 707 ⟶ 731:
==={{header|FUZE BASIC}}===
<
FOR k = 1 to n CYCLE
print "*";
Line 713 ⟶ 737:
PRINT
REPEAT
END</
==={{header|IS-BASIC}}===
<
110 FOR J=1 TO I
120 PRINT "*";
130 NEXT
140 PRINT
150 NEXT</
==={{header|IWBASIC}}===
<syntaxhighlight lang="iwbasic">
OPENCONSOLE
Line 744 ⟶ 768:
'Could also have been written the same way as the Creative Basic example, with no LOCATE command.
</syntaxhighlight>
==={{header|Liberty BASIC}}===
Unlike some BASICs, Liberty BASIC does not require that the counter variable be specified with 'next'.
<
for j = 1 to i
print "*";
next
print
next</syntaxhighlight>
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|Microsoft Small Basic}}===
<
For j = 1 To i
TextWindow.Write("*")
EndFor
TextWindow.WriteLine("")
EndFor</
==={{header|PureBasic}}===
<
Define i, j
For i=1 To 5
Line 776 ⟶ 801:
Print(#LFCR$+"Press ENTER to quit"): Input()
CloseConsole()
EndIf</
==={{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}}===
<
FOR j = 1 TO i
PRINT "*";
NEXT j
PRINT
NEXT i</syntaxhighlight>
==={{header|smart BASIC}}===
While some versions of BASIC allow for NEXT without a variable, smart BASIC requires the variable designation.
<
for m = 1 to n
print "*";
next m
print
next n</
==={{header|True BASIC}}===
<syntaxhighlight lang="basic">FOR i = 1 TO 5
FOR j = 1 TO i
PRINT "*";
Line 807 ⟶ 836:
PRINT
NEXT i
END</syntaxhighlight>
==={{header|Visual Basic}}===
'''Works with:''' VB6
<
For i = 0 To 4
For j = 0 To i
Line 819 ⟶ 846:
Next j
OutConsole.WriteLine
Next i</
==={{header|Visual Basic .NET}}===
{{works with|Visual Basic .Net 2002}}
<
For y As Integer = 0 To x
Console.Write("*")
Next
Console.WriteLine()
Next</
==={{header|Yabasic}}===
<
for j = 1 to i
print "*";
Line 837 ⟶ 864:
print
next i
end</
==={{header|ZX Spectrum Basic}}===
On the ZX Spectrum, we need line numbers:
<syntaxhighlight 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>
=={{header|Batch File}}==
<syntaxhighlight lang="text">@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
Line 865 ⟶ 888:
)
ENDLOCAL</
=={{header|bc}}==
<
for (j = 1; j <= i; j++) "*"
"
"
}
quit</
=={{header|BCPL}}==
<
let start() be
Line 882 ⟶ 905:
$( for j = 1 to i do wrch('**')
wrch('*N')
$)</
=={{header|Befunge}}==
<
| :-1<
^+1,+5+5<</
=={{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}}==
<
line = ""
for (j = 1; j <= i; j++)
Line 896 ⟶ 925:
end
print(line)
end</
=={{header|Bracmat}}==
<
& whl
' ( !i+1:~>5:?i
Line 907 ⟶ 936:
)
&
);</
=={{header|Brainf***}}==
<
+++++[>++[>>+<<-]<-]<< place \n in cell 4
+++++[ set outer loop count
Line 917 ⟶ 946:
>[>>.<<-]>>>.<<< print line
<<-] end inner loop
] end outer loop</
=={{header|Brat}}==
<
1.to i, { j |
print "*"
}
print "\n"
}</
=={{header|C}}==
<
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
putchar('*');
puts("");
}</
=={{header|C sharp|C#}}==
<
class Program {
Line 950 ⟶ 979:
}
}
}</
=={{header|C++}}==
<
for(int i = 0; i < 5; ++i) {
for(int j = 0; j < i; ++j)
Line 959 ⟶ 988:
std::cout.put('\n');
}</
=={{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}}==
<
for(i in 1..5) {
Line 970 ⟶ 1,007:
print("");
}
}</
=={{header|Chapel}}==
<
for 1..i do write('*');
writeln();
}</
=={{header|Chef}}==
<
This recipe prints a triangle of asterisks.
Line 1,008 ⟶ 1,045:
Pour contents of the mixing bowl into the baking dish.
Serves 1.</
=={{header|Clojure}}==
<
(print "*")
(if (= i j) (println)))</
=={{header|CLU}}==
<
po: stream := stream$primary_output()
Line 1,026 ⟶ 1,063:
stream$putl(po, "")
end
end start_up</
=={{header|COBOL}}==
<
PROGRAM-ID. Display-Triangle.
Line 1,050 ⟶ 1,087:
GOBACK
.
</syntaxhighlight>
=={{header|ColdFusion}}==
Line 1,056 ⟶ 1,093:
With tags:
<
<cfloop index = "j" from = "1" to = "#i#">
*
</cfloop>
< br />
</cfloop></
With script:
<
for( i = 1; i <= 5; i++ )
{
Line 1,072 ⟶ 1,109:
writeOutput( "< br />" );
}
</cfscript></
=={{header|Common Lisp}}==
<
(loop for j from 1 upto i do
(write-char #\*))
(terpri))</
or
<
(dotimes (j (+ i 1))
(write-char #\*))
(terpri))</
or
<
((> i 5))
(do ((j 1 (+ j 1)))
((> j i))
(write-char #\*))
(terpri))</
or
<
(iter
(for i from 1 to 5)
Line 1,098 ⟶ 1,135:
(for j from 1 to i)
(princ #\*))
(terpri))</
=={{header|Coq}}==
<
Variable T : Type.
Variable body : nat -> T -> T.
Line 1,125 ⟶ 1,162:
)
0 5 nil.
</syntaxhighlight>
=={{header|Cowgol}}==
<
var i: uint8 := 1;
Line 1,139 ⟶ 1,176:
print_nl();
i := i + 1;
end loop;</
=={{header|Crystal}}==
<
1.upto(5) do |i|
1.upto(i) do |j|
Line 1,150 ⟶ 1,187:
puts
end
</syntaxhighlight>
Or another way, more succinctly put:
<
puts (1..5).map { |i| "*" * i }.join("\n")
</syntaxhighlight>
=={{header|D}}==
<
void main() {
Line 1,174 ⟶ 1,211:
writeln();
}
}</
{{out}}
<pre>*
Line 1,189 ⟶ 1,226:
=={{header|Dao}}==
<
for( j = 1 : i ) io.write( '*' )
io.writeln()
}</
=={{header|Dart}}==
<
for (var i = 0; i < 5; i++)
for (var j = 0; j < i + 1; j++)
print("*");
print("\n");
}</
=={{header|dc}}==
Line 1,206 ⟶ 1,243:
{{trans|bc}}
<
[*]P [print asterisk]sz
lj 1 + d sj [increment j, leave it on stack]sz
Line 1,220 ⟶ 1,257:
]sB
1 d si [i = 1, leave it on stack]sz
5 !<B [enter loop B if 5 >= i]sz</
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 1,236 ⟶ 1,273:
Writeln;
end;
end.</
=={{header|Diego}}==
<
add_var({str},s)_value();
Line 1,254 ⟶ 1,291:
me_msg([output]);
reset_ns[];</
=={{header|DMS}}==
<
for (i = 1; i <= 5; i++)
{
Line 1,265 ⟶ 1,302:
}
Result( "\n" )
}</
=={{header|dodo0}}==
<
(
test(var) -> continue
Line 1,301 ⟶ 1,338:
)
| result
exit()</
=={{header|Draco}}==
<
byte i,j;
for i from 1 upto 5 do
Line 1,312 ⟶ 1,349:
writeln()
od
corp</
{{out}}
<pre>*
Line 1,321 ⟶ 1,358:
=={{header|Dragon}}==
<
for (j = 0, j <= i, j++) {
show "*"
}
showln ""
}</
=={{header|DWScript}}==
<
for i := 1 to 5 do begin
Line 1,335 ⟶ 1,372:
Print('*');
PrintLn('');
end;</
=={{header|Dyalect}}==
{{trans|Swift}}
<
for _ in 1..i {
print("*", terminator: "")
}
print()
}</
Output:
Line 1,357 ⟶ 1,394:
=={{header|E}}==
<
for _ in 1..width {
print("*")
}
println()
}</
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,368 ⟶ 1,405:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
for i
for j = 1 to i
write "*"
.
print ""
.
</syntaxhighlight>
=={{header|EDSAC order code}}==
Line 1,381 ⟶ 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.
<
=====
Line 1,430 ⟶ 1,469:
[ 24 ] P0F [ used to clear a ]
EZPF [ begin execution ]</
{{out}}
<pre>+
Line 1,439 ⟶ 1,478:
=={{header|EGL}}==
<
for ( i int to 5 )
str = "";
Line 1,446 ⟶ 1,485:
end
SysLib.writeStdout(str);
end</
=={{header|Ela}}==
<
loop m n | n < m = do
Line 1,462 ⟶ 1,501:
| else = do return ()
_ = loop 10 1 ::: IO</
Output:
<
***
****
Line 1,475 ⟶ 1,514:
********
*********
**********</
=={{header|Elena}}==
ELENA
<
public program()
{
for(int i := 0
{
for(int j := 0
{ console.write
console.writeLine()
}
}</
{{out}}
<pre>
Line 1,501 ⟶ 1,540:
=={{header|Elixir}}==
<
def loops_for(n) do
Enum.each(1..n, fn i ->
Line 1,510 ⟶ 1,549:
end
Loops.loops_for(5)</
one line (Comprehensions)
<
=={{header|Emacs Lisp}}==
<
;; (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
Line 1,526 ⟶ 1,565:
(< i 5))
(setq str (concat str "*"))
(message str) ) )</
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>
*
Line 1,539 ⟶ 1,594:
=={{header|Erlang}}==
<
-module(nested_loops).
-export([main/0, inner_loop/0]).
Line 1,563 ⟶ 1,618:
io:format("~n"),
inner_loop(N).
</syntaxhighlight>
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
FOR I=1 TO 5 DO
FOR J=1 TO I DO
Line 1,573 ⟶ 1,628:
PRINT
END FOR
</syntaxhighlight>
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
for i = 1 to 5 do
for j = 1 to i do
Line 1,584 ⟶ 1,639:
puts(1, "\n") -- Same as "puts(1, {'\n'})"
end for
</syntaxhighlight>
<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,591 ⟶ 1,646:
=={{header|F_Sharp|F#}}==
<
[<EntryPoint>]
let main args =
Line 1,598 ⟶ 1,653:
printf "*"
printfn ""
0</
=={{header|Factor}}==
<
=={{header|FALSE}}==
<
"1+]#%</
=={{header|Fantom}}==
Line 1,611 ⟶ 1,666:
Using for loops:
<
class ForLoops
{
Line 1,626 ⟶ 1,681:
}
}
</syntaxhighlight>
Using range objects:
<
class ForLoops
{
Line 1,645 ⟶ 1,700:
}
}
</syntaxhighlight>
=={{header|Fennel}}==
<
(for [j 1 i]
(io.write "*"))
(print))
</syntaxhighlight>
=={{header|Fermat}}==
<
for i = 1 to 5 do for j = 1 to i do !'*'; od; !; od
</syntaxhighlight>
=={{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.
<
02.10 FOR J=1,I; TYPE "*"
02.20 TYPE !</
{{out}}
<pre>
Line 1,674 ⟶ 1,729:
=={{header|Forth}}==
<
1+ 1 do
cr i 0 do [char] * emit loop
loop ;
5 triangle</
One more:
<
: limit_example
15 1 do r> r@ dup rot >r drop \ Bring limit on stack
Line 1,686 ⟶ 1,741:
loop ;
\ Gforth and JSForth all work, SP-Forth brakes (different 'for' implementation?)
</syntaxhighlight>
=={{header|Fortran}}==
{{works with|Fortran|77 and later}}
<
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,722 ⟶ 1,777:
5001 FORMAT (A, $)
C5001 FORMAT (A, ADVANCE='NO')
END</
{{works with|Fortran|90 and later}}
<
DO j = 1, i
WRITE(*, "(A)", ADVANCE="NO") "*"
END DO
WRITE(*,*)
END DO</
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}}
<
integer, dimension(10) :: v
forall (i=1:size(v)) v(i) = i</
But if one accepts that a do-loop can be expressed without the actual word "do" (or "for"), then
<syntaxhighlight lang="fortran">
DO 1 I = 1,5
1 WRITE (6,*) ("*", J = 1,I)
END
</syntaxhighlight>
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">
DO 1 I = 1,5
1 WRITE (6,2) (666, J = 1,I)
2 FORMAT(5I1)
END
</syntaxhighlight>
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}}==
<
For i As Integer = 1 To 5
Line 1,765 ⟶ 1,820:
Next
Sleep</
{{out}}
Line 1,777 ⟶ 1,832:
=={{header|Frink}}==
<
for n = 1 to 5
{
Line 1,785 ⟶ 1,840:
println[]
}
</syntaxhighlight>
=={{header|Futhark}}==
Line 1,793 ⟶ 1,848:
inner loop.
<syntaxhighlight lang="futhark">
fun main(n: int): [n]int =
loop (a = replicate n 0) = for i < n do
Line 1,801 ⟶ 1,856:
in a)
in a
</syntaxhighlight>
=={{header|FutureBasic}}==
<
long i, j
Line 1,815 ⟶ 1,870:
next
HandleEvents</
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=9be4a2a6e69b2bf2921279a057df18c5 Click this link to run this code]'''
<
Dim i, j As Integer
Line 1,829 ⟶ 1,884:
Next
End</
<pre>
*
Line 1,839 ⟶ 1,894:
=={{header|GAP}}==
<
for j in [1 .. i] do
Print("*");
Line 1,850 ⟶ 1,905:
# ***
# ****
# *****</
=={{header|GML}}==
<
for(i = 1; i <= 5; i += 1)
{
Line 1,862 ⟶ 1,917:
pattern += "#"
}
show_message(pattern)</
=={{header|Go}}==
<
import "fmt"
Line 1,876 ⟶ 1,931:
fmt.Printf("\n")
}
}</
{{out}}
<pre>
Line 1,888 ⟶ 1,943:
=={{header|Groovy}}==
Solution:
<
for(j in (1..i)) {
print '*'
}
println ()
}</
=={{header|GW-BASIC}}==
<
110 FOR J=1 TO I
120 PRINT "*";
Line 1,902 ⟶ 1,957:
140 PRINT
150 NEXT I
160 END</
{{out}}
Line 1,914 ⟶ 1,969:
=={{header|Hack}}==
<
for($j = 0; $j <= $i; $j++) {
echo '*';
Line 1,920 ⟶ 1,975:
echo '\n';
}</
=={{header|Haskell}}==
<
main = do
Line 1,929 ⟶ 1,984:
forM_ [1..i] $ \j -> do
putChar '*'
putChar '\n'</
But it's more Haskellish to do this without loops:
<
main = mapM_ putStrLn $ tail $ inits $ replicate 5 '*'</
Or, with a list comprehension:
<
Taking from an infinite stream of increasing length lines:
<
=={{header|Haxe}}==
<
for(j in 0...i) {
Sys.print('*');
}
Sys.println('');
}</
=={{header|hexiscript}}==
<
for let j 1; j <= i; j++
print "*"
endfor
println ""
endfor</
=={{header|HicEst}}==
<
DO j = 1, i
WRITE(APPend) "*"
ENDDO
WRITE() ' '
ENDDO</
=={{header|HolyC}}==
<
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
Print("*");
Print("\n");
}</
== Icon and Unicon ==
==={{header|Icon}}===
<
every i := 1 to 5 do {
every 1 to i do
Line 1,986 ⟶ 2,041:
write()
}
end</
==={{header|Unicon}}===
The Icon solution works in Unicon.
=={{header|Inform 7}}==
<
repeat with N running from 1 to length:
say "*";
say line break;</
=={{header|J}}==
J is array-oriented, so there is very little need for loops. For example
<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
for. i. i do.
end.
echo
end.
EMPTY
}}0
</syntaxhighlight>
But you would almost never see J code like this.
=={{header|Java}}==
<
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}</
=={{header|JavaScript}}==
<
for (i = 1; i <= 5; i += 1) {
s = '';
Line 2,033 ⟶ 2,093:
s += '*';
document.write(s + '<br>');
}</
Line 2,039 ⟶ 2,099:
or a simple range function which generates a range:
<
return i ? range(i - 1).concat(i) : [];
}
range(5) --> [1, 2, 3, 4, 5]</
We could write something like:
<
range(5).forEach(
Line 2,058 ⟶ 2,118:
);
console.log(s);</
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:
<
range(5).reduce(
function (a, n) {
Line 2,068 ⟶ 2,128:
}, ''
)
);</
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 2,074 ⟶ 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.
<
range(5).map(function(a) {
return Array(a + 1).join('*');
}).join('\n')
);</
=={{header|Jinja}}==
Variable usage inside a loop,
before version 2.10 :
<
{% for i in range(6) %}
{{ sum }}{% set sum = sum + i %}
{%- endfor %}""").render())
</syntaxhighlight>
Since 2.10 :
<
{% for i in range(6) %}
{{ sum.value }}{% set sum.value = sum.value + i %}
{%- endfor %}""").render())
</syntaxhighlight>
=={{header|jq}}==
<
def demo(m):
reduce range(0;m) as $i
Line 2,108 ⟶ 2,168:
# Variation of demo2 using an implicit inner loop:
def demo3(m): range(1;m) | "*" * . ;</
'''Example using demo(6)'''
{{Out}}
Line 2,120 ⟶ 2,180:
=={{header|Jsish}}==
Code from Javascript entry.
<
for (i = 1; i <= 5; i += 1) {
s = '';
for (j = 0; j < i; j += 1) s += '*';
puts(s);
}</
{{out}}
Line 2,136 ⟶ 2,196:
=={{header|Julia}}==
<syntaxhighlight lang="julia">
for i in 1:5
for j in 1:i
Line 2,143 ⟶ 2,203:
println()
end
</syntaxhighlight>
{{out}}
Line 2,153 ⟶ 2,213:
=={{header|Klong}}==
<syntaxhighlight lang="k">
:" x{p}:*y means repeat {p} x times starting at y "
Line 2,164 ⟶ 2,224:
{.p(x:^0c*)}'1+!5
</syntaxhighlight>
=={{header|Kotlin}}==
<
(1..5).forEach {
(1..it).forEach { print('*') }
println()
}
}</
=={{header|LabVIEW}}==
Line 2,179 ⟶ 2,239:
=={{header|Lambdatalk}}==
<
{def loops_for
{lambda {:i :n}
Line 2,208 ⟶ 2,268:
* * * *
* * * * *
</syntaxhighlight>
=={{header|Lang5}}==
<
: nip swap drop ; : last -1 extract nip ;
: times
Line 2,219 ⟶ 2,279:
: concat "" join ;
'* 1 5 "2dup reshape concat . cr 1 +" times</
=={{header|langur}}==
<
for .j = 0; .j <= .i; .j += 1 {
write "*"
}
writeln()
}</
A for of loop iterates over keys (when used with an array, string, or hash) and a for in loop iterates over values.
<
for of .i {
write "*"
}
writeln()
}</
Or, with one for loop...
<
writeln "*"
}</
=={{header|Lasso}}==
<
loop(loop_count) => {^ '*' ^}
'\r'
^}</
=={{header|LC3 Assembly}}==
<
AND R1,R1,0
Line 2,280 ⟶ 2,340:
LF .FILL 0x0A
.END</
Output:
<pre>*
Line 2,287 ⟶ 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.
<
for {set j 1} {$j <= $i} {inc j} {
write "*"
}
print
}</
{{out}}
Line 2,309 ⟶ 2,391:
=={{header|Lingo}}==
<
str = ""
repeat with j = 1 to i
Line 2,315 ⟶ 2,397:
end repeat
put str
end repeat</
=={{header|Lisaac}}==
<
1.to i do { dummy : INTEGER;
'*'.print;
};
'\n'.print;
};</
=={{header|LiveCode}}==
<
repeat for 5 times
add 1 to n
Line 2,333 ⟶ 2,415:
end repeat
put return
end repeat</
=={{header|Logo}}==
<
repeat 5 [repeat repcount [type "*] (print)]</
=={{header|Lua}}==
<
for i=1,5 do
for j=1,i do
Line 2,347 ⟶ 2,429:
io.write("\n")
end
</syntaxhighlight>
single loop
<
print(string.rep("*", i))
end</
or
<
print(("*"):rep(i))
end</
=={{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">
For i=1 to 5
For j=1 to i
Line 2,375 ⟶ 2,457:
}
Print "End2"
</syntaxhighlight>
=={{header|M4}}==
<
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
Line 2,386 ⟶ 2,468:
`for(`y',`1',x,`1',
`*')
')</
=={{header|make}}==
{{works with|BSD make}}
{{libheader|jot}}
<
ILIST != jot 5
Line 2,416 ⟶ 2,498:
. endfor
.endfor</
=={{header|Maple}}==
<
*
**
***
****
*****</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">n=5;
For[i=1,i<=5,i++,
string="";
For[j=1,j<=i,j++,string=string<>"*"];
Print[string]
]</
=={{header|MATLAB}} / {{header|Octave}}==
<
output = [];
for j = (1:i)
Line 2,441 ⟶ 2,523:
end
disp(output);
end</
Vectorized version:
<
disp(repmat('*',1,i));
end</
=={{header|Maxima}}==
<
s: "",
thru i do s: sconcat(s, "*"),
print(s)
);</
=={{header|MAXScript}}==
<
(
line = ""
Line 2,466 ⟶ 2,548:
)
format "%\n" line
)</
=={{header|Mercury}}==
<syntaxhighlight lang="text">:- module loops_for.
:- interface.
Line 2,490 ⟶ 2,572:
inner_loop_body(_, !IO) :-
io.write_char('*', !IO).</
=={{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:
<
s = ""
for j in range(1, i)
Line 2,500 ⟶ 2,582:
end for
print s
end for</
{{out}}
Line 2,511 ⟶ 2,593:
However, it is worth noting that MiniScript's string replication operator (*) makes a more natural solution possible:
<
print "*" * i
end for</
(Output same as above.)
Line 2,519 ⟶ 2,601:
=={{header|MIPS Assembly}}==
Thanks to [https://www.chibialiens.com/mips/ ChibiAliens] for the header and footer as well as print routines.
<
.include "\SrcAll\BasicMacros.asm"
.include "\SrcPSX\MemoryMap.asm"
Line 2,562 ⟶ 2,644:
.include "..\\SrcAll\monitor.asm"
.include "\SrcN64\Footer.asm"</
{{out}}
Line 2,573 ⟶ 2,655:
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 2,586 ⟶ 2,668:
InOut.WriteLn
END
END For.</
=={{header|Modula-3}}==
<
IMPORT IO;
Line 2,600 ⟶ 2,682:
IO.Put("\n");
END;
END Stars.</
=={{header|MOO}}==
<
s = "";
for j in [1..i]
Line 2,609 ⟶ 2,691:
endfor
player:tell(s);
endfor</
=={{header|Morfa}}==
<
import morfa.base;
Line 2,623 ⟶ 2,705:
println("");
}
</syntaxhighlight>
=={{header|MUMPS}}==
=== Routine ===
<
NEW I,J
FOR I=1:1:5 DO
Line 2,633 ⟶ 2,715:
..WRITE "*"
.WRITE !
QUIT</
{{out}}
<pre>
Line 2,646 ⟶ 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).
<
=={{header|Nanoquery}}==
<
for ($j = 0) ($j < $i) ($j = $j+1)
print "*"
end for
println
end for</
=={{header|Nemerle}}==
<
{
for (int j = 0; j <= i; j++)
Line 2,664 ⟶ 2,746:
}
WriteLine();
}</
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 2,679 ⟶ 2,761:
say
end i_
</syntaxhighlight>
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(for (i 1 5)
(for(j 1 i)
(print "*"))
(print "\n"))
</syntaxhighlight>
=={{header|Nim}}==
<
for j in 1..i:
stdout.write("*")
echo("")</
=={{header|NS-HUBASIC}}==
<
20 FOR J=1 TO I
30 PRINT "*";
40 NEXT
50 PRINT
60 NEXT</
=={{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
<
MODULE LoopFor;
IMPORT
Line 2,720 ⟶ 2,812:
END
END LoopFor.
</syntaxhighlight>
=={{header|Objeck}}==
<
bundle Default {
class For {
Line 2,740 ⟶ 2,832:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
<
for j = 1 to i do
print_string "*"
done;
print_newline ()
done</
=={{header|Octave}}==
<
for j = 0:1:i
printf("*");
endfor
printf("\n");
endfor</
=={{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}}==
<
| i j |
n loop: i [
i loop: j [ "*" print ]
printcr ;</
=={{header|Onyx}}==
<
Using repeat inside the for loop instead of nesting another for loop is shorter and more efficient.
=={{header|Order}}==
<
ORDER_PP(
Line 2,779 ⟶ 2,886:
8space)),
1, 6)
)</
(Order cannot print newlines, so this example just uses a space.)
=={{header|Oz}}==
<
for _ in 1..I do
{System.printInfo "*"}
end
{System.showInfo ""}
end</
Note: we don't use the inner loop variable, so we prefer not to give it a name.
=={{header|Panoramic}}==
<syntaxhighlight lang="panoramic">
dim x,y
Line 2,806 ⟶ 2,913:
next x
</syntaxhighlight>
=={{header|PARI/GP}}==
<
=={{header|Pascal}}==
<
var
Line 2,824 ⟶ 2,931:
writeln
end
end.</
=={{header|Perl}}==
<
for(my $y = 1; $y <= $x; $y++) {
print "*";
}
print "\n";
}</
<
foreach (1..$_) {
print '*';
}
print "\n";
}</
However, if we lift the constraint of two loops the code will be simpler:
<
or equivalently
<
=={{header|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,856 ⟶ 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>
<!--</
=={{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}}==
<
for ($j = 1; $j <= $i; $j++) {
echo '*';
}
echo "\n";
}</
or
<
foreach (range(1, $i) as $j) {
echo '*';
}
echo "\n";
}</
or
<
echo str_repeat('*', $i) , PHP_EOL;</
=={{header|Picat}}==
<
N = 5,
foreach(I in 1..N)
Line 2,884 ⟶ 3,019:
end,
nl
end.</
=={{header|PicoLisp}}==
<
(do N (prin "*"))
(prinl) )</
=={{header|Pike}}==
<
for(int i = 1; i <= 5; i++){
for(int j=1; j <= i; j++){
Line 2,900 ⟶ 3,035:
write("\n");
}
}</
=={{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.
<
*OuterLoop
C :j = 0
Line 2,915 ⟶ 3,050:
C :i = i + 1
J ( i < 6 ) :*OuterLoop
END:</
=={{header|PL/I}}==
Basic version:
<
do j = 1 to i;
put edit ('*') (a);
end;
put skip;
end;</
Advanced version:
<
put skip edit (('*' do j = 1 to i)) (a);
end;</
Due to the new line requirement a mono line version is not possible
<
=={{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.
<
Start up.
Write a triangle of asterisks on the console given 5.
Line 2,948 ⟶ 3,083:
If a counter is past the size, exit.
Write a row of asterisks on the console given the counter.
Repeat.</
=={{header|Pop11}}==
<
for i from 1 to 5 do
for j from 1 to i do
Line 2,957 ⟶ 3,092:
endfor;
printf('\n')
endfor;</
=={{header|PowerShell}}==
<
for ($j = 1; $j -le $i; $j++) {
Write-Host -NoNewline *
}
Write-Host
}</
Alternatively the same can be achieved with a slightly different way by using the range operator along with the <code>ForEach-Object</code> cmdlet:
<
1..$_ | ForEach-Object {
Write-Host -NoNewline *
}
Write-Host
}</
while the inner loop wouldn't strictly be necessary and can be replaced with simply <code>"*" * $_</code>.
=={{header|Processing}}==
<
for ( int i=20; i<=100; i+=20 )
for ( int j=10; j<=i; j+=20 )
text( "*", j,i );</
=={{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.
<
between(1,5,I), nl, between(1,I,_J),
write('*'), fail.
example.</
<pre>?- example.
Line 2,998 ⟶ 3,133:
=={{header|Python}}==
<
for i in xrange(5):
for j in xrange(i+1):
sys.stdout.write("*")
print</
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:
<
print '*' * i</
or
<
=={{header|QB64}}==
''CBTJD'': 2020/03/14
<
FOR n = 1 TO c
PRINT "*";
NEXT
PRINT
NEXT</
=={{header|Quackery}}==
<
{{Out}}
<pre>*
Line 3,029 ⟶ 3,164:
=={{header|R}}==
<
s <- ""
for(j in 0:i) {
Line 3,035 ⟶ 3,170:
}
print(s)
}</
=={{header|Racket}}==
<
=={{header|Raku}}==
Line 3,044 ⟶ 3,179:
{{works with|Rakudo|#22 "Thousand Oaks"}}
<syntaxhighlight lang="raku"
for 0..$_ {
Line 3,052 ⟶ 3,187:
print "\n";
}</
or using only one for loop:
<syntaxhighlight lang="raku"
or without using any loops at all:
<syntaxhighlight lang="raku"
=={{header|Rapira}}==
<
output: "*" * N
od</
=={{header|REBOL}}==
<
repeat i 5 [
Line 3,080 ⟶ 3,215:
loop i [prin "*"]
print ""
]</
=={{header|Red}}==
<
repeat i 5 [
loop i [prin "*"]
prin newline
]</
=={{header|ReScript}}==
<
for i in 1 to 5 {
for _ in 1 to i {
Line 3,098 ⟶ 3,233:
s := Js.String2.concat(s.contents, "\n")
}
Js.log(s.contents)</
{{output}}
<pre>
Line 3,111 ⟶ 3,246:
=={{header|Retro}}==
<
=={{header|REXX}}==
===using concatenation===
<
do j=1 for 5 /*this is the same as: do j=1 to 5 */
Line 3,123 ⟶ 3,258:
end /*k*/
say $ /*display character string being built.*/
end /*j*/ /*stick a fork in it, we're all done. */</
{{out|output|:}}
<pre>
Line 3,134 ⟶ 3,269:
===using abutment===
<
do j=1 for 5 /*this is the same as: do j=1 to 5 */
Line 3,142 ⟶ 3,277:
end /*k*/
say $ /*display character string being built.*/
end /*j*/ /*stick a fork in it, we're all done. */</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|Ring}}==
can be done in just one line:
<
for i = 1 to 5 for x = 1 to i see "*" next see nl next
</syntaxhighlight>
or multiple line
<
for i = 1 to 5
for x = 1 to i
Line 3,158 ⟶ 3,293:
see nl
next
</syntaxhighlight>
=={{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,168 ⟶ 3,313:
|-
| style="vertical-align: top;" |
<
for j in 1..i
print "*"
end
puts
end</
| style="vertical-align: top;" |
<
(1..i).each do |j|
print "*"
end
puts
end</
|}
Line 3,191 ⟶ 3,336:
|-
| style="vertical-align: top;" |
<
1.upto(i) do |j|
print "*"
end
puts
end</
| style="vertical-align: top;" |
<
# i goes from 0 to 4
(i+1).times do
Line 3,204 ⟶ 3,349:
end
puts
end</
| style="vertical-align: top;" |
<
loop do
j = 1
Line 3,215 ⟶ 3,360:
puts
break if (i += 1) > 5
end</
|}
Or we can use String#* as the inner loop, and Enumerable#map as the outer loop. This shrinks the program to one line.
<
=={{header|Rust}}==
The compiler warns when you create an unused variable; here we use _ to avoid this effect.
<
for i in 0..5 {
for _ in 0..=i {
Line 3,232 ⟶ 3,377:
println!();
}
}</
=={{header|Salmon}}==
<
{
iterate (y; [0...x])
print("*");;
print("\n");
};</
or
<
{
for (y; 0; y <= x)
print("*");;
print("\n");
};</
=={{header|SAS}}==
<
length a $5;
do n=1 to 5;
Line 3,272 ⟶ 3,417:
put a;
end;
run;</
=={{header|Sather}}==
Line 3,278 ⟶ 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.
<
-- from, to, step
for!(once init:INT, once to:INT, once inc:INT):INT is
Line 3,298 ⟶ 3,443:
end;
end;
</syntaxhighlight>
=={{header|Scala}}==
<
for (j <- 1 to i)
print("*")
println()
}</
=={{header|Scheme}}==
<
((> i 5))
(do ((j 1 (+ j 1)))
((> j i))
(display "*"))
(newline))</
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">for i=1:5
s=""
for j=1:i
Line 3,323 ⟶ 3,468:
end
printf("%s\n",s)
end</
{{out}}
<pre>*
Line 3,332 ⟶ 3,477:
=={{header|Seed7}}==
<syntaxhighlight lang="python">
$ include "seed7_05.s7i";
const proc: main is func
local
var integer: I is 1;
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}}==
<
for j in {1..i} loop
nprint( '*' );
end loop;
print; -- new line
end loop;</
=={{header|Sidef}}==
'''for(;;)''' loop:
<
for (var j = 1; j <= i; j++) {
print '*'
}
print "\n"
}</
'''for([])''' loop:
<
for (1..i) { print '*' }
print "\n"
}</
'''for-in''' loop:
<
for j in (1..i) { print '*' }
print "\n"
}</
Idiomatic:
<
i.times { print '*' }
print "\n"
}</
=={{header|Simula}}==
{{works with|SIMULA-67}}
<
integer i,j;
for i:=1 step 1 until 5 do
Line 3,385 ⟶ 3,549:
end
end
</syntaxhighlight>
=={{header|Slate}}==
<
=={{header|Smalltalk}}==
<
aNumber timesRepeat: [ '*' display ].
Character nl display.
]</
or:
<
1 to: row do: [:col | '*' display ].
]</
(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}}
<
Stdout next: n put: $*; cr
]</
=={{header|SNOBOL4}}==
A slightly longer, "mundane" version
<
inner = outer; stars = ""
il stars = ?gt(inner,0) stars "*" :f(disp)
inner = inner - 1 :(il)
disp output = stars; :(ol)
end</
The "real SNOBOL4" starts here:
<
inner t = t ?(b = (gt(b,0) b - 1)) "*" :s(inner)
t span("*") . terminal = :(outer)
end</
one "loop" only:
<
a a len(x = x + 1) . output :s(a)
end</
... or just (courtesy of GEP2):
{{works with|SNOBOL4|which defaults to anchored mode}}
<
end</
=={{header|SNUSP}}==
Line 3,466 ⟶ 3,630:
=={{header|Sparkling}}==
<
for (var col = 1; col <= row; col++) {
printf("*");
Line 3,472 ⟶ 3,636:
print();
}</
=={{header|Spin}}==
Line 3,479 ⟶ 3,643:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 3,496 ⟶ 3,660:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</
{{out}}
<pre>
Line 3,507 ⟶ 3,671:
=={{header|SPL}}==
<
> j, 1..i
#.output("*",#.rs)
<
#.output()
<</
=={{header|Stata}}==
<
local s ""
forvalues i=1/`n' {
Line 3,522 ⟶ 3,686:
}
display "`s'"
}</
=== Mata ===
<
for (j=1; j<=i; j++) printf("*")
printf("\n")
}</
=={{header|Suneido}}==
<
{
str = ''
Line 3,537 ⟶ 3,701:
str $= '*'
Print(str)
}</
=={{header|Swift}}==
<
for _ in 1...i {
print("*", terminator: "")
}
print()
}</
{{out}}
<pre>
Line 3,557 ⟶ 3,721:
=={{header|Tailspin}}==
Tailspin uses streams of values within streams of values rather than loops.
<
1..5 -> '$:1..$ -> '*';
' -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>
Line 3,571 ⟶ 3,735:
=={{header|Tcl}}==
<
for {set i 1} {$i <= $lines} {incr i} {
puts -nonewline "*"
}
puts ""
}</
Note that it would be more normal to produce this output with:
<
puts [string repeat "*" $i]
}</
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:
<
for { set io [open test.txt r] } { ![eof $io] } { gets $io line } {
if { $line != "" } { ...do something here... }
}</
(This is a somewhat awkward example; just to show what is possible)
Line 3,602 ⟶ 3,766:
=={{header|TI-89 BASIC}}==
<
ClrIO
For i, 1, 5
Line 3,608 ⟶ 3,772:
Output i*8, j*6, "*"
EndFor
EndFor</
=={{header|TorqueScript}}==
<
{
for(%x = %i; %x < 5; %x++)
Line 3,619 ⟶ 3,783:
echo(%string);
}
}</
=={{header|TransFORTH}}==
<
1 + 1 DO
I 0 DO
PRINT " * " LOOP
CR LOOP ;
5 PRINTSTARS</
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
m=""
Line 3,637 ⟶ 3,801:
PRINT m
ENDLOOP
</syntaxhighlight>
{{out}}
<pre>
Line 3,648 ⟶ 3,812:
=={{header|TypeScript}}==
<
let line: string = ""
for(let j: number = 0; j <= i; ++j) {
Line 3,655 ⟶ 3,819:
console.log(line)
}
</syntaxhighlight>
=={{header|UNIX Shell}}==
Line 3,661 ⟶ 3,825:
{{works with|Bourne Shell}}
<
# Using a while control construct to emulate a for loop
Line 3,675 ⟶ 3,839:
echo
l=`expr "$l" + 1` # Increment the outer counter
done</
The [[Bourne Shell]] has a for loop, but it requires a list of words to iterate.
Line 3,682 ⟶ 3,846:
{{works with|Bourne Shell}}
{{libheader|jot}}
<
for j in `jot $i`; do
printf \*
done
echo
done</
Bash has <tt>for</tt> loops that act like C. These loops are very good for this task.
{{works with|Bourne Again SHell|3}}
<
for (( y=1; y<=$x; y=$y+1 )); do
echo -n '*'
done
echo ""
done</
==={{header|C Shell}}===
{{libheader|jot}}
<
foreach j (`jot $i`)
echo -n \*
end
echo ""
end</
==={{header|
{{works with|
<
do
for ((y = 0; y < x; y += 1))
do
print -n '*'
done
print
done</syntaxhighlight>
=={{header|UnixPipes}}==
<
[ $n -gt 5 ] && exit 0;
yes \* | head -n $n | xargs -n $n echo
done)</
=={{header|Ursa}}==
<
# for loop
#
Line 3,734 ⟶ 3,900:
end for
out endl console
end for</
=={{header|Vala}}==
<
for (var i = 1; i <= 5; i++) {
for (var j = 1; j <= i; j++) {
Line 3,745 ⟶ 3,911:
}
return 0;
}</
=={{header|VBA}}==
<
Sub LoopEx()
Dim i As Long, j As Long, s As String
Line 3,758 ⟶ 3,924:
Debug.Print s
Next
End Sub</
=={{header|VBScript}}==
<
Dim i, j, s
For i = 1 To 5
Line 3,769 ⟶ 3,935:
Next
WScript.Echo s
Next</
=={{header|Vedit macro language}}==
<
for (#2 = 1; #2 <= #1; #2++) {
Type_Char('*')
}
Type_Newline
}</
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
module main;
integer i, j;
Line 3,794 ⟶ 3,960:
end
endmodule
</syntaxhighlight>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
for i in 1..6 {
for _ in 1..i+1 {
Line 3,804 ⟶ 3,970:
print("\n")
}
}</
{{out}}
<pre>
Line 3,815 ⟶ 3,981:
=={{header|Wart}}==
<
for j 0 (j < i) ++j
pr "*"
(prn)</
=={{header|Wee Basic}}==
print 1 "" ensures the end of program text is separate from the asterisk characters.
<
print 1 ""
for x=0 to y
Line 3,828 ⟶ 3,994:
next
next
end</
=={{header|Wren}}==
<
for (j in 1..i) System.write("*")
System.print()
}</
{{out}}
Line 3,847 ⟶ 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.
<
outerloop: mov cx, bx ; inner loop counter
Line 3,867 ⟶ 4,033:
jne outerloop
ret</
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<
PROGRAM "for"
Line 3,885 ⟶ 4,051:
END FUNCTION
END PROGRAM
</syntaxhighlight>
=={{header|XBS}}==
<
const maxIters = 5;
for(i=1;maxIters;1){
Line 3,896 ⟶ 4,062:
}
log(str);
}</
{{out}}
<pre>*
Line 3,906 ⟶ 4,072:
=={{header|XLISP}}==
The equivalent of other languages' <tt>FOR</tt> or <tt>DO</tt> loops can be written using <tt>DO</tt>:
<
(DO ((J 0 (+ J 1))) ((= J I))
(DISPLAY "*"))
(NEWLINE))</
{{out}}
<pre>*
Line 3,919 ⟶ 4,085:
=={{header|XPL0}}==
<
int I, J;
for I:= 1 to 5 do
Line 3,925 ⟶ 4,091:
ChOut(0, ^*);
CrLf(0);
]</
=={{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):
<
wr_char equ &bb5a ; write ASCII character in register A to screen
; (jumps into CPC ROM)
Line 3,969 ⟶ 4,135:
pop de
pop bc
ret ; return to BASIC interpreter</
=={{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}}==
<
foreach j in (i){print("*")}
println();
}</
{{out}}
<pre>
Line 3,984 ⟶ 4,173:
*****
</pre>
|