question

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 asked Hekzdaddy-3952 action

Problem with For...Next Loop

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
         MsgBox("success")
     Next
dotnet-visual-basic
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you want the success message to appear when the if-condition is true?

0 Votes 0 ·

Yes, only if all conditions are true. a PIN verifier with seven txtBoxes and each entry must be an integer and between a certain digit. I set up tryParse for each to check for an integer.

0 Votes 0 ·

If you want the success message to appear when the if-condition is true, then move MsgBox("success") inside of the if- statment.

0 Votes 0 ·
Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 answered Hekzdaddy-3952 commented

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
             MsgBox("success")
         ElseIf MsgBox("try again") Then
         End If
     Next
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I ran your code and I found that the try again message always appears. I modified your if-statment to:

 If intCount >= intMinPin(0) And intPin(0) <= intMaxPin(0) Then
          MsgBox("success")
 Else
          MsgBox("try again")
 End If
0 Votes 0 ·

doesnt work, thanks tho.

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered Hekzdaddy-3952 commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Public intMinPin() As Integer = {7, 5, 0, 0, 6, 3, 4}
Public intMaxPin() As Integer = {9, 7, 4, 9, 9, 6, 8}
Dim intCount As Integer

 Public intPin(6) As Integer

  1. the value for the textbox1 must be between 7&9, then textbox2 between 5&9 and so on...

  2. see above

  3. this is what I am having problems with so looking for help/advise.

  4. thanks for the example, i am learning it is an assignment for school and I am stuck, thats why it probably does not make sense.





0 Votes 0 ·

This seems to work but when I enter the something wrong into a textbox i still get the "success" msgbox, is it because my ValidateInputFields () is not in the If statement?


ValidateInputFields()

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

         If intPin(intCount) >= intMinPin(0) AndAlso dblEnteredNumber <= intMaxPin(0) Then
             ' intCount +=1
             MsgBox("success")

         End If

     Next

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered Hekzdaddy-3952 commented

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
earlier?

(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)

then

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

then

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

etc.

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

  • Wayne

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Not asking for it to be written out. What you are suggesting is good and makes me try and figure out the solution. I had the code below but the msgBoxes appear even if the txtBoxes are empty. MsgBox("wrong pin") appears 7 times then msgBox("Correct") appears. I understand what you are saying about comparing each intPin(0) to intMinPin(0) intMaxPin(0) and so on... thats is what I am trying to accomplish. I feel like I have 90% of what I need to do accomplished but I am missing something. Your explanations are helpful.

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

         If intPin(intCount) >= intMinPin(intCount) AndAlso intPin(intCount) <= intMaxPin(intCount) Then
             intCount += 1
         ElseIf MsgBox("wrong pin") Then

         End If


     Next
     MsgBox("success")

Thanks,
Hector.

0 Votes 0 ·

ElseIf MsgBox("wrong pin") Then

As was pointed out by an earlier poster, you should NOT
be using ElseIf as there is no comparison being done
on that line. It should be a simple Else statement.

  • Wayne

0 Votes 0 ·

Got it, I'll try that. Thanks for your input, I am new to this and really like it, but like anything else that is new it takes time and practice to learn it

0 Votes 0 ·

can you tell me what I am doing wrong here? 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,. I'm an not sure about the placement of my MsgBox, other than the the For... Next look is working properly.


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

         If intPin(intCount) >= intMinPin(intCount) AndAlso intPin(intCount) <= intMaxPin(intCount) Then
             MsgBox("success")
         Else

             MsgBox("check your pin")

         End If
     Next
0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered Hekzdaddy-3952 commented

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

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Option 1 sounds like it would work for me. It would make more sense if the loop fails to meet the criteria to END and display a message box at that time and not proceed, instead it continues. Grrrr... I am beyond frustrated with this For.. Loop statement, I know it is working and checking the equation but I am not seeing the results that I want.

I would like for the loop to STOP, msgBox appears and wont continue until the criteria has been met.

Thanks again.
Hector B.

0 Votes 0 ·

You can use this:

Exit For

to leave the For loop immediately.

  • Wayne

0 Votes 0 ·

Im pretty sure I tried it, should there be an expression following the exit for? It still doesn't fix my MsgBox problem... it seems to be a simple task but I am not grasping it.

Would exit for be after else? And what about the msgBox("success") if all values are correct. I understand I cant put that in the loop statement because I see it appear 7 times if everything is correct. Again, you mentioned that I must create a variable to check for the false entries? I.e ' Dim X as integer = 0? Where would I incorporate that?

0 Votes 0 ·
Show more comments