For...Next Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbhowell
    New Member
    • Apr 2010
    • 7

    For...Next Loop

    I'm trying to request a number, n, from 1 to 30 and one of the letters S or P. Then, depending upon whether S or P was selected, calculate the sum or product of the numbers from 1 to n. The calculations should be carried out in Funtion procedures.
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    And what code do you have so far? Look at the Rnd function in vb's help...

    Comment

    • yarbrough40
      Contributor
      • Jun 2009
      • 320

      #3
      It would also be helpful if you actually asked a question.

      Comment

      • jbhowell
        New Member
        • Apr 2010
        • 7

        #4
        I'm sorry my question is how do I get started. What I have so far isn't working which is no surprise. Below is what I have so far, please don't laugh, I'm new and learning but not well.

        Code:
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
                Dim num As String = ""
                For n = 1 To 30
                    num = txtNumber.Text
                Next
                CalculationS(num)
                CalculationP(CDbl(num))
            End Sub
            Function CalculationS(ByVal num As String) As String
                Dim calc As Double
                Dim s As String = ""
                If s = txtLetter.Text Then
                    calc = 1 + CDbl(num)
                End If
                Return CStr(calc)
            End Function
            Function CalculationP(ByVal num As Double) As Double
                Dim calc As Double
                Dim p As String = ""
                If p = txtLetter.Text Then
                    calc = 1 * num
                End If
                Return calc
            End Function
        End Class
        Thanks,
        Jennifer

        Comment

        • yarbrough40
          Contributor
          • Jun 2009
          • 320

          #5
          I'm still very confused by all this. Could you back up and explain a little better the whole thing. Start with what your inputs are and then what the desired outputs would be from those inputs...

          for example:
          I have this form "Form1" which has 2 textboxes: "txtNumber" , "txtLetter" . The user types a random number into txtNumber between 1 and 30 and then types either S or P into txtLetter - then presses a button. I would like for the app to take that number and do bla bla and then finally return bla to another textbox where the user can do bla.

          Comment

          • jbhowell
            New Member
            • Apr 2010
            • 7

            #6
            Originally posted by yarbrough40
            I'm still very confused by all this. Could you back up and explain a little better the whole thing. Start with what your inputs are and then what the desired outputs would be from those inputs...

            for example:
            I have this form "Form1" which has 2 textboxes: "txtNumber" , "txtLetter" . The user types a random number into txtNumber between 1 and 30 and then types either S or P into txtLetter - then presses a button. I would like for the app to take that number and do bla bla and then finally return bla to another textbox where the user can do bla.

            In a form I have a text box requesting the user to input a number between 1 and 30 as well as a letter p or s. When the button is clicked, depending on if p or s was selected, it should calculate the sum or product of the number from 1 to n. I'm supposed to use functions for the calculations. I've been working on it some more and have the below but it's still not right. Thanks for any advice you might have for me.
            Private Sub btnCalculate_Cl ick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCalculate.Cl ick
            Dim num As Double
            Dim calc As Double
            lstAnswer.Items .Clear()
            For n As Integer = 1 To 30
            num = CDbl(txtNumber. Text)
            Next
            CalculationS(nu m, calc)
            CalculationP(nu m, calc)
            lstAnswer.Items .Add(num & calc)
            End Sub
            Function CalculationS(By Val num As Double, ByVal calc As Double) As Double
            If txtLetter.Text = "S" Then
            calc = 1 + num
            End If
            Return calc
            lstAnswer.Items .Clear()
            End Function
            Function CalculationP(By Val num As Double, ByVal calc As Double) As Double
            If txtLetter.Text = "P" Then
            calc = 1 * num
            End If
            Return calc
            lstAnswer.Items .Clear()
            End Function

            Comment

            • patjones
              Recognized Expert Contributor
              • Jun 2007
              • 931

              #7
              I see a couple of problems here.

              First, you're testing for option "p" or "s" inside the functions that are supposedly going to do the calculating. Make the determination of p or s the first thing you do, and then call the appropriate function depending on the option.

              Second, your For...Next loop isn't really doing anything. It's just going to execute the assignment num = CDbl(txtNumber. Text) 30 times, giving you the same result for "num" each time. The way it is now, the calculations will take place outside the loop. These are accumulating calculations which must be done the number of times the loop specifies, and therefore placed inside the loop.

              Also, the loop is going to run from 1 to 30 all the time, the way you've written it...no matter what number the user inputs. If "n" is the numeric input, it seems you'd want something like

              Code:
              For j = 1 to n
                   'Calculations in here, probably depending on 'j'...
              Next

              Pat
              Last edited by patjones; Apr 16 '10, 04:42 AM. Reason: Change placement of a paragraph...

              Comment

              • jbhowell
                New Member
                • Apr 2010
                • 7

                #8
                Can you help with how to format the calculations? I've changed my code to the below but the calculations are way off, they keep giving me the answer 50 no matter if p or s is selected. Thanks!

                Private Sub btnCalculate_Cl ick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCalculate.Cl ick
                Dim num As Double = CDbl(txtNumber. Text)
                Dim sum, product As Double
                Dim n As Double
                lstAnswer.Items .Clear()
                If txtLetter.Text = "S" Then
                sum = CalculateS(n)
                lstAnswer.Items .Add(num & sum)
                ElseIf txtLetter.Text = "P" Then
                product = CalculationP(n)
                lstAnswer.Items .Add(num & product)
                End If
                End Sub
                Function CalculateS(ByVa l n As Double) As Double
                Dim result As Double
                For i As Integer = 1 To CInt(n)
                result += i

                Next
                Return result
                End Function
                Function CalculationP(By Val n As Double) As Double
                Dim result As Double
                For j As Integer = 1 To CInt(n)
                result += j
                result = n * j
                Next
                Return result
                End Function

                Comment

                • patjones
                  Recognized Expert Contributor
                  • Jun 2007
                  • 931

                  #9
                  I think this is a big improvement over your previous code.

                  First, don't you mean to pass "num" into the respective functions? As in sum = CalculateS(num) and product = CalculationP(nu m).

                  In function CalculateS I would initialize result = 0 before going into the For loop, just as a matter of good practice.

                  In CalculationP, I don't think that calculation is going to give the factorial. I would initialize result = 1 before the For loop, then put only result = result*j inside the loop.

                  Pat

                  Comment

                  • jbhowell
                    New Member
                    • Apr 2010
                    • 7

                    #10
                    Originally posted by zepphead80
                    I think this is a big improvement over your previous code.

                    First, don't you mean to pass "num" into the respective functions? As in sum = CalculateS(num) and product = CalculationP(nu m).

                    In function CalculateS I would initialize result = 0 before going into the For loop, just as a matter of good practice.

                    In CalculationP, I don't think that calculation is going to give the factorial. I would initialize result = 1 before the For loop, then put only result = result*j inside the loop.

                    Pat
                    That worked perfectly, thank you so much! It seems so simple which makes me feel so stupid. Thanks again!

                    Comment

                    Working...