Results 1 to 5 of 5

Thread: Question about program flow

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    4

    Question about program flow

    I am working my way through this book and I am a bit confused about the flow of this sample program:

    Code:
    ' Fig. 3.4: Interest.vb
    ' Calculating compound interest.
    
    Imports System.Windows.Forms
    
    Module modInterest
    
       Sub Main()
          Dim amount, principal As Decimal ' dollar amounts
          Dim rate As Double               ' interest rate
          Dim year As Integer              ' year counter
          Dim output As String             ' amount after each year
    
          principal = 1000
          rate = 0.05
    
          output = "Year" & vbTab & "Amount on deposit" & vbCrLf
    
          ' calculate amount after each year
          For year = 1 To 10
             amount = principal * (1 + rate) ^ year
             output &= year & vbTab & _
                String.Format("{0:C}", amount) & vbCrLf
          Next
    
          ' display output
          MessageBox.Show(output, "Compound Interest", _
             MessageBoxButtons.OK, MessageBoxIcon.Information)
       End Sub  ' Main
    
    End Module  ' modInterest
    The code displays a message box with 10 years' interest, but I don't see why. Since the message box code appears after the loop is complete, it seems to me that it would only display the last value of output, not all 10. I am really new to visual programming, and I can't figure out why the display code is not inside the loop. My background is in C/C++ so I am having trouble figuring this out. Thanks for any help.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Question about program flow

    VB Code:
    1. Imports System.Windows.Forms
    2.  
    3. Module modInterest
    4.  
    5.    Sub Main()
    6.       Dim amount, principal As Decimal ' dollar amounts
    7.       Dim rate As Double               ' interest rate
    8.       Dim year As Integer              ' year counter
    9.       Dim output As String             ' amount after each year
    10.  
    11.       principal = 1000
    12.       rate = 0.05
    13.  
    14.       output = "Year" & vbTab & "Amount on deposit" & vbCrLf
    15.  
    16.       ' calculate amount after each year
    17.       For year = 1 To 10
    18.          amount = principal * (1 + rate) ^ year
    19.          'this is because the values are being appended to this string here
    20.          'notice the &= operator instead of just plain =
    21.          'this adds the string on to it, instead of setting it to the value
    22.          'if you were to change this to = then it would give you your expected
    23.          'result.
    24.          output &= year & vbTab & _
    25.             String.Format("{0:C}", amount) & vbCrLf
    26.       Next
    27.  
    28.       ' display output
    29.       MessageBox.Show(output, "Compound Interest", _
    30.          MessageBoxButtons.OK, MessageBoxIcon.Information)
    31.    End Sub  ' Main
    32.  
    33. End Module  ' modInterest

    If your a C/C++ developer you may be more at home with C# than VB.Net.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    4

    Re: Question about program flow

    Quote Originally Posted by <ABX
    If your a C/C++ developer you may be more at home with C# than VB.Net.
    But I want to learn VB.

    This is not a "help me pick a programming language" post. I had a question about VB program flow.

  4. #4
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Question about program flow

    I just added that as I thought it might help you, did you not notice the Extra comments I added to your code?
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    4

    Re: Question about program flow

    Quote Originally Posted by <ABX
    I just added that as I thought it might help you, did you not notice the Extra comments I added to your code?
    Ah, now I see, thank you. It is printing the final value of output, it is just that output contains several vbCrLfs. Thank you, and sorry I didn't notice your comments.

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