# Android QuestionNested loop

#### Sub7

##### Active Member
Longtime User
Sorry for the dumb question, i know is something elementary but i'm stuck.

B4X:
``````Dim maxn As Int = 5
For i = 0 To  5
i = i+1

For k = 0 To maxn
Log(k)
Next

Next``````

The output is
-0 1 2 3 4 5
- 0 1 2 3 4 5
- 0 1 2 3 4 5

While i want the inner loop to generate:

- 0 1 2 3 4
- 5 6 7 8 9
- 10 11 12 13 14 and so on.

Thanks

#### sorex

##### Expert
Longtime User
B4X:
``````Dim maxn As Int = 5
For i = 0 To  5
'i = i+1   remove this

For k = 0 To maxn
Log(i*5+k)
Next

Next``````

Last edited:

#### Sub7

##### Active Member
Longtime User
B4X:
``````Dim maxn As Int = 5
For i = 0 To  5
i = i+1

For k = 0 To maxn
Log(i*5+k)
Next

Next``````

Hi Sorex look this

5
6
7
8
9
10

15
16
17..

While i want the inner loop to generate:

- 0 1 2 3 4
- 5 6 7 8 9
- 10 11 12 13 14

Thx

#### sorex

##### Expert
Longtime User
that's because you have an unneeded i=i+1 there

you also might need for k=0 to ...-1 depending on how you definded that variable (taking into account that it start from 0 or 1)

#### Sub7

##### Active Member
Longtime User
yeah right, thank you friend.

#### Sub7

##### Active Member
Longtime User
B4X:
``````Dim maxn As Int = 5
For i = 0 To  5
For k = 0 To maxn -1
Log(i*5+k)
Next
Log("five blocks")
Next``````

#### Sub7

##### Active Member
Longtime User
hmmm it's more tricky that what i tought, this is what i wanted to do:
I have to load 50 different images, to make this works i think i need five inner loops-

B4X:
``````Dim maxn As Int = 5
For i = 0 To  5

Dim p As Panel
p.Initialize("")
p.color = Colors.ARGB(255,235,235,235)

For k = 0 To maxn -1
Dim g As Int = i*5+k
img_ornnament1.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
img_ornnament2.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
img_ornnament3.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
img_ornnament4.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
img_ornnament5.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
Next

Log("five blocks")

Next``````

#### Sub7

##### Active Member
Longtime User
I feel stupid is just best work with an array of views. like tic tac toe example.

#### Sub7

##### Active Member
Longtime User
Sorry, i am trying to load 50 different images in a customlistview, five images each row.
I have named the images in this way 0_image 1_image to concatenate the filename inside a loop.

#### udg

##### Expert
Longtime User
Do you want to arrange 50 elements on 10 rows of 5 items each?
Something like?
B4X:
``````for i= 0 to maxitems-1 'maxitems = 50
row = floor(i/5)  ' 0,1,2,3,4,5,6,7,8,9
col = i mod 5     ' 0,1,2,3,4
log(row&" "&col)
next``````

#### klaus

##### Expert
Longtime User
Some thing like this:
B4X:
``````For row = 0 to 9
For col = 0 to 4
ImageIndex = row * 5 + col
ImageName = ImageIndex & "_image"
Next
Next``````

#### Sub7

##### Active Member
Longtime User
Yes thanks both. i am trying to arrange 50 elements on 10 rows of 5 items each.
Tryng...

#### Sub7

##### Active Member
Longtime User
I solved in this way

B4X:
``````For x = 0 To p.NumberOfViews -1
Next``````

Replies
6
Views
450
Replies
5
Views
1K
Replies
6
Views
944
Replies
1
Views
2K
Replies
0
Views
1K