1. Senior Member
Join Date
Dec 1969
Posts
217

Why does the following code yields 1,2,4,7 instead of 1,2,3,4<BR>&#060;%<BR>Dim test <BR>test = 1<BR>For intCounter = 0 to 3 <BR> test = test + intCounter <BR> response.write test & "<BR>"<BR>Next %&#062;<BR> <BR>

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

## For loops are just shorthand...

SO let&#039;s "UNROLL" that loop:<BR><BR>test = 1<BR>For intCounter = 0 to 3 <BR> test = test + intCounter <BR> response.write test & "&#060;br/&#062;"<BR>Next <BR><BR>Into this:<BR><BR>**************<BR><BR>test = 1 &#039; from your code<BR><BR>&#039; logical start of for loop<BR>intCounter = 0 <BR>&#039; test was 1, then we add zero to it:<BR>test = test + intCounter <BR>&#039; so now test equals 1, and we confirm that<BR>response.write test & "&#060;br/&#062;"<BR><BR>&#039; "next" bumps intCounter to one more than it was<BR>intCounter = 1<BR>&#039; test was 1, then we add 1 to it:<BR>test = test + intCounter <BR>&#039; so now test equals 2, and we confirm that<BR>response.write test & "&#060;br/&#062;"<BR><BR>&#039; "next" bumps intCounter to one more than it was<BR>intCounter = 2<BR>&#039; test was 2, and we add 2 to it:<BR>test = test + intCounter <BR>&#039; so now test equals 4, and we confirm that<BR>response.write test & "&#060;br/&#062;"<BR><BR>&#039; "next" bumps intCounter to one more than it was<BR>intCounter = 3<BR>&#039; test was 4, and we add 3 to it:<BR>test = test + intCounter <BR>&#039; so now test equals 7, and we confirm that<BR>response.write test & "&#060;br/&#062;"<BR><BR>**********<BR><BR>See? The expected results. Same as you get from the FOR loop.<BR><BR>When you use <BR> test = test + xxxx<BR>you are *CHANGING* the original value of "test".<BR><BR>If you didn&#039;t want to do that you should have done<BR> test2 = text + xxx<BR> Response.Write test2<BR><BR>

#### Posting Permissions

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