Results 1 to 4 of 4

Thread: looping problems??? (help)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    31

    Question looping problems??? (help)

    How do I change both For...Next statements to Do...Loop statements.


    'declare variables
    Dim term As Integer
    Dim principal, rate, payment As Double

    Try
    'assign input to a variable
    principal = Double.Parse(Me.uiPrincipalTextBox.Text)

    'display the years in the heading
    Me.uiPaymentLabel.Text = " 3 yrs 4 yrs 5 yrs" _
    & ControlChars.NewLine

    'calculate and display the monthly payments, using
    'rates of 5% to 10% and terms of 3 to 5 years
    For rate = 0.05 To 0.1 Step 0.01
    'display the current rate
    Me.uiPaymentLabel.Text = Me.uiPaymentLabel.Text & rate.ToString("P0") _
    & " "
    For term = 3 To 5
    payment = -Financial.Pmt(rate / 12, term * 12, principal)
    Me.uiPaymentLabel.Text = Me.uiPaymentLabel.Text _
    & payment.ToString("N2") & " "
    Next term
    Me.uiPaymentLabel.Text = Me.uiPaymentLabel.Text & ControlChars.NewLine
    Next rate


    Any Help would be greatly appreciated.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: looping problems??? (help)

    Think about what you want to happen. A Do loop does something either While something is True or Until it is True. You can test that condition either before you Do something or after. You simply need to ask yourself these questions:

    What do I want to Do?
    Do I want to do it While something is True or Until something is True?
    Do I need to test this condition before doing something or after?

    I have to say, if you need to change a working For loop to a Do loop then this must be homework, so for us to just provide code is unethical. The fact that you're using "ui" as a prefix is more evidence of this because I've never seen anyone but students use that. Your user name kind of gives it away too. I suggest that you answer those questions for yourself and see where that leads you. Try writing some code and post back if it doesn't work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: looping problems??? (help)

    VB Code:
    1. rate=0.05
    2. Do Until rate>0.1
    3.    'display the current rate
    4.    Me.uiPaymentLabel.Text = Me.uiPaymentLabel.Text & rate.ToString("P0") & " "
    5.    term=3
    6.    Do Until term>5
    7.       payment = -Financial.Pmt(rate / 12, term * 12, principal)
    8.       Me.uiPaymentLabel.Text = Me.uiPaymentLabel.Text _
    9.       & payment.ToString("N2") & " "
    10.       term+=1
    11.    Loop
    12.    Me.uiPaymentLabel.Text = Me.uiPaymentLabel.Text & ControlChars.NewLine
    13.    rate+=0.01
    14. Loop

    And does it look bad when I use [Highlight=VB] tag?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: looping problems??? (help)

    Ugh, code tags suck man. It formats it so you can like read and everything. Dude they're a total waste of time.

    As a side note he(/she?) also posted this in the classic VB forum and people have already given him the answer, myself included (hey it was 2:30AM, I wasn't thinking!).
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

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