Still Need FOR LOOP Help

Thread: Still Need FOR LOOP Help

1. Member
Join Date
Dec 1969
Posts
68

Still Need FOR LOOP Help

Here is some code: <BR><BR>For count=1 To thePeople(0) <BR>If Cint(count)&#060;&#062;Cint(Session("POS")) Then <BR>For x2=1 To numMatches <BR>If matchValues(count)&#062;=yourMatches(2,x2) Then <BR>For x3=numMatches To x2+1 Step -1 <BR>yourMatches(1,x3)=yourMatches(1,x3-1) <BR>yourMatches(2,x3)=yourMatches(2,x3-1) <BR>Next <BR>yourMatches(1,x3-1)=count <BR>yourMatches(2,x3-1)=matchValues(count) <BR>End If <BR>Next <BR>End If <BR>Next <BR><BR>Basically I am working with a 2D array here. The first for loop goes through all of the people in the database where thePeople(0) is the number of people following. We don&#039;t want to compare the user to his/herself, so if its a different person then we do the code. <BR><BR>For x2=1 To numMatches goes through 3 positions in the 2D array (as numMatches is set to 3) and if that persons value from another array is greater than or larger than what is stored in the 2D array, I want it to sort the rest down (ie, pos 3 goes to pos 2, 2 to 1) and the first place is replaced with new info. (3 is an example, it is a varying number depending on the amount of users so I must be using a loop form and not just code) <BR><BR>So basically what I need is a way to exit that loop with x2 because if I don&#039;t this is what will happen: <BR>Let&#039;s say the array is this to start: <BR>23 <BR>20 <BR>17 <BR>It will go through once and replace the new value (lets say its 40) into the top and move everything down (losing the last pos) <BR>40 <BR>23 <BR>20 (that works( <BR>But if I don&#039;t cancel the loop it will go again checking to see if its greater than the second position and so on and so forth thus filling 40 in the entire array. <BR>======================<BR>The help I have received said use Exit For, but that canceles both the inner and outer for. Any suggestions?<BR><BR>Thanks,<BR>Jordan

2. Senior Member
Join Date
Dec 1969
Posts
96,118

I believe you are mistaken

Exit For does *not* cancel any FOR loop except the innermost one that is *CURRENTLY* executing.<BR><BR>So in your code:<BR><BR>For count=1 To thePeople(0) <BR>&nbsp; &nbsp; If Cint(count)&#060;&#062;Cint(Session("POS")) Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; For x2=1 To numMatches <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If matchValues(count)&#062;=yourMatches(2,x2) Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; For x3=numMatches To x2+1 Step -1 <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(1,x3)=yourMatches(1,x3-1) <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(2,x3)=yourMatches(2,x3-1) <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IF WHATEVER THEN EXIT FOR &#039; exits only x3 loop<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Next <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(1,x3-1)=count <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(2,x3-1)=matchValues(count) <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IF WHATEVER THEN EXIT FOR &#039; exits x2 loop<BR>&nbsp; &nbsp; &nbsp; &nbsp; Next <BR>&nbsp; &nbsp; End If <BR>&nbsp; &nbsp; IF WHATEVER THEN EXIT FOR &#039; exits x1 loop<BR>Next <BR><BR>SO maybe you are just putting the exit for in the wrong spot in your code???<BR><BR>You *may* need to set a flag and then exit based on the flag.<BR><BR>Could you name your FOR loop variables something more enlightening than x1, x2, x3? It might make what you are trying to do more understandable. (I&#039;m a little lost.)<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

Looks to me like...

...you probably want the Exit For just before the next to last "END IF" statement?<BR><BR>For count=1 To thePeople(0) <BR>&nbsp; &nbsp; If Cint(count)&#060;&#062;Cint(Session("POS")) Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; For x2=1 To numMatches <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If matchValues(count) &#062;= yourMatches(2,x2) Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; For x3=numMatches To x2+1 Step -1 <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(1,x3)=yourMatches(1,x3-1) <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(2,x3)=yourMatches(2,x3-1) <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Next <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(1,x3-1) = count <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourMatches(2,x3-1) = matchValues(count) <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit For &#039; this will break out of x2 loop!<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If <BR>&nbsp; &nbsp; &nbsp; &nbsp; Next <BR>&nbsp; &nbsp; End If <BR>Next <BR><BR>

4. Member
Join Date
Dec 1969
Posts
68

RE: Looks to me like...

Thanks. You are always a help.<BR><BR>I am pretty sure I was putting it in the right spot but I will give it a try again.<BR><BR>It is kind of hard to explain because of the way my info is stored. x2,x3 are just meant to be counters just like &#039;i&#039; and &#039;count&#039; are...<BR><BR>Thanks, I&#039;ll give it a whirl.<BR><BR>Jordan

5. Member
Join Date
Dec 1969
Posts
68

RE: Looks to me like...

FIXED IT!<BR><BR>It was part some design flaw and part fiddling around with some code. I eventually got it working using a conditional to end the For Loop. Thanks for the help!<BR><BR>Jordan

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•