Loops/Nested: Difference between revisions

Content added Content deleted
(Added 11l)
(Updated to work with Nim 1.4. Changed the way to initialize the array. Done other syntactical changes.)
Line 2,518: Line 2,518:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import math, strutils
<lang nim>import random, strutils


const arrSize = 10
const ArrSize = 10


var a: array[0..arrSize-1, array[0..arrSize-1, int]]
var a: array[ArrSize, array[ArrSize, int]]
var s: string = ""
var s = ""


randomize() # different results each time this runs
randomize() # Different results each time this runs.


# Initialize using loops on items rather than indexes.
for i in 0 .. arrSize-1:
for j in countup(0,arrSize-1):
for row in a.mitems:
for item in row.mitems:
a[i][j] = random(20)+1
item = rand(1..20)


block outer:
block outer:
# Loop using indexes.
for i in countup(0,arrSize-1):
for j in 0 .. arrSize-1:
for i in 0..<ArrSize:
if a[i][j] < 10:
for j in 0..<ArrSize:
s.add(" ")
if a[i][j] < 10: s.add(' ')
addf(s,"$#",$a[i][j])
addf(s, "$#", $a[i][j])
if a[i][j] == 20:
if a[i][j] == 20: break outer
break outer
s.add(", ")
s.add(", ")
s.add('\n')

s.add("\n")
echo(s)</lang>
echo s</lang>

{{out}}
{{out}}
<pre> 9, 16, 3, 18, 4, 17, 2, 16, 7, 6,
<pre> 9, 16, 3, 18, 4, 17, 2, 16, 7, 6,