#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Help - Nested Loop Trouble


    Hey guys I'm having trouble with some homework hope someone can help. I have to display asterisks in a label using a nested Do..Loop within a For..Next Loop. It should look like this:

    **
    ****
    ******
    ********
    **********

    Here's my code so far :

    For Count As Integer = 2 To 10 Step 2
    Dim x As Integer
    Do Until x = 10
    x = x + 2
    lblAsterisks.Text = "**" &
    ControlChars.NewLine
    Loop
    Next Count

    Thanks in advance!
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    Though there are a number of ways of solving this problem, here you're supposed to build the asterisks one line at a time. There are two loops: one that loops for each line to build, and another inside it that loops for each pair of asterisks (because it shows two at a time) on that line.

    Look at it this way:
    Code:
    Line 1 has 1 pair of asterisks:
      Add the first pair.
    Line 2 has 2 pairs of asterisks:
      Add the first pair.
      Add the second pair.
    Line 3 has 3 pairs of asterisks:
      Add the first pair.
      Add the second pair.
      Add the third pair.
    Line 4 has 4 pairs of asterisks.
      Add the first pair.
      Add the second pair.
      Add the third pair.
      Add the fourth pair.
    Line 5 has 5 pairs of asterisks.
      Add the first pair.
      Add the second pair.
      Add the third pair.
      Add the fourth pair.
      Add the fifth pair.
    Do you see where the two loops are?

    And a suggestion: try writing it with two For loops first. When that works, convert the inner loop into a Do loop.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    I am not sure why you would need a Do Loop, but I will leave you to figure that one out.
    Code:
        Dim x As Integer
        Dim lblAsterisks As String
        For x = 2 To 10 Step 2
            lblAsterisks = lblAsterisks & String(x, "**") & vbCrLf
        Next x
        Debug.Print lblAsterisks
    J.A. Coutts

IMN logo majestic logo threadwatch logo seochat tools logo