Nested For Loop to populate matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #31
    Originally posted by QVeen72
    Hi,

    How about this :

    [code=vb]
    Private Sub Command1_Click( )
    Dim i As Integer
    Dim s As String
    Dim k As Integer
    Dim j As Integer
    s = ""
    k = -1
    For i = 1 To 10
    For j = 1 To 5
    k = k + 1
    s = s & " " & CStr(k)
    Next
    s = s & vbCrLf
    Next
    MsgBox s
    End Sub

    [/code]

    REgards
    Veena
    I'm impressed, I couldnt work this out myself, i ould get the result but couldnt see how to use 2 for loops.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #32
      I believe Veena's code is still needlessly complex. Try this version...

      [CODE=vb]Private Sub Command1_Click( )
      Dim i As Integer, j As Integer, s As String
      For i = 0 To 49 Step 5
      For j = i To i + 4
      s = s & " " & j
      Next
      s = s & vbNewLine
      Next
      MsgBox s
      End Sub[/CODE]This is part of the beauty of nested loops, of course - the inner one can make use of the counter value of the outer one. It can also change the value, but that's another story.

      Comment

      Working...