Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / BASIC - For / Next loop counter variable is incremented by 1 after the final Next...

Author
Message
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 15th Mar 2013 14:25
I have been programming in basic for some years now and have always thought (assumed) that the For / Next counter variable will equal the 'To' value on exit from a loop:

For countvariable = 1 to 10
Print(countvariable)
Next

This does indeed print number 1 to 10, However after the loop has finished 'countvariable' = 11

Can this be right? I know that countvariable would need to be incremented to check if it exceeds the 'To' value but should it not be returned to 10 in the example above?

Might be me, but it just seems wrong with obvious issues in programming!

Cheers

Paul
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Mar 2013 14:52
If you break it down into C, it kinda makes a bit more sense:

FOR(int countvariable = 1; countvariable <= 10; countvariable ++)
{
}

So countvariable is set to 1, and as long as it's <= 10, it'll increse countvariable and do whatever is inside the {}.

So, when countvariable is 10, it still increases it, because the ++ part happens each time it checks the for..next variable. When countvariable > 10, the loop is skipped. Countvariable has to equal 11 (or >10) to skip the loop.

It's typically a good practice to only use your for..next variable within the loop, once the loop is done, disregard it.

I got a fever, and the only prescription, is more memes.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 15th Mar 2013 15:09
Rudders, as Van B says, this is the correct behavior.

If you want the countvariable to stop at 10, you need a test inside the loop that breaks out when it is equal to 10.

It is not uncommon to use a for loop to search for a specific value in an array and break out if the value or condition for the indexed element is met. And then test to see if the index variable is greater than the maximum to see if something happened.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 15th Mar 2013 15:19
Cheers Guys,

My assumption of being able to use the count variable outside the loop was clearly wrong, my mistake..

Cheers
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 15th Mar 2013 15:54
You can use the variable all you want outside of the loop, just not necessarily assuming any value associated with the initial loop.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 15th Mar 2013 16:27
generally, the value of a loop variable is undefined after a loop, and should be used without modification. Most compilers will warn about this.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 15th Mar 2013 16:43
I would think it was only undefined if the variable was only defined in the loop: for (int avar=0;avar<10;avar++)

If the variable is defined outside of the loop, it should maintain the last value assigned to it.

In Basic (AGK variant as well as others), I'm not sure if it adds a variable to the larger scope of the code around the for statement if it is only appears in a for statement. I suspect it does.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master

Login to post a reply

Server time is: 2024-05-07 22:02:33
Your offset time is: 2024-05-07 22:02:33