Problem with For...Next Loop

asked 2021-04-20T17:28:00.927+00:00
Hekzdaddy 121 Reputation points

Hello all, I am having a problem with my For...Next Loop, even when the "If " statement is incorrect, I will get a "success" msg. I am not sure what I am doing right. Please advise.
IntPin(0) is an array with numbers from txtBoxes 1-7
IntMinPin is an array with integers
IntMaxPin is an array with integers

 For intCount As Integer = 0 To (intPin.Length - 1)

        If intCount = intPin(0) >= intMinPin(0) And intPin(0) <= intMaxPin(0) Then

        End If
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,825 questions
{count} votes

4 answers

Sort by: Most helpful
  1. answered 2021-04-20T18:08:51.207+00:00
    Hekzdaddy 121 Reputation points

    I did that here and I ended up getting the "try again" message 7 times, event after all integers were correct. I think i am missing the concept of intCount.

        For intCount As Integer = 0 To (intPin.Length - 1)
            If intCount >= intMinPin(0) And intPin(0) <= intMaxPin(0) Then
            ElseIf MsgBox("try again") Then
            End If

  2. answered 2021-04-20T23:48:03.647+00:00
    WayneAKing 4,246 Reputation points

    Since we have to guess what numbers are in the arrays,
    it's rather difficult to assess your code's execution
    path. However, there are some puzzling aspects of the
    code you posted that need clarification by you.

    (1) Why do you have an array of min values instead of just
    a single min value? Is the min value different for each
    digit of the pin?

    (2) The same question as (1) above can be asked about the
    max values. Why an array?

    (3) In the comparison:

    If intCount >= intMinPin(0) And intPin(0) <= intMaxPin(0) Then

    what sense does it make to compare the loop count variable
    intCount to the first min value in the array? Shouldn't you
    be comparing the value from the textbox in the intPin array
    to ensure that it is greater than or equal to the min value?

    (4) What sense does it make to compare the same values from
    intPin(0) and intMaxPin(0) on each iteration of the loop?
    Wouldn't it make more sense to compare each element in these
    arrays by using the intCount variable as an index? e.g. -

    If intPin(intCount) >= intMinPin(intCount) And intPin(intCount) <= intMaxPin(intCount) Then

    This assumes that the arrays of min and max values are actually
    needed. Obviously, if min and max values are the same for each
    digit of the pin being entered then arrays of values are not
    even needed.

    • Wayne

  3. answered 2021-04-21T05:05:09.053+00:00
    WayneAKing 4,246 Reputation points

    Because it's for a school assignment we should NOT give you
    complete code solutions.

    You seem to be just guessing instead of writing code that you
    understand clearly. The code you just posted suggests that you
    don't have a good grasp of what each step of the solution
    should be doing.

    (1) You are still comparing to just the first element in the
    min and max arrays, because you are still using index 0 on
    every iteration of the loop. Why aren't you using the
    loop count variable as I did in the line of code I posted

    (2) Why did you throw variable dblEnteredNumber into the mix?
    What does that accomplish? In the code you just posted it will
    contain the value from the first element of the intPin array
    ONLY. Why are you comparing THAT value on every iteration
    of the for loop instead of each successive integer from intPin
    in turn as the loop iterates - as you did for the min test?

    You should be checking the SAME number (digit) against both the
    min and max values.

    Your loop should compare

    intPin(0) against intMinPin(0) and intMaxPin(0)


    intPin(1) against intMinPin(1) and intMaxPin(1)


    intPin(2) against intMinPin(2) and intMaxPin(2)


    That's why you should be using the intCount variable as
    an index into all three arrays in the loop.

    • Wayne

  4. answered 2021-04-23T06:11:48.89+00:00
    WayneAKing 4,246 Reputation points

    My MsgBox("success") is repeated for each comparison.
    I only need it to show once, there is no need to tell
    the person entering the pin that each input is good,.

    Programming is about applied logic, so you should be
    thinking carefully and analytically about each
    activity of the code you write.

    It should be obvious to you why you are showing the
    messagebox multiple times: because it is inside the
    loop and gets shown every time a comparison is made
    on one of the digits/numbers of the entered PIN.

    So the solution should also be obvious: don't
    show the messagebox for each digit tested. Show
    it after all of the tests are completed. Which
    means you want to set an indicator that can be
    tested later instead of showing the messagebox
    every time a test is done.

    There are various ways to code that, the choice is
    yours and may depend on what you have learned to
    date. Some examples:

    (1) Use a simple integer variable declared before the
    loop begins with an initial value of zero. If a test
    fails set that variable to one (or add one to it,
    etc.). After the loop exits check the value of that
    variable to see if any of the individual tests failed
    and display the appropriate message.

    (2) The same sort of arrangement can be used but with
    a boolean variable instead of a simple integer. For
    example a boolean variable set to an initial state
    to indicate pass/fail via true/false, gets changed
    inside the loop based on the results of each test.

    • Wayne