Loops/Nested: Difference between revisions
Content added Content deleted
(Added Pike implementation) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 164: | Line 164: | ||
+3 +13 +6 +8 +6 +10 +9 +15 +20 |
+3 +13 +6 +8 +6 +10 +9 +15 +20 |
||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 430: | Line 429: | ||
bx lr |
bx lr |
||
</lang> |
</lang> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 613: | Line 611: | ||
Done: |
Done: |
||
printf("\n"); |
printf("\n"); |
||
return 0; |
|||
}</lang> |
|||
=={{header|C++}}== |
|||
Lambda call: |
|||
{{works with|C++11}} |
|||
<lang cpp>#include<cstdlib> |
|||
#include<ctime> |
|||
#include<iostream> |
|||
using namespace std; |
|||
int main() |
|||
{ |
|||
int arr[10][10]; |
|||
srand(time(NULL)); |
|||
for(auto& row: arr) |
|||
for(auto& col: row) |
|||
col = rand() % 20 + 1; |
|||
([&](){ |
|||
for(auto& row : arr) |
|||
for(auto& col: row) |
|||
{ |
|||
cout << col << endl; |
|||
if(col == 20)return; |
|||
} |
|||
})(); |
|||
return 0; |
|||
}</lang> |
|||
Goto statement: |
|||
{{works with|C++11}} |
|||
<lang cpp>#include<cstdlib> |
|||
#include<ctime> |
|||
#include<iostream> |
|||
using namespace std; |
|||
int main() |
|||
{ |
|||
int arr[10][10]; |
|||
srand(time(NULL)); |
|||
for(auto& row: arr) |
|||
for(auto& col: row) |
|||
col = rand() % 20 + 1; |
|||
for(auto& row : arr) { |
|||
for(auto& col: row) { |
|||
cout << ' ' << col; |
|||
if (col == 20) goto out; |
|||
} |
|||
cout << endl; |
|||
} |
|||
out: |
|||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
Line 724: | Line 669: | ||
Console.WriteLine(); |
Console.WriteLine(); |
||
} |
} |
||
}</lang> |
|||
=={{header|C++}}== |
|||
Lambda call: |
|||
{{works with|C++11}} |
|||
<lang cpp>#include<cstdlib> |
|||
#include<ctime> |
|||
#include<iostream> |
|||
using namespace std; |
|||
int main() |
|||
{ |
|||
int arr[10][10]; |
|||
srand(time(NULL)); |
|||
for(auto& row: arr) |
|||
for(auto& col: row) |
|||
col = rand() % 20 + 1; |
|||
([&](){ |
|||
for(auto& row : arr) |
|||
for(auto& col: row) |
|||
{ |
|||
cout << col << endl; |
|||
if(col == 20)return; |
|||
} |
|||
})(); |
|||
return 0; |
|||
}</lang> |
|||
Goto statement: |
|||
{{works with|C++11}} |
|||
<lang cpp>#include<cstdlib> |
|||
#include<ctime> |
|||
#include<iostream> |
|||
using namespace std; |
|||
int main() |
|||
{ |
|||
int arr[10][10]; |
|||
srand(time(NULL)); |
|||
for(auto& row: arr) |
|||
for(auto& col: row) |
|||
col = rand() % 20 + 1; |
|||
for(auto& row : arr) { |
|||
for(auto& col: row) { |
|||
cout << ' ' << col; |
|||
if (col == 20) goto out; |
|||
} |
|||
cout << endl; |
|||
} |
|||
out: |
|||
return 0; |
|||
}</lang> |
}</lang> |
||
Line 860: | Line 858: | ||
writeln(); |
writeln(); |
||
}</lang> |
}</lang> |
||
=={{header|Delphi}}/{{header|Pascal}}== |
|||
<lang delphi>var |
|||
matrix: array[1..10,1..10] of Integer; |
|||
row, col: Integer; |
|||
broken: Boolean; |
|||
begin |
|||
// Launch random number generator |
|||
randomize; |
|||
// Filling matrix with random numbers |
|||
for row := 1 to 10 do |
|||
for col := 1 to 10 do |
|||
matrix[row, col] := Succ(Random(20)); |
|||
// Displaying values one by one, until at the end or reached number 20 |
|||
Broken := False; |
|||
for row := 1 to 10 do |
|||
begin |
|||
for col := 1 to 10 do |
|||
begin |
|||
ShowMessage(IntToStr(matrix[row, col])); |
|||
if matrix[row, col] = 20 then |
|||
begin |
|||
Broken := True; |
|||
break; |
|||
end; |
|||
end; |
|||
if Broken then break; |
|||
end; |
|||
end;</lang> |
|||
=={{header|dc}}== |
=={{header|dc}}== |
||
Line 962: | Line 931: | ||
lb >L [Enter outer loop.]sz</lang> |
lb >L [Enter outer loop.]sz</lang> |
||
In this program, ''li lj + 3 + Q'' breaks both the inner loop and the outer loop. We must count how many levels of string execution to break. Our loops use tail recursion, so each iteration is a level of string execution. We have i + 1 calls to outer loop L, and j + 1 calls to inner loop I, and 1 call to condition D; so we break i + j + 3 levels with ''li lj + 3 + Q''. |
In this program, ''li lj + 3 + Q'' breaks both the inner loop and the outer loop. We must count how many levels of string execution to break. Our loops use tail recursion, so each iteration is a level of string execution. We have i + 1 calls to outer loop L, and j + 1 calls to inner loop I, and 1 call to condition D; so we break i + j + 3 levels with ''li lj + 3 + Q''. |
||
=={{header|Delphi}}/{{header|Pascal}}== |
|||
<lang delphi>var |
|||
matrix: array[1..10,1..10] of Integer; |
|||
row, col: Integer; |
|||
broken: Boolean; |
|||
begin |
|||
// Launch random number generator |
|||
randomize; |
|||
// Filling matrix with random numbers |
|||
for row := 1 to 10 do |
|||
for col := 1 to 10 do |
|||
matrix[row, col] := Succ(Random(20)); |
|||
// Displaying values one by one, until at the end or reached number 20 |
|||
Broken := False; |
|||
for row := 1 to 10 do |
|||
begin |
|||
for col := 1 to 10 do |
|||
begin |
|||
ShowMessage(IntToStr(matrix[row, col])); |
|||
if matrix[row, col] = 20 then |
|||
begin |
|||
Broken := True; |
|||
break; |
|||
end; |
|||
end; |
|||
if Broken then break; |
|||
end; |
|||
end;</lang> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
Line 2,106: | Line 2,104: | ||
Checkit |
Checkit |
||
</lang> |
</lang> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
Line 2,605: | Line 2,602: | ||
} |
} |
||
print "\n";</lang> |
print "\n";</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|rakudo|2015-09-18}} |
|||
<lang perl6>my @a = [ (1..20).roll(10) ] xx *; |
|||
LINE: for @a -> @line { |
|||
for @line -> $elem { |
|||
print " $elem"; |
|||
last LINE if $elem == 20; |
|||
} |
|||
print "\n"; |
|||
} |
|||
print "\n";</lang> |
|||
{{out}} |
|||
<pre> 15 6 14 13 14 7 9 16 8 18 |
|||
7 6 18 11 19 13 12 5 18 8 |
|||
17 17 9 5 4 8 17 8 3 11 |
|||
9 20</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,880: | Line 2,859: | ||
(scan matrix)</lang> |
(scan matrix)</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|rakudo|2015-09-18}} |
|||
<lang perl6>my @a = [ (1..20).roll(10) ] xx *; |
|||
LINE: for @a -> @line { |
|||
for @line -> $elem { |
|||
print " $elem"; |
|||
last LINE if $elem == 20; |
|||
} |
|||
print "\n"; |
|||
} |
|||
print "\n";</lang> |
|||
{{out}} |
|||
<pre> 15 6 14 13 14 7 9 16 8 18 |
|||
7 6 18 11 19 13 12 5 18 8 |
|||
17 17 9 5 4 8 17 8 3 11 |
|||
9 20</pre> |
|||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
Line 3,118: | Line 3,116: | ||
[end] |
[end] |
||
print "At row:";row;" col:";col</lang> |
print "At row:";row;" col:";col</lang> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 3,434: | Line 3,431: | ||
biarr whileTrue: [ :v | v ~= 20 ] |
biarr whileTrue: [ :v | v ~= 20 ] |
||
do: [ :v | v displayNl ]</lang> |
do: [ :v | v displayNl ]</lang> |
||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
<lang spl>'fill array |
<lang spl>'fill array |