Loops/For: Difference between revisions

2,496 bytes added ,  2 months ago
Zig sorted alphabetically before zkn
(Zig sorted alphabetically before zkn)
(23 intermediate revisions by 19 users not shown)
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}}==
Line 644 ⟶ 659:
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}}===
Line 753 ⟶ 777:
next
print
next</syntaxhighlight>
next
</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Microsoft Small Basic}}===
Line 777 ⟶ 802:
CloseConsole()
EndIf</syntaxhighlight>
 
==={{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}}===
<syntaxhighlight lang="runbasic">FOR i = 1 TO 5
FOR i = 1 TO 5
FOR j = 1 TO i
PRINT "*";
NEXT j
PRINT
NEXT i</syntaxhighlight>
</syntaxhighlight>
 
==={{header|smart BASIC}}===
Line 797 ⟶ 828:
print
next n</syntaxhighlight>
 
 
==={{header|True BASIC}}===
<syntaxhighlight lang="basic">FOR i = 1 TO 5
FOR i = 1 TO 5
FOR j = 1 TO i
PRINT "*";
Line 807 ⟶ 836:
PRINT
NEXT i
END</syntaxhighlight>
END
</syntaxhighlight>
 
 
==={{header|Visual Basic}}===
Line 840 ⟶ 867:
 
==={{header|ZX Spectrum Basic}}===
 
On the ZX Spectrum, we need line numbers:
 
<syntaxhighlight lang="basic">10 FOR i = 1 TO 5
10 FOR i = 1 TO 5
20 FOR j = 1 TO i
30 PRINT "*";
40 NEXT j
50 PRINT
60 NEXT i</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Batch File}}==
 
<syntaxhighlight lang="text">@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
Line 888 ⟶ 911:
| :-1<
^+1,+5+5<</syntaxhighlight>
 
=={{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 959 ⟶ 988:
 
std::cout.put('\n');
}</syntaxhighlight>
 
=={{header|C3}}==
<syntaxhighlight lang="c3">
for (int i = 0; i < 5; i++)
{
for (int j = 1; j <= i; j++) io::print("*");
io::printn("*");
}</syntaxhighlight>
 
Line 1,368 ⟶ 1,405:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">for i range 5
for i for= j1 rangeto i5
for j = 1 to i
write "*"
.
print ""
.
.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
Line 1,478 ⟶ 1,517:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight 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()
Line 1,530 ⟶ 1,569:
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,997 ⟶ 2,052:
 
=={{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
)</syntaxhighlight>
 
But you would almost never see J code like this.
Line 2,240 ⟶ 2,300:
Or, with one for loop...
<syntaxhighlight lang="langur">for .i of 5 {
writeln "*" x* .i
}</syntaxhighlight>
 
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}}==
Line 2,702 ⟶ 2,784:
50 PRINT
60 NEXT</syntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
for i in 1..5 {
for j in 1..$i {
print -n "*"
}
print ""
}
</syntaxhighlight>
 
=={{header|Oberon-2}}==
Line 2,872 ⟶ 2,964:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
 
=={{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}}==
Line 3,175 ⟶ 3,295:
</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,347 ⟶ 3,477:
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7python">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;</syntaxhighlight>
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}}==
Line 3,723 ⟶ 3,872:
end</syntaxhighlight>
 
==={{header|Korn Shellksh}}===
{{works with|Korn Shell 93ksh93}}
<syntaxhighlight lang="bashksh">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
done </syntaxhighlight>
print
done</syntaxhighlight>
 
=={{header|UnixPipes}}==
Line 3,811 ⟶ 3,962:
</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
for i in 1..6 {
for _ in 1..i+1 {
Line 3,846 ⟶ 3,997:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">for (i in 1..5) {
for (j in 1..i) System.write("*")
System.print()
Line 3,985 ⟶ 4,136:
pop bc
ret ; return to BASIC interpreter</syntaxhighlight>
 
=={{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}}==
Line 3,999 ⟶ 4,173:
*****
</pre>
 
=={{header|Zig}}==
<syntaxhighlight 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");
}
}</syntaxhighlight>
22

edits