Beginner in VB needs help ;)?! - LOOPS

ok for a project i am creating a simple calculator but it has to use loops instead of the actual operators like 3 * 3 = 9 i have to create a loop using the for ... next loop that will instead of multiplying those numbers it will add 3; 3 times to = 9. The user enters a number in N1.text and N2.text then it takes N1 * N2 = answer.

I've been doing good up until i had to learn loops and i having a hard ass time grabbing the concept can anyone help me I know the terminology or "syntax" for writing the loops, but it's what is inside the loop and getting it to calculate the right numbers that i am having trouble completing.

...so confused and real frustrated right now.



Answer this question

Beginner in VB needs help ;)?! - LOOPS

  • marun11

    This is what he meant, using the syntax you may be more familiar with:

    Dim N1, N2, num,  sum As Integer
    N1 = Integer.Parse(txtN1.Text)
    N2 = Integer.Parse(txtN2.Text)
    For num = 1 To N1
        sum = sum + N2
    Next num
    lblOutput.Text = "N1 * N2 = " & sum
    lblCheck.Text = (N1 * N2).ToString 'this checks to see that result was correct



  • rock.aut

    Providing the calculation logic outside of the Button Click Event provides for calling that logic for other events – such as a Key Click

  • . That's why I gave it to you as function. You would want to provide a better function name that calc.

    Private Sub btnTimes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimes.Click

    If Validatation() = False Then
    Exit Sub
    End If
    ' Call the function with value x and y from the textboxes - calling ToString() on returned Integer. Text is a string value - you wouldn't get away without this in some instances, so best to do it right.
    lblOutput.Text =
    Calc(Integer.Parse(txtN1.Text), Integer.Parse(txtN2.Text)).ToString()

    End Sub

    ' Clearly will only work with integers - not much sense in this, provided only for example

    Public Function Calc(ByVal X As Integer, ByVal Y As Integer) As Integer

    Dim answer As Integer = 0

    ' Y x X

    For i As Integer = 1 To X

    answer += Y

    Next

    Return answer

    End Function

    Best of luck,

    Martin.


  • BBBXXX

    agh thank you so much! that helps me a lot!!!


  • hommer

    ' Clearly will only work with integers - not muich sense in this, provided only for example

    Public Function Calc(ByVal X As Integer, ByVal Y As Integer) As Integer

    Dim answer As Integer = 0

    ' Y x X

    For i As Integer = 1 To X

    answer += Y

    Next

    Return answer

    End Function

    Rgds,

    Martin.


  • kevM

    eh that confused the *** outta me ha sry :(

    I'm looking for something of this nature .. just um right and will actually do the answer right.

    Private Sub btnTimes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimes.Click
    If Validatation() = False Then
    Exit Sub
    End If

    Dim N1, N2, Num, Sum As Integer
    N1 = Integer.Parse(txtN1.Text)
    N2 = Integer.Parse(txtN2.Text)
    Sum = 0
    For Num = 0 To N1 Step N1
    Num = N1 + 0
    Sum = Num
    Next Num
    lblOutput.Text = "N1 * N2 = " & Sum
    End Sub

    that's my pathetic try at getting this right ..


  • Beginner in VB needs help ;)?! - LOOPS