Results 1 to 3 of 3

Thread: loops

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    17

    Unhappy

    im trying to make this print out by using nested For/Next loops. I can do it with out them but it is more work and doesnt seem necessary..

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



  2. #2
    Guest
    You could use:

    Code:
      Dim intIndex As Integer
      
      For intIndex = 1 To 10
        Print String(intIndex, "*") & " " & String(11 - intIndex, "*") & " " & String(11 - intIndex, "*") & " " & String(intIndex, "*")
      Next intIndex
    That uses minimal code, but doesn't use nested loops. If you really wanted to use nested loops (and more code) you could do:

    Code:
      Dim intOuter As Integer
      Dim intInner As Integer
      
      For intOuter = 1 To 10
        For intInner = 1 To intOuter
          Print "*";
        Next intInner
        Print " ";
        For intInner = 1 To 11 - intOuter
          Print "*";
        Next intInner
        Print " ";
        For intInner = 1 To 11 - intOuter
          Print "*";
        Next intInner
        Print " ";
        For intInner = 1 To intOuter
          Print "*";
        Next intInner
        Print
      Next intOuter
    Paul

    PS = paulw: *chuckle*


    [Edited by PWNettle on 11-22-2000 at 11:46 AM]

  3. #3
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Use symmetry.

    Code:
    For i = 1 To 10
      strToPrint = ""
      For j = 1 To i
        strToPrint = strToPrint & "*"
      Next j
      strToPrint = strToPrint & " "
      For j = 1 To 11 - i
        strToPrint = strToPrint & "*"
      Next j
      strToPrint = strToPrint & " "
      For j = 1 To 11 - i
        strToPrint = strToPrint & "*"
      Next j
      strToPrint = strToPrint & " "
      For j = 1 To i
        strToPrint = strToPrint & "*"
      Next j
      Debug.Print strToPrint
    Next i
    OK?

    P.

    edit: Hey PWNettle - great minds think alike!!!


    [Edited by paulw on 11-22-2000 at 11:01 AM]
    Not nearly so tired now...

    Haven't been around much so be gentle...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width